[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Navigation Menu functions 4 * 5 * @package WordPress 6 * @subpackage Nav_Menus 7 * @since 3.0.0 8 */ 9 10 /** 11 * Returns a navigation menu object. 12 * 13 * @since 3.0.0 14 * 15 * @param int|string|WP_Term $menu Menu ID, slug, name, or object. 16 * @return WP_Term|false Menu object on success, false if $menu param isn't supplied or term does not exist. 17 */ 18 function wp_get_nav_menu_object( $menu ) { 19 $menu_obj = false; 20 21 if ( is_object( $menu ) ) { 22 $menu_obj = $menu; 23 } 24 25 if ( $menu && ! $menu_obj ) { 26 $menu_obj = get_term( $menu, 'nav_menu' ); 27 28 if ( ! $menu_obj ) { 29 $menu_obj = get_term_by( 'slug', $menu, 'nav_menu' ); 30 } 31 32 if ( ! $menu_obj ) { 33 $menu_obj = get_term_by( 'name', $menu, 'nav_menu' ); 34 } 35 } 36 37 if ( ! $menu_obj || is_wp_error( $menu_obj ) ) { 38 $menu_obj = false; 39 } 40 41 /** 42 * Filters the nav_menu term retrieved for wp_get_nav_menu_object(). 43 * 44 * @since 4.3.0 45 * 46 * @param WP_Term|false $menu_obj Term from nav_menu taxonomy, or false if nothing had been found. 47 * @param int|string|WP_Term $menu The menu ID, slug, name, or object passed to wp_get_nav_menu_object(). 48 */ 49 return apply_filters( 'wp_get_nav_menu_object', $menu_obj, $menu ); 50 } 51 52 /** 53 * Check if the given ID is a navigation menu. 54 * 55 * Returns true if it is; false otherwise. 56 * 57 * @since 3.0.0 58 * 59 * @param int|string|WP_Term $menu Menu ID, slug, name, or object of menu to check. 60 * @return bool Whether the menu exists. 61 */ 62 function is_nav_menu( $menu ) { 63 if ( ! $menu ) { 64 return false; 65 } 66 67 $menu_obj = wp_get_nav_menu_object( $menu ); 68 69 if ( 70 $menu_obj && 71 ! is_wp_error( $menu_obj ) && 72 ! empty( $menu_obj->taxonomy ) && 73 'nav_menu' === $menu_obj->taxonomy 74 ) { 75 return true; 76 } 77 78 return false; 79 } 80 81 /** 82 * Registers navigation menu locations for a theme. 83 * 84 * @since 3.0.0 85 * 86 * @global array $_wp_registered_nav_menus 87 * 88 * @param array $locations Associative array of menu location identifiers (like a slug) and descriptive text. 89 */ 90 function register_nav_menus( $locations = array() ) { 91 global $_wp_registered_nav_menus; 92 93 add_theme_support( 'menus' ); 94 95 foreach ( $locations as $key => $value ) { 96 if ( is_int( $key ) ) { 97 _doing_it_wrong( __FUNCTION__, __( 'Nav menu locations must be strings.' ), '5.3.0' ); 98 break; 99 } 100 } 101 102 $_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations ); 103 } 104 105 /** 106 * Unregisters a navigation menu location for a theme. 107 * 108 * @since 3.1.0 109 * 110 * @global array $_wp_registered_nav_menus 111 * 112 * @param string $location The menu location identifier. 113 * @return bool True on success, false on failure. 114 */ 115 function unregister_nav_menu( $location ) { 116 global $_wp_registered_nav_menus; 117 118 if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[ $location ] ) ) { 119 unset( $_wp_registered_nav_menus[ $location ] ); 120 if ( empty( $_wp_registered_nav_menus ) ) { 121 _remove_theme_support( 'menus' ); 122 } 123 return true; 124 } 125 return false; 126 } 127 128 /** 129 * Registers a navigation menu location for a theme. 130 * 131 * @since 3.0.0 132 * 133 * @param string $location Menu location identifier, like a slug. 134 * @param string $description Menu location descriptive text. 135 */ 136 function register_nav_menu( $location, $description ) { 137 register_nav_menus( array( $location => $description ) ); 138 } 139 /** 140 * Retrieves all registered navigation menu locations in a theme. 141 * 142 * @since 3.0.0 143 * 144 * @global array $_wp_registered_nav_menus 145 * 146 * @return array Registered navigation menu locations. If none are registered, an empty array. 147 */ 148 function get_registered_nav_menus() { 149 global $_wp_registered_nav_menus; 150 if ( isset( $_wp_registered_nav_menus ) ) { 151 return $_wp_registered_nav_menus; 152 } 153 return array(); 154 } 155 156 /** 157 * Retrieves all registered navigation menu locations and the menus assigned to them. 158 * 159 * @since 3.0.0 160 * 161 * @return array Registered navigation menu locations and the menus assigned them. 162 * If none are registered, an empty array. 163 */ 164 165 function get_nav_menu_locations() { 166 $locations = get_theme_mod( 'nav_menu_locations' ); 167 return ( is_array( $locations ) ) ? $locations : array(); 168 } 169 170 /** 171 * Determines whether a registered nav menu location has a menu assigned to it. 172 * 173 * @since 3.0.0 174 * 175 * @param string $location Menu location identifier. 176 * @return bool Whether location has a menu. 177 */ 178 function has_nav_menu( $location ) { 179 $has_nav_menu = false; 180 181 $registered_nav_menus = get_registered_nav_menus(); 182 if ( isset( $registered_nav_menus[ $location ] ) ) { 183 $locations = get_nav_menu_locations(); 184 $has_nav_menu = ! empty( $locations[ $location ] ); 185 } 186 187 /** 188 * Filters whether a nav menu is assigned to the specified location. 189 * 190 * @since 4.3.0 191 * 192 * @param bool $has_nav_menu Whether there is a menu assigned to a location. 193 * @param string $location Menu location. 194 */ 195 return apply_filters( 'has_nav_menu', $has_nav_menu, $location ); 196 } 197 198 /** 199 * Returns the name of a navigation menu. 200 * 201 * @since 4.9.0 202 * 203 * @param string $location Menu location identifier. 204 * @return string Menu name. 205 */ 206 function wp_get_nav_menu_name( $location ) { 207 $menu_name = ''; 208 209 $locations = get_nav_menu_locations(); 210 211 if ( isset( $locations[ $location ] ) ) { 212 $menu = wp_get_nav_menu_object( $locations[ $location ] ); 213 214 if ( $menu && $menu->name ) { 215 $menu_name = $menu->name; 216 } 217 } 218 219 /** 220 * Filters the navigation menu name being returned. 221 * 222 * @since 4.9.0 223 * 224 * @param string $menu_name Menu name. 225 * @param string $location Menu location identifier. 226 */ 227 return apply_filters( 'wp_get_nav_menu_name', $menu_name, $location ); 228 } 229 230 /** 231 * Determines whether the given ID is a nav menu item. 232 * 233 * @since 3.0.0 234 * 235 * @param int $menu_item_id The ID of the potential nav menu item. 236 * @return bool Whether the given ID is that of a nav menu item. 237 */ 238 function is_nav_menu_item( $menu_item_id = 0 ) { 239 return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' === get_post_type( $menu_item_id ) ) ); 240 } 241 242 /** 243 * Creates a navigation menu. 244 * 245 * Note that `$menu_name` is expected to be pre-slashed. 246 * 247 * @since 3.0.0 248 * 249 * @param string $menu_name Menu name. 250 * @return int|WP_Error Menu ID on success, WP_Error object on failure. 251 */ 252 function wp_create_nav_menu( $menu_name ) { 253 // expected_slashed ($menu_name) 254 return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) ); 255 } 256 257 /** 258 * Delete a Navigation Menu. 259 * 260 * @since 3.0.0 261 * 262 * @param int|string|WP_Term $menu Menu ID, slug, name, or object. 263 * @return bool|WP_Error True on success, false or WP_Error object on failure. 264 */ 265 function wp_delete_nav_menu( $menu ) { 266 $menu = wp_get_nav_menu_object( $menu ); 267 if ( ! $menu ) { 268 return false; 269 } 270 271 $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' ); 272 if ( ! empty( $menu_objects ) ) { 273 foreach ( $menu_objects as $item ) { 274 wp_delete_post( $item ); 275 } 276 } 277 278 $result = wp_delete_term( $menu->term_id, 'nav_menu' ); 279 280 // Remove this menu from any locations. 281 $locations = get_nav_menu_locations(); 282 foreach ( $locations as $location => $menu_id ) { 283 if ( $menu_id == $menu->term_id ) { 284 $locations[ $location ] = 0; 285 } 286 } 287 set_theme_mod( 'nav_menu_locations', $locations ); 288 289 if ( $result && ! is_wp_error( $result ) ) { 290 291 /** 292 * Fires after a navigation menu has been successfully deleted. 293 * 294 * @since 3.0.0 295 * 296 * @param int $term_id ID of the deleted menu. 297 */ 298 do_action( 'wp_delete_nav_menu', $menu->term_id ); 299 } 300 301 return $result; 302 } 303 304 /** 305 * Save the properties of a menu or create a new menu with those properties. 306 * 307 * Note that `$menu_data` is expected to be pre-slashed. 308 * 309 * @since 3.0.0 310 * 311 * @param int $menu_id The ID of the menu or "0" to create a new menu. 312 * @param array $menu_data The array of menu data. 313 * @return int|WP_Error Menu ID on success, WP_Error object on failure. 314 */ 315 function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) { 316 // expected_slashed ($menu_data) 317 $menu_id = (int) $menu_id; 318 319 $_menu = wp_get_nav_menu_object( $menu_id ); 320 321 $args = array( 322 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ), 323 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ), 324 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ), 325 'slug' => null, 326 ); 327 328 // Double-check that we're not going to have one menu take the name of another. 329 $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); 330 if ( 331 $_possible_existing && 332 ! is_wp_error( $_possible_existing ) && 333 isset( $_possible_existing->term_id ) && 334 $_possible_existing->term_id != $menu_id 335 ) { 336 return new WP_Error( 337 'menu_exists', 338 sprintf( 339 /* translators: %s: Menu name. */ 340 __( 'The menu name %s conflicts with another menu name. Please try another.' ), 341 '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>' 342 ) 343 ); 344 } 345 346 // Menu doesn't already exist, so create a new menu. 347 if ( ! $_menu || is_wp_error( $_menu ) ) { 348 $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); 349 350 if ( $menu_exists ) { 351 return new WP_Error( 352 'menu_exists', 353 sprintf( 354 /* translators: %s: Menu name. */ 355 __( 'The menu name %s conflicts with another menu name. Please try another.' ), 356 '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>' 357 ) 358 ); 359 } 360 361 $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args ); 362 363 if ( is_wp_error( $_menu ) ) { 364 return $_menu; 365 } 366 367 /** 368 * Fires after a navigation menu is successfully created. 369 * 370 * @since 3.0.0 371 * 372 * @param int $term_id ID of the new menu. 373 * @param array $menu_data An array of menu data. 374 */ 375 do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data ); 376 377 return (int) $_menu['term_id']; 378 } 379 380 if ( ! $_menu || ! isset( $_menu->term_id ) ) { 381 return 0; 382 } 383 384 $menu_id = (int) $_menu->term_id; 385 386 $update_response = wp_update_term( $menu_id, 'nav_menu', $args ); 387 388 if ( is_wp_error( $update_response ) ) { 389 return $update_response; 390 } 391 392 $menu_id = (int) $update_response['term_id']; 393 394 /** 395 * Fires after a navigation menu has been successfully updated. 396 * 397 * @since 3.0.0 398 * 399 * @param int $menu_id ID of the updated menu. 400 * @param array $menu_data An array of menu data. 401 */ 402 do_action( 'wp_update_nav_menu', $menu_id, $menu_data ); 403 return $menu_id; 404 } 405 406 /** 407 * Save the properties of a menu item or create a new one. 408 * 409 * The menu-item-title, menu-item-description, and menu-item-attr-title are expected 410 * to be pre-slashed since they are passed directly into `wp_insert_post()`. 411 * 412 * @since 3.0.0 413 * 414 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan. 415 * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item. 416 * @param array $menu_item_data The menu item's data. 417 * @return int|WP_Error The menu item's database ID or WP_Error object on failure. 418 */ 419 function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) { 420 $menu_id = (int) $menu_id; 421 $menu_item_db_id = (int) $menu_item_db_id; 422 423 // Make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects. 424 if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) ) { 425 return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) ); 426 } 427 428 $menu = wp_get_nav_menu_object( $menu_id ); 429 430 if ( ! $menu && 0 !== $menu_id ) { 431 return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) ); 432 } 433 434 if ( is_wp_error( $menu ) ) { 435 return $menu; 436 } 437 438 $defaults = array( 439 'menu-item-db-id' => $menu_item_db_id, 440 'menu-item-object-id' => 0, 441 'menu-item-object' => '', 442 'menu-item-parent-id' => 0, 443 'menu-item-position' => 0, 444 'menu-item-type' => 'custom', 445 'menu-item-title' => '', 446 'menu-item-url' => '', 447 'menu-item-description' => '', 448 'menu-item-attr-title' => '', 449 'menu-item-target' => '', 450 'menu-item-classes' => '', 451 'menu-item-xfn' => '', 452 'menu-item-status' => '', 453 'menu-item-post-date' => '', 454 'menu-item-post-date-gmt' => '', 455 ); 456 457 $args = wp_parse_args( $menu_item_data, $defaults ); 458 459 if ( 0 == $menu_id ) { 460 $args['menu-item-position'] = 1; 461 } elseif ( 0 == (int) $args['menu-item-position'] ) { 462 $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) ); 463 $last_item = array_pop( $menu_items ); 464 $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items ); 465 } 466 467 $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0; 468 469 if ( 'custom' === $args['menu-item-type'] ) { 470 // If custom menu item, trim the URL. 471 $args['menu-item-url'] = trim( $args['menu-item-url'] ); 472 } else { 473 /* 474 * If non-custom menu item, then: 475 * - use the original object's URL. 476 * - blank default title to sync with the original object's title. 477 */ 478 479 $args['menu-item-url'] = ''; 480 481 $original_title = ''; 482 if ( 'taxonomy' === $args['menu-item-type'] ) { 483 $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' ); 484 $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' ); 485 } elseif ( 'post_type' === $args['menu-item-type'] ) { 486 487 $original_object = get_post( $args['menu-item-object-id'] ); 488 $original_parent = (int) $original_object->post_parent; 489 $original_title = $original_object->post_title; 490 } elseif ( 'post_type_archive' === $args['menu-item-type'] ) { 491 $original_object = get_post_type_object( $args['menu-item-object'] ); 492 if ( $original_object ) { 493 $original_title = $original_object->labels->archives; 494 } 495 } 496 497 if ( wp_unslash( $args['menu-item-title'] ) === wp_specialchars_decode( $original_title ) ) { 498 $args['menu-item-title'] = ''; 499 } 500 501 // Hack to get wp to create a post object when too many properties are empty. 502 if ( '' === $args['menu-item-title'] && '' === $args['menu-item-description'] ) { 503 $args['menu-item-description'] = ' '; 504 } 505 } 506 507 // Populate the menu item object. 508 $post = array( 509 'menu_order' => $args['menu-item-position'], 510 'ping_status' => 0, 511 'post_content' => $args['menu-item-description'], 512 'post_excerpt' => $args['menu-item-attr-title'], 513 'post_parent' => $original_parent, 514 'post_title' => $args['menu-item-title'], 515 'post_type' => 'nav_menu_item', 516 ); 517 518 $post_date = wp_resolve_post_date( $args['menu-item-post-date'], $args['menu-item-post-date-gmt'] ); 519 if ( $post_date ) { 520 $post['post_date'] = $post_date; 521 } 522 523 $update = 0 != $menu_item_db_id; 524 525 // New menu item. Default is draft status. 526 if ( ! $update ) { 527 $post['ID'] = 0; 528 $post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft'; 529 $menu_item_db_id = wp_insert_post( $post ); 530 if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) { 531 return $menu_item_db_id; 532 } 533 534 /** 535 * Fires immediately after a new navigation menu item has been added. 536 * 537 * @since 4.4.0 538 * 539 * @see wp_update_nav_menu_item() 540 * 541 * @param int $menu_id ID of the updated menu. 542 * @param int $menu_item_db_id ID of the new menu item. 543 * @param array $args An array of arguments used to update/add the menu item. 544 */ 545 do_action( 'wp_add_nav_menu_item', $menu_id, $menu_item_db_id, $args ); 546 } 547 548 // Associate the menu item with the menu term. 549 // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms(). 550 if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) { 551 wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' ); 552 } 553 554 if ( 'custom' === $args['menu-item-type'] ) { 555 $args['menu-item-object-id'] = $menu_item_db_id; 556 $args['menu-item-object'] = 'custom'; 557 } 558 559 $menu_item_db_id = (int) $menu_item_db_id; 560 561 update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key( $args['menu-item-type'] ) ); 562 update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', (string) ( (int) $args['menu-item-parent-id'] ) ); 563 update_post_meta( $menu_item_db_id, '_menu_item_object_id', (string) ( (int) $args['menu-item-object-id'] ) ); 564 update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key( $args['menu-item-object'] ) ); 565 update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key( $args['menu-item-target'] ) ); 566 567 $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) ); 568 $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) ); 569 update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] ); 570 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] ); 571 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw( $args['menu-item-url'] ) ); 572 573 if ( 0 == $menu_id ) { 574 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() ); 575 } elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) ) { 576 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' ); 577 } 578 579 // Update existing menu item. Default is publish status. 580 if ( $update ) { 581 $post['ID'] = $menu_item_db_id; 582 $post['post_status'] = ( 'draft' === $args['menu-item-status'] ) ? 'draft' : 'publish'; 583 wp_update_post( $post ); 584 } 585 586 /** 587 * Fires after a navigation menu item has been updated. 588 * 589 * @since 3.0.0 590 * 591 * @see wp_update_nav_menu_item() 592 * 593 * @param int $menu_id ID of the updated menu. 594 * @param int $menu_item_db_id ID of the updated menu item. 595 * @param array $args An array of arguments used to update a menu item. 596 */ 597 do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args ); 598 599 return $menu_item_db_id; 600 } 601 602 /** 603 * Returns all navigation menu objects. 604 * 605 * @since 3.0.0 606 * @since 4.1.0 Default value of the 'orderby' argument was changed from 'none' 607 * to 'name'. 608 * 609 * @param array $args Optional. Array of arguments passed on to get_terms(). 610 * Default empty array. 611 * @return WP_Term[] An array of menu objects. 612 */ 613 function wp_get_nav_menus( $args = array() ) { 614 $defaults = array( 615 'taxonomy' => 'nav_menu', 616 'hide_empty' => false, 617 'orderby' => 'name', 618 ); 619 $args = wp_parse_args( $args, $defaults ); 620 621 /** 622 * Filters the navigation menu objects being returned. 623 * 624 * @since 3.0.0 625 * 626 * @see get_terms() 627 * 628 * @param WP_Term[] $menus An array of menu objects. 629 * @param array $args An array of arguments used to retrieve menu objects. 630 */ 631 return apply_filters( 'wp_get_nav_menus', get_terms( $args ), $args ); 632 } 633 634 /** 635 * Return if a menu item is valid. 636 * 637 * @link https://core.trac.wordpress.org/ticket/13958 638 * 639 * @since 3.2.0 640 * @access private 641 * 642 * @param object $item The menu item to check. 643 * @return bool False if invalid, otherwise true. 644 */ 645 function _is_valid_nav_menu_item( $item ) { 646 return empty( $item->_invalid ); 647 } 648 649 /** 650 * Retrieves all menu items of a navigation menu. 651 * 652 * Note: Most arguments passed to the `$args` parameter – save for 'output_key' – are 653 * specifically for retrieving nav_menu_item posts from get_posts() and may only 654 * indirectly affect the ultimate ordering and content of the resulting nav menu 655 * items that get returned from this function. 656 * 657 * @since 3.0.0 658 * 659 * @param int|string|WP_Term $menu Menu ID, slug, name, or object. 660 * @param array $args { 661 * Optional. Arguments to pass to get_posts(). 662 * 663 * @type string $order How to order nav menu items as queried with get_posts(). Will be ignored 664 * if 'output' is ARRAY_A. Default 'ASC'. 665 * @type string $orderby Field to order menu items by as retrieved from get_posts(). Supply an orderby 666 * field via 'output_key' to affect the output order of nav menu items. 667 * Default 'menu_order'. 668 * @type string $post_type Menu items post type. Default 'nav_menu_item'. 669 * @type string $post_status Menu items post status. Default 'publish'. 670 * @type string $output How to order outputted menu items. Default ARRAY_A. 671 * @type string $output_key Key to use for ordering the actual menu items that get returned. Note that 672 * that is not a get_posts() argument and will only affect output of menu items 673 * processed in this function. Default 'menu_order'. 674 * @type bool $nopaging Whether to retrieve all menu items (true) or paginate (false). Default true. 675 * } 676 * @return array|false Array of menu items, otherwise false. 677 */ 678 function wp_get_nav_menu_items( $menu, $args = array() ) { 679 $menu = wp_get_nav_menu_object( $menu ); 680 681 if ( ! $menu ) { 682 return false; 683 } 684 685 static $fetched = array(); 686 687 $items = get_objects_in_term( $menu->term_id, 'nav_menu' ); 688 if ( is_wp_error( $items ) ) { 689 return false; 690 } 691 692 $defaults = array( 693 'order' => 'ASC', 694 'orderby' => 'menu_order', 695 'post_type' => 'nav_menu_item', 696 'post_status' => 'publish', 697 'output' => ARRAY_A, 698 'output_key' => 'menu_order', 699 'nopaging' => true, 700 ); 701 $args = wp_parse_args( $args, $defaults ); 702 $args['include'] = $items; 703 704 if ( ! empty( $items ) ) { 705 $items = get_posts( $args ); 706 } else { 707 $items = array(); 708 } 709 710 // Get all posts and terms at once to prime the caches. 711 if ( empty( $fetched[ $menu->term_id ] ) && ! wp_using_ext_object_cache() ) { 712 $fetched[ $menu->term_id ] = true; 713 $posts = array(); 714 $terms = array(); 715 foreach ( $items as $item ) { 716 $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true ); 717 $object = get_post_meta( $item->ID, '_menu_item_object', true ); 718 $type = get_post_meta( $item->ID, '_menu_item_type', true ); 719 720 if ( 'post_type' === $type ) { 721 $posts[ $object ][] = $object_id; 722 } elseif ( 'taxonomy' === $type ) { 723 $terms[ $object ][] = $object_id; 724 } 725 } 726 727 if ( ! empty( $posts ) ) { 728 foreach ( array_keys( $posts ) as $post_type ) { 729 get_posts( 730 array( 731 'post__in' => $posts[ $post_type ], 732 'post_type' => $post_type, 733 'nopaging' => true, 734 'update_post_term_cache' => false, 735 ) 736 ); 737 } 738 } 739 unset( $posts ); 740 741 if ( ! empty( $terms ) ) { 742 foreach ( array_keys( $terms ) as $taxonomy ) { 743 get_terms( 744 array( 745 'taxonomy' => $taxonomy, 746 'include' => $terms[ $taxonomy ], 747 'hierarchical' => false, 748 ) 749 ); 750 } 751 } 752 unset( $terms ); 753 } 754 755 $items = array_map( 'wp_setup_nav_menu_item', $items ); 756 757 if ( ! is_admin() ) { // Remove invalid items only on front end. 758 $items = array_filter( $items, '_is_valid_nav_menu_item' ); 759 } 760 761 if ( ARRAY_A == $args['output'] ) { 762 $items = wp_list_sort( 763 $items, 764 array( 765 $args['output_key'] => 'ASC', 766 ) 767 ); 768 $i = 1; 769 foreach ( $items as $k => $item ) { 770 $items[ $k ]->{$args['output_key']} = $i++; 771 } 772 } 773 774 /** 775 * Filters the navigation menu items being returned. 776 * 777 * @since 3.0.0 778 * 779 * @param array $items An array of menu item post objects. 780 * @param object $menu The menu object. 781 * @param array $args An array of arguments used to retrieve menu item objects. 782 */ 783 return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args ); 784 } 785 786 /** 787 * Decorates a menu item object with the shared navigation menu item properties. 788 * 789 * Properties: 790 * - ID: The term_id if the menu item represents a taxonomy term. 791 * - attr_title: The title attribute of the link element for this menu item. 792 * - classes: The array of class attribute values for the link element of this menu item. 793 * - db_id: The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist). 794 * - description: The description of this menu item. 795 * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise. 796 * - object: The type of object originally represented, such as 'category', 'post', or 'attachment'. 797 * - object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories. 798 * - post_parent: The DB ID of the original object's parent object, if any (0 otherwise). 799 * - post_title: A "no title" label if menu item represents a post that lacks a title. 800 * - target: The target attribute of the link element for this menu item. 801 * - title: The title of this menu item. 802 * - type: The family of objects originally represented, such as 'post_type' or 'taxonomy'. 803 * - type_label: The singular label used to describe this type of menu item. 804 * - url: The URL to which this menu item points. 805 * - xfn: The XFN relationship expressed in the link of this menu item. 806 * - _invalid: Whether the menu item represents an object that no longer exists. 807 * 808 * @since 3.0.0 809 * 810 * @param object $menu_item The menu item to modify. 811 * @return object The menu item with standard menu item properties. 812 */ 813 function wp_setup_nav_menu_item( $menu_item ) { 814 if ( isset( $menu_item->post_type ) ) { 815 if ( 'nav_menu_item' === $menu_item->post_type ) { 816 $menu_item->db_id = (int) $menu_item->ID; 817 $menu_item->menu_item_parent = ! isset( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent; 818 $menu_item->object_id = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id; 819 $menu_item->object = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object; 820 $menu_item->type = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type; 821 822 if ( 'post_type' === $menu_item->type ) { 823 $object = get_post_type_object( $menu_item->object ); 824 if ( $object ) { 825 $menu_item->type_label = $object->labels->singular_name; 826 // Denote post states for special pages (only in the admin). 827 if ( function_exists( 'get_post_states' ) ) { 828 $menu_post = get_post( $menu_item->object_id ); 829 $post_states = get_post_states( $menu_post ); 830 if ( $post_states ) { 831 $menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) ); 832 } 833 } 834 } else { 835 $menu_item->type_label = $menu_item->object; 836 $menu_item->_invalid = true; 837 } 838 839 if ( 'trash' === get_post_status( $menu_item->object_id ) ) { 840 $menu_item->_invalid = true; 841 } 842 843 $original_object = get_post( $menu_item->object_id ); 844 845 if ( $original_object ) { 846 $menu_item->url = get_permalink( $original_object->ID ); 847 /** This filter is documented in wp-includes/post-template.php */ 848 $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID ); 849 } else { 850 $menu_item->url = ''; 851 $original_title = ''; 852 $menu_item->_invalid = true; 853 } 854 855 if ( '' === $original_title ) { 856 /* translators: %d: ID of a post. */ 857 $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id ); 858 } 859 860 $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title; 861 862 } elseif ( 'post_type_archive' === $menu_item->type ) { 863 $object = get_post_type_object( $menu_item->object ); 864 if ( $object ) { 865 $menu_item->title = ( '' === $menu_item->post_title ) ? $object->labels->archives : $menu_item->post_title; 866 $post_type_description = $object->description; 867 } else { 868 $post_type_description = ''; 869 $menu_item->_invalid = true; 870 } 871 872 $menu_item->type_label = __( 'Post Type Archive' ); 873 $post_content = wp_trim_words( $menu_item->post_content, 200 ); 874 $post_type_description = ( '' === $post_content ) ? $post_type_description : $post_content; 875 $menu_item->url = get_post_type_archive_link( $menu_item->object ); 876 877 } elseif ( 'taxonomy' === $menu_item->type ) { 878 $object = get_taxonomy( $menu_item->object ); 879 if ( $object ) { 880 $menu_item->type_label = $object->labels->singular_name; 881 } else { 882 $menu_item->type_label = $menu_item->object; 883 $menu_item->_invalid = true; 884 } 885 886 $original_object = get_term( (int) $menu_item->object_id, $menu_item->object ); 887 888 if ( $original_object && ! is_wp_error( $original_object ) ) { 889 $menu_item->url = get_term_link( (int) $menu_item->object_id, $menu_item->object ); 890 $original_title = $original_object->name; 891 } else { 892 $menu_item->url = ''; 893 $original_title = ''; 894 $menu_item->_invalid = true; 895 } 896 897 if ( '' === $original_title ) { 898 /* translators: %d: ID of a term. */ 899 $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id ); 900 } 901 902 $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title; 903 904 } else { 905 $menu_item->type_label = __( 'Custom Link' ); 906 $menu_item->title = $menu_item->post_title; 907 $menu_item->url = ! isset( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url; 908 } 909 910 $menu_item->target = ! isset( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target; 911 912 /** 913 * Filters a navigation menu item's title attribute. 914 * 915 * @since 3.0.0 916 * 917 * @param string $item_title The menu item title attribute. 918 */ 919 $menu_item->attr_title = ! isset( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title; 920 921 if ( ! isset( $menu_item->description ) ) { 922 /** 923 * Filters a navigation menu item's description. 924 * 925 * @since 3.0.0 926 * 927 * @param string $description The menu item description. 928 */ 929 $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) ); 930 } 931 932 $menu_item->classes = ! isset( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes; 933 $menu_item->xfn = ! isset( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn; 934 } else { 935 $menu_item->db_id = 0; 936 $menu_item->menu_item_parent = 0; 937 $menu_item->object_id = (int) $menu_item->ID; 938 $menu_item->type = 'post_type'; 939 940 $object = get_post_type_object( $menu_item->post_type ); 941 $menu_item->object = $object->name; 942 $menu_item->type_label = $object->labels->singular_name; 943 944 if ( '' === $menu_item->post_title ) { 945 /* translators: %d: ID of a post. */ 946 $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID ); 947 } 948 949 $menu_item->title = $menu_item->post_title; 950 $menu_item->url = get_permalink( $menu_item->ID ); 951 $menu_item->target = ''; 952 953 /** This filter is documented in wp-includes/nav-menu.php */ 954 $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' ); 955 956 /** This filter is documented in wp-includes/nav-menu.php */ 957 $menu_item->description = apply_filters( 'nav_menu_description', '' ); 958 $menu_item->classes = array(); 959 $menu_item->xfn = ''; 960 } 961 } elseif ( isset( $menu_item->taxonomy ) ) { 962 $menu_item->ID = $menu_item->term_id; 963 $menu_item->db_id = 0; 964 $menu_item->menu_item_parent = 0; 965 $menu_item->object_id = (int) $menu_item->term_id; 966 $menu_item->post_parent = (int) $menu_item->parent; 967 $menu_item->type = 'taxonomy'; 968 969 $object = get_taxonomy( $menu_item->taxonomy ); 970 $menu_item->object = $object->name; 971 $menu_item->type_label = $object->labels->singular_name; 972 973 $menu_item->title = $menu_item->name; 974 $menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy ); 975 $menu_item->target = ''; 976 $menu_item->attr_title = ''; 977 $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy ); 978 $menu_item->classes = array(); 979 $menu_item->xfn = ''; 980 981 } 982 983 /** 984 * Filters a navigation menu item object. 985 * 986 * @since 3.0.0 987 * 988 * @param object $menu_item The menu item object. 989 */ 990 return apply_filters( 'wp_setup_nav_menu_item', $menu_item ); 991 } 992 993 /** 994 * Get the menu items associated with a particular object. 995 * 996 * @since 3.0.0 997 * 998 * @param int $object_id Optional. The ID of the original object. Default 0. 999 * @param string $object_type Optional. The type of object, such as 'post_type' or 'taxonomy'. 1000 * Default 'post_type'. 1001 * @param string $taxonomy Optional. If $object_type is 'taxonomy', $taxonomy is the name 1002 * of the tax that $object_id belongs to. Default empty. 1003 * @return int[] The array of menu item IDs; empty array if none. 1004 */ 1005 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) { 1006 $object_id = (int) $object_id; 1007 $menu_item_ids = array(); 1008 1009 $query = new WP_Query; 1010 $menu_items = $query->query( 1011 array( 1012 'meta_key' => '_menu_item_object_id', 1013 'meta_value' => $object_id, 1014 'post_status' => 'any', 1015 'post_type' => 'nav_menu_item', 1016 'posts_per_page' => -1, 1017 ) 1018 ); 1019 foreach ( (array) $menu_items as $menu_item ) { 1020 if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) { 1021 $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true ); 1022 if ( 1023 'post_type' === $object_type && 1024 'post_type' === $menu_item_type 1025 ) { 1026 $menu_item_ids[] = (int) $menu_item->ID; 1027 } elseif ( 1028 'taxonomy' === $object_type && 1029 'taxonomy' === $menu_item_type && 1030 get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy 1031 ) { 1032 $menu_item_ids[] = (int) $menu_item->ID; 1033 } 1034 } 1035 } 1036 1037 return array_unique( $menu_item_ids ); 1038 } 1039 1040 /** 1041 * Callback for handling a menu item when its original object is deleted. 1042 * 1043 * @since 3.0.0 1044 * @access private 1045 * 1046 * @param int $object_id The ID of the original object being trashed. 1047 */ 1048 function _wp_delete_post_menu_item( $object_id ) { 1049 $object_id = (int) $object_id; 1050 1051 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' ); 1052 1053 foreach ( (array) $menu_item_ids as $menu_item_id ) { 1054 wp_delete_post( $menu_item_id, true ); 1055 } 1056 } 1057 1058 /** 1059 * Serves as a callback for handling a menu item when its original object is deleted. 1060 * 1061 * @since 3.0.0 1062 * @access private 1063 * 1064 * @param int $object_id The ID of the original object being trashed. 1065 * @param int $tt_id Term taxonomy ID. Unused. 1066 * @param string $taxonomy Taxonomy slug. 1067 */ 1068 function _wp_delete_tax_menu_item( $object_id, $tt_id, $taxonomy ) { 1069 $object_id = (int) $object_id; 1070 1071 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy ); 1072 1073 foreach ( (array) $menu_item_ids as $menu_item_id ) { 1074 wp_delete_post( $menu_item_id, true ); 1075 } 1076 } 1077 1078 /** 1079 * Automatically add newly published page objects to menus with that as an option. 1080 * 1081 * @since 3.0.0 1082 * @access private 1083 * 1084 * @param string $new_status The new status of the post object. 1085 * @param string $old_status The old status of the post object. 1086 * @param WP_Post $post The post object being transitioned from one status to another. 1087 */ 1088 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) { 1089 if ( 'publish' !== $new_status || 'publish' === $old_status || 'page' !== $post->post_type ) { 1090 return; 1091 } 1092 if ( ! empty( $post->post_parent ) ) { 1093 return; 1094 } 1095 $auto_add = get_option( 'nav_menu_options' ); 1096 if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) ) { 1097 return; 1098 } 1099 $auto_add = $auto_add['auto_add']; 1100 if ( empty( $auto_add ) || ! is_array( $auto_add ) ) { 1101 return; 1102 } 1103 1104 $args = array( 1105 'menu-item-object-id' => $post->ID, 1106 'menu-item-object' => $post->post_type, 1107 'menu-item-type' => 'post_type', 1108 'menu-item-status' => 'publish', 1109 ); 1110 1111 foreach ( $auto_add as $menu_id ) { 1112 $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) ); 1113 if ( ! is_array( $items ) ) { 1114 continue; 1115 } 1116 foreach ( $items as $item ) { 1117 if ( $post->ID == $item->object_id ) { 1118 continue 2; 1119 } 1120 } 1121 wp_update_nav_menu_item( $menu_id, 0, $args ); 1122 } 1123 } 1124 1125 /** 1126 * Delete auto-draft posts associated with the supplied changeset. 1127 * 1128 * @since 4.8.0 1129 * @access private 1130 * 1131 * @param int $post_id Post ID for the customize_changeset. 1132 */ 1133 function _wp_delete_customize_changeset_dependent_auto_drafts( $post_id ) { 1134 $post = get_post( $post_id ); 1135 1136 if ( ! $post || 'customize_changeset' !== $post->post_type ) { 1137 return; 1138 } 1139 1140 $data = json_decode( $post->post_content, true ); 1141 if ( empty( $data['nav_menus_created_posts']['value'] ) ) { 1142 return; 1143 } 1144 remove_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' ); 1145 foreach ( $data['nav_menus_created_posts']['value'] as $stub_post_id ) { 1146 if ( empty( $stub_post_id ) ) { 1147 continue; 1148 } 1149 if ( 'auto-draft' === get_post_status( $stub_post_id ) ) { 1150 wp_delete_post( $stub_post_id, true ); 1151 } elseif ( 'draft' === get_post_status( $stub_post_id ) ) { 1152 wp_trash_post( $stub_post_id ); 1153 delete_post_meta( $stub_post_id, '_customize_changeset_uuid' ); 1154 } 1155 } 1156 add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' ); 1157 } 1158 1159 /** 1160 * Handle menu config after theme change. 1161 * 1162 * @access private 1163 * @since 4.9.0 1164 */ 1165 function _wp_menus_changed() { 1166 $old_nav_menu_locations = get_option( 'theme_switch_menu_locations', array() ); 1167 $new_nav_menu_locations = get_nav_menu_locations(); 1168 $mapped_nav_menu_locations = wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations ); 1169 1170 set_theme_mod( 'nav_menu_locations', $mapped_nav_menu_locations ); 1171 delete_option( 'theme_switch_menu_locations' ); 1172 } 1173 1174 /** 1175 * Maps nav menu locations according to assignments in previously active theme. 1176 * 1177 * @since 4.9.0 1178 * 1179 * @param array $new_nav_menu_locations New nav menu locations assignments. 1180 * @param array $old_nav_menu_locations Old nav menu locations assignments. 1181 * @return array Nav menus mapped to new nav menu locations. 1182 */ 1183 function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations ) { 1184 $registered_nav_menus = get_registered_nav_menus(); 1185 $new_nav_menu_locations = array_intersect_key( $new_nav_menu_locations, $registered_nav_menus ); 1186 1187 // Short-circuit if there are no old nav menu location assignments to map. 1188 if ( empty( $old_nav_menu_locations ) ) { 1189 return $new_nav_menu_locations; 1190 } 1191 1192 // If old and new theme have just one location, map it and we're done. 1193 if ( 1 === count( $old_nav_menu_locations ) && 1 === count( $registered_nav_menus ) ) { 1194 $new_nav_menu_locations[ key( $registered_nav_menus ) ] = array_pop( $old_nav_menu_locations ); 1195 return $new_nav_menu_locations; 1196 } 1197 1198 $old_locations = array_keys( $old_nav_menu_locations ); 1199 1200 // Map locations with the same slug. 1201 foreach ( $registered_nav_menus as $location => $name ) { 1202 if ( in_array( $location, $old_locations, true ) ) { 1203 $new_nav_menu_locations[ $location ] = $old_nav_menu_locations[ $location ]; 1204 unset( $old_nav_menu_locations[ $location ] ); 1205 } 1206 } 1207 1208 // If there are no old nav menu locations left, then we're done. 1209 if ( empty( $old_nav_menu_locations ) ) { 1210 return $new_nav_menu_locations; 1211 } 1212 1213 /* 1214 * If old and new theme both have locations that contain phrases 1215 * from within the same group, make an educated guess and map it. 1216 */ 1217 $common_slug_groups = array( 1218 array( 'primary', 'menu-1', 'main', 'header', 'navigation', 'top' ), 1219 array( 'secondary', 'menu-2', 'footer', 'subsidiary', 'bottom' ), 1220 array( 'social' ), 1221 ); 1222 1223 // Go through each group... 1224 foreach ( $common_slug_groups as $slug_group ) { 1225 1226 // ...and see if any of these slugs... 1227 foreach ( $slug_group as $slug ) { 1228 1229 // ...and any of the new menu locations... 1230 foreach ( $registered_nav_menus as $new_location => $name ) { 1231 1232 // ...actually match! 1233 if ( is_string( $new_location ) && false === stripos( $new_location, $slug ) && false === stripos( $slug, $new_location ) ) { 1234 continue; 1235 } elseif ( is_numeric( $new_location ) && $new_location !== $slug ) { 1236 continue; 1237 } 1238 1239 // Then see if any of the old locations... 1240 foreach ( $old_nav_menu_locations as $location => $menu_id ) { 1241 1242 // ...and any slug in the same group... 1243 foreach ( $slug_group as $slug ) { 1244 1245 // ... have a match as well. 1246 if ( is_string( $location ) && false === stripos( $location, $slug ) && false === stripos( $slug, $location ) ) { 1247 continue; 1248 } elseif ( is_numeric( $location ) && $location !== $slug ) { 1249 continue; 1250 } 1251 1252 // Make sure this location wasn't mapped and removed previously. 1253 if ( ! empty( $old_nav_menu_locations[ $location ] ) ) { 1254 1255 // We have a match that can be mapped! 1256 $new_nav_menu_locations[ $new_location ] = $old_nav_menu_locations[ $location ]; 1257 1258 // Remove the mapped location so it can't be mapped again. 1259 unset( $old_nav_menu_locations[ $location ] ); 1260 1261 // Go back and check the next new menu location. 1262 continue 3; 1263 } 1264 } // End foreach ( $slug_group as $slug ). 1265 } // End foreach ( $old_nav_menu_locations as $location => $menu_id ). 1266 } // End foreach foreach ( $registered_nav_menus as $new_location => $name ). 1267 } // End foreach ( $slug_group as $slug ). 1268 } // End foreach ( $common_slug_groups as $slug_group ). 1269 1270 return $new_nav_menu_locations; 1271 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 2 01:00:04 2021 | Cross-referenced by PHPXref 0.7.1 |