From 1da26bc3592670d963fbbf7ffa1bdcf774e6228a Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 28 Aug 2015 11:01:59 +0200 Subject: [PATCH 1/4] Pass the current `Connectors` instance to the `wp_stream_after_connectors_registration` action. --- classes/class-connectors.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/classes/class-connectors.php b/classes/class-connectors.php index d54fe0490..0307f842c 100644 --- a/classes/class-connectors.php +++ b/classes/class-connectors.php @@ -178,13 +178,14 @@ public function load_connectors() { ); } - $connectors = $this->term_labels['stream_connector']; + $labels = $this->term_labels['stream_connector']; /** * Fires after all connectors have been registered. * - * @param array $connectors All register connectors labels array + * @param array $labels All register connectors labels array + * @param Connectors $connectors The Connectors object */ - do_action( 'wp_stream_after_connectors_registration', $connectors ); + do_action( 'wp_stream_after_connectors_registration', $labels, $this ); } } From 55a7765a0b665adbbe22634f63d0d96c945d9688 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 28 Aug 2015 11:03:53 +0200 Subject: [PATCH 2/4] Don't re-instantiate every connector class when adding actions. This allows connector actions to be unhooked, which it's currently impossible to do. --- classes/class-connector.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/classes/class-connector.php b/classes/class-connector.php index ade0ae61d..e99debd4c 100644 --- a/classes/class-connector.php +++ b/classes/class-connector.php @@ -34,14 +34,11 @@ abstract class Connector { * Register all context hooks */ public function register() { - $class_name = get_called_class(); - $class = new $class_name; - - foreach ( $class->actions as $action ) { - add_action( $action, array( $class, 'callback' ), null, 5 ); + foreach ( $this->actions as $action ) { + add_action( $action, array( $this, 'callback' ), 10, 5 ); } - add_filter( 'wp_stream_action_links_' . $class->name, array( $class, 'action_links' ), 10, 2 ); + add_filter( 'wp_stream_action_links_' . $this->name, array( $this, 'action_links' ), 10, 2 ); } /** From 49f86f22084686485814bdfc7c5c5fb0e9257250 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 28 Aug 2015 11:14:58 +0200 Subject: [PATCH 3/4] Implement a connector for the User Switching plugin. --- classes/class-connectors.php | 1 + connectors/class-connector-user-switching.php | 232 ++++++++++++++++++ connectors/class-connector-users.php | 3 + 3 files changed, 236 insertions(+) create mode 100644 connectors/class-connector-user-switching.php diff --git a/classes/class-connectors.php b/classes/class-connectors.php index 0307f842c..fd812d981 100644 --- a/classes/class-connectors.php +++ b/classes/class-connectors.php @@ -76,6 +76,7 @@ public function load_connectors() { 'edd', 'gravityforms', 'jetpack', + 'user-switching', 'woocommerce', 'wordpress-seo', ); diff --git a/connectors/class-connector-user-switching.php b/connectors/class-connector-user-switching.php new file mode 100644 index 000000000..712af939f --- /dev/null +++ b/connectors/class-connector-user-switching.php @@ -0,0 +1,232 @@ +unhook_user_action( $connectors, 'clear_auth_cookie' ); + $this->unhook_user_action( $connectors, 'set_logged_in_cookie' ); + break; + + case 'switch_to_olduser': + $this->unhook_user_action( $connectors, 'clear_auth_cookie' ); + $this->unhook_user_action( $connectors, 'set_logged_in_cookie' ); + break; + + case 'switch_off': + $this->unhook_user_action( $connectors, 'clear_auth_cookie' ); + break; + + } + + } + + /** + * Fired when a user switches user with the User Switching plugin. + * + * @param int $user_id The ID of the user being switched to. + * @param int $old_user_id The ID of the user being switched from. + */ + public function callback_switch_to_user( $user_id, $old_user_id ) { + + $user = get_userdata( $user_id ); + $old_user = get_userdata( $old_user_id ); + $message = _x( + 'Switched user to %1$s (%2$s)', + '1: User display name, 2: User login', + 'stream' + ); + + $this->log( + $message, + array( + 'display_name' => $user->display_name, + 'user_login' => $user->user_login, + ), + $old_user->ID, + 'sessions', + 'switched-to', + $old_user->ID + ); + + } + + /** + * Fired when a user switches back to their previous user account with the User Switching plugin. + * + * @param int $user_id The ID of the user being switched back to. + * @param int|false $old_user_id The ID of the user being switched from, or false if the user is switching back + * after having been switched off. + */ + public function callback_switch_back_user( $user_id, $old_user_id ) { + + $user = get_userdata( $user_id ); + $old_user = get_userdata( $old_user_id ); + $message = _x( + 'Switched back to %1$s (%2$s)', + '1: User display name, 2: User login', + 'stream' + ); + + $this->log( + $message, + array( + 'display_name' => $user->display_name, + 'user_login' => $user->user_login, + ), + $old_user->ID, + 'sessions', + 'switched-back', + $old_user->ID + ); + + } + + /** + * Fired when a user switches off with the User Switching plugin. + * + * @param int $old_user_id The ID of the user switching off. + */ + public function callback_switch_off_user( $old_user_id ) { + + $old_user = get_userdata( $old_user_id ); + $message = __( + 'Switched off', + 'stream' + ); + + $this->log( + $message, + array(), + $old_user->ID, + 'sessions', + 'switched-off', + $old_user->ID + ); + + } + + /** + * Unhook the requested action from the Users connector. + * + * @param Connectors $connectors The Connectors instance + * @param string $action The name of the action to unhook + */ + protected function unhook_user_action( Connectors $connectors, $action ) { + foreach ( $connectors->connectors as $connector ) { + if ( 'users' === $connector->name ) { + remove_action( $action, array( $connector, 'callback' ) ); + } + } + } + +} diff --git a/connectors/class-connector-users.php b/connectors/class-connector-users.php index 5eb52d434..1d61242a0 100644 --- a/connectors/class-connector-users.php +++ b/connectors/class-connector-users.php @@ -55,6 +55,9 @@ public function get_action_labels() { 'forgot-password' => esc_html__( 'Lost Password', 'stream' ), 'login' => esc_html__( 'Log In', 'stream' ), 'logout' => esc_html__( 'Log Out', 'stream' ), + 'switched-to' => esc_html__( 'Switched To', 'stream' ), + 'switched-back' => esc_html__( 'Switched Back', 'stream' ), + 'switched-off' => esc_html__( 'Switched Off', 'stream' ), ); } From c9904b6ffbec8fab21c7205e5fcc8aedbbc84902 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 28 Aug 2015 15:57:07 +0200 Subject: [PATCH 4/4] Associate the action with the correct user when switching back on after being switched off. --- connectors/class-connector-user-switching.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/connectors/class-connector-user-switching.php b/connectors/class-connector-user-switching.php index 712af939f..7d2853ce0 100644 --- a/connectors/class-connector-user-switching.php +++ b/connectors/class-connector-user-switching.php @@ -170,13 +170,18 @@ public function callback_switch_to_user( $user_id, $old_user_id ) { public function callback_switch_back_user( $user_id, $old_user_id ) { $user = get_userdata( $user_id ); - $old_user = get_userdata( $old_user_id ); $message = _x( 'Switched back to %1$s (%2$s)', '1: User display name, 2: User login', 'stream' ); + if ( $old_user_id ) { + $old_user = get_userdata( $old_user_id ); + } else { + $old_user = $user; + } + $this->log( $message, array(