WordPress.org

Make WordPress Core

Ticket #19206: 19206.diff

File 19206.diff, 9.9 KB (added by koopersmith, 20 months ago)
  • wp-admin/menu-header.php

     
    3636function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { 
    3737        global $self, $parent_file, $submenu_file, $plugin_page, $pagenow, $typenow; 
    3838 
    39         $first = true; 
    40         // 0 = name, 1 = capability, 2 = file, 3 = class, 4 = id, 5 = icon src 
     39        $first_key = key( $menu ); 
     40 
     41        // 0 = title, 1 = capability, 2 = file, 3 = page title (ignore), 4 = class, 5 = id, 6 = icon src 
    4142        foreach ( $menu as $key => $item ) { 
    42                 $admin_is_parent = false; 
    43                 $class = array(); 
    4443 
    45                 if ( $first ) { 
    46                         $class[] = 'wp-first-item'; 
    47                         $first = false; 
    48                 } 
     44                $item = array_pad( $item, 7, '' ); 
     45                list( $title, $capability, $slug, $ignore, $class, $id, $icon_src ) = $item; 
    4946 
    50                 $submenu_items = false; 
    51                 if ( ! empty( $submenu[$item[2]] ) ) { 
    52                         $class[] = 'wp-has-submenu'; 
    53                         $submenu_items = $submenu[$item[2]]; 
    54                 } 
     47                // @todo: Clarify whether we should texturize or not. 
     48                $title = wptexturize( $title ); 
    5549 
    56                 if ( ( $parent_file && $item[2] == $parent_file ) || ( empty($typenow) && $self == $item[2] ) ) 
    57                         $class[] = ! empty( $submenu_items ) ? 'wp-has-current-submenu wp-menu-open' : 'current'; 
     50                $has_submenu   = ! empty( $submenu[ $slug ] ); 
     51                $submenu_items = ( $has_submenu ) ? $submenu[ $slug ] : false; 
     52 
     53                $parent = $slug; 
     54 
     55                if ( $key == $first_key ) 
     56                        $class .= ' wp-first-item'; 
     57 
     58                if ( $has_submenu ) 
     59                        $class .= ' wp-has-submenu'; 
     60 
     61                if ( ( $parent_file && $slug == $parent_file ) || ( empty( $typenow ) && $self == $slug ) ) 
     62                        $class .= ( $has_submenu ) ? ' wp-has-current-submenu wp-menu-open' : ' current'; 
    5863                else 
    59                         $class[] = 'wp-not-current-submenu'; 
     64                        $class .= ' wp-not-current-submenu'; 
    6065 
    61                 if ( ! empty( $item[4] ) ) 
    62                         $class[] = $item[4]; 
     66                // GENERATE MENU ITEM 
     67                if ( $submenu_as_parent && $has_submenu ) { 
     68                        $submenu_items = array_values( $submenu_items );  // Re-index. 
     69                        $item_slug     = $submenu_items[0][2]; 
     70                        $item_hook     = get_plugin_page_hook( $item_slug, $slug ); 
    6371 
    64                 $class = $class ? ' class="' . join( ' ', $class ) . '"' : ''; 
    65                 $tabindex = ' tabindex="1"'; 
    66                 $id = ! empty( $item[5] ) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : ''; 
    67                 $img = ''; 
    68                 if ( ! empty( $item[6] ) ) 
    69                         $img = ( 'div' === $item[6] ) ? '<br />' : '<img src="' . $item[6] . '" alt="" />'; 
    70                 $arrow = '<div class="wp-menu-arrow"><div></div></div>'; 
     72                } elseif ( ! empty( $slug ) && current_user_can( $capability ) ) { 
     73                        $item_slug = $slug; 
     74                        $item_hook = get_plugin_page_hook( $item_slug, 'admin.php' ); 
     75                } 
    7176 
    72                 $title = wptexturize( $item[0] ); 
     77                if ( isset( $item_slug ) ) { 
     78                        $item_file = $item_slug; 
     79                        if ( false !== ( $pos = strpos( $item_file, '?' ) ) ) 
     80                                $item_file = substr( $item_file, 0, $pos ); 
    7381 
    74                 echo "\n\t<li$class$id>"; 
     82                        $item_url = $item_slug; 
     83                        if ( ! empty( $item_hook ) || ( ('index.php' != $item_slug ) && file_exists( WP_PLUGIN_DIR . "/$item_file" ) ) ) { 
     84                                $parent   = 'admin.php'; 
     85                                $item_url = "admin.php?page=$item_slug"; 
     86                        } 
     87                } 
    7588 
     89                // RENDER MENU ITEM 
     90                echo '<li id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '">'; 
     91 
    7692                if ( false !== strpos( $class, 'wp-menu-separator' ) ) { 
    7793                        echo '<div class="separator"></div>'; 
    78                 } elseif ( $submenu_as_parent && ! empty( $submenu_items ) ) { 
    79                         $submenu_items = array_values( $submenu_items );  // Re-index. 
    80                         $menu_hook = get_plugin_page_hook( $submenu_items[0][2], $item[2] ); 
    81                         $menu_file = $submenu_items[0][2]; 
    82                         if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) 
    83                                 $menu_file = substr( $menu_file, 0, $pos ); 
    84                         if ( ! empty( $menu_hook ) || ( ('index.php' != $submenu_items[0][2]) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) ) ) { 
    85                                 $admin_is_parent = true; 
    86                                 echo "<div class='wp-menu-image'><a href='admin.php?page={$submenu_items[0][2]}'>$img</a></div>$arrow<a href='admin.php?page={$submenu_items[0][2]}'$class$tabindex>$title</a>"; 
    87                         } else { 
    88                                 echo "\n\t<div class='wp-menu-image'><a href='{$submenu_items[0][2]}'>$img</a></div>$arrow<a href='{$submenu_items[0][2]}'$class$tabindex>$title</a>"; 
     94 
     95                } elseif ( isset( $item_slug ) ) { 
     96                        // Menu image 
     97                        echo '<div class="wp-menu-image">'; 
     98                        echo '<a href="' . esc_url( $item_url ) . '">'; 
     99 
     100                        if ( ! empty( $icon_src ) ) { 
     101                                if ( 'div' === $icon_src ) 
     102                                        echo '<br />'; 
     103                                else 
     104                                        echo '<img src="' . esc_url( $icon_src ) . '" alt="" />'; 
    89105                        } 
    90                 } elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) { 
    91                         $menu_hook = get_plugin_page_hook( $item[2], 'admin.php' ); 
    92                         $menu_file = $item[2]; 
     106 
     107                        echo '</a></div>'; 
     108 
     109                        // Menu arrow 
     110                        echo '<div class="wp-menu-arrow"><div></div></div>'; 
     111 
     112                        // Menu title 
     113                        echo '<a href="' . esc_url( $item_url ) . '" class="' . esc_attr( $class ) . '" tabindex="1">'; 
     114                        echo $title; 
     115                        echo '</a>'; 
     116                } 
     117 
     118 
     119                // HANDLE SUBMENUS 
     120                if ( $has_submenu ) { 
     121 
     122                        // Submenu wrapper. 
     123                        echo "<div class='wp-submenu'><div class='wp-submenu-wrap'>"; 
     124                        echo "<div class='wp-submenu-head'>$title</div><ul>"; 
     125 
     126                        $menu_file = $slug; 
    93127                        if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) 
    94128                                $menu_file = substr( $menu_file, 0, $pos ); 
    95                         if ( ! empty( $menu_hook ) || ( ('index.php' != $item[2]) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) ) ) { 
    96                                 $admin_is_parent = true; 
    97                                 echo "\n\t<div class='wp-menu-image'><a href='admin.php?page={$item[2]}'>$img</a></div>$arrow<a href='admin.php?page={$item[2]}'$class$tabindex>{$item[0]}</a>"; 
    98                         } else { 
    99                                 echo "\n\t<div class='wp-menu-image'><a href='{$item[2]}'>$img</a></div>$arrow<a href='{$item[2]}'$class$tabindex>{$item[0]}</a>"; 
    100                         } 
    101                 } 
    102129 
    103                 if ( ! empty( $submenu_items ) ) { 
    104                         echo "\n\t<div class='wp-submenu'><div class='wp-submenu-wrap'>"; 
    105                         echo "<div class='wp-submenu-head'>{$item[0]}</div><ul>"; 
    106                         $first = true; 
     130                        // Clarify the location of the parent. 
     131                         if ( ( 'admin.php' != $parent && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! is_dir( WP_PLUGIN_DIR . "/$slug" ) ) || file_exists( $menu_file ) ) 
     132                                $parent = $slug; 
     133                        else 
     134                                $parent = 'admin.php'; 
     135 
     136                        // Handle current for post_type=post|page|foo pages, which won't match $self. 
     137                        $self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing'; 
     138 
     139                        $first_sub_key = key( $submenu_items ); 
     140 
    107141                        foreach ( $submenu_items as $sub_key => $sub_item ) { 
    108                                 if ( ! current_user_can( $sub_item[1] ) ) 
     142                                // 0 = title, 1 = cap, 2 = slug, 3 = page title 
     143                                list( $sub_title, $sub_cap, $sub_slug ) = $sub_item; 
     144 
     145                                if ( ! current_user_can( $sub_cap ) ) 
    109146                                        continue; 
    110147 
    111                                 $class = array(); 
    112                                 if ( $first ) { 
    113                                         $class[] = 'wp-first-item'; 
    114                                         $first = false; 
    115                                 } 
     148                                $sub_title = wptexturize( $sub_title ); 
     149                                $class = ''; 
    116150 
    117                                 $menu_file = $item[2]; 
     151                                if ( $sub_key == $first_sub_key ) 
     152                                        $class .= ' wp-first-item'; 
    118153 
    119                                 if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) 
    120                                         $menu_file = substr( $menu_file, 0, $pos ); 
     154                                // Determine if we are on the current page. 
     155                                if ( isset( $submenu_file ) ) { 
     156                                        if ( $submenu_file == $sub_slug ) 
     157                                                $class .= ' current'; 
    121158 
    122                                 // Handle current for post_type=post|page|foo pages, which won't match $self. 
    123                                 $self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing'; 
    124  
    125                                 if ( isset( $submenu_file ) ) { 
    126                                         if ( $submenu_file == $sub_item[2] ) 
    127                                                 $class[] = 'current'; 
    128159                                // If plugin_page is set the parent must either match the current page or not physically exist. 
    129160                                // This allows plugin pages with the same hook to exist under different parents. 
    130161                                } else if ( 
    131                                         ( ! isset( $plugin_page ) && $self == $sub_item[2] ) || 
    132                                         ( isset( $plugin_page ) && $plugin_page == $sub_item[2] && ( $item[2] == $self_type || $item[2] == $self || file_exists($menu_file) === false ) ) 
     162                                        ( ! isset( $plugin_page ) && $self == $sub_slug ) || 
     163                                        ( isset( $plugin_page ) && $plugin_page == $sub_slug && 
     164                                                ( $slug == $self_type || $slug == $self || file_exists( $menu_file ) === false ) 
     165                                        ) 
    133166                                ) { 
    134                                         $class[] = 'current'; 
     167                                        $class .= ' current'; 
    135168                                } 
    136169 
    137                                 $class = $class ? ' class="' . join( ' ', $class ) . '"' : ''; 
    138  
    139                                 $menu_hook = get_plugin_page_hook($sub_item[2], $item[2]); 
    140                                 $sub_file = $sub_item[2]; 
     170                                $sub_hook = get_plugin_page_hook( $sub_slug, $slug ); 
     171                                $sub_file  = $sub_slug; 
    141172                                if ( false !== ( $pos = strpos( $sub_file, '?' ) ) ) 
    142                                         $sub_file = substr($sub_file, 0, $pos); 
     173                                        $sub_file = substr( $sub_file, 0, $pos ); 
    143174 
    144                                 $title = wptexturize($sub_item[0]); 
    145175 
    146                                 if ( ! empty( $menu_hook ) || ( ('index.php' != $sub_item[2]) && file_exists( WP_PLUGIN_DIR . "/$sub_file" ) ) ) { 
    147                                         // If admin.php is the current page or if the parent exists as a file in the plugins or admin dir 
    148                                         if ( (!$admin_is_parent && file_exists(WP_PLUGIN_DIR . "/$menu_file") && !is_dir(WP_PLUGIN_DIR . "/{$item[2]}")) || file_exists($menu_file) ) 
    149                                                 $sub_item_url = add_query_arg( array('page' => $sub_item[2]), $item[2] ); 
    150                                         else 
    151                                                 $sub_item_url = add_query_arg( array('page' => $sub_item[2]), 'admin.php' ); 
     176                                $sub_url = $sub_slug; 
     177                                if ( ! empty( $sub_hook ) || ( ( 'index.php' != $sub_slug ) && file_exists( WP_PLUGIN_DIR . "/$sub_file" ) ) ) { 
     178                                        $sub_url = add_query_arg( array( 'page' => $sub_slug ), $parent ); 
     179                                } 
    152180 
    153                                         $sub_item_url = esc_url( $sub_item_url ); 
    154                                         echo "<li$class><a href='$sub_item_url'$class$tabindex>$title</a></li>"; 
    155                                 } else { 
    156                                         echo "<li$class><a href='{$sub_item[2]}'$class$tabindex>$title</a></li>"; 
    157                                 } 
     181                                // RENDER SUBMENU ITEM 
     182                                echo '<li class="' . esc_attr( $class ) . '">'; 
     183                                echo '<a href="' . esc_url( $sub_url ) . '" class="' . esc_attr( $class ) . '" tabindex="1">'; 
     184                                echo $sub_title; 
     185                                echo '</a></li>'; 
    158186                        } 
    159                         echo "</ul></div></div>"; 
     187 
     188                        echo '</ul></div></div>'; 
    160189                } 
    161                 echo "</li>"; 
     190                echo '</li>'; 
    162191        } 
    163192 
    164193        echo '<li id="collapse-menu" class="hide-if-no-js"><div id="collapse-button"><div></div></div>';