WC_Admin_Addons::fetch_featured()public staticWC 1.0

Fetch featured products from WCCOM's the Featured 3.0 Endpoint and cache the data for a day.

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

Хуки из метода

Возвращает

Массив|WP_Error.

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

$result = WC_Admin_Addons::fetch_featured();

Код WC_Admin_Addons::fetch_featured() WC 9.3.3

public static function fetch_featured() {
	$transient_name = 'wc_addons_featured';
	// Important: WCCOM Extensions API v3.0 is used.
	$url      = 'https://woocommerce.com/wp-json/wccom-extensions/3.0/featured';
	$locale   = get_user_locale();
	$featured = self::get_locale_data_from_transient( $transient_name, $locale );

	if ( false === $featured ) {
		$fetch_options = array(
			'auth'    => true,
			'locale'  => true,
			'country' => true,
		);
		$raw_featured  = self::fetch( $url, $fetch_options );

		if ( is_wp_error( $raw_featured ) ) {
			do_action( 'woocommerce_page_wc-addons_connection_error', $raw_featured->get_error_message() );

			$message = self::is_ssl_error( $raw_featured->get_error_message() )
				? __( 'We encountered an SSL error. Please ensure your site supports TLS version 1.2 or above.', 'woocommerce' )
				: $raw_featured->get_error_message();

			return new WP_Error( 'wc-addons-connection-error', $message );
		}

		$response_code = (int) wp_remote_retrieve_response_code( $raw_featured );
		if ( 200 !== $response_code ) {
			do_action( 'woocommerce_page_wc-addons_connection_error', $response_code );

			/* translators: %d: HTTP error code. */
			$message = sprintf(
				esc_html(
					/* translators: Error code  */
					__(
						'Our request to the featured API got error code %d.',
						'woocommerce'
					)
				),
				$response_code
			);

			return new WP_Error( 'wc-addons-connection-error', $message );
		}

		$featured = json_decode( wp_remote_retrieve_body( $raw_featured ) );
		if ( empty( $featured ) || ! is_array( $featured ) ) {
			do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' );
			$message = __( 'Our request to the featured API got a malformed response.', 'woocommerce' );

			return new WP_Error( 'wc-addons-connection-error', $message );
		}

		if ( $featured ) {
			self::set_locale_data_in_transient( $transient_name, $featured, $locale, DAY_IN_SECONDS );
		}
	}

	return $featured;
}