PluginProbe
Packeta / 1.4.3
Packeta v1.4.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 / BulkActions.php

BulkActions.php in Packeta 1.4.3, at src/Packetery/Module/Order/BulkActions.php

219 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class BulkActions
4 *
5 * @package Packetery\Order
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Order;
11
12 use Packetery\Module\Log;
13 use PacketeryLatte\Engine;
14 use PacketeryNette\Http\Request;
15 use WC_Order;
16
17 /**
18 * Class BulkActions
19 *
20 * @package Packetery\Order
21 */
22 class BulkActions {
23 /**
24 * Latte engine.
25 *
26 * @var Engine
27 */
28 private $latteEngine;
29
30 /**
31 * HTTP request.
32 *
33 * @var Request
34 */
35 private $httpRequest;
36
37 /**
38 * OrderApi.
39 *
40 * @var PacketSubmitter
41 */
42 private $packetSubmitter;
43
44 /**
45 * Log page.
46 *
47 * @var Log\Page
48 */
49 private $logPage;
50
51 /**
52 * BulkActions constructor.
53 *
54 * @param Engine $latteEngine Latte engine.
55 * @param Request $httpRequest HTTP request.
56 * @param PacketSubmitter $packetSubmitter Order API Client.
57 * @param Log\Page $logPage Log page.
58 */
59 public function __construct( Engine $latteEngine, Request $httpRequest, PacketSubmitter $packetSubmitter, Log\Page $logPage ) {
60 $this->latteEngine = $latteEngine;
61 $this->httpRequest = $httpRequest;
62 $this->packetSubmitter = $packetSubmitter;
63 $this->logPage = $logPage;
64 }
65
66 /**
67 * Adds custom actions to dropdown in admin order list.
68 *
69 * @param array $actions Array of action.
70 *
71 * @return array
72 */
73 public function addActions( array $actions ): array {
74 $actions['submit_to_api'] = __( 'Submit orders to Packeta', 'packeta' );
75 $actions[ LabelPrint::ACTION_PACKETA_LABELS ] = __( 'Print labels', 'packeta' );
76 $actions[ LabelPrint::ACTION_CARRIER_LABELS ] = __( 'Print carrier labels', 'packeta' );
77 $actions[ CollectionPrint::ACTION_PRINT_ORDER_COLLECTION ] = __( 'Print AWB', 'packeta' );
78
79 return $actions;
80 }
81
82 /**
83 * Executes the action for selected orders and returns url to redirect to.
84 *
85 * @param string $redirectTo Url.
86 * @param string $action Action id.
87 * @param array $postIds Order ids.
88 *
89 * @return string
90 */
91 public function handleActions( string $redirectTo, string $action, array $postIds ): string {
92 if ( CollectionPrint::ACTION_PRINT_ORDER_COLLECTION === $action ) {
93 set_transient( CollectionPrint::getOrderIdsTransientName(), $postIds );
94
95 return add_query_arg(
96 [
97 'page' => CollectionPrint::PAGE_SLUG,
98 ],
99 admin_url( 'admin.php' )
100 );
101 }
102
103 if ( in_array( $action, [ LabelPrint::ACTION_PACKETA_LABELS, LabelPrint::ACTION_CARRIER_LABELS ], true ) ) {
104 set_transient( LabelPrint::getOrderIdsTransientName(), $postIds, 60 * 60 );
105 set_transient( LabelPrint::getBackLinkTransientName(), $redirectTo, 60 * 60 );
106
107 return add_query_arg(
108 [
109 'page' => LabelPrint::MENU_SLUG,
110 LabelPrint::LABEL_TYPE_PARAM => $action,
111 ],
112 'admin.php'
113 );
114 }
115
116 if ( 'submit_to_api' === $action ) {
117 $resultsCounter = [
118 'success' => 0,
119 'ignored' => 0,
120 'errors' => 0,
121 'logs' => 0,
122 ];
123 foreach ( $postIds as $postId ) {
124 $order = wc_get_order( $postId );
125 if ( is_a( $order, WC_Order::class ) ) {
126 $this->packetSubmitter->submitPacket( $order, $resultsCounter );
127 }
128 }
129
130 $queryArgs = $resultsCounter;
131 $queryArgs['submit_to_api'] = true;
132
133 if ( count( $postIds ) === 1 ) {
134 $queryArgs['packetery_order_id'] = array_pop( $postIds );
135 }
136
137 return add_query_arg( $queryArgs, $redirectTo );
138 }
139
140 return $redirectTo;
141 }
142
143 /**
144 * Renders packets export result.
145 */
146 public function renderPacketsExportResult(): void {
147 $get = $this->httpRequest->getQuery();
148 if ( empty( $get['submit_to_api'] ) ) {
149 return;
150 }
151
152 $orderId = ( $get['packetery_order_id'] ?? null );
153 if ( is_numeric( $orderId ) ) {
154 $orderId = (int) $orderId;
155 } else {
156 $orderId = null;
157 }
158
159 $success = null;
160 if ( is_numeric( $get['success'] ) && $get['success'] > 0 ) {
161 if ( $get['logs'] > 0 ) {
162 $success = sprintf(
163 // translators: 1: link start 2: link end.
164 esc_html__( 'Shipments were submitted successfully. %1$sShow logs%2$s', 'packeta' ),
165 '<a href="' . $this->logPage->createLogListUrl( $orderId ) . '">',
166 '</a>'
167 );
168 } else {
169 $success = esc_html__( 'Shipments were submitted successfully.', 'packeta' );
170 }
171 }
172 $ignored = null;
173 if ( is_numeric( $get['ignored'] ) && $get['ignored'] > 0 ) {
174 if ( $get['logs'] > 0 ) {
175 $ignored = sprintf(
176 // translators: 1: total number of shipments 2: link start 3: link end.
177 esc_html__( 'Some shipments (%1$s in total) were not submitted (these were submitted already or are not Packeta orders). %2$sShow logs%3$s', 'packeta' ),
178 $get['ignored'],
179 '<a href="' . $this->logPage->createLogListUrl( $orderId ) . '">',
180 '</a>'
181 );
182 } else {
183 $ignored = sprintf(
184 // translators: %s is count.
185 esc_html__( 'Some shipments (%s in total) were not submitted (these were submitted already or are not Packeta orders).', 'packeta' ),
186 $get['ignored']
187 );
188 }
189 }
190 $errors = null;
191 if ( is_numeric( $get['errors'] ) && $get['errors'] > 0 ) {
192 if ( $get['logs'] > 0 ) {
193 $errors = sprintf(
194 // translators: 1: total number of shipments 2: link start 3: link end.
195 esc_html__( 'Some shipments (%1$s in total) failed to be submitted to Packeta. %2$sShow logs%3$s', 'packeta' ),
196 $get['errors'],
197 '<a href="' . $this->logPage->createLogListUrl( $orderId ) . '">',
198 '</a>'
199 );
200 } else {
201 $errors = sprintf(
202 // translators: %s is count.
203 esc_html__( 'Some shipments (%s in total) failed to be submitted to Packeta.', 'packeta' ),
204 $get['errors']
205 );
206 }
207 } elseif ( isset( $get['errors'] ) ) {
208 $errors = $get['errors'];
209 }
210
211 $latteParams = [
212 'success' => $success,
213 'ignored' => $ignored,
214 'errors' => $errors,
215 ];
216 $this->latteEngine->render( PACKETERY_PLUGIN_DIR . '/template/order/export-result.latte', $latteParams );
217 }
218 }
219