. */ class ApermoAdminBar { /** * Contains the known sites * * @var array */ private $sites = array(); /** * Static Counter for spacers * * @var int */ private static $spacer_count; /** * Containing the current site * * @var string */ private $current; /** * Contains the allowed page types * * @var array */ private $allowed_page_types = array(); /** * Private copy of $_wp_admin_css_colors * * @var array */ private $admin_colors = array(); /** * Indicator if the sites were loaded from a filter. * * @var bool */ private $is_from_filter = false; /** * ApLiveDevAdminBar constructor. */ public function __construct() { $this->load_translation(); add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); add_action( 'admin_init', array( $this, 'settings_init' ) ); add_action( 'init', array( $this, 'init' ) ); add_action( 'init', array( $this, 'sort_admin_colors' ), 99 ); add_action( 'admin_enqueue_scripts', array( $this, 'color_scheme' ), 99 ); add_action( 'wp_enqueue_scripts', array( $this, 'color_scheme' ), 99 ); } /** * Loading Textdomain * * Thanks to @kau-boy */ public function load_translation() { load_plugin_textdomain( 'apermo-adminbar', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } /** * Called on init-hook */ public function init() { /** * Entry format * * 'key_for_form' => array( 'label' => 'Readable Label', 'descroption' => 'Short description' ) */ $types = array( 'dev' => array( 'label' => __( 'Development Site', 'apermo-adminbar' ), 'description' => __( 'Your development site, probably a local version on the development machine', 'apermo-adminbar' ), 'default' => 'sunrise', ), 'staging' => array( 'label' => __( 'Staging Site', 'apermo-adminbar' ), 'description' => __( 'Your staging site, for testing and other purposes', 'apermo-adminbar' ), 'default' => 'blue', ), 'live' => array( 'label' => __( 'Live Site', 'apermo-adminbar' ), 'description' => __( 'Your production site', 'apermo-adminbar' ), 'default' => 'fresh', ), ); remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' ); // Allow to add (or remove) further page types via filter. $this->allowed_page_types = apply_filters( 'apermo-adminbar-types', $types ); // 'all' is reserved, it is used for the serialized data, and it would possibly create side effects. unset( $this->allowed_page_types['all'] ); $this->load_sites(); if ( count( $this->sites ) ) { add_action( 'admin_bar_menu', array( $this, 'admin_bar_filter' ), 99 ); $this->set_current(); } } /** * Load Settings from Database */ private function load_sites() { // Check if a filter was added from within the theme. if ( has_filter( 'apermo-adminbar-sites' ) ) { $dummysites = array(); foreach ( $this->allowed_page_types as $key => $allowed_page_type ) { $dummysites[ $key ]['name'] = $allowed_page_type['label']; $dummysites[ $key ]['color'] = $allowed_page_type['default']; $dummysites[ $key ]['url'] = ''; } // Filter against a default set of sites and afterwards use the sanitize function. $this->is_from_filter = true; $this->sites = $this->sanitize( apply_filters( 'apermo-adminbar-sites', $dummysites ) ); } // If the sites are still empty load the settings from the DB. if ( ! count( $this->sites ) ) { $this->is_from_filter = false; $this->sites = get_option( 'apermo_adminbar_sites', array() ); } } /** * Set $this->current for later use * * @return void */ private function set_current() { foreach ( $this->sites as $key => $site ) { // Just give me the domain + everything that follows. $url = trim( substr( $site['url'], strpos( $site['url'], '://' ) + 3 ), '/' ); if ( $url && false !== strpos( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $url ) ) { $this->current = $key; return; } } } /** * Sort the Admin colors * * Based on the function admin_color_scheme_picker( $user_id ) from WordPress Core * * @return void */ public function sort_admin_colors() { global $_wp_admin_css_colors; register_admin_color_schemes(); $this->admin_colors = $_wp_admin_css_colors; ksort( $this->admin_colors ); if ( isset( $this->admin_colors['fresh'] ) ) { // Set Default ('fresh') and Light should go first. $this->admin_colors = array_filter( array_merge( array( 'fresh' => '', 'light' => '' ), $this->admin_colors ) ); } $this->admin_colors = apply_filters( 'apermo-adminbar-colors', $this->admin_colors ); } /** * Load the AdminBar Color Scheme */ public function color_scheme() { $scheme = $this->admin_colors[ $this->sites[ $this->current ]['color'] ]->url; if ( current_user_can( 'edit_posts' ) && ( is_admin() || is_admin_bar_showing() ) ) { wp_enqueue_style( 'apermo-adminbar-colors', $scheme, array() ); wp_enqueue_style( 'apermo-adminbar', plugins_url( 'css/style.css', __FILE__ ) ); } } /** * Adds a spacer to the admin-bar * * Static on purpose, so that developers can add spacers to the admin-bar themselves without needing to copy the code * * @param WP_Admin_Bar $wp_admin_bar The WP AdminBar. */ public static function add_spacer( $wp_admin_bar ) { $wp_admin_bar->add_node( array( 'id' => 'spacer' . self::$spacer_count, 'title' => '', 'parent' => 'site-name', 'href' => false, 'meta' => array( 'class' => 'spacer', ), ) ); self::$spacer_count++; } /** * Filters the AdminBar to add the links between the different pages * * @param WP_Admin_Bar $wp_admin_bar The WP AdminBar. * * @return void */ public function admin_bar_filter( $wp_admin_bar ) { if ( ! current_user_can( 'edit_posts' ) ) { // This feature is only for contributors or better. return; } self::add_spacer( $wp_admin_bar ); foreach ( $this->sites as $key => $site ) { // Check if there is a URL. if ( isset( $site['url'] ) && $site['url'] ) { // Makes no sense to add links to the site we are currently on. if ( $key !== $this->current ) { // Add the node to home of the other site. $wp_admin_bar->add_node( array( 'id' => esc_attr( 'apermo_adminbar_menu_' . $key ), 'title' => esc_html( $site['name'] ), 'parent' => 'site-name', 'href' => esc_url( $site['url'] ), ) ); // Check if we are on a different page than the homepage. if ( strlen( $this->get_request() ) > 1 ) { $wp_admin_bar->add_node( array( 'id' => esc_attr( 'apermo_adminbar_menu_' . $key . '-same' ), 'title' => esc_html( $site['name'] ) . ' ' . __( '(Same page)', 'apermo-adminbar' ), 'parent' => 'site-name', 'href' => esc_url( $site['url'] . $this->get_request() ), ) ); } } } } if ( ! is_admin() ) { self::add_spacer( $wp_admin_bar ); } } /** * Get the Request Part that is not Subfolder for the WordPress installation. */ public function get_request() { $request = esc_url_raw( $_SERVER['REQUEST_URI'] ); $url = parse_url( $this->sites[ $this->current ]['url'] ); if ( isset( $url['path'] ) && 0 === strpos( $request, $url['path'] ) ) { return substr( $request, strlen( $url['path'] ) ); } return $request; } /** * Options page callback * * @return void */ public function options_page() { ?>
*)
sites ) ) { ?>
sites ); ?>sanitize( $all ); if ( is_array( $output ) && count( $output ) ) { return $output; } } unset( $input['all'] ); } // Check all incoming pages. foreach ( $input as $key => $data ) { // Probably useless, but safety is the mother of the Porzellankiste. $key = sanitize_key( $key ); // Check if the incoming page exists, otherwise ignore. if ( array_key_exists( $key, $this->allowed_page_types ) ) { $data['name'] = esc_html( strip_tags( $data['name'] ) ); if ( ! array_key_exists( $data['color'], $this->admin_colors ) ) { $data['color'] = $this->allowed_page_types[ $key ]['default']; } $data['url'] = trim( esc_url_raw( $data['url'] ), '/' ); // It only makes sense to save, if there is a URL, otherwise just drop it. if ( $data['url'] ) { $output[ $key ] = $data; } } } return $output; } } // Run boy, run! add_action( 'plugins_loaded', function () { new ApermoAdminBar(); } );