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
PostsRedirectionController.php
229 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Internal\Admin\Orders; |
| 3 | |
| 4 | use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; |
| 5 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 6 | |
| 7 | /** |
| 8 | * When {@see OrdersTableDataStore} is in use, this class takes care of redirecting admins from CPT-based URLs |
| 9 | * to the new ones. |
| 10 | */ |
| 11 | class PostsRedirectionController { |
| 12 | |
| 13 | /** |
| 14 | * Instance of the PageController class. |
| 15 | * |
| 16 | * @var PageController |
| 17 | */ |
| 18 | private $page_controller; |
| 19 | |
| 20 | /** |
| 21 | * Constructor. |
| 22 | * |
| 23 | * @param PageController $page_controller Page controller instance. Used to generate links/URLs. |
| 24 | */ |
| 25 | public function __construct( PageController $page_controller ) { |
| 26 | $this->page_controller = $page_controller; |
| 27 | |
| 28 | if ( ! wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | add_action( |
| 33 | 'admin_menu', |
| 34 | function () { |
| 35 | $this->maybe_update_menu_items(); |
| 36 | }, |
| 37 | 9999 |
| 38 | ); |
| 39 | |
| 40 | add_action( |
| 41 | 'load-edit.php', |
| 42 | function() { |
| 43 | $this->maybe_redirect_to_orders_page(); |
| 44 | } |
| 45 | ); |
| 46 | |
| 47 | add_action( |
| 48 | 'load-post-new.php', |
| 49 | function() { |
| 50 | $this->maybe_redirect_to_new_order_page(); |
| 51 | } |
| 52 | ); |
| 53 | |
| 54 | add_action( |
| 55 | 'load-post.php', |
| 56 | function() { |
| 57 | $this->maybe_redirect_to_edit_order_page(); |
| 58 | } |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * If needed, performs a redirection to the main orders page. |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | private function maybe_redirect_to_orders_page(): void { |
| 68 | $post_type = $_GET['post_type'] ?? ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 69 | |
| 70 | if ( ! $post_type || ! in_array( $post_type, wc_get_order_types( 'admin-menu' ), true ) ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Respect query args, except for 'post_type'. |
| 75 | $query_args = wp_unslash( $_GET ); |
| 76 | $action = $query_args['action'] ?? ''; |
| 77 | $posts = $query_args['post'] ?? array(); |
| 78 | unset( $query_args['post_type'], $query_args['post'], $query_args['_wpnonce'], $query_args['_wp_http_referer'], $query_args['action'] ); |
| 79 | |
| 80 | // Remap 'post_status' arg. |
| 81 | if ( isset( $query_args['post_status'] ) ) { |
| 82 | $query_args['status'] = $query_args['post_status']; |
| 83 | unset( $query_args['post_status'] ); |
| 84 | } |
| 85 | |
| 86 | $new_url = $this->page_controller->get_base_page_url( $post_type ); |
| 87 | $new_url = add_query_arg( $query_args, $new_url ); |
| 88 | |
| 89 | // Handle bulk actions. |
| 90 | if ( $action && in_array( $action, array( 'trash', 'untrash', 'delete', 'mark_processing', 'mark_on-hold', 'mark_completed', 'mark_cancelled' ), true ) ) { |
| 91 | check_admin_referer( 'bulk-posts' ); |
| 92 | |
| 93 | $new_url = add_query_arg( |
| 94 | array( |
| 95 | 'action' => $action, |
| 96 | 'id' => $posts, |
| 97 | '_wp_http_referer' => $this->page_controller->get_orders_url(), |
| 98 | '_wpnonce' => wp_create_nonce( 'bulk-orders' ), |
| 99 | ), |
| 100 | $new_url |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | wp_safe_redirect( $new_url, 301 ); |
| 105 | exit; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * If needed, performs a redirection to the new order page. |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | private function maybe_redirect_to_new_order_page(): void { |
| 114 | $post_type = $_GET['post_type'] ?? ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 115 | |
| 116 | if ( ! $post_type || ! in_array( $post_type, wc_get_order_types( 'admin-menu' ), true ) ) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // Respect query args, except for 'post_type'. |
| 121 | $query_args = wp_unslash( $_GET ); |
| 122 | unset( $query_args['post_type'] ); |
| 123 | |
| 124 | $new_url = $this->page_controller->get_new_page_url( $post_type ); |
| 125 | $new_url = add_query_arg( $query_args, $new_url ); |
| 126 | |
| 127 | wp_safe_redirect( $new_url, 301 ); |
| 128 | exit; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * If needed, performs a redirection to the edit order page. |
| 133 | * |
| 134 | * @return void |
| 135 | */ |
| 136 | private function maybe_redirect_to_edit_order_page(): void { |
| 137 | $post_id = absint( $_GET['post'] ?? 0 ); |
| 138 | if ( ! $post_id ) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | $redirect_from_types = wc_get_order_types( 'admin-menu' ); |
| 143 | $redirect_from_types[] = 'shop_order_placehold'; |
| 144 | |
| 145 | $post_type = get_post_type( $post_id ); |
| 146 | $order_type = $post_type ? $post_type : OrderUtil::get_order_type( $post_id ); |
| 147 | if ( ! in_array( $order_type, $redirect_from_types, true ) || ! isset( $_GET['action'] ) ) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Respect query args, except for 'post'. |
| 152 | $query_args = wp_unslash( $_GET ); |
| 153 | $action = $query_args['action']; |
| 154 | unset( $query_args['post'], $query_args['_wpnonce'], $query_args['_wp_http_referer'], $query_args['action'] ); |
| 155 | |
| 156 | $new_url = ''; |
| 157 | |
| 158 | switch ( $action ) { |
| 159 | case 'edit': |
| 160 | $new_url = $this->page_controller->get_edit_url( $post_id ); |
| 161 | break; |
| 162 | |
| 163 | case 'trash': |
| 164 | case 'untrash': |
| 165 | case 'delete': |
| 166 | // Re-generate nonce if validation passes. |
| 167 | check_admin_referer( $action . '-post_' . $post_id ); |
| 168 | |
| 169 | $new_url = add_query_arg( |
| 170 | array( |
| 171 | 'action' => $action, |
| 172 | 'order' => array( $post_id ), |
| 173 | '_wp_http_referer' => $this->page_controller->get_orders_url(), |
| 174 | '_wpnonce' => wp_create_nonce( 'bulk-orders' ), |
| 175 | ), |
| 176 | $this->page_controller->get_orders_url() |
| 177 | ); |
| 178 | |
| 179 | break; |
| 180 | |
| 181 | default: |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | if ( ! $new_url ) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | $new_url = add_query_arg( $query_args, $new_url ); |
| 190 | |
| 191 | wp_safe_redirect( $new_url, 301 ); |
| 192 | exit; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Rewrites legacy post type menu items to point to the HPOS orders page when the main WooCommerce menu is not visible. |
| 197 | * |
| 198 | * @since 10.3.0 |
| 199 | */ |
| 200 | private function maybe_update_menu_items(): void { |
| 201 | global $pagenow, $submenu; |
| 202 | |
| 203 | // Do not conflict with CPT > HPOS redirection. |
| 204 | if ( 'edit.php' === $pagenow && in_array( $_GET['post_type'] ?? '', wc_get_order_types( 'admin-menu' ), true ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | if ( \WC_Admin_Menus::can_view_woocommerce_menu_item() ) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | $post_types = array_filter( array_map( 'get_post_type_object', wc_get_order_types( 'admin-menu' ) ) ); |
| 213 | foreach ( $post_types as $post_type ) { |
| 214 | if ( ! current_user_can( $post_type->cap->edit_posts ) || ! isset( $submenu[ 'edit.php?post_type=' . $post_type->name ] ) ) { |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | $post_type_menu = &$submenu[ 'edit.php?post_type=' . $post_type->name ]; |
| 219 | $menu_indexes = array_flip( array_map( fn( $x ) => $x[2], $post_type_menu ) ); |
| 220 | |
| 221 | // Rewrite URL for the legacy menu item. |
| 222 | $post_type_menu[ $menu_indexes[ 'edit.php?post_type=' . $post_type->name ] ][2] = $this->page_controller->get_base_page_url( $post_type->name ); |
| 223 | |
| 224 | // Hide the legacy "Add New" menu item. |
| 225 | unset( $post_type_menu[ $menu_indexes[ "post-new.php?post_type={$post_type->name}" ] ] ); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 |