wp/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    82 
    82 
    83 	/**
    83 	/**
    84 	 * Storage of pre-setup menu item to prevent wasted calls to wp_setup_nav_menu_item().
    84 	 * Storage of pre-setup menu item to prevent wasted calls to wp_setup_nav_menu_item().
    85 	 *
    85 	 *
    86 	 * @since 4.3.0
    86 	 * @since 4.3.0
    87 	 * @var array
    87 	 * @var array|null
    88 	 */
    88 	 */
    89 	protected $value;
    89 	protected $value;
    90 
    90 
    91 	/**
    91 	/**
    92 	 * Previous (placeholder) post ID used before creating a new menu item.
    92 	 * Previous (placeholder) post ID used before creating a new menu item.
   153 	 *
   153 	 *
   154 	 * Any supplied $args override class property defaults.
   154 	 * Any supplied $args override class property defaults.
   155 	 *
   155 	 *
   156 	 * @since 4.3.0
   156 	 * @since 4.3.0
   157 	 *
   157 	 *
   158 	 * @param WP_Customize_Manager $manager Bootstrap Customizer instance.
   158 	 * @throws Exception If $id is not valid for this setting type.
   159 	 * @param string               $id      An specific ID of the setting. Can be a
   159 	 *
   160 	 *                                      theme mod or option name.
   160 	 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
       
   161 	 * @param string               $id      A specific ID of the setting.
       
   162 	 *                                      Can be a theme mod or option name.
   161 	 * @param array                $args    Optional. Setting arguments.
   163 	 * @param array                $args    Optional. Setting arguments.
   162 	 *
       
   163 	 * @throws Exception If $id is not valid for this setting type.
       
   164 	 */
   164 	 */
   165 	public function __construct( WP_Customize_Manager $manager, $id, array $args = array() ) {
   165 	public function __construct( WP_Customize_Manager $manager, $id, array $args = array() ) {
   166 		if ( empty( $manager->nav_menus ) ) {
   166 		if ( empty( $manager->nav_menus ) ) {
   167 			throw new Exception( 'Expected WP_Customize_Manager::$nav_menus to be set.' );
   167 			throw new Exception( 'Expected WP_Customize_Manager::$nav_menus to be set.' );
   168 		}
   168 		}
   209 	 * @see wp_setup_nav_menu_item()
   209 	 * @see wp_setup_nav_menu_item()
   210 	 *
   210 	 *
   211 	 * @return array|false Instance data array, or false if the item is marked for deletion.
   211 	 * @return array|false Instance data array, or false if the item is marked for deletion.
   212 	 */
   212 	 */
   213 	public function value() {
   213 	public function value() {
   214 		if ( $this->is_previewed && $this->_previewed_blog_id === get_current_blog_id() ) {
   214 		if ( $this->is_previewed && get_current_blog_id() === $this->_previewed_blog_id ) {
   215 			$undefined  = new stdClass(); // Symbol.
   215 			$undefined  = new stdClass(); // Symbol.
   216 			$post_value = $this->post_value( $undefined );
   216 			$post_value = $this->post_value( $undefined );
   217 
   217 
   218 			if ( $undefined === $post_value ) {
   218 			if ( $undefined === $post_value ) {
   219 				$value = $this->_original_value;
   219 				$value = $this->_original_value;
   272 			if ( $original_object ) {
   272 			if ( $original_object ) {
   273 				/** This filter is documented in wp-includes/post-template.php */
   273 				/** This filter is documented in wp-includes/post-template.php */
   274 				$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
   274 				$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
   275 
   275 
   276 				if ( '' === $original_title ) {
   276 				if ( '' === $original_title ) {
   277 					/* translators: %d: ID of a post */
   277 					/* translators: %d: ID of a post. */
   278 					$original_title = sprintf( __( '#%d (no title)' ), $original_object->ID );
   278 					$original_title = sprintf( __( '#%d (no title)' ), $original_object->ID );
   279 				}
   279 				}
   280 			}
   280 			}
   281 		} elseif ( 'taxonomy' === $item->type && ! empty( $item->object_id ) ) {
   281 		} elseif ( 'taxonomy' === $item->type && ! empty( $item->object_id ) ) {
   282 			$original_term_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
   282 			$original_term_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
   297 	 * Get type label.
   297 	 * Get type label.
   298 	 *
   298 	 *
   299 	 * @since 4.7.0
   299 	 * @since 4.7.0
   300 	 *
   300 	 *
   301 	 * @param object $item Nav menu item.
   301 	 * @param object $item Nav menu item.
   302 	 * @returns string The type label.
   302 	 * @return string The type label.
   303 	 */
   303 	 */
   304 	protected function get_type_label( $item ) {
   304 	protected function get_type_label( $item ) {
   305 		if ( 'post_type' === $item->type ) {
   305 		if ( 'post_type' === $item->type ) {
   306 			$object = get_post_type_object( $item->object );
   306 			$object = get_post_type_object( $item->object );
   307 			if ( $object ) {
   307 			if ( $object ) {
   475 	 * @param array     $args  An array of arguments used to retrieve menu item objects.
   475 	 * @param array     $args  An array of arguments used to retrieve menu item objects.
   476 	 * @return WP_Post[] Array of menu item objects.
   476 	 * @return WP_Post[] Array of menu item objects.
   477 	 */
   477 	 */
   478 	public function filter_wp_get_nav_menu_items( $items, $menu, $args ) {
   478 	public function filter_wp_get_nav_menu_items( $items, $menu, $args ) {
   479 		$this_item                = $this->value();
   479 		$this_item                = $this->value();
   480 		$current_nav_menu_term_id = $this_item['nav_menu_term_id'];
   480 		$current_nav_menu_term_id = null;
   481 		unset( $this_item['nav_menu_term_id'] );
   481 		if ( isset( $this_item['nav_menu_term_id'] ) ) {
       
   482 			$current_nav_menu_term_id = $this_item['nav_menu_term_id'];
       
   483 			unset( $this_item['nav_menu_term_id'] );
       
   484 		}
   482 
   485 
   483 		$should_filter = (
   486 		$should_filter = (
   484 			$menu->term_id === $this->original_nav_menu_term_id
   487 			$menu->term_id === $this->original_nav_menu_term_id
   485 			||
   488 			||
   486 			$menu->term_id === $current_nav_menu_term_id
   489 			$menu->term_id === $current_nav_menu_term_id
   491 
   494 
   492 		// Handle deleted menu item, or menu item moved to another menu.
   495 		// Handle deleted menu item, or menu item moved to another menu.
   493 		$should_remove = (
   496 		$should_remove = (
   494 			false === $this_item
   497 			false === $this_item
   495 			||
   498 			||
   496 			true === $this_item['_invalid']
   499 			( isset( $this_item['_invalid'] ) && true === $this_item['_invalid'] )
   497 			||
   500 			||
   498 			(
   501 			(
   499 				$this->original_nav_menu_term_id === $menu->term_id
   502 				$this->original_nav_menu_term_id === $menu->term_id
   500 				&&
   503 				&&
   501 				$current_nav_menu_term_id !== $this->original_nav_menu_term_id
   504 				$current_nav_menu_term_id !== $this->original_nav_menu_term_id
   793 					$this->update_status = 'error';
   796 					$this->update_status = 'error';
   794 					$this->update_error  = new WP_Error( 'nav_menu_setting_failure' );
   797 					$this->update_error  = new WP_Error( 'nav_menu_setting_failure' );
   795 					return;
   798 					return;
   796 				}
   799 				}
   797 
   800 
   798 				if ( $nav_menu_setting->previous_term_id !== intval( $value['nav_menu_term_id'] ) ) {
   801 				if ( intval( $value['nav_menu_term_id'] ) !== $nav_menu_setting->previous_term_id ) {
   799 					$this->update_status = 'error';
   802 					$this->update_status = 'error';
   800 					$this->update_error  = new WP_Error( 'unexpected_previous_term_id' );
   803 					$this->update_error  = new WP_Error( 'unexpected_previous_term_id' );
   801 					return;
   804 					return;
   802 				}
   805 				}
   803 
   806 
   819 					$this->update_status = 'error';
   822 					$this->update_status = 'error';
   820 					$this->update_error  = new WP_Error( 'nav_menu_item_setting_failure' );
   823 					$this->update_error  = new WP_Error( 'nav_menu_item_setting_failure' );
   821 					return;
   824 					return;
   822 				}
   825 				}
   823 
   826 
   824 				if ( $parent_nav_menu_item_setting->previous_post_id !== intval( $value['menu_item_parent'] ) ) {
   827 				if ( intval( $value['menu_item_parent'] ) !== $parent_nav_menu_item_setting->previous_post_id ) {
   825 					$this->update_status = 'error';
   828 					$this->update_status = 'error';
   826 					$this->update_error  = new WP_Error( 'unexpected_previous_post_id' );
   829 					$this->update_error  = new WP_Error( 'unexpected_previous_post_id' );
   827 					return;
   830 					return;
   828 				}
   831 				}
   829 
   832