WC_Admin_Addons::render_featured()public staticWC 1.0

Render featured products and banners using WCCOM's the Featured 2.0 Endpoint

{} It's a method of the class: WC_Admin_Addons{}

Return

null. Nothing.

Usage

$result = WC_Admin_Addons::render_featured();

Code of WC_Admin_Addons::render_featured() WC 6.7.0

public static function render_featured() {
	$featured = get_transient( 'wc_addons_featured' );
	if ( false === $featured ) {
		$headers = array();
		$auth    = WC_Helper_Options::get( 'auth' );

		if ( ! empty( $auth['access_token'] ) ) {
			$headers['Authorization'] = 'Bearer ' . $auth['access_token'];
		}

		$parameter_string = '';
		$country          = WC()->countries->get_base_country();
		if ( ! empty( $country ) ) {
			$parameter_string = '?' . http_build_query( array( 'country' => $country ) );
		}

		// Important: WCCOM Extensions API v2.0 is used.
		$raw_featured = wp_safe_remote_get(
			'https://woocommerce.com/wp-json/wccom-extensions/2.0/featured' . $parameter_string,
			array(
				'headers' => $headers,
			)
		);

		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();

			self::output_empty( $message );

			return;
		}

		$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
			);

			self::output_empty( $message );

			return;
		}

		$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' );
			self::output_empty( $message );

			return;
		}

		if ( $featured ) {
			set_transient( 'wc_addons_featured', $featured, DAY_IN_SECONDS );
		}
	}

	self::output_featured( $featured );
}