Skip to content

Commit

Permalink
Merge pull request #2449 from woocommerce/tweak/handle-wp-error-revok…
Browse files Browse the repository at this point in the history
…e-token-deactivating-plugin

[API Pull] Handle exception when deactivating the plugin and the WPCOM token does not exist.
  • Loading branch information
jorgemd24 authored Jul 4, 2024
2 parents b6fcae8 + dc34f56 commit 13b47f0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
11 changes: 10 additions & 1 deletion src/API/WP/OAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ 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 ) {
do_action(
'woocommerce_gla_error',
sprintf( 'Error revoking the WPCOM token: %s', $e->getMessage() ),
__METHOD__
);
}
}
}
26 changes: 0 additions & 26 deletions src/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,14 +748,6 @@ protected function render_admin_page() {
</td>
</tr>
<?php } ?>
<tr>
<th><label>Revoke WPCOM Partner Access:</label></th>
<td>
<p>
<a class="button" href="<?php echo esc_url( wp_nonce_url( add_query_arg( [ 'action' => 'revoke-wpcom-partner-access' ], $url ), 'revoke-wpcom-partner-access' ) ); ?>">Revoke WPCOM Partner Access</a>
</p>
</td>
</tr>
</table>
<?php wp_nonce_field( 'partner-notification' ); ?>
<input name="page" value="connection-test-admin-page" type="hidden" />
Expand Down Expand Up @@ -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 );
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/API/WP/OAuthServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,17 @@ 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' ) );

$this->account_service->expects( $this->never() )
->method( 'reset_wpcom_api_authorization_data' );

// The exception should be caught and ignored.
$this->service->deactivate();

$this->assertEquals( 1, did_action( 'woocommerce_gla_error' ) );
}

/**
Expand Down

0 comments on commit 13b47f0

Please sign in to comment.