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


Generated: Sat Sep 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1