[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

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


Generated : Thu Apr 3 08:20:01 2025 Cross-referenced by PHPXref