> Read the accompanying readme.txt file for instructions and documentation. * =>> Also, visit the plugin's homepage for additional information and updates. * =>> Or visit: https://wordpress.org/plugins/admin-trim-interface/ * * @package Admin_Trim_Interface * @author Scott Reilly * @version 3.2 */ /* * TODO: * - Options to remove other admin bar icons: my sites * - Options to remvoe other admin bar nodes: udpates, comments, new content, search (front-end) * - Add unit tests */ /* Copyright (c) 2009-2018 by Scott Reilly (aka coffee2code) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ defined( 'ABSPATH' ) or die(); if ( ! class_exists( 'c2c_AdminTrimInterface' ) ) : require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'c2c-plugin.php' ); final class c2c_AdminTrimInterface extends c2c_AdminTrimInterface_Plugin_048 { /** * Name of plugin's setting. * * @since 3.2 * @var string */ const SETTING_NAME = 'c2c_admin_trim_interface'; /** * The one true instance. * * @var c2c_AdminTrimInterface */ private static $instance; /** * Get singleton instance. * * @since 3.0 */ public static function get_instance() { if ( ! isset( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } /** * Constructor */ protected function __construct() { parent::__construct( '3.2', 'admin-trim-interface', 'c2c', __FILE__, array( 'settings_page' => 'themes' ) ); register_activation_hook( __FILE__, array( __CLASS__, 'activation' ) ); self::$instance = $this; } /** * Handles activation tasks, such as registering the uninstall hook. * * @since 2.1 */ public static function activation() { register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) ); } /** * Handles uninstallation tasks, such as deleting plugin options. * * This can be overridden. * * @since 2.0 */ public static function uninstall() { delete_option( self::SETTING_NAME ); } /** * Initializes the plugin's configuration and localizable text variables. */ public function load_config() { $this->name = __( 'Admin Trim Interface', 'admin-trim-interface' ); $this->menu_name = __( 'Admin Trim Interface', 'admin-trim-interface' ); $this->config = array( 'legend_image' => array( 'input' => 'custom', ), 'hide_wp_logo' => array( 'input' => 'checkbox', 'default' => false, 'numbered' => true, 'label' => __( 'Hide WordPress logo in admin bar?', 'admin-trim-interface' ), ), 'hide_home_icon' => array( 'input' => 'checkbox', 'default' => false, 'numbered' => true, 'label' => __( 'Hide home icon in admin bar?', 'admin-trim-interface' ), ), 'hide_howdy' => array( 'input' => 'checkbox', 'default' => false, 'numbered' => true, 'label' => __( 'Hide "Howdy"?', 'admin-trim-interface' ), ), 'hide_username' => array( 'input' => 'checkbox', 'default' => false, 'numbered' => true, 'label' => __( 'Hide username in admin bar?', 'admin-trim-interface' ), ), 'hide_avatar' => array( 'input' => 'checkbox', 'default' => false, 'wpgte' => '3.2.99', 'numbered' => true, 'label' => __( 'Hide user avatar in admin bar?', 'admin-trim-interface' ), ), 'hide_dashboard' => array( 'input' => 'checkbox', 'default' => false, 'numbered' => true, 'label' => __( 'Hide Dashboard menu link?', 'admin-trim-interface' ), ), 'hide_help' => array( 'input' => 'checkbox', 'default' => false, 'numbered' => true, 'label' => __( 'Hide contextual "Help" link?', 'admin-trim-interface' ), ), 'hide_footer_left' => array( 'input' => 'checkbox', 'default' => false, 'numbered' => true, 'label' => __( 'Hide footer links?', 'admin-trim-interface' ), ), 'hide_footer_version' => array( 'input' => 'checkbox', 'default' => false, 'numbered' => true, 'label' => __( 'Hide WordPress version in footer?', 'admin-trim-interface' ), ), ); } /** * Override the plugin framework's register_filters() to actually actions against filters. */ public function register_filters() { add_action( '_network_admin_menu', array( $this, 'hide_dashboard' ) ); add_action( '_user_admin_menu', array( $this, 'hide_dashboard' ) ); add_action( '_admin_menu', array( $this, 'hide_dashboard' ) ); add_action( 'admin_init', array( $this, 'admin_init' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'add_css' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'add_css' ) ); add_filter( 'contextual_help', array( $this, 'clear_contextual_help' ), 1000 ); add_filter( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 5 ); add_action( 'admin_head', array( $this, 'hide_help_tabs' ) ); add_action( 'admin_notices', array( $this, 'show_admin_notices' ) ); add_filter( 'explain_nonce_'.$this->nonce_field, array( $this, 'explain_nonce' ) ); add_action( $this->get_hook( 'custom_display_option'), array( $this, 'show_legend_image' ) ); } /** * Remove call to core_update_footer filter. * * @since 2.1 */ public function admin_init() { $options = $this->get_options(); if ( $options['hide_footer_left'] ) { add_filter( 'admin_footer_text', '__return_false' ); } if ( $options['hide_footer_version'] ) { remove_filter( 'update_footer', 'core_update_footer' ); } } /** * Shows settings admin notices. * * Settings notices are only shown for admin pages listed under Settings. * This plugin has its settings page under Appearance. * * @since 3.2 */ public function show_admin_notices() { // Bail if not on the plugin setting page. if ( $this->options_page !== get_current_screen()->id ) { return; } settings_errors(); } /** * Hides help tabs. * * @since 2.2 */ public function hide_help_tabs() { $options = $this->get_options(); if ( $options['hide_help'] ) { $screen = get_current_screen(); $screen->remove_help_tabs(); } } /** * Clears old-style contextual help defined via filter. * * @since 2.2 * * @param string $text The contextual help text. * @return string Empty string if help tab is being hidden. */ public function clear_contextual_help( $text ) { $options = $this->get_options(); if ( $options['hide_help'] ) { $text = ''; } return $text; } /** * Hides the dashboard menu link. * * @since 2.1 */ public function hide_dashboard() { global $menu, $submenu; $options = $this->get_options(); if ( $options['hide_dashboard'] ) { remove_menu_page( 'index.php' ); remove_menu_page( 'separator1' ); } } /** * Modify admin bar according to settings. * * Much of the node building is based on wp_admin_bar_my_account_item(). * * @since 2.2 * * @param obj $wp_admin_bar The admin bar. */ public function admin_bar_menu( $wp_admin_bar ) { $options = $this->get_options(); // Possibly hide the WP logo. if ( $options['hide_wp_logo'] ) { remove_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 ); } // Now determine if the user node needs removal/paring. $user_id = get_current_user_id(); if ( ! $user_id ) { return; } $current_user = wp_get_current_user(); $profile_url = get_edit_profile_url( $user_id ); // If any element in the my-account admin bar node is being hidden, // remove the whole thing and rebuild it if ( $options['hide_username'] || $options['hide_howdy'] || $options['hide_avatar'] ) { remove_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 ); } // All done if everything in the my-account node is being hidden. if ( $options['hide_username'] && $options['hide_howdy'] && $options['hide_avatar'] ) { return; } /* * The rest of this function is adapted from wp_admin_bar_my_account_item(). */ $avatar = $options['hide_avatar'] ? '' : get_avatar( $user_id, 26 ); if ( $options['hide_howdy'] ) { $howdy = $options['hide_username'] ? '' : $current_user->display_name; } else { $howdy = $options['hide_username'] ? __( 'Howdy' ) : sprintf( __( 'Howdy, %1$s' ), $current_user->display_name ); } $class = $avatar ? 'with-avatar' : ''; $wp_admin_bar->add_menu( array( 'id' => 'my-account', 'parent' => 'top-secondary', 'title' => $howdy . $avatar, 'href' => $profile_url, 'meta' => array( 'class' => $class, ), ) ); } /** * Output message if nonce has expired. * * @param string $msg The message WordPress would have shown. * @return string The error message. */ public function explain_nonce( $msg ) { return __( 'Your session has expired. Please log in to continue where you left off.' ); } /** * Outputs CSS. * * Runs for both front-end and admin areas of the site. */ public function add_css() { $options = $this->get_options(); $css = array(); $extra_css = ''; // Don't bother outputting CSS related to admin bar if it isn't showing. if ( is_admin_bar_showing() ) { // Remove the home icon from admin bar. if ( $options['hide_home_icon'] ) { if ( is_admin() ){ $extra_css .= '.wp-admin #wpwrap #wpadminbar #wp-admin-bar-site-name>.ab-item:before { content: ""; }' . "\n"; } else { $extra_css .= 'body #wpadminbar #wp-admin-bar-site-name>.ab-item:before { content: ""; }' . "\n"; } } // Remove the user icon from admin bar. if ( $options['hide_avatar'] ) { $css[] = 'body #wp-admin-bar-user-info .avatar'; $extra_css .= 'body #wp-admin-bar-my-account>.ab-item:before { content: ""; }' . "\n"; } } // Style the legend image on the plugin's setting page. if ( is_admin() ) { $extra_css .= << {$css} {$extra_css} HTML; } } /** * Outputs the text above the setting form. * * @param string $localized_heading_text Optional. Localized page heading text. */ public function options_page_description( $localized_heading_text = '' ) { $options = $this->get_options(); parent::options_page_description( __( 'Admin Trim Interface Settings', 'admin-trim-interface' ) ); echo '

' . __( 'Use the image below to correlate the settings below with the admin interface element they hide.', 'admin-trim-interface' ) . '

'; echo '

' . __( 'Note: These settings are global and will affect all users who are able to visit the admin pages.', 'admin-trim-interface' ) . '

'; } /** * Outputs the image that demonstrates the sections of the site that admin that correspond to the various settings. */ public function show_legend_image() { $link = plugins_url( 'screenshot-1.png', __FILE__ ); printf( '%s', esc_url( $link ), esc_attr__( 'Settings to admin interface mapping; click to view full size', 'admin-trim-interface' ), 'c2c-ati-image', esc_url( $link ), esc_attr__( 'Settings to admin interface mapping', 'admin-trim-interface' ) ); echo '
' . __( 'Click to view full size.', 'admin-trim-interface' ) . '
'; } } // end c2c_AdminTrimInterface c2c_AdminTrimInterface::get_instance(); endif; // end if !class_exists()