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


Generated : Thu May 9 08:20:02 2024 Cross-referenced by PHPXref