PluginProbe
Packeta / 1.3.2
Packeta v1.3.2
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.3.2, at src/Packetery/Module/Order/Controller.php

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