[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/ -> nav-menus.php (source)

   1  <?php
   2  /**
   3   * WordPress Administration for Navigation Menus
   4   * Interface functions
   5   *
   6   * @version 2.0.0
   7   *
   8   * @package WordPress
   9   * @subpackage Administration
  10   */
  11  
  12  /** Load WordPress Administration Bootstrap */
  13  require_once  __DIR__ . '/admin.php';
  14  
  15  // Load all the nav menu interface functions.
  16  require_once  ABSPATH . 'wp-admin/includes/nav-menu.php';
  17  
  18  if ( ! current_theme_supports( 'menus' ) && ! current_theme_supports( 'widgets' ) ) {
  19      wp_die( __( 'Your theme does not support navigation menus or widgets.' ) );
  20  }
  21  
  22  // Permissions check.
  23  if ( ! current_user_can( 'edit_theme_options' ) ) {
  24      wp_die(
  25          '<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
  26          '<p>' . __( 'Sorry, you are not allowed to edit theme options on this site.' ) . '</p>',
  27          403
  28      );
  29  }
  30  
  31  // Used in the HTML title tag.
  32  $title = __( 'Menus' );
  33  
  34  wp_enqueue_script( 'nav-menu' );
  35  
  36  if ( wp_is_mobile() ) {
  37      wp_enqueue_script( 'jquery-touch-punch' );
  38  }
  39  
  40  // Container for any messages displayed to the user.
  41  $messages = array();
  42  
  43  // Container that stores the name of the active menu.
  44  $nav_menu_selected_title = '';
  45  
  46  // The menu id of the current menu being edited.
  47  $nav_menu_selected_id = isset( $_REQUEST['menu'] ) ? (int) $_REQUEST['menu'] : 0;
  48  
  49  // Get existing menu locations assignments.
  50  $locations      = get_registered_nav_menus();
  51  $menu_locations = get_nav_menu_locations();
  52  $num_locations  = count( array_keys( $locations ) );
  53  
  54  // Allowed actions: add, update, delete.
  55  $action = $_REQUEST['action'] ?? 'edit';
  56  
  57  /*
  58   * If a JSON blob of navigation menu data is found, expand it and inject it
  59   * into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134.
  60   */
  61  _wp_expand_nav_menu_post_data();
  62  
  63  switch ( $action ) {
  64      case 'add-menu-item':
  65          check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  66  
  67          if ( isset( $_REQUEST['nav-menu-locations'] ) ) {
  68              set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) );
  69          } elseif ( isset( $_REQUEST['menu-item'] ) ) {
  70              wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] );
  71          }
  72  
  73          break;
  74  
  75      case 'move-down-menu-item':
  76          // Moving down a menu item is the same as moving up the next in order.
  77          check_admin_referer( 'move-menu_item' );
  78  
  79          $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
  80  
  81          if ( is_nav_menu_item( $menu_item_id ) ) {
  82              $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
  83  
  84              if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
  85                  $menu_id            = (int) $menus[0];
  86                  $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
  87                  $menu_item_data     = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
  88  
  89                  // Set up the data we need in one pass through the array of menu items.
  90                  $dbids_to_orders = array();
  91                  $orders_to_dbids = array();
  92  
  93                  foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) {
  94                      if ( isset( $ordered_menu_item_object->ID ) ) {
  95                          if ( isset( $ordered_menu_item_object->menu_order ) ) {
  96                              $dbids_to_orders[ $ordered_menu_item_object->ID ]         = $ordered_menu_item_object->menu_order;
  97                              $orders_to_dbids[ $ordered_menu_item_object->menu_order ] = $ordered_menu_item_object->ID;
  98                          }
  99                      }
 100                  }
 101  
 102                  // Get next in order.
 103                  if ( isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ] ) ) {
 104                      $next_item_id   = $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ];
 105                      $next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) );
 106  
 107                      // If not siblings of same parent, bubble menu item up but keep order.
 108                      if ( ! empty( $menu_item_data['menu_item_parent'] )
 109                          && ( empty( $next_item_data['menu_item_parent'] )
 110                              || (int) $next_item_data['menu_item_parent'] !== (int) $menu_item_data['menu_item_parent'] )
 111                      ) {
 112                          if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) {
 113                              $parent_db_id = (int) $menu_item_data['menu_item_parent'];
 114                          } else {
 115                              $parent_db_id = 0;
 116                          }
 117  
 118                          $parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
 119  
 120                          if ( ! is_wp_error( $parent_object ) ) {
 121                              $parent_data                        = (array) $parent_object;
 122                              $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
 123  
 124                              // Reset invalid `menu_item_parent`.
 125                              $menu_item_data = _wp_reset_invalid_menu_item_parent( $menu_item_data );
 126  
 127                              update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
 128                          }
 129  
 130                          // Make menu item a child of its next sibling.
 131                      } else {
 132                          $next_item_data['menu_order'] = $next_item_data['menu_order'] - 1;
 133                          $menu_item_data['menu_order'] = $menu_item_data['menu_order'] + 1;
 134  
 135                          $menu_item_data['menu_item_parent'] = $next_item_data['ID'];
 136  
 137                          // Reset invalid `menu_item_parent`.
 138                          $menu_item_data = _wp_reset_invalid_menu_item_parent( $menu_item_data );
 139  
 140                          update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
 141  
 142                          wp_update_post( $menu_item_data );
 143                          wp_update_post( $next_item_data );
 144                      }
 145  
 146                      // The item is last but still has a parent, so bubble up.
 147                  } elseif ( ! empty( $menu_item_data['menu_item_parent'] )
 148                      && in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true )
 149                  ) {
 150                      $menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true );
 151  
 152                      // Reset invalid `menu_item_parent`.
 153                      $menu_item_data = _wp_reset_invalid_menu_item_parent( $menu_item_data );
 154  
 155                      update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
 156                  }
 157              }
 158          }
 159  
 160          break;
 161  
 162      case 'move-up-menu-item':
 163          check_admin_referer( 'move-menu_item' );
 164  
 165          $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
 166  
 167          if ( is_nav_menu_item( $menu_item_id ) ) {
 168              if ( isset( $_REQUEST['menu'] ) ) {
 169                  $menus = array( (int) $_REQUEST['menu'] );
 170              } else {
 171                  $menus = wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
 172              }
 173  
 174              if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
 175                  $menu_id            = (int) $menus[0];
 176                  $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
 177                  $menu_item_data     = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
 178  
 179                  // Set up the data we need in one pass through the array of menu items.
 180                  $dbids_to_orders = array();
 181                  $orders_to_dbids = array();
 182  
 183                  foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) {
 184                      if ( isset( $ordered_menu_item_object->ID ) ) {
 185                          if ( isset( $ordered_menu_item_object->menu_order ) ) {
 186                              $dbids_to_orders[ $ordered_menu_item_object->ID ]         = $ordered_menu_item_object->menu_order;
 187                              $orders_to_dbids[ $ordered_menu_item_object->menu_order ] = $ordered_menu_item_object->ID;
 188                          }
 189                      }
 190                  }
 191  
 192                  // If this menu item is not first.
 193                  if ( ! empty( $dbids_to_orders[ $menu_item_id ] )
 194                      && ! empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
 195                  ) {
 196  
 197                      // If this menu item is a child of the previous.
 198                      if ( ! empty( $menu_item_data['menu_item_parent'] )
 199                          && isset( $dbids_to_orders[ (int) $menu_item_data['menu_item_parent'] ] )
 200                          && ( (int) $menu_item_data['menu_item_parent'] === $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
 201                      ) {
 202                          if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) {
 203                              $parent_db_id = (int) $menu_item_data['menu_item_parent'];
 204                          } else {
 205                              $parent_db_id = 0;
 206                          }
 207  
 208                          $parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
 209  
 210                          if ( ! is_wp_error( $parent_object ) ) {
 211                              $parent_data = (array) $parent_object;
 212  
 213                              /*
 214                               * If there is something before the parent and parent a child of it,
 215                               * make menu item a child also of it.
 216                               */
 217                              if ( ! empty( $dbids_to_orders[ $parent_db_id ] )
 218                                  && ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] )
 219                                  && ! empty( $parent_data['menu_item_parent'] )
 220                              ) {
 221                                  $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
 222  
 223                                  /*
 224                                  * Else if there is something before parent and parent not a child of it,
 225                                  * make menu item a child of that something's parent
 226                                  */
 227                              } elseif ( ! empty( $dbids_to_orders[ $parent_db_id ] )
 228                                  && ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] )
 229                              ) {
 230                                  $_possible_parent_id = (int) get_post_meta( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ], '_menu_item_menu_item_parent', true );
 231  
 232                                  if ( isset( $dbids_to_orders[ $_possible_parent_id ] ) ) {
 233                                      $menu_item_data['menu_item_parent'] = $_possible_parent_id;
 234                                  } else {
 235                                      $menu_item_data['menu_item_parent'] = 0;
 236                                  }
 237  
 238                                  // Else there isn't something before the parent.
 239                              } else {
 240                                  $menu_item_data['menu_item_parent'] = 0;
 241                              }
 242  
 243                              // Set former parent's [menu_order] to that of menu-item's.
 244                              $parent_data['menu_order'] = $parent_data['menu_order'] + 1;
 245  
 246                              // Set menu-item's [menu_order] to that of former parent.
 247                              $menu_item_data['menu_order'] = $menu_item_data['menu_order'] - 1;
 248  
 249                              // Save changes.
 250                              update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
 251                              wp_update_post( $menu_item_data );
 252                              wp_update_post( $parent_data );
 253                          }
 254  
 255                          // Else this menu item is not a child of the previous.
 256                      } elseif ( empty( $menu_item_data['menu_order'] )
 257                          || empty( $menu_item_data['menu_item_parent'] )
 258                          || ! isset( $dbids_to_orders[ (int) $menu_item_data['menu_item_parent'] ] )
 259                          || empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
 260                          || $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] !== (int) $menu_item_data['menu_item_parent']
 261                      ) {
 262                          // Just make it a child of the previous; keep the order.
 263                          $menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ];
 264  
 265                          // Reset invalid `menu_item_parent`.
 266                          $menu_item_data = _wp_reset_invalid_menu_item_parent( $menu_item_data );
 267  
 268                          update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
 269                          wp_update_post( $menu_item_data );
 270                      }
 271                  }
 272              }
 273          }
 274  
 275          break;
 276  
 277      case 'delete-menu-item':
 278          $menu_item_id = (int) $_REQUEST['menu-item'];
 279  
 280          check_admin_referer( 'delete-menu_item_' . $menu_item_id );
 281  
 282          if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) ) {
 283              $messages[] = wp_get_admin_notice(
 284                  __( 'The menu item has been successfully deleted.' ),
 285                  array(
 286                      'id'                 => 'message',
 287                      'additional_classes' => array( 'updated' ),
 288                      'dismissible'        => true,
 289                  )
 290              );
 291          }
 292  
 293          break;
 294  
 295      case 'delete':
 296          check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id );
 297  
 298          if ( is_nav_menu( $nav_menu_selected_id ) ) {
 299              $deletion = wp_delete_nav_menu( $nav_menu_selected_id );
 300          } else {
 301              // Reset the selected menu.
 302              $nav_menu_selected_id = 0;
 303              unset( $_REQUEST['menu'] );
 304          }
 305  
 306          if ( ! isset( $deletion ) ) {
 307              break;
 308          }
 309  
 310          if ( is_wp_error( $deletion ) ) {
 311              $messages[] = wp_get_admin_notice(
 312                  $deletion->get_error_message(),
 313                  array(
 314                      'id'                 => 'message',
 315                      'additional_classes' => array( 'error' ),
 316                      'dismissible'        => true,
 317                  )
 318              );
 319          } else {
 320              $messages[] = wp_get_admin_notice(
 321                  __( 'The menu has been successfully deleted.' ),
 322                  array(
 323                      'id'                 => 'message',
 324                      'additional_classes' => array( 'updated' ),
 325                      'dismissible'        => true,
 326                  )
 327              );
 328          }
 329  
 330          break;
 331  
 332      case 'delete_menus':
 333          check_admin_referer( 'nav_menus_bulk_actions' );
 334  
 335          foreach ( $_REQUEST['delete_menus'] as $menu_id_to_delete ) {
 336              if ( ! is_nav_menu( $menu_id_to_delete ) ) {
 337                  continue;
 338              }
 339  
 340              $deletion = wp_delete_nav_menu( $menu_id_to_delete );
 341  
 342              if ( is_wp_error( $deletion ) ) {
 343                  $messages[]     = wp_get_admin_notice(
 344                      $deletion->get_error_message(),
 345                      array(
 346                          'id'                 => 'message',
 347                          'additional_classes' => array( 'error' ),
 348                          'dismissible'        => true,
 349                      )
 350                  );
 351                  $deletion_error = true;
 352              }
 353          }
 354  
 355          if ( empty( $deletion_error ) ) {
 356              $messages[] = wp_get_admin_notice(
 357                  __( 'Selected menus have been successfully deleted.' ),
 358                  array(
 359                      'id'                 => 'message',
 360                      'additional_classes' => array( 'updated' ),
 361                      'dismissible'        => true,
 362                  )
 363              );
 364          }
 365  
 366          break;
 367  
 368      case 'update':
 369          check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
 370  
 371          // Merge new and existing menu locations if any new ones are set.
 372          $new_menu_locations = array();
 373          if ( isset( $_POST['menu-locations'] ) ) {
 374              $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
 375              $menu_locations     = array_merge( $menu_locations, $new_menu_locations );
 376          }
 377  
 378          // Add Menu.
 379          if ( 0 === $nav_menu_selected_id ) {
 380              $new_menu_title = trim( esc_html( $_POST['menu-name'] ) );
 381  
 382              if ( $new_menu_title ) {
 383                  $_nav_menu_selected_id = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_menu_title ) );
 384  
 385                  if ( is_wp_error( $_nav_menu_selected_id ) ) {
 386                      $messages[] = wp_get_admin_notice(
 387                          $_nav_menu_selected_id->get_error_message(),
 388                          array(
 389                              'id'                 => 'message',
 390                              'additional_classes' => array( 'error' ),
 391                              'dismissible'        => true,
 392                          )
 393                      );
 394                  } else {
 395                      $_menu_object            = wp_get_nav_menu_object( $_nav_menu_selected_id );
 396                      $nav_menu_selected_id    = $_nav_menu_selected_id;
 397                      $nav_menu_selected_title = $_menu_object->name;
 398  
 399                      if ( isset( $_REQUEST['menu-item'] ) ) {
 400                          wp_save_nav_menu_items( $nav_menu_selected_id, absint( $_REQUEST['menu-item'] ) );
 401                      }
 402  
 403                      if ( isset( $_REQUEST['zero-menu-state'] ) || ! empty( $_POST['auto-add-pages'] ) ) {
 404                          // If there are menu items, add them.
 405                          wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title );
 406                      }
 407  
 408                      if ( isset( $_REQUEST['zero-menu-state'] ) ) {
 409                          // Auto-save nav_menu_locations.
 410                          $locations = get_nav_menu_locations();
 411  
 412                          foreach ( $locations as $location => $menu_id ) {
 413                                  $locations[ $location ] = $nav_menu_selected_id;
 414                                  break; // There should only be 1.
 415                          }
 416  
 417                          set_theme_mod( 'nav_menu_locations', $locations );
 418                      } elseif ( count( $new_menu_locations ) > 0 ) {
 419                          // If locations have been selected for the new menu, save those.
 420                          $locations = get_nav_menu_locations();
 421  
 422                          foreach ( array_keys( $new_menu_locations ) as $location ) {
 423                              $locations[ $location ] = $nav_menu_selected_id;
 424                          }
 425  
 426                          set_theme_mod( 'nav_menu_locations', $locations );
 427                      }
 428  
 429                      if ( isset( $_REQUEST['use-location'] ) ) {
 430                          $locations      = get_registered_nav_menus();
 431                          $menu_locations = get_nav_menu_locations();
 432  
 433                          if ( isset( $locations[ $_REQUEST['use-location'] ] ) ) {
 434                              $menu_locations[ $_REQUEST['use-location'] ] = $nav_menu_selected_id;
 435                          }
 436  
 437                          set_theme_mod( 'nav_menu_locations', $menu_locations );
 438                      }
 439  
 440                      wp_redirect( admin_url( 'nav-menus.php?menu=' . $_nav_menu_selected_id ) );
 441                      exit;
 442                  }
 443              } else {
 444                  $messages[] = wp_get_admin_notice(
 445                      __( 'Please enter a valid menu name.' ),
 446                      array(
 447                          'id'                 => 'message',
 448                          'additional_classes' => array( 'error' ),
 449                          'dismissible'        => true,
 450                      )
 451                  );
 452              }
 453  
 454              // Update existing menu.
 455          } else {
 456              // Remove menu locations that have been unchecked.
 457              foreach ( $locations as $location => $description ) {
 458                  if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) )
 459                      && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] === $nav_menu_selected_id
 460                  ) {
 461                      unset( $menu_locations[ $location ] );
 462                  }
 463              }
 464  
 465              // Set menu locations.
 466              set_theme_mod( 'nav_menu_locations', $menu_locations );
 467  
 468              $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
 469  
 470              $menu_title = trim( esc_html( $_POST['menu-name'] ) );
 471  
 472              if ( ! $menu_title ) {
 473                  $messages[] = wp_get_admin_notice(
 474                      __( 'Please enter a valid menu name.' ),
 475                      array(
 476                          'id'                 => 'message',
 477                          'additional_classes' => array( 'error' ),
 478                          'dismissible'        => true,
 479                      )
 480                  );
 481                  $menu_title = $_menu_object->name;
 482              }
 483  
 484              if ( ! is_wp_error( $_menu_object ) ) {
 485                  $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) );
 486  
 487                  if ( is_wp_error( $_nav_menu_selected_id ) ) {
 488                      $_menu_object = $_nav_menu_selected_id;
 489                      $messages[]   = wp_get_admin_notice(
 490                          $_nav_menu_selected_id->get_error_message(),
 491                          array(
 492                              'id'                 => 'message',
 493                              'additional_classes' => array( 'error' ),
 494                              'dismissible'        => true,
 495                          )
 496                      );
 497                  } else {
 498                      $_menu_object            = wp_get_nav_menu_object( $_nav_menu_selected_id );
 499                      $nav_menu_selected_title = $_menu_object->name;
 500                  }
 501              }
 502  
 503              // Update menu items.
 504              if ( ! is_wp_error( $_menu_object ) ) {
 505                  $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $_nav_menu_selected_id, $nav_menu_selected_title ) );
 506  
 507                  // If the menu ID changed, redirect to the new URL.
 508                  if ( $nav_menu_selected_id !== $_nav_menu_selected_id ) {
 509                      wp_redirect( admin_url( 'nav-menus.php?menu=' . (int) $_nav_menu_selected_id ) );
 510                      exit;
 511                  }
 512              }
 513          }
 514  
 515          break;
 516  
 517      case 'locations':
 518          if ( ! $num_locations ) {
 519              wp_redirect( admin_url( 'nav-menus.php' ) );
 520              exit;
 521          }
 522  
 523          add_filter( 'screen_options_show_screen', '__return_false' );
 524  
 525          if ( isset( $_POST['menu-locations'] ) ) {
 526              check_admin_referer( 'save-menu-locations' );
 527  
 528              $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
 529              $menu_locations     = array_merge( $menu_locations, $new_menu_locations );
 530              // Set menu locations.
 531              set_theme_mod( 'nav_menu_locations', $menu_locations );
 532  
 533              $messages[] = wp_get_admin_notice(
 534                  __( 'Menu locations updated.' ),
 535                  array(
 536                      'id'                 => 'message',
 537                      'additional_classes' => array( 'updated' ),
 538                      'dismissible'        => true,
 539                  )
 540              );
 541          }
 542  
 543          break;
 544  }
 545  
 546  // Get all nav menus.
 547  $nav_menus  = wp_get_nav_menus();
 548  $menu_count = count( $nav_menus );
 549  
 550  // Are we on the add new screen?
 551  $add_new_screen = isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'];
 552  
 553  $locations_screen = isset( $_GET['action'] ) && 'locations' === $_GET['action'];
 554  
 555  $page_count = wp_count_posts( 'page' );
 556  
 557  /*
 558   * If we have one registered menu location, and zero menus, we take them right
 559   * into editing their first menu.
 560   */
 561  if ( 1 === count( get_registered_nav_menus() ) && ! $add_new_screen
 562      && empty( $nav_menus ) && ! empty( $page_count->publish )
 563  ) {
 564      $one_theme_location_no_menus = true;
 565  } else {
 566      $one_theme_location_no_menus = false;
 567  }
 568  
 569  $nav_menus_l10n = array(
 570      'oneThemeLocationNoMenus' => $one_theme_location_no_menus,
 571      'moveUp'                  => __( 'Move up one' ),
 572      'moveDown'                => __( 'Move down one' ),
 573      'moveToTop'               => __( 'Move to the top' ),
 574      /* translators: %s: Previous item name. */
 575      'moveUnder'               => __( 'Move under %s' ),
 576      /* translators: %s: Previous item name. */
 577      'moveOutFrom'             => __( 'Move out from under %s' ),
 578      /* translators: %s: Previous item name. */
 579      'under'                   => __( 'Under %s' ),
 580      /* translators: %s: Previous item name. */
 581      'outFrom'                 => __( 'Out from under %s' ),
 582      /* translators: 1: Item name, 2: Item type, 3: Item index, 4: Total items. */
 583      'menuFocus'               => __( 'Edit %1$s (%2$s, %3$d of %4$d)' ),
 584      /* translators: 1: Item name, 2: Item type, 3: Item index, 4: Total items, 5: Item parent. */
 585      'subMenuFocus'            => __( 'Edit %1$s (%2$s, sub-item %3$d of %4$d under %5$s)' ),
 586      /* translators: 1: Item name, 2: Item type, 3: Item index, 4: Total items, 5: Item parent, 6: Item depth. */
 587      'subMenuMoreDepthFocus'   => __( 'Edit %1$s (%2$s, sub-item %3$d of %4$d under %5$s, level %6$d)' ),
 588      /* translators: %s: Item name. */
 589      'menuItemDeletion'        => __( 'item %s' ),
 590      /* translators: %s: Item name. */
 591      'itemsDeleted'            => __( 'Deleted menu item: %s.' ),
 592      'itemAdded'               => __( 'Menu item added' ),
 593      'itemRemoved'             => __( 'Menu item removed' ),
 594      'movedUp'                 => __( 'Menu item moved up' ),
 595      'movedDown'               => __( 'Menu item moved down' ),
 596      'movedTop'                => __( 'Menu item moved to the top' ),
 597      'movedLeft'               => __( 'Menu item moved out of submenu' ),
 598      'movedRight'              => __( 'Menu item is now a sub-item' ),
 599      'parentUpdated'           => __( 'Menu parent updated' ),
 600      'orderUpdated'            => __( 'Menu order updated' ),
 601  );
 602  wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n );
 603  
 604  /*
 605   * Redirect to add screen if there are no menus and this user has either zero
 606   * or more than one registered menu location.
 607   */
 608  if ( 0 === $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus ) {
 609      wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) );
 610  }
 611  
 612  // Get recently edited nav menu.
 613  $recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) );
 614  if ( empty( $recently_edited ) && is_nav_menu( $nav_menu_selected_id ) ) {
 615      $recently_edited = $nav_menu_selected_id;
 616  }
 617  
 618  // Use $recently_edited if none are selected.
 619  if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) ) {
 620      $nav_menu_selected_id = $recently_edited;
 621  }
 622  
 623  // On deletion of menu, if another menu exists, show it.
 624  if ( ! $add_new_screen && $menu_count > 0 && isset( $_GET['action'] ) && 'delete' === $_GET['action'] ) {
 625      $nav_menu_selected_id = $nav_menus[0]->term_id;
 626  }
 627  
 628  // Set $nav_menu_selected_id to 0 if no menus.
 629  if ( $one_theme_location_no_menus ) {
 630      $nav_menu_selected_id = 0;
 631  } elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) {
 632      // If we have no selection yet, and we have menus, set to the first one in the list.
 633      $nav_menu_selected_id = $nav_menus[0]->term_id;
 634  }
 635  
 636  // Update the user's setting.
 637  if ( $nav_menu_selected_id !== $recently_edited && is_nav_menu( $nav_menu_selected_id ) ) {
 638      update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
 639  }
 640  
 641  // If there's a menu, get its name.
 642  if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) {
 643      $_menu_object            = wp_get_nav_menu_object( $nav_menu_selected_id );
 644      $nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : '';
 645  }
 646  
 647  // Generate truncated menu names.
 648  foreach ( (array) $nav_menus as $key => $_nav_menu ) {
 649      $nav_menus[ $key ]->truncated_name = wp_html_excerpt( $_nav_menu->name, 40, '&hellip;' );
 650  }
 651  
 652  // Retrieve menu locations.
 653  if ( current_theme_supports( 'menus' ) ) {
 654      $locations      = get_registered_nav_menus();
 655      $menu_locations = get_nav_menu_locations();
 656  }
 657  
 658  /*
 659   * Ensure the user will be able to scroll horizontally
 660   * by adding a class for the max menu depth.
 661   *
 662   * @global int $_wp_nav_menu_max_depth
 663   */
 664  global $_wp_nav_menu_max_depth;
 665  $_wp_nav_menu_max_depth = 0;
 666  
 667  // Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth.
 668  if ( is_nav_menu( $nav_menu_selected_id ) ) {
 669      $menu_items  = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'post_status' => 'any' ) );
 670      $edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id );
 671  }
 672  
 673  /**
 674   * Adds a CSS class to display the max depth of the navigation menu.
 675   *
 676   * @since 3.0.0
 677   *
 678   * @global int $_wp_nav_menu_max_depth Maximum depth of the navigation menu.
 679   *
 680   * @param string $classes Existing CSS classes for the body tag.
 681   * @return string Modified CSS classes including the menu max depth class.
 682   */
 683  function wp_nav_menu_max_depth( $classes ) {
 684      global $_wp_nav_menu_max_depth;
 685      return "$classes menu-max-depth-$_wp_nav_menu_max_depth";
 686  }
 687  
 688  add_filter( 'admin_body_class', 'wp_nav_menu_max_depth' );
 689  
 690  wp_nav_menu_setup();
 691  wp_initial_nav_menu_meta_boxes();
 692  
 693  if ( ! current_theme_supports( 'menus' ) && ! $num_locations ) {
 694      $message_no_theme_support = sprintf(
 695          /* translators: %s: URL to Widgets screen. */
 696          __( 'Your theme does not natively support menus, but you can use them in sidebars by adding a &#8220;Navigation Menu&#8221; widget on the <a href="%s">Widgets</a> screen.' ),
 697          admin_url( 'widgets.php' )
 698      );
 699      $messages[] = wp_get_admin_notice(
 700          $message_no_theme_support,
 701          array(
 702              'id'                 => 'message',
 703              'additional_classes' => array( 'updated' ),
 704              'dismissible'        => true,
 705          )
 706      );
 707  }
 708  
 709  if ( ! $locations_screen ) : // Main tab.
 710      $overview  = '<p>' . __( 'This screen is used for managing your navigation menus.' ) . '</p>';
 711      $overview .= '<p>' . sprintf(
 712          /* translators: 1: URL to Widgets screen, 2 and 3: The names of the default themes. */
 713          __( 'Menus can be displayed in locations defined by your theme, even used in sidebars by adding a &#8220;Navigation Menu&#8221; widget on the <a href="%1$s">Widgets</a> screen. If your theme does not support the navigation menus feature (the default themes, %2$s and %3$s, do), you can learn about adding this support by following the documentation link to the side.' ),
 714          admin_url( 'widgets.php' ),
 715          'Twenty Twenty',
 716          'Twenty Twenty-One'
 717      ) . '</p>';
 718      $overview .= '<p>' . __( 'From this screen you can:' ) . '</p>';
 719      $overview .= '<ul><li>' . __( 'Create, edit, and delete menus' ) . '</li>';
 720      $overview .= '<li>' . __( 'Add, organize, and modify individual menu items' ) . '</li></ul>';
 721  
 722      get_current_screen()->add_help_tab(
 723          array(
 724              'id'      => 'overview',
 725              'title'   => __( 'Overview' ),
 726              'content' => $overview,
 727          )
 728      );
 729  
 730      $menu_management  = '<p>' . __( 'The menu management box at the top of the screen is used to control which menu is opened in the editor below.' ) . '</p>';
 731      $menu_management .= '<ul><li>' . __( 'To edit an existing menu, <strong>choose a menu from the dropdown and click Select</strong>' ) . '</li>';
 732      $menu_management .= '<li>' . __( 'If you have not yet created any menus, <strong>click the &#8217;create a new menu&#8217; link</strong> to get started' ) . '</li></ul>';
 733      $menu_management .= '<p>' . __( 'You can assign individual menus to the theme&#8217;s menu locations by <strong>selecting the desired settings</strong> at the bottom of the menu editor. To assign menus to all theme menu locations at once, <strong>visit the Manage Locations tab</strong> at the top of the screen.' ) . '</p>';
 734  
 735      get_current_screen()->add_help_tab(
 736          array(
 737              'id'      => 'menu-management',
 738              'title'   => __( 'Menu Management' ),
 739              'content' => $menu_management,
 740          )
 741      );
 742  
 743      $editing_menus  = '<p>' . __( 'Each navigation menu may contain a mix of links to pages, categories, custom URLs or other content types. Menu links are added by selecting items from the expanding boxes in the left-hand column below.' ) . '</p>';
 744      $editing_menus .= '<p>' . __( '<strong>Clicking the arrow to the right of any menu item</strong> in the editor will reveal a standard group of settings. Additional settings such as link target, CSS classes, link relationships, and link descriptions can be enabled and disabled via the Screen Options tab.' ) . '</p>';
 745      $editing_menus .= '<ul><li>' . __( 'Add one or several items at once by <strong>selecting the checkbox next to each item and clicking Add to Menu</strong>' ) . '</li>';
 746      $editing_menus .= '<li>' . __( 'To add a custom link, <strong>expand the Custom Links section, enter a URL and link text, and click Add to Menu</strong>' ) . '</li>';
 747      $editing_menus .= '<li>' . __( 'To reorganize menu items, <strong>drag and drop items with your mouse or use your keyboard</strong>. Drag or move a menu item a little to the right to make it a submenu' ) . '</li>';
 748      $editing_menus .= '<li>' . __( 'Delete a menu item by <strong>expanding it and clicking the Remove link</strong>' ) . '</li></ul>';
 749  
 750      get_current_screen()->add_help_tab(
 751          array(
 752              'id'      => 'editing-menus',
 753              'title'   => __( 'Editing Menus' ),
 754              'content' => $editing_menus,
 755          )
 756      );
 757  else : // Locations tab.
 758      $locations_overview  = '<p>' . __( 'This screen is used for globally assigning menus to locations defined by your theme.' ) . '</p>';
 759      $locations_overview .= '<ul><li>' . __( 'To assign menus to one or more theme menu locations, <strong>select a menu from each location&#8217;s dropdown</strong>. When you are finished, <strong>click Save Changes</strong>' ) . '</li>';
 760      $locations_overview .= '<li>' . __( 'To edit a menu currently assigned to a theme menu location, <strong>click the adjacent &#8217;Edit&#8217; link</strong>' ) . '</li>';
 761      $locations_overview .= '<li>' . __( 'To add a new menu instead of assigning an existing one, <strong>click the &#8217;Use new menu&#8217; link</strong>. Your new menu will be automatically assigned to that location in the theme.' ) . '</li></ul>';
 762  
 763      get_current_screen()->add_help_tab(
 764          array(
 765              'id'      => 'locations-overview',
 766              'title'   => __( 'Overview' ),
 767              'content' => $locations_overview,
 768          )
 769      );
 770  endif;
 771  
 772  get_current_screen()->set_help_sidebar(
 773      '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 774      '<p>' . __( '<a href="https://wordpress.org/documentation/article/appearance-menus-screen/">Documentation on Menus</a>' ) . '</p>' .
 775      '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
 776  );
 777  
 778  // Get the admin header.
 779  require_once  ABSPATH . 'wp-admin/admin-header.php';
 780  ?>
 781  <div class="wrap">
 782      <h1 class="wp-heading-inline"><?php esc_html_e( 'Menus' ); ?></h1>
 783      <?php
 784      if ( current_user_can( 'customize' ) ) :
 785          $focus = $locations_screen ? array( 'section' => 'menu_locations' ) : array( 'panel' => 'nav_menus' );
 786          printf(
 787              ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>',
 788              esc_url(
 789                  add_query_arg(
 790                      array(
 791                          array( 'autofocus' => $focus ),
 792                          'return' => urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ),
 793                      ),
 794                      admin_url( 'customize.php' )
 795                  )
 796              ),
 797              __( 'Manage with Live Preview' )
 798          );
 799      endif;
 800  
 801      $nav_tab_active_class = '';
 802      $nav_aria_current     = '';
 803  
 804      if ( ! isset( $_GET['action'] ) || isset( $_GET['action'] ) && 'locations' !== $_GET['action'] ) {
 805          $nav_tab_active_class = ' nav-tab-active';
 806          $nav_aria_current     = ' aria-current="page"';
 807      }
 808      ?>
 809  
 810      <hr class="wp-header-end">
 811  
 812      <nav class="nav-tab-wrapper wp-clearfix" aria-label="<?php esc_attr_e( 'Secondary menu' ); ?>">
 813          <a href="<?php echo esc_url( admin_url( 'nav-menus.php' ) ); ?>" class="nav-tab<?php echo $nav_tab_active_class; ?>"<?php echo $nav_aria_current; ?>><?php esc_html_e( 'Edit Menus' ); ?></a>
 814          <?php
 815          if ( $num_locations && $menu_count ) {
 816              $active_tab_class = '';
 817              $aria_current     = '';
 818  
 819              if ( $locations_screen ) {
 820                  $active_tab_class = ' nav-tab-active';
 821                  $aria_current     = ' aria-current="page"';
 822              }
 823              ?>
 824              <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>" class="nav-tab<?php echo $active_tab_class; ?>"<?php echo $aria_current; ?>><?php esc_html_e( 'Manage Locations' ); ?></a>
 825              <?php
 826          }
 827          ?>
 828      </nav>
 829      <?php
 830      foreach ( $messages as $message ) :
 831          echo $message . "\n";
 832      endforeach;
 833      ?>
 834      <?php
 835      if ( $locations_screen ) :
 836          if ( 1 === $num_locations ) {
 837              echo '<p>' . __( 'Your theme supports one menu. Select which menu you would like to use.' ) . '</p>';
 838          } else {
 839              echo '<p>' . sprintf(
 840                  /* translators: %s: Number of menus. */
 841                  _n(
 842                      'Your theme supports %s menu. Select which menu appears in each location.',
 843                      'Your theme supports %s menus. Select which menu appears in each location.',
 844                      $num_locations
 845                  ),
 846                  number_format_i18n( $num_locations )
 847              ) . '</p>';
 848          }
 849          ?>
 850      <div id="menu-locations-wrap">
 851          <form method="post" action="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>">
 852              <table class="widefat fixed" id="menu-locations-table">
 853                  <thead>
 854                  <tr>
 855                      <th scope="col" class="manage-column column-locations"><?php _e( 'Menu Location' ); ?></th>
 856                      <th scope="col" class="manage-column column-menus"><?php _e( 'Assigned Menu' ); ?></th>
 857                  </tr>
 858                  </thead>
 859                  <tbody class="menu-locations">
 860                  <?php foreach ( $locations as $_location => $_name ) { ?>
 861                      <tr class="menu-locations-row">
 862                          <td class="menu-location-title"><label for="locations-<?php echo $_location; ?>"><?php echo $_name; ?></label></td>
 863                          <td class="menu-location-menus">
 864                              <select name="menu-locations[<?php echo $_location; ?>]" id="locations-<?php echo $_location; ?>">
 865                                  <option value="0"><?php printf( '&mdash; %s &mdash;', esc_html__( 'Select a Menu' ) ); ?></option>
 866                                  <?php
 867                                  foreach ( $nav_menus as $menu ) :
 868                                      $data_orig = '';
 869                                      $selected  = isset( $menu_locations[ $_location ] ) && $menu_locations[ $_location ] === $menu->term_id;
 870  
 871                                      if ( $selected ) {
 872                                          $data_orig = 'data-orig="true"';
 873                                      }
 874                                      ?>
 875                                      <option <?php echo $data_orig; ?> <?php selected( $selected ); ?> value="<?php echo $menu->term_id; ?>">
 876                                          <?php echo wp_html_excerpt( $menu->name, 40, '&hellip;' ); ?>
 877                                      </option>
 878                                  <?php endforeach; ?>
 879                              </select>
 880                              <div class="locations-row-links">
 881                                  <?php if ( isset( $menu_locations[ $_location ] ) && 0 !== $menu_locations[ $_location ] ) : ?>
 882                                  <span class="locations-edit-menu-link">
 883                                      <?php
 884                                      printf(
 885                                          '<a href="%1$s">
 886                                              <span aria-hidden="true">%2$s</span>
 887                                              <span class="screen-reader-text">%3$s</span>
 888                                          </a>',
 889                                          esc_url(
 890                                              add_query_arg(
 891                                                  array(
 892                                                      'action' => 'edit',
 893                                                      'menu' => $menu_locations[ $_location ],
 894                                                  ),
 895                                                  admin_url( 'nav-menus.php' )
 896                                              )
 897                                          ),
 898                                          _x( 'Edit', 'menu' ),
 899                                          /* translators: Hidden accessibility text. */
 900                                          __( 'Edit selected menu' )
 901                                      );
 902                                      ?>
 903                                  </span>
 904                                  <?php endif; ?>
 905                                  <span class="locations-add-menu-link">
 906                                      <?php
 907                                      printf(
 908                                          '<a href="%1$s">%2$s</a>',
 909                                          esc_url(
 910                                              add_query_arg(
 911                                                  array(
 912                                                      'action' => 'edit',
 913                                                      'menu' => 0,
 914                                                      'use-location' => $_location,
 915                                                  ),
 916                                                  admin_url( 'nav-menus.php' )
 917                                              )
 918                                          ),
 919                                          _x( 'Use new menu', 'menu' )
 920                                      );
 921                                      ?>
 922                                  </span>
 923                              </div><!-- .locations-row-links -->
 924                          </td><!-- .menu-location-menus -->
 925                      </tr><!-- .menu-locations-row -->
 926                  <?php } // End foreach. ?>
 927                  </tbody>
 928              </table>
 929              <p class="button-controls wp-clearfix"><?php submit_button( __( 'Save Changes' ), 'primary left', 'nav-menu-locations', false ); ?></p>
 930              <?php wp_nonce_field( 'save-menu-locations' ); ?>
 931              <input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
 932          </form>
 933      </div><!-- #menu-locations-wrap -->
 934          <?php
 935          /**
 936           * Fires after the menu locations table is displayed.
 937           *
 938           * @since 3.6.0
 939           */
 940          do_action( 'after_menu_locations_table' );
 941          ?>
 942      <?php else : ?>
 943      <div class="manage-menus">
 944          <?php if ( $menu_count < 1 ) : ?>
 945          <span class="first-menu-message">
 946              <?php _e( 'Create your first menu below.' ); ?>
 947              <span class="screen-reader-text">
 948                  <?php
 949                  /* translators: Hidden accessibility text. */
 950                  _e( 'Fill in the Menu Name and click the Create Menu button to create your first menu.' );
 951                  ?>
 952              </span>
 953          </span><!-- /first-menu-message -->
 954          <?php elseif ( $menu_count < 2 ) : ?>
 955          <span class="add-edit-menu-action">
 956              <?php
 957              printf(
 958                  /* translators: %s: URL to create a new menu. */
 959                  __( 'Edit your menu below, or <a href="%s">create a new menu</a>. Do not forget to save your changes!' ),
 960                  esc_url(
 961                      add_query_arg(
 962                          array(
 963                              'action' => 'edit',
 964                              'menu'   => 0,
 965                          ),
 966                          admin_url( 'nav-menus.php' )
 967                      )
 968                  )
 969              );
 970              ?>
 971              <span class="screen-reader-text">
 972                  <?php
 973                  /* translators: Hidden accessibility text. */
 974                  _e( 'Click the Save Menu button to save your changes.' );
 975                  ?>
 976              </span>
 977          </span><!-- /add-edit-menu-action -->
 978          <?php else : ?>
 979              <form method="get" action="<?php echo esc_url( admin_url( 'nav-menus.php' ) ); ?>">
 980              <input type="hidden" name="action" value="edit" />
 981              <label for="select-menu-to-edit" class="selected-menu"><?php _e( 'Select a menu to edit:' ); ?></label>
 982              <select name="menu" id="select-menu-to-edit">
 983                  <?php if ( $add_new_screen ) : ?>
 984                      <option value="0" selected="selected"><?php _e( '&mdash; Select &mdash;' ); ?></option>
 985                  <?php endif; ?>
 986                  <?php foreach ( (array) $nav_menus as $_nav_menu ) : ?>
 987                      <option value="<?php echo esc_attr( $_nav_menu->term_id ); ?>" <?php selected( $_nav_menu->term_id, $nav_menu_selected_id ); ?>>
 988                          <?php
 989                          echo esc_html( $_nav_menu->truncated_name );
 990  
 991                          if ( ! empty( $menu_locations ) && in_array( $_nav_menu->term_id, $menu_locations, true ) ) {
 992                              $locations_assigned_to_this_menu = array();
 993  
 994                              foreach ( array_keys( $menu_locations, $_nav_menu->term_id, true ) as $menu_location_key ) {
 995                                  if ( isset( $locations[ $menu_location_key ] ) ) {
 996                                      $locations_assigned_to_this_menu[] = $locations[ $menu_location_key ];
 997                                  }
 998                              }
 999  
1000                              /**
1001                               * Filters the number of locations listed per menu in the drop-down select.
1002                               *
1003                               * @since 3.6.0
1004                               *
1005                               * @param int $locations Number of menu locations to list. Default 3.
1006                               */
1007                              $locations_listed_per_menu = absint( apply_filters( 'wp_nav_locations_listed_per_menu', 3 ) );
1008  
1009                              $assigned_locations = array_slice( $locations_assigned_to_this_menu, 0, $locations_listed_per_menu );
1010  
1011                              // Adds ellipses following the number of locations defined in $assigned_locations.
1012                              if ( ! empty( $assigned_locations ) ) {
1013                                  printf(
1014                                      ' (%1$s%2$s)',
1015                                      implode( ', ', $assigned_locations ),
1016                                      count( $locations_assigned_to_this_menu ) > count( $assigned_locations ) ? ' &hellip;' : ''
1017                                  );
1018                              }
1019                          }
1020                          ?>
1021                      </option>
1022                  <?php endforeach; ?>
1023              </select>
1024              <span class="submit-btn"><input type="submit" class="button" value="<?php esc_attr_e( 'Select' ); ?>"></span>
1025              <span class="add-new-menu-action">
1026                  <?php
1027                  printf(
1028                      /* translators: %s: URL to create a new menu. */
1029                      __( 'or <a href="%s">create a new menu</a>. Do not forget to save your changes!' ),
1030                      esc_url(
1031                          add_query_arg(
1032                              array(
1033                                  'action' => 'edit',
1034                                  'menu'   => 0,
1035                              ),
1036                              admin_url( 'nav-menus.php' )
1037                          )
1038                      )
1039                  );
1040                  ?>
1041                  <span class="screen-reader-text">
1042                      <?php
1043                      /* translators: Hidden accessibility text. */
1044                      _e( 'Click the Save Menu button to save your changes.' );
1045                      ?>
1046                  </span>
1047              </span><!-- /add-new-menu-action -->
1048          </form>
1049              <?php
1050          endif;
1051  
1052          $metabox_holder_disabled_class = '';
1053  
1054          if ( isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'] ) {
1055              $metabox_holder_disabled_class = ' metabox-holder-disabled';
1056          }
1057          ?>
1058      </div><!-- /manage-menus -->
1059      <div id="nav-menus-frame" class="wp-clearfix">
1060      <div id="menu-settings-column" class="metabox-holder<?php echo $metabox_holder_disabled_class; ?>">
1061  
1062          <div class="clear"></div>
1063  
1064          <form id="nav-menu-meta" class="nav-menu-meta" method="post" enctype="multipart/form-data">
1065              <input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
1066              <input type="hidden" name="action" value="add-menu-item" />
1067              <?php wp_nonce_field( 'add-menu_item', 'menu-settings-column-nonce' ); ?>
1068              <h2><?php _e( 'Add menu items' ); ?></h2>
1069              <?php do_accordion_sections( 'nav-menus', 'side', null ); ?>
1070          </form>
1071  
1072      </div><!-- /#menu-settings-column -->
1073      <div id="menu-management-liquid">
1074          <div id="menu-management">
1075              <form id="update-nav-menu" method="post" enctype="multipart/form-data">
1076                  <h2><?php _e( 'Menu structure' ); ?></h2>
1077                  <div class="menu-edit">
1078                      <input type="hidden" name="nav-menu-data">
1079                      <?php
1080                      wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
1081                      wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
1082                      wp_nonce_field( 'update-nav_menu', 'update-nav-menu-nonce' );
1083  
1084                      $menu_name_aria_desc = $add_new_screen ? ' aria-describedby="menu-name-desc"' : '';
1085  
1086                      if ( $one_theme_location_no_menus ) {
1087                          $menu_name_val = 'value="' . esc_attr__( 'Menu 1' ) . '"';
1088                          ?>
1089                          <input type="hidden" name="zero-menu-state" value="true" />
1090                          <?php
1091                      } else {
1092                          $menu_name_val = 'value="' . esc_attr( $nav_menu_selected_title ) . '"';
1093                      }
1094                      ?>
1095                      <input type="hidden" name="action" value="update" />
1096                      <input type="hidden" name="menu" id="menu" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
1097                      <div id="nav-menu-header">
1098                          <div class="major-publishing-actions wp-clearfix">
1099                              <label class="menu-name-label" for="menu-name"><?php _e( 'Menu Name' ); ?></label>
1100                              <input name="menu-name" id="menu-name" type="text" class="menu-name regular-text menu-item-textbox form-required" required="required" <?php echo $menu_name_val . $menu_name_aria_desc; ?> />
1101                              <div class="publishing-action">
1102                                  <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'primary large menu-save', 'save_menu', false, array( 'id' => 'save_menu_header' ) ); ?>
1103                              </div><!-- END .publishing-action -->
1104                          </div><!-- END .major-publishing-actions -->
1105                      </div><!-- END .nav-menu-header -->
1106                      <div id="post-body">
1107                          <div id="post-body-content" class="wp-clearfix">
1108                              <?php if ( ! $add_new_screen ) : ?>
1109                                  <?php
1110                                  $hide_style = '';
1111  
1112                                  if ( isset( $menu_items ) && 0 === count( $menu_items ) ) {
1113                                      $hide_style = 'style="display: none;"';
1114                                  }
1115  
1116                                  if ( $one_theme_location_no_menus ) {
1117                                      $starter_copy = __( 'Edit your default menu by adding or removing items. Drag the items into the order you prefer. Click Create Menu to save your changes.' );
1118                                  } else {
1119                                      $starter_copy = __( 'Drag the items into the order you prefer. Click the arrow on the right of the item to reveal additional configuration options.' );
1120                                  }
1121                                  ?>
1122                                  <div class="drag-instructions post-body-plain" <?php echo $hide_style; ?>>
1123                                      <p><?php echo $starter_copy; ?></p>
1124                                  </div>
1125  
1126                                  <div id="nav-menu-bulk-actions-top" class="bulk-actions" <?php echo $hide_style; ?>>
1127                                      <label class="bulk-select-button" for="bulk-select-switcher-top">
1128                                          <input type="checkbox" id="bulk-select-switcher-top" name="bulk-select-switcher-top" class="bulk-select-switcher">
1129                                          <span class="bulk-select-button-label"><?php _e( 'Bulk Select' ); ?></span>
1130                                      </label>
1131                                  </div>
1132  
1133                                  <?php
1134                                  if ( isset( $edit_markup ) && ! is_wp_error( $edit_markup ) ) {
1135                                      echo $edit_markup;
1136                                  } else {
1137                                      ?>
1138                                      <ul class="menu" id="menu-to-edit"></ul>
1139                                  <?php } ?>
1140  
1141                              <?php endif; ?>
1142  
1143                              <?php if ( $add_new_screen ) : ?>
1144                                  <p class="post-body-plain" id="menu-name-desc"><?php _e( 'Give your menu a name, then click Create Menu.' ); ?></p>
1145                                  <?php if ( isset( $_GET['use-location'] ) ) : ?>
1146                                      <input type="hidden" name="use-location" value="<?php echo esc_attr( $_GET['use-location'] ); ?>" />
1147                                  <?php endif; ?>
1148  
1149                                  <?php
1150                              endif;
1151  
1152                              $no_menus_style = '';
1153  
1154                              if ( $one_theme_location_no_menus ) {
1155                                  $no_menus_style = 'style="display: none;"';
1156                              }
1157                              ?>
1158  
1159                              <?php if ( ! $add_new_screen ) : ?>
1160                                  <div id="nav-menu-bulk-actions-bottom" class="bulk-actions" <?php echo $hide_style; ?>>
1161                                      <label class="bulk-select-button" for="bulk-select-switcher-bottom">
1162                                          <input type="checkbox" id="bulk-select-switcher-bottom" name="bulk-select-switcher-top" class="bulk-select-switcher">
1163                                          <span class="bulk-select-button-label"><?php _e( 'Bulk Select' ); ?></span>
1164                                      </label>
1165                                      <input type="button" class="deletion menu-items-delete disabled" value="<?php esc_attr_e( 'Remove Selected Items' ); ?>">
1166                                      <div id="pending-menu-items-to-delete">
1167                                          <p><?php _e( 'List of menu items selected for deletion:' ); ?></p>
1168                                          <ul></ul>
1169                                      </div>
1170                                  </div>
1171                              <?php endif; ?>
1172  
1173                              <div class="menu-settings" <?php echo $no_menus_style; ?>>
1174                                  <h3><?php _e( 'Menu Settings' ); ?></h3>
1175                                  <?php
1176                                  if ( ! isset( $auto_add ) ) {
1177                                      $auto_add = get_option( 'nav_menu_options' );
1178  
1179                                      if ( ! isset( $auto_add['auto_add'] ) ) {
1180                                          $auto_add = false;
1181                                      } elseif ( in_array( $nav_menu_selected_id, $auto_add['auto_add'], true ) ) {
1182                                          $auto_add = true;
1183                                      } else {
1184                                          $auto_add = false;
1185                                      }
1186                                  }
1187                                  ?>
1188  
1189                                  <fieldset class="menu-settings-group auto-add-pages">
1190                                      <legend class="menu-settings-group-name howto"><?php _e( 'Auto add pages' ); ?></legend>
1191                                      <div class="menu-settings-input checkbox-input">
1192                                          <input type="checkbox"<?php checked( $auto_add ); ?> name="auto-add-pages" id="auto-add-pages" value="1" /> <label for="auto-add-pages"><?php printf( __( 'Automatically add new top-level pages to this menu' ), esc_url( admin_url( 'edit.php?post_type=page' ) ) ); ?></label>
1193                                      </div>
1194                                  </fieldset>
1195  
1196                                  <?php if ( current_theme_supports( 'menus' ) ) : ?>
1197  
1198                                      <fieldset class="menu-settings-group menu-theme-locations">
1199                                          <legend class="menu-settings-group-name howto"><?php _e( 'Menu location' ); ?></legend>
1200                                          <?php
1201                                          foreach ( $locations as $location => $description ) :
1202                                              $checked               = false;
1203                                              $theme_location_set_id = '';
1204  
1205                                              if ( isset( $menu_locations[ $location ] )
1206                                                  && 0 !== $nav_menu_selected_id
1207                                                  && $menu_locations[ $location ] === $nav_menu_selected_id
1208                                              ) {
1209                                                  $checked = true;
1210                                              }
1211  
1212                                              if ( ! empty( $menu_locations[ $location ] ) && $menu_locations[ $location ] !== $nav_menu_selected_id ) {
1213                                                  $theme_location_set_id = "theme-location-set-$location";
1214                                              }
1215                                              ?>
1216                                              <div class="menu-settings-input checkbox-input">
1217                                                  <input type="checkbox"<?php checked( $checked ); ?>
1218                                                      name="menu-locations[<?php echo esc_attr( $location ); ?>]"
1219                                                      id="locations-<?php echo esc_attr( $location ); ?>"
1220                                                      value="<?php echo esc_attr( $nav_menu_selected_id ); ?>"
1221                                                      <?php if ( '' !== $theme_location_set_id ) : ?>
1222                                                          aria-describedby="<?php echo esc_attr( $theme_location_set_id ); ?>"
1223                                                      <?php endif; ?>
1224                                                  />
1225                                                  <label for="locations-<?php echo esc_attr( $location ); ?>">
1226                                                      <?php echo esc_html( $description ); ?>
1227                                                  </label>
1228                                                  <?php if ( '' !== $theme_location_set_id ) : ?>
1229                                                      <span class="theme-location-set" id="<?php echo esc_attr( $theme_location_set_id ); ?>">
1230                                                          <?php
1231                                                          printf(
1232                                                              /* translators: %s: Menu name, or a message indicating that the menu was not found. */
1233                                                              _x( '(Currently set to: %s)', 'menu location' ),
1234                                                              is_nav_menu( $menu_locations[ $location ] )
1235                                                                  ? esc_html( wp_get_nav_menu_object( $menu_locations[ $location ] )->name )
1236                                                                  : __( 'an unknown menu' )
1237                                                          );
1238                                                          ?>
1239                                                      </span>
1240                                                  <?php endif; ?>
1241                                              </div>
1242                                          <?php endforeach; ?>
1243                                      </fieldset>
1244  
1245                                  <?php endif; ?>
1246  
1247                              </div>
1248                          </div><!-- /#post-body-content -->
1249                      </div><!-- /#post-body -->
1250                      <div id="nav-menu-footer">
1251                          <div class="major-publishing-actions">
1252                              <div class="publishing-action">
1253                                  <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'primary large menu-save', 'save_menu', false, array( 'id' => 'save_menu_footer' ) ); ?>
1254                              </div><!-- END .publishing-action -->
1255                              <?php if ( $menu_count > 0 ) : ?>
1256  
1257                                  <?php if ( $add_new_screen ) : ?>
1258                                  <span class="cancel-action">
1259                                      <?php
1260                                      printf(
1261                                          '<a class="submitcancel cancellation menu-cancel" href="%1$s">%2$s</a>',
1262                                          esc_url( admin_url( 'nav-menus.php' ) ),
1263                                          __( 'Cancel' )
1264                                      );
1265                                      ?>
1266                                  </span><!-- END .cancel-action -->
1267                                  <?php else : ?>
1268                                  <span class="delete-action">
1269                                      <?php
1270                                      printf(
1271                                          '<a class="submitdelete deletion menu-delete" href="%1$s">%2$s</a>',
1272                                          esc_url(
1273                                              wp_nonce_url(
1274                                                  add_query_arg(
1275                                                      array(
1276                                                          'action' => 'delete',
1277                                                          'menu' => $nav_menu_selected_id,
1278                                                      ),
1279                                                      admin_url( 'nav-menus.php' )
1280                                                  ),
1281                                                  'delete-nav_menu-' . $nav_menu_selected_id
1282                                              )
1283                                          ),
1284                                          __( 'Delete Menu' )
1285                                      );
1286                                      ?>
1287                                  </span><!-- END .delete-action -->
1288                                  <?php endif; ?>
1289  
1290                              <?php endif; ?>
1291                          </div><!-- END .major-publishing-actions -->
1292                      </div><!-- /#nav-menu-footer -->
1293                  </div><!-- /.menu-edit -->
1294              </form><!-- /#update-nav-menu -->
1295          </div><!-- /#menu-management -->
1296      </div><!-- /#menu-management-liquid -->
1297      </div><!-- /#nav-menus-frame -->
1298      <?php endif; ?>
1299  </div><!-- /.wrap-->
1300  <?php require_once  ABSPATH . 'wp-admin/admin-footer.php'; ?>


Generated : Thu Nov 12 08:20:02 2026 Cross-referenced by PHPXref