From 49edaea6f15e74b8355b161c9683b8631009c2d1 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 16 Jun 2020 18:38:08 +0000 Subject: [PATCH] Coding Standards: Fix WPCS issues in `wp-admin/nav-menus.php`. Includes minor code layout fixes for better readability. See #49542. Built from https://develop.svn.wordpress.org/trunk@48063 git-svn-id: http://core.svn.wordpress.org/trunk@47830 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/nav-menus.php | 163 +++++++++++++++++++++++++--------------- wp-includes/version.php | 2 +- 2 files changed, 103 insertions(+), 62 deletions(-) diff --git a/wp-admin/nav-menus.php b/wp-admin/nav-menus.php index 24fc03a31b..a4ef4d5c36 100644 --- a/wp-admin/nav-menus.php +++ b/wp-admin/nav-menus.php @@ -60,18 +60,24 @@ _wp_expand_nav_menu_post_data(); switch ( $action ) { case 'add-menu-item': check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' ); + if ( isset( $_REQUEST['nav-menu-locations'] ) ) { set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) ); } elseif ( isset( $_REQUEST['menu-item'] ) ) { wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] ); } + break; + case 'move-down-menu-item': // Moving down a menu item is the same as moving up the next in order. check_admin_referer( 'move-menu_item' ); + $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0; + if ( is_nav_menu_item( $menu_item_id ) ) { $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) ); + if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) { $menu_id = (int) $menus[0]; $ordered_menu_items = wp_get_nav_menu_items( $menu_id ); @@ -80,6 +86,7 @@ switch ( $action ) { // Set up the data we need in one pass through the array of menu items. $dbids_to_orders = array(); $orders_to_dbids = array(); + foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) { if ( isset( $ordered_menu_item_object->ID ) ) { if ( isset( $ordered_menu_item_object->menu_order ) ) { @@ -90,19 +97,14 @@ switch ( $action ) { } // Get next in order. - if ( - isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ] ) - ) { + if ( isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ] ) ) { $next_item_id = $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ]; $next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) ); // If not siblings of same parent, bubble menu item up but keep order. - if ( - ! empty( $menu_item_data['menu_item_parent'] ) && - ( - empty( $next_item_data['menu_item_parent'] ) || - $next_item_data['menu_item_parent'] != $menu_item_data['menu_item_parent'] - ) + if ( ! empty( $menu_item_data['menu_item_parent'] ) + && ( empty( $next_item_data['menu_item_parent'] ) + || (int) $next_item_data['menu_item_parent'] !== (int) $menu_item_data['menu_item_parent'] ) ) { if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) { $parent_db_id = (int) $menu_item_data['menu_item_parent']; @@ -116,7 +118,6 @@ switch ( $action ) { $parent_data = (array) $parent_object; $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent']; update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] ); - } // Make menu item a child of its next sibling. @@ -132,9 +133,8 @@ switch ( $action ) { } // The item is last but still has a parent, so bubble up. - } elseif ( - ! empty( $menu_item_data['menu_item_parent'] ) && - in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) + } elseif ( ! empty( $menu_item_data['menu_item_parent'] ) + && in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) { $menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true ); update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] ); @@ -143,11 +143,19 @@ switch ( $action ) { } break; + case 'move-up-menu-item': check_admin_referer( 'move-menu_item' ); + $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0; + if ( is_nav_menu_item( $menu_item_id ) ) { - $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) ); + if ( isset( $_REQUEST['menu'] ) ) { + $menus = array( (int) $_REQUEST['menu'] ); + } else { + $menus = wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) ); + } + if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) { $menu_id = (int) $menus[0]; $ordered_menu_items = wp_get_nav_menu_items( $menu_id ); @@ -156,6 +164,7 @@ switch ( $action ) { // Set up the data we need in one pass through the array of menu items. $dbids_to_orders = array(); $orders_to_dbids = array(); + foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) { if ( isset( $ordered_menu_item_object->ID ) ) { if ( isset( $ordered_menu_item_object->menu_order ) ) { @@ -166,14 +175,15 @@ switch ( $action ) { } // If this menu item is not first. - if ( ! empty( $dbids_to_orders[ $menu_item_id ] ) && ! empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) ) { + if ( ! empty( $dbids_to_orders[ $menu_item_id ] ) + && ! empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) + ) { // If this menu item is a child of the previous. - if ( - ! empty( $menu_item_data['menu_item_parent'] ) && - in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true ) && - isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) && - ( $menu_item_data['menu_item_parent'] == $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) + if ( ! empty( $menu_item_data['menu_item_parent'] ) + && in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true ) + && isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) + && ( (int) $menu_item_data['menu_item_parent'] === $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) ) { if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) { $parent_db_id = (int) $menu_item_data['menu_item_parent']; @@ -190,10 +200,9 @@ switch ( $action ) { * If there is something before the parent and parent a child of it, * make menu item a child also of it. */ - if ( - ! empty( $dbids_to_orders[ $parent_db_id ] ) && - ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] ) && - ! empty( $parent_data['menu_item_parent'] ) + if ( ! empty( $dbids_to_orders[ $parent_db_id ] ) + && ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] ) + && ! empty( $parent_data['menu_item_parent'] ) ) { $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent']; @@ -201,11 +210,11 @@ switch ( $action ) { * Else if there is something before parent and parent not a child of it, * make menu item a child of that something's parent */ - } elseif ( - ! empty( $dbids_to_orders[ $parent_db_id ] ) && - ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] ) + } elseif ( ! empty( $dbids_to_orders[ $parent_db_id ] ) + && ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] ) ) { $_possible_parent_id = (int) get_post_meta( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ], '_menu_item_menu_item_parent', true ); + if ( in_array( $_possible_parent_id, array_keys( $dbids_to_orders ), true ) ) { $menu_item_data['menu_item_parent'] = $_possible_parent_id; } else { @@ -230,12 +239,11 @@ switch ( $action ) { } // Else this menu item is not a child of the previous. - } elseif ( - empty( $menu_item_data['menu_order'] ) || - empty( $menu_item_data['menu_item_parent'] ) || - ! in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true ) || - empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) || - $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] != $menu_item_data['menu_item_parent'] + } elseif ( empty( $menu_item_data['menu_order'] ) + || empty( $menu_item_data['menu_item_parent'] ) + || ! in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true ) + || empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] ) + || $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] !== (int) $menu_item_data['menu_item_parent'] ) { // Just make it a child of the previous; keep the order. $menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ]; @@ -245,6 +253,7 @@ switch ( $action ) { } } } + break; case 'delete-menu-item': @@ -255,10 +264,12 @@ switch ( $action ) { if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) ) { $messages[] = '

' . __( 'The menu item has been successfully deleted.' ) . '

'; } + break; case 'delete': check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id ); + if ( is_nav_menu( $nav_menu_selected_id ) ) { $deletion = wp_delete_nav_menu( $nav_menu_selected_id ); } else { @@ -276,16 +287,19 @@ switch ( $action ) { } else { $messages[] = '

' . __( 'The menu has been successfully deleted.' ) . '

'; } + break; case 'delete_menus': check_admin_referer( 'nav_menus_bulk_actions' ); + foreach ( $_REQUEST['delete_menus'] as $menu_id_to_delete ) { if ( ! is_nav_menu( $menu_id_to_delete ) ) { continue; } $deletion = wp_delete_nav_menu( $menu_id_to_delete ); + if ( is_wp_error( $deletion ) ) { $messages[] = '

' . $deletion->get_error_message() . '

'; $deletion_error = true; @@ -295,6 +309,7 @@ switch ( $action ) { if ( empty( $deletion_error ) ) { $messages[] = '

' . __( 'Selected menus have been successfully deleted.' ) . '

'; } + break; case 'update': @@ -307,7 +322,7 @@ switch ( $action ) { } // Add Menu. - if ( 0 == $nav_menu_selected_id ) { + if ( 0 === $nav_menu_selected_id ) { $new_menu_title = trim( esc_html( $_POST['menu-name'] ) ); if ( $new_menu_title ) { @@ -319,35 +334,45 @@ switch ( $action ) { $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id ); $nav_menu_selected_id = $_nav_menu_selected_id; $nav_menu_selected_title = $_menu_object->name; + if ( isset( $_REQUEST['menu-item'] ) ) { wp_save_nav_menu_items( $nav_menu_selected_id, absint( $_REQUEST['menu-item'] ) ); } + // Set the menu_location value correctly for the newly created menu. foreach ( $menu_locations as $location => $id ) { if ( 0 === $id ) { $menu_locations[ $location ] = $nav_menu_selected_id; } } + set_theme_mod( 'nav_menu_locations', $menu_locations ); + if ( isset( $_REQUEST['zero-menu-state'] ) || ! empty( $_POST['auto-add-pages'] ) ) { // If there are menu items, add them. wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ); } + if ( isset( $_REQUEST['zero-menu-state'] ) ) { // Auto-save nav_menu_locations. $locations = get_nav_menu_locations(); + foreach ( $locations as $location => $menu_id ) { $locations[ $location ] = $nav_menu_selected_id; break; // There should only be 1. } + set_theme_mod( 'nav_menu_locations', $locations ); } + if ( isset( $_REQUEST['use-location'] ) ) { $locations = get_registered_nav_menus(); $menu_locations = get_nav_menu_locations(); + if ( isset( $locations[ $_REQUEST['use-location'] ] ) ) { $menu_locations[ $_REQUEST['use-location'] ] = $nav_menu_selected_id; } + set_theme_mod( 'nav_menu_locations', $menu_locations ); } @@ -362,7 +387,9 @@ switch ( $action ) { } else { // Remove menu locations that have been unchecked. foreach ( $locations as $location => $description ) { - if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) ) && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $nav_menu_selected_id ) { + if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) ) + && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] === $nav_menu_selected_id + ) { unset( $menu_locations[ $location ] ); } } @@ -373,6 +400,7 @@ switch ( $action ) { $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id ); $menu_title = trim( esc_html( $_POST['menu-name'] ) ); + if ( ! $menu_title ) { $messages[] = '

' . __( 'Please enter a valid menu name.' ) . '

'; $menu_title = $_menu_object->name; @@ -380,6 +408,7 @@ switch ( $action ) { if ( ! is_wp_error( $_menu_object ) ) { $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) ); + if ( is_wp_error( $_nav_menu_selected_id ) ) { $_menu_object = $_nav_menu_selected_id; $messages[] = '

' . $_nav_menu_selected_id->get_error_message() . '

'; @@ -394,13 +423,15 @@ switch ( $action ) { $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $_nav_menu_selected_id, $nav_menu_selected_title ) ); // If the menu ID changed, redirect to the new URL. - if ( $nav_menu_selected_id != $_nav_menu_selected_id ) { + if ( $nav_menu_selected_id !== $_nav_menu_selected_id ) { wp_redirect( admin_url( 'nav-menus.php?menu=' . intval( $_nav_menu_selected_id ) ) ); exit; } } } + break; + case 'locations': if ( ! $num_locations ) { wp_redirect( admin_url( 'nav-menus.php' ) ); @@ -419,6 +450,7 @@ switch ( $action ) { $messages[] = '

' . __( 'Menu locations updated.' ) . '

'; } + break; } @@ -427,7 +459,7 @@ $nav_menus = wp_get_nav_menus(); $menu_count = count( $nav_menus ); // Are we on the add new screen? -$add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false; +$add_new_screen = ( isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'] ) ? true : false; $locations_screen = ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) ? true : false; @@ -469,7 +501,7 @@ wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n ); * Redirect to add screen if there are no menus and this users has either zero, * or more than 1 theme locations. */ -if ( 0 == $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus ) { +if ( 0 === $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus ) { wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) ); } @@ -485,7 +517,7 @@ if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( } // On deletion of menu, if another menu exists, show it. -if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' === $_GET['action'] ) { +if ( ! $add_new_screen && $menu_count > 0 && isset( $_GET['action'] ) && 'delete' === $_GET['action'] ) { $nav_menu_selected_id = $nav_menus[0]->term_id; } @@ -498,7 +530,7 @@ if ( $one_theme_location_no_menus ) { } // Update the user's setting. -if ( $nav_menu_selected_id != $recently_edited && is_nav_menu( $nav_menu_selected_id ) ) { +if ( $nav_menu_selected_id !== $recently_edited && is_nav_menu( $nav_menu_selected_id ) ) { update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id ); } @@ -652,7 +684,8 @@ require_once ABSPATH . 'wp-admin/admin-header.php'; $nav_tab_active_class = ''; $nav_aria_current = ''; - if ( ! isset( $_GET['action'] ) || isset( $_GET['action'] ) && 'locations' != $_GET['action'] ) { + + if ( ! isset( $_GET['action'] ) || isset( $_GET['action'] ) && 'locations' !== $_GET['action'] ) { $nav_tab_active_class = ' nav-tab-active'; $nav_aria_current = ' aria-current="page"'; } @@ -666,6 +699,7 @@ require_once ABSPATH . 'wp-admin/admin-header.php'; if ( $num_locations && $menu_count ) { $active_tab_class = ''; $aria_current = ''; + if ( $locations_screen ) { $active_tab_class = ' nav-tab-active'; $aria_current = ' aria-current="page"'; @@ -683,7 +717,7 @@ require_once ABSPATH . 'wp-admin/admin-header.php'; ?> ' . __( 'Your theme supports one menu. Select which menu you would like to use.' ) . '

'; } else { echo '

' . sprintf( @@ -716,7 +750,8 @@ require_once ABSPATH . 'wp-admin/admin-header.php'; term_id; + $selected = isset( $menu_locations[ $_location ] ) && $menu_locations[ $_location ] === $menu->term_id; + if ( $selected ) { $data_orig = 'data-orig="true"'; } @@ -727,7 +762,7 @@ require_once ABSPATH . 'wp-admin/admin-header.php';