← All changes
|
src/Packetery/Module/Order/PacketActionsCommonLogic.php
+29
-45
1.5.3
→
2.3.2
View file →
| @@ -10,12 +10,12 @@ | ||
| 10 | 10 | namespace Packetery\Module\Order; |
| 11 | 11 | |
| 12 | 12 | use Packetery\Core\Entity; |
| 13 | 13 | use Packetery\Module\MessageManager; |
| 14 | +use Packetery\Module\ModuleHelper; | |
| 14 | 15 | use Packetery\Module\Plugin; |
| 15 | -use PacketeryNette\Http\Request; | |
| 16 | +use Packetery\Nette\Http\Request; | |
| 16 | 17 | |
| 17 | - | |
| 18 | 18 | /** |
| 19 | 19 | * Class PacketActionsBrain |
| 20 | 20 | * |
| 21 | 21 | * @package Packetery\Module\Order |
| @@ -21,15 +21,17 @@ | ||
| 21 | 21 | * @package Packetery\Module\Order |
| 22 | 22 | */ |
| 23 | 23 | class PacketActionsCommonLogic { |
| 24 | 24 | |
| 25 | - public const PARAM_ORDER_ID = 'order_id'; | |
| 26 | - public const PARAM_REDIRECT_TO = 'packetery_redirect_to'; | |
| 27 | - public const PARAM_ORDER_GRID_PARAMS = 'packetery_order_grid_params'; | |
| 28 | - public const REDIRECT_TO_ORDER_GRID = 'order-grid'; | |
| 29 | - public const REDIRECT_TO_ORDER_DETAIL = 'order-detail'; | |
| 30 | - public const ACTION_CANCEL_PACKET = 'cancel_packet'; | |
| 31 | - public const ACTION_SUBMIT_PACKET = 'submit_packet'; | |
| 25 | + public const PARAM_ORDER_ID = 'order_id'; | |
| 26 | + public const PARAM_PACKET_ID = 'packet_id'; | |
| 27 | + public const PARAM_REDIRECT_TO = 'packetery_redirect_to'; | |
| 28 | + public const PARAM_ORDER_GRID_PARAMS = 'packetery_order_grid_params'; | |
| 29 | + public const REDIRECT_TO_ORDER_GRID = 'order-grid'; | |
| 30 | + public const REDIRECT_TO_ORDER_DETAIL = 'order-detail'; | |
| 31 | + public const ACTION_CANCEL_PACKET = 'cancel_packet'; | |
| 32 | + public const ACTION_SUBMIT_PACKET = 'submit_packet'; | |
| 33 | + public const ACTION_SUBMIT_PACKET_CLAIM = 'submit_packet_claim'; | |
| 32 | 34 | |
| 33 | 35 | /** |
| 34 | 36 | * Order repository. |
| 35 | 37 | * |
| @@ -78,23 +80,19 @@ | ||
| 78 | 80 | */ |
| 79 | 81 | public function checkAction( string $action, Entity\Order $order ): void { |
| 80 | 82 | $redirectTo = $this->request->getQuery( self::PARAM_REDIRECT_TO ); |
| 81 | 83 | |
| 82 | - if ( 1 !== wp_verify_nonce( $this->request->getQuery( Plugin::PARAM_NONCE ), self::createNonceAction( $action, $order->getNumber() ) ) ) { | |
| 84 | + if ( wp_verify_nonce( $this->request->getQuery( Plugin::PARAM_NONCE ), self::createNonceAction( $action, $order->getNumber() ) ) !== 1 ) { | |
| 83 | 85 | $this->messageManager->flash_message( __( 'Link has expired. Please try again.', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 84 | 86 | $this->redirectTo( $redirectTo, $order ); |
| 85 | 87 | } |
| 86 | 88 | } |
| 87 | 89 | |
| 88 | - /** | |
| 89 | - * Creates nonce action name. | |
| 90 | - * | |
| 91 | - * @param string $action Action. | |
| 92 | - * @param string $orderNumber Order number. | |
| 93 | - * | |
| 94 | - * @return string | |
| 95 | - */ | |
| 96 | - public static function createNonceAction( string $action, string $orderNumber ): string { | |
| 90 | + public static function createNonceAction( string $action, ?string $orderNumber ): string { | |
| 91 | + if ( $orderNumber === null ) { | |
| 92 | + return ''; | |
| 93 | + } | |
| 94 | + | |
| 97 | 95 | return $action . '_' . $orderNumber; |
| 98 | 96 | } |
| 99 | 97 | |
| 100 | 98 | /** |
| @@ -105,37 +103,23 @@ | ||
| 105 | 103 | * |
| 106 | 104 | * @return void |
| 107 | 105 | */ |
| 108 | 106 | public function redirectTo( string $redirectTo, ?Entity\Order $order = null ): void { |
| 109 | - if ( self::REDIRECT_TO_ORDER_GRID === $redirectTo ) { | |
| 110 | - $orderGridParams = []; | |
| 111 | - parse_str( $this->request->getQuery( self::PARAM_ORDER_GRID_PARAMS ) ?? '', $orderGridParams ); | |
| 107 | + if ( $redirectTo === self::REDIRECT_TO_ORDER_GRID ) { | |
| 108 | + $queryVars = []; | |
| 109 | + parse_str( $this->request->getQuery( self::PARAM_ORDER_GRID_PARAMS ) ?? '', $queryVars ); | |
| 112 | 110 | |
| 113 | - $redirectLink = add_query_arg( | |
| 114 | - [ | |
| 115 | - 'post_type' => 'shop_order', | |
| 116 | - ] + $orderGridParams, | |
| 117 | - admin_url( 'edit.php' ) | |
| 118 | - ); | |
| 119 | - | |
| 120 | - if ( wp_safe_redirect( $redirectLink ) ) { | |
| 111 | + if ( wp_safe_redirect( ModuleHelper::getOrderGridUrl( $queryVars ) ) ) { | |
| 121 | 112 | exit; |
| 122 | 113 | } |
| 123 | 114 | } |
| 124 | 115 | |
| 125 | - if ( self::REDIRECT_TO_ORDER_DETAIL === $redirectTo && null !== $order ) { | |
| 126 | - $redirectLink = add_query_arg( | |
| 127 | - [ | |
| 128 | - 'post_type' => 'shop_order', | |
| 129 | - 'post' => $order->getNumber(), | |
| 130 | - 'action' => 'edit', | |
| 131 | - ], | |
| 132 | - admin_url( 'post.php' ) | |
| 133 | - ); | |
| 134 | - | |
| 135 | - if ( wp_safe_redirect( $redirectLink ) ) { | |
| 136 | - exit; | |
| 137 | - } | |
| 116 | + if ( | |
| 117 | + $redirectTo === self::REDIRECT_TO_ORDER_DETAIL && | |
| 118 | + $order !== null && | |
| 119 | + wp_safe_redirect( ModuleHelper::getOrderDetailUrl( (int) $order->getNumber() ) ) | |
| 120 | + ) { | |
| 121 | + exit; | |
| 138 | 122 | } |
| 139 | 123 | } |
| 140 | 124 | |
| 141 | 125 | /** |
| @@ -158,10 +142,10 @@ | ||
| 158 | 142 | * @return Entity\Order|null |
| 159 | 143 | */ |
| 160 | 144 | public function getOrder(): ?Entity\Order { |
| 161 | 145 | $orderId = $this->getOrderId(); |
| 162 | - if ( null !== $orderId ) { | |
| 163 | - return $this->orderRepository->getById( $orderId ); | |
| 146 | + if ( $orderId !== null ) { | |
| 147 | + return $this->orderRepository->getByIdWithValidCarrier( $orderId ); | |
| 164 | 148 | } |
| 165 | 149 | |
| 166 | 150 | return null; |
| 167 | 151 | } |