Make WordPress Core


Ignore:
Timestamp:
06/16/2020 06:36:42 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Fix WPCS issues in wp-admin/nav-menus.php.

Includes minor code layout fixes for better readability.

See #49542.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/nav-menus.php

    r48052 r48063  
    6161    case 'add-menu-item':
    6262        check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' );
     63
    6364        if ( isset( $_REQUEST['nav-menu-locations'] ) ) {
    6465            set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) );
     
    6667            wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] );
    6768        }
     69
    6870        break;
     71
    6972    case 'move-down-menu-item':
    7073        // Moving down a menu item is the same as moving up the next in order.
    7174        check_admin_referer( 'move-menu_item' );
     75
    7276        $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
     77
    7378        if ( is_nav_menu_item( $menu_item_id ) ) {
    7479            $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
     80
    7581            if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
    7682                $menu_id            = (int) $menus[0];
     
    8187                $dbids_to_orders = array();
    8288                $orders_to_dbids = array();
     89
    8390                foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) {
    8491                    if ( isset( $ordered_menu_item_object->ID ) ) {
     
    9198
    9299                // Get next in order.
    93                 if (
    94                     isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ] )
    95                 ) {
     100                if ( isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ] ) ) {
    96101                    $next_item_id   = $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ];
    97102                    $next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) );
    98103
    99104                    // If not siblings of same parent, bubble menu item up but keep order.
    100                     if (
    101                         ! empty( $menu_item_data['menu_item_parent'] ) &&
    102                         (
    103                             empty( $next_item_data['menu_item_parent'] ) ||
    104                             $next_item_data['menu_item_parent'] != $menu_item_data['menu_item_parent']
    105                         )
     105                    if ( ! empty( $menu_item_data['menu_item_parent'] )
     106                        && ( empty( $next_item_data['menu_item_parent'] )
     107                            || (int) $next_item_data['menu_item_parent'] !== (int) $menu_item_data['menu_item_parent'] )
    106108                    ) {
    107109                        if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) {
     
    117119                            $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
    118120                            update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
    119 
    120121                        }
    121122
     
    133134
    134135                    // The item is last but still has a parent, so bubble up.
    135                 } elseif (
    136                     ! empty( $menu_item_data['menu_item_parent'] ) &&
    137                     in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true )
     136                } elseif ( ! empty( $menu_item_data['menu_item_parent'] )
     137                    && in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true )
    138138                ) {
    139139                    $menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true );
     
    144144
    145145        break;
     146
    146147    case 'move-up-menu-item':
    147148        check_admin_referer( 'move-menu_item' );
     149
    148150        $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
     151
    149152        if ( is_nav_menu_item( $menu_item_id ) ) {
    150             $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
     153            if ( isset( $_REQUEST['menu'] ) ) {
     154                $menus = array( (int) $_REQUEST['menu'] );
     155            } else {
     156                $menus = wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
     157            }
     158
    151159            if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
    152160                $menu_id            = (int) $menus[0];
     
    157165                $dbids_to_orders = array();
    158166                $orders_to_dbids = array();
     167
    159168                foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) {
    160169                    if ( isset( $ordered_menu_item_object->ID ) ) {
     
    167176
    168177                // If this menu item is not first.
    169                 if ( ! empty( $dbids_to_orders[ $menu_item_id ] ) && ! empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) ) {
     178                if ( ! empty( $dbids_to_orders[ $menu_item_id ] )
     179                    && ! empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
     180                ) {
    170181
    171182                    // If this menu item is a child of the previous.
    172                     if (
    173                         ! empty( $menu_item_data['menu_item_parent'] ) &&
    174                         in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true ) &&
    175                         isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) &&
    176                         ( $menu_item_data['menu_item_parent'] == $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
     183                    if ( ! empty( $menu_item_data['menu_item_parent'] )
     184                        && in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true )
     185                        && isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
     186                        && ( (int) $menu_item_data['menu_item_parent'] === $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
    177187                    ) {
    178188                        if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) {
     
    191201                             * make menu item a child also of it.
    192202                             */
    193                             if (
    194                                 ! empty( $dbids_to_orders[ $parent_db_id ] ) &&
    195                                 ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] ) &&
    196                                 ! empty( $parent_data['menu_item_parent'] )
     203                            if ( ! empty( $dbids_to_orders[ $parent_db_id ] )
     204                                && ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] )
     205                                && ! empty( $parent_data['menu_item_parent'] )
    197206                            ) {
    198207                                $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
     
    202211                                * make menu item a child of that something's parent
    203212                                */
    204                             } elseif (
    205                                 ! empty( $dbids_to_orders[ $parent_db_id ] ) &&
    206                                 ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] )
     213                            } elseif ( ! empty( $dbids_to_orders[ $parent_db_id ] )
     214                                && ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] )
    207215                            ) {
    208216                                $_possible_parent_id = (int) get_post_meta( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ], '_menu_item_menu_item_parent', true );
     217
    209218                                if ( in_array( $_possible_parent_id, array_keys( $dbids_to_orders ), true ) ) {
    210219                                    $menu_item_data['menu_item_parent'] = $_possible_parent_id;
     
    231240
    232241                        // Else this menu item is not a child of the previous.
    233                     } elseif (
    234                         empty( $menu_item_data['menu_order'] ) ||
    235                         empty( $menu_item_data['menu_item_parent'] ) ||
    236                         ! in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true ) ||
    237                         empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) ||
    238                         $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] != $menu_item_data['menu_item_parent']
     242                    } elseif ( empty( $menu_item_data['menu_order'] )
     243                        || empty( $menu_item_data['menu_item_parent'] )
     244                        || ! in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true )
     245                        || empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
     246                        || $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] !== (int) $menu_item_data['menu_item_parent']
    239247                    ) {
    240248                        // Just make it a child of the previous; keep the order.
     
    246254            }
    247255        }
     256
    248257        break;
    249258
     
    256265            $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'The menu item has been successfully deleted.' ) . '</p></div>';
    257266        }
     267
    258268        break;
    259269
    260270    case 'delete':
    261271        check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id );
     272
    262273        if ( is_nav_menu( $nav_menu_selected_id ) ) {
    263274            $deletion = wp_delete_nav_menu( $nav_menu_selected_id );
     
    277288            $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'The menu has been successfully deleted.' ) . '</p></div>';
    278289        }
     290
    279291        break;
    280292
    281293    case 'delete_menus':
    282294        check_admin_referer( 'nav_menus_bulk_actions' );
     295
    283296        foreach ( $_REQUEST['delete_menus'] as $menu_id_to_delete ) {
    284297            if ( ! is_nav_menu( $menu_id_to_delete ) ) {
     
    287300
    288301            $deletion = wp_delete_nav_menu( $menu_id_to_delete );
     302
    289303            if ( is_wp_error( $deletion ) ) {
    290304                $messages[]     = '<div id="message" class="error notice is-dismissible"><p>' . $deletion->get_error_message() . '</p></div>';
     
    296310            $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Selected menus have been successfully deleted.' ) . '</p></div>';
    297311        }
     312
    298313        break;
    299314
     
    308323
    309324        // Add Menu.
    310         if ( 0 == $nav_menu_selected_id ) {
     325        if ( 0 === $nav_menu_selected_id ) {
    311326            $new_menu_title = trim( esc_html( $_POST['menu-name'] ) );
    312327
     
    320335                    $nav_menu_selected_id    = $_nav_menu_selected_id;
    321336                    $nav_menu_selected_title = $_menu_object->name;
     337
    322338                    if ( isset( $_REQUEST['menu-item'] ) ) {
    323339                        wp_save_nav_menu_items( $nav_menu_selected_id, absint( $_REQUEST['menu-item'] ) );
    324340                    }
     341
    325342                    // Set the menu_location value correctly for the newly created menu.
    326343                    foreach ( $menu_locations as $location => $id ) {
     
    329346                        }
    330347                    }
     348
    331349                    set_theme_mod( 'nav_menu_locations', $menu_locations );
     350
    332351                    if ( isset( $_REQUEST['zero-menu-state'] ) || ! empty( $_POST['auto-add-pages'] ) ) {
    333352                        // If there are menu items, add them.
    334353                        wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title );
    335354                    }
     355
    336356                    if ( isset( $_REQUEST['zero-menu-state'] ) ) {
    337357                        // Auto-save nav_menu_locations.
    338358                        $locations = get_nav_menu_locations();
     359
    339360                        foreach ( $locations as $location => $menu_id ) {
    340361                                $locations[ $location ] = $nav_menu_selected_id;
    341362                                break; // There should only be 1.
    342363                        }
     364
    343365                        set_theme_mod( 'nav_menu_locations', $locations );
    344366                    }
     367
    345368                    if ( isset( $_REQUEST['use-location'] ) ) {
    346369                        $locations      = get_registered_nav_menus();
    347370                        $menu_locations = get_nav_menu_locations();
     371
    348372                        if ( isset( $locations[ $_REQUEST['use-location'] ] ) ) {
    349373                            $menu_locations[ $_REQUEST['use-location'] ] = $nav_menu_selected_id;
    350374                        }
     375
    351376                        set_theme_mod( 'nav_menu_locations', $menu_locations );
    352377                    }
     
    363388            // Remove menu locations that have been unchecked.
    364389            foreach ( $locations as $location => $description ) {
    365                 if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) ) && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $nav_menu_selected_id ) {
     390                if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) )
     391                    && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] === $nav_menu_selected_id
     392                ) {
    366393                    unset( $menu_locations[ $location ] );
    367394                }
     
    374401
    375402            $menu_title = trim( esc_html( $_POST['menu-name'] ) );
     403
    376404            if ( ! $menu_title ) {
    377405                $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
     
    381409            if ( ! is_wp_error( $_menu_object ) ) {
    382410                $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) );
     411
    383412                if ( is_wp_error( $_nav_menu_selected_id ) ) {
    384413                    $_menu_object = $_nav_menu_selected_id;
     
    395424
    396425                // If the menu ID changed, redirect to the new URL.
    397                 if ( $nav_menu_selected_id != $_nav_menu_selected_id ) {
     426                if ( $nav_menu_selected_id !== $_nav_menu_selected_id ) {
    398427                    wp_redirect( admin_url( 'nav-menus.php?menu=' . intval( $_nav_menu_selected_id ) ) );
    399428                    exit;
     
    401430            }
    402431        }
     432
    403433        break;
     434
    404435    case 'locations':
    405436        if ( ! $num_locations ) {
     
    420451            $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Menu locations updated.' ) . '</p></div>';
    421452        }
     453
    422454        break;
    423455}
     
    428460
    429461// Are we on the add new screen?
    430 $add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false;
     462$add_new_screen = ( isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'] ) ? true : false;
    431463
    432464$locations_screen = ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) ? true : false;
     
    470502 * or more than 1 theme locations.
    471503 */
    472 if ( 0 == $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus ) {
     504if ( 0 === $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus ) {
    473505    wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) );
    474506}
     
    486518
    487519// On deletion of menu, if another menu exists, show it.
    488 if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' === $_GET['action'] ) {
     520if ( ! $add_new_screen && $menu_count > 0 && isset( $_GET['action'] ) && 'delete' === $_GET['action'] ) {
    489521    $nav_menu_selected_id = $nav_menus[0]->term_id;
    490522}
     
    499531
    500532// Update the user's setting.
    501 if ( $nav_menu_selected_id != $recently_edited && is_nav_menu( $nav_menu_selected_id ) ) {
     533if ( $nav_menu_selected_id !== $recently_edited && is_nav_menu( $nav_menu_selected_id ) ) {
    502534    update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
    503535}
     
    653685    $nav_tab_active_class = '';
    654686    $nav_aria_current     = '';
    655     if ( ! isset( $_GET['action'] ) || isset( $_GET['action'] ) && 'locations' != $_GET['action'] ) {
     687
     688    if ( ! isset( $_GET['action'] ) || isset( $_GET['action'] ) && 'locations' !== $_GET['action'] ) {
    656689        $nav_tab_active_class = ' nav-tab-active';
    657690        $nav_aria_current     = ' aria-current="page"';
     
    667700            $active_tab_class = '';
    668701            $aria_current     = '';
     702
    669703            if ( $locations_screen ) {
    670704                $active_tab_class = ' nav-tab-active';
     
    684718    <?php
    685719    if ( $locations_screen ) :
    686         if ( 1 == $num_locations ) {
     720        if ( 1 === $num_locations ) {
    687721            echo '<p>' . __( 'Your theme supports one menu. Select which menu you would like to use.' ) . '</p>';
    688722        } else {
     
    717751                                foreach ( $nav_menus as $menu ) :
    718752                                    $data_orig = '';
    719                                     $selected  = isset( $menu_locations[ $_location ] ) && $menu_locations[ $_location ] == $menu->term_id;
     753                                    $selected  = isset( $menu_locations[ $_location ] ) && $menu_locations[ $_location ] === $menu->term_id;
     754
    720755                                    if ( $selected ) {
    721756                                        $data_orig = 'data-orig="true"';
     
    728763                            </select>
    729764                            <div class="locations-row-links">
    730                                 <?php if ( isset( $menu_locations[ $_location ] ) && 0 != $menu_locations[ $_location ] ) : ?>
     765                                <?php if ( isset( $menu_locations[ $_location ] ) && 0 !== $menu_locations[ $_location ] ) : ?>
    731766                                <span class="locations-edit-menu-link">
    732767                                    <a href="
     
    880915
    881916        $metabox_holder_disabled_class = '';
    882         if ( isset( $_GET['menu'] ) && '0' == $_GET['menu'] ) {
     917
     918        if ( isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'] ) {
    883919            $metabox_holder_disabled_class = ' metabox-holder-disabled';
    884920        }
     
    938974                                <?php
    939975                                $hide_style = '';
     976
    940977                                if ( isset( $menu_items ) && 0 === count( $menu_items ) ) {
    941978                                    $hide_style = 'style="display: none;"';
     
    9721009
    9731010                            $no_menus_style = '';
     1011
    9741012                            if ( $one_theme_location_no_menus ) {
    9751013                                $no_menus_style = 'style="display: none;"';
     
    10031041                                    <fieldset class="menu-settings-group menu-theme-locations">
    10041042                                        <legend class="menu-settings-group-name howto"><?php _e( 'Display location' ); ?></legend>
    1005                                         <?php foreach ( $locations as $location => $description ) : ?>
    1006                                         <div class="menu-settings-input checkbox-input">
    1007                                             <input type="checkbox"<?php checked( isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $nav_menu_selected_id ); ?> name="menu-locations[<?php echo esc_attr( $location ); ?>]" id="locations-<?php echo esc_attr( $location ); ?>" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
    1008                                             <label for="locations-<?php echo esc_attr( $location ); ?>"><?php echo $description; ?></label>
    1009                                             <?php if ( ! empty( $menu_locations[ $location ] ) && $menu_locations[ $location ] != $nav_menu_selected_id ) : ?>
    1010                                                 <span class="theme-location-set">
    1011                                                 <?php
    1012                                                     printf(
    1013                                                         /* translators: %s: Menu name. */
    1014                                                         _x( '(Currently set to: %s)', 'menu location' ),
    1015                                                         wp_get_nav_menu_object( $menu_locations[ $location ] )->name
    1016                                                     );
    1017                                                 ?>
    1018                                                 </span>
    1019                                             <?php endif; ?>
    1020                                         </div>
     1043                                        <?php
     1044                                        foreach ( $locations as $location => $description ) :
     1045                                            $checked = isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] === $nav_menu_selected_id;
     1046                                            ?>
     1047                                            <div class="menu-settings-input checkbox-input">
     1048                                                <input type="checkbox"<?php checked( $checked ); ?> name="menu-locations[<?php echo esc_attr( $location ); ?>]" id="locations-<?php echo esc_attr( $location ); ?>" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
     1049                                                <label for="locations-<?php echo esc_attr( $location ); ?>"><?php echo $description; ?></label>
     1050                                                <?php if ( ! empty( $menu_locations[ $location ] ) && $menu_locations[ $location ] !== $nav_menu_selected_id ) : ?>
     1051                                                    <span class="theme-location-set">
     1052                                                    <?php
     1053                                                        printf(
     1054                                                            /* translators: %s: Menu name. */
     1055                                                            _x( '(Currently set to: %s)', 'menu location' ),
     1056                                                            wp_get_nav_menu_object( $menu_locations[ $location ] )->name
     1057                                                        );
     1058                                                    ?>
     1059                                                    </span>
     1060                                                <?php endif; ?>
     1061                                            </div>
    10211062                                        <?php endforeach; ?>
    10221063                                    </fieldset>
Note: See TracChangeset for help on using the changeset viewer.