Automattic\WooCommerce\Internal\Admin\Orders

COTRedirectionController::handle_hpos_admin_requests()privateWC 1.0

Listen for denied admin requests and, if they appear to relate to HPOS admin screens, potentially redirect the user to the equivalent CPT-driven screens.

Method of the class: COTRedirectionController{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->handle_hpos_admin_requests( $query_params );
$query_params(array|null)
The query parameters to use when determining the redirect. If not provided, the $_GET superglobal will be used.
Default: null

COTRedirectionController::handle_hpos_admin_requests() code WC 8.7.0

private function handle_hpos_admin_requests( $query_params = null ) {
	$query_params = is_array( $query_params ) ? $query_params : $_GET;

	if ( ! isset( $query_params['page'] ) || 'wc-orders' !== $query_params['page'] ) {
		return;
	}

	$params = wp_unslash( $query_params );
	$action = $params['action'] ?? '';
	unset( $params['page'] );

	if ( 'edit' === $action && isset( $params['id'] ) ) {
		$params['post'] = $params['id'];
		unset( $params['id'] );
		$new_url = add_query_arg( $params, get_admin_url( null, 'post.php' ) );
	} elseif ( 'new' === $action ) {
		unset( $params['action'] );
		$params['post_type'] = 'shop_order';
		$new_url             = add_query_arg( $params, get_admin_url( null, 'post-new.php' ) );
	} else {
		// If nonce parameters are present and valid, rebuild them for the CPT admin list table.
		if ( isset( $params['_wpnonce'] ) && check_admin_referer( 'bulk-orders' ) ) {
			$params['_wp_http_referer'] = get_admin_url( null, 'edit.php?post_type=shop_order' );
			$params['_wpnonce']         = wp_create_nonce( 'bulk-posts' );
		}

		// If an `id` array parameter is present, rename as `post`.
		if ( isset( $params['id'] ) && is_array( $params['id'] ) ) {
			$params['post'] = $params['id'];
			unset( $params['id'] );
		}

		$params['post_type'] = 'shop_order';
		$new_url             = add_query_arg( $params, get_admin_url( null, 'edit.php' ) );
	}

	if ( ! empty( $new_url ) && wp_safe_redirect( $new_url, 301 ) ) {
		exit;
	}
}