WC_Admin_Addons::fetch()public staticWC 1.0

Make wp_safe_remote_get request to WooCommerce.com endpoint. Optionally pass user auth token, locale or country.

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

Хуков нет.

Возвращает

Массив|WP_Error.

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

$result = WC_Admin_Addons::fetch( $url, $options );
$url(строка) (обязательный)
URL to request.
$options(?array)
Options for the request. For example, to pass auth token, locale and country, pass array( 'auth' => true, 'locale' => true, 'country' => true, ).
По умолчанию: array()

Код WC_Admin_Addons::fetch() WC 9.4.2

public static function fetch( $url, $options = array() ) {
	$headers = array();

	if ( isset( $options['auth'] ) && $options['auth'] ) {
		$auth = WC_Helper_Options::get( 'auth' );

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

	$parameters = array();

	if ( isset( $options['locale'] ) && $options['locale'] ) {
		$parameters['locale'] = get_user_locale();
	}

	if ( isset( $options['country'] ) && $options['country'] ) {
		$country = WC()->countries->get_base_country();
		if ( ! empty( $country ) ) {
			$parameters['country'] = $country;
		}
	}

	$query_string = ! empty( $parameters ) ? '?' . http_build_query( $parameters ) : '';

	return wp_safe_remote_get(
		$url . $query_string,
		array(
			'headers'    => $headers,
			'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
		)
	);
}