PluginProbe
Packeta / 1.4.1
Packeta v1.4.1
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 / Controller.php

Controller.php in Packeta 1.4.1, at src/Packetery/Module/Order/Controller.php

235 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Controller
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\Size;
13 use Packetery\Module\Order;
14 use WP_Error;
15 use WP_REST_Controller;
16 use WP_REST_Request;
17 use WP_REST_Response;
18 use WP_REST_Server;
19
20 /**
21 * Class Controller
22 *
23 * @package Packetery\Module\Order
24 */
25 class Controller extends WP_REST_Controller {
26
27 public const PATH_SAVE_MODAL = '/save';
28 public const PATH_SUBMIT_TO_API = '/submit-to-api';
29
30 /**
31 * Order modal.
32 *
33 * @var Modal
34 */
35 private $orderModal;
36
37 /**
38 * Router.
39 *
40 * @var ControllerRouter
41 */
42 private $router;
43
44 /**
45 * Packet submitter.
46 *
47 * @var PacketSubmitter
48 */
49 private $packetSubmitter;
50
51 /**
52 * Order repository.
53 *
54 * @var Repository
55 */
56 private $orderRepository;
57
58 /**
59 * Grid extender service.
60 *
61 * @var GridExtender
62 */
63 private $gridExtender;
64
65 /**
66 * Controller constructor.
67 *
68 * @param Modal $orderModal Modal.
69 * @param ControllerRouter $controllerRouter Router.
70 * @param PacketSubmitter $packetSubmitter Packet submitter.
71 * @param Order\Repository $orderRepository Order repository.
72 * @param GridExtender $gridExtender Grid extender.
73 */
74 public function __construct(
75 Modal $orderModal,
76 ControllerRouter $controllerRouter,
77 PacketSubmitter $packetSubmitter,
78 Order\Repository $orderRepository,
79 GridExtender $gridExtender
80 ) {
81 $this->orderModal = $orderModal;
82 $this->router = $controllerRouter;
83 $this->namespace = $controllerRouter->getNamespace();
84 $this->rest_base = $controllerRouter->getRestBase();
85 $this->packetSubmitter = $packetSubmitter;
86 $this->orderRepository = $orderRepository;
87 $this->gridExtender = $gridExtender;
88 }
89
90 /**
91 * Register the routes of the controller.
92 *
93 * @return void
94 */
95 public function registerRoutes(): void {
96 $this->router->registerRoute(
97 self::PATH_SAVE_MODAL,
98 [
99 [
100 'methods' => WP_REST_Server::EDITABLE,
101 'callback' => [ $this, 'saveModal' ],
102 'permission_callback' => function () {
103 return current_user_can( 'edit_posts' );
104 },
105 ],
106 ]
107 );
108 $this->router->registerRoute(
109 self::PATH_SUBMIT_TO_API,
110 [
111 [
112 'methods' => WP_REST_Server::ALLMETHODS,
113 'callback' => [ $this, 'submitToApi' ],
114 'permission_callback' => function () {
115 return current_user_can( 'edit_posts' );
116 },
117 ],
118 ]
119 );
120 }
121
122 /**
123 * Submit packet to API.
124 *
125 * @param WP_REST_Request $request Full data about the request.
126 *
127 * @return WP_REST_Response
128 */
129 public function submitToApi( WP_REST_Request $request ) {
130 $data = [];
131 $parameters = $request->get_body_params();
132 $orderId = $parameters['orderId'];
133 $wcOrder = wc_get_order( $orderId );
134
135 $resultsCounter = [
136 'success' => 0,
137 'ignored' => 0,
138 'errors' => 0,
139 'logs' => 0,
140 ];
141 if ( false === $wcOrder ) {
142 // translators: %s is order id.
143 $resultsCounter['errors'] = sprintf( __( 'Order %s does not exist.', 'packeta' ), $orderId );
144 } else {
145 $this->packetSubmitter->submitPacket( $wcOrder, $resultsCounter );
146 }
147 $data['redirectTo'] = add_query_arg(
148 [
149 'post_type' => 'shop_order',
150 'packetery_order_id' => $orderId,
151 'submit_to_api' => '1',
152 ] + $resultsCounter,
153 admin_url( 'edit.php' )
154 );
155
156 return new WP_REST_Response( $data, 200 );
157 }
158
159 /**
160 * Update one item from the collection
161 *
162 * @param WP_REST_Request $request Full data about the request.
163 *
164 * @return WP_REST_Response|WP_Error
165 */
166 public function saveModal( WP_REST_Request $request ) {
167 $data = [];
168 $parameters = $request->get_body_params();
169 $packeteryWeight = $parameters['packeteryWeight'];
170 $packeteryOriginalWeight = $parameters['packeteryOriginalWeight'];
171 $packeteryWidth = $parameters['packeteryWidth'];
172 $packeteryLength = $parameters['packeteryLength'];
173 $packeteryHeight = $parameters['packeteryHeight'];
174 $orderId = (int) $parameters['orderId'];
175
176 $form = $this->orderModal->createForm();
177 $form->setValues(
178 [
179 'packetery_weight' => $packeteryWeight,
180 'packetery_original_weight' => $packeteryOriginalWeight,
181 'packetery_width' => $packeteryWidth,
182 'packetery_length' => $packeteryLength,
183 'packetery_height' => $packeteryHeight,
184 ]
185 );
186
187 if ( false === $form->isValid() ) {
188 return new WP_Error( 'form_invalid', implode( ', ', $form->getErrors() ), 400 );
189 }
190
191 $order = $this->orderRepository->getById( $orderId );
192 if ( null === $order ) {
193 return new WP_Error( 'order_not_loaded', __( 'Order could not be loaded.', 'packeta' ), 400 );
194 }
195
196 $values = $form->getValues( 'array' );
197
198 if ( ! is_numeric( $values['packetery_weight'] ) ) {
199 $order->setWeight( null );
200 } elseif ( (float) $values['packetery_weight'] !== (float) $values['packetery_original_weight'] ) {
201 $order->setWeight( (float) $values['packetery_weight'] );
202 }
203
204 foreach ( [ 'packetery_length', 'packetery_width', 'packetery_height' ] as $sizeKey ) {
205 if ( ! is_numeric( $values[ $sizeKey ] ) ) {
206 $values[ $sizeKey ] = null;
207 }
208 }
209
210 $size = new Size(
211 $values['packetery_length'],
212 $values['packetery_width'],
213 $values['packetery_height']
214 );
215
216 $order->setSize( $size );
217 $this->orderRepository->save( $order );
218
219 $data['message'] = __( 'Success', 'packeta' );
220 $data['data'] = [
221 'fragments' => [
222 sprintf( '[data-packetery-order-id="%d"][data-packetery-order-grid-cell-weight]', $orderId ) => $this->gridExtender->getWeightCellContent( $order ),
223 ],
224 'packetery_weight' => $order->getFinalWeight(),
225 'packetery_length' => $order->getLength(),
226 'packetery_width' => $order->getWidth(),
227 'packetery_height' => $order->getHeight(),
228 'showWarningIcon' => $this->orderModal->showWarningIcon( $order ),
229 'hasOrderManualWeight' => $order->hasManualWeight(),
230 ];
231
232 return new WP_REST_Response( $data, 200 );
233 }
234 }
235