From 906bc4e5bd44a44759e9940a9892329df64cdbce Mon Sep 17 00:00:00 2001 From: Jorge M Date: Wed, 3 Jul 2024 10:54:16 +0200 Subject: [PATCH 1/4] Catch exception when revoking api token --- src/API/WP/OAuthService.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/API/WP/OAuthService.php b/src/API/WP/OAuthService.php index 7773366ce8..f40d95a1b9 100644 --- a/src/API/WP/OAuthService.php +++ b/src/API/WP/OAuthService.php @@ -180,6 +180,11 @@ public function revoke_wpcom_api_auth(): string { * Revoke token on deactivation. */ public function deactivate(): void { - $this->revoke_wpcom_api_auth(); + // Try to revoke the token on deactivation. If no token is available, it will throw an exception which we can ignore. + try { + $this->revoke_wpcom_api_auth(); + } catch ( Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch + // Do nothing. + } } } From ecefb154b91c509e68acd14df82be972152056ed Mon Sep 17 00:00:00 2001 From: Jorge M Date: Wed, 3 Jul 2024 10:54:30 +0200 Subject: [PATCH 2/4] Adjust tests when revoking token --- tests/Unit/API/WP/OAuthServiceTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Unit/API/WP/OAuthServiceTest.php b/tests/Unit/API/WP/OAuthServiceTest.php index 40e8fb342e..9210f07b67 100644 --- a/tests/Unit/API/WP/OAuthServiceTest.php +++ b/tests/Unit/API/WP/OAuthServiceTest.php @@ -123,8 +123,6 @@ public function test_deactivation_ok() { public function test_deactivation_with_wp_error() { $this->assertInstanceOf( Deactivateable::class, $this->service ); - $this->expectException( Exception::class ); - $this->expectExceptionMessage( 'error message' ); $this->jp->expects( $this->once() ) ->method( 'remote_request' )->willReturn( new WP_Error( 'error', 'error message' ) ); @@ -132,6 +130,7 @@ public function test_deactivation_with_wp_error() { $this->account_service->expects( $this->never() ) ->method( 'reset_wpcom_api_authorization_data' ); + // The exception should be caught and ignored. $this->service->deactivate(); } From b55803b95762132ca6029a14dbfcbae27384eda2 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Wed, 3 Jul 2024 10:55:32 +0200 Subject: [PATCH 3/4] Remove redundant revoke token --- src/ConnectionTest.php | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/src/ConnectionTest.php b/src/ConnectionTest.php index 97d70e0081..cae3bf4a9b 100644 --- a/src/ConnectionTest.php +++ b/src/ConnectionTest.php @@ -748,14 +748,6 @@ protected function render_admin_page() { - - - -

- Revoke WPCOM Partner Access -

- - @@ -889,24 +881,6 @@ protected function handle_actions() { } - if ( 'revoke-wpcom-partner-access' === $_GET['action'] && check_admin_referer( 'revoke-wpcom-partner-access' ) ) { - - $revoke_args = [ - 'method' => 'DELETE', - 'timeout' => 30, - 'url' => 'https://public-api.wordpress.com/wpcom/v2/sites/' . Jetpack_Options::get_option( 'id' ) . '/wc/partners/google/revoke-token', - 'user_id' => get_current_user_id(), - ]; - - $revoke_response = Client::remote_request( $revoke_args, null ); - - if ( is_wp_error( $revoke_response ) ) { - $this->response .= $revoke_response->get_error_message(); - } else { - $this->response .= wp_remote_retrieve_body( $revoke_response ); - } - } - if ( 'disconnect-wp-api' === $_GET['action'] && check_admin_referer( 'disconnect-wp-api' ) ) { $request = new Request( 'DELETE', '/wc/gla/rest-api/authorize' ); $this->send_rest_request( $request ); From dc34f563ffa220a58bcc23cdd63f28585371cc6c Mon Sep 17 00:00:00 2001 From: Jorge M Date: Wed, 3 Jul 2024 11:21:42 +0200 Subject: [PATCH 4/4] Log deactivate error --- src/API/WP/OAuthService.php | 8 ++++++-- tests/Unit/API/WP/OAuthServiceTest.php | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/API/WP/OAuthService.php b/src/API/WP/OAuthService.php index f40d95a1b9..14b9d6d33d 100644 --- a/src/API/WP/OAuthService.php +++ b/src/API/WP/OAuthService.php @@ -183,8 +183,12 @@ public function deactivate(): void { // Try to revoke the token on deactivation. If no token is available, it will throw an exception which we can ignore. try { $this->revoke_wpcom_api_auth(); - } catch ( Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch - // Do nothing. + } catch ( Exception $e ) { + do_action( + 'woocommerce_gla_error', + sprintf( 'Error revoking the WPCOM token: %s', $e->getMessage() ), + __METHOD__ + ); } } } diff --git a/tests/Unit/API/WP/OAuthServiceTest.php b/tests/Unit/API/WP/OAuthServiceTest.php index 9210f07b67..59ec5b08de 100644 --- a/tests/Unit/API/WP/OAuthServiceTest.php +++ b/tests/Unit/API/WP/OAuthServiceTest.php @@ -132,6 +132,8 @@ public function test_deactivation_with_wp_error() { // The exception should be caught and ignored. $this->service->deactivate(); + + $this->assertEquals( 1, did_action( 'woocommerce_gla_error' ) ); } /**