PluginProbe
Packeta / 2.1
Packeta v2.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
← All changes | src/Packetery/Module/Order/GridExtender.php +220 -109 1.6.12.1 View file →
@@ -9,18 +9,23 @@
9 9
10 10 namespace Packetery\Module\Order;
11 11
12 12 use Packetery\Core;
13 -use Packetery\Module;
14 -use Packetery\Module\Carrier;
13 +use Packetery\Core\CoreHelper;
14 +use Packetery\Latte\Engine;
15 +use Packetery\Module\Carrier\CarrierOptionsFactory;
15 16 use Packetery\Module\ContextResolver;
17 +use Packetery\Module\EntityFactory\SizeFactory;
16 18 use Packetery\Module\Exception\InvalidCarrierException;
19 +use Packetery\Module\Framework\WpAdapter;
17 20 use Packetery\Module\Log\Purger;
18 -use Packetery\Latte\Engine;
21 +use Packetery\Module\ModuleHelper;
22 +use Packetery\Module\Plugin;
19 23 use Packetery\Nette\Http\Request;
20 -use Packetery\Module\Plugin;
21 24 use WC_Order;
22 25
26 +use function esc_html;
27 +
23 28 /**
24 29 * Class GridExtender.
25 30 *
26 31 * @package Packetery\Order
@@ -26,25 +31,18 @@
26 31 * @package Packetery\Order
27 32 */
28 33 class GridExtender {
29 34
30 - const TEMPLATE_GRID_COLUMN_WEIGHT = PACKETERY_PLUGIN_DIR . '/template/order/grid-column-weight.latte';
35 + private const TEMPLATE_GRID_COLUMN_WEIGHT = PACKETERY_PLUGIN_DIR . '/template/order/grid-column-weight.latte';
31 36
32 37 /**
33 - * Generic Helper.
38 + * Generic CoreHelper.
34 39 *
35 - * @var Core\Helper
40 + * @var CoreHelper
36 41 */
37 - private $helper;
42 + private $coreHelper;
38 43
39 44 /**
40 - * Carrier repository.
41 - *
42 - * @var Carrier\EntityRepository
43 - */
44 - private $carrierRepository;
45 -
46 - /**
47 45 * Latte Engine.
48 46 *
49 47 * @var Engine
50 48 */
@@ -78,73 +76,149 @@
78 76 */
79 77 private $contextResolver;
80 78
81 79 /**
80 + * Carrier options factory.
81 + *
82 + * @var CarrierOptionsFactory
83 + */
84 + private $carrierOptionsFactory;
85 +
86 + /**
87 + * WordPress Adapter.
88 + *
89 + * @var WpAdapter
90 + */
91 + private $wpAdapter;
92 +
93 + /**
94 + * Module helper
95 + *
96 + * @var ModuleHelper
97 + */
98 + private $moduleHelper;
99 +
100 + /**
101 + * @var SizeFactory
102 + */
103 + private $sizeFactory;
104 +
105 + /**
106 + * @var PacketStatusResolver
107 + */
108 + private $packetStatusResolver;
109 +
110 + /**
82 111 * GridExtender constructor.
83 112 *
84 - * @param Core\Helper $helper Helper.
85 - * @param Carrier\EntityRepository $carrierRepository Carrier repository.
86 - * @param Engine $latteEngine Latte Engine.
87 - * @param Request $httpRequest Http Request.
88 - * @param Repository $orderRepository Order repository.
89 - * @param Core\Validator\Order $orderValidator Order validator.
90 - * @param ContextResolver $contextResolver Context resolver.
113 + * @param CoreHelper $coreHelper CoreHelper.
114 + * @param Engine $latteEngine Latte Engine.
115 + * @param Request $httpRequest Http Request.
116 + * @param Repository $orderRepository Order repository.
117 + * @param OrderValidatorFactory $orderValidatorFactory Order validator.
118 + * @param ContextResolver $contextResolver Context resolver.
119 + * @param CarrierOptionsFactory $carrierOptionsFactory Carrier options factory.
120 + * @param WpAdapter $wpAdapter WordPress adapter.
121 + * @param ModuleHelper $moduleHelper Module helper.
122 + * @param SizeFactory $sizeFactory Size factory.
123 + * @param PacketStatusResolver $packetStatusResolver Packet status resolver.
91 124 */
92 125 public function __construct(
93 - Core\Helper $helper,
94 - Carrier\EntityRepository $carrierRepository,
126 + CoreHelper $coreHelper,
95 127 Engine $latteEngine,
96 128 Request $httpRequest,
97 129 Repository $orderRepository,
98 - Core\Validator\Order $orderValidator,
99 - ContextResolver $contextResolver
130 + OrderValidatorFactory $orderValidatorFactory,
131 + ContextResolver $contextResolver,
132 + CarrierOptionsFactory $carrierOptionsFactory,
133 + WpAdapter $wpAdapter,
134 + ModuleHelper $moduleHelper,
135 + SizeFactory $sizeFactory,
136 + PacketStatusResolver $packetStatusResolver
100 137 ) {
101 - $this->helper = $helper;
102 - $this->carrierRepository = $carrierRepository;
103 - $this->latteEngine = $latteEngine;
104 - $this->httpRequest = $httpRequest;
105 - $this->orderRepository = $orderRepository;
106 - $this->orderValidator = $orderValidator;
107 - $this->contextResolver = $contextResolver;
138 + $this->coreHelper = $coreHelper;
139 + $this->latteEngine = $latteEngine;
140 + $this->httpRequest = $httpRequest;
141 + $this->orderRepository = $orderRepository;
142 + $this->orderValidator = $orderValidatorFactory->create();
143 + $this->contextResolver = $contextResolver;
144 + $this->carrierOptionsFactory = $carrierOptionsFactory;
145 + $this->wpAdapter = $wpAdapter;
146 + $this->moduleHelper = $moduleHelper;
147 + $this->sizeFactory = $sizeFactory;
148 + $this->packetStatusResolver = $packetStatusResolver;
108 149 }
109 150
110 151 /**
111 152 * Adds custom filtering links to order grid.
112 153 *
113 - * @param array $var Array of html links.
154 + * @param string[] $htmlLinks Array of html links.
114 155 *
115 - * @return array
156 + * @return string[]
116 157 */
117 - public function addFilterLinks( array $var ): array {
118 - $latteParams = [
119 - 'link' => Module\Helper::getOrderGridUrl(
120 - [
121 - 'filter_action' => 'packetery_filter_link',
122 - 'packetery_to_submit' => '1',
123 - 'packetery_to_print' => false,
124 - ]
125 - ),
126 - 'title' => __( 'Packeta orders to submit', 'packeta' ),
127 - 'orderCount' => $this->orderRepository->countOrdersToSubmit(),
128 - 'active' => ( $this->httpRequest->getQuery( 'packetery_to_submit' ) === '1' ),
129 - ];
130 - $var[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams );
158 + public function addFilterLinks( array $htmlLinks ): array {
159 + $linkConfig = new GridLinksConfig(
160 + $this->wpAdapter->__( 'Packeta orders to submit', 'packeta' ),
161 + $this->wpAdapter->__( 'Packeta orders to print', 'packeta' ),
162 + $this->wpAdapter->__( 'Run Packeta wizard', 'packeta' )
163 + );
164 + $originalLinkConfig = clone $linkConfig;
165 + $linkConfig = $this->wpAdapter->applyFilters( 'packeta_order_grid_links_settings', $linkConfig );
166 + if ( ! $linkConfig instanceof GridLinksConfig ) {
167 + $linkConfig = $originalLinkConfig;
168 + }
131 169
132 - $latteParams = [
133 - 'link' => Module\Helper::getOrderGridUrl(
134 - [
135 - 'filter_action' => 'packetery_filter_link',
136 - 'packetery_to_submit' => false,
137 - 'packetery_to_print' => '1',
138 - ]
139 - ),
140 - 'title' => __( 'Packeta orders to print', 'packeta' ),
141 - 'orderCount' => $this->orderRepository->countOrdersToPrint(),
142 - 'active' => ( $this->httpRequest->getQuery( 'packetery_to_print' ) === '1' ),
143 - ];
144 - $var[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams );
170 + if ( $linkConfig->isFilterOrdersToSubmitEnabled() === true ) {
171 + $latteParams = [
172 + 'link' => ModuleHelper::getOrderGridUrl(
173 + [
174 + 'filter_action' => 'packetery_filter_link',
175 + 'packetery_to_submit' => '1',
176 + 'packetery_to_print' => false,
177 + ]
178 + ),
179 + 'title' => $linkConfig->getFilterOrdersToSubmitTitle(),
180 + 'orderCount' => $this->orderRepository->countOrdersToSubmit(),
181 + 'active' => ( $this->httpRequest->getQuery( 'packetery_to_submit' ) === '1' ),
182 + ];
183 + $htmlLinks['js-wizard-packetery-filter-orders-to-submit'] = $this->latteEngine->renderToString(
184 + PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte',
185 + $latteParams
186 + );
187 + }
145 188
146 - return $var;
189 + if ( $linkConfig->isFilterOrdersToPrintEnabled() === true ) {
190 + $latteParams = [
191 + 'link' => ModuleHelper::getOrderGridUrl(
192 + [
193 + 'filter_action' => 'packetery_filter_link',
194 + 'packetery_to_submit' => false,
195 + 'packetery_to_print' => '1',
196 + ]
197 + ),
198 + 'title' => $linkConfig->getFilterOrdersToPrintTitle(),
199 + 'orderCount' => $this->orderRepository->countOrdersToPrint(),
200 + 'active' => ( $this->httpRequest->getQuery( 'packetery_to_print' ) === '1' ),
201 + ];
202 + $htmlLinks['js-wizard-packetery-filter-orders-to-print'] = $this->latteEngine->renderToString(
203 + PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte',
204 + $latteParams
205 + );
206 + }
207 +
208 + if ( $linkConfig->isOrderGridRunWizardEnabled() === true ) {
209 + $latteParams = [
210 + 'link' => $this->wpAdapter->adminUrl( 'admin.php?page=wc-orders&wizard-enabled=true&wizard-order-grid-enabled=true' ),
211 + 'title' => $this->wpAdapter->__( 'Use this link to start interactive tutorial', 'packeta' ),
212 + 'escapedText' => '▶ ' . htmlspecialchars( $linkConfig->getOrderGridRunWizardTitle() ),
213 + ];
214 + $htmlLinks['js-wizard-packetery-order-grid-run'] = $this->latteEngine->renderToString(
215 + PACKETERY_PLUGIN_DIR . '/template/order/unescaped-link.latte',
216 + $latteParams
217 + );
218 + }
219 +
220 + return $htmlLinks;
147 221 }
148 222
149 223 /**
150 224 * Adds select to order grid.
@@ -149,19 +223,19 @@
149 223 /**
150 224 * Adds select to order grid.
151 225 */
152 226 public function renderOrderTypeSelect(): void {
153 - if ( false === $this->contextResolver->isOrderGridPage() ) {
227 + if ( $this->contextResolver->isOrderGridPage() === false ) {
154 228 return;
155 229 }
156 230
157 231 $linkFilters = [];
158 232
159 - if ( null !== $this->httpRequest->getQuery( 'packetery_to_submit' ) ) {
233 + if ( $this->httpRequest->getQuery( 'packetery_to_submit' ) !== null ) {
160 234 $linkFilters['packetery_to_submit'] = '1';
161 235 }
162 236
163 - if ( null !== $this->httpRequest->getQuery( 'packetery_to_print' ) ) {
237 + if ( $this->httpRequest->getQuery( 'packetery_to_print' ) !== null ) {
164 238 $linkFilters['packetery_to_print'] = '1';
165 239 }
166 240
167 241 $this->latteEngine->render(
@@ -196,9 +270,9 @@
196 270 * Gets weight cell content.
197 271 *
198 272 * @param Core\Entity\Order $order Order.
199 273 *
200 - * @return array
274 + * @return array{orderNumber: null|string, weight: null|float}
201 275 */
202 276 private function getWeightCellContentParams( Core\Entity\Order $order ): array {
203 277 return [
204 278 'orderNumber' => $order->getNumber(),
@@ -226,13 +300,13 @@
226 300
227 301 /**
228 302 * Fills custom order list columns.
229 303 *
230 - * @param string|mixed $column Current order column name.
231 - * @param \WC_Order|int|mixed $wcOrder WC Order.
304 + * @param string|mixed $column Current order column name.
305 + * @param WC_Order|int|mixed $wcOrder WC Order.
232 306 */
233 307 public function fillCustomOrderListColumns( $column, $wcOrder ): void {
234 - if ( false === is_string( $column ) ) {
308 + if ( is_string( $column ) === false ) {
235 309 return;
236 310 }
237 311
238 312 if ( $wcOrder instanceof WC_Order ) {
@@ -245,15 +319,15 @@
245 319
246 320 try {
247 321 $order = $this->getOrderByIdCached( $orderId );
248 322 } catch ( InvalidCarrierException $exception ) {
249 - if ( 'packetery' === $column ) {
250 - Module\Helper::renderString( $exception->getMessage() );
323 + if ( $column === 'packetery' ) {
324 + echo esc_html( $exception->getMessage() );
251 325 }
252 326
253 327 return;
254 328 }
255 - if ( null === $order ) {
329 + if ( $order === null ) {
256 330 return;
257 331 }
258 332
259 333 switch ( $column ) {
@@ -261,12 +335,13 @@
261 335 $this->latteEngine->render(
262 336 self::TEMPLATE_GRID_COLUMN_WEIGHT,
263 337 $this->getWeightCellContentParams( $order )
264 338 );
339 +
265 340 break;
266 341 case 'packetery_destination':
267 342 $pickupPoint = $order->getPickupPoint();
268 - if ( null !== $pickupPoint ) {
343 + if ( $pickupPoint !== null ) {
269 344 $pointName = $pickupPoint->getName();
270 345 $pointId = $pickupPoint->getId();
271 346 if ( ! $order->isExternalCarrier() ) {
272 347 echo esc_html( "$pointName ($pointId)" );
@@ -272,13 +347,15 @@
272 347 echo esc_html( "$pointName ($pointId)" );
273 348 } else {
274 349 echo esc_html( $pointName );
275 350 }
351 +
276 352 break;
277 353 }
278 354
279 - $homeDeliveryCarrierOptions = Carrier\Options::createByCarrierId( $order->getCarrier()->getId() );
355 + $homeDeliveryCarrierOptions = $this->carrierOptionsFactory->createByCarrierId( $order->getCarrier()->getId() );
280 356 echo esc_html( $homeDeliveryCarrierOptions->getName() );
357 +
281 358 break;
282 359 case 'packetery_packet_id':
283 360 $packetId = $order->getPacketId();
284 361 $packetClaimId = $order->getPacketClaimId();
@@ -290,14 +367,14 @@
290 367 'packetClaimTracking' => __( 'Packet claim tracking', 'packeta' ),
291 368 ],
292 369 ];
293 370
294 - if ( $packetId ) {
295 - $latteParams['packetIdTrackingUrl'] = $this->helper->get_tracking_url( $packetId );
371 + if ( $packetId !== null ) {
372 + $latteParams['packetIdTrackingUrl'] = $this->coreHelper->getTrackingUrl( $packetId );
296 373 }
297 374
298 - if ( $packetClaimId ) {
299 - $latteParams['packetClaimIdTrackingUrl'] = $this->helper->get_tracking_url( $packetClaimId );
375 + if ( $packetClaimId !== null ) {
376 + $latteParams['packetClaimIdTrackingUrl'] = $this->coreHelper->getTrackingUrl( $packetClaimId );
300 377 }
301 378
302 379 $this->latteEngine->render(
303 380 PACKETERY_PLUGIN_DIR . '/template/order/grid-column-tracking.latte',
@@ -302,11 +379,14 @@
302 379 $this->latteEngine->render(
303 380 PACKETERY_PLUGIN_DIR . '/template/order/grid-column-tracking.latte',
304 381 $latteParams
305 382 );
383 +
306 384 break;
307 385 case 'packetery':
308 - $encodedOrderGridParams = rawurlencode( $this->httpRequest->getUrl()->getQuery() );
386 + $orderGridParams = $this->httpRequest->getUrl()->getQueryParameters();
387 + unset( $orderGridParams['wizard-enabled'], $orderGridParams['wizard-order-grid-enabled'] );
388 + $encodedOrderGridParams = rawurlencode( http_build_query( $orderGridParams ) );
309 389 $packetSubmitUrl = add_query_arg(
310 390 [
311 391 PacketActionsCommonLogic::PARAM_ORDER_ID => $order->getNumber(),
312 392 Plugin::PARAM_PACKETERY_ACTION => PacketActionsCommonLogic::ACTION_SUBMIT_PACKET,
@@ -341,36 +421,53 @@
341 421
342 422 $this->latteEngine->render(
343 423 PACKETERY_PLUGIN_DIR . '/template/order/grid-column-packetery.latte',
344 424 [
345 - 'order' => $order,
346 - 'orderIsSubmittable' => $this->orderValidator->isValid( $order ),
347 - 'packetSubmitUrl' => $packetSubmitUrl,
348 - 'packetCancelLink' => $packetCancelLink,
349 - 'printLink' => $printLink,
350 - 'helper' => new Core\Helper(),
351 - 'datePickerFormat' => Core\Helper::DATEPICKER_FORMAT,
352 - 'logPurgerDatetimeModifier' => get_option( Purger::PURGER_OPTION_NAME, Purger::PURGER_MODIFIER_DEFAULT ),
353 - 'packetDeliverOn' => $this->helper->getStringFromDateTime( $order->getDeliverOn(), Core\Helper::DATEPICKER_FORMAT ),
354 - 'translations' => [
355 - 'printLabel' => __( 'Print label', 'packeta' ),
356 - 'setAdditionalPacketInfo' => __( 'Set additional packet information', 'packeta' ),
357 - 'submitToPacketa' => __( 'Submit to packeta', 'packeta' ),
425 + 'order' => $order,
426 + 'dimensions' => $this->sizeFactory->createSizeInSetDimensionUnit( $order ),
427 + 'orderIsSubmittable' => $this->orderValidator->isValid( $order ),
428 + 'isPossibleExtendPacketPickUpDate' => $order->isPossibleExtendPacketPickUpDate(),
429 + 'storedUntil' => $this->coreHelper->getStringFromDateTime( $order->getStoredUntil(), CoreHelper::DATEPICKER_FORMAT ),
430 + 'orderWarningFields' => Form::getInvalidFieldsFromValidationResult( $this->orderValidator->validate( $order ) ),
431 + 'packetSubmitUrl' => $packetSubmitUrl,
432 + 'packetCancelLink' => $packetCancelLink,
433 + 'printLink' => $printLink,
434 + 'helper' => $this->coreHelper,
435 + 'datePickerFormat' => CoreHelper::DATEPICKER_FORMAT,
436 + 'logPurgerDatetimeModifier' => $this->wpAdapter->getOption( Purger::PURGER_OPTION_NAME, Purger::PURGER_MODIFIER_DEFAULT ),
437 + 'packetDeliverOn' => $this->coreHelper->getStringFromDateTime( $order->getDeliverOn(), CoreHelper::DATEPICKER_FORMAT ),
438 + 'translations' => [
439 + 'printLabel' => __( 'Print label', 'packeta' ),
440 + 'setAdditionalPacketInfo' => __( 'Set additional packet information', 'packeta' ),
441 + 'setStoredUntil' => __( 'Set the pickup date extension', 'packeta' ),
442 + 'packetSubmissionNotPossible' => __( 'It is not possible to submit the shipment because all the information required for this shipment is not filled.', 'packeta' ),
443 + 'submitToPacketa' => __( 'Submit to Packeta', 'packeta' ),
358 444 // translators: %s: Order number.
359 - 'reallyCancelPacketHeading' => sprintf( __( 'Order #%s', 'packeta' ), $order->getCustomNumber() ),
445 + 'reallyCancelPacketHeading' => sprintf( __( 'Order #%s', 'packeta' ), $order->getCustomNumber() ),
360 446 // translators: %s: Packet number.
361 - 'reallyCancelPacket' => sprintf( __( 'Do you really wish to cancel parcel number %s?', 'packeta' ), (string) $order->getPacketId() ),
447 + 'reallyCancelPacket' => sprintf( __( 'Do you really wish to cancel parcel number %s?', 'packeta' ), (string) $order->getPacketId() ),
362 448
363 - 'cancelPacket' => __( 'Cancel packet', 'packeta' ),
364 - 'lastErrorFromApi' => __( 'Last error from Packeta API', 'packeta' ),
449 + 'cancelPacket' => __( 'Cancel packet', 'packeta' ),
450 + 'lastErrorFromApi' => __( 'Last error from Packeta API', 'packeta' ),
365 451 ],
366 452 ]
367 453 );
454 +
368 455 break;
369 456 case 'packetery_packet_status':
370 - $statuses = PacketSynchronizer::getPacketStatuses();
371 - echo esc_html( isset( $statuses[ $order->getPacketStatus() ] ) ? $statuses[ $order->getPacketStatus() ]->getTranslatedName() : $order->getPacketStatus() );
457 + echo esc_html( $this->packetStatusResolver->getTranslatedName( $order->getPacketStatus() ) );
458 +
372 459 break;
460 + case 'packetery_packet_stored_until':
461 + $this->latteEngine->render(
462 + PACKETERY_PLUGIN_DIR . '/template/order/grid-column-stored-until.latte',
463 + [
464 + 'orderNumber' => $order->getNumber(),
465 + 'storedUntil' => $this->moduleHelper->getTranslatedStringFromDateTime( $order->getStoredUntil() ) ?? '',
466 + ]
467 + );
468 +
469 + break;
373 470 }
374 471 }
375 472
376 473 /**
@@ -380,21 +477,35 @@
380 477 *
381 478 * @return string[] All columns.
382 479 */
383 480 public function addOrderListColumns( array $columns ): array {
384 - $new_columns = array();
481 + $newColumns = array();
385 482
386 - foreach ( $columns as $column_name => $column_info ) {
387 - $new_columns[ $column_name ] = $column_info;
483 + foreach ( $columns as $columnName => $columnInfo ) {
484 + $newColumns[ $columnName ] = $columnInfo;
388 485
389 - if ( 'order_total' === $column_name ) {
390 - $new_columns['packetery_weight'] = __( 'Weight', 'packeta' );
391 - $new_columns['packetery'] = __( 'Packeta', 'packeta' );
392 - $new_columns['packetery_packet_id'] = __( 'Tracking No.', 'packeta' );
393 - $new_columns['packetery_packet_status'] = __( 'Packeta packet status', 'packeta' );
394 - $new_columns['packetery_destination'] = __( 'Pickup point or carrier', 'packeta' );
486 + if ( $columnName === 'order_total' ) {
487 + $newColumns['packetery_weight'] = __( 'Weight', 'packeta' );
488 + $newColumns['packetery'] = __( 'Packeta', 'packeta' );
489 + $newColumns['packetery_packet_id'] = __( 'Tracking No.', 'packeta' );
490 + $newColumns['packetery_packet_status'] = __( 'Packeta packet status', 'packeta' );
491 + $newColumns['packetery_packet_stored_until'] = __( 'Stored until', 'packeta' );
492 + $newColumns['packetery_destination'] = __( 'Pickup point or carrier', 'packeta' );
395 493 }
396 494 }
397 495
398 - return $new_columns;
496 + return $newColumns;
497 + }
498 +
499 + /**
500 + * Add order list sortable columns.
501 + *
502 + * @param string[] $columns Order list columns.
503 + *
504 + * @return string[] All columns.
505 + */
506 + public function makeOrderListSpecificColumnsSortable( array $columns ): array {
507 + $metaKey = 'packetery_packet_stored_until';
508 +
509 + return wp_parse_args( [ 'packetery_packet_stored_until' => $metaKey ], $columns );
399 510 }
400 511 }