Changes in trunk/wp-includes/nav-menu.php [15008:15235]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/nav-menu.php
r15008 r15235 17 17 * 18 18 * @param string $menu Menu id, slug or name 19 * @return mixed false if $menu param isn't supplied or term does not exist, menu object if successful l19 * @return mixed false if $menu param isn't supplied or term does not exist, menu object if successful. 20 20 */ 21 21 function wp_get_nav_menu_object( $menu ) { … … 31 31 $menu_obj = get_term_by( 'name', $menu, 'nav_menu' ); 32 32 33 if ( ! $menu_obj ) {33 if ( ! $menu_obj ) 34 34 $menu_obj = false; 35 }36 35 37 36 return $menu_obj; … … 39 38 40 39 /** 41 * Check if the given ID is a nav menu.40 * Check if the given ID is a navigation menu. 42 41 * 43 42 * Returns true if it is; false otherwise. … … 66 65 67 66 /** 68 * Register nav menus for a theme.67 * Register navigation menus for a theme. 69 68 * 70 69 * @since 3.0.0 … … 75 74 global $_wp_registered_nav_menus; 76 75 77 add_theme_support( ' nav-menus' );76 add_theme_support( 'menus' ); 78 77 79 78 $_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations ); … … 81 80 82 81 /** 83 * Register navmenu for a theme.82 * Register a navigation menu for a theme. 84 83 * 85 84 * @since 3.0.0 … … 92 91 } 93 92 /** 94 * Returns an array of all registered nav menus in a theme93 * Returns an array of all registered navigation menus in a theme 95 94 * 96 95 * @since 3.0.0 … … 105 104 106 105 /** 107 * Returns an array with the registered nav menu locations and the menu assigned to it106 * Returns an array with the registered navigation menu locations and the menu assigned to it 108 107 * 109 108 * @since 3.0.0 … … 112 111 113 112 function get_nav_menu_locations() { 114 return get_theme_mod('nav_menu_locations'); 113 return get_theme_mod( 'nav_menu_locations' ); 114 } 115 116 /** 117 * Whether a registered nav menu location has a menu assigned to it. 118 * 119 * @since 3.0.0 120 * @param string $location Menu location identifier. 121 * @return bool Whether location has a menu. 122 */ 123 function has_nav_menu( $location ) { 124 $locations = get_nav_menu_locations(); 125 return ( ! empty( $locations[ $location ] ) ); 115 126 } 116 127 … … 130 141 * Create a Navigation Menu. 131 142 * 132 * Optional args:133 * slug - the url friendly version of the nav menu.134 *135 143 * @since 3.0.0 136 144 * 137 145 * @param string $menu_name Menu Name 138 * @param string $args Optional. 139 * @return mixed Menu object on sucess|WP_Error on failure 140 */ 141 function wp_create_nav_menu( $menu_name, $args = array() ) { 142 $menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' ); 143 144 if ( $menu_exists ) 145 return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_name ) ) ); 146 147 $menu = wp_insert_term( $menu_name, 'nav_menu' ); 148 149 if ( is_wp_error($menu) ) 150 return $menu; 151 152 $result = get_term( $menu['term_id'], 'nav_menu' ); 153 154 if ( $result && !is_wp_error($result) ) { 155 do_action( 'wp_create_nav_menu', $menu['term_id'] ); 156 return $result; 157 } else { 158 return $result; 159 } 146 * @return mixed Menu object on success|WP_Error on failure 147 */ 148 function wp_create_nav_menu( $menu_name ) { 149 return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) ); 160 150 } 161 151 … … 166 156 * 167 157 * @param string $menu name|id|slug 168 * @return mixed Menu object on suc ess|WP_Error on failure158 * @return mixed Menu object on success|WP_Error on failure 169 159 */ 170 160 function wp_delete_nav_menu( $menu ) { 171 161 $menu = wp_get_nav_menu_object( $menu ); 172 if ( ! $menu 162 if ( ! $menu ) 173 163 return false; 174 164 175 165 $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' ); 176 if ( ! empty( $menu_objects ) ) {166 if ( ! empty( $menu_objects ) ) { 177 167 foreach ( $menu_objects as $item ) { 178 168 wp_delete_post( $item ); … … 182 172 $result = wp_delete_term( $menu->term_id, 'nav_menu' ); 183 173 184 if ( $result && !is_wp_error($result) ) {174 if ( $result && !is_wp_error($result) ) 185 175 do_action( 'wp_delete_nav_menu', $menu->term_id ); 186 return $result; 187 } else { 188 return $result; 189 } 176 177 return $result; 190 178 } 191 179 … … 195 183 * @since 3.0.0 196 184 * 197 * @param int $menu_id The ID of the menu 185 * @param int $menu_id The ID of the menu or "0" to create a new menu. 198 186 * @param array $menu_data The array of menu data. 199 187 * @return int|error object The menu's ID or WP_Error object. … … 204 192 $_menu = wp_get_nav_menu_object( $menu_id ); 205 193 206 // menu doesn't already exist 207 if ( ! $_menu || is_wp_error( $_menu ) ) 208 $_menu = wp_create_nav_menu( $menu_data['menu-name'] ); 209 210 if ( is_wp_error( $_menu ) ) 211 return $_menu; 212 213 if ( $_menu && isset( $_menu->term_id ) ) { 214 $args = array( 215 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ), 216 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ), 217 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ), 218 'slug' => null, 219 ); 220 221 $menu_id = (int) $_menu->term_id; 222 223 // double-check that we're not changing a menu to the name of another 224 $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); 225 if ( 226 $_possible_existing && 227 ! is_wp_error( $_possible_existing ) && 228 isset( $_possible_existing->term_id ) && 229 $_possible_existing->term_id != $menu_id 230 ) { 194 $args = array( 195 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ), 196 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ), 197 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ), 198 'slug' => null, 199 ); 200 201 // double-check that we're not going to have one menu take the name of another 202 $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); 203 if ( 204 $_possible_existing && 205 ! is_wp_error( $_possible_existing ) && 206 isset( $_possible_existing->term_id ) && 207 $_possible_existing->term_id != $menu_id 208 ) 209 return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_data['menu-name'] ) ) ); 210 211 // menu doesn't already exist, so create a new menu 212 if ( ! $_menu || is_wp_error( $_menu ) ) { 213 $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); 214 215 if ( $menu_exists ) 231 216 return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_data['menu-name'] ) ) ); 232 } 233 234 $update_response = wp_update_term( $menu_id, 'nav_menu', $args ); 235 236 if ( ! is_wp_error( $update_response ) ) 237 return $menu_id; 238 else 239 return $update_response; 240 } else { 217 218 $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args ); 219 220 if ( is_wp_error( $_menu ) ) 221 return $_menu; 222 223 do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data ); 224 225 return (int) $_menu['term_id']; 226 } 227 228 if ( ! $_menu || ! isset( $_menu->term_id ) ) 241 229 return 0; 242 } 230 231 $menu_id = (int) $_menu->term_id; 232 233 $update_response = wp_update_term( $menu_id, 'nav_menu', $args ); 234 235 if ( is_wp_error( $update_response ) ) 236 return $update_response; 237 238 do_action( 'wp_update_nav_menu', $menu_id, $menu_data ); 239 return $menu_id; 243 240 } 244 241 … … 248 245 * @since 3.0.0 249 246 * 250 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draftorphan.247 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a pending orphan. 251 248 * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item. 252 249 * @param array $menu_item_data The menu item's data. … … 266 263 return $menu; 267 264 268 $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => ' publish,draft' ) );265 $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'draft,pending,publish' ) ); 269 266 270 267 $count = count( $menu_items ); … … 288 285 289 286 $args = wp_parse_args( $menu_item_data, $defaults ); 290 287 291 288 if ( 0 == $menu_id ) { 292 289 $args['menu-item-position'] = 1; … … 316 313 $original_title = $original_object->post_title; 317 314 318 if ( 'trash' == get_post_status( $args['menu-item-object-id'] ) ) {315 if ( 'trash' == get_post_status( $args['menu-item-object-id'] ) ) 319 316 return new WP_Error('update_nav_menu_item_failed', sprintf(__('The menu item "%1$s" belongs to something that is in the trash, so it cannot be updated.'), $args['menu-item-title'] ) ); 320 }321 317 } 322 318 … … 325 321 326 322 // hack to get wp to create a post object when too many properties are empty 327 if ( empty( $args['menu-item-description'] ) ) {323 if ( empty( $args['menu-item-description'] ) ) 328 324 $args['menu-item-description'] = ' '; 329 }330 325 } 331 326 } … … 345 340 $post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_id ) ) ); 346 341 347 // New menu item. Default is draftstatus342 // New menu item. Default is pending status 348 343 if ( 0 == $menu_item_db_id ) { 349 344 $post['ID'] = 0; 350 $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : ' draft';345 $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'pending'; 351 346 $menu_item_db_id = wp_insert_post( $post ); 352 347 … … 363 358 } 364 359 365 if ( $menu_item_db_id && ! is_wp_error( $menu_item_db_id ) ) {366 367 $menu_item_db_id = (int) $menu_item_db_id; 368 369 update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) ); 370 update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', (int) $args['menu-item-parent-id']);371 update_post_meta( $menu_item_db_id, '_menu_item_object_id', (int) $args['menu-item-object-id'] );372 update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']));373 update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) );374 375 $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) ); 376 $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] )) );377 update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes']);378 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );379 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']));380 381 if ( 0 == $menu_id ) 382 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', time() );383 else384 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );385 386 do_action('wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args ); 387 }360 if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) 361 return $menu_item_db_id; 362 363 $menu_item_db_id = (int) $menu_item_db_id; 364 365 update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) ); 366 update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', (int) $args['menu-item-parent-id'] ); 367 update_post_meta( $menu_item_db_id, '_menu_item_object_id', (int) $args['menu-item-object-id'] ); 368 update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) ); 369 update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) ); 370 371 $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) ); 372 $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) ); 373 update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] ); 374 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] ); 375 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) ); 376 377 if ( 0 == $menu_id ) 378 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', time() ); 379 else 380 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' ); 381 382 do_action('wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args ); 388 383 389 384 return $menu_item_db_id; … … 401 396 $defaults = array( 'hide_empty' => false, 'orderby' => 'none' ); 402 397 $args = wp_parse_args( $args, $defaults ); 403 return get_terms( 'nav_menu', $args); 404 } 405 398 return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args), $args ); 399 } 406 400 407 401 /** … … 415 409 * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b. 416 410 */ 417 function _sort_nav_menu_items( $a, $b) {411 function _sort_nav_menu_items( $a, $b ) { 418 412 global $_menu_item_sort_prop; 419 413 420 if ( empty( $_menu_item_sort_prop ) ) {414 if ( empty( $_menu_item_sort_prop ) ) 421 415 return 0; 422 } 423 424 if ( isset( $a->$_menu_item_sort_prop ) && isset( $b->$_menu_item_sort_prop ) ) { 425 $_a = (int) $a->$_menu_item_sort_prop; 426 $_b = (int) $b->$_menu_item_sort_prop; 427 428 if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop ) { 429 return 0; 430 } elseif ( 431 ( $_a == $a->$_menu_item_sort_prop ) && 432 ( $_b == $b->$_menu_item_sort_prop ) 433 ) { 434 return $_a < $_b ? -1 : 1; 435 } else { 436 return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop ); 437 } 438 } else { 416 417 if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) ) 439 418 return 0; 440 } 419 420 $_a = (int) $a->$_menu_item_sort_prop; 421 $_b = (int) $b->$_menu_item_sort_prop; 422 423 if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop ) 424 return 0; 425 elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop ) 426 return $_a < $_b ? -1 : 1; 427 else 428 return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop ); 441 429 } 442 430 … … 465 453 return $items; 466 454 467 $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true, 468 'update_post_term_cache' => false); 455 $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 456 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true, 457 'update_post_term_cache' => false ); 469 458 $args = wp_parse_args( $args, $defaults ); 470 459 if ( count( $items ) > 1 ) … … 479 468 480 469 // Get all posts and terms at once to prime the caches 481 if ( empty( $fetched[$menu->term_id]) || $_wp_using_ext_object_cache ) {470 if ( empty( $fetched[$menu->term_id] ) || $_wp_using_ext_object_cache ) { 482 471 $fetched[$menu->term_id] = true; 483 472 $posts = array(); … … 485 474 foreach ( $items as $item ) { 486 475 $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true ); 487 $object = get_post_meta( $item->ID, '_menu_item_object',true );488 $type = get_post_meta( $item->ID, '_menu_item_type',true );476 $object = get_post_meta( $item->ID, '_menu_item_object', true ); 477 $type = get_post_meta( $item->ID, '_menu_item_type', true ); 489 478 490 479 if ( 'post_type' == $type ) … … 494 483 } 495 484 496 if ( ! empty($posts) ) {485 if ( ! empty( $posts ) ) { 497 486 foreach ( array_keys($posts) as $post_type ) { 498 487 get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) ); … … 501 490 unset($posts); 502 491 503 if ( ! empty($terms) ) {492 if ( ! empty( $terms ) ) { 504 493 foreach ( array_keys($terms) as $taxonomy ) { 505 494 get_terms($taxonomy, array('include' => $terms[$taxonomy]) ); … … 656 645 foreach( (array) $menu_items as $menu_item ) { 657 646 if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) { 658 if ( get_post_meta( $menu_item->ID, '_menu_item_type', true) != $object_type )647 if ( get_post_meta( $menu_item->ID, '_menu_item_type', true ) != $object_type ) 659 648 continue; 660 649 … … 682 671 foreach( (array) $menu_item_ids as $menu_item_id ) { 683 672 $menu_item = get_post( $menu_item_id, ARRAY_A ); 684 $menu_item['post_status'] = ' draft';673 $menu_item['post_status'] = 'pending'; 685 674 wp_insert_post($menu_item); 686 675 } … … 747 736 748 737 /** 749 * Automatically add newly published page objects to menus with that as an option.738 * Modify a navigational menu upon post object status change, if appropos. 750 739 * 751 740 * @since 3.0.0 … … 757 746 * @return void 758 747 */ 759 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) { 760 if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type ) 761 return; 762 $auto_add = get_option( 'nav_menu_options' ); 763 if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) ) 764 return; 765 $auto_add = $auto_add['auto_add']; 766 if ( empty( $auto_add ) || ! is_array( $auto_add ) ) 767 return; 768 769 $args = array( 770 'menu-item-object-id' => $post->ID, 771 'menu-item-object' => $post->post_type, 772 'menu-item-type' => 'post_type', 773 'menu-item-status' => 'publish', 774 ); 775 776 foreach ( $auto_add as $menu_id ) { 777 $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) ); 778 if ( ! is_array( $items ) ) 779 continue; 780 foreach ( $items as $item ) { 781 if ( $post->ID == $item->object_id ) 782 continue 2; 783 } 784 wp_update_nav_menu_item( $menu_id, 0, $args ); 785 } 786 } 787 add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 ); 748 function _wp_menu_changing_status_observer( $new_status, $old_status, $post ) { 749 // append new top-level page objects to a menu for which that option is selected 750 if ( 751 'publish' == $new_status && 752 'publish' != $old_status && 753 'page' == $post->post_type && 754 empty( $post->post_parent ) 755 ) { 756 $auto_add = get_option( 'nav_menu_options' ); 757 if ( 758 isset( $auto_add['auto_add'] ) && 759 is_array( $auto_add['auto_add'] ) 760 ) { 761 $args = array( 762 'menu-item-object-id' => $post->ID, 763 'menu-item-object' => $post->post_type, 764 'menu-item-type' => 'post_type', 765 'menu-item-status' => 'publish', 766 ); 767 768 foreach ( $auto_add['auto_add'] as $menu_id ) { 769 $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'draft,pending,publish' ) ); 770 if ( ! is_array( $items ) ) 771 continue; 772 foreach ( $items as $item ) { 773 if ( $post->ID == $item->object_id ) 774 continue 2; 775 } 776 wp_update_nav_menu_item( $menu_id, 0, $args ); 777 } 778 } 779 } 780 781 // give menu items draft status if their associated post objects change from "publish" to "draft", or vice versa (draft item being re-published) 782 if ( 783 ! empty( $post->ID ) && 784 ( 785 ( 'publish' == $old_status && 'draft' == $new_status ) || 786 ( 'draft' == $old_status && 'publish' == $new_status ) 787 ) 788 ) { 789 $menu_items = get_posts(array( 790 'meta_key' => '_menu_item_object_id', 791 'meta_value' => $post->ID, 792 'post_status' => 'any', 793 'post_type' => 'nav_menu_item', 794 )); 795 796 foreach( (array) $menu_items as $menu_item ) { 797 if ( ! empty( $menu_item->ID ) ) { 798 $properties = get_object_vars( $menu_item ); 799 $properties['post_status'] = $new_status; 800 801 wp_insert_post( $properties ); 802 } 803 } 804 } 805 } 788 806 789 807 ?>
Note: See TracChangeset
for help on using the changeset viewer.