Skip to content

Commit

Permalink
Add our own toolbar when the adminbar isn't showing
Browse files Browse the repository at this point in the history
- Fixes #4
- Adds an option to hide/show our toolbar when now view is selected and
the admin bar is not shown.
  • Loading branch information
JoryHogeveen committed Oct 5, 2016
1 parent 67e99bc commit 6b1bb27
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 12 deletions.
10 changes: 10 additions & 0 deletions assets/css/view-admin-as.css
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,13 @@

#wpadminbar #wp-admin-bar-view-as #wp-admin-bar-settings > .ab-item::before {content: '\f108';}
#wpadminbar #wp-admin-bar-view-as #wp-admin-bar-role-defaults > .ab-item::before {content: '\f337';}

/**
* Our custom frontend toolbar
*/

#vaa_toolbar #wpadminbar {width: auto; min-width: 0; right: 0; left: auto; border-radius: 0 0 0 5px;}
#vaa_toolbar #wpadminbar #wp-admin-bar-view-as > .ab-item .ab-icon {margin-left: 0;}
#vaa_toolbar #wpadminbar #wp-admin-bar-view-as > .ab-item .ab-label {display: none;}
#vaa_toolbar #wpadminbar #wp-admin-bar-view-as:hover > .ab-item .ab-icon {margin-left: 6px;}
#vaa_toolbar #wpadminbar #wp-admin-bar-view-as:hover > .ab-item .ab-label {display: inline-block;}
10 changes: 10 additions & 0 deletions assets/js/view-admin-as.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ if ( 'undefined' == typeof VAA_View_Admin_As ) {
}
VAA_View_Admin_As.ajax( viewAs, true );
});

// Enable hide front
$(document).on('change', VAA_View_Admin_As.prefix+'#wp-admin-bar-settings-hide-front input#vaa_settings_hide_front', function( e ) {
e.preventDefault();
var viewAs = { user_setting : { hide_front : "no" } };
if ( this.checked ) {
viewAs = { user_setting : { hide_front : "yes" } };
}
VAA_View_Admin_As.ajax( viewAs, false );
});
};


Expand Down
17 changes: 17 additions & 0 deletions includes/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,21 @@ final public static function set_array_data( $array, $var, $key = false, $append
return $var;
}

/**
* Is our custom toolbar showing?
*
* @since 1.5.x
* @access public
* @static
*
* @return bool
*/
public static function is_vaa_toolbar_showing() {

if ( class_exists( 'VAA_View_Admin_As_Toolbar' ) && VAA_View_Admin_As_Toolbar::$showing ) {
return true;
}
return false;
}

} // end class
11 changes: 7 additions & 4 deletions includes/class-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class VAA_View_Admin_As_Store
'VAA_View_Admin_As_Compat',
'VAA_View_Admin_As_Update',
'VAA_View_Admin_As_Admin_Bar',
'VAA_View_Admin_As_Toolbar',
'VAA_View_Admin_As_Role_Defaults',
);

Expand Down Expand Up @@ -93,7 +94,8 @@ final class VAA_View_Admin_As_Store
private $defaultUserSettings = array(
'view_mode' => 'browse',
'admin_menu_location' => 'top-secondary',
'force_group_users' => "no",
'force_group_users' => 'no',
'hide_front' => 'no',
);

/**
Expand All @@ -105,9 +107,10 @@ final class VAA_View_Admin_As_Store
* @var array
*/
private $allowedUserSettings = array(
'view_mode' => array('browse', 'single'),
'admin_menu_location' => array('top-secondary', 'my-account'),
'force_group_users' => array("yes", "no"),
'view_mode' => array( 'browse', 'single' ),
'admin_menu_location' => array( 'top-secondary', 'my-account' ),
'force_group_users' => array( 'yes', 'no' ),
'hide_front' => array( 'yes', 'no' ),
);

/**
Expand Down
29 changes: 23 additions & 6 deletions ui/admin-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ public function vaa_init() {
$this->groupUserRoles = false;
}

// Add the default nodes to the admin bar
// Add the default nodes to the WP admin bar
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) );
add_action( 'vaa_toolbar_menu', array( $this, 'admin_bar_menu' ), 10, 2 );

// Add the caps nodes to the admin bar
add_action( 'vaa_admin_bar_menu', array( $this, 'admin_bar_menu_settings' ) );

Expand All @@ -111,9 +113,10 @@ public function vaa_init() {
* @see 'admin_bar_menu' action
* @link https://codex.wordpress.org/Class_Reference/WP_Admin_Bar
* @param object $admin_bar
* @param string $root
* @return void
*/
public function admin_bar_menu( $admin_bar ) {
public function admin_bar_menu( $admin_bar, $root = '' ) {

$icon = 'dashicons-hidden';
$title = __('Default view (Off)', 'view-admin-as');
Expand All @@ -135,15 +138,17 @@ public function admin_bar_menu( $admin_bar ) {
$title = __('Viewing as user', 'view-admin-as') . ': ' . $this->get_selectedUser()->data->display_name . ' <span class="user-role">(' . implode( ', ', $selected_user_roles ) . ')</span>';
}

$view_as_location = 'top-secondary';
if ( $this->get_userSettings('admin_menu_location') && in_array( $this->get_userSettings('admin_menu_location'), $this->get_allowedUserSettings('admin_menu_location') ) ) {
$view_as_location = $this->get_userSettings('admin_menu_location');
if ( empty( $root ) ) {
$root = 'top-secondary';
if ( $this->get_userSettings('admin_menu_location') && in_array( $this->get_userSettings('admin_menu_location'), $this->get_allowedUserSettings('admin_menu_location') ) ) {
$root = $this->get_userSettings('admin_menu_location');
}
}

// Add menu item
$admin_bar->add_node( array(
'id' => 'view-as',
'parent' => $view_as_location,
'parent' => $root,
'title' => '<span class="ab-label">' . $title . '</span><span class="ab-icon alignright dashicons ' . $icon . '"></span>',
'href' => false,
'meta' => array(
Expand Down Expand Up @@ -245,6 +250,18 @@ public function admin_bar_menu_settings( $admin_bar ) {
),
) );

$admin_bar->add_node( array(
'id' => 'settings-hide-front',
'parent' => 'settings',
'title' => '<input type="checkbox" class="checkbox vaa_settings_hide_front" value="1" id="vaa_settings_hide_front" name="vaa_settings_hide_front" ' . checked( $this->get_userSettings('hide_front'), 'yes', false ) . '>
<label for="vaa_settings_hide_front">' . __('Hide on frontend', 'view-admin-as') . '</label>
<p class="description ab-item">' . __('Hide on frontend when no view is selected and the admin bar is not shown', 'view-admin-as') . '</p>',
'href' => false,
'meta' => array(
'class' => 'auto-height',
),
) );

/**
* force_group_users setting
* @since 1.5.2
Expand Down
147 changes: 147 additions & 0 deletions ui/toolbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* View Admin As - Toolbar UI
*
* Toolbar UI for View Admin As
*
* @author Jory Hogeveen <info@keraweb.nl>
* @package view-admin-as
* @version 1.5.x
*/

! defined( 'ABSPATH' ) and die( 'You shall not pass!' );

if ( ! class_exists( 'WP_Admin_Bar' ) && file_exists( ABSPATH . WPINC . '/class-wp-admin-bar.php' ) ) {
require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
}

if ( class_exists( 'WP_Admin_Bar' ) ) {

final class VAA_View_Admin_As_Toolbar extends WP_Admin_Bar
{
/**
* The single instance of the class.
*
* @since 1.5.x
* @var VAA_View_Admin_As_Admin_Bar
*/
private static $_instance = null;

/**
* Is this toolbar being rendered?
*
* @since 1.5.x
* @var bool
*/
public static $showing = false;

/**
* View Admin As store
*
* @since 1.5.x
* @var object|bool
*/
private $vaa_store = false;

/**
* Construct function
* Protected to make sure it isn't declared elsewhere
*
* @since 1.5.x
* @access protected
*/
protected function __construct() {
$this->vaa_store = VAA_View_Admin_As_Store::get_instance( $this );

if ( ! is_admin() ) {
add_action( 'vaa_view_admin_as_init', array( $this, 'vaa_init' ) );
}
}

/**
* Init function that initializes this plugin after the main VAA class is loaded
*
* @since 1.5.x
* @access public
* @see 'vaa_view_admin_as_init' action
* @return void
*/
public function vaa_init() {
add_action('init', array( $this, 'vaa_toolbar_init' ) );
}

/**
* Init function for the toolbar
*
* @since 1.5.x
* @access public
* @return void
*/
public function vaa_toolbar_init() {

if ( ! is_admin_bar_showing() && ( 'no' == $this->vaa_store->get_userSettings( 'hide_front' ) || $this->vaa_store->get_viewAs() ) ) {

self::$showing = true;

wp_enqueue_script( 'admin-bar' );
wp_enqueue_style( 'admin-bar' );

add_action( 'wp_footer', array( $this, 'vaa_toolbar_render' ), 100 );
}
}

/**
* Render our toolbar using the render function from WP_Admin_bar
*/
public function vaa_toolbar_render() {

$this->add_group( array(
'id' => 'top-secondary',
'meta' => array(
'class' => 'ab-top-secondary',
),
) );

// Load our admin bar nodes and force the location
do_action( 'vaa_toolbar_menu', $this, 'top-secondary' );

$toolbar_classes = array_map( 'esc_attr', apply_filters( 'vaa_toolbar_classes', array() ) );
echo '<div id="vaa_toolbar" class="' . implode( ' ', $toolbar_classes ) . '">';

$this->render();

echo '</div>';
}

/**
* Overwrite WP_Admin_Bar initialize()
* @since 1.5.x
*/
public function initialize() {
// Nope
}

/**
* Main Instance.
*
* Ensures only one instance of this class is loaded or can be loaded.
*
* @since 1.5.x
* @access public
* @static
* @param object|bool $caller The referrer class
* @return VAA_View_Admin_As_Admin_Bar|bool
*/
public static function get_instance( $caller = false ) {
if ( is_object( $caller ) && 'VAA_View_Admin_As' == get_class( $caller ) ) {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
return false;
}

} // end class

}
21 changes: 19 additions & 2 deletions view-admin-as.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,15 @@ public function init() {
$this->store->set_viewAs( $this->get_view() );
// If view is set,
if ( $this->store->get_viewAs() ) {
/*
// Force display of admin bar (older WP versions)
if ( function_exists('show_admin_bar') ) {
show_admin_bar( true );
}
// Force display of admin bar (WP 3.3+)
remove_all_filters( 'show_admin_bar' );
add_filter( 'show_admin_bar', '__return_true', 999999999 );
*/

// Change current user object so changes can be made on various screen settings
// wp_set_current_user() returns the new user object
Expand Down Expand Up @@ -323,7 +325,8 @@ public function init() {
* @return void
*/
private function load_ui() {
// The admin bar ui

// The default admin bar ui
if ( ! class_exists('VAA_View_Admin_As_Admin_Bar') ) {
include_once( VIEW_ADMIN_AS_DIR . 'ui/admin-bar.php' );
self::$vaa_class_names[] = 'VAA_View_Admin_As_Admin_Bar';
Expand All @@ -336,6 +339,20 @@ private function load_ui() {
. ' <code>(' . sprintf( __('Class %s already exists', 'view-admin-as'), 'VAA_View_Admin_As_Admin_Bar' ) . ')</code>',
) );
}

// Our custom toolbar
if ( ! class_exists('VAA_View_Admin_As_Toolbar') ) {
include_once( VIEW_ADMIN_AS_DIR . 'ui/toolbar.php' );
self::$vaa_class_names[] = 'VAA_View_Admin_As_Toolbar';
$this->ui['toolbar'] = VAA_View_Admin_As_Toolbar::get_instance( $this );
} else {
$this->add_notice('class-error-toolbar', array(
'type' => 'notice-error',
'message' => '<strong>' . __('View Admin As', 'view-admin-as') . ':</strong> '
. __('Plugin not loaded because of a conflict with an other plugin or theme', 'view-admin-as')
. ' <code>(' . sprintf( __('Class %s already exists', 'view-admin-as'), 'VAA_View_Admin_As_Toolbar' ) . ')</code>',
) );
}
}

/**
Expand Down Expand Up @@ -792,7 +809,7 @@ public function reset_all_views( $user_login = false, $user = false ) {
*/
public function enqueue_scripts() {
// Only enqueue scripts if the admin bar is enabled otherwise they have no use
if ( is_admin_bar_showing() && $this->is_enabled() ) {
if ( ( is_admin_bar_showing() || VAA_API::is_vaa_toolbar_showing() ) && $this->is_enabled() ) {
$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; // Use non-minified versions
$version = defined('WP_DEBUG') && WP_DEBUG ? time() : $this->store->get_version(); // Prevent browser cache

Expand Down

0 comments on commit 6b1bb27

Please sign in to comment.