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

191 lines 4.3 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 * Controller constructor.
59 *
60 * @param Modal $orderModal Modal.
61 * @param ControllerRouter $controllerRouter Router.
62 * @param PacketSubmitter $packetSubmitter Packet submitter.
63 * @param Order\Repository $orderRepository Order repository.
64 */
65 public function __construct(
66 Modal $orderModal,
67 ControllerRouter $controllerRouter,
68 PacketSubmitter $packetSubmitter,
69 Order\Repository $orderRepository
70 ) {
71 $this->orderModal = $orderModal;
72 $this->router = $controllerRouter;
73 $this->namespace = $controllerRouter->getNamespace();
74 $this->rest_base = $controllerRouter->getRestBase();
75 $this->packetSubmitter = $packetSubmitter;
76 $this->orderRepository = $orderRepository;
77 }
78
79 /**
80 * Register the routes of the controller.
81 *
82 * @return void
83 */
84 public function registerRoutes(): void {
85 $this->router->registerRoute(
86 self::PATH_SAVE_MODAL,
87 [
88 [
89 'methods' => WP_REST_Server::EDITABLE,
90 'callback' => [ $this, 'saveModal' ],
91 'permission_callback' => function () {
92 return current_user_can( 'edit_posts' );
93 },
94 ],
95 ]
96 );
97 $this->router->registerRoute(
98 self::PATH_SUBMIT_TO_API,
99 [
100 [
101 'methods' => WP_REST_Server::ALLMETHODS,
102 'callback' => [ $this, 'submitToApi' ],
103 'permission_callback' => function () {
104 return current_user_can( 'edit_posts' );
105 },
106 ],
107 ]
108 );
109 }
110
111 /**
112 * Submit packet to API.
113 *
114 * @param WP_REST_Request $request Full data about the request.
115 *
116 * @return WP_REST_Response
117 */
118 public function submitToApi( WP_REST_Request $request ) {
119 $data = [];
120 $parameters = $request->get_body_params();
121 $orderId = $parameters['orderId'];
122 $wcOrder = wc_get_order( $orderId );
123
124 $resultsCounter = [
125 'success' => 0,
126 'ignored' => 0,
127 'errors' => 0,
128 ];
129 if ( false === $wcOrder ) {
130 // translators: %s is order id.
131 $resultsCounter['errors'] = sprintf( __( 'Order %s does not exist.', 'packetery' ), $orderId );
132 } else {
133 $this->packetSubmitter->submitPacket( $wcOrder, $resultsCounter );
134 }
135 $data['redirectTo'] = add_query_arg(
136 [
137 'post_type' => 'shop_order',
138 'submit_to_api' => '1',
139 ] + $resultsCounter,
140 admin_url( 'edit.php' )
141 );
142
143 return new WP_REST_Response( $data, 200 );
144 }
145
146 /**
147 * Update one item from the collection
148 *
149 * @param WP_REST_Request $request Full data about the request.
150 *
151 * @return WP_REST_Response|WP_Error
152 */
153 public function saveModal( WP_REST_Request $request ) {
154 $data = [];
155 $parameters = $request->get_body_params();
156 $packeteryWeight = $parameters['packeteryWeight'];
157 $orderId = (int) $parameters['orderId'];
158
159 $form = $this->orderModal->createForm();
160 $form->setValues(
161 [
162 'packetery_weight' => $packeteryWeight,
163 ]
164 );
165
166 if ( false === $form->isValid() ) {
167 return new WP_Error( 'form_invalid', implode( ', ', $form->getErrors() ), 400 );
168 }
169
170 $order = $this->orderRepository->getById( $orderId );
171 if ( null === $order ) {
172 return new WP_Error( 'order_not_loaded', __( 'Order could not be loaded.', 'packetery' ), 400 );
173 }
174
175 $values = $form->getValues( 'array' );
176 if ( ! is_numeric( $values['packetery_weight'] ) ) {
177 $values['packetery_weight'] = $order->getCalculatedWeight();
178 }
179
180 $order->setWeight( $values['packetery_weight'] );
181 $this->orderRepository->save( $order );
182
183 $data['message'] = __( 'Success', 'packetery' );
184 $data['data'] = [
185 'packetery_weight' => $order->getWeight(),
186 ];
187
188 return new WP_REST_Response( $data, 200 );
189 }
190 }
191