Skip to content

Commit

Permalink
Masterbar: send wpcom user ID to wpcom when attempting to log… (#13777)
Browse files Browse the repository at this point in the history
* Masterbar: send wpcom user ID to wpcom when attempting to log out

Fixes #13680

Until now, we would send a sync event to WordPress.com anytime someone attempted to log out from a site using the "Sign Out" button in the masterbar.
From now on, we'll only do that when we have info about the connected WordPress.com user linked to that local user, and we'll send their wpcom user ID to WordPress.com so WordPress.com can disconnect them (and no one else) from WordPress.com.

* Masterbar: hook into logout_redirect to handle post logout action

WP 5.3 changed the way logouts worked:
https://core.trac.wordpress.org/changeset/46467

As a result we cannot pull the user's ID as they are logging out via the `wp_logout` filter anymore, because by then their info has already been cleared.

Let's instead hooked into `logout_redirect`, where we still have info about the user that logged out.
  • Loading branch information
jeherve authored Mar 18, 2020
1 parent 4c93023 commit 84cd060
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
32 changes: 29 additions & 3 deletions modules/masterbar/masterbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Automattic\Jetpack\Assets;
use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;

require_once dirname( __FILE__ ) . '/rtl-admin-bar.php';

Expand Down Expand Up @@ -80,7 +81,7 @@ public function __construct() {
add_action( 'admin_bar_init', array( $this, 'init' ) );

// Post logout on the site, also log the user out of WordPress.com.
add_action( 'wp_logout', array( $this, 'maybe_logout_user_from_wpcom' ) );
add_filter( 'logout_redirect', array( $this, 'maybe_logout_user_from_wpcom' ), 10, 3 );
}

/**
Expand Down Expand Up @@ -166,8 +167,12 @@ class_exists( 'Jetpack_AMP_Support' )

/**
* Log out from WordPress.com when logging out of the local site.
*
* @param string $redirect_to The redirect destination URL.
* @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
* @param WP_User $user The WP_User object for the user that's logging out.
*/
public function maybe_logout_user_from_wpcom() {
public function maybe_logout_user_from_wpcom( $redirect_to, $requested_redirect_to, $user ) {
/**
* Whether we should sign out from wpcom too when signing out from the masterbar.
*
Expand All @@ -182,8 +187,29 @@ public function maybe_logout_user_from_wpcom() {
&& 'masterbar' === $_GET['context'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended
&& $masterbar_should_logout_from_wpcom
) {
do_action( 'wp_masterbar_logout' );
/*
* Get the associated WordPress.com User ID, if the user is connected.
*/
$connection_manager = new Connection_Manager();
if ( $connection_manager->is_user_connected( $user->ID ) ) {
$wpcom_user_data = $connection_manager->get_connected_user_data( $user->ID );
if ( ! empty( $wpcom_user_data['ID'] ) ) {
/**
* Hook into the log out event happening from the Masterbar.
*
* @since 5.1.0
* @since 7.9.0 Added the $wpcom_user_id parameter to the action.
*
* @module masterbar
*
* @param int $wpcom_user_id WordPress.com User ID.
*/
do_action( 'wp_masterbar_logout', $wpcom_user_data['ID'] );
}
}
}

return $redirect_to;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sync/src/modules/class-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function init_listeners( $callable ) {
add_action( 'jetpack_wp_login', $callable, 10, 3 );

add_action( 'wp_logout', $callable, 10, 0 );
add_action( 'wp_masterbar_logout', $callable, 10, 0 );
add_action( 'wp_masterbar_logout', $callable, 10, 1 );

// Add on init.
add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_add_user', array( $this, 'expand_action' ) );
Expand Down

0 comments on commit 84cd060

Please sign in to comment.