Skip to content

Commit

Permalink
Migrate code to namespace based composer environment
Browse files Browse the repository at this point in the history
  • Loading branch information
drazenbebic committed Feb 11, 2020
1 parent 540a919 commit 5a09e7a
Show file tree
Hide file tree
Showing 8 changed files with 555 additions and 270 deletions.
261 changes: 30 additions & 231 deletions admin-bar-user-switching.php
Original file line number Diff line number Diff line change
@@ -1,252 +1,51 @@
<?php
/**
* Plugin Name: User Switching in Admin Bar
* Plugin URI: http://markwilkinson.me
* Description: Building upon the <a href="http://wordpress.org/extend/plugins/user-switching/">User Switching plugin</a> by John Blackbourn this plugin adds a dropdown list of users in the WordPress admin bar with a link to switch to that user, then providing a switch back link in the admin bar too.
* Author: Mark Wilkinson
* Author URI: http://markwilkinson.me
* Plugin URI: https://www.bebic.at
* Description: Originally developed by <a href="https://markwilkinson.me/">Mark Wilkinson</a>, this plugin builds upon the <a href="https://wordpress.org/plugins/user-switching/">User Switching</a> plugin by John Blackbourn and adds a dropdown list of users in the WordPress admin bar with a link to switch to that user, then providing a switch back link in the admin bar as well.
* Author: Dražen Bebić
* Author URI: https://www.bebic.at
* Version: 1.1.1
*/

/**
* Function abus_current_url
* Determine the URL of the currently viewed page - will return array if $parse set to true
* Taken from https://github.com/scottsweb/null/blob/master/functions.php
*/
function abus_current_url( $parse = false ) {

$s = empty( $_SERVER[ 'HTTPS' ] ) ? '' : ( $_SERVER[ 'HTTPS' ] == 'on' ) ? 's' : '';
$protocol = substr( strtolower( $_SERVER[ 'SERVER_PROTOCOL' ] ), 0, strpos( strtolower( $_SERVER[ 'SERVER_PROTOCOL' ] ), '/' ) ) . $s;
$port = ( $_SERVER[ 'SERVER_PORT' ] == '80') ? '' : ( ":".$_SERVER[ 'SERVER_PORT' ] );

if ( $parse ) {
return parse_url( $protocol . "://" . $_SERVER[ 'HTTP_HOST' ] . $port . $_SERVER[ 'REQUEST_URI' ] );
} else {
return $protocol . "://" . $_SERVER[ 'HTTP_HOST' ] . $port . $_SERVER[ 'REQUEST_URI' ];
}

}

/**
* Function abus_usab_initialisation
* Initialisation plugin to add error message to admin if user switching plugin not present
*/
function abus_usab_initialisation() {
add_action( 'admin_notices', 'abus_error' );
}
use AdminBarUserSwitching\Plugin as AdminBarUserSwitchingPlugin;

/**
* Function abus_usab_error
* Deactivates the plugin and throws and error message when User Switching plugin not active
*/
function abus_error() {
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/functions/abus-core-functions.php';

if( ! class_exists( 'user_switching' ) ) {

deactivate_plugins( 'admin-bar-user-switching/admin-bar-user-switching.php', 'admin-bar-user-switching.php' );

echo '<div class="error"><p>This plugin has <strong>been deactivated</strong>. The reason for this, is that it requires the User Switching plugin in order to work. Please install the User Switching plugin, then activate this plugin again. <strong>Please ignore the Plugin Activated message below</strong>.</p></div>';

}

// Define ABUS_PLUGIN_FILE
if (!defined('ABUS_PLUGIN_FILE')) {
define('ABUS_PLUGIN_FILE', __FILE__);
}

/* start this plugin once all other plugins have loaded */
add_action( 'plugins_loaded', 'abus_usab_initialisation' );

/**
* function abus_adminbar_output()
* output the admin bar markup for the user search box
*/
function abus_adminbar_output() {

/* if user switching is not active - go no further! */
if( ! class_exists( 'user_switching' ) ) {
return;
}

/* check wether the admin bar is showing */
if( is_admin_bar_showing() ) {

global $user_switching;

/* load the global admin bar variable */
global $wp_admin_bar;

/* check whether the current user can edit users - cap is filterable */
if( current_user_can( apply_filters( 'abus_switch_to_capability', 'edit_users' ) ) ) {

/* add admin bar menu for switching to a user */
$wp_admin_bar->add_menu(
array(
'id' => 'abus_switch_to_user',
'title' => apply_filters( 'abus_switch_to_text', 'Switch to User' ),
'href' => '#',
)
);

/* create a nonce */
$nonce = wp_create_nonce( 'abus_user_search_nonce' );

/* build the user search form markup */
$form = '
<div id="abus_wrapper">
<form method="post" action="abus_user_search">
<input id="abus_search_text" name="abus_search_text" type="text" placeholder="Enter a username" />
<input id="abus_search_submit" name="abus_search_submit" type="submit" />
<input name="abus_current_url" type="hidden" value="' . esc_url( abus_current_url() ) . '" />
<input name="abus_nonce" type="hidden" value="' . wp_create_nonce( 'abus_nonce' ) . '" />
</form>
<div id="abus_result"></div>
</div>
';

/* add the admin bar sub menu item for the search form */
$wp_admin_bar->add_menu(
array(
'id' => 'abus_user_search',
'parent' => 'abus_switch_to_user',
'title' => apply_filters( 'abus_form_output', $form ),
)
);

} // end if super admin

/* check if there is an old user stored i.e. this logged in user is through switching */
if( $user_switching->get_old_user() ) {

/* build the switch back url */
$abus_switch_back_url = $user_switching->switch_back_url( $user_switching->get_old_user() );

/* we are logged in throught swtiching so add admin bar menu to create the switch back link */
$wp_admin_bar->add_menu( array(
'id' => 'switch_back',
'title' => apply_filters( 'abus_switch_back_text', 'Switch Back' ),
'href' => esc_url( add_query_arg( array( 'redirect_to' => abus_current_url() ), $abus_switch_back_url ) )
) );

} // end if old user present

} // end if admin bar showing

// Define ABUS_PLUGIN_DIR
if (!defined('ABUS_PLUGIN_DIR')) {
define('ABUS_PLUGIN_DIR', __DIR__);
}

add_action( 'wp_before_admin_bar_render', 'abus_adminbar_output', 1 );

/**
* function abus_user_search()
* searches for the required user depending what was entered into the search box
* in the admin bar
*/
function abus_user_search() {

global $user_switching;

/* get the posted query search, current url and nonce */
$q = esc_attr( $_POST[ 'query' ] );
$url = esc_url( $_POST[ 'currenturl' ] );
$nonce = esc_attr( $_POST[ 'nonce' ] );

/* check nonce passes for intent */
if( ! wp_verify_nonce( $nonce, 'abus_nonce' ) )
exit();

$args = apply_filters(
'abus_user_search_args',
array(
'search' => is_numeric( $q ) ? $q : '*' . $q . '*',
)
);

/* query the users */
$user_query = new WP_User_Query( $args );

echo '<div class="abus_user_results">';

/* check we have results returned */
if ( ! empty( $user_query->results ) ) {

/* loop through each returned user */
foreach ( $user_query->results as $user ) {

/* if this user is the current user - skip to next user */
if( $user->ID == get_current_user_id() ) {
continue;
}

$link = user_switching::maybe_switch_url( $user );
if ( $link ) {
$link = add_query_arg( 'redirect_to', apply_filters( 'abus_switch_to_url', $url ), $link );
echo '<p class="result"><a href="' . esc_url( $link, $user ) . '">' . $user->display_name . '</a></p>';
}

}

/* no users match search */
} else {

echo '<p class="result">No users found.</p>';

}

echo '</div>';

die();

// Define ABUS_PLUGIN_URL
if (!defined('ABUS_PLUGIN_URL')) {
define('ABUS_PLUGIN_URL', plugins_url('', __FILE__) . '/');
}

add_action( 'wp_ajax_abus_user_search', 'abus_user_search' );

/**
* function abus_enqueue_scripts()
* enqueues the necessary js and css for the plugin
*/
function abus_enqueue_scripts() {

wp_register_script(
'abus_script',
plugins_url( '/assets/js/abus_script.js', __FILE__ ),
array( 'jquery' )
);

$args = array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'magicWord' => '',
);

$args = apply_filters( 'abus_ajax_args', $args );

wp_localize_script(
'abus_script',
'abus_ajax',
$args
);

if( is_user_logged_in() ) {
wp_enqueue_script( 'abus_script' );
}

// Define ABUS_PLUGIN_VERSION
if (!defined('ABUS_PLUGIN_VERSION')) {
define('ABUS_PLUGIN_VERSION', '1.1.1');
}

add_action( 'wp_enqueue_scripts', 'abus_enqueue_scripts' );
add_action( 'admin_enqueue_scripts', 'abus_enqueue_scripts' );
// Define ABUS_PLUGIN_SLUG
if (!defined('ABUS_PLUGIN_SLUG')) {
define('ABUS_PLUGIN_SLUG', 'admin-bar-user-switching');
}

/**
* function abus_enqueue_styles()
* enqueues the plugin stylsheet
* Main instance of AdminBarUserSwitching\.
*
* @return AdminBarUserSwitchingPlugin
*/
function abus_styles() {

$styles = '
<style type="text/css">
#wpadminbar .quicklinks #wp-admin-bar-abus_switch_to_user ul li .ab-item { height: auto; }
#abus_user_results { background-color: #000000; }
</style>
';

echo apply_filters( 'abus_styles', $styles );

function AdminBarUserSwitching() {
return AdminBarUserSwitchingPlugin::instance();
}

add_action( 'wp_head', 'abus_styles' );
add_action( 'admin_head', 'abus_styles' );
// Global for backwards compatibility.
$GLOBALS[ABUS_PLUGIN_SLUG] = AdminBarUserSwitching();
Loading

0 comments on commit 5a09e7a

Please sign in to comment.