wpseek.com
A WordPress-centric search engine for devs and theme authors



_wp_migrate_menu_to_navigation_post › WordPress Function

Since5.9.0
Deprecatedn/a
_wp_migrate_menu_to_navigation_post ( $new_name, $new_theme, $old_theme )
Access:
  • private
Parameters: (3)
  • (string) $new_name Name of the new theme.
    Required: Yes
  • (WP_Theme) $new_theme New theme.
    Required: Yes
  • (WP_Theme) $old_theme Old theme.
    Required: Yes
Defined at:
Codex:

Migrates classic menus to a block-based navigation post on theme switch.

Assigns the created navigation post to the corresponding navigation area.


Source

function _wp_migrate_menu_to_navigation_post( $new_name, WP_Theme $new_theme, WP_Theme $old_theme ) {
	// Do nothing when switching to a theme that does not support site editor.
	if ( ! wp_is_block_template_theme() ) {
		return;
	}

	// get_nav_menu_locations() calls get_theme_mod() which depends on the stylesheet option.
	// At the same time, switch_theme runs only after the stylesheet option was updated to $new_theme.
	// To retrieve theme mods of the old theme, the getter is hooked to get_option( 'stylesheet' ) so that we
	// get the old theme, which causes the get_nav_menu_locations to get the locations of the old theme.
	$get_old_theme_stylesheet = static function() use ( $old_theme ) {
		return $old_theme->get_stylesheet();
	};
	add_filter( 'option_stylesheet', $get_old_theme_stylesheet );

	$locations    = get_nav_menu_locations();
	$area_mapping = get_option( 'wp_navigation_areas', array() );

	foreach ( $locations as $location_name => $menu_id ) {
		// Get the menu from the location, skipping if there is no
		// menu or there was an error.
		$menu = wp_get_nav_menu_object( $menu_id );
		if ( ! $menu || is_wp_error( $menu ) ) {
			continue;
		}

		$menu_items = _wp_get_menu_items_at_location( $location_name );
		if ( empty( $menu_items ) ) {
			continue;
		}

		$post_name = 'classic_menu_' . $menu_id;

		// Get or create to avoid creating too many wp_navigation posts.
		$query          = new WP_Query;
		$matching_posts = $query->query(
			array(
				'name'           => $post_name,
				'post_status'    => 'publish',
				'post_type'      => 'wp_navigation',
				'posts_per_page' => 1,
				'fields'         => 'ids',
			)
		);

		if ( ! empty( $matching_posts ) ) {
			$navigation_post_id = $matching_posts[0];
		} else {
			$menu_items_by_parent_id = _wp_sort_menu_items_by_parent_id( $menu_items );
			$parsed_blocks           = _wp_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id );
			$post_data               = array(
				'post_type'    => 'wp_navigation',
				'post_title'   => sprintf(
					/* translators: %s: the name of the menu, e.g. "Main Menu". */
					__( 'Classic menu: %s' ),
					$menu->name
				),
				'post_name'    => $post_name,
				'post_content' => serialize_blocks( $parsed_blocks ),
				'post_status'  => 'publish',
			);
			$navigation_post_id      = wp_insert_post( $post_data, true );
			// If wp_insert_post fails *at any time*, then bail out of the
			// entire migration attempt returning the WP_Error object.
			if ( is_wp_error( $navigation_post_id ) ) {
				return $navigation_post_id;
			}
		}

		$area_mapping[ $location_name ] = $navigation_post_id;
	}
	remove_filter( 'option_stylesheet', $get_old_theme_stylesheet );

	update_option( 'wp_navigation_areas', $area_mapping );
}