WP_Customize_Nav_Menu_Item_Setting::filter_wp_get_nav_menu_items

Advertisement

Summery Summery

Filters the wp_get_nav_menu_items() result to supply the previewed menu items.

Syntax Syntax

WP_Customize_Nav_Menu_Item_Setting::filter_wp_get_nav_menu_items( WP_Post[] $items, WP_Term $menu, array $args )

Parameters Parameters

$items

(Required) An array of menu item post objects.

$menu

(Required) The menu object.

$args

(Required) An array of arguments used to retrieve menu item objects.

Return Return

(WP_Post[]) Array of menu item objects.

Source Source

File: wp-includes/customize/class-wp-customize-nav-menu-item-setting.php

	public function filter_wp_get_nav_menu_items( $items, $menu, $args ) {
		$this_item                = $this->value();
		$current_nav_menu_term_id = null;
		if ( isset( $this_item['nav_menu_term_id'] ) ) {
			$current_nav_menu_term_id = $this_item['nav_menu_term_id'];
			unset( $this_item['nav_menu_term_id'] );
		}

		$should_filter = (
			$menu->term_id === $this->original_nav_menu_term_id
			||
			$menu->term_id === $current_nav_menu_term_id
		);
		if ( ! $should_filter ) {
			return $items;
		}

		// Handle deleted menu item, or menu item moved to another menu.
		$should_remove = (
			false === $this_item
			||
			( isset( $this_item['_invalid'] ) && true === $this_item['_invalid'] )
			||
			(
				$this->original_nav_menu_term_id === $menu->term_id
				&&
				$current_nav_menu_term_id !== $this->original_nav_menu_term_id
			)
		);
		if ( $should_remove ) {
			$filtered_items = array();
			foreach ( $items as $item ) {
				if ( $item->db_id !== $this->post_id ) {
					$filtered_items[] = $item;
				}
			}
			return $filtered_items;
		}

		$mutated       = false;
		$should_update = (
			is_array( $this_item )
			&&
			$current_nav_menu_term_id === $menu->term_id
		);
		if ( $should_update ) {
			foreach ( $items as $item ) {
				if ( $item->db_id === $this->post_id ) {
					foreach ( get_object_vars( $this->value_as_wp_post_nav_menu_item() ) as $key => $value ) {
						$item->$key = $value;
					}
					$mutated = true;
				}
			}

			// Not found so we have to append it..
			if ( ! $mutated ) {
				$items[] = $this->value_as_wp_post_nav_menu_item();
			}
		}

		return $items;
	}

Advertisement

Changelog Changelog

Changelog
Version Description
4.3.0 Introduced.

See also See also

Advertisement

Leave a Reply