WC_Helper::connect_with_password()public staticWC 1.0

Allows to connect with WCCOM using application password. used it to connect via CLI

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

Хуков нет.

Возвращает

null|WP_Error.

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

$result = WC_Helper::connect_with_password( $password );
$password(строка) (обязательный)
The application password.

Код WC_Helper::connect_with_password() WC 9.4.2

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'] );
}