Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Pull] Handle exception when deactivating the plugin and the WPCOM token does not exist. #2449

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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