Automattic\WooCommerce\Internal\Admin\Orders

PostsRedirectionController::maybe_update_menu_itemsprivateWC 10.3.0

Rewrites legacy post type menu items to point to the HPOS orders page when the main WooCommerce menu is not visible.

Method of the class: PostsRedirectionController{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->maybe_update_menu_items(): void;

Changelog

Since 10.3.0 Introduced.

PostsRedirectionController::maybe_update_menu_items() code WC 10.3.3

private function maybe_update_menu_items(): void {
	global $pagenow, $submenu;

	// Do not conflict with CPT > HPOS redirection.
	if ( 'edit.php' === $pagenow && in_array( $_GET['post_type'] ?? '', wc_get_order_types( 'admin-menu' ), true ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		return;
	}

	if ( \WC_Admin_Menus::can_view_woocommerce_menu_item() ) {
		return;
	}

	$post_types = array_filter( array_map( 'get_post_type_object', wc_get_order_types( 'admin-menu' ) ) );
	foreach ( $post_types as $post_type ) {
		if ( ! current_user_can( $post_type->cap->edit_posts ) || ! isset( $submenu[ 'edit.php?post_type=' . $post_type->name ] ) ) {
			continue;
		}

		$post_type_menu = &$submenu[ 'edit.php?post_type=' . $post_type->name ];
		$menu_indexes   = array_flip( array_map( fn( $x ) => $x[2], $post_type_menu ) );

		// Rewrite URL for the legacy menu item.
		$post_type_menu[ $menu_indexes[ 'edit.php?post_type=' . $post_type->name ] ][2] = $this->page_controller->get_base_page_url( $post_type->name );

		// Hide the legacy "Add New" menu item.
		unset( $post_type_menu[ $menu_indexes[ "post-new.php?post_type={$post_type->name}" ] ] );
	}
}