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