| @@ -9,15 +9,23 @@ | ||
| 9 | 9 | |
| 10 | 10 | namespace Packetery\Module\Order; |
| 11 | 11 | |
| 12 | 12 | use Packetery\Core; |
| 13 | -use Packetery\Core\Entity; | |
| 14 | -use Packetery\Core\Helper; | |
| 15 | -use Packetery\Module\Carrier; | |
| 16 | -use PacketeryLatte\Engine; | |
| 17 | -use PacketeryNette\Http\Request; | |
| 13 | +use Packetery\Core\CoreHelper; | |
| 14 | +use Packetery\Latte\Engine; | |
| 15 | +use Packetery\Module\Carrier\CarrierOptionsFactory; | |
| 16 | +use Packetery\Module\ContextResolver; | |
| 17 | +use Packetery\Module\EntityFactory\SizeFactory; | |
| 18 | +use Packetery\Module\Exception\InvalidCarrierException; | |
| 19 | +use Packetery\Module\Framework\WpAdapter; | |
| 20 | +use Packetery\Module\Log\Purger; | |
| 21 | +use Packetery\Module\ModuleHelper; | |
| 18 | 22 | use Packetery\Module\Plugin; |
| 23 | +use Packetery\Nette\Http\Request; | |
| 24 | +use WC_Order; | |
| 19 | 25 | |
| 26 | +use function esc_html; | |
| 27 | + | |
| 20 | 28 | /** |
| 21 | 29 | * Class GridExtender. |
| 22 | 30 | * |
| 23 | 31 | * @package Packetery\Order |
| @@ -23,146 +31,153 @@ | ||
| 23 | 31 | * @package Packetery\Order |
| 24 | 32 | */ |
| 25 | 33 | class GridExtender { |
| 26 | 34 | |
| 27 | - 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'; | |
| 28 | 36 | |
| 29 | 37 | /** |
| 30 | - * Order entity cache. | |
| 38 | + * Generic CoreHelper. | |
| 31 | 39 | * |
| 32 | - * @var Entity\Order|null | |
| 40 | + * @var CoreHelper | |
| 33 | 41 | */ |
| 34 | - public static $orderCache; | |
| 42 | + private $coreHelper; | |
| 35 | 43 | |
| 36 | 44 | /** |
| 37 | - * Generic Helper. | |
| 45 | + * Latte Engine. | |
| 38 | 46 | * |
| 39 | - * @var Helper | |
| 47 | + * @var Engine | |
| 40 | 48 | */ |
| 41 | - private $helper; | |
| 49 | + private $latteEngine; | |
| 42 | 50 | |
| 43 | 51 | /** |
| 44 | - * Carrier repository. | |
| 52 | + * Http Request. | |
| 45 | 53 | * |
| 46 | - * @var Carrier\Repository | |
| 54 | + * @var Request | |
| 47 | 55 | */ |
| 48 | - private $carrierRepository; | |
| 56 | + private $httpRequest; | |
| 49 | 57 | |
| 50 | 58 | /** |
| 51 | - * Latte Engine. | |
| 59 | + * Order repository. | |
| 52 | 60 | * |
| 53 | - * @var Engine | |
| 61 | + * @var Repository | |
| 54 | 62 | */ |
| 55 | - private $latteEngine; | |
| 63 | + private $orderRepository; | |
| 56 | 64 | |
| 57 | 65 | /** |
| 58 | - * Http Request. | |
| 66 | + * Order Validator. | |
| 59 | 67 | * |
| 60 | - * @var Request | |
| 68 | + * @var Core\Validator\Order | |
| 61 | 69 | */ |
| 62 | - private $httpRequest; | |
| 70 | + private $orderValidator; | |
| 63 | 71 | |
| 64 | 72 | /** |
| 65 | - * Controller router. | |
| 73 | + * Context resolver. | |
| 66 | 74 | * |
| 67 | - * @var ControllerRouter | |
| 75 | + * @var ContextResolver | |
| 68 | 76 | */ |
| 69 | - private $orderControllerRouter; | |
| 77 | + private $contextResolver; | |
| 70 | 78 | |
| 71 | 79 | /** |
| 72 | - * Order repository. | |
| 80 | + * Carrier options factory. | |
| 73 | 81 | * |
| 74 | - * @var Repository | |
| 82 | + * @var CarrierOptionsFactory | |
| 75 | 83 | */ |
| 76 | - private $orderRepository; | |
| 84 | + private $carrierOptionsFactory; | |
| 77 | 85 | |
| 78 | 86 | /** |
| 79 | - * Modal. | |
| 87 | + * WordPress Adapter. | |
| 80 | 88 | * |
| 81 | - * @var Modal | |
| 89 | + * @var WpAdapter | |
| 82 | 90 | */ |
| 83 | - private $modal; | |
| 91 | + private $wpAdapter; | |
| 84 | 92 | |
| 85 | 93 | /** |
| 86 | - * Packet synchronizer. | |
| 94 | + * Module helper | |
| 87 | 95 | * |
| 88 | - * @var PacketSynchronizer | |
| 96 | + * @var ModuleHelper | |
| 89 | 97 | */ |
| 90 | - private $packetSynchronizer; | |
| 98 | + private $moduleHelper; | |
| 91 | 99 | |
| 92 | 100 | /** |
| 101 | + * @var SizeFactory | |
| 102 | + */ | |
| 103 | + private $sizeFactory; | |
| 104 | + | |
| 105 | + /** | |
| 93 | 106 | * GridExtender constructor. |
| 94 | 107 | * |
| 95 | - * @param Helper $helper Helper. | |
| 96 | - * @param Carrier\Repository $carrierRepository Carrier repository. | |
| 97 | - * @param Engine $latteEngine Latte Engine. | |
| 98 | - * @param Request $httpRequest Http Request. | |
| 99 | - * @param ControllerRouter $orderControllerRouter Order controller router. | |
| 100 | - * @param Repository $orderRepository Order repository. | |
| 101 | - * @param Modal $modal Modal dialog. | |
| 102 | - * @param PacketSynchronizer $packetSynchronizer Packet synchronizer. | |
| 108 | + * @param CoreHelper $coreHelper CoreHelper. | |
| 109 | + * @param Engine $latteEngine Latte Engine. | |
| 110 | + * @param Request $httpRequest Http Request. | |
| 111 | + * @param Repository $orderRepository Order repository. | |
| 112 | + * @param OrderValidatorFactory $orderValidatorFactory Order validator. | |
| 113 | + * @param ContextResolver $contextResolver Context resolver. | |
| 114 | + * @param CarrierOptionsFactory $carrierOptionsFactory Carrier options factory. | |
| 115 | + * @param WpAdapter $wpAdapter WordPress adapter. | |
| 116 | + * @param ModuleHelper $moduleHelper Module helper. | |
| 117 | + * @param SizeFactory $sizeFactory Size factory. | |
| 103 | 118 | */ |
| 104 | 119 | public function __construct( |
| 105 | - Helper $helper, | |
| 106 | - Carrier\Repository $carrierRepository, | |
| 120 | + CoreHelper $coreHelper, | |
| 107 | 121 | Engine $latteEngine, |
| 108 | 122 | Request $httpRequest, |
| 109 | - ControllerRouter $orderControllerRouter, | |
| 110 | 123 | Repository $orderRepository, |
| 111 | - Modal $modal, | |
| 112 | - PacketSynchronizer $packetSynchronizer | |
| 124 | + OrderValidatorFactory $orderValidatorFactory, | |
| 125 | + ContextResolver $contextResolver, | |
| 126 | + CarrierOptionsFactory $carrierOptionsFactory, | |
| 127 | + WpAdapter $wpAdapter, | |
| 128 | + ModuleHelper $moduleHelper, | |
| 129 | + SizeFactory $sizeFactory | |
| 113 | 130 | ) { |
| 114 | - $this->helper = $helper; | |
| 115 | - $this->carrierRepository = $carrierRepository; | |
| 131 | + $this->coreHelper = $coreHelper; | |
| 116 | 132 | $this->latteEngine = $latteEngine; |
| 117 | 133 | $this->httpRequest = $httpRequest; |
| 118 | - $this->orderControllerRouter = $orderControllerRouter; | |
| 119 | 134 | $this->orderRepository = $orderRepository; |
| 120 | - $this->modal = $modal; | |
| 121 | - $this->packetSynchronizer = $packetSynchronizer; | |
| 135 | + $this->orderValidator = $orderValidatorFactory->create(); | |
| 136 | + $this->contextResolver = $contextResolver; | |
| 137 | + $this->carrierOptionsFactory = $carrierOptionsFactory; | |
| 138 | + $this->wpAdapter = $wpAdapter; | |
| 139 | + $this->moduleHelper = $moduleHelper; | |
| 140 | + $this->sizeFactory = $sizeFactory; | |
| 122 | 141 | } |
| 123 | 142 | |
| 124 | 143 | /** |
| 125 | 144 | * Adds custom filtering links to order grid. |
| 126 | 145 | * |
| 127 | - * @param array $var Array of html links. | |
| 146 | + * @param string[] $htmlLinks Array of html links. | |
| 128 | 147 | * |
| 129 | - * @return array | |
| 148 | + * @return string[] | |
| 130 | 149 | */ |
| 131 | - public function addFilterLinks( array $var ): array { | |
| 150 | + public function addFilterLinks( array $htmlLinks ): array { | |
| 132 | 151 | $latteParams = [ |
| 133 | - 'link' => add_query_arg( | |
| 152 | + 'link' => ModuleHelper::getOrderGridUrl( | |
| 134 | 153 | [ |
| 135 | - 'post_type' => 'shop_order', | |
| 136 | 154 | 'filter_action' => 'packetery_filter_link', |
| 137 | 155 | 'packetery_to_submit' => '1', |
| 138 | 156 | 'packetery_to_print' => false, |
| 139 | - ], | |
| 140 | - admin_url( 'edit.php' ) | |
| 157 | + ] | |
| 141 | 158 | ), |
| 142 | 159 | 'title' => __( 'Packeta orders to submit', 'packeta' ), |
| 143 | 160 | 'orderCount' => $this->orderRepository->countOrdersToSubmit(), |
| 144 | 161 | 'active' => ( $this->httpRequest->getQuery( 'packetery_to_submit' ) === '1' ), |
| 145 | 162 | ]; |
| 146 | - $var[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams ); | |
| 163 | + $htmlLinks[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams ); | |
| 147 | 164 | |
| 148 | 165 | $latteParams = [ |
| 149 | - 'link' => add_query_arg( | |
| 166 | + 'link' => ModuleHelper::getOrderGridUrl( | |
| 150 | 167 | [ |
| 151 | - 'post_type' => 'shop_order', | |
| 152 | 168 | 'filter_action' => 'packetery_filter_link', |
| 153 | 169 | 'packetery_to_submit' => false, |
| 154 | 170 | 'packetery_to_print' => '1', |
| 155 | - ], | |
| 156 | - admin_url( 'edit.php' ) | |
| 171 | + ] | |
| 157 | 172 | ), |
| 158 | 173 | 'title' => __( 'Packeta orders to print', 'packeta' ), |
| 159 | 174 | 'orderCount' => $this->orderRepository->countOrdersToPrint(), |
| 160 | 175 | 'active' => ( $this->httpRequest->getQuery( 'packetery_to_print' ) === '1' ), |
| 161 | 176 | ]; |
| 162 | - $var[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams ); | |
| 177 | + $htmlLinks[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams ); | |
| 163 | 178 | |
| 164 | - return $var; | |
| 179 | + return $htmlLinks; | |
| 165 | 180 | } |
| 166 | 181 | |
| 167 | 182 | /** |
| 168 | 183 | * Adds select to order grid. |
| @@ -167,19 +182,19 @@ | ||
| 167 | 182 | /** |
| 168 | 183 | * Adds select to order grid. |
| 169 | 184 | */ |
| 170 | 185 | public function renderOrderTypeSelect(): void { |
| 171 | - if ( ! $this->isOrderGridPage() ) { | |
| 186 | + if ( $this->contextResolver->isOrderGridPage() === false ) { | |
| 172 | 187 | return; |
| 173 | 188 | } |
| 174 | 189 | |
| 175 | 190 | $linkFilters = []; |
| 176 | 191 | |
| 177 | - if ( null !== $this->httpRequest->getQuery( 'packetery_to_submit' ) ) { | |
| 192 | + if ( $this->httpRequest->getQuery( 'packetery_to_submit' ) !== null ) { | |
| 178 | 193 | $linkFilters['packetery_to_submit'] = '1'; |
| 179 | 194 | } |
| 180 | 195 | |
| 181 | - if ( null !== $this->httpRequest->getQuery( 'packetery_to_print' ) ) { | |
| 196 | + if ( $this->httpRequest->getQuery( 'packetery_to_print' ) !== null ) { | |
| 182 | 197 | $linkFilters['packetery_to_print'] = '1'; |
| 183 | 198 | } |
| 184 | 199 | |
| 185 | 200 | $this->latteEngine->render( |
| @@ -214,9 +229,9 @@ | ||
| 214 | 229 | * Gets weight cell content. |
| 215 | 230 | * |
| 216 | 231 | * @param Core\Entity\Order $order Order. |
| 217 | 232 | * |
| 218 | - * @return array | |
| 233 | + * @return array{orderNumber: null|string, weight: null|float} | |
| 219 | 234 | */ |
| 220 | 235 | private function getWeightCellContentParams( Core\Entity\Order $order ): array { |
| 221 | 236 | return [ |
| 222 | 237 | 'orderNumber' => $order->getNumber(), |
| @@ -224,24 +239,57 @@ | ||
| 224 | 239 | ]; |
| 225 | 240 | } |
| 226 | 241 | |
| 227 | 242 | /** |
| 243 | + * Get Order Entity from cache | |
| 244 | + * | |
| 245 | + * @param int $orderId Order ID. | |
| 246 | + * | |
| 247 | + * @return Core\Entity\Order|null | |
| 248 | + * @throws InvalidCarrierException InvalidCarrierException. | |
| 249 | + */ | |
| 250 | + private function getOrderByIdCached( int $orderId ): ?Core\Entity\Order { | |
| 251 | + static $ordersCache; | |
| 252 | + | |
| 253 | + if ( ! isset( $ordersCache[ $orderId ] ) ) { | |
| 254 | + $ordersCache[ $orderId ] = $this->orderRepository->getById( $orderId ); | |
| 255 | + } | |
| 256 | + | |
| 257 | + return $ordersCache[ $orderId ] ?? null; | |
| 258 | + } | |
| 259 | + | |
| 260 | + /** | |
| 228 | 261 | * Fills custom order list columns. |
| 229 | 262 | * |
| 230 | - * @param string $column Current order column name. | |
| 263 | + * @param string|mixed $column Current order column name. | |
| 264 | + * @param WC_Order|int|mixed $wcOrder WC Order. | |
| 231 | 265 | */ |
| 232 | - public function fillCustomOrderListColumns( string $column ): void { | |
| 233 | - global $post; | |
| 266 | + public function fillCustomOrderListColumns( $column, $wcOrder ): void { | |
| 267 | + if ( is_string( $column ) === false ) { | |
| 268 | + return; | |
| 269 | + } | |
| 234 | 270 | |
| 235 | - if ( ! isset( self::$orderCache ) || (int) self::$orderCache->getNumber() !== $post->ID ) { | |
| 236 | - self::$orderCache = $this->orderRepository->getById( $post->ID ); | |
| 237 | - if ( null === self::$orderCache ) { | |
| 238 | - return; | |
| 271 | + if ( $wcOrder instanceof WC_Order ) { | |
| 272 | + $orderId = $wcOrder->get_id(); | |
| 273 | + } elseif ( is_numeric( $wcOrder ) ) { | |
| 274 | + $orderId = (int) $wcOrder; | |
| 275 | + } else { | |
| 276 | + return; | |
| 277 | + } | |
| 278 | + | |
| 279 | + try { | |
| 280 | + $order = $this->getOrderByIdCached( $orderId ); | |
| 281 | + } catch ( InvalidCarrierException $exception ) { | |
| 282 | + if ( $column === 'packetery' ) { | |
| 283 | + echo esc_html( $exception->getMessage() ); | |
| 239 | 284 | } |
| 285 | + | |
| 286 | + return; | |
| 240 | 287 | } |
| 288 | + if ( $order === null ) { | |
| 289 | + return; | |
| 290 | + } | |
| 241 | 291 | |
| 242 | - $order = self::$orderCache; | |
| 243 | - | |
| 244 | 292 | switch ( $column ) { |
| 245 | 293 | case 'packetery_weight': |
| 246 | 294 | $this->latteEngine->render( |
| 247 | 295 | self::TEMPLATE_GRID_COLUMN_WEIGHT, |
| @@ -246,53 +294,85 @@ | ||
| 246 | 294 | $this->latteEngine->render( |
| 247 | 295 | self::TEMPLATE_GRID_COLUMN_WEIGHT, |
| 248 | 296 | $this->getWeightCellContentParams( $order ) |
| 249 | 297 | ); |
| 298 | + | |
| 250 | 299 | break; |
| 251 | 300 | case 'packetery_destination': |
| 252 | 301 | $pickupPoint = $order->getPickupPoint(); |
| 253 | - if ( null !== $pickupPoint ) { | |
| 254 | - $pointName = $pickupPoint->getName(); | |
| 255 | - $pointId = $pickupPoint->getId(); | |
| 256 | - $country = $order->getShippingCountry(); | |
| 257 | - $internalCountries = array_keys( $this->carrierRepository->getZpointCarriers() ); | |
| 258 | - if ( in_array( $country, $internalCountries, true ) ) { | |
| 302 | + if ( $pickupPoint !== null ) { | |
| 303 | + $pointName = $pickupPoint->getName(); | |
| 304 | + $pointId = $pickupPoint->getId(); | |
| 305 | + if ( ! $order->isExternalCarrier() ) { | |
| 259 | 306 | echo esc_html( "$pointName ($pointId)" ); |
| 260 | 307 | } else { |
| 261 | 308 | echo esc_html( $pointName ); |
| 262 | 309 | } |
| 310 | + | |
| 263 | 311 | break; |
| 264 | 312 | } |
| 265 | - $homeDeliveryCarrier = $this->carrierRepository->getById( (int) $order->getCarrierId() ); | |
| 266 | - if ( $homeDeliveryCarrier ) { | |
| 267 | - $homeDeliveryCarrierEntity = new Carrier\Entity( $homeDeliveryCarrier ); | |
| 268 | - echo esc_html( $homeDeliveryCarrierEntity->getFinalName() ); | |
| 269 | - } | |
| 313 | + | |
| 314 | + $homeDeliveryCarrierOptions = $this->carrierOptionsFactory->createByCarrierId( $order->getCarrier()->getId() ); | |
| 315 | + echo esc_html( $homeDeliveryCarrierOptions->getName() ); | |
| 316 | + | |
| 270 | 317 | break; |
| 271 | 318 | case 'packetery_packet_id': |
| 272 | - $packetId = $order->getPacketId(); | |
| 273 | - if ( $packetId ) { | |
| 274 | - echo '<a href="' . esc_attr( $this->helper->get_tracking_url( $packetId ) ) . '" target="_blank">Z' . esc_html( $packetId ) . '</a>'; | |
| 319 | + $packetId = $order->getPacketId(); | |
| 320 | + $packetClaimId = $order->getPacketClaimId(); | |
| 321 | + $latteParams = [ | |
| 322 | + 'order' => $order, | |
| 323 | + 'packetIdTrackingUrl' => null, | |
| 324 | + 'packetClaimIdTrackingUrl' => null, | |
| 325 | + 'translations' => [ | |
| 326 | + 'packetClaimTracking' => __( 'Packet claim tracking', 'packeta' ), | |
| 327 | + ], | |
| 328 | + ]; | |
| 329 | + | |
| 330 | + if ( $packetId !== null ) { | |
| 331 | + $latteParams['packetIdTrackingUrl'] = $this->coreHelper->getTrackingUrl( $packetId ); | |
| 275 | 332 | } |
| 333 | + | |
| 334 | + if ( $packetClaimId !== null ) { | |
| 335 | + $latteParams['packetClaimIdTrackingUrl'] = $this->coreHelper->getTrackingUrl( $packetClaimId ); | |
| 336 | + } | |
| 337 | + | |
| 338 | + $this->latteEngine->render( | |
| 339 | + PACKETERY_PLUGIN_DIR . '/template/order/grid-column-tracking.latte', | |
| 340 | + $latteParams | |
| 341 | + ); | |
| 342 | + | |
| 276 | 343 | break; |
| 277 | 344 | case 'packetery': |
| 278 | - $packetSubmitUrl = add_query_arg( [], $this->orderControllerRouter->getRouteUrl( Controller::PATH_SUBMIT_TO_API ) ); | |
| 279 | - $printLink = add_query_arg( | |
| 345 | + $encodedOrderGridParams = rawurlencode( $this->httpRequest->getUrl()->getQuery() ); | |
| 346 | + $packetSubmitUrl = add_query_arg( | |
| 280 | 347 | [ |
| 348 | + PacketActionsCommonLogic::PARAM_ORDER_ID => $order->getNumber(), | |
| 349 | + Plugin::PARAM_PACKETERY_ACTION => PacketActionsCommonLogic::ACTION_SUBMIT_PACKET, | |
| 350 | + PacketActionsCommonLogic::PARAM_REDIRECT_TO => PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID, | |
| 351 | + Plugin::PARAM_NONCE => wp_create_nonce( PacketActionsCommonLogic::createNonceAction( PacketActionsCommonLogic::ACTION_SUBMIT_PACKET, $order->getNumber() ) ), | |
| 352 | + PacketActionsCommonLogic::PARAM_ORDER_GRID_PARAMS => $encodedOrderGridParams, | |
| 353 | + ], | |
| 354 | + admin_url( 'admin.php' ) | |
| 355 | + ); | |
| 356 | + $printLink = add_query_arg( | |
| 357 | + [ | |
| 281 | 358 | 'page' => LabelPrint::MENU_SLUG, |
| 282 | 359 | LabelPrint::LABEL_TYPE_PARAM => ( $order->isExternalCarrier() ? LabelPrint::ACTION_CARRIER_LABELS : LabelPrint::ACTION_PACKETA_LABELS ), |
| 283 | 360 | 'id' => $order->getNumber(), |
| 284 | 361 | 'packet_id' => $order->getPacketId(), |
| 285 | 362 | 'offset' => 0, |
| 363 | + PacketActionsCommonLogic::PARAM_ORDER_GRID_PARAMS => $encodedOrderGridParams, | |
| 286 | 364 | ], |
| 287 | 365 | admin_url( 'admin.php' ) |
| 288 | 366 | ); |
| 289 | - $packetCancelLink = add_query_arg( | |
| 367 | + $packetCancelLink = add_query_arg( | |
| 290 | 368 | [ |
| 291 | - PacketCanceller::PARAM_ORDER_ID => $order->getNumber(), | |
| 292 | - Plugin::PARAM_PACKETERY_ACTION => PacketCanceller::ACTION_CANCEL_PACKET, | |
| 293 | - PacketCanceller::PARAM_REDIRECT_TO => PacketCanceller::REDIRECT_TO_ORDER_GRID, | |
| 294 | - Plugin::PARAM_NONCE => wp_create_nonce( PacketCanceller::createNonceAction( PacketCanceller::ACTION_CANCEL_PACKET, $order->getNumber() ) ), | |
| 369 | + PacketActionsCommonLogic::PARAM_ORDER_ID => $order->getNumber(), | |
| 370 | + PacketActionsCommonLogic::PARAM_PACKET_ID => $order->getPacketId(), | |
| 371 | + Plugin::PARAM_PACKETERY_ACTION => PacketActionsCommonLogic::ACTION_CANCEL_PACKET, | |
| 372 | + PacketActionsCommonLogic::PARAM_REDIRECT_TO => PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID, | |
| 373 | + Plugin::PARAM_NONCE => wp_create_nonce( PacketActionsCommonLogic::createNonceAction( PacketActionsCommonLogic::ACTION_CANCEL_PACKET, $order->getNumber() ) ), | |
| 374 | + PacketActionsCommonLogic::PARAM_ORDER_GRID_PARAMS => $encodedOrderGridParams, | |
| 295 | 375 | ], |
| 296 | 376 | admin_url( 'admin.php' ) |
| 297 | 377 | ); |
| 298 | 378 | |
| @@ -298,30 +378,53 @@ | ||
| 298 | 378 | |
| 299 | 379 | $this->latteEngine->render( |
| 300 | 380 | PACKETERY_PLUGIN_DIR . '/template/order/grid-column-packetery.latte', |
| 301 | 381 | [ |
| 302 | - 'order' => $order, | |
| 303 | - 'showWarningIcon' => $this->modal->showWarningIcon( $order ), | |
| 304 | - 'packetSubmitUrl' => $packetSubmitUrl, | |
| 305 | - 'packetCancelLink' => $packetCancelLink, | |
| 306 | - 'restNonce' => wp_create_nonce( 'wp_rest' ), | |
| 307 | - 'printLink' => $printLink, | |
| 308 | - 'translations' => [ | |
| 309 | - 'printLabel' => __( 'Print label', 'packeta' ), | |
| 310 | - 'setAdditionalPacketInfo' => __( 'Set additional packet information', 'packeta' ), | |
| 311 | - 'submitToPacketa' => __( 'Submit to packeta', 'packeta' ), | |
| 382 | + 'order' => $order, | |
| 383 | + 'dimensions' => $this->sizeFactory->createSizeInSetDimensionUnit( $order ), | |
| 384 | + 'orderIsSubmittable' => $this->orderValidator->isValid( $order ), | |
| 385 | + 'isPossibleExtendPacketPickUpDate' => $order->isPossibleExtendPacketPickUpDate(), | |
| 386 | + 'storedUntil' => $this->coreHelper->getStringFromDateTime( $order->getStoredUntil(), CoreHelper::DATEPICKER_FORMAT ), | |
| 387 | + 'orderWarningFields' => Form::getInvalidFieldsFromValidationResult( $this->orderValidator->validate( $order ) ), | |
| 388 | + 'packetSubmitUrl' => $packetSubmitUrl, | |
| 389 | + 'packetCancelLink' => $packetCancelLink, | |
| 390 | + 'printLink' => $printLink, | |
| 391 | + 'helper' => $this->coreHelper, | |
| 392 | + 'datePickerFormat' => CoreHelper::DATEPICKER_FORMAT, | |
| 393 | + 'logPurgerDatetimeModifier' => $this->wpAdapter->getOption( Purger::PURGER_OPTION_NAME, Purger::PURGER_MODIFIER_DEFAULT ), | |
| 394 | + 'packetDeliverOn' => $this->coreHelper->getStringFromDateTime( $order->getDeliverOn(), CoreHelper::DATEPICKER_FORMAT ), | |
| 395 | + 'translations' => [ | |
| 396 | + 'printLabel' => __( 'Print label', 'packeta' ), | |
| 397 | + 'setAdditionalPacketInfo' => __( 'Set additional packet information', 'packeta' ), | |
| 398 | + 'setStoredUntil' => __( 'Set the pickup date extension', 'packeta' ), | |
| 399 | + 'packetSubmissionNotPossible' => __( 'It is not possible to submit the shipment because all the information required for this shipment is not filled.', 'packeta' ), | |
| 400 | + 'submitToPacketa' => __( 'Submit to Packeta', 'packeta' ), | |
| 312 | 401 | // translators: %s: Order number. |
| 313 | - 'reallyCancelPacketHeading' => sprintf( __( 'Order #%s', 'packeta' ), $order->getCustomNumber() ), | |
| 402 | + 'reallyCancelPacketHeading' => sprintf( __( 'Order #%s', 'packeta' ), $order->getCustomNumber() ), | |
| 314 | 403 | // translators: %s: Packet number. |
| 315 | - 'reallyCancelPacket' => sprintf( __( 'Do you really wish to cancel parcel number %s?', 'packeta' ), (string) $order->getPacketId() ), | |
| 316 | - 'cancelPacket' => __( 'Cancel packet', 'packeta' ), | |
| 404 | + 'reallyCancelPacket' => sprintf( __( 'Do you really wish to cancel parcel number %s?', 'packeta' ), (string) $order->getPacketId() ), | |
| 405 | + | |
| 406 | + 'cancelPacket' => __( 'Cancel packet', 'packeta' ), | |
| 407 | + 'lastErrorFromApi' => __( 'Last error from Packeta API', 'packeta' ), | |
| 317 | 408 | ], |
| 318 | 409 | ] |
| 319 | 410 | ); |
| 411 | + | |
| 320 | 412 | break; |
| 321 | 413 | case 'packetery_packet_status': |
| 322 | - echo esc_html( $this->packetSynchronizer->getPacketStatusTranslated( $order->getPacketStatus() ) ); | |
| 414 | + echo esc_html( PacketStatusResolver::getTranslatedName( $order->getPacketStatus() ) ); | |
| 415 | + | |
| 323 | 416 | break; |
| 417 | + case 'packetery_packet_stored_until': | |
| 418 | + $this->latteEngine->render( | |
| 419 | + PACKETERY_PLUGIN_DIR . '/template/order/grid-column-stored-until.latte', | |
| 420 | + [ | |
| 421 | + 'orderNumber' => $order->getNumber(), | |
| 422 | + 'storedUntil' => $this->moduleHelper->getTranslatedStringFromDateTime( $order->getStoredUntil() ) ?? '', | |
| 423 | + ] | |
| 424 | + ); | |
| 425 | + | |
| 426 | + break; | |
| 324 | 427 | } |
| 325 | 428 | } |
| 326 | 429 | |
| 327 | 430 | /** |
| @@ -331,32 +434,35 @@ | ||
| 331 | 434 | * |
| 332 | 435 | * @return string[] All columns. |
| 333 | 436 | */ |
| 334 | 437 | public function addOrderListColumns( array $columns ): array { |
| 335 | - $new_columns = array(); | |
| 438 | + $newColumns = array(); | |
| 336 | 439 | |
| 337 | - foreach ( $columns as $column_name => $column_info ) { | |
| 338 | - $new_columns[ $column_name ] = $column_info; | |
| 440 | + foreach ( $columns as $columnName => $columnInfo ) { | |
| 441 | + $newColumns[ $columnName ] = $columnInfo; | |
| 339 | 442 | |
| 340 | - if ( 'order_total' === $column_name ) { | |
| 341 | - $new_columns['packetery_weight'] = __( 'Weight', 'packeta' ); | |
| 342 | - $new_columns['packetery_packet_status'] = __( 'Packeta packet status', 'packeta' ); | |
| 343 | - $new_columns['packetery'] = __( 'Packeta', 'packeta' ); | |
| 344 | - $new_columns['packetery_packet_id'] = __( 'Barcode', 'packeta' ); | |
| 345 | - $new_columns['packetery_destination'] = __( 'Pick up point or carrier', 'packeta' ); | |
| 443 | + if ( $columnName === 'order_total' ) { | |
| 444 | + $newColumns['packetery_weight'] = __( 'Weight', 'packeta' ); | |
| 445 | + $newColumns['packetery'] = __( 'Packeta', 'packeta' ); | |
| 446 | + $newColumns['packetery_packet_id'] = __( 'Tracking No.', 'packeta' ); | |
| 447 | + $newColumns['packetery_packet_status'] = __( 'Packeta packet status', 'packeta' ); | |
| 448 | + $newColumns['packetery_packet_stored_until'] = __( 'Stored until', 'packeta' ); | |
| 449 | + $newColumns['packetery_destination'] = __( 'Pickup point or carrier', 'packeta' ); | |
| 346 | 450 | } |
| 347 | 451 | } |
| 348 | 452 | |
| 349 | - return $new_columns; | |
| 453 | + return $newColumns; | |
| 350 | 454 | } |
| 351 | 455 | |
| 352 | 456 | /** |
| 353 | - * Checks if current admin page is order grid using WP globals. | |
| 457 | + * Add order list sortable columns. | |
| 354 | 458 | * |
| 355 | - * @return bool | |
| 459 | + * @param string[] $columns Order list columns. | |
| 460 | + * | |
| 461 | + * @return string[] All columns. | |
| 356 | 462 | */ |
| 357 | - public function isOrderGridPage(): bool { | |
| 358 | - global $pagenow, $typenow; | |
| 463 | + public function makeOrderListSpecificColumnsSortable( array $columns ): array { | |
| 464 | + $metaKey = 'packetery_packet_stored_until'; | |
| 359 | 465 | |
| 360 | - return ( 'edit.php' === $pagenow && 'shop_order' === $typenow ); | |
| 466 | + return wp_parse_args( [ 'packetery_packet_stored_until' => $metaKey ], $columns ); | |
| 361 | 467 | } |
| 362 | 468 | } |