PluginProbe
Packeta / 1.4
Packeta v1.4
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 / Plugin.php

Plugin.php in Packeta 1.4, at src/Packetery/Module/Plugin.php

856 lines 24.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main Packeta plugin class.
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module;
11
12 use Packetery\Core\Log\ILogger;
13 use Packetery\Module\Carrier\OptionsPage;
14 use Packetery\Module\Log;
15 use Packetery\Module\Options;
16 use Packetery\Module\Order;
17 use Packetery\Module\Product;
18 use PacketeryLatte\Engine;
19 use PacketeryNette\Http\Request;
20 use PacketeryNette\Utils\Html;
21 use WC_Email;
22 use WC_Order;
23
24 /**
25 * Class Plugin
26 *
27 * @package Packetery
28 */
29 class Plugin {
30
31 public const VERSION = '1.4';
32 public const DOMAIN = 'packeta';
33 public const MIN_LISTENER_PRIORITY = - 9998;
34 public const PARAM_PACKETERY_ACTION = 'packetery_action';
35 public const PARAM_NONCE = '_wpnonce';
36
37 /**
38 * Options page.
39 *
40 * @var Options\Page Options page,
41 */
42 private $options_page;
43
44 /**
45 * PacketeryLatte engine.
46 *
47 * @var Engine
48 */
49 private $latte_engine;
50
51 /**
52 * Dashboard widget.
53 *
54 * @var DashboardWidget
55 */
56 private $dashboardWidget;
57
58 /**
59 * Country options page.
60 *
61 * @var OptionsPage
62 */
63 private $carrierOptionsPage;
64
65 /**
66 * Path to main plugin file.
67 *
68 * @var string Path to main plugin file.
69 */
70 private $main_file_path;
71
72 /**
73 * Admin edit order page Packeta metabox.
74 *
75 * @var Order\Metabox
76 */
77 private $order_metabox;
78
79 /**
80 * Message manager.
81 *
82 * @var MessageManager
83 */
84 private $message_manager;
85
86 /**
87 * Checkout object.
88 *
89 * @var Checkout
90 */
91 private $checkout;
92
93 /**
94 * Order BulkActions.
95 *
96 * @var Order\BulkActions
97 */
98 private $orderBulkActions;
99
100 /**
101 * Label printing.
102 *
103 * @var Order\LabelPrint
104 */
105 private $labelPrint;
106
107 /**
108 * Order collection printing.
109 *
110 * @var Order\CollectionPrint
111 */
112 private $orderCollectionPrint;
113
114 /**
115 * Order grid extender.
116 *
117 * @var Order\GridExtender
118 */
119 private $gridExtender;
120
121 /**
122 * Product tab.
123 *
124 * @var Product\DataTab
125 */
126 private $productTab;
127
128 /**
129 * Log page.
130 *
131 * @var Log\Page
132 */
133 private $logPage;
134
135 /**
136 * Log manager.
137 *
138 * @var ILogger
139 */
140 private $logger;
141
142 /**
143 * Cron service.
144 *
145 * @var CronService
146 */
147 private $cronService;
148
149 /**
150 * Order controller.
151 *
152 * @var Order\Controller
153 */
154 private $orderController;
155
156 /**
157 * Order modal.
158 *
159 * @var Order\Modal
160 */
161 private $orderModal;
162
163 /**
164 * Options exporter.
165 *
166 * @var Options\Exporter
167 */
168 private $exporter;
169
170 /**
171 * Request.
172 *
173 * @var Request
174 */
175 private $request;
176
177 /**
178 * Order repository.
179 *
180 * @var Order\Repository
181 */
182 private $orderRepository;
183
184 /**
185 * Plugin upgrade.
186 *
187 * @var Upgrade
188 */
189 private $upgrade;
190
191 /**
192 * QueryProcessor.
193 *
194 * @var QueryProcessor
195 */
196 private $queryProcessor;
197
198 /**
199 * Options provider.
200 *
201 * @var Options\Provider
202 */
203 private $optionsProvider;
204
205 /**
206 * Packet canceller.
207 *
208 * @var Order\PacketCanceller
209 */
210 private $packetCanceller;
211
212 /**
213 * Context resolver.
214 *
215 * @var ContextResolver
216 */
217 private $contextResolver;
218
219 /**
220 * Plugin constructor.
221 *
222 * @param Order\Metabox $order_metabox Order metabox.
223 * @param MessageManager $message_manager Message manager.
224 * @param Options\Page $options_page Options page.
225 * @param Checkout $checkout Checkout class.
226 * @param Engine $latte_engine PacketeryLatte engine.
227 * @param OptionsPage $carrierOptionsPage Carrier options page.
228 * @param Order\BulkActions $orderBulkActions Order BulkActions.
229 * @param Order\LabelPrint $labelPrint Label printing.
230 * @param Order\GridExtender $gridExtender Order grid extender.
231 * @param Product\DataTab $productTab Product tab.
232 * @param Log\Page $logPage Log page.
233 * @param ILogger $logger Log manager.
234 * @param Order\Controller $orderController Order controller.
235 * @param Order\Modal $orderModal Order modal.
236 * @param Options\Exporter $exporter Options exporter.
237 * @param Order\CollectionPrint $orderCollectionPrint Order collection print.
238 * @param Request $request HTTP request.
239 * @param Order\Repository $orderRepository Order repository.
240 * @param Upgrade $upgrade Plugin upgrade.
241 * @param QueryProcessor $queryProcessor QueryProcessor.
242 * @param Options\Provider $optionsProvider Options provider.
243 * @param CronService $cronService Cron service.
244 * @param Order\PacketCanceller $packetCanceller Packet canceller.
245 * @param ContextResolver $contextResolver Context resolver.
246 * @param DashboardWidget $dashboardWidget Dashboard widget.
247 */
248 public function __construct(
249 Order\Metabox $order_metabox,
250 MessageManager $message_manager,
251 Options\Page $options_page,
252 Checkout $checkout,
253 Engine $latte_engine,
254 OptionsPage $carrierOptionsPage,
255 Order\BulkActions $orderBulkActions,
256 Order\LabelPrint $labelPrint,
257 Order\GridExtender $gridExtender,
258 Product\DataTab $productTab,
259 Log\Page $logPage,
260 ILogger $logger,
261 Order\Controller $orderController,
262 Order\Modal $orderModal,
263 Options\Exporter $exporter,
264 Order\CollectionPrint $orderCollectionPrint,
265 Request $request,
266 Order\Repository $orderRepository,
267 Upgrade $upgrade,
268 QueryProcessor $queryProcessor,
269 Options\Provider $optionsProvider,
270 CronService $cronService,
271 Order\PacketCanceller $packetCanceller,
272 ContextResolver $contextResolver,
273 DashboardWidget $dashboardWidget
274 ) {
275 $this->options_page = $options_page;
276 $this->latte_engine = $latte_engine;
277 $this->main_file_path = PACKETERY_PLUGIN_DIR . '/packeta.php';
278 $this->order_metabox = $order_metabox;
279 $this->message_manager = $message_manager;
280 $this->checkout = $checkout;
281 $this->carrierOptionsPage = $carrierOptionsPage;
282 $this->orderBulkActions = $orderBulkActions;
283 $this->labelPrint = $labelPrint;
284 $this->gridExtender = $gridExtender;
285 $this->productTab = $productTab;
286 $this->logPage = $logPage;
287 $this->logger = $logger;
288 $this->orderController = $orderController;
289 $this->orderModal = $orderModal;
290 $this->exporter = $exporter;
291 $this->orderCollectionPrint = $orderCollectionPrint;
292 $this->request = $request;
293 $this->orderRepository = $orderRepository;
294 $this->upgrade = $upgrade;
295 $this->queryProcessor = $queryProcessor;
296 $this->optionsProvider = $optionsProvider;
297 $this->cronService = $cronService;
298 $this->packetCanceller = $packetCanceller;
299 $this->contextResolver = $contextResolver;
300 $this->dashboardWidget = $dashboardWidget;
301 }
302
303 /**
304 * Method to register hooks
305 */
306 public function run(): void {
307 add_action( 'init', [ $this, 'loadTranslation' ] );
308
309 if ( ! self::isWooCommercePluginActive() ) {
310 add_action( 'admin_notices', [ $this, 'echoInactiveWooCommerceNotice' ] );
311
312 return;
313 }
314
315 add_action( 'init', [ $this->upgrade, 'check' ] );
316 add_action( 'init', [ $this->logger, 'register' ] );
317 add_action( 'init', [ $this->message_manager, 'init' ] );
318 add_action( 'rest_api_init', [ $this->orderController, 'registerRoutes' ] );
319
320 add_action( 'admin_enqueue_scripts', array( $this, 'enqueueAdminAssets' ) );
321 add_action( 'wp_enqueue_scripts', [ $this, 'enqueueFrontAssets' ] );
322
323 add_action(
324 'admin_notices',
325 function () {
326 $this->message_manager->render( MessageManager::RENDERER_WORDPRESS );
327 },
328 self::MIN_LISTENER_PRIORITY
329 );
330 add_action( 'init', array( $this, 'init' ) );
331
332 // TODO: deactivation_hook.
333 register_deactivation_hook(
334 $this->main_file_path,
335 static function () {
336 CronService::deactivate();
337 }
338 );
339
340 register_uninstall_hook( $this->main_file_path, array( __CLASS__, 'uninstall' ) );
341
342 add_action( 'woocommerce_email_footer', [ $this, 'renderEmailFooter' ] );
343 add_filter( 'woocommerce_shipping_methods', array( $this, 'add_shipping_method' ) );
344
345 add_filter( 'views_edit-shop_order', [ $this->gridExtender, 'addFilterLinks' ] );
346 add_action( 'restrict_manage_posts', [ $this->gridExtender, 'renderOrderTypeSelect' ] );
347 $this->queryProcessor->register();
348
349 add_filter( 'manage_edit-shop_order_columns', [ $this->gridExtender, 'addOrderListColumns' ] );
350 add_action( 'manage_shop_order_posts_custom_column', [ $this->gridExtender, 'fillCustomOrderListColumns' ] );
351
352 add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', [ $this->upgrade, 'handleCustomQueryVar' ], 10, 2 );
353
354 add_action( 'admin_menu', array( $this, 'add_menu_pages' ) );
355 add_action( 'admin_head', array( $this->labelPrint, 'hideFromMenus' ) );
356 add_action( 'admin_head', array( $this->orderCollectionPrint, 'hideFromMenus' ) );
357 add_action( 'admin_head', [ $this, 'renderConfirmModalTemplate' ] );
358 $this->orderModal->register();
359 $this->order_metabox->register();
360
361 $this->checkout->register_hooks();
362 $this->productTab->register();
363 $this->cronService->register();
364
365 add_action( 'woocommerce_admin_order_data_after_shipping_address', [ $this, 'renderDeliveryDetail' ] );
366 add_action( 'woocommerce_order_details_after_order_table', [ $this, 'renderOrderDetail' ] );
367
368 // Adding custom actions to dropdown in admin order list.
369 add_filter( 'bulk_actions-edit-shop_order', [ $this->orderBulkActions, 'addActions' ], 20, 1 );
370 // Execute the action for selected orders.
371 add_filter(
372 'handle_bulk_actions-edit-shop_order',
373 [
374 $this->orderBulkActions,
375 'handleActions',
376 ],
377 10,
378 3
379 );
380 // Print packets export result.
381 add_action( 'admin_notices', [ $this->orderBulkActions, 'renderPacketsExportResult' ], self::MIN_LISTENER_PRIORITY );
382
383 add_action( 'admin_init', [ $this->labelPrint, 'outputLabelsPdf' ] );
384 add_action( 'admin_init', [ $this->orderCollectionPrint, 'print' ] );
385
386 add_action( 'admin_init', [ $this->exporter, 'outputExportTxt' ] );
387 add_action( 'admin_init', [ $this->packetCanceller, 'processActions' ] );
388 add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', [ $this, 'transformGetOrdersQuery' ] );
389
390 add_action( 'deleted_post', [ $this->orderRepository, 'deletedPostHook' ], 10, 2 );
391 $this->dashboardWidget->register();
392 }
393
394 /**
395 * Print inactive WooCommerce notice.
396 *
397 * @return void
398 */
399 public function echoInactiveWooCommerceNotice(): void {
400 if ( self::isWooCommercePluginActive() ) {
401 // When Packeta plugin is active and WooCommerce plugin is inactive.
402 // If user decides to activate WooCommerce plugin then invalid notice will not be rendered.
403 // Packeta plugin probably bootstraps twice in such case.
404 return;
405 }
406
407 $this->latte_engine->render(
408 PACKETERY_PLUGIN_DIR . '/template/admin-notice.latte',
409 [
410 'message' => [
411 'type' => 'error',
412 'message' => __( 'Packeta plugin requires WooCommerce. Please install and activate it first.', 'packeta' ),
413 ],
414 ]
415 );
416 }
417
418 /**
419 * Is WC plugin active.
420 *
421 * @return bool
422 */
423 private static function isWooCommercePluginActive(): bool {
424 return Helper::isPluginActive( 'woocommerce/woocommerce.php' );
425 }
426
427 /**
428 * Creates HTML link parts in array.
429 *
430 * @param string $href Href.
431 *
432 * @return string[]
433 */
434 public static function createLinkParts( string $href ): array {
435 $link = Html::el( 'a' )->href( $href );
436 return [ $link->startTag(), $link->endTag() ];
437 }
438
439 /**
440 * Filter queries.
441 *
442 * @param array $query Query.
443 *
444 * @return array
445 */
446 public function transformGetOrdersQuery( array $query ): array {
447 if ( ! empty( $query['packetery_meta_query'] ) ) {
448 // @codingStandardsIgnoreStart
449 $query['meta_query'] = $query['packetery_meta_query'];
450 // @codingStandardsIgnoreEnd
451 }
452
453 return $query;
454 }
455
456 /**
457 * Renders delivery detail for packetery orders.
458 *
459 * @param WC_Order $order WordPress order.
460 */
461 public function renderDeliveryDetail( WC_Order $order ): void {
462 $orderEntity = $this->orderRepository->getByWcOrder( $order );
463 if ( null === $orderEntity ) {
464 return;
465 }
466
467 $carrierId = $orderEntity->getCarrierId();
468 $carrierOptions = Carrier\Options::createByCarrierId( $carrierId );
469
470 $this->latte_engine->render(
471 PACKETERY_PLUGIN_DIR . '/template/order/delivery-detail.latte',
472 [
473 'pickupPoint' => $orderEntity->getPickupPoint(),
474 'validatedDeliveryAddress' => $orderEntity->getValidatedDeliveryAddress(),
475 'carrierAddressValidation' => $carrierOptions->getAddressValidation(),
476 'translations' => [
477 'packeta' => __( 'Packeta', 'packeta' ),
478 'pickupPointDetail' => __( 'Pickup Point Detail', 'packeta' ),
479 'name' => __( 'Name', 'packeta' ),
480 'address' => __( 'Address', 'packeta' ),
481 'pickupPointDetailCaps' => __( 'Pickup Point Detail', 'packeta' ),
482 'addressWasNotValidated' => __( 'Address was not validated', 'packeta' ),
483 'validatedAddress' => __( 'Validated address', 'packeta' ),
484 'street' => __( 'Street', 'packeta' ),
485 'houseNumber' => __( 'House number', 'packeta' ),
486 'city' => __( 'City', 'packeta' ),
487 'zip' => __( 'Zip', 'packeta' ),
488 'county' => __( 'County', 'packeta' ),
489 'gps' => __( 'GPS', 'packeta' ),
490 ],
491 ]
492 );
493 }
494
495 /**
496 * Renders delivery detail for packetery orders, on "thank you" page and in frontend detail.
497 *
498 * @param WC_Order $wcOrder WordPress order.
499 */
500 public function renderOrderDetail( WC_Order $wcOrder ): void {
501 $order = $this->orderRepository->getById( $wcOrder->get_id() );
502 if ( null === $order ) {
503 return;
504 }
505
506 $pickupPoint = $order->getPickupPoint();
507 $validatedDeliveryAddress = $order->getValidatedDeliveryAddress();
508 if ( null === $pickupPoint && null === $validatedDeliveryAddress ) {
509 return;
510 }
511
512 $this->latte_engine->render(
513 PACKETERY_PLUGIN_DIR . '/template/order/detail.latte',
514 [
515 'displayPickupPointInfo' => $this->shouldDisplayPickupPointInfo(),
516 'pickupPoint' => $pickupPoint,
517 'validatedDeliveryAddress' => $validatedDeliveryAddress,
518 'translations' => [
519 'packeta' => __( 'Packeta', 'packeta' ),
520 'selectedPickupPoint' => __( 'Selected pickup point', 'packeta' ),
521 'pickupPointName' => __( 'Pickup Point Name', 'packeta' ),
522 'pickupPointDetail' => __( 'Pickup Point Detail', 'packeta' ),
523 'validatedAddress' => __( 'Validated address', 'packeta' ),
524 'address' => __( 'Address', 'packeta' ),
525 ],
526 ]
527 );
528 }
529
530 /**
531 * Renders email footer.
532 *
533 * @param mixed $email Email data.
534 */
535 public function renderEmailFooter( $email ): void {
536 if ( ! $email instanceof WC_Email || ! $email->object instanceof WC_Order ) {
537 return;
538 }
539
540 $packeteryOrder = $this->orderRepository->getByWcOrder( $email->object );
541 if ( null === $packeteryOrder ) {
542 return;
543 }
544
545 $pickupPoint = $packeteryOrder->getPickupPoint();
546 $validatedDeliveryAddress = $packeteryOrder->getValidatedDeliveryAddress();
547
548 $this->latte_engine->render(
549 PACKETERY_PLUGIN_DIR . '/template/email/footer.latte',
550 [
551 'displayPickupPointInfo' => $this->shouldDisplayPickupPointInfo(),
552 'pickupPoint' => $pickupPoint,
553 'validatedDeliveryAddress' => $validatedDeliveryAddress,
554 'translations' => [
555 'packeta' => __( 'Packeta', 'packeta' ),
556 'pickupPointDetail' => __( 'Pickup Point Detail', 'packeta' ),
557 'pickupPointName' => __( 'Pickup Point Name', 'packeta' ),
558 'link' => __( 'Link', 'packeta' ),
559 'pickupPointAddress' => __( 'Pickup Point Address', 'packeta' ),
560 'validatedDeliveryAddress' => __( 'validated delivery address', 'packeta' ),
561 'street' => __( 'Street', 'packeta' ),
562 'houseNumber' => __( 'House number', 'packeta' ),
563 'city' => __( 'City', 'packeta' ),
564 'zip' => __( 'Zip', 'packeta' ),
565 'county' => __( 'County', 'packeta' ),
566 ],
567 ]
568 );
569 }
570
571 /**
572 * Tells if pickup point info should be displayed.
573 *
574 * @return bool
575 */
576 public function shouldDisplayPickupPointInfo(): bool {
577 return ! $this->optionsProvider->replaceShippingAddressWithPickupPointAddress() || wc_ship_to_billing_address_only();
578 }
579
580 /**
581 * Renders confirm modal template.
582 */
583 public function renderConfirmModalTemplate(): void {
584 if ( $this->contextResolver->isPacketeryConfirmPage() ) {
585 $this->latte_engine->render(
586 PACKETERY_PLUGIN_DIR . '/template/confirm-modal-template.latte',
587 [
588 'translations' => [
589 'closeModalPanel' => __( 'Close modal panel', 'packeta' ),
590 'no' => __( 'No', 'packeta' ),
591 'yes' => __( 'Yes', 'packeta' ),
592 ],
593 ]
594 );
595 }
596 }
597
598 /**
599 * Enqueues admin JS file.
600 *
601 * @param string $name Name of script.
602 * @param string $file Relative file path.
603 * @param bool $inFooter Tells where to include script.
604 * @param array $deps Script dependencies.
605 */
606 private function enqueueScript( string $name, string $file, bool $inFooter, array $deps = [] ): void {
607 wp_enqueue_script(
608 $name,
609 plugin_dir_url( $this->main_file_path ) . $file,
610 $deps,
611 md5( (string) filemtime( PACKETERY_PLUGIN_DIR . '/' . $file ) ),
612 $inFooter
613 );
614 }
615
616 /**
617 * Enqueues CSS file.
618 *
619 * @param string $name Name of script.
620 * @param string $file Relative file path.
621 */
622 private function enqueueStyle( string $name, string $file ): void {
623 wp_enqueue_style(
624 $name,
625 plugin_dir_url( $this->main_file_path ) . $file,
626 [],
627 md5( (string) filemtime( PACKETERY_PLUGIN_DIR . '/' . $file ) )
628 );
629 }
630
631 /**
632 * Builds asset URL.
633 *
634 * @param string $asset Relative asset path without leading slash.
635 *
636 * @return string
637 */
638 public static function buildAssetUrl( string $asset ): string {
639 $url = plugin_dir_url( PACKETERY_PLUGIN_DIR . '/packeta.php' ) . $asset;
640 $filename = PACKETERY_PLUGIN_DIR . '/' . $asset;
641
642 return add_query_arg( [ 'v' => md5( (string) filemtime( $filename ) ) ], $url );
643 }
644
645 /**
646 * Enqueues javascript files and stylesheets for checkout.
647 */
648 public function enqueueFrontAssets(): void {
649 if ( is_checkout() ) {
650 $this->enqueueStyle( 'packetery-front-styles', 'public/front.css' );
651 $this->enqueueStyle( 'packetery-custom-front-styles', 'public/custom-front.css' );
652 $this->enqueueScript( 'packetery-checkout', 'public/checkout.js', true, [ 'jquery' ] );
653 wp_localize_script( 'packetery-checkout', 'packeteryCheckoutSettings', $this->checkout->createSettings() );
654 }
655 }
656
657 /**
658 * Enqueues javascript files and stylesheets for administration.
659 */
660 public function enqueueAdminAssets(): void {
661 $page = $this->request->getQuery( 'page' );
662 $isOrderGridPage = $this->contextResolver->isOrderGridPage();
663 $isOrderDetailPage = $this->contextResolver->isOrderDetailPage();
664
665 if ( $isOrderGridPage || $isOrderDetailPage || in_array( $page, [ Carrier\OptionsPage::SLUG, Options\Page::SLUG ], true ) ) {
666 $this->enqueueScript( 'live-form-validation-options', 'public/live-form-validation-options.js', false );
667 $this->enqueueScript( 'live-form-validation', 'public/libs/live-form-validation/live-form-validation.js', false, [ 'live-form-validation-options' ] );
668 $this->enqueueScript( 'live-form-validation-extension', 'public/live-form-validation-extension.js', false, [ 'live-form-validation' ] );
669 }
670
671 if ( Carrier\OptionsPage::SLUG === $page ) {
672 $this->enqueueScript( 'packetery-admin-country-carrier', 'public/admin-country-carrier.js', true, [ 'jquery' ] );
673 }
674
675 $isProductDetailPage = $this->contextResolver->isProductDetailPage();
676
677 if ( $isOrderGridPage || $isOrderDetailPage || $isProductDetailPage || in_array( $page, [ Options\Page::SLUG, Carrier\OptionsPage::SLUG, Log\Page::SLUG, Order\labelPrint::MENU_SLUG ], true ) ) {
678 $this->enqueueStyle( 'packetery-admin-styles', 'public/admin.css' );
679 }
680
681 if ( $isOrderGridPage ) {
682 $this->enqueueScript( 'packetery-admin-grid-order-edit-js', 'public/admin-grid-order-edit.js', true, [ 'jquery', 'wp-util', 'backbone' ] );
683 }
684
685 if ( $isOrderDetailPage ) {
686 $this->enqueueScript( 'packetery-admin-pickup-point-picker', 'public/admin-pickup-point-picker.js', false, [ 'jquery' ] );
687 }
688
689 if ( $this->contextResolver->isPacketeryConfirmPage() ) {
690 $this->enqueueScript( 'packetery-confirm', 'public/confirm.js', true, [ 'jquery', 'backbone' ] );
691 }
692 }
693
694 /**
695 * Add links to left admin menu.
696 */
697 public function add_menu_pages(): void {
698 $this->options_page->register();
699 $this->carrierOptionsPage->register();
700 $this->labelPrint->register();
701 $this->orderCollectionPrint->register();
702 $this->logPage->register();
703 }
704
705 /**
706 * Gets current locale.
707 */
708 private function getLocale(): string {
709 /**
710 * Applies plugin_locale filters.
711 *
712 * @since 1.0.0
713 */
714 return apply_filters(
715 'plugin_locale',
716 ( is_admin() ? get_user_locale() : get_locale() ),
717 self::DOMAIN
718 );
719 }
720
721 /**
722 * Loads plugin translation file by user locale.
723 */
724 public function loadTranslation(): void {
725 $domain = self::DOMAIN;
726 $locale = $this->getLocale();
727
728 $moFile = PACKETERY_PLUGIN_DIR . "/languages/$domain-$locale.mo";
729 $result = load_textdomain( $domain, $moFile );
730
731 if ( false === $result ) {
732 $moFile = PACKETERY_PLUGIN_DIR . "/languages/$domain-en_US.mo";
733 load_textdomain( $domain, $moFile );
734 }
735 }
736
737 /**
738 * Inits plugin.
739 */
740 public function init(): void {
741 add_filter(
742 'plugin_action_links_' . plugin_basename( $this->main_file_path ),
743 [
744 $this,
745 'addPluginActionLinks',
746 ]
747 );
748 add_filter( 'plugin_row_meta', [ $this, 'addPluginRowMeta' ], 10, 2 );
749 }
750
751 /**
752 * Uninstalls plugin and drops custom database table.
753 * Only a static class method or function can be used in an uninstall hook.
754 */
755 public static function uninstall(): void {
756 if ( defined( 'PACKETERY_DEBUG' ) && PACKETERY_DEBUG === true ) {
757 return;
758 }
759
760 $container = require PACKETERY_PLUGIN_DIR . '/bootstrap.php';
761
762 $optionsRepository = $container->getByType( Options\Repository::class );
763 $pluginOptions = $optionsRepository->getPluginOptions();
764 foreach ( $pluginOptions as $option ) {
765 delete_option( $option->option_name );
766 }
767
768 $logRepository = $container->getByType( Log\Repository::class );
769 $logRepository->drop();
770
771 $carrierRepository = $container->getByType( Carrier\Repository::class );
772 $carrierRepository->drop();
773
774 $orderRepository = $container->getByType( Order\Repository::class );
775 $orderRepository->drop();
776 }
777
778 /**
779 * Adds action links visible at the plugin screen.
780 *
781 * @link https://developer.wordpress.org/reference/hooks/plugin_action_links_plugin_file/
782 *
783 * @param array $links Plugin Action links.
784 *
785 * @return array
786 */
787 public function addPluginActionLinks( array $links ): array {
788 $settingsLink = '<a href="' . esc_url( admin_url( 'admin.php?page=' . Options\Page::SLUG ) ) . '" aria-label="' .
789 esc_attr__( 'View the plugin documentation', 'packeta' ) . '">' .
790 esc_html__( 'Settings', 'packeta' ) . '</a>';
791
792 array_unshift( $links, $settingsLink );
793
794 return $links;
795 }
796
797 /**
798 * Show row meta on the plugin screen.
799 *
800 * @link https://developer.wordpress.org/reference/hooks/plugin_row_meta/
801 *
802 * @param array $links Plugin Row Meta.
803 * @param string $pluginFileName Plugin Base file.
804 *
805 * @return array
806 */
807 public function addPluginRowMeta( array $links, string $pluginFileName ): array {
808 if ( ! strpos( $pluginFileName, basename( $this->main_file_path ) ) ) {
809 return $links;
810 }
811 $links[] = '<a href="' . esc_url( 'https://github.com/Zasilkovna/WooCommerce/wiki' ) . '" aria-label="' .
812 esc_attr__( 'View Packeta documentation', 'packeta' ) . '">' .
813 esc_html__( 'Documentation', 'packeta' ) . '</a>';
814
815 return $links;
816 }
817
818 /**
819 * Adds Packeta method to available shipping methods.
820 *
821 * @param array $methods Previous state.
822 *
823 * @return array
824 */
825 public function add_shipping_method( array $methods ): array {
826 $methods[ ShippingMethod::PACKETERY_METHOD_ID ] = ShippingMethod::class;
827
828 return $methods;
829 }
830
831 /**
832 * Hides submenu item. Must not be called too early.
833 *
834 * @param string $itemSlug Item slug.
835 */
836 public static function hideSubmenuItem( string $itemSlug ): void {
837 global $submenu;
838 if ( isset( $submenu[ Options\Page::SLUG ] ) ) {
839 foreach ( $submenu[ Options\Page::SLUG ] as $key => $menu ) {
840 if ( $itemSlug === $menu[2] ) {
841 unset( $submenu[ Options\Page::SLUG ][ $key ] );
842 }
843 }
844 }
845 }
846
847 /**
848 * Gets software identity for Packeta APIs.
849 *
850 * @return string
851 */
852 public static function getAppIdentity(): string {
853 return 'Woocommerce: ' . get_bloginfo( 'version' ) . ', WordPress: ' . WC_VERSION . ', plugin Packeta: ' . self::VERSION;
854 }
855 }
856