Changeset 48063 for trunk/src/wp-admin/nav-menus.php
- Timestamp:
- 06/16/2020 06:36:42 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/nav-menus.php
r48052 r48063 61 61 case 'add-menu-item': 62 62 check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' ); 63 63 64 if ( isset( $_REQUEST['nav-menu-locations'] ) ) { 64 65 set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) ); … … 66 67 wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] ); 67 68 } 69 68 70 break; 71 69 72 case 'move-down-menu-item': 70 73 // Moving down a menu item is the same as moving up the next in order. 71 74 check_admin_referer( 'move-menu_item' ); 75 72 76 $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0; 77 73 78 if ( is_nav_menu_item( $menu_item_id ) ) { 74 79 $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) ); 80 75 81 if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) { 76 82 $menu_id = (int) $menus[0]; … … 81 87 $dbids_to_orders = array(); 82 88 $orders_to_dbids = array(); 89 83 90 foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) { 84 91 if ( isset( $ordered_menu_item_object->ID ) ) { … … 91 98 92 99 // 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 ] ) ) { 96 101 $next_item_id = $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ]; 97 102 $next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) ); 98 103 99 104 // 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'] ) 106 108 ) { 107 109 if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) { … … 117 119 $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent']; 118 120 update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] ); 119 120 121 } 121 122 … … 133 134 134 135 // 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 ) 138 138 ) { 139 139 $menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true ); … … 144 144 145 145 break; 146 146 147 case 'move-up-menu-item': 147 148 check_admin_referer( 'move-menu_item' ); 149 148 150 $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0; 151 149 152 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 151 159 if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) { 152 160 $menu_id = (int) $menus[0]; … … 157 165 $dbids_to_orders = array(); 158 166 $orders_to_dbids = array(); 167 159 168 foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) { 160 169 if ( isset( $ordered_menu_item_object->ID ) ) { … … 167 176 168 177 // 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 ) { 170 181 171 182 // 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 ] ) 177 187 ) { 178 188 if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) { … … 191 201 * make menu item a child also of it. 192 202 */ 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'] ) 197 206 ) { 198 207 $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent']; … … 202 211 * make menu item a child of that something's parent 203 212 */ 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 ] ) 207 215 ) { 208 216 $_possible_parent_id = (int) get_post_meta( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ], '_menu_item_menu_item_parent', true ); 217 209 218 if ( in_array( $_possible_parent_id, array_keys( $dbids_to_orders ), true ) ) { 210 219 $menu_item_data['menu_item_parent'] = $_possible_parent_id; … … 231 240 232 241 // 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'] 239 247 ) { 240 248 // Just make it a child of the previous; keep the order. … … 246 254 } 247 255 } 256 248 257 break; 249 258 … … 256 265 $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'The menu item has been successfully deleted.' ) . '</p></div>'; 257 266 } 267 258 268 break; 259 269 260 270 case 'delete': 261 271 check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id ); 272 262 273 if ( is_nav_menu( $nav_menu_selected_id ) ) { 263 274 $deletion = wp_delete_nav_menu( $nav_menu_selected_id ); … … 277 288 $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'The menu has been successfully deleted.' ) . '</p></div>'; 278 289 } 290 279 291 break; 280 292 281 293 case 'delete_menus': 282 294 check_admin_referer( 'nav_menus_bulk_actions' ); 295 283 296 foreach ( $_REQUEST['delete_menus'] as $menu_id_to_delete ) { 284 297 if ( ! is_nav_menu( $menu_id_to_delete ) ) { … … 287 300 288 301 $deletion = wp_delete_nav_menu( $menu_id_to_delete ); 302 289 303 if ( is_wp_error( $deletion ) ) { 290 304 $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $deletion->get_error_message() . '</p></div>'; … … 296 310 $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Selected menus have been successfully deleted.' ) . '</p></div>'; 297 311 } 312 298 313 break; 299 314 … … 308 323 309 324 // Add Menu. 310 if ( 0 == $nav_menu_selected_id ) {325 if ( 0 === $nav_menu_selected_id ) { 311 326 $new_menu_title = trim( esc_html( $_POST['menu-name'] ) ); 312 327 … … 320 335 $nav_menu_selected_id = $_nav_menu_selected_id; 321 336 $nav_menu_selected_title = $_menu_object->name; 337 322 338 if ( isset( $_REQUEST['menu-item'] ) ) { 323 339 wp_save_nav_menu_items( $nav_menu_selected_id, absint( $_REQUEST['menu-item'] ) ); 324 340 } 341 325 342 // Set the menu_location value correctly for the newly created menu. 326 343 foreach ( $menu_locations as $location => $id ) { … … 329 346 } 330 347 } 348 331 349 set_theme_mod( 'nav_menu_locations', $menu_locations ); 350 332 351 if ( isset( $_REQUEST['zero-menu-state'] ) || ! empty( $_POST['auto-add-pages'] ) ) { 333 352 // If there are menu items, add them. 334 353 wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ); 335 354 } 355 336 356 if ( isset( $_REQUEST['zero-menu-state'] ) ) { 337 357 // Auto-save nav_menu_locations. 338 358 $locations = get_nav_menu_locations(); 359 339 360 foreach ( $locations as $location => $menu_id ) { 340 361 $locations[ $location ] = $nav_menu_selected_id; 341 362 break; // There should only be 1. 342 363 } 364 343 365 set_theme_mod( 'nav_menu_locations', $locations ); 344 366 } 367 345 368 if ( isset( $_REQUEST['use-location'] ) ) { 346 369 $locations = get_registered_nav_menus(); 347 370 $menu_locations = get_nav_menu_locations(); 371 348 372 if ( isset( $locations[ $_REQUEST['use-location'] ] ) ) { 349 373 $menu_locations[ $_REQUEST['use-location'] ] = $nav_menu_selected_id; 350 374 } 375 351 376 set_theme_mod( 'nav_menu_locations', $menu_locations ); 352 377 } … … 363 388 // Remove menu locations that have been unchecked. 364 389 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 ) { 366 393 unset( $menu_locations[ $location ] ); 367 394 } … … 374 401 375 402 $menu_title = trim( esc_html( $_POST['menu-name'] ) ); 403 376 404 if ( ! $menu_title ) { 377 405 $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>'; … … 381 409 if ( ! is_wp_error( $_menu_object ) ) { 382 410 $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) ); 411 383 412 if ( is_wp_error( $_nav_menu_selected_id ) ) { 384 413 $_menu_object = $_nav_menu_selected_id; … … 395 424 396 425 // 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 ) { 398 427 wp_redirect( admin_url( 'nav-menus.php?menu=' . intval( $_nav_menu_selected_id ) ) ); 399 428 exit; … … 401 430 } 402 431 } 432 403 433 break; 434 404 435 case 'locations': 405 436 if ( ! $num_locations ) { … … 420 451 $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Menu locations updated.' ) . '</p></div>'; 421 452 } 453 422 454 break; 423 455 } … … 428 460 429 461 // 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; 431 463 432 464 $locations_screen = ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) ? true : false; … … 470 502 * or more than 1 theme locations. 471 503 */ 472 if ( 0 == $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus ) {504 if ( 0 === $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus ) { 473 505 wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) ); 474 506 } … … 486 518 487 519 // On deletion of menu, if another menu exists, show it. 488 if ( ! $add_new_screen && 0 < $menu_count&& isset( $_GET['action'] ) && 'delete' === $_GET['action'] ) {520 if ( ! $add_new_screen && $menu_count > 0 && isset( $_GET['action'] ) && 'delete' === $_GET['action'] ) { 489 521 $nav_menu_selected_id = $nav_menus[0]->term_id; 490 522 } … … 499 531 500 532 // Update the user's setting. 501 if ( $nav_menu_selected_id != $recently_edited && is_nav_menu( $nav_menu_selected_id ) ) {533 if ( $nav_menu_selected_id !== $recently_edited && is_nav_menu( $nav_menu_selected_id ) ) { 502 534 update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id ); 503 535 } … … 653 685 $nav_tab_active_class = ''; 654 686 $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'] ) { 656 689 $nav_tab_active_class = ' nav-tab-active'; 657 690 $nav_aria_current = ' aria-current="page"'; … … 667 700 $active_tab_class = ''; 668 701 $aria_current = ''; 702 669 703 if ( $locations_screen ) { 670 704 $active_tab_class = ' nav-tab-active'; … … 684 718 <?php 685 719 if ( $locations_screen ) : 686 if ( 1 == $num_locations ) {720 if ( 1 === $num_locations ) { 687 721 echo '<p>' . __( 'Your theme supports one menu. Select which menu you would like to use.' ) . '</p>'; 688 722 } else { … … 717 751 foreach ( $nav_menus as $menu ) : 718 752 $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 720 755 if ( $selected ) { 721 756 $data_orig = 'data-orig="true"'; … … 728 763 </select> 729 764 <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 ] ) : ?> 731 766 <span class="locations-edit-menu-link"> 732 767 <a href=" … … 880 915 881 916 $metabox_holder_disabled_class = ''; 882 if ( isset( $_GET['menu'] ) && '0' == $_GET['menu'] ) { 917 918 if ( isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'] ) { 883 919 $metabox_holder_disabled_class = ' metabox-holder-disabled'; 884 920 } … … 938 974 <?php 939 975 $hide_style = ''; 976 940 977 if ( isset( $menu_items ) && 0 === count( $menu_items ) ) { 941 978 $hide_style = 'style="display: none;"'; … … 972 1009 973 1010 $no_menus_style = ''; 1011 974 1012 if ( $one_theme_location_no_menus ) { 975 1013 $no_menus_style = 'style="display: none;"'; … … 1003 1041 <fieldset class="menu-settings-group menu-theme-locations"> 1004 1042 <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> 1021 1062 <?php endforeach; ?> 1022 1063 </fieldset>
Note: See TracChangeset
for help on using the changeset viewer.