# my-wp/1.5/controller/modules/mywp.controller.module.admin.toolbar.php

My WP Customize Admin/Frontend, version 1.5. 751 lines.

- Page: https://pluginprobe.com/plugins/my-wp/1.5/code/controller/modules/mywp.controller.module.admin.toolbar.php
- Raw: https://pluginprobe.com/plugins/my-wp/1.5/raw/controller/modules/mywp.controller.module.admin.toolbar.php
- Modified: 2017-12-08T06:04:36+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/my-wp/1.5/code/controller/modules/mywp.controller.module.admin.toolbar.php#L10-L20`.

```php
<?php

if ( ! defined( 'ABSPATH' ) ) {
  exit;
}

if( ! class_exists( 'MywpControllerAbstractModule' ) ) {
  return false;
}

if ( ! class_exists( 'MywpControllerModuleAdminToolbar' ) ) :

final class MywpControllerModuleAdminToolbar extends MywpControllerAbstractModule {

  static protected $id = 'admin_toolbar';

  static private $toolbar = false;

  static private $toolbar_items = false;

  static private $toolbar_items_added_classes = false;

  static private $child_items = array();

  static private $parent_items = array();

  static private $found_parent_item_ids = array();

  static private $current_url = false;

  static private $find_parent_id = array();

  public static function mywp_controller_initial_data( $initial_data ) {

    $initial_data['custom_menu_ui'] = '';

    return $initial_data;

  }

  public static function mywp_controller_default_data( $default_data ) {

    $default_data['custom_menu_ui'] = false;

    return $default_data;

  }

  public static function mywp_wp_loaded() {

    if( ! is_admin() ) {

      return false;

    }

    if( is_network_admin() ) {

      return false;

    }

    if( ! self::is_do_controller() ) {

      return false;

    }

    add_action( 'admin_enqueue_scripts' , array( __CLASS__ , 'admin_enqueue_scripts' ) );
    add_action( 'wp_before_admin_bar_render' , array( __CLASS__ , 'remove_detault_menus' ) , 1000 );
    add_action( 'wp_before_admin_bar_render' , array( __CLASS__ , 'customize_admin_bar' ) , 1000 );

  }

  private static function get_toolbar() {

    if( ! empty( self::$toolbar ) ) {

      return self::$toolbar;

    }

    $args = array( 'post_status' => array( 'publish' ) , 'post_type' => 'mywp_admin_toolbar' , 'order' => 'ASC' , 'orderby' => 'menu_order' , 'posts_per_page' => -1 );

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

    $posts = MywpPostType::get_posts( $args );

    $toolbar = apply_filters( 'mywp_controller_admin_toolbar_get_toolbar' , $posts );

    self::$toolbar = $toolbar;

    return $toolbar;

  }

  private static function get_toolbar_items() {

    if( ! empty( self::$toolbar_items ) ) {

      return self::$toolbar_items;

    }

    $toolbar = self::get_toolbar();

    if( empty( $toolbar ) ) {

      return false;

    }

    $toolbar_items = array();

    foreach( $toolbar as $key => $toolbar_item ) {

      if( $toolbar_item->item_type == 'default') {

        $toolbar_item = MywpAdminToolbar::default_item_convert( $toolbar_item );

        if( ! empty( $toolbar_item ) ) {

          $toolbar_items[] = $toolbar_item;

        }

      } else {

        $toolbar_items[] = $toolbar_item;

      }

    }

    $toolbar_items = apply_filters( 'mywp_controller_admin_toolbar_get_toolbar_item' , $toolbar_items );

    if( empty( $toolbar_items ) ) {

      return false;

    }

    ksort( $toolbar_items );

    self::$toolbar_items = $toolbar_items;

    return $toolbar_items;

  }

  private static function get_toolbar_items_added_classes(){

    if( ! empty( self::$toolbar_items_added_classes ) ) {

      return self::$toolbar_items_added_classes;

    }

    $toolbar_items = self::get_toolbar_items();

    if( empty( $toolbar_items ) ) {

      return false;

    }

    foreach( $toolbar_items as $key => $toolbar_item ) {

      if( ! is_object( $toolbar_item ) ) {

        unset( $toolbar_item[ $key ] );

        continue;

      }

      if( empty( $toolbar_item->item_li_class ) ) {

        $toolbar_items[ $key ]->item_li_class = '';

      }

      if( empty( $toolbar_item->item_link_class ) ) {

        $toolbar_items[ $key ]->item_link_class = '';

      }

      if( empty( $toolbar_item->item_link_url ) ) {

        $toolbar_items[ $key ]->item_link_url = '';

      }

      if( empty( $toolbar_item->item_link_url_parse ) ) {

        $toolbar_items[ $key ]->item_link_url_parse = array();

      }

      if( empty( $toolbar_item->item_link_url_parse_query ) ) {

        $toolbar_items[ $key ]->item_link_url_parse_query = array();

      }

      $toolbar_item->item_link_url = do_shortcode( $toolbar_item->item_link_url );

      $toolbar_items[ $key ]->item_link_url = $toolbar_item->item_link_url;

      if( ! empty( $toolbar_item->item_link_url ) ) {

        $item_link_url_parse = parse_url( $toolbar_item->item_link_url );

        if( isset( $item_link_url_parse['fragment'] ) ) {

          unset( $item_link_url_parse['fragment'] );

        }

        if( empty( $item_link_url_parse['query'] ) ) {

          $item_link_url_parse['query'] = '';

        }

        $toolbar_items[ $key ]->item_link_url_parse = $item_link_url_parse;

        if( ! empty( $item_link_url_parse['query'] ) ) {

          wp_parse_str( $item_link_url_parse['query'] , $item_link_url_parse_query );

          ksort( $item_link_url_parse_query );

          $toolbar_items[ $key ]->item_link_url_parse_query = $item_link_url_parse_query;

        }

      }

    }

    foreach( $toolbar_items as $key => $toolbar_item ) {

      self::$child_items[ $toolbar_item->item_parent ][] = $toolbar_item;

    }

    $tmp = $toolbar_items;

    foreach( $toolbar_items as $key => $toolbar_item ) {

      if( empty( $toolbar_item->item_parent ) ) {

        continue;

      }

      foreach( $tmp as $tmp_key => $tmp_toolbar_item ) {

        if( $tmp_toolbar_item->ID == $toolbar_item->item_parent ) {

          self::$parent_items[ $toolbar_item->ID ][] = $tmp_toolbar_item;

          break;

        }

      }

    }

    unset( $tmp );

    $first = true;

    foreach( $toolbar_items as $key => $toolbar_item ) {

    }

    $current_url = self::get_current_url();

    $current_url_parse = parse_url( $current_url );
    $current_url_query = array();

    if( isset( $current_url_parse['fragment'] ) ) {

      unset( $current_url_parse['fragment'] );

    }

    if( ! empty( $current_url_parse['query'] ) ) {

      wp_parse_str( $current_url_parse['query'] , $current_url_query );

      ksort( $current_url_query );

    } else {

      $current_url_parse['query'] = '';

    }

    $found_current_item_ids = array();

    foreach( $toolbar_items as $key => $toolbar_item ) {

      if( empty( $toolbar_item->item_link_url_parse['host'] ) or empty( $toolbar_item->item_link_url_parse['path'] ) ) {

        continue;

      }

      if(
        $current_url_parse['scheme'] === $toolbar_item->item_link_url_parse['scheme'] &&
        $current_url_parse['host'] === $toolbar_item->item_link_url_parse['host'] &&
        $current_url_parse['path'] === $toolbar_item->item_link_url_parse['path'] &&
        $current_url_query === $toolbar_item->item_link_url_parse_query
        ) {

        $found_current_item_ids[] = $toolbar_item->ID;

      }

    }

    if( empty( $found_current_item_ids ) ) {

      $identification_query = array();

      if( ! empty( $current_url_query['page'] ) ) {

        $identification_query['page'] = $current_url_query['page'];

      }

      if( ! empty( $current_url_query['post_type'] ) ) {

        if( $current_url_query['post_type'] != 'post' ) {

          $identification_query['post_type'] = $current_url_query['post_type'];

        }

      }

      if( ! empty( $current_url_query['taxonomy'] ) ) {

        $identification_query['taxonomy'] = $current_url_query['taxonomy'];

      }

      foreach( $toolbar_items as $key => $toolbar_item ) {

        if( empty( $toolbar_item->item_link_url_parse['host'] ) or empty( $toolbar_item->item_link_url_parse['path'] ) ) {

          continue;

        }

        if(
          $current_url_parse['scheme'] === $toolbar_item->item_link_url_parse['scheme'] &&
          $current_url_parse['host'] === $toolbar_item->item_link_url_parse['host'] &&
          $current_url_parse['path'] === $toolbar_item->item_link_url_parse['path'] &&
          http_build_query( $identification_query ) === $toolbar_item->item_link_url_parse['query']
        ) {

          $found_current_item_ids[] = $toolbar_item->ID;

        }

      }

    }

    $found_current_item_ids = apply_filters( 'mywp_controller_admin_toolbar_get_toolbar_item_added_classes_found_current_item_ids' , $found_current_item_ids , $toolbar_items , $current_url , $current_url_parse , $current_url_query );

    if( ! empty( $found_current_item_ids ) ) {

      $found_current_item_ids = array_map( 'strip_tags' , $found_current_item_ids );

    }

    if( ! empty( $found_current_item_ids ) ) {

      foreach( $toolbar_items as $key => $toolbar_item ) {

        if( in_array( $toolbar_item->ID , $found_current_item_ids ) ) {

          $toolbar_items[ $key ]->item_li_class .= ' current';
          $toolbar_items[ $key ]->item_link_class .= ' current';

        }

      }

    }

    $toolbar_items = apply_filters( 'mywp_controller_admin_toolbar_get_toolbar_item_added_classes' , $toolbar_items );

    self::$toolbar_items_added_classes = $toolbar_items;

    return self::$toolbar_items_added_classes;

  }

  private static function get_find_menu_items_to_parent_id( $parent_id = 0 ) {

    $toolbar_items_added_classes = self::get_toolbar_items_added_classes();

    if( empty( $toolbar_items_added_classes ) ) {

      return false;

    }

    if( ! empty( $parent_id ) ) {

      if( is_numeric( $parent_id ) ) {

        $parent_id = intval( $parent_id );

      } else {

        $parent_id = strip_tags( $parent_id );

      }

    }

    if( ! empty( self::$find_parent_id[ $parent_id ] ) ) {

      return self::$find_parent_id[ $parent_id ];

    }

    $find_items = array();

    foreach( $toolbar_items_added_classes as $item ) {

      $item_parent = $item->item_parent;

      if( ! empty( $item_parent ) ) {

        if( is_numeric( $item_parent ) ) {

          $item_parent = intval( $item_parent );

        } else {

          $item_parent = strip_tags( $item_parent );

        }

      }

      if( $item_parent !== $parent_id ) {

        continue;

      }

      $find_items[] = $item;

    }

    if( empty( $find_items ) ) {

      return false;

    }

    self::$find_parent_id[ $parent_id ] = $find_items;

    return $find_items;

  }

  private static function get_current_url() {

    if( ! empty( self::$current_url ) ) {

      return self::$current_url;

    }

    $current_url = urldecode( do_shortcode( '[mywp_url current="1"]' ) );

    self::$current_url = $current_url;

    return $current_url;

  }

  public static function admin_enqueue_scripts() {

    $toolbar = self::get_toolbar();

    if( empty( $toolbar ) ) {

      return false;

    }

    wp_register_style( 'mywp_admin_toolbar' , MywpApi::get_plugin_url( 'css' ) . 'admin-toolbar.css' , array() , MYWP_VERSION );
    wp_register_script( 'mywp_admin_toolbar' , MywpApi::get_plugin_url( 'js' ) . 'admin-toolbar.js' , array( 'jquery' ) , MYWP_VERSION );

    wp_enqueue_style( 'mywp_admin_toolbar' );
    wp_enqueue_script( 'mywp_admin_toolbar' );

    $setting_data = self::get_setting_data();

    if( ! empty( $setting_data['custom_menu_ui'] ) ) {

      wp_register_style( 'mywp_admin_toolbar_custom_ui' , MywpApi::get_plugin_url( 'css' ) . 'admin-toolbar-custom-ui.css' , array( 'mywp_admin_toolbar' ) , MYWP_VERSION );
      wp_register_script( 'mywp_admin_toolbar_custom_ui' , MywpApi::get_plugin_url( 'js' ) . 'admin-toolbar-custom-ui.js' , array( 'jquery' , 'mywp_admin_toolbar' ) , MYWP_VERSION );

      wp_enqueue_style( 'mywp_admin_toolbar_custom_ui' );
      wp_enqueue_script( 'mywp_admin_toolbar_custom_ui' );

    }

  }

  public static function remove_detault_menus() {

    global $wp_admin_bar;

    if( empty( $wp_admin_bar ) ) {

      return false;

    }

    if( ! self::is_do_function( __FUNCTION__ ) ) {

      return false;

    }

    $parent_items = self::get_find_menu_items_to_parent_id();

    if( empty( $parent_items ) ) {

      return false;

    }

    $admin_bar_all_nodes = $wp_admin_bar->get_nodes();

    foreach( $admin_bar_all_nodes as $node ) {

      if( $node->id === 'top-secondary' ) {

        continue;

      }

      $wp_admin_bar->remove_menu( $node->id );

    }

    self::after_do_function( __FUNCTION__ );

  }

  public static function customize_admin_bar() {

    global $wp_admin_bar;

    if( empty( $wp_admin_bar ) ) {

      return false;

    }

    if( ! self::is_do_function( __FUNCTION__ ) ) {

      return false;

    }

    $parent_items = self::get_find_menu_items_to_parent_id();

    if( empty( $parent_items ) ) {

      return false;

    }

    foreach( $parent_items as $item ) {

      self::add_toolbar_item( $item );

    }

    self::after_do_function( __FUNCTION__ );

  }

  private static function add_toolbar_item( $item ) {

    global $wp_admin_bar;

    if( empty( $item ) or empty( $item->item_type ) or empty( $item->ID ) ) {

      return false;

    }

    $toolbar_items_added_classes = self::get_toolbar_items_added_classes();

    $item = apply_filters( 'mywp_controller_admin_toolbar_add_toolbar_item' , $item );

    if( ! empty( $item->item_capability ) ) {

      if( ! current_user_can( $item->item_capability ) ) {

        return false;

      }

    }

    $item_id = $item->ID;

    if( is_numeric( $item_id ) ) {

      $item_id = intval( $item_id );

    } else {

      $item_id = strip_tags( $item_id );

    }


    $item_type = $item->item_type;
    $node_parent = $item->item_parent;
    $item_location = $item->item_location;

    $item->item_link_title = do_shortcode( $item->item_link_title );

    $li_class = '';

    if( ! empty( $item->item_li_class ) ) {

      $li_class = $item->item_li_class;

    }

    $li_id = '';

    if( ! empty( $item->item_li_id ) ) {

      $li_id = $item->item_li_id;

    }

    if( $node_parent == '0' ) {

      $node_parent = '';

    }

    if( $item_location === 'right' ) {

      if( $node_parent == '' ) {

        $node_parent = 'top-secondary';

      }

    }

    $node_id = $item->ID;

    if( ! empty( $item->item_default_id ) ) {

      $node_id = $item->item_default_id;

      foreach( $toolbar_items_added_classes as $toolbar_items_added_classes_item ) {

        if( $toolbar_items_added_classes_item->ID === $item->item_parent ) {

          $node_parent = $toolbar_items_added_classes_item->item_default_id;

          break;

        }

      }

    }

    $node_group = false;

    if( $item_type === 'group' ) {

      $node_group = 1;

    } elseif( $item_type === 'custom' ) {

      $item->item_meta['html'] = $item->item_custom_html;

    }

    if( ! empty( $item->item_icon_class ) ) {

      $title = sprintf( '<span class="%s"></span>' , esc_attr( $item->item_icon_class ) ) . $item->item_link_title;

    } else {

      $title = $item->item_link_title;

    }

    if( empty( $node_group ) ) {

      $add_menu = array( 'id' => $node_id , 'title' => $title , 'parent' => $node_parent , 'href' => $item->item_link_url , 'group' => $node_group , 'meta' => $item->item_meta );

      $wp_admin_bar->add_menu( $add_menu );

    } else {

      $add_menu = array( 'id' => $node_id , 'parent' => $node_parent , 'group' => $node_group , 'meta' => $item->item_meta );

      $wp_admin_bar->add_group( $add_menu );

    }

    $child_items = self::get_find_menu_items_to_parent_id( $item_id );

    if( ! empty( $child_items ) ) {

      foreach( $child_items as $child_item ) {

        self::add_toolbar_item( $child_item );

      }

    }

  }

}

MywpControllerModuleAdminToolbar::init();

endif;

```
