[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/includes/ -> nav-menu.php (source)

   1  <?php
   2  /**
   3   * Core Navigation Menu API
   4   *
   5   * @package WordPress
   6   * @subpackage Nav_Menus
   7   * @since 3.0.0
   8   */
   9  
  10  /** Walker_Nav_Menu_Edit class */
  11  require_once ABSPATH . 'wp-admin/includes/class-walker-nav-menu-edit.php';
  12  
  13  /** Walker_Nav_Menu_Checklist class */
  14  require_once ABSPATH . 'wp-admin/includes/class-walker-nav-menu-checklist.php';
  15  
  16  /**
  17   * Prints the appropriate response to a menu quick search.
  18   *
  19   * @since 3.0.0
  20   *
  21   * @param array $request The unsanitized request values.
  22   */
  23  function _wp_ajax_menu_quick_search( $request = array() ) {
  24      $args            = array();
  25      $type            = isset( $request['type'] ) ? $request['type'] : '';
  26      $object_type     = isset( $request['object_type'] ) ? $request['object_type'] : '';
  27      $query           = isset( $request['q'] ) ? $request['q'] : '';
  28      $response_format = isset( $request['response-format'] ) ? $request['response-format'] : '';
  29  
  30      if ( ! $response_format || ! in_array( $response_format, array( 'json', 'markup' ), true ) ) {
  31          $response_format = 'json';
  32      }
  33  
  34      if ( 'markup' === $response_format ) {
  35          $args['walker'] = new Walker_Nav_Menu_Checklist;
  36      }
  37  
  38      if ( 'get-post-item' === $type ) {
  39          if ( post_type_exists( $object_type ) ) {
  40              if ( isset( $request['ID'] ) ) {
  41                  $object_id = (int) $request['ID'];
  42                  if ( 'markup' === $response_format ) {
  43                      echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_post( $object_id ) ) ), 0, (object) $args );
  44                  } elseif ( 'json' === $response_format ) {
  45                      echo wp_json_encode(
  46                          array(
  47                              'ID'         => $object_id,
  48                              'post_title' => get_the_title( $object_id ),
  49                              'post_type'  => get_post_type( $object_id ),
  50                          )
  51                      );
  52                      echo "\n";
  53                  }
  54              }
  55          } elseif ( taxonomy_exists( $object_type ) ) {
  56              if ( isset( $request['ID'] ) ) {
  57                  $object_id = (int) $request['ID'];
  58                  if ( 'markup' === $response_format ) {
  59                      echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args );
  60                  } elseif ( 'json' === $response_format ) {
  61                      $post_obj = get_term( $object_id, $object_type );
  62                      echo wp_json_encode(
  63                          array(
  64                              'ID'         => $object_id,
  65                              'post_title' => $post_obj->name,
  66                              'post_type'  => $object_type,
  67                          )
  68                      );
  69                      echo "\n";
  70                  }
  71              }
  72          }
  73      } elseif ( preg_match( '/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches ) ) {
  74          if ( 'posttype' === $matches[1] && get_post_type_object( $matches[2] ) ) {
  75              $post_type_obj = _wp_nav_menu_meta_box_object( get_post_type_object( $matches[2] ) );
  76              $args          = array_merge(
  77                  $args,
  78                  array(
  79                      'no_found_rows'          => true,
  80                      'update_post_meta_cache' => false,
  81                      'update_post_term_cache' => false,
  82                      'posts_per_page'         => 10,
  83                      'post_type'              => $matches[2],
  84                      's'                      => $query,
  85                  )
  86              );
  87              if ( isset( $post_type_obj->_default_query ) ) {
  88                  $args = array_merge( $args, (array) $post_type_obj->_default_query );
  89              }
  90              $search_results_query = new WP_Query( $args );
  91              if ( ! $search_results_query->have_posts() ) {
  92                  return;
  93              }
  94              while ( $search_results_query->have_posts() ) {
  95                  $post = $search_results_query->next_post();
  96                  if ( 'markup' === $response_format ) {
  97                      $var_by_ref = $post->ID;
  98                      echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args );
  99                  } elseif ( 'json' === $response_format ) {
 100                      echo wp_json_encode(
 101                          array(
 102                              'ID'         => $post->ID,
 103                              'post_title' => get_the_title( $post->ID ),
 104                              'post_type'  => $matches[2],
 105                          )
 106                      );
 107                      echo "\n";
 108                  }
 109              }
 110          } elseif ( 'taxonomy' === $matches[1] ) {
 111              $terms = get_terms(
 112                  array(
 113                      'taxonomy'   => $matches[2],
 114                      'name__like' => $query,
 115                      'number'     => 10,
 116                      'hide_empty' => false,
 117                  )
 118              );
 119              if ( empty( $terms ) || is_wp_error( $terms ) ) {
 120                  return;
 121              }
 122              foreach ( (array) $terms as $term ) {
 123                  if ( 'markup' === $response_format ) {
 124                      echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args );
 125                  } elseif ( 'json' === $response_format ) {
 126                      echo wp_json_encode(
 127                          array(
 128                              'ID'         => $term->term_id,
 129                              'post_title' => $term->name,
 130                              'post_type'  => $matches[2],
 131                          )
 132                      );
 133                      echo "\n";
 134                  }
 135              }
 136          }
 137      }
 138  }
 139  
 140  /**
 141   * Register nav menu meta boxes and advanced menu items.
 142   *
 143   * @since 3.0.0
 144   */
 145  function wp_nav_menu_setup() {
 146      // Register meta boxes.
 147      wp_nav_menu_post_type_meta_boxes();
 148      add_meta_box( 'add-custom-links', __( 'Custom Links' ), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' );
 149      wp_nav_menu_taxonomy_meta_boxes();
 150  
 151      // Register advanced menu items (columns).
 152      add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns' );
 153  
 154      // If first time editing, disable advanced items by default.
 155      if ( false === get_user_option( 'managenav-menuscolumnshidden' ) ) {
 156          $user = wp_get_current_user();
 157          update_user_option(
 158              $user->ID,
 159              'managenav-menuscolumnshidden',
 160              array(
 161                  0 => 'link-target',
 162                  1 => 'css-classes',
 163                  2 => 'xfn',
 164                  3 => 'description',
 165                  4 => 'title-attribute',
 166              ),
 167              true
 168          );
 169      }
 170  }
 171  
 172  /**
 173   * Limit the amount of meta boxes to pages, posts, links, and categories for first time users.
 174   *
 175   * @since 3.0.0
 176   *
 177   * @global array $wp_meta_boxes
 178   */
 179  function wp_initial_nav_menu_meta_boxes() {
 180      global $wp_meta_boxes;
 181  
 182      if ( get_user_option( 'metaboxhidden_nav-menus' ) !== false || ! is_array( $wp_meta_boxes ) ) {
 183          return;
 184      }
 185  
 186      $initial_meta_boxes = array( 'add-post-type-page', 'add-post-type-post', 'add-custom-links', 'add-category' );
 187      $hidden_meta_boxes  = array();
 188  
 189      foreach ( array_keys( $wp_meta_boxes['nav-menus'] ) as $context ) {
 190          foreach ( array_keys( $wp_meta_boxes['nav-menus'][ $context ] ) as $priority ) {
 191              foreach ( $wp_meta_boxes['nav-menus'][ $context ][ $priority ] as $box ) {
 192                  if ( in_array( $box['id'], $initial_meta_boxes, true ) ) {
 193                      unset( $box['id'] );
 194                  } else {
 195                      $hidden_meta_boxes[] = $box['id'];
 196                  }
 197              }
 198          }
 199      }
 200  
 201      $user = wp_get_current_user();
 202      update_user_option( $user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true );
 203  }
 204  
 205  /**
 206   * Creates meta boxes for any post type menu item..
 207   *
 208   * @since 3.0.0
 209   */
 210  function wp_nav_menu_post_type_meta_boxes() {
 211      $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
 212  
 213      if ( ! $post_types ) {
 214          return;
 215      }
 216  
 217      foreach ( $post_types as $post_type ) {
 218          /**
 219           * Filters whether a menu items meta box will be added for the current
 220           * object type.
 221           *
 222           * If a falsey value is returned instead of an object, the menu items
 223           * meta box for the current meta box object will not be added.
 224           *
 225           * @since 3.0.0
 226           *
 227           * @param WP_Post_Type|false $post_type The current object to add a menu items
 228           *                                      meta box for.
 229           */
 230          $post_type = apply_filters( 'nav_menu_meta_box_object', $post_type );
 231          if ( $post_type ) {
 232              $id = $post_type->name;
 233              // Give pages a higher priority.
 234              $priority = ( 'page' === $post_type->name ? 'core' : 'default' );
 235              add_meta_box( "add-post-type-{$id}", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', $priority, $post_type );
 236          }
 237      }
 238  }
 239  
 240  /**
 241   * Creates meta boxes for any taxonomy menu item.
 242   *
 243   * @since 3.0.0
 244   */
 245  function wp_nav_menu_taxonomy_meta_boxes() {
 246      $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
 247  
 248      if ( ! $taxonomies ) {
 249          return;
 250      }
 251  
 252      foreach ( $taxonomies as $tax ) {
 253          /** This filter is documented in wp-admin/includes/nav-menu.php */
 254          $tax = apply_filters( 'nav_menu_meta_box_object', $tax );
 255          if ( $tax ) {
 256              $id = $tax->name;
 257              add_meta_box( "add-{$id}", $tax->labels->name, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax );
 258          }
 259      }
 260  }
 261  
 262  /**
 263   * Check whether to disable the Menu Locations meta box submit button and inputs.
 264   *
 265   * @since 3.6.0
 266   * @since 5.3.1 The `$echo` parameter was added.
 267   *
 268   * @global bool $one_theme_location_no_menus to determine if no menus exist
 269   *
 270   * @param int|string $nav_menu_selected_id ID, name, or slug of the currently selected menu.
 271   * @param bool       $echo                 Whether to echo or just return the string.
 272   * @return string|false Disabled attribute if at least one menu exists, false if not.
 273   */
 274  function wp_nav_menu_disabled_check( $nav_menu_selected_id, $echo = true ) {
 275      global $one_theme_location_no_menus;
 276  
 277      if ( $one_theme_location_no_menus ) {
 278          return false;
 279      }
 280  
 281      return disabled( $nav_menu_selected_id, 0, $echo );
 282  }
 283  
 284  /**
 285   * Displays a meta box for the custom links menu item.
 286   *
 287   * @since 3.0.0
 288   *
 289   * @global int        $_nav_menu_placeholder
 290   * @global int|string $nav_menu_selected_id
 291   */
 292  function wp_nav_menu_item_link_meta_box() {
 293      global $_nav_menu_placeholder, $nav_menu_selected_id;
 294  
 295      $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
 296  
 297      ?>
 298      <div class="customlinkdiv" id="customlinkdiv">
 299          <input type="hidden" value="custom" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" />
 300          <p id="menu-item-url-wrap" class="wp-clearfix">
 301              <label class="howto" for="custom-menu-item-url"><?php _e( 'URL' ); ?></label>
 302              <input id="custom-menu-item-url" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-url]" type="text"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="code menu-item-textbox form-required" placeholder="https://" />
 303          </p>
 304  
 305          <p id="menu-item-name-wrap" class="wp-clearfix">
 306              <label class="howto" for="custom-menu-item-name"><?php _e( 'Link Text' ); ?></label>
 307              <input id="custom-menu-item-name" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-title]" type="text"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="regular-text menu-item-textbox" />
 308          </p>
 309  
 310          <p class="button-controls wp-clearfix">
 311              <span class="add-to-menu">
 312                  <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-custom-menu-item" id="submit-customlinkdiv" />
 313                  <span class="spinner"></span>
 314              </span>
 315          </p>
 316  
 317      </div><!-- /.customlinkdiv -->
 318      <?php
 319  }
 320  
 321  /**
 322   * Displays a meta box for a post type menu item.
 323   *
 324   * @since 3.0.0
 325   *
 326   * @global int        $_nav_menu_placeholder
 327   * @global int|string $nav_menu_selected_id
 328   *
 329   * @param string $object Not used.
 330   * @param array  $box {
 331   *     Post type menu item meta box arguments.
 332   *
 333   *     @type string       $id       Meta box 'id' attribute.
 334   *     @type string       $title    Meta box title.
 335   *     @type callable     $callback Meta box display callback.
 336   *     @type WP_Post_Type $args     Extra meta box arguments (the post type object for this meta box).
 337   * }
 338   */
 339  function wp_nav_menu_item_post_type_meta_box( $object, $box ) {
 340      global $_nav_menu_placeholder, $nav_menu_selected_id;
 341  
 342      $post_type_name = $box['args']->name;
 343      $post_type      = get_post_type_object( $post_type_name );
 344      $tab_name       = $post_type_name . '-tab';
 345  
 346      // Paginate browsing for large numbers of post objects.
 347      $per_page = 50;
 348      $pagenum  = isset( $_REQUEST[ $tab_name ] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
 349      $offset   = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
 350  
 351      $args = array(
 352          'offset'                 => $offset,
 353          'order'                  => 'ASC',
 354          'orderby'                => 'title',
 355          'posts_per_page'         => $per_page,
 356          'post_type'              => $post_type_name,
 357          'suppress_filters'       => true,
 358          'update_post_term_cache' => false,
 359          'update_post_meta_cache' => false,
 360      );
 361  
 362      if ( isset( $box['args']->_default_query ) ) {
 363          $args = array_merge( $args, (array) $box['args']->_default_query );
 364      }
 365  
 366      /*
 367       * If we're dealing with pages, let's prioritize the Front Page,
 368       * Posts Page and Privacy Policy Page at the top of the list.
 369       */
 370      $important_pages = array();
 371      if ( 'page' === $post_type_name ) {
 372          $suppress_page_ids = array();
 373  
 374          // Insert Front Page or custom Home link.
 375          $front_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0;
 376  
 377          $front_page_obj = null;
 378          if ( ! empty( $front_page ) ) {
 379              $front_page_obj                = get_post( $front_page );
 380              $front_page_obj->front_or_home = true;
 381  
 382              $important_pages[]   = $front_page_obj;
 383              $suppress_page_ids[] = $front_page_obj->ID;
 384          } else {
 385              $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? (int) $_nav_menu_placeholder - 1 : -1;
 386              $front_page_obj        = (object) array(
 387                  'front_or_home' => true,
 388                  'ID'            => 0,
 389                  'object_id'     => $_nav_menu_placeholder,
 390                  'post_content'  => '',
 391                  'post_excerpt'  => '',
 392                  'post_parent'   => '',
 393                  'post_title'    => _x( 'Home', 'nav menu home label' ),
 394                  'post_type'     => 'nav_menu_item',
 395                  'type'          => 'custom',
 396                  'url'           => home_url( '/' ),
 397              );
 398  
 399              $important_pages[] = $front_page_obj;
 400          }
 401  
 402          // Insert Posts Page.
 403          $posts_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_for_posts' ) : 0;
 404  
 405          if ( ! empty( $posts_page ) ) {
 406              $posts_page_obj             = get_post( $posts_page );
 407              $posts_page_obj->posts_page = true;
 408  
 409              $important_pages[]   = $posts_page_obj;
 410              $suppress_page_ids[] = $posts_page_obj->ID;
 411          }
 412  
 413          // Insert Privacy Policy Page.
 414          $privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
 415  
 416          if ( ! empty( $privacy_policy_page_id ) ) {
 417              $privacy_policy_page = get_post( $privacy_policy_page_id );
 418              if ( $privacy_policy_page instanceof WP_Post && 'publish' === $privacy_policy_page->post_status ) {
 419                  $privacy_policy_page->privacy_policy_page = true;
 420  
 421                  $important_pages[]   = $privacy_policy_page;
 422                  $suppress_page_ids[] = $privacy_policy_page->ID;
 423              }
 424          }
 425  
 426          // Add suppression array to arguments for WP_Query.
 427          if ( ! empty( $suppress_page_ids ) ) {
 428              $args['post__not_in'] = $suppress_page_ids;
 429          }
 430      }
 431  
 432      // @todo Transient caching of these results with proper invalidation on updating of a post of this type.
 433      $get_posts = new WP_Query;
 434      $posts     = $get_posts->query( $args );
 435  
 436      // Only suppress and insert when more than just suppression pages available.
 437      if ( ! $get_posts->post_count ) {
 438          if ( ! empty( $suppress_page_ids ) ) {
 439              unset( $args['post__not_in'] );
 440              $get_posts = new WP_Query;
 441              $posts     = $get_posts->query( $args );
 442          } else {
 443              echo '<p>' . __( 'No items.' ) . '</p>';
 444              return;
 445          }
 446      } elseif ( ! empty( $important_pages ) ) {
 447          $posts = array_merge( $important_pages, $posts );
 448      }
 449  
 450      $num_pages = $get_posts->max_num_pages;
 451  
 452      $page_links = paginate_links(
 453          array(
 454              'base'               => add_query_arg(
 455                  array(
 456                      $tab_name     => 'all',
 457                      'paged'       => '%#%',
 458                      'item-type'   => 'post_type',
 459                      'item-object' => $post_type_name,
 460                  )
 461              ),
 462              'format'             => '',
 463              'prev_text'          => '<span aria-label="' . esc_attr__( 'Previous page' ) . '">' . __( '&laquo;' ) . '</span>',
 464              'next_text'          => '<span aria-label="' . esc_attr__( 'Next page' ) . '">' . __( '&raquo;' ) . '</span>',
 465              'before_page_number' => '<span class="screen-reader-text">' . __( 'Page' ) . '</span> ',
 466              'total'              => $num_pages,
 467              'current'            => $pagenum,
 468          )
 469      );
 470  
 471      $db_fields = false;
 472      if ( is_post_type_hierarchical( $post_type_name ) ) {
 473          $db_fields = array(
 474              'parent' => 'post_parent',
 475              'id'     => 'ID',
 476          );
 477      }
 478  
 479      $walker = new Walker_Nav_Menu_Checklist( $db_fields );
 480  
 481      $current_tab = 'most-recent';
 482  
 483      if ( isset( $_REQUEST[ $tab_name ] ) && in_array( $_REQUEST[ $tab_name ], array( 'all', 'search' ), true ) ) {
 484          $current_tab = $_REQUEST[ $tab_name ];
 485      }
 486  
 487      if ( ! empty( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] ) ) {
 488          $current_tab = 'search';
 489      }
 490  
 491      $removed_args = array(
 492          'action',
 493          'customlink-tab',
 494          'edit-menu-item',
 495          'menu-item',
 496          'page-tab',
 497          '_wpnonce',
 498      );
 499  
 500      $most_recent_url = '';
 501      $view_all_url    = '';
 502      $search_url      = '';
 503      if ( $nav_menu_selected_id ) {
 504          $most_recent_url = esc_url( add_query_arg( $tab_name, 'most-recent', remove_query_arg( $removed_args ) ) );
 505          $view_all_url    = esc_url( add_query_arg( $tab_name, 'all', remove_query_arg( $removed_args ) ) );
 506          $search_url      = esc_url( add_query_arg( $tab_name, 'search', remove_query_arg( $removed_args ) ) );
 507      }
 508      ?>
 509      <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv">
 510          <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs">
 511              <li <?php echo ( 'most-recent' === $current_tab ? ' class="tabs"' : '' ); ?>>
 512                  <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-most-recent" href="<?php echo $most_recent_url; ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent">
 513                      <?php _e( 'Most Recent' ); ?>
 514                  </a>
 515              </li>
 516              <li <?php echo ( 'all' === $current_tab ? ' class="tabs"' : '' ); ?>>
 517                  <a class="nav-tab-link" data-type="<?php echo esc_attr( $post_type_name ); ?>-all" href="<?php echo $view_all_url; ?>#<?php echo $post_type_name; ?>-all">
 518                      <?php _e( 'View All' ); ?>
 519                  </a>
 520              </li>
 521              <li <?php echo ( 'search' === $current_tab ? ' class="tabs"' : '' ); ?>>
 522                  <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-search" href="<?php echo $search_url; ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-search">
 523                      <?php _e( 'Search' ); ?>
 524                  </a>
 525              </li>
 526          </ul><!-- .posttype-tabs -->
 527  
 528          <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php echo ( 'most-recent' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" role="region" aria-label="<?php _e( 'Most Recent' ); ?>" tabindex="0">
 529              <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear">
 530                  <?php
 531                  $recent_args    = array_merge(
 532                      $args,
 533                      array(
 534                          'orderby'        => 'post_date',
 535                          'order'          => 'DESC',
 536                          'posts_per_page' => 15,
 537                      )
 538                  );
 539                  $most_recent    = $get_posts->query( $recent_args );
 540                  $args['walker'] = $walker;
 541  
 542                  /**
 543                   * Filters the posts displayed in the 'Most Recent' tab of the current
 544                   * post type's menu items meta box.
 545                   *
 546                   * The dynamic portion of the hook name, `$post_type_name`, refers to the post type name.
 547                   *
 548                   * Possible hook names include:
 549                   *
 550                   *  - `nav_menu_items_post_recent`
 551                   *  - `nav_menu_items_page_recent`
 552                   *
 553                   * @since 4.3.0
 554                   * @since 4.9.0 Added the `$recent_args` parameter.
 555                   *
 556                   * @param WP_Post[] $most_recent An array of post objects being listed.
 557                   * @param array     $args        An array of `WP_Query` arguments for the meta box.
 558                   * @param array     $box         Arguments passed to `wp_nav_menu_item_post_type_meta_box()`.
 559                   * @param array     $recent_args An array of `WP_Query` arguments for 'Most Recent' tab.
 560                   */
 561                  $most_recent = apply_filters( "nav_menu_items_{$post_type_name}_recent", $most_recent, $args, $box, $recent_args );
 562  
 563                  echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $most_recent ), 0, (object) $args );
 564                  ?>
 565              </ul>
 566          </div><!-- /.tabs-panel -->
 567  
 568          <div class="tabs-panel <?php echo ( 'search' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search" role="region" aria-label="<?php echo $post_type->labels->search_items; ?>" tabindex="0">
 569              <?php
 570              if ( isset( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] ) ) {
 571                  $searched       = esc_attr( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] );
 572                  $search_results = get_posts(
 573                      array(
 574                          's'         => $searched,
 575                          'post_type' => $post_type_name,
 576                          'fields'    => 'all',
 577                          'order'     => 'DESC',
 578                      )
 579                  );
 580              } else {
 581                  $searched       = '';
 582                  $search_results = array();
 583              }
 584              ?>
 585              <p class="quick-search-wrap">
 586                  <label for="quick-search-posttype-<?php echo $post_type_name; ?>" class="screen-reader-text"><?php _e( 'Search' ); ?></label>
 587                  <input type="search"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="quick-search" value="<?php echo $searched; ?>" name="quick-search-posttype-<?php echo $post_type_name; ?>" id="quick-search-posttype-<?php echo $post_type_name; ?>" />
 588                  <span class="spinner"></span>
 589                  <?php submit_button( __( 'Search' ), 'small quick-search-submit hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-posttype-' . $post_type_name ) ); ?>
 590              </p>
 591  
 592              <ul id="<?php echo $post_type_name; ?>-search-checklist" data-wp-lists="list:<?php echo $post_type_name; ?>" class="categorychecklist form-no-clear">
 593              <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
 594                  <?php
 595                  $args['walker'] = $walker;
 596                  echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $search_results ), 0, (object) $args );
 597                  ?>
 598              <?php elseif ( is_wp_error( $search_results ) ) : ?>
 599                  <li><?php echo $search_results->get_error_message(); ?></li>
 600              <?php elseif ( ! empty( $searched ) ) : ?>
 601                  <li><?php _e( 'No results found.' ); ?></li>
 602              <?php endif; ?>
 603              </ul>
 604          </div><!-- /.tabs-panel -->
 605  
 606          <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php echo ( 'all' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" role="region" aria-label="<?php echo $post_type->labels->all_items; ?>" tabindex="0">
 607              <?php if ( ! empty( $page_links ) ) : ?>
 608                  <div class="add-menu-item-pagelinks">
 609                      <?php echo $page_links; ?>
 610                  </div>
 611              <?php endif; ?>
 612              <ul id="<?php echo $post_type_name; ?>checklist" data-wp-lists="list:<?php echo $post_type_name; ?>" class="categorychecklist form-no-clear">
 613                  <?php
 614                  $args['walker'] = $walker;
 615  
 616                  if ( $post_type->has_archive ) {
 617                      $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? (int) $_nav_menu_placeholder - 1 : -1;
 618                      array_unshift(
 619                          $posts,
 620                          (object) array(
 621                              'ID'           => 0,
 622                              'object_id'    => $_nav_menu_placeholder,
 623                              'object'       => $post_type_name,
 624                              'post_content' => '',
 625                              'post_excerpt' => '',
 626                              'post_title'   => $post_type->labels->archives,
 627                              'post_type'    => 'nav_menu_item',
 628                              'type'         => 'post_type_archive',
 629                              'url'          => get_post_type_archive_link( $post_type_name ),
 630                          )
 631                      );
 632                  }
 633  
 634                  /**
 635                   * Filters the posts displayed in the 'View All' tab of the current
 636                   * post type's menu items meta box.
 637                   *
 638                   * The dynamic portion of the hook name, `$post_type_name`, refers
 639                   * to the slug of the current post type.
 640                   *
 641                   * Possible hook names include:
 642                   *
 643                   *  - `nav_menu_items_post`
 644                   *  - `nav_menu_items_page`
 645                   *
 646                   * @since 3.2.0
 647                   * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
 648                   *
 649                   * @see WP_Query::query()
 650                   *
 651                   * @param object[]     $posts     The posts for the current post type. Mostly `WP_Post` objects, but
 652                   *                                can also contain "fake" post objects to represent other menu items.
 653                   * @param array        $args      An array of `WP_Query` arguments.
 654                   * @param WP_Post_Type $post_type The current post type object for this menu item meta box.
 655                   */
 656                  $posts = apply_filters( "nav_menu_items_{$post_type_name}", $posts, $args, $post_type );
 657  
 658                  $checkbox_items = walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $posts ), 0, (object) $args );
 659  
 660                  echo $checkbox_items;
 661                  ?>
 662              </ul>
 663              <?php if ( ! empty( $page_links ) ) : ?>
 664                  <div class="add-menu-item-pagelinks">
 665                      <?php echo $page_links; ?>
 666                  </div>
 667              <?php endif; ?>
 668          </div><!-- /.tabs-panel -->
 669  
 670          <p class="button-controls wp-clearfix" data-items-type="posttype-<?php echo esc_attr( $post_type_name ); ?>">
 671              <span class="list-controls hide-if-no-js">
 672                  <input type="checkbox"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> id="<?php echo esc_attr( $tab_name ); ?>" class="select-all" />
 673                  <label for="<?php echo esc_attr( $tab_name ); ?>"><?php _e( 'Select All' ); ?></label>
 674              </span>
 675  
 676              <span class="add-to-menu">
 677                  <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-post-type-menu-item" id="<?php echo esc_attr( 'submit-posttype-' . $post_type_name ); ?>" />
 678                  <span class="spinner"></span>
 679              </span>
 680          </p>
 681  
 682      </div><!-- /.posttypediv -->
 683      <?php
 684  }
 685  
 686  /**
 687   * Displays a meta box for a taxonomy menu item.
 688   *
 689   * @since 3.0.0
 690   *
 691   * @global int|string $nav_menu_selected_id
 692   *
 693   * @param string $object Not used.
 694   * @param array  $box {
 695   *     Taxonomy menu item meta box arguments.
 696   *
 697   *     @type string   $id       Meta box 'id' attribute.
 698   *     @type string   $title    Meta box title.
 699   *     @type callable $callback Meta box display callback.
 700   *     @type object   $args     Extra meta box arguments (the taxonomy object for this meta box).
 701   * }
 702   */
 703  function wp_nav_menu_item_taxonomy_meta_box( $object, $box ) {
 704      global $nav_menu_selected_id;
 705  
 706      $taxonomy_name = $box['args']->name;
 707      $taxonomy      = get_taxonomy( $taxonomy_name );
 708      $tab_name      = $taxonomy_name . '-tab';
 709  
 710      // Paginate browsing for large numbers of objects.
 711      $per_page = 50;
 712      $pagenum  = isset( $_REQUEST[ $tab_name ] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
 713      $offset   = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
 714  
 715      $args = array(
 716          'taxonomy'     => $taxonomy_name,
 717          'child_of'     => 0,
 718          'exclude'      => '',
 719          'hide_empty'   => false,
 720          'hierarchical' => 1,
 721          'include'      => '',
 722          'number'       => $per_page,
 723          'offset'       => $offset,
 724          'order'        => 'ASC',
 725          'orderby'      => 'name',
 726          'pad_counts'   => false,
 727      );
 728  
 729      $terms = get_terms( $args );
 730  
 731      if ( ! $terms || is_wp_error( $terms ) ) {
 732          echo '<p>' . __( 'No items.' ) . '</p>';
 733          return;
 734      }
 735  
 736      $num_pages = ceil(
 737          wp_count_terms(
 738              array_merge(
 739                  $args,
 740                  array(
 741                      'number' => '',
 742                      'offset' => '',
 743                  )
 744              )
 745          ) / $per_page
 746      );
 747  
 748      $page_links = paginate_links(
 749          array(
 750              'base'               => add_query_arg(
 751                  array(
 752                      $tab_name     => 'all',
 753                      'paged'       => '%#%',
 754                      'item-type'   => 'taxonomy',
 755                      'item-object' => $taxonomy_name,
 756                  )
 757              ),
 758              'format'             => '',
 759              'prev_text'          => '<span aria-label="' . esc_attr__( 'Previous page' ) . '">' . __( '&laquo;' ) . '</span>',
 760              'next_text'          => '<span aria-label="' . esc_attr__( 'Next page' ) . '">' . __( '&raquo;' ) . '</span>',
 761              'before_page_number' => '<span class="screen-reader-text">' . __( 'Page' ) . '</span> ',
 762              'total'              => $num_pages,
 763              'current'            => $pagenum,
 764          )
 765      );
 766  
 767      $db_fields = false;
 768      if ( is_taxonomy_hierarchical( $taxonomy_name ) ) {
 769          $db_fields = array(
 770              'parent' => 'parent',
 771              'id'     => 'term_id',
 772          );
 773      }
 774  
 775      $walker = new Walker_Nav_Menu_Checklist( $db_fields );
 776  
 777      $current_tab = 'most-used';
 778  
 779      if ( isset( $_REQUEST[ $tab_name ] ) && in_array( $_REQUEST[ $tab_name ], array( 'all', 'most-used', 'search' ), true ) ) {
 780          $current_tab = $_REQUEST[ $tab_name ];
 781      }
 782  
 783      if ( ! empty( $_REQUEST[ 'quick-search-taxonomy-' . $taxonomy_name ] ) ) {
 784          $current_tab = 'search';
 785      }
 786  
 787      $removed_args = array(
 788          'action',
 789          'customlink-tab',
 790          'edit-menu-item',
 791          'menu-item',
 792          'page-tab',
 793          '_wpnonce',
 794      );
 795  
 796      $most_used_url = '';
 797      $view_all_url  = '';
 798      $search_url    = '';
 799      if ( $nav_menu_selected_id ) {
 800          $most_used_url = esc_url( add_query_arg( $tab_name, 'most-used', remove_query_arg( $removed_args ) ) );
 801          $view_all_url  = esc_url( add_query_arg( $tab_name, 'all', remove_query_arg( $removed_args ) ) );
 802          $search_url    = esc_url( add_query_arg( $tab_name, 'search', remove_query_arg( $removed_args ) ) );
 803      }
 804      ?>
 805      <div id="taxonomy-<?php echo $taxonomy_name; ?>" class="taxonomydiv">
 806          <ul id="taxonomy-<?php echo $taxonomy_name; ?>-tabs" class="taxonomy-tabs add-menu-item-tabs">
 807              <li <?php echo ( 'most-used' === $current_tab ? ' class="tabs"' : '' ); ?>>
 808                  <a class="nav-tab-link" data-type="tabs-panel-<?php echo esc_attr( $taxonomy_name ); ?>-pop" href="<?php echo $most_used_url; ?>#tabs-panel-<?php echo $taxonomy_name; ?>-pop">
 809                      <?php echo esc_html( $taxonomy->labels->most_used ); ?>
 810                  </a>
 811              </li>
 812              <li <?php echo ( 'all' === $current_tab ? ' class="tabs"' : '' ); ?>>
 813                  <a class="nav-tab-link" data-type="tabs-panel-<?php echo esc_attr( $taxonomy_name ); ?>-all" href="<?php echo $view_all_url; ?>#tabs-panel-<?php echo $taxonomy_name; ?>-all">
 814                      <?php _e( 'View All' ); ?>
 815                  </a>
 816              </li>
 817              <li <?php echo ( 'search' === $current_tab ? ' class="tabs"' : '' ); ?>>
 818                  <a class="nav-tab-link" data-type="tabs-panel-search-taxonomy-<?php echo esc_attr( $taxonomy_name ); ?>" href="<?php echo $search_url; ?>#tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>">
 819                      <?php _e( 'Search' ); ?>
 820                  </a>
 821              </li>
 822          </ul><!-- .taxonomy-tabs -->
 823  
 824          <div id="tabs-panel-<?php echo $taxonomy_name; ?>-pop" class="tabs-panel <?php echo ( 'most-used' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" role="region" aria-label="<?php echo $taxonomy->labels->most_used; ?>" tabindex="0">
 825              <ul id="<?php echo $taxonomy_name; ?>checklist-pop" class="categorychecklist form-no-clear" >
 826                  <?php
 827                  $popular_terms  = get_terms(
 828                      array(
 829                          'taxonomy'     => $taxonomy_name,
 830                          'orderby'      => 'count',
 831                          'order'        => 'DESC',
 832                          'number'       => 10,
 833                          'hierarchical' => false,
 834                      )
 835                  );
 836                  $args['walker'] = $walker;
 837                  echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $popular_terms ), 0, (object) $args );
 838                  ?>
 839              </ul>
 840          </div><!-- /.tabs-panel -->
 841  
 842          <div id="tabs-panel-<?php echo $taxonomy_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php echo ( 'all' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" role="region" aria-label="<?php echo $taxonomy->labels->all_items; ?>" tabindex="0">
 843              <?php if ( ! empty( $page_links ) ) : ?>
 844                  <div class="add-menu-item-pagelinks">
 845                      <?php echo $page_links; ?>
 846                  </div>
 847              <?php endif; ?>
 848              <ul id="<?php echo $taxonomy_name; ?>checklist" data-wp-lists="list:<?php echo $taxonomy_name; ?>" class="categorychecklist form-no-clear">
 849                  <?php
 850                  $args['walker'] = $walker;
 851                  echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $terms ), 0, (object) $args );
 852                  ?>
 853              </ul>
 854              <?php if ( ! empty( $page_links ) ) : ?>
 855                  <div class="add-menu-item-pagelinks">
 856                      <?php echo $page_links; ?>
 857                  </div>
 858              <?php endif; ?>
 859          </div><!-- /.tabs-panel -->
 860  
 861          <div class="tabs-panel <?php echo ( 'search' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" id="tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>" role="region" aria-label="<?php echo $taxonomy->labels->search_items; ?>" tabindex="0">
 862              <?php
 863              if ( isset( $_REQUEST[ 'quick-search-taxonomy-' . $taxonomy_name ] ) ) {
 864                  $searched       = esc_attr( $_REQUEST[ 'quick-search-taxonomy-' . $taxonomy_name ] );
 865                  $search_results = get_terms(
 866                      array(
 867                          'taxonomy'     => $taxonomy_name,
 868                          'name__like'   => $searched,
 869                          'fields'       => 'all',
 870                          'orderby'      => 'count',
 871                          'order'        => 'DESC',
 872                          'hierarchical' => false,
 873                      )
 874                  );
 875              } else {
 876                  $searched       = '';
 877                  $search_results = array();
 878              }
 879              ?>
 880              <p class="quick-search-wrap">
 881                  <label for="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" class="screen-reader-text"><?php _e( 'Search' ); ?></label>
 882                  <input type="search" class="quick-search" value="<?php echo $searched; ?>" name="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" id="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" />
 883                  <span class="spinner"></span>
 884                  <?php submit_button( __( 'Search' ), 'small quick-search-submit hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-taxonomy-' . $taxonomy_name ) ); ?>
 885              </p>
 886  
 887              <ul id="<?php echo $taxonomy_name; ?>-search-checklist" data-wp-lists="list:<?php echo $taxonomy_name; ?>" class="categorychecklist form-no-clear">
 888              <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
 889                  <?php
 890                  $args['walker'] = $walker;
 891                  echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $search_results ), 0, (object) $args );
 892                  ?>
 893              <?php elseif ( is_wp_error( $search_results ) ) : ?>
 894                  <li><?php echo $search_results->get_error_message(); ?></li>
 895              <?php elseif ( ! empty( $searched ) ) : ?>
 896                  <li><?php _e( 'No results found.' ); ?></li>
 897              <?php endif; ?>
 898              </ul>
 899          </div><!-- /.tabs-panel -->
 900  
 901          <p class="button-controls wp-clearfix" data-items-type="taxonomy-<?php echo esc_attr( $taxonomy_name ); ?>">
 902              <span class="list-controls hide-if-no-js">
 903                  <input type="checkbox"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> id="<?php echo esc_attr( $tab_name ); ?>" class="select-all" />
 904                  <label for="<?php echo esc_attr( $tab_name ); ?>"><?php _e( 'Select All' ); ?></label>
 905              </span>
 906  
 907              <span class="add-to-menu">
 908                  <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-taxonomy-menu-item" id="<?php echo esc_attr( 'submit-taxonomy-' . $taxonomy_name ); ?>" />
 909                  <span class="spinner"></span>
 910              </span>
 911          </p>
 912  
 913      </div><!-- /.taxonomydiv -->
 914      <?php
 915  }
 916  
 917  /**
 918   * Save posted nav menu item data.
 919   *
 920   * @since 3.0.0
 921   *
 922   * @param int     $menu_id   The menu ID for which to save this item. Value of 0 makes a draft, orphaned menu item. Default 0.
 923   * @param array[] $menu_data The unsanitized POSTed menu item data.
 924   * @return int[] The database IDs of the items saved
 925   */
 926  function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
 927      $menu_id     = (int) $menu_id;
 928      $items_saved = array();
 929  
 930      if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {
 931  
 932          // Loop through all the menu items' POST values.
 933          foreach ( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
 934              if (
 935                  // Checkbox is not checked.
 936                  empty( $_item_object_data['menu-item-object-id'] ) &&
 937                  (
 938                      // And item type either isn't set.
 939                      ! isset( $_item_object_data['menu-item-type'] ) ||
 940                      // Or URL is the default.
 941                      in_array( $_item_object_data['menu-item-url'], array( 'https://', 'http://', '' ), true ) ||
 942                      // Or it's not a custom menu item (but not the custom home page).
 943                      ! ( 'custom' === $_item_object_data['menu-item-type'] && ! isset( $_item_object_data['menu-item-db-id'] ) ) ||
 944                      // Or it *is* a custom menu item that already exists.
 945                      ! empty( $_item_object_data['menu-item-db-id'] )
 946                  )
 947              ) {
 948                  // Then this potential menu item is not getting added to this menu.
 949                  continue;
 950              }
 951  
 952              // If this possible menu item doesn't actually have a menu database ID yet.
 953              if (
 954                  empty( $_item_object_data['menu-item-db-id'] ) ||
 955                  ( 0 > $_possible_db_id ) ||
 956                  $_possible_db_id != $_item_object_data['menu-item-db-id']
 957              ) {
 958                  $_actual_db_id = 0;
 959              } else {
 960                  $_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
 961              }
 962  
 963              $args = array(
 964                  'menu-item-db-id'       => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
 965                  'menu-item-object-id'   => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
 966                  'menu-item-object'      => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
 967                  'menu-item-parent-id'   => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
 968                  'menu-item-position'    => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),
 969                  'menu-item-type'        => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
 970                  'menu-item-title'       => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
 971                  'menu-item-url'         => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
 972                  'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),
 973                  'menu-item-attr-title'  => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
 974                  'menu-item-target'      => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),
 975                  'menu-item-classes'     => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),
 976                  'menu-item-xfn'         => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
 977              );
 978  
 979              $items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
 980  
 981          }
 982      }
 983      return $items_saved;
 984  }
 985  
 986  /**
 987   * Adds custom arguments to some of the meta box object types.
 988   *
 989   * @since 3.0.0
 990   *
 991   * @access private
 992   *
 993   * @param object $object The post type or taxonomy meta-object.
 994   * @return object The post type or taxonomy object.
 995   */
 996  function _wp_nav_menu_meta_box_object( $object = null ) {
 997      if ( isset( $object->name ) ) {
 998  
 999          if ( 'page' === $object->name ) {
1000              $object->_default_query = array(
1001                  'orderby'     => 'menu_order title',
1002                  'post_status' => 'publish',
1003              );
1004  
1005              // Posts should show only published items.
1006          } elseif ( 'post' === $object->name ) {
1007              $object->_default_query = array(
1008                  'post_status' => 'publish',
1009              );
1010  
1011              // Categories should be in reverse chronological order.
1012          } elseif ( 'category' === $object->name ) {
1013              $object->_default_query = array(
1014                  'orderby' => 'id',
1015                  'order'   => 'DESC',
1016              );
1017  
1018              // Custom post types should show only published items.
1019          } else {
1020              $object->_default_query = array(
1021                  'post_status' => 'publish',
1022              );
1023          }
1024      }
1025  
1026      return $object;
1027  }
1028  
1029  /**
1030   * Returns the menu formatted to edit.
1031   *
1032   * @since 3.0.0
1033   *
1034   * @param int $menu_id Optional. The ID of the menu to format. Default 0.
1035   * @return string|WP_Error The menu formatted to edit or error object on failure.
1036   */
1037  function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
1038      $menu = wp_get_nav_menu_object( $menu_id );
1039  
1040      // If the menu exists, get its items.
1041      if ( is_nav_menu( $menu ) ) {
1042          $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'post_status' => 'any' ) );
1043          $result     = '<div id="menu-instructions" class="post-body-plain';
1044          $result    .= ( ! empty( $menu_items ) ) ? ' menu-instructions-inactive">' : '">';
1045          $result    .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>';
1046          $result    .= '</div>';
1047  
1048          if ( empty( $menu_items ) ) {
1049              return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';
1050          }
1051  
1052          /**
1053           * Filters the Walker class used when adding nav menu items.
1054           *
1055           * @since 3.0.0
1056           *
1057           * @param string $class   The walker class to use. Default 'Walker_Nav_Menu_Edit'.
1058           * @param int    $menu_id ID of the menu being rendered.
1059           */
1060          $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id );
1061  
1062          if ( class_exists( $walker_class_name ) ) {
1063              $walker = new $walker_class_name;
1064          } else {
1065              return new WP_Error(
1066                  'menu_walker_not_exist',
1067                  sprintf(
1068                      /* translators: %s: Walker class name. */
1069                      __( 'The Walker class named %s does not exist.' ),
1070                      '<strong>' . $walker_class_name . '</strong>'
1071                  )
1072              );
1073          }
1074  
1075          $some_pending_menu_items = false;
1076          $some_invalid_menu_items = false;
1077          foreach ( (array) $menu_items as $menu_item ) {
1078              if ( isset( $menu_item->post_status ) && 'draft' === $menu_item->post_status ) {
1079                  $some_pending_menu_items = true;
1080              }
1081              if ( ! empty( $menu_item->_invalid ) ) {
1082                  $some_invalid_menu_items = true;
1083              }
1084          }
1085  
1086          if ( $some_pending_menu_items ) {
1087              $result .= '<div class="notice notice-info notice-alt inline"><p>' . __( 'Click Save Menu to make pending menu items public.' ) . '</p></div>';
1088          }
1089  
1090          if ( $some_invalid_menu_items ) {
1091              $result .= '<div class="notice notice-error notice-alt inline"><p>' . __( 'There are some invalid menu items. Please check or delete them.' ) . '</p></div>';
1092          }
1093  
1094          $result .= '<ul class="menu" id="menu-to-edit"> ';
1095          $result .= walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $menu_items ), 0, (object) array( 'walker' => $walker ) );
1096          $result .= ' </ul> ';
1097          return $result;
1098      } elseif ( is_wp_error( $menu ) ) {
1099          return $menu;
1100      }
1101  
1102  }
1103  
1104  /**
1105   * Returns the columns for the nav menus page.
1106   *
1107   * @since 3.0.0
1108   *
1109   * @return string[] Array of column titles keyed by their column name.
1110   */
1111  function wp_nav_menu_manage_columns() {
1112      return array(
1113          '_title'          => __( 'Show advanced menu properties' ),
1114          'cb'              => '<input type="checkbox" />',
1115          'link-target'     => __( 'Link Target' ),
1116          'title-attribute' => __( 'Title Attribute' ),
1117          'css-classes'     => __( 'CSS Classes' ),
1118          'xfn'             => __( 'Link Relationship (XFN)' ),
1119          'description'     => __( 'Description' ),
1120      );
1121  }
1122  
1123  /**
1124   * Deletes orphaned draft menu items
1125   *
1126   * @access private
1127   * @since 3.0.0
1128   *
1129   * @global wpdb $wpdb WordPress database abstraction object.
1130   */
1131  function _wp_delete_orphaned_draft_menu_items() {
1132      global $wpdb;
1133      $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
1134  
1135      // Delete orphaned draft menu items.
1136      $menu_items_to_delete = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS m ON p.ID = m.post_id WHERE post_type = 'nav_menu_item' AND post_status = 'draft' AND meta_key = '_menu_item_orphaned' AND meta_value < %d", $delete_timestamp ) );
1137  
1138      foreach ( (array) $menu_items_to_delete as $menu_item_id ) {
1139          wp_delete_post( $menu_item_id, true );
1140      }
1141  }
1142  
1143  /**
1144   * Saves nav menu items
1145   *
1146   * @since 3.6.0
1147   *
1148   * @param int|string $nav_menu_selected_id    ID, slug, or name of the currently-selected menu.
1149   * @param string     $nav_menu_selected_title Title of the currently-selected menu.
1150   * @return array The menu updated message
1151   */
1152  function wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ) {
1153      $unsorted_menu_items = wp_get_nav_menu_items(
1154          $nav_menu_selected_id,
1155          array(
1156              'orderby'     => 'ID',
1157              'output'      => ARRAY_A,
1158              'output_key'  => 'ID',
1159              'post_status' => 'draft,publish',
1160          )
1161      );
1162  
1163      $messages   = array();
1164      $menu_items = array();
1165  
1166      // Index menu items by DB ID.
1167      foreach ( $unsorted_menu_items as $_item ) {
1168          $menu_items[ $_item->db_id ] = $_item;
1169      }
1170  
1171      $post_fields = array(
1172          'menu-item-db-id',
1173          'menu-item-object-id',
1174          'menu-item-object',
1175          'menu-item-parent-id',
1176          'menu-item-position',
1177          'menu-item-type',
1178          'menu-item-title',
1179          'menu-item-url',
1180          'menu-item-description',
1181          'menu-item-attr-title',
1182          'menu-item-target',
1183          'menu-item-classes',
1184          'menu-item-xfn',
1185      );
1186  
1187      wp_defer_term_counting( true );
1188  
1189      // Loop through all the menu items' POST variables.
1190      if ( ! empty( $_POST['menu-item-db-id'] ) ) {
1191          foreach ( (array) $_POST['menu-item-db-id'] as $_key => $k ) {
1192  
1193              // Menu item title can't be blank.
1194              if ( ! isset( $_POST['menu-item-title'][ $_key ] ) || '' === $_POST['menu-item-title'][ $_key ] ) {
1195                  continue;
1196              }
1197  
1198              $args = array();
1199              foreach ( $post_fields as $field ) {
1200                  $args[ $field ] = isset( $_POST[ $field ][ $_key ] ) ? $_POST[ $field ][ $_key ] : '';
1201              }
1202  
1203              $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][ $_key ] != $_key ? 0 : $_key ), $args );
1204  
1205              if ( is_wp_error( $menu_item_db_id ) ) {
1206                  $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
1207              } else {
1208                  unset( $menu_items[ $menu_item_db_id ] );
1209              }
1210          }
1211      }
1212  
1213      // Remove menu items from the menu that weren't in $_POST.
1214      if ( ! empty( $menu_items ) ) {
1215          foreach ( array_keys( $menu_items ) as $menu_item_id ) {
1216              if ( is_nav_menu_item( $menu_item_id ) ) {
1217                  wp_delete_post( $menu_item_id );
1218              }
1219          }
1220      }
1221  
1222      // Store 'auto-add' pages.
1223      $auto_add        = ! empty( $_POST['auto-add-pages'] );
1224      $nav_menu_option = (array) get_option( 'nav_menu_options' );
1225  
1226      if ( ! isset( $nav_menu_option['auto_add'] ) ) {
1227          $nav_menu_option['auto_add'] = array();
1228      }
1229  
1230      if ( $auto_add ) {
1231          if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'], true ) ) {
1232              $nav_menu_option['auto_add'][] = $nav_menu_selected_id;
1233          }
1234      } else {
1235          $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'], true );
1236          if ( false !== $key ) {
1237              unset( $nav_menu_option['auto_add'][ $key ] );
1238          }
1239      }
1240  
1241      // Remove non-existent/deleted menus.
1242      $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) );
1243      update_option( 'nav_menu_options', $nav_menu_option );
1244  
1245      wp_defer_term_counting( false );
1246  
1247      /** This action is documented in wp-includes/nav-menu.php */
1248      do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
1249  
1250      $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' .
1251          sprintf(
1252              /* translators: %s: Nav menu title. */
1253              __( '%s has been updated.' ),
1254              '<strong>' . $nav_menu_selected_title . '</strong>'
1255          ) . '</p></div>';
1256  
1257      unset( $menu_items, $unsorted_menu_items );
1258  
1259      return $messages;
1260  }
1261  
1262  /**
1263   * If a JSON blob of navigation menu data is in POST data, expand it and inject
1264   * it into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134.
1265   *
1266   * @ignore
1267   * @since 4.5.3
1268   * @access private
1269   */
1270  function _wp_expand_nav_menu_post_data() {
1271      if ( ! isset( $_POST['nav-menu-data'] ) ) {
1272          return;
1273      }
1274  
1275      $data = json_decode( stripslashes( $_POST['nav-menu-data'] ) );
1276  
1277      if ( ! is_null( $data ) && $data ) {
1278          foreach ( $data as $post_input_data ) {
1279              // For input names that are arrays (e.g. `menu-item-db-id[3][4][5]`),
1280              // derive the array path keys via regex and set the value in $_POST.
1281              preg_match( '#([^\[]*)(\[(.+)\])?#', $post_input_data->name, $matches );
1282  
1283              $array_bits = array( $matches[1] );
1284  
1285              if ( isset( $matches[3] ) ) {
1286                  $array_bits = array_merge( $array_bits, explode( '][', $matches[3] ) );
1287              }
1288  
1289              $new_post_data = array();
1290  
1291              // Build the new array value from leaf to trunk.
1292              for ( $i = count( $array_bits ) - 1; $i >= 0; $i-- ) {
1293                  if ( count( $array_bits ) - 1 == $i ) {
1294                      $new_post_data[ $array_bits[ $i ] ] = wp_slash( $post_input_data->value );
1295                  } else {
1296                      $new_post_data = array( $array_bits[ $i ] => $new_post_data );
1297                  }
1298              }
1299  
1300              $_POST = array_replace_recursive( $_POST, $new_post_data );
1301          }
1302      }
1303  }


Generated: Fri May 14 01:00:04 2021 Cross-referenced by PHPXref 0.7.1