PluginProbe
Packeta / 1.5.3
Packeta v1.5.3
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / PacketActionsCommonLogic.php

PacketActionsCommonLogic.php in Packeta 1.5.3, at src/Packetery/Module/Order/PacketActionsCommonLogic.php

169 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class PacketCanceller
4 *
5 * @package Packetery\Module\Order
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Order;
11
12 use Packetery\Core\Entity;
13 use Packetery\Module\MessageManager;
14 use Packetery\Module\Plugin;
15 use PacketeryNette\Http\Request;
16
17
18 /**
19 * Class PacketActionsBrain
20 *
21 * @package Packetery\Module\Order
22 */
23 class PacketActionsCommonLogic {
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';
32
33 /**
34 * Order repository.
35 *
36 * @var Repository
37 */
38 protected $orderRepository;
39
40 /**
41 * Request.
42 *
43 * @var Request
44 */
45 protected $request;
46
47 /**
48 * Message manager.
49 *
50 * @var MessageManager
51 */
52 protected $messageManager;
53
54 /**
55 * Constructor.
56 *
57 * @param Repository $orderRepository Order repository.
58 * @param Request $request Request.
59 * @param MessageManager $messageManager Message manager.
60 */
61 public function __construct(
62 Repository $orderRepository,
63 Request $request,
64 MessageManager $messageManager
65 ) {
66 $this->orderRepository = $orderRepository;
67 $this->request = $request;
68 $this->messageManager = $messageManager;
69 }
70
71 /**
72 * Process actions.
73 *
74 * @param string $action Action.
75 * @param Entity\Order $order Order.
76 *
77 * @return void
78 */
79 public function checkAction( string $action, Entity\Order $order ): void {
80 $redirectTo = $this->request->getQuery( self::PARAM_REDIRECT_TO );
81
82 if ( 1 !== wp_verify_nonce( $this->request->getQuery( Plugin::PARAM_NONCE ), self::createNonceAction( $action, $order->getNumber() ) ) ) {
83 $this->messageManager->flash_message( __( 'Link has expired. Please try again.', 'packeta' ), MessageManager::TYPE_ERROR );
84 $this->redirectTo( $redirectTo, $order );
85 }
86 }
87
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 {
97 return $action . '_' . $orderNumber;
98 }
99
100 /**
101 * Redirects.
102 *
103 * @param string $redirectTo Redirect to.
104 * @param Entity\Order|null $order Order.
105 *
106 * @return void
107 */
108 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 );
112
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 ) ) {
121 exit;
122 }
123 }
124
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 }
138 }
139 }
140
141 /**
142 * Gets order ID.
143 *
144 * @return int|null
145 */
146 private function getOrderId(): ?int {
147 $orderId = $this->request->getQuery( self::PARAM_ORDER_ID );
148 if ( is_numeric( $orderId ) ) {
149 return (int) $orderId;
150 }
151
152 return null;
153 }
154
155 /**
156 * Gets order.
157 *
158 * @return Entity\Order|null
159 */
160 public function getOrder(): ?Entity\Order {
161 $orderId = $this->getOrderId();
162 if ( null !== $orderId ) {
163 return $this->orderRepository->getById( $orderId );
164 }
165
166 return null;
167 }
168 }
169