Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/menu-header.php

    r42201 r42343  
    1212 * @global string $self
    1313 */
    14 $self = preg_replace('|^.*/wp-admin/network/|i', '', $_SERVER['PHP_SELF']);
    15 $self = preg_replace('|^.*/wp-admin/|i', '', $self);
    16 $self = preg_replace('|^.*/plugins/|i', '', $self);
    17 $self = preg_replace('|^.*/mu-plugins/|i', '', $self);
     14$self = preg_replace( '|^.*/wp-admin/network/|i', '', $_SERVER['PHP_SELF'] );
     15$self = preg_replace( '|^.*/wp-admin/|i', '', $self );
     16$self = preg_replace( '|^.*/plugins/|i', '', $self );
     17$self = preg_replace( '|^.*/mu-plugins/|i', '', $self );
    1818
    1919/**
    … …  
    7373        foreach ( $menu as $key => $item ) {
    7474                $admin_is_parent = false;
    75                 $class = array();
     75                $class           = array();
    7676                $aria_attributes = '';
    77                 $aria_hidden = '';
    78                 $is_separator = false;
     77                $aria_hidden     = '';
     78                $is_separator    = false;
    7979
    8080                if ( $first ) {
    8181                        $class[] = 'wp-first-item';
    82                         $first = false;
     82                        $first   = false;
    8383                }
    8484
    8585                $submenu_items = array();
    86                 if ( ! empty( $submenu[$item[2]] ) ) {
    87                         $class[] = 'wp-has-submenu';
    88                         $submenu_items = $submenu[$item[2]];
    89                 }
    90 
    91                 if ( ( $parent_file && $item[2] == $parent_file ) || ( empty($typenow) && $self == $item[2] ) ) {
     86                if ( ! empty( $submenu[ $item[2] ] ) ) {
     87                        $class[]       = 'wp-has-submenu';
     88                        $submenu_items = $submenu[ $item[2] ];
     89                }
     90
     91                if ( ( $parent_file && $item[2] == $parent_file ) || ( empty( $typenow ) && $self == $item[2] ) ) {
    9292                        if ( ! empty( $submenu_items ) ) {
    9393                                $class[] = 'wp-has-current-submenu wp-menu-open';
    9494                        } else {
    95                                 $class[] = 'current';
     95                                $class[]          = 'current';
    9696                                $aria_attributes .= 'aria-current="page"';
    9797                        }
    9898                } else {
    9999                        $class[] = 'wp-not-current-submenu';
    100                         if ( ! empty( $submenu_items ) )
     100                        if ( ! empty( $submenu_items ) ) {
    101101                                $aria_attributes .= 'aria-haspopup="true"';
    102                 }
    103 
    104                 if ( ! empty( $item[4] ) )
     102                        }
     103                }
     104
     105                if ( ! empty( $item[4] ) ) {
    105106                        $class[] = esc_attr( $item[4] );
    106 
    107                 $class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
    108                 $id = ! empty( $item[5] ) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : '';
    109                 $img = $img_style = '';
     107                }
     108
     109                $class     = $class ? ' class="' . join( ' ', $class ) . '"' : '';
     110                $id        = ! empty( $item[5] ) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : '';
     111                $img       = $img_style = '';
    110112                $img_class = ' dashicons-before';
    111113
    … …  
    126128                                $img = '<br />';
    127129                        } elseif ( 0 === strpos( $item[6], 'data:image/svg+xml;base64,' ) ) {
    128                                 $img = '<br />';
     130                                $img       = '<br />';
    129131                                $img_style = ' style="background-image:url(\'' . esc_attr( $item[6] ) . '\')"';
    130132                                $img_class = ' svg';
    131133                        } elseif ( 0 === strpos( $item[6], 'dashicons-' ) ) {
    132                                 $img = '<br />';
     134                                $img       = '<br />';
    133135                                $img_class = ' dashicons-before ' . sanitize_html_class( $item[6] );
    134136                        }
    … …  
    149151                } elseif ( $submenu_as_parent && ! empty( $submenu_items ) ) {
    150152                        $submenu_items = array_values( $submenu_items );  // Re-index.
    151                         $menu_hook = get_plugin_page_hook( $submenu_items[0][2], $item[2] );
    152                         $menu_file = $submenu_items[0][2];
    153                         if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
     153                        $menu_hook     = get_plugin_page_hook( $submenu_items[0][2], $item[2] );
     154                        $menu_file     = $submenu_items[0][2];
     155                        if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) {
    154156                                $menu_file = substr( $menu_file, 0, $pos );
     157                        }
    155158                        if ( ! empty( $menu_hook ) || ( ( 'index.php' != $submenu_items[0][2] ) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) {
    156159                                $admin_is_parent = true;
    … …  
    162165                        $menu_hook = get_plugin_page_hook( $item[2], 'admin.php' );
    163166                        $menu_file = $item[2];
    164                         if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
     167                        if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) {
    165168                                $menu_file = substr( $menu_file, 0, $pos );
     169                        }
    166170                        if ( ! empty( $menu_hook ) || ( ( 'index.php' != $item[2] ) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) {
    167171                                $admin_is_parent = true;
    … …  
    180184                        // 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes
    181185                        foreach ( $submenu_items as $sub_key => $sub_item ) {
    182                                 if ( ! current_user_can( $sub_item[1] ) )
     186                                if ( ! current_user_can( $sub_item[1] ) ) {
    183187                                        continue;
    184 
    185                                 $class = array();
     188                                }
     189
     190                                $class           = array();
    186191                                $aria_attributes = '';
    187192                                if ( $first ) {
    188193                                        $class[] = 'wp-first-item';
    189                                         $first = false;
     194                                        $first   = false;
    190195                                }
    191196
    192197                                $menu_file = $item[2];
    193198
    194                                 if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
     199                                if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) {
    195200                                        $menu_file = substr( $menu_file, 0, $pos );
     201                                }
    196202
    197203                                // Handle current for post_type=post|page|foo pages, which won't match $self.
    … …  
    200206                                if ( isset( $submenu_file ) ) {
    201207                                        if ( $submenu_file == $sub_item[2] ) {
    202                                                 $class[] = 'current';
     208                                                $class[]          = 'current';
    203209                                                $aria_attributes .= ' aria-current="page"';
    204210                                        }
    205                                 // If plugin_page is set the parent must either match the current page or not physically exist.
    206                                 // This allows plugin pages with the same hook to exist under different parents.
     211                                        // If plugin_page is set the parent must either match the current page or not physically exist.
     212                                        // This allows plugin pages with the same hook to exist under different parents.
    207213                                } elseif (
    208214                                        ( ! isset( $plugin_page ) && $self == $sub_item[2] ) ||
    209                                         ( isset( $plugin_page ) && $plugin_page == $sub_item[2] && ( $item[2] == $self_type || $item[2] == $self || file_exists($menu_file) === false ) )
     215                                        ( isset( $plugin_page ) && $plugin_page == $sub_item[2] && ( $item[2] == $self_type || $item[2] == $self || file_exists( $menu_file ) === false ) )
    210216                                ) {
    211                                         $class[] = 'current';
     217                                        $class[]          = 'current';
    212218                                        $aria_attributes .= ' aria-current="page"';
    213219                                }
    … …  
    219225                                $class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
    220226
    221                                 $menu_hook = get_plugin_page_hook($sub_item[2], $item[2]);
    222                                 $sub_file = $sub_item[2];
    223                                 if ( false !== ( $pos = strpos( $sub_file, '?' ) ) )
    224                                         $sub_file = substr($sub_file, 0, $pos);
    225 
    226                                 $title = wptexturize($sub_item[0]);
     227                                $menu_hook = get_plugin_page_hook( $sub_item[2], $item[2] );
     228                                $sub_file  = $sub_item[2];
     229                                if ( false !== ( $pos = strpos( $sub_file, '?' ) ) ) {
     230                                        $sub_file = substr( $sub_file, 0, $pos );
     231                                }
     232
     233                                $title = wptexturize( $sub_item[0] );
    227234
    228235                                if ( ! empty( $menu_hook ) || ( ( 'index.php' != $sub_item[2] ) && file_exists( WP_PLUGIN_DIR . "/$sub_file" ) && ! file_exists( ABSPATH . "/wp-admin/$sub_file" ) ) ) {
    229236                                        // If admin.php is the current page or if the parent exists as a file in the plugins or admin dir
    230                                         if ( ( ! $admin_is_parent && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! is_dir( WP_PLUGIN_DIR . "/{$item[2]}" ) ) || file_exists( $menu_file ) )
     237                                        if ( ( ! $admin_is_parent && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! is_dir( WP_PLUGIN_DIR . "/{$item[2]}" ) ) || file_exists( $menu_file ) ) {
    231238                                                $sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), $item[2] );
    232                                         else
     239                                        } else {
    233240                                                $sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), 'admin.php' );
     241                                        }
    234242
    235243                                        $sub_item_url = esc_url( $sub_item_url );
    … …  
    239247                                }
    240248                        }
    241                         echo "</ul>";
    242                 }
    243                 echo "</li>";
     249                        echo '</ul>';
     250                }
     251                echo '</li>';
    244252        }
    245253
Note: See TracChangeset for help on using the changeset viewer.