PluginProbe
Packeta / 1.4.2
Packeta v1.4.2
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 / Options / Page.php

Page.php in Packeta 1.4.2, at src/Packetery/Module/Options/Page.php

653 lines 22.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Page
4 *
5 * @package Packetery\Options
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Options;
11
12 use Packetery\Core\Api\Soap\Request\SenderGetReturnRouting;
13 use Packetery\Core\Log;
14 use Packetery\Module\FormFactory;
15 use Packetery\Module\MessageManager;
16 use Packetery\Module\Order\PacketSynchronizer;
17 use PacketeryLatte\Engine;
18 use PacketeryNette\Forms\Form;
19
20 /**
21 * Class Page
22 *
23 * @package Packetery\Options
24 */
25 class Page {
26
27 private const FORM_FIELDS_CONTAINER = 'packetery';
28 private const FORM_FIELD_PACKETA_LABEL_FORMAT = 'packeta_label_format';
29 private const FORM_FIELD_CARRIER_LABEL_FORMAT = 'carrier_label_format';
30
31 public const ACTION_VALIDATE_SENDER = 'validate-sender';
32
33 public const SLUG = 'packeta-options';
34
35 public const TAB_GENERAL = 'general';
36 public const TAB_SUPPORT = 'support';
37 public const TAB_PACKET_STATUS_SYNC = 'packet-status-sync';
38
39 public const PARAM_TAB = 'tab';
40
41 /**
42 * PacketeryLatte_engine.
43 *
44 * @var Engine PacketeryLatte engine.
45 */
46 private $latte_engine;
47
48 /**
49 * Options Provider
50 *
51 * @var Provider
52 */
53 private $optionsProvider;
54
55 /**
56 * Form factory.
57 *
58 * @var FormFactory Form factory.
59 */
60 private $formFactory;
61
62 /**
63 * Packeta client.
64 *
65 * @var \Packetery\Core\Api\Soap\Client
66 */
67 private $packetaClient;
68
69 /**
70 * Logger
71 *
72 * @var Log\ILogger
73 */
74 private $logger;
75
76 /**
77 * Message manager.
78 *
79 * @var MessageManager
80 */
81 private $messageManager;
82
83 /**
84 * HTTP request.
85 *
86 * @var \PacketeryNette\Http\Request
87 */
88 private $httpRequest;
89
90 /**
91 * Packet synchronizer.
92 *
93 * @var PacketSynchronizer
94 */
95 private $packetSynchronizer;
96
97 /**
98 * Plugin constructor.
99 *
100 * @param Engine $latte_engine PacketeryLatte_engine.
101 * @param Provider $optionsProvider Options provider.
102 * @param FormFactory $formFactory Form factory.
103 * @param \Packetery\Core\Api\Soap\Client $packetaClient Packeta Client.
104 * @param Log\ILogger $logger Logger.
105 * @param MessageManager $messageManager Message manager.
106 * @param \PacketeryNette\Http\Request $httpRequest HTTP request.
107 * @param PacketSynchronizer $packetSynchronizer Packet synchronizer.
108 */
109 public function __construct( Engine $latte_engine, Provider $optionsProvider, FormFactory $formFactory, \Packetery\Core\Api\Soap\Client $packetaClient, Log\ILogger $logger, MessageManager $messageManager, \PacketeryNette\Http\Request $httpRequest, PacketSynchronizer $packetSynchronizer ) {
110 $this->latte_engine = $latte_engine;
111 $this->optionsProvider = $optionsProvider;
112 $this->formFactory = $formFactory;
113 $this->packetaClient = $packetaClient;
114 $this->logger = $logger;
115 $this->messageManager = $messageManager;
116 $this->httpRequest = $httpRequest;
117 $this->packetSynchronizer = $packetSynchronizer;
118 }
119
120 /**
121 * Registers WP callbacks.
122 */
123 public function register(): void {
124 add_action( 'admin_init', array( $this, 'admin_init' ) );
125 $icon = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDI1LjEuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IlZyc3R2YV8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCIKCSB2aWV3Qm94PSIwIDAgMzcgNDAiIHN0eWxlPSJmaWxsOiNhN2FhYWQiIHhtbDpzcGFjZT0icHJlc2VydmUiPgo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPgoJLnN0MHtmaWxsLXJ1bGU6ZXZlbm9kZDtjbGlwLXJ1bGU6ZXZlbm9kZDtmaWxsOiAjYTdhYWFkO30KPC9zdHlsZT4KPHBhdGggY2xhc3M9InN0MCIgZD0iTTE5LjQsMTYuMWwtMC45LDAuNGwtMC45LTAuNGwtMTMtNi41bDYuMi0yLjRsMTMuNCw2LjVMMTkuNCwxNi4xeiBNMzIuNSw5LjZsLTQuNywyLjNsLTEzLjUtNmw0LjItMS42CglMMzIuNSw5LjZ6Ii8+CjxwYXRoIGNsYXNzPSJzdDAiIGQ9Ik0xOSwwbDE3LjIsNi42bC0yLjQsMS45bC0xNS4yLTZMMy4yLDguNkwwLjgsNi42TDE4LDBMMTksMEwxOSwweiBNMzQuMSw5LjFsMi44LTEuMWwtMi4xLDE3LjZsLTAuNCwwLjgKCUwxOS40LDQwbC0wLjUtMy4xbDEzLjQtMTJMMzQuMSw5LjF6IE0yLjUsMjYuNWwtMC40LTAuOEwwLDguMWwyLjgsMS4xbDEuOSwxNS43bDEzLjQsMTJMMTcuNiw0MEwyLjUsMjYuNXoiLz4KPHBhdGggY2xhc3M9InN0MCIgZD0iTTI4LjIsMTIuNGw0LjMtMi43bC0xLjcsMTQuMkwxOC42LDM1bDAuNi0xN2w1LjQtMy4zTDI0LjMsMjNsMy4zLTIuM0wyOC4yLDEyLjR6Ii8+CjxwYXRoIGNsYXNzPSJzdDAiIGQ9Ik0xNy43LDE3LjlsMC42LDE3bC0xMi4yLTExTDQuNCw5LjhMMTcuNywxNy45eiIvPgo8L3N2Zz4K';
126 add_menu_page(
127 __( 'Packeta', 'packeta' ),
128 __( 'Packeta', 'packeta' ),
129 'manage_options',
130 self::SLUG,
131 '',
132 $icon
133 );
134 add_submenu_page(
135 self::SLUG,
136 __( 'Settings', 'packeta' ),
137 __( 'Settings', 'packeta' ),
138 'manage_options',
139 self::SLUG,
140 array(
141 $this,
142 'render',
143 ),
144 1
145 );
146 add_action( 'admin_init', [ $this, 'processActions' ] );
147
148 add_filter( 'custom_menu_order', '__return_true' );
149 add_filter( 'menu_order', [ $this, 'customMenuOrder' ] );
150 }
151
152 /**
153 * Returns menu_order with Packeta item in last position.
154 *
155 * @param array $menuOrder WP $menu_order.
156 *
157 * @return array
158 */
159 public function customMenuOrder( array $menuOrder ): array {
160 $currentPosition = array_search( self::SLUG, $menuOrder, true );
161
162 if ( false !== $currentPosition ) {
163 unset( $menuOrder[ $currentPosition ] );
164 $menuOrder[] = self::SLUG;
165 }
166
167 return $menuOrder;
168 }
169
170 /**
171 * Get chosen keys by hashed choice data.
172 *
173 * @param array $choiceData Choice data.
174 * @param array $chosenHashedFormOrderStatuses Picked items.
175 *
176 * @return array
177 */
178 private function getChosenKeys( array $choiceData, array $chosenHashedFormOrderStatuses ): array {
179 $statuses = [];
180 foreach ( $chosenHashedFormOrderStatuses as $hash => $chosen ) {
181 if ( $chosen ) {
182 $statuses[] = $choiceData[ $hash ]['key'];
183 }
184 }
185
186 return $statuses;
187 }
188
189 /**
190 * Gets all packet statuses.
191 *
192 * @return array
193 */
194 public function getPacketStatusesChoiceData(): array {
195 $statuses = PacketSynchronizer::getPacketStatuses();
196
197 $result = [];
198
199 foreach ( $statuses as $status => $defaultValue ) {
200 $result[ md5( $status ) ] = [
201 'key' => $status,
202 'default' => $defaultValue,
203 'label' => $this->packetSynchronizer->getPacketStatusTranslated( $status ),
204 ];
205 }
206
207 return $result;
208 }
209
210 /**
211 * Gets order statuses for form.
212 *
213 * @return array
214 */
215 public static function getOrderStatusesChoiceData(): array {
216 $orderStatuses = wc_get_order_statuses();
217 $orderStatusesTransformed = [];
218
219 foreach ( $orderStatuses as $orderStatusKey => $orderStatusLabel ) {
220 $orderStatusesTransformed[ md5( $orderStatusKey ) ] = [
221 'key' => $orderStatusKey,
222 'label' => $orderStatusLabel,
223 ];
224 }
225
226 return $orderStatusesTransformed;
227 }
228
229 /**
230 * Creates settings form.
231 *
232 * @return Form
233 */
234 private function createPacketStatusSyncForm(): Form {
235 $form = $this->formFactory->create( 'packetery_packet_status_sync_form' );
236 $settings = $this->optionsProvider->data_to_array( Provider::OPTION_NAME_PACKETERY_SYNC );
237
238 $form->addText( 'max_status_syncing_packets', __( 'Number of orders synced during one cron call', 'packeta' ) )
239 ->setRequired( false )
240 ->addRule( Form::INTEGER )
241 ->addRule( Form::MIN, null, 0 )
242 ->setDefaultValue( Provider::MAX_STATUS_SYNCING_PACKETS_DEFAULT );
243
244 $form->addText( 'max_days_of_packet_status_syncing', __( 'Number of days for which the order status is checked', 'packeta' ) )
245 ->setRequired( false )
246 ->addRule( Form::INTEGER )
247 ->addRule( Form::MIN, null, 0 )
248 ->setDefaultValue( Provider::MAX_DAYS_OF_PACKET_STATUS_SYNCING_DEFAULT );
249
250 $orderStatusesTransformed = self::getOrderStatusesChoiceData();
251 $orderStatusesContainer = $form->addContainer( 'status_syncing_order_statuses' );
252
253 foreach ( $orderStatusesTransformed as $orderStatusKeyHash => $orderStatusData ) {
254 $item = $orderStatusesContainer->addCheckbox( $orderStatusKeyHash, $orderStatusData['label'] );
255 if ( isset( $settings['status_syncing_order_statuses'] ) && in_array( $orderStatusData['key'], $settings['status_syncing_order_statuses'], true ) ) {
256 $item->setDefaultValue( true );
257 }
258 }
259 unset( $settings['status_syncing_order_statuses'] );
260
261 $packetStatuses = $this->getPacketStatusesChoiceData();
262 $packetStatusesContainer = $form->addContainer( 'status_syncing_packet_statuses' );
263
264 foreach ( $packetStatuses as $packetStatusHash => $packetStatusData ) {
265 $item = $packetStatusesContainer->addCheckbox( $packetStatusHash, $packetStatusData['label'] );
266 if ( ! isset( $settings['status_syncing_packet_statuses'] ) ) {
267 $item->setDefaultValue( $packetStatusData['default'] );
268 } elseif ( in_array( $packetStatusData['key'], $settings['status_syncing_packet_statuses'], true ) ) {
269 $item->setDefaultValue( true );
270 }
271 }
272 unset( $settings['status_syncing_packet_statuses'] );
273
274 $form->setDefaults( $settings );
275
276 $form->addSubmit( 'save', __( 'Save changes', 'packeta' ) );
277
278 $form->onSuccess[] = [ $this, 'onPacketStatusSyncFormSuccess' ];
279
280 return $form;
281 }
282
283 /**
284 * On packet status sync form success.
285 *
286 * @param Form $form Form.
287 * @param array $values Values.
288 *
289 * @return void
290 */
291 public function onPacketStatusSyncFormSuccess( Form $form, array $values ): void {
292
293 $values['status_syncing_order_statuses'] = $this->getChosenKeys(
294 self::getOrderStatusesChoiceData(),
295 $values['status_syncing_order_statuses']
296 );
297 $values['status_syncing_packet_statuses'] = $this->getChosenKeys(
298 $this->getPacketStatusesChoiceData(),
299 $values['status_syncing_packet_statuses']
300 );
301
302 if ( '' === $values['max_status_syncing_packets'] ) {
303 unset( $values['max_status_syncing_packets'] );
304 }
305
306 if ( '' === $values['max_days_of_packet_status_syncing'] ) {
307 unset( $values['max_days_of_packet_status_syncing'] );
308 }
309
310 update_option( Provider::OPTION_NAME_PACKETERY_SYNC, $values );
311
312 $this->messageManager->flash_message( __( 'Settings saved.', 'packeta' ), MessageManager::TYPE_SUCCESS, MessageManager::RENDERER_PACKETERY, 'plugin-options' );
313
314 if ( wp_safe_redirect( $this->createLink( self::TAB_PACKET_STATUS_SYNC ) ) ) {
315 exit;
316 }
317 }
318
319 /**
320 * Creates settings form.
321 *
322 * @return Form
323 */
324 public function create_form(): Form {
325 $form = $this->formFactory->create();
326 $form->setAction( 'options.php' );
327
328 $container = $form->addContainer( self::FORM_FIELDS_CONTAINER );
329 $container->addText( 'api_password', __( 'API password', 'packeta' ) )
330 ->setRequired()
331 ->addRule( $form::PATTERN, __( 'API password must be 32 characters long and must contain valid characters!', 'packeta' ), '[a-z\d]{32}' );
332 $container->addText( 'sender', __( 'Sender', 'packeta' ) )
333 ->setRequired();
334
335 $packetaLabelFormats = $this->optionsProvider->getPacketaLabelFormats();
336 $container->addSelect(
337 self::FORM_FIELD_PACKETA_LABEL_FORMAT,
338 __( 'Packeta Label Format', 'packeta' ),
339 $packetaLabelFormats
340 )->checkDefaultValue( false )->setDefaultValue( Provider::DEFAULT_VALUE_PACKETA_LABEL_FORMAT );
341
342 $carrierLabelFormats = $this->optionsProvider->getCarrierLabelFormat();
343 $container->addSelect(
344 self::FORM_FIELD_CARRIER_LABEL_FORMAT,
345 __( 'Carrier Label Format', 'packeta' ),
346 $carrierLabelFormats
347 )->checkDefaultValue( false )->setDefaultValue( Provider::DEFAULT_VALUE_CARRIER_LABEL_FORMAT );
348
349 $gateways = $this->getAvailablePaymentGateways();
350 $enabledGateways = [];
351 foreach ( $gateways as $gateway ) {
352 $enabledGateways[ $gateway->id ] = $gateway->get_method_title();
353 }
354 $container->addSelect(
355 'cod_payment_method',
356 __( 'Payment method that represents cash on delivery', 'packeta' ),
357 $enabledGateways
358 )->setPrompt( '--' )->checkDefaultValue( false );
359
360 $container->addText( 'packaging_weight', __( 'Weight of packaging material', 'packeta' ) . ' (kg)' )
361 ->setRequired( true )
362 ->addRule( Form::FLOAT )
363 ->addRule( Form::MIN, null, 0 )
364 ->setDefaultValue( 0 );
365
366 // TODO: Packet status sync.
367
368 $container->addCheckbox( 'replace_shipping_address_with_pickup_point_address', __( 'Replace shipping address with pickup point address', 'packeta' ) )
369 ->setRequired( false );
370
371 $container->addSelect(
372 'checkout_widget_button_location',
373 __( 'Widget button location in checkout', 'packeta' ),
374 [
375 'after_shipping_rate' => __( 'After shipping rate', 'packeta' ),
376 'after_transport_methods' => __( 'After transport methods', 'packeta' ),
377 ]
378 );
379
380 $container->addCheckbox( 'force_packet_cancel', __( 'Force order cancellation', 'packeta' ) )
381 ->setRequired( false )
382 ->setDefaultValue( Provider::FORCE_PACKET_CANCEL_DEFAULT );
383
384 if ( $this->optionsProvider->has_any( Provider::OPTION_NAME_PACKETERY ) ) {
385 $container->setDefaults( $this->optionsProvider->data_to_array( Provider::OPTION_NAME_PACKETERY ) );
386 }
387
388 return $form;
389 }
390
391 /**
392 * Get available gateways.
393 *
394 * @return \WC_Payment_Gateway[]
395 */
396 public function getAvailablePaymentGateways(): array {
397 $availableGateways = [];
398
399 foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) {
400 if ( 'yes' === $gateway->enabled ) {
401 $availableGateways[ $gateway->id ] = $gateway;
402 }
403 }
404
405 return array_filter( $availableGateways, [ $this, 'filterValidGatewayClass' ] );
406 }
407
408 /**
409 * Callback for array filter. Returns true if gateway is of correct type.
410 *
411 * @param object $gateway Gateway to check.
412 * @return bool
413 */
414 protected function filterValidGatewayClass( object $gateway ): bool {
415 return ( $gateway instanceof \WC_Payment_Gateway );
416 }
417
418 /**
419 * Admin_init callback.
420 */
421 public function admin_init(): void {
422 register_setting( self::FORM_FIELDS_CONTAINER, self::FORM_FIELDS_CONTAINER, array( $this, 'options_validate' ) );
423 add_settings_section( 'packetery_main', __( 'Main Settings', 'packeta' ), '', self::SLUG );
424 }
425
426 /**
427 * Validates options.
428 *
429 * @param array $options Packetery_options.
430 *
431 * @return array
432 */
433 public function options_validate( array $options ): array {
434 $form = $this->create_form();
435 $form[ self::FORM_FIELDS_CONTAINER ]->setValues( $options );
436 if ( $form->isValid() === false ) {
437 foreach ( $form[ self::FORM_FIELDS_CONTAINER ]->getControls() as $control ) {
438 if ( $control->hasErrors() === false ) {
439 continue;
440 }
441
442 add_settings_error( $control->getCaption(), esc_attr( $control->getName() ), $control->getError() );
443 $options[ $control->getName() ] = '';
444 }
445 }
446
447 $api_password = $form[ self::FORM_FIELDS_CONTAINER ]['api_password'];
448 if ( $api_password->hasErrors() === false ) {
449 $api_pass = $api_password->getValue();
450 $options['api_key'] = substr( $api_pass, 0, 16 );
451 $this->packetaClient->setApiPassword( $api_pass );
452 } else {
453 $options['api_key'] = '';
454 }
455
456 $this->validateSender( $options['sender'] );
457 $options['force_packet_cancel'] = (int) $form[ self::FORM_FIELDS_CONTAINER ]['force_packet_cancel']->getValue();
458
459 return $options;
460 }
461
462 /**
463 * Validates sender.
464 *
465 * @param string $senderLabel Sender lbel.
466 *
467 * @return bool|null
468 */
469 private function validateSender( string $senderLabel ): ?bool {
470 $senderValidationRequest = new SenderGetReturnRouting( $senderLabel );
471 $senderValidationResponse = $this->packetaClient->senderGetReturnRouting( $senderValidationRequest );
472
473 $senderValidationLog = new Log\Record();
474 $senderValidationLog->action = Log\Record::ACTION_SENDER_VALIDATION;
475
476 $senderValidationLog->status = Log\Record::STATUS_SUCCESS;
477 $senderValidationLog->title = __( 'Sender validation was successful', 'packeta' );
478
479 if ( $senderValidationResponse->hasFault() ) {
480 $senderValidationLog->status = Log\Record::STATUS_ERROR;
481 $senderValidationLog->params = [
482 'errorMessage' => $senderValidationResponse->getFaultString(),
483 ];
484 $senderValidationLog->title = __( 'Sender could not be validated', 'packeta' );
485 }
486
487 $this->logger->add( $senderValidationLog );
488
489 $senderExists = $senderValidationResponse->senderExists();
490
491 if ( false === $senderExists ) {
492 $this->messageManager->flash_message( __( 'Specified sender does not exist', 'packeta' ), MessageManager::TYPE_INFO, MessageManager::RENDERER_PACKETERY, 'plugin-options' );
493 }
494
495 if ( null === $senderExists ) {
496 $this->messageManager->flash_message( __( 'Unable to check specified sender', 'packeta' ), MessageManager::TYPE_INFO, MessageManager::RENDERER_PACKETERY, 'plugin-options' );
497 }
498
499 return $senderExists;
500 }
501
502 /**
503 * Process actions.
504 *
505 * @return void
506 */
507 public function processActions(): void {
508 $action = $this->httpRequest->getQuery( 'action' );
509 if ( self::ACTION_VALIDATE_SENDER === $action ) {
510 $result = $this->validateSender( $this->optionsProvider->get_sender() );
511
512 if ( true === $result ) {
513 $this->messageManager->flash_message( __( 'Specified sender has been validated.', 'packeta' ), MessageManager::TYPE_SUCCESS, MessageManager::RENDERER_PACKETERY, 'plugin-options' );
514 }
515
516 $doRedirect = wp_safe_redirect(
517 add_query_arg(
518 [
519 'page' => self::SLUG,
520 ],
521 get_admin_url( null, 'admin.php' )
522 )
523 );
524
525 if ( $doRedirect ) {
526 exit;
527 }
528 }
529
530 $packetStatusSyncForm = $this->createPacketStatusSyncForm();
531 if ( $packetStatusSyncForm['save']->isSubmittedBy() ) {
532 $packetStatusSyncForm->fireEvents();
533 }
534 }
535
536 /**
537 * Renders page.
538 */
539 public function render(): void {
540 $activeTab = ( $this->httpRequest->getQuery( self::PARAM_TAB ) ?? self::TAB_GENERAL );
541
542 $latteParams = [];
543 if ( self::TAB_PACKET_STATUS_SYNC === $activeTab ) {
544 $latteParams = [ 'form' => $this->createPacketStatusSyncForm() ];
545 } elseif ( self::TAB_GENERAL === $activeTab ) {
546 $latteParams = [ 'form' => $this->create_form() ];
547 }
548
549 if ( ! extension_loaded( 'soap' ) ) {
550 $latteParams['error'] = __( 'This plugin requires an active SOAP library for proper operation. Contact your web hosting administrator.', 'packeta' );
551 }
552
553 $latteParams['apiPasswordLink'] = trim( $this->latte_engine->renderToString( PACKETERY_PLUGIN_DIR . '/template/options/help-block-link.latte', [ 'href' => 'https://client.packeta.com/support' ] ) );
554
555 $latteParams['exportLink'] = add_query_arg(
556 [
557 'page' => self::SLUG,
558 'action' => Exporter::ACTION_EXPORT_SETTINGS,
559 ],
560 get_admin_url( null, 'admin.php' )
561 );
562
563 $latteParams['activeTab'] = ( $this->httpRequest->getQuery( self::PARAM_TAB ) ?? self::TAB_GENERAL );
564 $latteParams['generalTabLink'] = $this->createLink();
565 $latteParams['supportTabLink'] = $this->createLink( self::TAB_SUPPORT );
566 $latteParams['packetStatusSyncTabLink'] = $this->createLink( self::TAB_PACKET_STATUS_SYNC );
567
568 $latteParams['canValidateSender'] = (bool) $this->optionsProvider->get_sender();
569 $latteParams['senderValidationLink'] = add_query_arg(
570 [
571 'page' => self::SLUG,
572 'action' => self::ACTION_VALIDATE_SENDER,
573 ],
574 get_admin_url( null, 'admin.php' )
575 );
576
577 $lastExport = null;
578 $lastExportOption = get_option( Exporter::OPTION_LAST_SETTINGS_EXPORT );
579 if ( false !== $lastExportOption ) {
580 $date = \DateTime::createFromFormat( DATE_ATOM, $lastExportOption );
581 if ( false !== $date ) {
582 $date->setTimezone( wp_timezone() );
583 $lastExport = $date->format( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
584 }
585 }
586 if ( $lastExport ) {
587 $latteParams['lastExport'] = $lastExport;
588 }
589
590 $latteParams['forcePacketCancelDescription'] = __( 'Cancel the order even if the cancellation in the Packeta system will be unsuccessful', 'packeta' );
591 $latteParams['messages'] = $this->messageManager->renderToString( MessageManager::RENDERER_PACKETERY, 'plugin-options' );
592 $latteParams['translations'] = [
593 'packeta' => __( 'Packeta', 'packeta' ),
594 'options' => __( 'Options', 'packeta' ),
595 'general' => __( 'General', 'packeta' ),
596 // translators: %s represents URL, keep intact.
597 'apiPasswordCanBeFoundAt%sUrl' => __( 'API password can be found at %s', 'packeta' ),
598 'saveChanges' => __( 'Save changes', 'packeta' ),
599 'validateSender' => __( 'Validate sender', 'packeta' ),
600 'support' => __( 'Support', 'packeta' ),
601 'optionsExportInfo1' => __(
602 'By clicking the button, you will export the settings of your plugin into a separate file. The export does not contain any sensitive information about your e-shop. Please send the resulting file to the technical support of Packeta (you can find the e-mail address here:',
603 'packeta'
604 ),
605 'optionsExportInfo2' => __(
606 ') along with the description of your problem. For a better understanding of your problem, we recommend adding screenshots, which show the problem (if possible).',
607 'packeta'
608 ),
609 'exportPluginSettings' => __( 'Export the plugin settings', 'packeta' ),
610 'settingsExportDatetime' => __( 'Date and time of the last export of settings', 'packeta' ),
611 'settingsNotYetExported' => __( 'The settings have not been exported yet.', 'packeta' ),
612 'senderDescription' => sprintf(
613 /* translators: 1: emphasis start 2: emphasis end 3: client section link start 4: client section link end */
614 esc_html__( 'Fill here %1$ssender label%2$s - you will find it in %3$sclient section%4$s - user information - field \'Indication\'.', 'packeta' ),
615 '<strong>',
616 '</strong>',
617 '<a href="https://client.packeta.com/senders" target="_blank">',
618 '</a>'
619 ),
620 'packagingWeightDescription' => __( 'This parameter is used to determine the weight of the packaging material. This value is automatically added to the total weight of each order that contains products with non-zero weight. This value is also taken into account when evaluating the weight rules in the cart.', 'packeta' ),
621 'packetStatusSyncTabLinkLabel' => __( 'Packet status tracking', 'packeta' ),
622 'statusSyncingOrderStatusesLabel' => __( 'Order statuses, for which cron will check the packet status', 'packeta' ),
623 'statusSyncingOrderStatusesDescription' => __( 'Cron will automatically track all orders with these statuses and check if the shipment status has changed.', 'packeta' ),
624 'statusSyncingPacketStatusesLabel' => __( 'Packet statuses that are being checked', 'packeta' ),
625 'statusSyncingPacketStatusesDescription' => __( 'If an order has a shipment with one of these selected statuses, the shipment status will be tracked.', 'packeta' ),
626 'numberOfDaysToCheckDescription' => __( 'Number of days after the creation of an order, during which the order status will be checked.', 'packeta' ),
627 ];
628
629 $this->latte_engine->render( PACKETERY_PLUGIN_DIR . '/template/options/page.latte', $latteParams );
630 }
631
632 /**
633 * Creates tab link.
634 *
635 * @param string|null $tab Tab ID.
636 * @return string
637 */
638 public function createLink( ?string $tab = null ): string {
639 $params = [
640 'page' => self::SLUG,
641 ];
642
643 if ( null !== $tab ) {
644 $params[ self::PARAM_TAB ] = $tab;
645 }
646
647 return add_query_arg(
648 $params,
649 get_admin_url( null, 'admin.php' )
650 );
651 }
652 }
653