PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
← All changes | src/Packetery/Module/Options/Page.php +396 -148 1.6.12.1 View file →
@@ -8,18 +8,29 @@
8 8 declare( strict_types=1 );
9 9
10 10 namespace Packetery\Module\Options;
11 11
12 +use DateTime;
13 +use Packetery\Core\Api;
12 14 use Packetery\Core\Api\Soap\Request\SenderGetReturnRouting;
15 +use Packetery\Core\CoreHelper;
16 +use Packetery\Core\Entity\PacketStatus;
13 17 use Packetery\Core\Log;
18 +use Packetery\Latte\Engine;
19 +use Packetery\Module\Dashboard\DashboardPage;
14 20 use Packetery\Module\FormFactory;
21 +use Packetery\Module\Framework\WpAdapter;
15 22 use Packetery\Module\MessageManager;
23 +use Packetery\Module\ModuleHelper;
16 24 use Packetery\Module\Order\PacketAutoSubmitter;
17 25 use Packetery\Module\Order\PacketSynchronizer;
18 -use Packetery\Latte\Engine;
19 26 use Packetery\Module\PaymentGatewayHelper;
27 +use Packetery\Module\Views\UrlBuilder;
20 28 use Packetery\Nette\Forms\Container;
29 +use Packetery\Nette\Forms\Controls\BaseControl;
30 +use Packetery\Nette\Forms\Controls\SubmitButton;
21 31 use Packetery\Nette\Forms\Form;
32 +use Packetery\Nette\Http;
22 33
23 34 /**
24 35 * Class Page
25 36 *
@@ -26,12 +37,12 @@
26 37 * @package Packetery\Options
27 38 */
28 39 class Page {
29 40
30 - private const FORM_FIELDS_CONTAINER = 'packetery';
31 - private const FORM_FIELD_PACKETA_LABEL_FORMAT = 'packeta_label_format';
32 - private const FORM_FIELD_CARRIER_LABEL_FORMAT = 'carrier_label_format';
33 - private const FORM_FIELD_ORDER_STATUS_AUTO_CHANGE = 'order_status_auto_change';
41 + private const FORM_FIELDS_CONTAINER = 'packetery';
42 + private const FORM_FIELD_PACKETA_LABEL_FORMAT = 'packeta_label_format';
43 + private const FORM_FIELD_CARRIER_LABEL_FORMAT = 'carrier_label_format';
44 + private const FORM_FIELD_FREE_SHIPPING_SHOWN = 'free_shipping_shown';
34 45
35 46 public const ACTION_VALIDATE_SENDER = 'validate-sender';
36 47
37 48 public const SLUG = 'packeta-options';
@@ -36,8 +47,9 @@
36 47
37 48 public const SLUG = 'packeta-options';
38 49
39 50 public const TAB_GENERAL = 'general';
51 + public const TAB_ADVANCED = 'advanced';
40 52 public const TAB_SUPPORT = 'support';
41 53 public const TAB_PACKET_STATUS_SYNC = 'packet-status-sync';
42 54 public const TAB_AUTO_SUBMISSION = 'auto-submission';
43 55
@@ -42,19 +54,21 @@
42 54 public const TAB_AUTO_SUBMISSION = 'auto-submission';
43 55
44 56 public const PARAM_TAB = 'tab';
45 57
58 + const PACKETA_SVG_LOGO = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDI1LjEuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IlZyc3R2YV8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCIKCSB2aWV3Qm94PSIwIDAgMzcgNDAiIHN0eWxlPSJmaWxsOiNhN2FhYWQiIHhtbDpzcGFjZT0icHJlc2VydmUiPgo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPgoJLnN0MHtmaWxsLXJ1bGU6ZXZlbm9kZDtjbGlwLXJ1bGU6ZXZlbm9kZDtmaWxsOiAjYTdhYWFkO30KPC9zdHlsZT4KPHBhdGggY2xhc3M9InN0MCIgZD0iTTE5LjQsMTYuMWwtMC45LDAuNGwtMC45LTAuNGwtMTMtNi41bDYuMi0yLjRsMTMuNCw2LjVMMTkuNCwxNi4xeiBNMzIuNSw5LjZsLTQuNywyLjNsLTEzLjUtNmw0LjItMS42CglMMzIuNSw5LjZ6Ii8+CjxwYXRoIGNsYXNzPSJzdDAiIGQ9Ik0xOSwwbDE3LjIsNi42bC0yLjQsMS45bC0xNS4yLTZMMy4yLDguNkwwLjgsNi42TDE4LDBMMTksMEwxOSwweiBNMzQuMSw5LjFsMi44LTEuMWwtMi4xLDE3LjZsLTAuNCwwLjgKCUwxOS40LDQwbC0wLjUtMy4xbDEzLjQtMTJMMzQuMSw5LjF6IE0yLjUsMjYuNWwtMC40LTAuOEwwLDguMWwyLjgsMS4xbDEuOSwxNS43bDEzLjQsMTJMMTcuNiw0MEwyLjUsMjYuNXoiLz4KPHBhdGggY2xhc3M9InN0MCIgZD0iTTI4LjIsMTIuNGw0LjMtMi43bC0xLjcsMTQuMkwxOC42LDM1bDAuNi0xN2w1LjQtMy4zTDI0LjMsMjNsMy4zLTIuM0wyOC4yLDEyLjR6Ii8+CjxwYXRoIGNsYXNzPSJzdDAiIGQ9Ik0xNy43LDE3LjlsMC42LDE3bC0xMi4yLTExTDQuNCw5LjhMMTcuNywxNy45eiIvPgo8L3N2Zz4K';
59 +
46 60 /**
47 61 * PacketeryLatte_engine.
48 62 *
49 63 * @var Engine PacketeryLatte engine.
50 64 */
51 - private $latte_engine;
65 + private $latteEngine;
52 66
53 67 /**
54 68 * Options Provider
55 69 *
56 - * @var Provider
70 + * @var OptionsProvider
57 71 */
58 72 private $optionsProvider;
59 73
60 74 /**
@@ -66,9 +80,9 @@
66 80
67 81 /**
68 82 * Packeta client.
69 83 *
70 - * @var \Packetery\Core\Api\Soap\Client
84 + * @var Api\Soap\Client
71 85 */
72 86 private $packetaClient;
73 87
74 88 /**
@@ -85,61 +99,88 @@
85 99 */
86 100 private $messageManager;
87 101
88 102 /**
89 - * HTTP request.
90 - *
91 - * @var \Packetery\Nette\Http\Request
103 + * @var Http\Request
92 104 */
93 105 private $httpRequest;
94 106
95 107 /**
96 - * Plugin constructor.
97 - *
98 - * @param Engine $latte_engine PacketeryLatte_engine.
99 - * @param Provider $optionsProvider Options provider.
100 - * @param FormFactory $formFactory Form factory.
101 - * @param \Packetery\Core\Api\Soap\Client $packetaClient Packeta Client.
102 - * @param Log\ILogger $logger Logger.
103 - * @param MessageManager $messageManager Message manager.
104 - * @param \Packetery\Nette\Http\Request $httpRequest HTTP request.
108 + * @var ModuleHelper
105 109 */
110 + private $moduleHelper;
111 +
112 + /**
113 + * @var UrlBuilder
114 + */
115 + private $urlBuilder;
116 +
117 + /**
118 + * @var PacketSynchronizer
119 + */
120 + private $packetSynchronizer;
121 +
122 + /**
123 + * @var WpAdapter
124 + */
125 + private $wpAdapter;
126 +
127 + /**
128 + * @var string
129 + */
130 + private $supportEmailAddress;
131 +
106 132 public function __construct(
107 - Engine $latte_engine,
108 - Provider $optionsProvider,
133 + Engine $latteEngine,
134 + OptionsProvider $optionsProvider,
109 135 FormFactory $formFactory,
110 - \Packetery\Core\Api\Soap\Client $packetaClient,
136 + Api\Soap\Client $packetaClient,
111 137 Log\ILogger $logger,
112 138 MessageManager $messageManager,
113 - \Packetery\Nette\Http\Request $httpRequest
139 + Http\Request $httpRequest,
140 + ModuleHelper $moduleHelper,
141 + UrlBuilder $urlBuilder,
142 + PacketSynchronizer $packetSynchronizer,
143 + WpAdapter $wpAdapter,
144 + string $supportEmailAddress
114 145 ) {
115 - $this->latte_engine = $latte_engine;
116 - $this->optionsProvider = $optionsProvider;
117 - $this->formFactory = $formFactory;
118 - $this->packetaClient = $packetaClient;
119 - $this->logger = $logger;
120 - $this->messageManager = $messageManager;
121 - $this->httpRequest = $httpRequest;
146 + $this->latteEngine = $latteEngine;
147 + $this->optionsProvider = $optionsProvider;
148 + $this->formFactory = $formFactory;
149 + $this->packetaClient = $packetaClient;
150 + $this->logger = $logger;
151 + $this->messageManager = $messageManager;
152 + $this->httpRequest = $httpRequest;
153 + $this->moduleHelper = $moduleHelper;
154 + $this->urlBuilder = $urlBuilder;
155 + $this->packetSynchronizer = $packetSynchronizer;
156 + $this->supportEmailAddress = $supportEmailAddress;
157 + $this->wpAdapter = $wpAdapter;
122 158 }
123 159
124 - /**
125 - * Registers WP callbacks.
126 - */
127 160 public function register(): void {
128 - add_action( 'admin_init', array( $this, 'admin_init' ) );
129 - $icon = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDI1LjEuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IlZyc3R2YV8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCIKCSB2aWV3Qm94PSIwIDAgMzcgNDAiIHN0eWxlPSJmaWxsOiNhN2FhYWQiIHhtbDpzcGFjZT0icHJlc2VydmUiPgo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPgoJLnN0MHtmaWxsLXJ1bGU6ZXZlbm9kZDtjbGlwLXJ1bGU6ZXZlbm9kZDtmaWxsOiAjYTdhYWFkO30KPC9zdHlsZT4KPHBhdGggY2xhc3M9InN0MCIgZD0iTTE5LjQsMTYuMWwtMC45LDAuNGwtMC45LTAuNGwtMTMtNi41bDYuMi0yLjRsMTMuNCw2LjVMMTkuNCwxNi4xeiBNMzIuNSw5LjZsLTQuNywyLjNsLTEzLjUtNmw0LjItMS42CglMMzIuNSw5LjZ6Ii8+CjxwYXRoIGNsYXNzPSJzdDAiIGQ9Ik0xOSwwbDE3LjIsNi42bC0yLjQsMS45bC0xNS4yLTZMMy4yLDguNkwwLjgsNi42TDE4LDBMMTksMEwxOSwweiBNMzQuMSw5LjFsMi44LTEuMWwtMi4xLDE3LjZsLTAuNCwwLjgKCUwxOS40LDQwbC0wLjUtMy4xbDEzLjQtMTJMMzQuMSw5LjF6IE0yLjUsMjYuNWwtMC40LTAuOEwwLDguMWwyLjgsMS4xbDEuOSwxNS43bDEzLjQsMTJMMTcuNiw0MEwyLjUsMjYuNXoiLz4KPHBhdGggY2xhc3M9InN0MCIgZD0iTTI4LjIsMTIuNGw0LjMtMi43bC0xLjcsMTQuMkwxOC42LDM1bDAuNi0xN2w1LjQtMy4zTDI0LjMsMjNsMy4zLTIuM0wyOC4yLDEyLjR6Ii8+CjxwYXRoIGNsYXNzPSJzdDAiIGQ9Ik0xNy43LDE3LjlsMC42LDE3bC0xMi4yLTExTDQuNCw5LjhMMTcuNywxNy45eiIvPgo8L3N2Zz4K';
130 - add_menu_page(
131 - __( 'Packeta', 'packeta' ),
132 - __( 'Packeta', 'packeta' ),
161 + add_action( 'admin_init', [ $this, 'admin_init' ] );
162 + add_action( 'admin_init', [ $this, 'processActions' ] );
163 + add_filter( 'custom_menu_order', '__return_true' );
164 + add_filter( 'menu_order', [ $this, 'customMenuOrder' ] );
165 + }
166 +
167 + public function registerMenuPage(): void {
168 + $this->wpAdapter->addMenuPage(
169 + $this->wpAdapter->__( 'Packeta', 'packeta' ),
170 + $this->wpAdapter->__( 'Packeta', 'packeta' ),
133 171 'manage_options',
134 - self::SLUG,
135 - '',
136 - $icon
172 + DashboardPage::SLUG,
173 + function () {},
174 + self::PACKETA_SVG_LOGO
137 175 );
138 - add_submenu_page(
139 - self::SLUG,
140 - __( 'Settings', 'packeta' ),
141 - __( 'Settings', 'packeta' ),
176 + }
177 +
178 + public function registerSubmenuPage(): void {
179 + $this->wpAdapter->addSubmenuPage(
180 + DashboardPage::SLUG,
181 + $this->wpAdapter->__( 'Settings', 'packeta' ),
182 + $this->wpAdapter->__( 'Settings', 'packeta' ),
142 183 'manage_options',
143 184 self::SLUG,
144 185 array(
145 186 $this,
@@ -146,12 +187,8 @@
146 187 'render',
147 188 ),
148 189 1
149 190 );
150 - add_action( 'admin_init', [ $this, 'processActions' ] );
151 -
152 - add_filter( 'custom_menu_order', '__return_true' );
153 - add_filter( 'menu_order', [ $this, 'customMenuOrder' ] );
154 191 }
155 192
156 193 /**
157 194 * Returns menu_order with Packeta item in last position.
@@ -162,9 +199,9 @@
162 199 */
163 200 public function customMenuOrder( array $menuOrder ): array {
164 201 $currentPosition = array_search( self::SLUG, $menuOrder, true );
165 202
166 - if ( false !== $currentPosition ) {
203 + if ( $currentPosition !== false ) {
167 204 unset( $menuOrder[ $currentPosition ] );
168 205 $menuOrder[] = self::SLUG;
169 206 }
170 207
@@ -190,15 +227,54 @@
190 227 return $statuses;
191 228 }
192 229
193 230 /**
231 + * Translate hashes to packet status keys.
232 + *
233 + * @param array $choiceData Choice data.
234 + * @param array $formData Picked items.
235 + *
236 + * @return array
237 + */
238 + private function translateStatuses( array $choiceData, array $formData ): array {
239 + $statuses = [];
240 + foreach ( $formData as $hash => $value ) {
241 + if ( $value ) {
242 + $statuses[ $choiceData[ $hash ]['key'] ] = $value;
243 + }
244 + }
245 +
246 + return $statuses;
247 + }
248 +
249 + /**
250 + * Gets status syncing packet statuses.
251 + *
252 + * @return array<string, array{key: int|string, default: bool, label:string}>
253 + */
254 + public function getStatusSyncingPacketStatusesChoiceData(): array {
255 + $statuses = $this->packetSynchronizer->getDefaultPacketStatuses();
256 +
257 + return $this->createPacketStatusChoiceData( $statuses );
258 + }
259 +
260 + /**
194 261 * Gets all packet statuses.
195 262 *
196 - * @return array
263 + * @return array<string, array{key: int|string, default: bool, label:string}>
197 264 */
198 - public function getPacketStatusesChoiceData(): array {
199 - $statuses = PacketSynchronizer::getPacketStatuses();
265 + public function getAllPacketStatusesChoiceData(): array {
266 + $statuses = $this->packetSynchronizer->getPacketStatuses();
200 267
268 + return $this->createPacketStatusChoiceData( $statuses );
269 + }
270 +
271 + /**
272 + * @param PacketStatus[] $statuses Packet statuses.
273 + *
274 + * @return array<string, array{key: int|string, default: bool, label:string}>
275 + */
276 + private function createPacketStatusChoiceData( array $statuses ): array {
201 277 $result = [];
202 278
203 279 foreach ( $statuses as $status => $statusEntity ) {
204 280 $result[ md5( $status ) ] = [
@@ -213,9 +289,9 @@
213 289
214 290 /**
215 291 * Gets order statuses for form.
216 292 *
217 - * @return array
293 + * @return array<string, array<string, int|string>>
218 294 */
219 295 public static function getOrderStatusesChoiceData(): array {
220 296 $orderStatuses = wc_get_order_statuses();
221 297 $orderStatusesTransformed = [];
@@ -237,13 +313,13 @@
237 313 */
238 314 public function createAutoSubmissionForm(): Form {
239 315 $gateways = PaymentGatewayHelper::getAvailablePaymentGateways();
240 316 $form = $this->formFactory->create( 'packetery_auto_submission_form' );
241 - $defaults = $this->optionsProvider->getOptionsByName( Provider::OPTION_NAME_PACKETERY_AUTO_SUBMISSION );
317 + $defaults = $this->optionsProvider->getOptionsByName( OptionNames::PACKETERY_AUTO_SUBMISSION );
242 318
243 319 $form->addCheckbox( 'allow', __( 'Allow packet auto-submission', 'packeta' ) )
244 320 ->setRequired( false )
245 - ->setDefaultValue( Provider::PACKET_AUTO_SUBMISSION_ALLOWED_DEFAULT );
321 + ->setDefaultValue( OptionsProvider::PACKET_AUTO_SUBMISSION_ALLOWED_DEFAULT );
246 322
247 323 $eventChoices = [
248 324 PacketAutoSubmitter::EVENT_ON_ORDER_CREATION_FE => __( 'On order creation at e-shop checkout', 'packeta' ),
249 325 PacketAutoSubmitter::EVENT_ON_ORDER_PROCESSING => __( 'On order processing', 'packeta' ),
@@ -255,10 +331,10 @@
255 331 $paymentMethodEventsMethod = $paymentMethodEvents->addContainer(
256 332 $this->optionsProvider->sanitizePaymentGatewayId( $gateway->id )
257 333 );
258 334 $paymentMethodEventsMethod->addSelect( 'event', $gateway->get_method_title(), $eventChoices )
259 - ->setPrompt( __( 'Select event', 'packeta' ) )
260 - ->checkDefaultValue( false );
335 + ->setPrompt( __( 'Select event', 'packeta' ) )
336 + ->checkDefaultValue( false );
261 337 }
262 338
263 339 $form->addSubmit( 'save', __( 'Save changes', 'packeta' ) );
264 340
@@ -277,9 +353,9 @@
277 353 *
278 354 * @return void
279 355 */
280 356 public function onAutoSubmissionFormSuccess( Form $form, array $values ): void {
281 - update_option( Provider::OPTION_NAME_PACKETERY_AUTO_SUBMISSION, $values );
357 + update_option( OptionNames::PACKETERY_AUTO_SUBMISSION, $values );
282 358
283 359 $this->messageManager->flash_message( __( 'Settings saved.', 'packeta' ), MessageManager::TYPE_SUCCESS, MessageManager::RENDERER_PACKETERY, 'plugin-options' );
284 360
285 361 if ( wp_safe_redirect( $this->createLink( self::TAB_AUTO_SUBMISSION ) ) ) {
@@ -293,21 +369,21 @@
293 369 * @return Form
294 370 */
295 371 private function createPacketStatusSyncForm(): Form {
296 372 $form = $this->formFactory->create( 'packetery_packet_status_sync_form' );
297 - $settings = $this->optionsProvider->getOptionsByName( Provider::OPTION_NAME_PACKETERY_SYNC );
373 + $settings = $this->optionsProvider->getOptionsByName( OptionNames::PACKETERY_SYNC );
298 374
299 375 $form->addText( 'max_status_syncing_packets', __( 'Number of orders synced during one cron call', 'packeta' ) )
300 376 ->setRequired( false )
301 377 ->addRule( Form::INTEGER )
302 378 ->addRule( Form::MIN, null, 0 )
303 - ->setDefaultValue( Provider::MAX_STATUS_SYNCING_PACKETS_DEFAULT );
379 + ->setDefaultValue( OptionsProvider::MAX_STATUS_SYNCING_PACKETS_DEFAULT );
304 380
305 381 $form->addText( 'max_days_of_packet_status_syncing', __( 'Number of days for which the order status is checked', 'packeta' ) )
306 382 ->setRequired( false )
307 383 ->addRule( Form::INTEGER )
308 384 ->addRule( Form::MIN, null, 0 )
309 - ->setDefaultValue( Provider::MAX_DAYS_OF_PACKET_STATUS_SYNCING_DEFAULT );
385 + ->setDefaultValue( OptionsProvider::MAX_DAYS_OF_PACKET_STATUS_SYNCING_DEFAULT );
310 386
311 387 $orderStatusesTransformed = self::getOrderStatusesChoiceData();
312 388 $orderStatusesContainer = $form->addContainer( 'status_syncing_order_statuses' );
313 389
@@ -318,9 +394,9 @@
318 394 }
319 395 }
320 396 unset( $settings['status_syncing_order_statuses'] );
321 397
322 - $packetStatuses = $this->getPacketStatusesChoiceData();
398 + $packetStatuses = $this->getStatusSyncingPacketStatusesChoiceData();
323 399 $packetStatusesContainer = $form->addContainer( 'status_syncing_packet_statuses' );
324 400
325 401 foreach ( $packetStatuses as $packetStatusHash => $packetStatusData ) {
326 402 $item = $packetStatusesContainer->addCheckbox( $packetStatusHash, $packetStatusData['label'] );
@@ -331,8 +407,27 @@
331 407 }
332 408 }
333 409 unset( $settings['status_syncing_packet_statuses'] );
334 410
411 + $form->addCheckbox( 'allow_order_status_change', __( 'Allow order status change', 'packeta' ) )
412 + ->setRequired( false )
413 + ->setDefaultValue( false )
414 + ->addCondition( Form::EQUAL, true )
415 + ->toggle( '.order_status_change_content' );
416 +
417 + $orderStatusChangePacketStatuses = $form->addContainer( 'order_status_change_packet_statuses' );
418 + $orderStatuses = wc_get_order_statuses();
419 + $packetStatuses = $this->getAllPacketStatusesChoiceData();
420 + foreach ( $packetStatuses as $packetStatusHash => $packetStatusData ) {
421 + $item = $orderStatusChangePacketStatuses->addSelect( $packetStatusHash, $packetStatusData['label'], $orderStatuses )
422 + ->setPrompt( __( 'Order status', 'packeta' ) );
423 + $targetStatus = $settings['order_status_change_packet_statuses'][ $packetStatusData['key'] ] ?? null;
424 + if ( $targetStatus !== null && array_key_exists( $targetStatus, $orderStatuses ) ) {
425 + $item->setDefaultValue( $targetStatus );
426 + }
427 + }
428 + unset( $settings['order_status_change_packet_statuses'] );
429 +
335 430 $form->setDefaults( $settings );
336 431
337 432 $form->addSubmit( 'save', __( 'Save changes', 'packeta' ) );
338 433
@@ -350,26 +445,30 @@
350 445 * @return void
351 446 */
352 447 public function onPacketStatusSyncFormSuccess( Form $form, array $values ): void {
353 448
354 - $values['status_syncing_order_statuses'] = $this->getChosenKeys(
449 + $values['status_syncing_order_statuses'] = $this->getChosenKeys(
355 450 self::getOrderStatusesChoiceData(),
356 451 $values['status_syncing_order_statuses']
357 452 );
358 - $values['status_syncing_packet_statuses'] = $this->getChosenKeys(
359 - $this->getPacketStatusesChoiceData(),
453 + $values['status_syncing_packet_statuses'] = $this->getChosenKeys(
454 + $this->getStatusSyncingPacketStatusesChoiceData(),
360 455 $values['status_syncing_packet_statuses']
361 456 );
457 + $values['order_status_change_packet_statuses'] = $this->translateStatuses(
458 + $this->getAllPacketStatusesChoiceData(),
459 + $values['order_status_change_packet_statuses']
460 + );
362 461
363 - if ( '' === $values['max_status_syncing_packets'] ) {
462 + if ( $values['max_status_syncing_packets'] === '' ) {
364 463 unset( $values['max_status_syncing_packets'] );
365 464 }
366 465
367 - if ( '' === $values['max_days_of_packet_status_syncing'] ) {
466 + if ( $values['max_days_of_packet_status_syncing'] === '' ) {
368 467 unset( $values['max_days_of_packet_status_syncing'] );
369 468 }
370 469
371 - update_option( Provider::OPTION_NAME_PACKETERY_SYNC, $values );
470 + update_option( OptionNames::PACKETERY_SYNC, $values );
372 471
373 472 $this->messageManager->flash_message( __( 'Settings saved.', 'packeta' ), MessageManager::TYPE_SUCCESS, MessageManager::RENDERER_PACKETERY, 'plugin-options' );
374 473
375 474 if ( wp_safe_redirect( $this->createLink( self::TAB_PACKET_STATUS_SYNC ) ) ) {
@@ -376,8 +475,35 @@
376 475 exit;
377 476 }
378 477 }
379 478
479 + public function createAdvancedForm(): Form {
480 + $form = $this->formFactory->create( 'packetery_advanced_form' );
481 + $defaults = $this->optionsProvider->getOptionsByName( OptionNames::PACKETERY_ADVANCED );
482 +
483 + $form->addCheckbox( 'new_carrier_settings_enabled', __( 'Advanced carrier settings', 'packeta' ) )
484 + ->setRequired( false )
485 + ->setDefaultValue( OptionsProvider::DEFAULT_VALUE_CARRIER_SETTINGS );
486 +
487 + $form->addSubmit( 'save', __( 'Save changes', 'packeta' ) );
488 +
489 + $form->setDefaults( $defaults );
490 +
491 + $form->onSuccess[] = [ $this, 'onAdvancedFormSuccess' ];
492 +
493 + return $form;
494 + }
495 +
496 + public function onAdvancedFormSuccess( Form $form, array $values ): void {
497 + update_option( OptionNames::PACKETERY_ADVANCED, $values );
498 +
499 + $this->messageManager->flash_message( __( 'Settings saved.', 'packeta' ), MessageManager::TYPE_SUCCESS, MessageManager::RENDERER_PACKETERY, 'plugin-options' );
500 +
501 + if ( wp_safe_redirect( $this->createLink( self::TAB_ADVANCED ) ) ) {
502 + exit;
503 + }
504 + }
505 +
380 506 /**
381 507 * Creates settings form.
382 508 *
383 509 * @return Form
@@ -387,12 +513,12 @@
387 513 $form->setAction( 'options.php' );
388 514
389 515 $container = $form->addContainer( self::FORM_FIELDS_CONTAINER );
390 516 $container->addText( 'api_password', __( 'API password', 'packeta' ) )
391 - ->setRequired()
392 - ->addRule( $form::PATTERN, __( 'API password must be 32 characters long and must contain valid characters!', 'packeta' ), '[a-z\d]{32}' );
517 + ->setRequired()
518 + ->addRule( $form::PATTERN, __( 'API password must be 32 characters long and must contain valid characters!', 'packeta' ), '[a-z\d]{32}' );
393 519 $container->addText( 'sender', __( 'Sender', 'packeta' ) )
394 - ->setRequired();
520 + ->setRequired();
395 521
396 522 $packetaLabelFormats = $this->optionsProvider->getPacketaLabelFormats();
397 523 $container->addSelect(
398 524 self::FORM_FIELD_PACKETA_LABEL_FORMAT,
@@ -397,9 +523,9 @@
397 523 $container->addSelect(
398 524 self::FORM_FIELD_PACKETA_LABEL_FORMAT,
399 525 __( 'Packeta Label Format', 'packeta' ),
400 526 $packetaLabelFormats
401 - )->checkDefaultValue( false )->setDefaultValue( Provider::DEFAULT_VALUE_PACKETA_LABEL_FORMAT );
527 + )->checkDefaultValue( false )->setDefaultValue( OptionsProvider::DEFAULT_VALUE_PACKETA_LABEL_FORMAT );
402 528
403 529 $carrierLabelFormats = $this->optionsProvider->getCarrierLabelFormat();
404 530 $container->addSelect(
405 531 self::FORM_FIELD_CARRIER_LABEL_FORMAT,
@@ -404,9 +530,9 @@
404 530 $container->addSelect(
405 531 self::FORM_FIELD_CARRIER_LABEL_FORMAT,
406 532 __( 'Carrier Label Format', 'packeta' ),
407 533 $carrierLabelFormats
408 - )->checkDefaultValue( false )->setDefaultValue( Provider::DEFAULT_VALUE_CARRIER_LABEL_FORMAT );
534 + )->checkDefaultValue( false )->setDefaultValue( OptionsProvider::DEFAULT_VALUE_CARRIER_LABEL_FORMAT );
409 535
410 536 $gateways = PaymentGatewayHelper::getAvailablePaymentGateways();
411 537 $enabledGateways = [];
412 538 foreach ( $gateways as $gateway ) {
@@ -411,36 +537,91 @@
411 537 $enabledGateways = [];
412 538 foreach ( $gateways as $gateway ) {
413 539 $enabledGateways[ $gateway->id ] = $gateway->get_method_title();
414 540 }
415 - $container->addSelect(
416 - 'cod_payment_method',
417 - __( 'Payment method that represents cash on delivery', 'packeta' ),
541 + $container->addMultiSelect(
542 + 'cod_payment_methods',
543 + __( 'Payment methods that represent cash on delivery', 'packeta' ),
418 544 $enabledGateways
419 - )->setPrompt( '--' )->checkDefaultValue( false );
545 + )->checkDefaultValue( false );
420 546
421 547 $container->addText( 'packaging_weight', __( 'Weight of packaging material', 'packeta' ) . ' (kg)' )
422 - ->setRequired( true )
423 - ->addRule( Form::FLOAT )
424 - ->addRule( Form::MIN, null, 0 )
425 - ->setDefaultValue( 0 );
548 + ->setRequired()
549 + ->addRule( Form::FLOAT )
550 + ->addRule( Form::MIN, null, 0 )
551 + ->setDefaultValue( 0 );
426 552
427 553 $container->addCheckbox( 'default_weight_enabled', __( 'Enable default weight', 'packeta' ) )
428 - ->addCondition( Form::EQUAL, true )
429 - ->toggle( '#packetery-default-weight-value' );
554 + ->addCondition( Form::EQUAL, true )
555 + ->toggle( '#packetery-default-weight-value' );
430 556
431 557 $container->addText( 'default_weight', __( 'Default weight', 'packeta' ) . ' (kg)' )
432 - ->addRule( Form::FLOAT )
433 - ->addRule( Form::MIN, null, 0.1 )
434 - ->addConditionOn( $form[ self::FORM_FIELDS_CONTAINER ]['default_weight_enabled'], Form::EQUAL, true )
435 - ->setRequired();
558 + ->addConditionOn( $form[ self::FORM_FIELDS_CONTAINER ]['default_weight_enabled'], Form::EQUAL, true )
559 + ->setRequired()
560 + ->addRule( Form::FLOAT )
561 + ->addRule( Form::MIN, null, 0.1 );
436 562
437 - // TODO: Packet status sync.
563 + $container->addSelect(
564 + 'dimensions_unit',
565 + __( 'Units used for dimensions', 'packeta' ),
566 + [
567 + OptionsProvider::DIMENSIONS_UNIT_CM => 'cm',
568 + OptionsProvider::DEFAULT_DIMENSIONS_UNIT_MM => 'mm',
569 + ]
570 + )
571 + ->setDefaultValue( $this->optionsProvider::DEFAULT_DIMENSIONS_UNIT_MM );
438 572
573 + $container->addCheckbox( 'default_dimensions_enabled', __( 'Enable default dimensions', 'packeta' ) )
574 + ->addCondition( Form::EQUAL, true )
575 + ->toggle( '#packetery-default-dimensions-value' );
576 +
577 + $container->addText( 'default_length', __( 'Length', 'packeta' ) )
578 + ->addConditionOn( $form[ self::FORM_FIELDS_CONTAINER ]['default_dimensions_enabled'], Form::EQUAL, true )
579 + ->setRequired()
580 + ->addConditionOn( $form[ self::FORM_FIELDS_CONTAINER ]['dimensions_unit'], Form::EQUAL, $this->optionsProvider::DEFAULT_DIMENSIONS_UNIT_MM )
581 + ->addRule( Form::INTEGER, __( 'Provide a full number!', 'packeta' ) )
582 + ->addRule( Form::MIN, 'Value must be greater than 0', 1 )
583 + ->elseCondition()
584 + ->addRule( Form::FLOAT, __( 'Provide a decimal value!', 'packeta' ) )
585 + ->addRule( Form::MIN, 'Value must be greater than 0', 0.1 )
586 + ->endCondition();
587 +
588 + $container->addText( 'default_height', __( 'Height', 'packeta' ) )
589 + ->addConditionOn( $form[ self::FORM_FIELDS_CONTAINER ]['default_dimensions_enabled'], Form::EQUAL, true )
590 + ->setRequired()
591 + ->addConditionOn( $form[ self::FORM_FIELDS_CONTAINER ]['dimensions_unit'], Form::EQUAL, $this->optionsProvider::DEFAULT_DIMENSIONS_UNIT_MM )
592 + ->addRule( Form::INTEGER, __( 'Provide a full number!', 'packeta' ) )
593 + ->addRule( Form::MIN, 'Value must be greater than 0', 1 )
594 + ->elseCondition()
595 + ->addRule( Form::FLOAT, __( 'Provide a decimal value!', 'packeta' ) )
596 + ->addRule( Form::MIN, 'Value must be greater than 0', 0.1 )
597 + ->endCondition();
598 +
599 + $container->addText( 'default_width', __( 'Width', 'packeta' ) )
600 + ->addConditionOn( $form[ self::FORM_FIELDS_CONTAINER ]['default_dimensions_enabled'], Form::EQUAL, true )
601 + ->setRequired()
602 + ->addConditionOn( $form[ self::FORM_FIELDS_CONTAINER ]['dimensions_unit'], Form::EQUAL, $this->optionsProvider::DEFAULT_DIMENSIONS_UNIT_MM )
603 + ->addRule( Form::INTEGER, __( 'Provide a full number!', 'packeta' ) )
604 + ->addRule( Form::MIN, 'Value must be greater than 0', 1 )
605 + ->elseCondition()
606 + ->addRule( Form::FLOAT, __( 'Provide a decimal value!', 'packeta' ) )
607 + ->addRule( Form::MIN, 'Value must be greater than 0', 0.1 )
608 + ->endCondition();
609 +
439 610 $container->addCheckbox( 'replace_shipping_address_with_pickup_point_address', __( 'Replace shipping address with pickup point address', 'packeta' ) )
440 - ->setRequired( false );
611 + ->setRequired( false );
441 612
442 613 $container->addSelect(
614 + 'checkout_detection',
615 + __( 'Force checkout type', 'packeta' ),
616 + [
617 + OptionsProvider::AUTOMATIC_CHECKOUT_DETECTION => __( 'Automatic detection', 'packeta' ),
618 + OptionsProvider::CLASSIC_CHECKOUT_DETECTION => __( 'Classic checkout', 'packeta' ),
619 + OptionsProvider::BLOCK_CHECKOUT_DETECTION => __( 'Block-based checkout', 'packeta' ),
620 + ]
621 + );
622 +
623 + $container->addSelect(
443 624 'checkout_widget_button_location',
444 625 __( 'Widget button location in checkout', 'packeta' ),
445 626 [
446 627 'after_shipping_rate' => __( 'After shipping rate', 'packeta' ),
@@ -447,8 +628,12 @@
447 628 'after_transport_methods' => __( 'After transport methods', 'packeta' ),
448 629 ]
449 630 );
450 631
632 + $container->addCheckbox( 'hide_checkout_logo', __( 'Hide Packeta checkout logo', 'packeta' ) )
633 + ->setRequired( false )
634 + ->setDefaultValue( OptionsProvider::HIDE_CHECKOUT_LOGO_DEFAULT );
635 +
451 636 $container->addSelect(
452 637 'email_hook',
453 638 __( 'Hook used to view information in email', 'packeta' ),
454 639 [
@@ -459,43 +644,27 @@
459 644 ]
460 645 );
461 646
462 647 $container->addCheckbox( 'force_packet_cancel', __( 'Force order cancellation', 'packeta' ) )
463 - ->setRequired( false )
464 - ->setDefaultValue( Provider::FORCE_PACKET_CANCEL_DEFAULT );
648 + ->setRequired( false )
649 + ->setDefaultValue( OptionsProvider::FORCE_PACKET_CANCEL_DEFAULT );
465 650
466 651 $container->addCheckbox( 'widget_auto_open', __( 'Automatically open widget when shipping was selected', 'packeta' ) )
467 - ->setRequired( false )
468 - ->setDefaultValue( Provider::WIDGET_AUTO_OPEN_DEFAULT );
652 + ->setRequired( false )
653 + ->setDefaultValue( OptionsProvider::WIDGET_AUTO_OPEN_DEFAULT );
469 654
470 - $container->addCheckbox( self::FORM_FIELD_ORDER_STATUS_AUTO_CHANGE, __( 'Order status change after parcel creation', 'packeta' ) )
471 - ->setRequired( false )
472 - ->setDefaultValue( Provider::ORDER_STATUS_AUTO_CHANGE_DEFAULT )
473 - ->addCondition( Form::EQUAL, true )
474 - ->toggle( '.packetery-order-status-auto-change-row' );
655 + $container->addCheckbox( self::FORM_FIELD_FREE_SHIPPING_SHOWN, __( 'Display the FREE shipping text in checkout', 'packeta' ) )
656 + ->setRequired( false )
657 + ->setDefaultValue( OptionsProvider::DISPLAY_FREE_SHIPPING_IN_CHECKOUT_DEFAULT );
475 658
476 - $orderStatuses = wc_get_order_statuses();
477 - $container->addSelect(
478 - Provider::AUTO_ORDER_STATUS,
479 - __( 'New order status', 'packeta' ),
480 - $orderStatuses
481 - )
482 - ->checkDefaultValue( false )
483 - ->setDefaultValue( Provider::AUTO_ORDER_STATUS_DEFAULT )
484 - ->setPrompt( __( 'Select new order status', 'packeta' ) )
485 - ->addRule( Form::IS_IN, __( 'Select valid order status', 'packeta' ), array_keys( $orderStatuses ) )
486 - ->addConditionOn( $form[ self::FORM_FIELDS_CONTAINER ][ self::FORM_FIELD_ORDER_STATUS_AUTO_CHANGE ], Form::EQUAL, true )
487 - ->setRequired();
659 + $container->addCheckbox( 'prices_include_tax', __( 'Prices include tax', 'packeta' ) )
660 + ->setRequired( false )
661 + ->setDefaultValue( OptionsProvider::PRICES_INCLUDE_TAX_DEFAULT );
488 662
489 - $container->addCheckbox(
490 - 'order_status_auto_change_for_auto_submit_at_frontend',
491 - __( 'Change order status after automatic packet submit at checkout', 'packeta' )
492 - );
493 -
494 663 $form->addSubmit( 'save', __( 'Save changes', 'packeta' ) );
495 664
496 - if ( $this->optionsProvider->has_any( Provider::OPTION_NAME_PACKETERY ) ) {
497 - $container->setDefaults( $this->optionsProvider->getOptionsByName( Provider::OPTION_NAME_PACKETERY ) );
665 + if ( $this->optionsProvider->has_any( OptionNames::PACKETERY ) ) {
666 + $container->setDefaults( $this->optionsProvider->getOptionsByName( OptionNames::PACKETERY ) );
498 667 }
499 668
500 669 return $form;
501 670 }
@@ -505,9 +674,9 @@
505 674 */
506 675 public function admin_init(): void {
507 676 add_filter( 'pre_update_option_packetery', [ $this, 'validatePacketeryOptions' ] );
508 677 register_setting( self::FORM_FIELDS_CONTAINER, self::FORM_FIELDS_CONTAINER, [ $this, 'sanitizePacketeryOptions' ] );
509 - add_settings_section( 'packetery_main', __( 'Main Settings', 'packeta' ), '', self::SLUG );
678 + add_settings_section( 'packetery_main', __( 'Main Settings', 'packeta' ), function () {}, self::SLUG );
510 679 }
511 680
512 681 /**
513 682 * Validates packetery options.
@@ -517,9 +686,13 @@
517 686 * @return array
518 687 */
519 688 public function validatePacketeryOptions( array $options ): array {
520 689 $form = $this->create_form();
521 - /** Packetery container. @var Container $packeteryContainer */
690 + /**
691 + * Packetery container.
692 + *
693 + * @var Container $packeteryContainer
694 + */
522 695 $packeteryContainer = $form[ self::FORM_FIELDS_CONTAINER ];
523 696 $packeteryContainer->setValues( $options );
524 697 if ( $form->isValid() === false ) {
525 698 foreach ( $packeteryContainer->getControls() as $control ) {
@@ -526,14 +699,14 @@
526 699 if ( $control->hasErrors() === false ) {
527 700 continue;
528 701 }
529 702
530 - add_settings_error( $control->getCaption(), esc_attr( $control->getName() ), $control->getError() );
703 + add_settings_error( OptionNames::PACKETERY, esc_attr( $control->getName() ), "{$control->getCaption()}: {$control->getError()}" );
531 704 }
532 705 }
533 706
534 707 $apiPassword = $packeteryContainer['api_password'];
535 - if ( $apiPassword->hasErrors() === false ) {
708 + if ( $apiPassword instanceof BaseControl && $apiPassword->hasErrors() === false ) {
536 709 $this->packetaClient->setApiPassword( $apiPassword->getValue() );
537 710 }
538 711
539 712 $this->validateSender( $options['sender'] );
@@ -549,9 +722,13 @@
549 722 * @return array
550 723 */
551 724 public function sanitizePacketeryOptions( array $options ): array {
552 725 $form = $this->create_form();
553 - /** Packetery container. @var Container $packeteryContainer */
726 + /**
727 + * Packetery container.
728 + *
729 + * @var Container $packeteryContainer
730 + */
554 731 $packeteryContainer = $form[ self::FORM_FIELDS_CONTAINER ];
555 732 $packeteryContainer->setValues( $options );
556 733 if ( $form->isValid() === false ) {
557 734 foreach ( $packeteryContainer->getControls() as $control ) {
@@ -562,19 +739,57 @@
562 739 $options[ $control->getName() ] = '';
563 740 }
564 741 }
565 742
566 - $api_password = $packeteryContainer['api_password'];
567 - if ( $api_password->hasErrors() === false ) {
568 - $api_pass = $api_password->getValue();
569 - $options['api_key'] = substr( $api_pass, 0, 16 );
570 - $this->packetaClient->setApiPassword( $api_pass );
743 + $apiPassword = $packeteryContainer['api_password'];
744 +
745 + if ( $apiPassword instanceof BaseControl && $apiPassword->hasErrors() === false ) {
746 + $apiPass = $apiPassword->getValue();
747 + $options['api_key'] = substr( $apiPass, 0, 16 );
748 + $this->packetaClient->setApiPassword( $apiPass );
571 749 } else {
572 750 $options['api_key'] = '';
573 751 }
574 752
575 - $options['force_packet_cancel'] = (int) $packeteryContainer['force_packet_cancel']->getValue();
753 + if ( $packeteryContainer['default_weight'] instanceof BaseControl ) {
754 + $defaultWeight = $packeteryContainer['default_weight']->getValue();
755 + $options['default_weight'] = is_numeric( $defaultWeight ) ? CoreHelper::trimDecimalPlaces( (float) $defaultWeight, 3 ) : $defaultWeight;
756 + }
576 757
758 + foreach ( [ 'default_length', 'default_width', 'default_height' ] as $dimension ) {
759 + if ( $packeteryContainer[ $dimension ] instanceof BaseControl ) {
760 + $options[ $dimension ] = is_numeric( $packeteryContainer[ $dimension ]->getValue() )
761 + ? CoreHelper::trimDecimalPlaces( (float) $packeteryContainer[ $dimension ]->getValue(), 1 )
762 + : $packeteryContainer[ $dimension ]->getValue();
763 + }
764 + }
765 +
766 + if ( $packeteryContainer['force_packet_cancel'] instanceof BaseControl ) {
767 + $options['force_packet_cancel'] = (int) $packeteryContainer['force_packet_cancel']->getValue();
768 + }
769 +
770 + if ( $packeteryContainer['free_shipping_shown'] instanceof BaseControl ) {
771 + $options['free_shipping_shown'] = (int) $packeteryContainer['free_shipping_shown']->getValue();
772 + }
773 +
774 + $previousOptions = $this->optionsProvider->getOptionsByName( OptionNames::PACKETERY );
775 + if ( ! isset( $options['default_weight_enabled'] ) ) {
776 + if ( isset( $previousOptions['default_weight'] ) ) {
777 + $options['default_weight'] = $previousOptions['default_weight'];
778 + } else {
779 + unset( $options['default_weight'] );
780 + }
781 + }
782 + if ( ! isset( $options['default_dimensions_enabled'] ) ) {
783 + foreach ( [ 'default_length', 'default_width', 'default_height' ] as $dimension ) {
784 + if ( isset( $previousOptions[ $dimension ] ) ) {
785 + $options[ $dimension ] = $previousOptions[ $dimension ];
786 + } else {
787 + unset( $options[ $dimension ] );
788 + }
789 + }
790 + }
791 +
577 792 return $options;
578 793 }
579 794
580 795 /**
@@ -605,13 +820,13 @@
605 820 $this->logger->add( $senderValidationLog );
606 821
607 822 $senderExists = $senderValidationResponse->senderExists();
608 823
609 - if ( false === $senderExists ) {
824 + if ( $senderExists === false ) {
610 825 $this->messageManager->flash_message( __( 'Specified sender does not exist', 'packeta' ), MessageManager::TYPE_INFO, MessageManager::RENDERER_PACKETERY, 'plugin-options' );
611 826 }
612 827
613 - if ( null === $senderExists ) {
828 + if ( $senderExists === null ) {
614 829 $this->messageManager->flash_message( __( 'Unable to check specified sender', 'packeta' ), MessageManager::TYPE_INFO, MessageManager::RENDERER_PACKETERY, 'plugin-options' );
615 830 }
616 831
617 832 return $senderExists;
@@ -623,12 +838,12 @@
623 838 * @return void
624 839 */
625 840 public function processActions(): void {
626 841 $action = $this->httpRequest->getQuery( 'action' );
627 - if ( self::ACTION_VALIDATE_SENDER === $action ) {
842 + if ( $action === self::ACTION_VALIDATE_SENDER ) {
628 843 $result = $this->validateSender( $this->optionsProvider->get_sender() );
629 844
630 - if ( true === $result ) {
845 + if ( $result === true ) {
631 846 $this->messageManager->flash_message( __( 'Specified sender has been validated.', 'packeta' ), MessageManager::TYPE_SUCCESS, MessageManager::RENDERER_PACKETERY, 'plugin-options' );
632 847 }
633 848
634 849 $doRedirect = wp_safe_redirect(
@@ -645,16 +860,30 @@
645 860 }
646 861 }
647 862
648 863 $packetStatusSyncForm = $this->createPacketStatusSyncForm();
649 - if ( $packetStatusSyncForm['save']->isSubmittedBy() ) {
864 + if (
865 + $packetStatusSyncForm['save'] instanceof SubmitButton &&
866 + $packetStatusSyncForm['save']->isSubmittedBy()
867 + ) {
650 868 $packetStatusSyncForm->fireEvents();
651 869 }
652 870
653 871 $autoSubmissionForm = $this->createAutoSubmissionForm();
654 - if ( $autoSubmissionForm['save']->isSubmittedBy() ) {
872 + if (
873 + $autoSubmissionForm['save'] instanceof SubmitButton &&
874 + $autoSubmissionForm['save']->isSubmittedBy()
875 + ) {
655 876 $autoSubmissionForm->fireEvents();
656 877 }
878 +
879 + $advancedForm = $this->createAdvancedForm();
880 + if (
881 + $advancedForm['save'] instanceof SubmitButton &&
882 + $advancedForm['save']->isSubmittedBy()
883 + ) {
884 + $advancedForm->fireEvents();
885 + }
657 886 }
658 887
659 888 /**
660 889 * Renders page.
@@ -662,13 +891,15 @@
662 891 public function render(): void {
663 892 $activeTab = ( $this->httpRequest->getQuery( self::PARAM_TAB ) ?? self::TAB_GENERAL );
664 893
665 894 $latteParams = [];
666 - if ( self::TAB_PACKET_STATUS_SYNC === $activeTab ) {
895 + if ( $activeTab === self::TAB_PACKET_STATUS_SYNC ) {
667 896 $latteParams = [ 'form' => $this->createPacketStatusSyncForm() ];
668 - } elseif ( self::TAB_AUTO_SUBMISSION === $activeTab ) {
897 + } elseif ( $activeTab === self::TAB_AUTO_SUBMISSION ) {
669 898 $latteParams = [ 'form' => $this->createAutoSubmissionForm() ];
670 - } elseif ( self::TAB_GENERAL === $activeTab ) {
899 + } elseif ( $activeTab === self::TAB_ADVANCED ) {
900 + $latteParams = [ 'form' => $this->createAdvancedForm() ];
901 + } elseif ( $activeTab === self::TAB_GENERAL ) {
671 902 $latteParams = [ 'form' => $this->create_form() ];
672 903 }
673 904
674 905 if ( ! extension_loaded( 'soap' ) ) {
@@ -674,9 +905,9 @@
674 905 if ( ! extension_loaded( 'soap' ) ) {
675 906 $latteParams['error'] = __( 'This plugin requires an active SOAP library for proper operation. Contact your web hosting administrator.', 'packeta' );
676 907 }
677 908
678 - $latteParams['apiPasswordLink'] = trim( $this->latte_engine->renderToString( PACKETERY_PLUGIN_DIR . '/template/options/help-block-link.latte', [ 'href' => 'https://client.packeta.com/support' ] ) );
909 + $latteParams['apiPasswordLink'] = trim( $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/options/help-block-link.latte', [ 'href' => 'https://client.packeta.com/support' ] ) );
679 910
680 911 $latteParams['exportLink'] = add_query_arg(
681 912 [
682 913 'page' => self::SLUG,
@@ -686,8 +917,9 @@
686 917 );
687 918
688 919 $latteParams['activeTab'] = ( $this->httpRequest->getQuery( self::PARAM_TAB ) ?? self::TAB_GENERAL );
689 920 $latteParams['generalTabLink'] = $this->createLink();
921 + $latteParams['advancedTabLink'] = $this->createLink( self::TAB_ADVANCED );
690 922 $latteParams['supportTabLink'] = $this->createLink( self::TAB_SUPPORT );
691 923 $latteParams['packetStatusSyncTabLink'] = $this->createLink( self::TAB_PACKET_STATUS_SYNC );
692 924 $latteParams['autoSubmissionTabLink'] = $this->createLink( self::TAB_AUTO_SUBMISSION );
693 925
@@ -700,23 +932,32 @@
700 932 get_admin_url( null, 'admin.php' )
701 933 );
702 934
703 935 $lastExport = null;
704 - $lastExportOption = get_option( Exporter::OPTION_LAST_SETTINGS_EXPORT );
705 - if ( false !== $lastExportOption ) {
706 - $date = \DateTime::createFromFormat( DATE_ATOM, $lastExportOption );
707 - if ( false !== $date ) {
936 + $lastExportOption = get_option( OptionNames::LAST_SETTINGS_EXPORT );
937 + if ( $lastExportOption !== false ) {
938 + $date = DateTime::createFromFormat( DATE_ATOM, $lastExportOption );
939 + if ( $date !== false ) {
708 940 $date->setTimezone( wp_timezone() );
709 941 $lastExport = $date->format( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
710 942 }
711 943 }
712 - if ( $lastExport ) {
944 + if ( $lastExport !== null ) {
713 945 $latteParams['lastExport'] = $lastExport;
714 946 }
715 947
716 948 $latteParams['forcePacketCancelDescription'] = __( 'Cancel the packet for an order even if the cancellation in the Packeta system will not be successful.', 'packeta' );
717 949 $latteParams['messages'] = $this->messageManager->renderToString( MessageManager::RENDERER_PACKETERY, 'plugin-options' );
718 - $latteParams['translations'] = [
950 + $latteParams['isCzechLocale'] = $this->moduleHelper->isCzechLocale();
951 + $latteParams['logoZasilkovna'] = $this->urlBuilder->buildAssetUrl( 'public/images/logo-zasilkovna.svg' );
952 + $latteParams['logoPacketa'] = $this->urlBuilder->buildAssetUrl( 'public/images/logo-packeta.svg' );
953 + $advancedCarrierSettingsDescription = sprintf(
954 + // translators: first %s is line break, second one is e-mail address
955 + __( 'BETA: Once enabled, Packeta carriers will appear as separate shipping methods in WooCommerce - Settings - Shipping. After enabling this feature, you will need to set up shipping methods in WooCommerce again.%1$sThis is an experimental feature. If you experience any issues, please email us at %2$s with a description of the issue.', 'packeta' ),
956 + '<br>',
957 + '<a href="mailto:' . $this->supportEmailAddress . '">' . $this->supportEmailAddress . '</a>'
958 + );
959 + $latteParams['translations'] = [
719 960 'packeta' => __( 'Packeta', 'packeta' ),
720 961 'title' => __( 'Options', 'packeta' ),
721 962 'general' => __( 'General', 'packeta' ),
722 963 'packetAutoSubmission' => __( 'Packet auto-submission', 'packeta' ),
@@ -725,8 +966,9 @@
725 966 'apiPasswordCanBeFoundAt%sUrl' => __( 'API password can be found at %s', 'packeta' ),
726 967
727 968 'saveChanges' => __( 'Save changes', 'packeta' ),
728 969 'validateSender' => __( 'Validate sender', 'packeta' ),
970 + 'advanced' => __( 'Advanced', 'packeta' ),
729 971 'support' => __( 'Support', 'packeta' ),
730 972 'optionsExportInfo1' => __(
731 973 '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:',
732 974 'packeta'
@@ -745,10 +987,13 @@
745 987 '</strong>',
746 988 '<a href="https://client.packeta.com/senders" target="_blank">',
747 989 '</a>'
748 990 ),
991 + 'advancedCarrierSettingsDescription' => $advancedCarrierSettingsDescription,
749 992 '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' ),
750 993 'defaultWeightDescription' => __( 'This value is automatically added to the total weight of each order that contains products with zero weight.', 'packeta' ),
994 + 'defaultDimensionsDescription' => __( 'These dimensions will be applied to the packet by default, if required by the carrier.', 'packeta' ),
995 + 'setCheckoutDetectionDescription' => __( 'If you have trouble displaying the widget button in the checkout, you can force what type of checkout you are using.', 'packeta' ),
751 996 'packetStatusSyncTabLinkLabel' => __( 'Packet status tracking', 'packeta' ),
752 997 'statusSyncingOrderStatusesLabel' => __( 'Order statuses, for which cron will check the packet status', 'packeta' ),
753 998 'statusSyncingOrderStatusesDescription' => __( 'Cron will automatically track all orders with these statuses and check if the shipment status has changed.', 'packeta' ),
754 999 'statusSyncingPacketStatusesLabel' => __( 'Packet statuses that are being checked', 'packeta' ),
@@ -755,11 +1000,14 @@
755 1000 'statusSyncingPacketStatusesDescription' => __( 'If an order has a shipment with one of these selected statuses, the shipment status will be tracked.', 'packeta' ),
756 1001 'numberOfDaysToCheckDescription' => __( 'Number of days after the creation of an order, during which the order status will be checked.', 'packeta' ),
757 1002 'widgetAutoOpenDescription' => __( 'If this option is active, the widget for selecting pickup points will open automatically after selecting the shipping method at the checkout.', 'packeta' ),
758 1003 'autoOrderStatusChangeDescription' => __( 'Change order status after data submission to Packeta.', 'packeta' ),
1004 + 'freeShippingTextDescription' => __( 'If enabled, "FREE" will be displayed after the name of the shipping method, if free shipping is applied.', 'packeta' ),
1005 + 'orderStatusChangeSettings' => __( 'Order status change settings', 'packeta' ),
1006 + 'dimensionsLabel' => __( 'Dimensions', 'packeta' ),
759 1007 ];
760 1008
761 - $this->latte_engine->render( PACKETERY_PLUGIN_DIR . '/template/options/page.latte', $latteParams );
1009 + $this->latteEngine->render( PACKETERY_PLUGIN_DIR . '/template/options/page.latte', $latteParams );
762 1010 }
763 1011
764 1012 /**
765 1013 * Creates tab link.
@@ -771,9 +1019,9 @@
771 1019 $params = [
772 1020 'page' => self::SLUG,
773 1021 ];
774 1022
775 - if ( null !== $tab ) {
1023 + if ( $tab !== null ) {
776 1024 $params[ self::PARAM_TAB ] = $tab;
777 1025 }
778 1026
779 1027 return add_query_arg(