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


Generated: Mon Mar 30 01:00:03 2020 Cross-referenced by PHPXref 0.7.1