Changeset 15078
- Timestamp:
- 05/31/2010 04:11:20 PM (15 years ago)
- Location:
- trunk/wp-includes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/default-filters.php
r14996 r15078 230 230 add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' ); 231 231 add_action( 'wp_scheduled_delete', 'wp_scheduled_delete' ); 232 233 // Navigation menu actions 232 234 add_action( 'trash_post', '_wp_trash_menu_item' ); 233 235 add_action( 'untrash_post', '_wp_untrash_menu_item' ); 234 236 add_action( 'delete_post', '_wp_delete_post_menu_item' ); 235 237 add_action( 'delete_term', '_wp_delete_tax_menu_item' ); 238 add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 ); 236 239 237 240 // Post Thumbnail CSS class filtering -
trunk/wp-includes/nav-menu.php
r15008 r15078 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 … … 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' ); 115 114 } 116 115 … … 130 129 * Create a Navigation Menu. 131 130 * 132 * Optional args:133 * slug - the url friendly version of the nav menu.134 *135 131 * @since 3.0.0 136 132 * 137 133 * @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() ) { 134 * @return mixed Menu object on success|WP_Error on failure 135 */ 136 function wp_create_nav_menu( $menu_name ) { 142 137 $menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' ); 143 138 … … 152 147 $result = get_term( $menu['term_id'], 'nav_menu' ); 153 148 154 if ( $result && !is_wp_error($result) ) {149 if ( $result && !is_wp_error($result) ) 155 150 do_action( 'wp_create_nav_menu', $menu['term_id'] ); 156 return $result; 157 } else { 158 return $result; 159 } 151 152 return $result; 160 153 } 161 154 … … 166 159 * 167 160 * @param string $menu name|id|slug 168 * @return mixed Menu object on suc ess|WP_Error on failure161 * @return mixed Menu object on success|WP_Error on failure 169 162 */ 170 163 function wp_delete_nav_menu( $menu ) { 171 164 $menu = wp_get_nav_menu_object( $menu ); 172 if ( ! $menu 165 if ( ! $menu ) 173 166 return false; 174 167 175 168 $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' ); 176 if ( ! empty( $menu_objects ) ) {169 if ( ! empty( $menu_objects ) ) { 177 170 foreach ( $menu_objects as $item ) { 178 171 wp_delete_post( $item ); … … 182 175 $result = wp_delete_term( $menu->term_id, 'nav_menu' ); 183 176 184 if ( $result && !is_wp_error($result) ) {177 if ( $result && !is_wp_error($result) ) 185 178 do_action( 'wp_delete_nav_menu', $menu->term_id ); 186 return $result; 187 } else { 188 return $result; 189 } 179 180 return $result; 190 181 } 191 182 … … 211 202 return $_menu; 212 203 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 ) { 231 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 { 204 if ( ! $_menu || ! isset( $_menu->term_id ) ) 241 205 return 0; 242 } 206 207 $args = array( 208 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ), 209 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ), 210 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ), 211 'slug' => null, 212 ); 213 214 $menu_id = (int) $_menu->term_id; 215 216 // double-check that we're not changing a menu to the name of another 217 $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); 218 if ( 219 $_possible_existing && 220 ! is_wp_error( $_possible_existing ) && 221 isset( $_possible_existing->term_id ) && 222 $_possible_existing->term_id != $menu_id 223 ) 224 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'] ) ) ); 225 226 $update_response = wp_update_term( $menu_id, 'nav_menu', $args ); 227 228 if ( ! is_wp_error( $update_response ) ) 229 return $menu_id; 230 else 231 return $update_response; 243 232 } 244 233 … … 316 305 $original_title = $original_object->post_title; 317 306 318 if ( 'trash' == get_post_status( $args['menu-item-object-id'] ) ) {307 if ( 'trash' == get_post_status( $args['menu-item-object-id'] ) ) 319 308 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 309 } 322 310 … … 325 313 326 314 // hack to get wp to create a post object when too many properties are empty 327 if ( empty( $args['menu-item-description'] ) ) {315 if ( empty( $args['menu-item-description'] ) ) 328 316 $args['menu-item-description'] = ' '; 329 }330 317 } 331 318 } … … 363 350 } 364 351 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 }352 if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) 353 return $menu_item_db_id; 354 355 $menu_item_db_id = (int) $menu_item_db_id; 356 357 update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) ); 358 update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', (int) $args['menu-item-parent-id'] ); 359 update_post_meta( $menu_item_db_id, '_menu_item_object_id', (int) $args['menu-item-object-id'] ); 360 update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) ); 361 update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) ); 362 363 $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) ); 364 $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) ); 365 update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] ); 366 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] ); 367 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) ); 368 369 if ( 0 == $menu_id ) 370 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', time() ); 371 else 372 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' ); 373 374 do_action('wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args ); 388 375 389 376 return $menu_item_db_id; … … 404 391 } 405 392 406 407 393 /** 408 394 * Sort menu items by the desired key. … … 415 401 * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b. 416 402 */ 417 function _sort_nav_menu_items( $a, $b) {403 function _sort_nav_menu_items( $a, $b ) { 418 404 global $_menu_item_sort_prop; 419 405 420 if ( empty( $_menu_item_sort_prop ) ) {406 if ( empty( $_menu_item_sort_prop ) ) 421 407 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 { 408 409 if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) ) 439 410 return 0; 440 } 411 412 $_a = (int) $a->$_menu_item_sort_prop; 413 $_b = (int) $b->$_menu_item_sort_prop; 414 415 if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop ) 416 return 0; 417 elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop ) 418 return $_a < $_b ? -1 : 1; 419 else 420 return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop ); 441 421 } 442 422 … … 465 445 return $items; 466 446 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); 447 $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 448 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true, 449 'update_post_term_cache' => false ); 469 450 $args = wp_parse_args( $args, $defaults ); 470 451 if ( count( $items ) > 1 ) … … 479 460 480 461 // Get all posts and terms at once to prime the caches 481 if ( empty( $fetched[$menu->term_id]) || $_wp_using_ext_object_cache ) {462 if ( empty( $fetched[$menu->term_id] ) || $_wp_using_ext_object_cache ) { 482 463 $fetched[$menu->term_id] = true; 483 464 $posts = array(); … … 485 466 foreach ( $items as $item ) { 486 467 $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 );468 $object = get_post_meta( $item->ID, '_menu_item_object', true ); 469 $type = get_post_meta( $item->ID, '_menu_item_type', true ); 489 470 490 471 if ( 'post_type' == $type ) … … 494 475 } 495 476 496 if ( ! empty($posts) ) {477 if ( ! empty( $posts ) ) { 497 478 foreach ( array_keys($posts) as $post_type ) { 498 479 get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) ); … … 501 482 unset($posts); 502 483 503 if ( ! empty($terms) ) {484 if ( ! empty( $terms ) ) { 504 485 foreach ( array_keys($terms) as $taxonomy ) { 505 486 get_terms($taxonomy, array('include' => $terms[$taxonomy]) ); … … 656 637 foreach( (array) $menu_items as $menu_item ) { 657 638 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 )639 if ( get_post_meta( $menu_item->ID, '_menu_item_type', true ) != $object_type ) 659 640 continue; 660 641 … … 785 766 } 786 767 } 787 add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 );788 768 789 769 ?>
Note: See TracChangeset
for help on using the changeset viewer.