[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/ -> menu-header.php (source)

   1  <?php
   2  /**
   3   * Displays Administration Menu.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  // Don't load directly.
  10  if ( ! defined( 'ABSPATH' ) ) {
  11      die( '-1' );
  12  }
  13  
  14  /**
  15   * The current page.
  16   *
  17   * @global string $self
  18   */
  19  $self = preg_replace( '|^.*/wp-admin/network/|i', '', $_SERVER['PHP_SELF'] );
  20  $self = preg_replace( '|^.*/wp-admin/|i', '', $self );
  21  $self = preg_replace( '|^.*/plugins/|i', '', $self );
  22  $self = preg_replace( '|^.*/mu-plugins/|i', '', $self );
  23  
  24  /**
  25   * For when admin-header is included from within a function.
  26   *
  27   * @global array  $menu
  28   * @global array  $submenu
  29   * @global string $parent_file
  30   * @global string $submenu_file
  31   */
  32  global $menu, $submenu, $parent_file, $submenu_file;
  33  
  34  /**
  35   * Filters the parent file of an admin menu sub-menu item.
  36   *
  37   * Allows plugins to move sub-menu items around.
  38   *
  39   * @since MU (3.0.0)
  40   *
  41   * @param string $parent_file The parent file.
  42   */
  43  $parent_file = apply_filters( 'parent_file', $parent_file );
  44  
  45  /**
  46   * Filters the file of an admin menu sub-menu item.
  47   *
  48   * @since 4.4.0
  49   *
  50   * @param string $submenu_file The submenu file.
  51   * @param string $parent_file  The submenu item's parent file.
  52   */
  53  $submenu_file = apply_filters( 'submenu_file', $submenu_file, $parent_file );
  54  
  55  get_admin_page_parent();
  56  
  57  /**
  58   * Display menu.
  59   *
  60   * @access private
  61   * @since 2.7.0
  62   *
  63   * @global string $self
  64   * @global string $parent_file
  65   * @global string $submenu_file
  66   * @global string $plugin_page
  67   * @global string $typenow      The post type of the current screen.
  68   *
  69   * @param array $menu
  70   * @param array $submenu
  71   * @param bool  $submenu_as_parent
  72   */
  73  function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) {
  74      global $self, $parent_file, $submenu_file, $plugin_page, $typenow;
  75  
  76      $first = true;
  77      // 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes, 5 = hookname, 6 = icon_url.
  78      foreach ( $menu as $key => $item ) {
  79          $admin_is_parent = false;
  80          $class           = array();
  81          $aria_attributes = '';
  82          $aria_hidden     = '';
  83          $is_separator    = false;
  84  
  85          if ( $first ) {
  86              $class[] = 'wp-first-item';
  87              $first   = false;
  88          }
  89  
  90          $submenu_items = array();
  91          if ( ! empty( $submenu[ $item[2] ] ) ) {
  92              $class[]       = 'wp-has-submenu';
  93              $submenu_items = $submenu[ $item[2] ];
  94          }
  95  
  96          if ( ( $parent_file && $item[2] === $parent_file ) || ( empty( $typenow ) && $self === $item[2] ) ) {
  97              if ( ! empty( $submenu_items ) ) {
  98                  $class[] = 'wp-has-current-submenu wp-menu-open';
  99              } else {
 100                  $class[]          = 'current';
 101                  $aria_attributes .= 'aria-current="page"';
 102              }
 103          } else {
 104              $class[] = 'wp-not-current-submenu';
 105              if ( ! empty( $submenu_items ) ) {
 106                  $aria_attributes .= 'data-ariahaspopup';
 107              }
 108          }
 109  
 110          if ( ! empty( $item[4] ) ) {
 111              $class[] = esc_attr( $item[4] );
 112          }
 113  
 114          $class     = $class ? ' class="' . implode( ' ', $class ) . '"' : '';
 115          $id        = ! empty( $item[5] ) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : '';
 116          $img       = '';
 117          $img_style = '';
 118          $img_class = ' dashicons-before';
 119  
 120          if ( str_contains( $class, 'wp-menu-separator' ) ) {
 121              $is_separator = true;
 122          }
 123  
 124          /*
 125           * If the string 'none' (previously 'div') is passed instead of a URL, don't output
 126           * the default menu image so an icon can be added to div.wp-menu-image as background
 127           * with CSS. Dashicons and base64-encoded data:image/svg_xml URIs are also handled
 128           * as special cases.
 129           */
 130          if ( ! empty( $item[6] ) ) {
 131              $img = '<img src="' . esc_url( $item[6] ) . '" alt="" />';
 132  
 133              if ( 'none' === $item[6] || 'div' === $item[6] ) {
 134                  $img = '<br />';
 135              } elseif ( str_starts_with( $item[6], 'data:image/svg+xml;base64,' ) ) {
 136                  $img = '<br />';
 137                  // The value is base64-encoded data, so esc_attr() is used here instead of esc_url().
 138                  $img_style = ' style="background-image:url(\'' . esc_attr( $item[6] ) . '\')"';
 139                  $img_class = ' svg';
 140              } elseif ( str_starts_with( $item[6], 'dashicons-' ) ) {
 141                  $img       = '<br />';
 142                  $img_class = ' dashicons-before ' . sanitize_html_class( $item[6] );
 143              }
 144          }
 145  
 146          $title = wptexturize( $item[0] );
 147  
 148          // Hide separators from screen readers.
 149          if ( $is_separator ) {
 150              $aria_hidden = ' aria-hidden="true"';
 151          }
 152  
 153          echo "\n\t<li$class$id$aria_hidden>";
 154  
 155          if ( $is_separator ) {
 156              echo '<div class="separator"></div>';
 157          } elseif ( $submenu_as_parent && ! empty( $submenu_items ) ) {
 158              $submenu_items = array_values( $submenu_items );  // Re-index.
 159              $menu_hook     = get_plugin_page_hook( $submenu_items[0][2], $item[2] );
 160              $menu_file     = $submenu_items[0][2];
 161              $pos           = strpos( $menu_file, '?' );
 162  
 163              if ( false !== $pos ) {
 164                  $menu_file = substr( $menu_file, 0, $pos );
 165              }
 166  
 167              if ( ! empty( $menu_hook )
 168                  || ( ( 'index.php' !== $submenu_items[0][2] )
 169                      && file_exists( WP_PLUGIN_DIR . "/$menu_file" )
 170                      && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) )
 171              ) {
 172                  $admin_is_parent = true;
 173                  echo "<a href='admin.php?page={$submenu_items[0][2]}'$class $aria_attributes><div class='wp-menu-image$img_class'$img_style aria-hidden='true'>$img</div><div class='wp-menu-name'>$title</div></a>";
 174              } else {
 175                  echo "\n\t<a href='{$submenu_items[0][2]}'$class $aria_attributes><div class='wp-menu-image$img_class'$img_style aria-hidden='true'>$img</div><div class='wp-menu-name'>$title</div></a>";
 176              }
 177          } elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) {
 178              $menu_hook = get_plugin_page_hook( $item[2], 'admin.php' );
 179              $menu_file = $item[2];
 180              $pos       = strpos( $menu_file, '?' );
 181  
 182              if ( false !== $pos ) {
 183                  $menu_file = substr( $menu_file, 0, $pos );
 184              }
 185  
 186              if ( ! empty( $menu_hook )
 187                  || ( ( 'index.php' !== $item[2] )
 188                      && file_exists( WP_PLUGIN_DIR . "/$menu_file" )
 189                      && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) )
 190              ) {
 191                  $admin_is_parent = true;
 192                  echo "\n\t<a href='admin.php?page={$item[2]}'$class $aria_attributes><div class='wp-menu-image$img_class'$img_style aria-hidden='true'>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>";
 193              } else {
 194                  echo "\n\t<a href='{$item[2]}'$class $aria_attributes><div class='wp-menu-image$img_class'$img_style aria-hidden='true'>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>";
 195              }
 196          }
 197  
 198          if ( ! empty( $submenu_items ) ) {
 199              echo "\n\t<ul class='wp-submenu wp-submenu-wrap'>";
 200              echo "<li class='wp-submenu-head' aria-hidden='true'>{$item[0]}</li>";
 201  
 202              $first = true;
 203  
 204              // 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes.
 205              foreach ( $submenu_items as $sub_key => $sub_item ) {
 206                  if ( ! current_user_can( $sub_item[1] ) ) {
 207                      continue;
 208                  }
 209  
 210                  $class           = array();
 211                  $aria_attributes = '';
 212  
 213                  if ( $first ) {
 214                      $class[] = 'wp-first-item';
 215                      $first   = false;
 216                  }
 217  
 218                  $menu_file = $item[2];
 219                  $pos       = strpos( $menu_file, '?' );
 220  
 221                  if ( false !== $pos ) {
 222                      $menu_file = substr( $menu_file, 0, $pos );
 223                  }
 224  
 225                  // Handle current for post_type=post|page|foo pages, which won't match $self.
 226                  $self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing';
 227  
 228                  if ( isset( $submenu_file ) ) {
 229                      if ( $submenu_file === $sub_item[2] ) {
 230                          $class[]          = 'current';
 231                          $aria_attributes .= ' aria-current="page"';
 232                      }
 233                      /*
 234                       * If plugin_page is set the parent must either match the current page or not physically exist.
 235                       * This allows plugin pages with the same hook to exist under different parents.
 236                       */
 237                  } elseif (
 238                      ( ! isset( $plugin_page ) && $self === $sub_item[2] )
 239                      || ( isset( $plugin_page ) && $plugin_page === $sub_item[2]
 240                          && ( $item[2] === $self_type || $item[2] === $self || file_exists( $menu_file ) === false ) )
 241                  ) {
 242                      $class[]          = 'current';
 243                      $aria_attributes .= ' aria-current="page"';
 244                  }
 245  
 246                  if ( ! empty( $sub_item[4] ) ) {
 247                      $class[] = esc_attr( $sub_item[4] );
 248                  }
 249  
 250                  $class = $class ? ' class="' . implode( ' ', $class ) . '"' : '';
 251  
 252                  $menu_hook = get_plugin_page_hook( $sub_item[2], $item[2] );
 253                  $sub_file  = $sub_item[2];
 254                  $pos       = strpos( $sub_file, '?' );
 255                  if ( false !== $pos ) {
 256                      $sub_file = substr( $sub_file, 0, $pos );
 257                  }
 258  
 259                  $title = wptexturize( $sub_item[0] );
 260  
 261                  if ( ! empty( $menu_hook )
 262                      || ( ( 'index.php' !== $sub_item[2] )
 263                          && file_exists( WP_PLUGIN_DIR . "/$sub_file" )
 264                          && ! file_exists( ABSPATH . "/wp-admin/$sub_file" ) )
 265                  ) {
 266                      // If admin.php is the current page or if the parent exists as a file in the plugins or admin directory.
 267                      if ( ( ! $admin_is_parent && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! is_dir( WP_PLUGIN_DIR . "/{$item[2]}" ) ) || file_exists( $menu_file ) ) {
 268                          $sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), $item[2] );
 269                      } else {
 270                          $sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), 'admin.php' );
 271                      }
 272  
 273                      $sub_item_url = esc_url( $sub_item_url );
 274                      echo "<li$class><a href='$sub_item_url'$class$aria_attributes>$title</a></li>";
 275                  } else {
 276                      echo "<li$class><a href='{$sub_item[2]}'$class$aria_attributes>$title</a></li>";
 277                  }
 278              }
 279              echo '</ul>';
 280          }
 281          echo '</li>';
 282      }
 283  
 284      echo '<li id="collapse-menu" class="hide-if-no-js">' .
 285          '<button type="button" id="collapse-button" aria-label="' . esc_attr__( 'Collapse Main Menu' ) . '" aria-expanded="true">' .
 286          '<span class="collapse-button-icon" aria-hidden="true"></span>' .
 287          '<span class="collapse-button-label">' . __( 'Collapse Menu' ) . '</span>' .
 288          '</button></li>';
 289  }
 290  
 291  ?>
 292  
 293  <div id="adminmenumain" role="navigation" aria-label="<?php esc_attr_e( 'Main menu' ); ?>">
 294  <a href="#wpbody-content" class="screen-reader-shortcut"><?php _e( 'Skip to main content' ); ?></a>
 295  <a href="#wp-toolbar" class="screen-reader-shortcut"><?php _e( 'Skip to toolbar' ); ?></a>
 296  <div id="adminmenuback"></div>
 297  <div id="adminmenuwrap">
 298  <ul id="adminmenu">
 299  
 300  <?php
 301  
 302  _wp_menu_output( $menu, $submenu );
 303  /**
 304   * Fires after the admin menu has been output.
 305   *
 306   * @since 2.5.0
 307   */
 308  do_action( 'adminmenu' );
 309  
 310  ?>
 311  </ul>
 312  </div>
 313  </div>


Generated : Fri Feb 21 08:20:01 2025 Cross-referenced by PHPXref