Add `wc com disconnect` command (#33999)

* extract `disconnect` function

* extract `is_site_connected` function

* add `wc com disconnect` command

* add changelog

* fix phpcs errors
This commit is contained in:
Rommel Castro 2022-08-15 06:54:55 -06:00 committed by GitHub
parent de46baf8fc
commit 2ab9398167
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 13 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: dev
Adds `wc com disconnect` command to allow store being disconnected from WooCommerce.com via CLI

View File

@ -71,7 +71,7 @@ class WC_Helper {
$notices = self::_get_return_notices();
// No active connection.
if ( empty( $auth['access_token'] ) ) {
if ( ! self::is_site_connected() ) {
$connect_url = add_query_arg(
array(
'page' => 'wc-addons',
@ -884,18 +884,7 @@ class WC_Helper {
admin_url( 'admin.php' )
);
WC_Helper_API::post(
'oauth/invalidate_token',
array(
'authenticated' => true,
)
);
WC_Helper_Options::update( 'auth', array() );
WC_Helper_Options::update( 'auth_user_data', array() );
self::_flush_subscriptions_cache();
self::_flush_updates_cache();
self::disconnect();
wp_safe_redirect( $redirect_uri );
die();
@ -1676,6 +1665,38 @@ class WC_Helper {
self::$log->log( $level, $message, array( 'source' => 'helper' ) );
}
/**
* Handles WC Helper disconnect tasks.
*
* @return void
*/
public static function disconnect() {
WC_Helper_API::post(
'oauth/invalidate_token',
array(
'authenticated' => true,
)
);
WC_Helper_Options::update( 'auth', array() );
WC_Helper_Options::update( 'auth_user_data', array() );
self::_flush_subscriptions_cache();
self::_flush_updates_cache();
}
/**
* Checks if `access_token` exists in `auth` option.
*
* @return bool
*/
public static function is_site_connected(): bool {
$auth = WC_Helper_Options::get( 'auth' );
// If `access_token` is empty, there's no active connection.
return ! empty( $auth['access_token'] );
}
}
WC_Helper::load();

View File

@ -21,6 +21,7 @@ class WC_CLI_COM_Command {
*/
public static function register_commands() {
WP_CLI::add_command( 'wc com extension list', array( 'WC_CLI_COM_Command', 'list_extensions' ) );
WP_CLI::add_command( 'wc com disconnect', array( 'WC_CLI_COM_Command', 'disconnect' ) );
}
/**
@ -85,4 +86,34 @@ class WC_CLI_COM_Command {
$formatter->display_items( $data );
}
/**
* ## OPTIONS
*
* [--yes]
* : Do not prompt for confirmation.
*
* ## EXAMPLES
*
* # Disconnect from site.
* $ wp wc com disconnect
*
* # Disconnect without prompt for confirmation.
* $ wp wc com disconnect --yes
*
* @param array $args Positional arguments to include when calling the command.
* @param array $assoc_args Associative arguments to include when calling the command.
* @return void
* @throws \WP_CLI\ExitException If WP_CLI::$capture_exit is true.
*/
public static function disconnect( array $args, array $assoc_args ) {
if ( ! WC_Helper::is_site_connected() ) {
WP_CLI::error( 'Your store is not connected to WooCommerce.com. Run `wp wc com connect` command.' );
}
WP_CLI::confirm( 'Are you sure you want to disconnect your store from WooCommerce.com?', $assoc_args );
WC_Helper::disconnect();
WP_CLI::success( 'You have successfully disconnected your store from WooCommerce.com' );
}
}