WC_Helper::get_subscriptionspublic staticWC 1.0

Get the connected user's subscriptions.

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

Хуков нет.

Возвращает

Массив.

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

$result = WC_Helper::get_subscriptions();

Код WC_Helper::get_subscriptions() WC 9.9.3

public static function get_subscriptions() {
	$cache_key = '_woocommerce_helper_subscriptions';
	$data      = get_transient( $cache_key );
	if ( false !== $data ) {
		return $data;
	}

	try {
		$request_uri = wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		$source      = '';
		if ( stripos( $request_uri, 'wc-addons' ) ) :
			$source = 'my-subscriptions';
		elseif ( stripos( $request_uri, 'plugins.php' ) ) :
			$source = 'plugins';
		elseif ( stripos( $request_uri, 'wc-admin' ) ) :
			$source = 'inbox-notes';
		elseif ( stripos( $request_uri, 'admin-ajax.php' ) ) :
			$source = 'heartbeat-api';
		elseif ( stripos( $request_uri, 'installer' ) ) :
			$source = 'wccom-site-installer';
		elseif ( defined( 'WP_CLI' ) && WP_CLI ) :
			$source = 'wc-cli';
		endif;

		// Obtain the connected user info.
		$request = WC_Helper_API::get(
			'subscriptions',
			array(
				'authenticated' => true,
				'query_string'  => '' !== $source ? esc_url( '?source=' . $source ) : '',
			)
		);

		if ( is_wp_error( $request ) ) {
			set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS );

			throw new Exception( $request->get_error_message() );
		}

		$code = wp_remote_retrieve_response_code( $request );
		if ( 200 !== $code ) {
			set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS );

			throw new Exception( self::get_message_for_response_code( $code ) );
		}

		$data = json_decode( wp_remote_retrieve_body( $request ), true );
		if ( ! is_array( $data ) ) {
			set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS );

			throw new Exception( __( 'WooCommerce.com API returned an invalid response.', 'woocommerce' ) );
		}

		set_transient( $cache_key, $data, 1 * HOUR_IN_SECONDS );
		return $data;
	} catch ( Exception $e ) {
		self::log( 'Error getting subscriptions: ' . $e->getMessage(), 'error' );
		throw $e;
	}
}