WC_Helper::connect_with_password
Allows to connect with WCCOM using application password. used it to connect via CLI
Method of the class: WC_Helper{}
No Hooks.
Returns
null|WP_Error.
Usage
$result = WC_Helper::connect_with_password( $password );
- $password(string) (required)
- The application password.
WC_Helper::connect_with_password() WC Helper::connect with password code WC 10.4.3
public static function connect_with_password( string $password ) {
$request = WC_Helper_API::post(
'connect',
array(
'headers' => array(
'X-API-Key' => $password,
'Content-Type' => 'application/json',
),
'body' => wp_json_encode( array( 'home_url' => home_url() ) ),
'authenticated' => false,
)
);
$code = wp_remote_retrieve_response_code( $request );
if ( $code === 403 ) {
$message = 'Invalid password';
self::log( $message );
return new WP_Error( 'connect-with-password-invalid-password', $message );
} elseif ( $code !== 200 ) {
$message = sprintf( 'Call to /connect returned a non-200 response code (%d)', $code );
self::log( $message );
return new WP_Error( 'connect-with-password-' . $code, $message );
}
$access_data = json_decode( wp_remote_retrieve_body( $request ), true );
if ( empty( $access_data['access_token'] ) || empty( $access_data['access_token_secret'] ) ) {
$message = sprintf( 'Call to /connect returned an invalid body: %s', wp_remote_retrieve_body( $request ) );
self::log( $message );
return new WP_Error( 'connect-with-password-invalid-response', $message );
}
self::update_auth_option( $access_data['access_token'], $access_data['access_token_secret'], $access_data['site_id'], home_url() );
}