WC_Admin_Addons::get_extension_data()public staticWC 1.0

Call API to get extensions

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

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

Возвращает

Объект|WP_Error. Object with products and promotions properties, or WP_Error

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

$result = WC_Admin_Addons::get_extension_data( $category, $term, $country );
$category(строка) (обязательный)
Addon (sub) category.
$term(строка) (обязательный)
Search terms.
$country(строка) (обязательный)
Store country.

Код WC_Admin_Addons::get_extension_data() WC 7.3.0

public static function get_extension_data( $category, $term, $country ) {
	$parameters = self::build_parameter_string( $category, $term, $country );

	$headers = array();
	$auth    = WC_Helper_Options::get( 'auth' );

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

	$raw_extensions = wp_safe_remote_get(
		'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' . $parameters,
		array(
			'headers'    => $headers,
			'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
		)
	);

	if ( is_wp_error( $raw_extensions ) ) {
		do_action( 'woocommerce_page_wc-addons_connection_error', $raw_extensions->get_error_message() );
		return $raw_extensions;
	}

	$response_code = (int) wp_remote_retrieve_response_code( $raw_extensions );
	if ( 200 !== $response_code ) {
		do_action( 'woocommerce_page_wc-addons_connection_error', $response_code );
		return new WP_Error(
			'error',
			sprintf(
				esc_html(
					/* translators: Error code  */
					__( 'Our request to the search API got response code %s.', 'woocommerce' )
				),
				$response_code
			)
		);
	}

	$addons = json_decode( wp_remote_retrieve_body( $raw_extensions ) );

	if ( ! is_object( $addons ) || ! isset( $addons->products ) ) {
		do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' );
		return new WP_Error( 'error', __( 'Our request to the search API got a malformed response.', 'woocommerce' ) );
	}
	return $addons;
}