MetaBoxes
2 months ago
COTRedirectionController.php
1 year ago
Edit.php
1 year ago
EditLock.php
1 year ago
ListTable.php
4 weeks ago
PageController.php
9 months ago
PostsRedirectionController.php
9 months ago
COTRedirectionController.php
75 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\Admin\Orders; |
| 4 | |
| 5 | /** |
| 6 | * When Custom Order Tables are not the default order store (ie, posts are authoritative), we should take care of |
| 7 | * redirecting requests for the order editor and order admin list table to the equivalent posts-table screens. |
| 8 | * |
| 9 | * If the redirect logic is problematic, it can be unhooked using code like the following example: |
| 10 | * |
| 11 | * remove_action( |
| 12 | * 'admin_page_access_denied', |
| 13 | * array( wc_get_container()->get( COTRedirectionController::class ), 'handle_hpos_admin_requests' ) |
| 14 | * ); |
| 15 | */ |
| 16 | class COTRedirectionController { |
| 17 | |
| 18 | /** |
| 19 | * Add hooks needed to perform our magic. |
| 20 | */ |
| 21 | public function setup(): void { |
| 22 | // Only take action in cases where access to the admin screen would otherwise be denied. |
| 23 | add_action( 'admin_page_access_denied', array( $this, 'handle_hpos_admin_requests' ) ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Listen for denied admin requests and, if they appear to relate to HPOS admin screens, potentially |
| 28 | * redirect the user to the equivalent CPT-driven screens. |
| 29 | * |
| 30 | * @param array|null $query_params The query parameters to use when determining the redirect. If not provided, the $_GET superglobal will be used. |
| 31 | * |
| 32 | * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed. |
| 33 | */ |
| 34 | public function handle_hpos_admin_requests( $query_params = null ) { |
| 35 | $query_params = is_array( $query_params ) ? $query_params : $_GET; |
| 36 | |
| 37 | if ( ! isset( $query_params['page'] ) || 'wc-orders' !== $query_params['page'] ) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | $params = wp_unslash( $query_params ); |
| 42 | $action = $params['action'] ?? ''; |
| 43 | unset( $params['page'] ); |
| 44 | |
| 45 | if ( 'edit' === $action && isset( $params['id'] ) ) { |
| 46 | $params['post'] = $params['id']; |
| 47 | unset( $params['id'] ); |
| 48 | $new_url = add_query_arg( $params, get_admin_url( null, 'post.php' ) ); |
| 49 | } elseif ( 'new' === $action ) { |
| 50 | unset( $params['action'] ); |
| 51 | $params['post_type'] = 'shop_order'; |
| 52 | $new_url = add_query_arg( $params, get_admin_url( null, 'post-new.php' ) ); |
| 53 | } else { |
| 54 | // If nonce parameters are present and valid, rebuild them for the CPT admin list table. |
| 55 | if ( isset( $params['_wpnonce'] ) && check_admin_referer( 'bulk-orders' ) ) { |
| 56 | $params['_wp_http_referer'] = get_admin_url( null, 'edit.php?post_type=shop_order' ); |
| 57 | $params['_wpnonce'] = wp_create_nonce( 'bulk-posts' ); |
| 58 | } |
| 59 | |
| 60 | // If an `id` array parameter is present, rename as `post`. |
| 61 | if ( isset( $params['id'] ) && is_array( $params['id'] ) ) { |
| 62 | $params['post'] = $params['id']; |
| 63 | unset( $params['id'] ); |
| 64 | } |
| 65 | |
| 66 | $params['post_type'] = 'shop_order'; |
| 67 | $new_url = add_query_arg( $params, get_admin_url( null, 'edit.php' ) ); |
| 68 | } |
| 69 | |
| 70 | if ( ! empty( $new_url ) && wp_safe_redirect( $new_url, 301 ) ) { |
| 71 | exit; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 |