WC_Helper_API::_authenticate()private staticWC 1.0

Adds authentication headers to an HTTP request.

Method of the class: WC_Helper_API{}

No Hooks.

Return

true|false. Were the headers added?

Usage

$result = WC_Helper_API::_authenticate( $url, $args );
$url(string) (required) (passed by reference — &)
The request URI.
$args(array) (required) (passed by reference — &)
By-ref, the args that will be passed to wp_remote_request().

WC_Helper_API::_authenticate() code WC 8.7.0

private static function _authenticate( &$url, &$args ) {
	$auth = WC_Helper_Options::get( 'auth' );

	if ( empty( $auth['access_token'] ) || empty( $auth['access_token_secret'] ) ) {
		return false;
	}

	$request_uri  = parse_url( $url, PHP_URL_PATH );
	$query_string = parse_url( $url, PHP_URL_QUERY );

	if ( is_string( $query_string ) ) {
		$request_uri .= '?' . $query_string;
	}

	$data = array(
		'host'        => parse_url( $url, PHP_URL_HOST ),
		'request_uri' => $request_uri,
		'method'      => ! empty( $args['method'] ) ? $args['method'] : 'GET',
	);

	if ( ! empty( $args['body'] ) ) {
		$data['body'] = $args['body'];
	}

	$signature = hash_hmac( 'sha256', json_encode( $data ), $auth['access_token_secret'] );
	if ( empty( $args['headers'] ) ) {
		$args['headers'] = array();
	}

	$headers         = array(
		'Authorization'   => 'Bearer ' . $auth['access_token'],
		'X-Woo-Signature' => $signature,
	);
	$args['headers'] = wp_parse_args( $headers, $args['headers'] );

	$url = add_query_arg(
		array(
			'token'     => $auth['access_token'],
			'signature' => $signature,
		),
		$url
	);

	return true;
}