WC_Helper::get_subscription_list_data()public staticWC 1.0

Get the connected user's subscription list data. Here, we merge connected subscriptions with locally installed Woo plugins and themes. We also add in information about available updates.

Used by the My Subscriptions page.

Метод класса: WC_Helper{}

Хуков нет.

Возвращает

Массив.

Использование

$result = WC_Helper::get_subscription_list_data();

Код WC_Helper::get_subscription_list_data() WC 9.4.2

public static function get_subscription_list_data() {
	// First, connected subscriptions.
	$subscriptions = self::get_subscriptions();

	// Then, installed plugins and themes, with or without an active subscription.
	$woo_plugins = self::get_local_woo_plugins();
	$woo_themes  = self::get_local_woo_themes();

	// Get the product IDs of the subscriptions.
	$subscriptions_product_ids = wp_list_pluck( $subscriptions, 'product_id' );

	// Get the site ID.
	$auth    = WC_Helper_Options::get( 'auth' );
	$site_id = isset( $auth['site_id'] ) ? absint( $auth['site_id'] ) : 0;

	// Now, merge installed products without a subscription.
	foreach ( array_merge( $woo_plugins, $woo_themes ) as $filename => $data ) {
		if ( in_array( $data['_product_id'], $subscriptions_product_ids, true ) ) {
			continue;
		}

		// We add these as subscriptions to the previous connected subscriptions list.
		$subscriptions[] = array(
			'product_key'       => '',
			'product_id'        => $data['_product_id'],
			'product_name'      => $data['Name'],
			'product_url'       => $data['PluginURI'] ?? '',
			'zip_slug'          => $data['slug'],
			'documentation_url' => '',
			'key_type'          => '',
			'key_type_label'    => '',
			'lifetime'          => false,
			'product_status'    => 'publish',
			// Connections is empty because this is not a connected subscription.
			'connections'       => array(),
			'expires'           => 0,
			'expired'           => true,
			'expiring'          => false,
			'sites_max'         => 0,
			'sites_active'      => 0,
			'autorenew'         => false,
			'maxed'             => false,
		);
	}

	// Fetch updates so we can refine subscriptions with information about updates.
	$updates = WC_Helper_Updater::get_update_data();

	// Add local data to merged subscriptions list (both locally installed and purchased).
	foreach ( $subscriptions as &$subscription ) {
		$subscription['active'] = in_array( $site_id, $subscription['connections'], true );

		$subscription['local'] = self::get_subscription_local_data( $subscription );

		$subscription['has_update'] = false;
		if ( $subscription['local']['installed'] && ! empty( $updates[ $subscription['product_id'] ] ) ) {
			$subscription['has_update'] = version_compare( $updates[ $subscription['product_id'] ]['version'], $subscription['local']['version'], '>' );
		}

		if ( ! empty( $updates[ $subscription['product_id'] ] ) ) {
			$subscription['version'] = $updates[ $subscription['product_id'] ]['version'];
		}

		// If the update endpoint returns a URL, we prefer it over the default PluginURI.
		if ( ! empty( $updates[ $subscription['product_id'] ]['url'] ) ) {
			$subscription['product_url'] = $updates[ $subscription['product_id'] ]['url'];
		}
	}

	// Sort subscriptions by name and expiration date.
	usort(
		$subscriptions,
		function ( $a, $b ) {
			$compare_value = strcasecmp( $a['product_name'], $b['product_name'] );
			if ( 0 === $compare_value ) {
				return strcasecmp( $a['expires'], $b['expires'] );
			}
			return $compare_value;
		}
	);

	// Add subscription install flags after the active and local data is set.
	foreach ( $subscriptions as &$subscription ) {
		$subscription['subscription_available'] = self::is_subscription_available( $subscription, $subscriptions );
		$subscription['subscription_installed'] = self::is_subscription_installed( $subscription, $subscriptions );
	}

	// Break the by-ref.
	unset( $subscription );

	return $subscriptions;
}