wp/wp-includes/nav-menu.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    11  * Returns a navigation menu object.
    11  * Returns a navigation menu object.
    12  *
    12  *
    13  * @since 3.0.0
    13  * @since 3.0.0
    14  *
    14  *
    15  * @param int|string|WP_Term $menu Menu ID, slug, name, or object.
    15  * @param int|string|WP_Term $menu Menu ID, slug, name, or object.
    16  * @return WP_Term|false False if $menu param isn't supplied or term does not exist, menu object if successful.
    16  * @return WP_Term|false Menu object on success, false if $menu param isn't supplied or term does not exist.
    17  */
    17  */
    18 function wp_get_nav_menu_object( $menu ) {
    18 function wp_get_nav_menu_object( $menu ) {
    19 	$menu_obj = false;
    19 	$menu_obj = false;
    20 
    20 
    21 	if ( is_object( $menu ) ) {
    21 	if ( is_object( $menu ) ) {
    83  *
    83  *
    84  * @since 3.0.0
    84  * @since 3.0.0
    85  *
    85  *
    86  * @global array $_wp_registered_nav_menus
    86  * @global array $_wp_registered_nav_menus
    87  *
    87  *
    88  * @param array $locations Associative array of menu location identifiers (like a slug) and descriptive text.
    88  * @param string[] $locations Associative array of menu location identifiers (like a slug) and descriptive text.
    89  */
    89  */
    90 function register_nav_menus( $locations = array() ) {
    90 function register_nav_menus( $locations = array() ) {
    91 	global $_wp_registered_nav_menus;
    91 	global $_wp_registered_nav_menus;
    92 
    92 
    93 	add_theme_support( 'menus' );
    93 	add_theme_support( 'menus' );
   141  *
   141  *
   142  * @since 3.0.0
   142  * @since 3.0.0
   143  *
   143  *
   144  * @global array $_wp_registered_nav_menus
   144  * @global array $_wp_registered_nav_menus
   145  *
   145  *
   146  * @return array Registered navigation menu locations. If none are registered, an empty array.
   146  * @return string[] Associative array of egistered navigation menu descriptions keyed
       
   147  *                  by their location. If none are registered, an empty array.
   147  */
   148  */
   148 function get_registered_nav_menus() {
   149 function get_registered_nav_menus() {
   149 	global $_wp_registered_nav_menus;
   150 	global $_wp_registered_nav_menus;
   150 	if ( isset( $_wp_registered_nav_menus ) ) {
   151 	if ( isset( $_wp_registered_nav_menus ) ) {
   151 		return $_wp_registered_nav_menus;
   152 		return $_wp_registered_nav_menus;
   156 /**
   157 /**
   157  * Retrieves all registered navigation menu locations and the menus assigned to them.
   158  * Retrieves all registered navigation menu locations and the menus assigned to them.
   158  *
   159  *
   159  * @since 3.0.0
   160  * @since 3.0.0
   160  *
   161  *
   161  * @return array Registered navigation menu locations and the menus assigned them.
   162  * @return int[] Associative array of egistered navigation menu IDs keyed by their
   162  *               If none are registered, an empty array.
   163  *               location name. If none are registered, an empty array.
   163  */
   164  */
   164 
       
   165 function get_nav_menu_locations() {
   165 function get_nav_menu_locations() {
   166 	$locations = get_theme_mod( 'nav_menu_locations' );
   166 	$locations = get_theme_mod( 'nav_menu_locations' );
   167 	return ( is_array( $locations ) ) ? $locations : array();
   167 	return ( is_array( $locations ) ) ? $locations : array();
   168 }
   168 }
   169 
   169 
   434 	if ( is_wp_error( $menu ) ) {
   434 	if ( is_wp_error( $menu ) ) {
   435 		return $menu;
   435 		return $menu;
   436 	}
   436 	}
   437 
   437 
   438 	$defaults = array(
   438 	$defaults = array(
   439 		'menu-item-db-id'       => $menu_item_db_id,
   439 		'menu-item-db-id'         => $menu_item_db_id,
   440 		'menu-item-object-id'   => 0,
   440 		'menu-item-object-id'     => 0,
   441 		'menu-item-object'      => '',
   441 		'menu-item-object'        => '',
   442 		'menu-item-parent-id'   => 0,
   442 		'menu-item-parent-id'     => 0,
   443 		'menu-item-position'    => 0,
   443 		'menu-item-position'      => 0,
   444 		'menu-item-type'        => 'custom',
   444 		'menu-item-type'          => 'custom',
   445 		'menu-item-title'       => '',
   445 		'menu-item-title'         => '',
   446 		'menu-item-url'         => '',
   446 		'menu-item-url'           => '',
   447 		'menu-item-description' => '',
   447 		'menu-item-description'   => '',
   448 		'menu-item-attr-title'  => '',
   448 		'menu-item-attr-title'    => '',
   449 		'menu-item-target'      => '',
   449 		'menu-item-target'        => '',
   450 		'menu-item-classes'     => '',
   450 		'menu-item-classes'       => '',
   451 		'menu-item-xfn'         => '',
   451 		'menu-item-xfn'           => '',
   452 		'menu-item-status'      => '',
   452 		'menu-item-status'        => '',
       
   453 		'menu-item-post-date'     => '',
       
   454 		'menu-item-post-date-gmt' => '',
   453 	);
   455 	);
   454 
   456 
   455 	$args = wp_parse_args( $menu_item_data, $defaults );
   457 	$args = wp_parse_args( $menu_item_data, $defaults );
   456 
   458 
   457 	if ( 0 == $menu_id ) {
   459 	if ( 0 == $menu_id ) {
   510 		'post_excerpt' => $args['menu-item-attr-title'],
   512 		'post_excerpt' => $args['menu-item-attr-title'],
   511 		'post_parent'  => $original_parent,
   513 		'post_parent'  => $original_parent,
   512 		'post_title'   => $args['menu-item-title'],
   514 		'post_title'   => $args['menu-item-title'],
   513 		'post_type'    => 'nav_menu_item',
   515 		'post_type'    => 'nav_menu_item',
   514 	);
   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 	}
   515 
   522 
   516 	$update = 0 != $menu_item_db_id;
   523 	$update = 0 != $menu_item_db_id;
   517 
   524 
   518 	// New menu item. Default is draft status.
   525 	// New menu item. Default is draft status.
   519 	if ( ! $update ) {
   526 	if ( ! $update ) {
   550 	}
   557 	}
   551 
   558 
   552 	$menu_item_db_id = (int) $menu_item_db_id;
   559 	$menu_item_db_id = (int) $menu_item_db_id;
   553 
   560 
   554 	update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key( $args['menu-item-type'] ) );
   561 	update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key( $args['menu-item-type'] ) );
   555 	update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
   562 	update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', (string) ( (int) $args['menu-item-parent-id'] ) );
   556 	update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
   563 	update_post_meta( $menu_item_db_id, '_menu_item_object_id', (string) ( (int) $args['menu-item-object-id'] ) );
   557 	update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key( $args['menu-item-object'] ) );
   564 	update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key( $args['menu-item-object'] ) );
   558 	update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key( $args['menu-item-target'] ) );
   565 	update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key( $args['menu-item-target'] ) );
   559 
   566 
   560 	$args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
   567 	$args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
   561 	$args['menu-item-xfn']     = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
   568 	$args['menu-item-xfn']     = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
   749 
   756 
   750 	if ( ! is_admin() ) { // Remove invalid items only on front end.
   757 	if ( ! is_admin() ) { // Remove invalid items only on front end.
   751 		$items = array_filter( $items, '_is_valid_nav_menu_item' );
   758 		$items = array_filter( $items, '_is_valid_nav_menu_item' );
   752 	}
   759 	}
   753 
   760 
   754 	if ( ARRAY_A == $args['output'] ) {
   761 	if ( ARRAY_A === $args['output'] ) {
   755 		$items = wp_list_sort(
   762 		$items = wp_list_sort(
   756 			$items,
   763 			$items,
   757 			array(
   764 			array(
   758 				$args['output_key'] => 'ASC',
   765 				$args['output_key'] => 'ASC',
   759 			)
   766 			)
   760 		);
   767 		);
   761 		$i     = 1;
   768 
       
   769 		$i = 1;
       
   770 
   762 		foreach ( $items as $k => $item ) {
   771 		foreach ( $items as $k => $item ) {
   763 			$items[ $k ]->{$args['output_key']} = $i++;
   772 			$items[ $k ]->{$args['output_key']} = $i++;
   764 		}
   773 		}
   765 	}
   774 	}
   766 
   775 
   986 /**
   995 /**
   987  * Get the menu items associated with a particular object.
   996  * Get the menu items associated with a particular object.
   988  *
   997  *
   989  * @since 3.0.0
   998  * @since 3.0.0
   990  *
   999  *
   991  * @param int    $object_id   The ID of the original object.
  1000  * @param int    $object_id   Optional. The ID of the original object. Default 0.
   992  * @param string $object_type The type of object, such as 'taxonomy' or 'post_type'.
  1001  * @param string $object_type Optional. The type of object, such as 'post_type' or 'taxonomy'.
   993  * @param string $taxonomy    If $object_type is 'taxonomy', $taxonomy is the name of the tax
  1002  *                            Default 'post_type'.
   994  *                            that $object_id belongs to.
  1003  * @param string $taxonomy    Optional. If $object_type is 'taxonomy', $taxonomy is the name
   995  * @return int[] The array of menu item IDs; empty array if none;
  1004  *                            of the tax that $object_id belongs to. Default empty.
       
  1005  * @return int[] The array of menu item IDs; empty array if none.
   996  */
  1006  */
   997 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
  1007 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
   998 	$object_id     = (int) $object_id;
  1008 	$object_id     = (int) $object_id;
   999 	$menu_item_ids = array();
  1009 	$menu_item_ids = array();
  1000 
  1010 
  1035  * @since 3.0.0
  1045  * @since 3.0.0
  1036  * @access private
  1046  * @access private
  1037  *
  1047  *
  1038  * @param int $object_id The ID of the original object being trashed.
  1048  * @param int $object_id The ID of the original object being trashed.
  1039  */
  1049  */
  1040 function _wp_delete_post_menu_item( $object_id = 0 ) {
  1050 function _wp_delete_post_menu_item( $object_id ) {
  1041 	$object_id = (int) $object_id;
  1051 	$object_id = (int) $object_id;
  1042 
  1052 
  1043 	$menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
  1053 	$menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
  1044 
  1054 
  1045 	foreach ( (array) $menu_item_ids as $menu_item_id ) {
  1055 	foreach ( (array) $menu_item_ids as $menu_item_id ) {
  1051  * Serves as a callback for handling a menu item when its original object is deleted.
  1061  * Serves as a callback for handling a menu item when its original object is deleted.
  1052  *
  1062  *
  1053  * @since 3.0.0
  1063  * @since 3.0.0
  1054  * @access private
  1064  * @access private
  1055  *
  1065  *
  1056  * @param int    $object_id Optional. The ID of the original object being trashed. Default 0.
  1066  * @param int    $object_id The ID of the original object being trashed.
  1057  * @param int    $tt_id     Term taxonomy ID. Unused.
  1067  * @param int    $tt_id     Term taxonomy ID. Unused.
  1058  * @param string $taxonomy  Taxonomy slug.
  1068  * @param string $taxonomy  Taxonomy slug.
  1059  */
  1069  */
  1060 function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
  1070 function _wp_delete_tax_menu_item( $object_id, $tt_id, $taxonomy ) {
  1061 	$object_id = (int) $object_id;
  1071 	$object_id = (int) $object_id;
  1062 
  1072 
  1063 	$menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
  1073 	$menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
  1064 
  1074 
  1065 	foreach ( (array) $menu_item_ids as $menu_item_id ) {
  1075 	foreach ( (array) $menu_item_ids as $menu_item_id ) {