Skip to content

Commit

Permalink
Merge pull request #744 from johnbillion/user-switching
Browse files Browse the repository at this point in the history
Implement a User Switching connector
  • Loading branch information
Luke Carbis committed Aug 31, 2015
2 parents de3e67e + c9904b6 commit dcb2482
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 9 deletions.
9 changes: 3 additions & 6 deletions classes/class-connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
8 changes: 5 additions & 3 deletions classes/class-connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function load_connectors() {
'edd',
'gravityforms',
'jetpack',
'user-switching',
'woocommerce',
'wordpress-seo',
);
Expand Down Expand Up @@ -178,13 +179,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 );
}
}
237 changes: 237 additions & 0 deletions connectors/class-connector-user-switching.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?php
namespace WP_Stream;

class Connector_User_Switching extends Connector {

/**
* Connector slug
*
* @var string
*/
public $name = 'user-switching';

/**
* Actions registered for this connector
*
* @var array
*/
public $actions = array(
'wp_stream_after_connectors_registration',
'switch_to_user',
'switch_back_user',
'switch_off_user',
);

/**
* Check if plugin dependencies are satisfied and add an admin notice if not
*
* @return bool
*/
public function is_dependency_satisfied() {
if ( class_exists( 'user_switching' ) ) {
return true;
}

return false;
}

/**
* Return translated connector label
*
* @return string Translated connector label
*/
public function get_label() {
return esc_html_x( 'User Switching', 'user-switching', 'stream' );
}

/**
* Return translated action term labels
*
* @return array Action terms label translation
*/
public function get_action_labels() {
return array();
}

/**
* Return translated context labels
*
* @return array Context label translations
*/
public function get_context_labels() {
return array();
}

/**
* Register this connector.
*
* Overrides the default `Connector::register()` method.
*
*/
public function register() {
parent::register();

add_filter( 'wp_stream_log_data', array( $this, 'log_override' ) );
}

/**
* Override connector log for our own actions
*
* This changes the connector property to the Users connector if the log entry is from
* our User_Switching connector.
*
* @param array $data The log data.
* @return array The updated log data.
*/
public function log_override( $data ) {
if ( ! is_array( $data ) ) {
return $data;
}

if ( 'User_Switching' === $data['connector'] ) {
$data['connector'] = 'Users';
}

return $data;
}

/**
* Fired after Stream has instantiated all of its connectors.
*
* This unhooks the Users connector's login and logout actions so they don't appear when a user switches
* user with the User Switching plugin.
*
* @param array $labels All registered connector labels
* @param Connectors $connectors The Connectors object instance
*/
public function callback_wp_stream_after_connectors_registration( array $labels, Connectors $connectors ) {

if ( ! isset( $_REQUEST['action'] ) ) {
return;
}

switch ( $_REQUEST['action'] ) {

case 'switch_to_user':
$this->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 );
$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(
'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' ) );
}
}
}

}
3 changes: 3 additions & 0 deletions connectors/class-connector-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
);
}

Expand Down

0 comments on commit dcb2482

Please sign in to comment.