PluginProbe
Packeta / 1.5.2
Packeta v1.5.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 / Provider.php

Provider.php in Packeta 1.5.2, at src/Packetery/Module/Options/Provider.php

560 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Provider
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Options;
11
12 use Packetery\Core\Entity\PacketStatus;
13 use Packetery\Module\Order\PacketSynchronizer;
14
15 /**
16 * Class Provider
17 *
18 * @package Packetery
19 */
20 class Provider {
21
22 const OPTION_NAME_PACKETERY = 'packetery';
23 const OPTION_NAME_PACKETERY_SYNC = 'packetery_sync';
24 const OPTION_NAME_PACKETERY_AUTO_SUBMISSION = 'packetery_auto_submission';
25
26 const DEFAULT_VALUE_PACKETA_LABEL_FORMAT = 'A6 on A4';
27 const DEFAULT_VALUE_CARRIER_LABEL_FORMAT = self::DEFAULT_VALUE_PACKETA_LABEL_FORMAT;
28 const MAX_STATUS_SYNCING_PACKETS_DEFAULT = 100;
29 const MAX_DAYS_OF_PACKET_STATUS_SYNCING_DEFAULT = 14;
30 const FORCE_PACKET_CANCEL_DEFAULT = true;
31 const PACKET_AUTO_SUBMISSION_ALLOWED_DEFAULT = false;
32 const WIDGET_AUTO_OPEN_DEFAULT = false;
33 const ORDER_STATUS_AUTO_CHANGE_DEFAULT = false;
34 const AUTO_ORDER_STATUS_DEFAULT = '';
35 const ORDER_STATUS_AUTO_CHANGE_FOR_AUTO_SUBMIT_AT_FRONTEND_DEFAULT = false;
36 const AUTO_ORDER_STATUS = 'auto_order_status';
37
38 /**
39 * Options data.
40 *
41 * @var array
42 */
43 private $data;
44
45 /**
46 * Sync data.
47 *
48 * @var array
49 */
50 private $syncData;
51
52 /**
53 * Auto submission data.
54 *
55 * @var array
56 */
57 private $autoSubmissionData;
58
59 /**
60 * Provider constructor.
61 */
62 public function __construct() {
63 $data = get_option( self::OPTION_NAME_PACKETERY );
64 if ( ! $data ) {
65 $data = array();
66 }
67
68 $syncData = get_option( self::OPTION_NAME_PACKETERY_SYNC );
69 if ( ! $syncData ) {
70 $syncData = [];
71 }
72
73 $autoSubmissionData = get_option( self::OPTION_NAME_PACKETERY_AUTO_SUBMISSION );
74 if ( ! $autoSubmissionData ) {
75 $autoSubmissionData = [];
76 }
77
78 $this->data = $data;
79 $this->syncData = $syncData;
80 $this->autoSubmissionData = $autoSubmissionData;
81 }
82
83 /**
84 * Casts data to array.
85 *
86 * @param string $optionName Option name of settings.
87 *
88 * @return array Data.
89 */
90 public function data_to_array( ?string $optionName = null ): array {
91 if ( self::OPTION_NAME_PACKETERY === $optionName ) {
92 return $this->data;
93 }
94
95 if ( self::OPTION_NAME_PACKETERY_SYNC === $optionName ) {
96 return $this->syncData;
97 }
98
99 if ( self::OPTION_NAME_PACKETERY_AUTO_SUBMISSION === $optionName ) {
100 return $this->autoSubmissionData;
101 }
102
103 if ( null === $optionName ) {
104 return [
105 self::OPTION_NAME_PACKETERY => $this->data,
106 self::OPTION_NAME_PACKETERY_SYNC => $this->syncData,
107 self::OPTION_NAME_PACKETERY_AUTO_SUBMISSION => $this->autoSubmissionData,
108 ];
109 }
110 }
111
112 /**
113 * Tells if provider has any data,
114 *
115 * @param string $optionName Option name of settings.
116 *
117 * @return bool Has any data.
118 */
119 public function has_any( string $optionName ): bool {
120 return ! empty( $this->data_to_array( $optionName ) );
121 }
122
123 /**
124 * Gets content from options array.
125 *
126 * @param string $key Options array key.
127 *
128 * @return mixed|null Content.
129 */
130 private function get( string $key ) {
131 return ( $this->data[ $key ] ?? null );
132 }
133
134 /**
135 * API key dynamically crafted from API password.
136 *
137 * @return string|null Content.
138 */
139 public function get_api_key(): ?string {
140 return $this->get( 'api_key' );
141 }
142
143 /**
144 * API password from client section.
145 *
146 * @return string|null Content.
147 */
148 public function get_api_password(): ?string {
149 return $this->get( 'api_password' );
150 }
151
152 /**
153 * Sender.
154 *
155 * @return string|null Content.
156 */
157 public function get_sender(): ?string {
158 return $this->get( 'sender' );
159 }
160
161 /**
162 * Carrier label format.
163 *
164 * @return string Content.
165 */
166 public function get_carrier_label_format(): string {
167 return $this->get( 'carrier_label_format' ) ?? self::DEFAULT_VALUE_CARRIER_LABEL_FORMAT;
168 }
169
170 /**
171 * Packeta label format.
172 *
173 * @return string Content.
174 */
175 public function get_packeta_label_format(): string {
176 return $this->get( 'packeta_label_format' ) ?? self::DEFAULT_VALUE_PACKETA_LABEL_FORMAT;
177 }
178
179 /**
180 * Does user allow label emailing?
181 *
182 * @return bool|null Content.
183 */
184 public function get_allow_label_emailing(): ?bool {
185 return (bool) $this->get( 'allow_label_emailing' );
186 }
187
188 /**
189 * Which payment rate id COD?
190 *
191 * @return string|null Content.
192 */
193 public function getCodPaymentMethod(): ?string {
194 $value = $this->get( 'cod_payment_method' );
195 if ( ! $value ) {
196 return null;
197 }
198
199 return $value;
200 }
201
202 /**
203 * Returns the location of the widget button in the cart
204 *
205 * @return string|null
206 */
207 public function getCheckoutWidgetButtonLocation(): ?string {
208 $value = $this->get( 'checkout_widget_button_location' );
209 if ( ! $value ) {
210 return null;
211 }
212
213 return $value;
214 }
215
216 /**
217 * Order packaging weight.
218 *
219 * @return float
220 */
221 public function getPackagingWeight(): float {
222 return (float) $this->get( 'packaging_weight' );
223 }
224
225 /**
226 * Order default weight enabled.
227 *
228 * @return bool
229 */
230 public function isDefaultWeightEnabled(): bool {
231 return (bool) $this->get( 'default_weight_enabled' );
232 }
233
234 /**
235 * Order default weight.
236 *
237 * @return float
238 */
239 public function getDefaultWeight(): float {
240 if ( $this->get( 'default_weight' ) === null ) {
241 return 0.0;
242 }
243 return (float) $this->get( 'default_weight' );
244 }
245
246 /**
247 * Max syncing packets.
248 *
249 * @return int
250 */
251 public function getMaxStatusSyncingPackets(): int {
252 $value = ( $this->syncData['max_status_syncing_packets'] ?? null );
253 if ( is_numeric( $value ) ) {
254 return (int) $value;
255 }
256
257 return self::MAX_STATUS_SYNCING_PACKETS_DEFAULT;
258 }
259
260 /**
261 * Max days of packet status syncing.
262 *
263 * @return int
264 */
265 public function getMaxDaysOfPacketStatusSyncing(): int {
266 $value = ( $this->syncData['max_days_of_packet_status_syncing'] ?? null );
267 if ( is_numeric( $value ) ) {
268 return (int) $value;
269 }
270
271 return self::MAX_DAYS_OF_PACKET_STATUS_SYNCING_DEFAULT;
272 }
273
274 /**
275 * Status syncing order statuses.
276 *
277 * @return array
278 */
279 public function getStatusSyncingOrderStatuses(): array {
280 $value = ( $this->syncData['status_syncing_order_statuses'] ?? null );
281 if ( is_array( $value ) ) {
282 return $value;
283 }
284
285 return [];
286 }
287
288 /**
289 * Status syncing order statuses.
290 *
291 * @return array
292 */
293 public function getExistingStatusSyncingOrderStatuses(): array {
294 $statuses = $this->getStatusSyncingOrderStatuses();
295 $choices = array_column( Page::getOrderStatusesChoiceData(), 'key' );
296
297 return array_intersect( $statuses, $choices );
298 }
299
300 /**
301 * Status syncing packet statuses.
302 *
303 * @return array
304 */
305 public function getStatusSyncingPacketStatuses(): array {
306 $value = ( $this->syncData['status_syncing_packet_statuses'] ?? null );
307 if ( is_array( $value ) ) {
308 return $value;
309 }
310
311 return array_keys(
312 array_filter(
313 PacketSynchronizer::getPacketStatuses(),
314 static function ( PacketStatus $packetStatus ): bool {
315 return true === $packetStatus->hasDefaultSynchronization();
316 }
317 )
318 );
319 }
320
321 /**
322 * Transform shipping address to contain pickup point address?
323 *
324 * @return bool
325 */
326 public function replaceShippingAddressWithPickupPointAddress(): bool {
327 return (bool) $this->get( 'replace_shipping_address_with_pickup_point_address' );
328 }
329
330 /**
331 * Tells if packet cancellation should be forced.
332 *
333 * @return bool
334 */
335 public function isPacketCancellationForced(): bool {
336 $value = $this->get( 'force_packet_cancel' );
337 if ( null !== $value ) {
338 return (bool) $value;
339 }
340
341 return self::FORCE_PACKET_CANCEL_DEFAULT;
342 }
343
344 /**
345 * Gets packet auto submission payment method and event mapping.
346 *
347 * @return array
348 */
349 private function getPacketAutoSubmissionPaymentMethodEventsMapping(): array {
350 return $this->autoSubmissionData['payment_method_events'] ?? [];
351 }
352
353 /**
354 * Gets array of mapped events.
355 *
356 * @return string[]
357 */
358 public function getPacketAutoSubmissionMappedUniqueEvents(): array {
359 $mapping = $this->getPacketAutoSubmissionPaymentMethodEventsMapping();
360 $result = [];
361
362 foreach ( $mapping as $gatewayMapping ) {
363 $result[ $gatewayMapping['event'] ] = $gatewayMapping['event'];
364 }
365
366 return $result;
367 }
368
369 /**
370 * Gets packet auto-submission event by payment gateway ID.
371 *
372 * @param string $paymentGatewayId Payment gateway ID.
373 *
374 * @return string|null
375 */
376 public function getPacketAutoSubmissionEventForPaymentGateway( string $paymentGatewayId ): ?string {
377 return $this->getPacketAutoSubmissionPaymentMethodEventsMapping()[ $paymentGatewayId ]['event'] ?? null;
378 }
379
380 /**
381 * Tells if packet auto submission is enabled.
382 *
383 * @return bool
384 */
385 public function isPacketAutoSubmissionEnabled(): bool {
386 $value = $this->autoSubmissionData['allow'] ?? null;
387 if ( null !== $value ) {
388 return (bool) $value;
389 }
390
391 return self::PACKET_AUTO_SUBMISSION_ALLOWED_DEFAULT;
392 }
393
394 /**
395 * Provides available labels.
396 *
397 * @return array[]
398 */
399 public function getLabelFormats(): array {
400 return [
401 'A6 on A4' => [
402 'name' => __( '1/4 A4, print on A4, 4pcs/page', 'packeta' ),
403 'directLabels' => true,
404 'maxOffset' => 3,
405 ],
406 'A6 on A6' => [
407 'name' => __( '1/4 A4, direct print, 1pc/page', 'packeta' ),
408 'directLabels' => true,
409 'maxOffset' => 0,
410 ],
411 'A7 on A7' => [
412 'name' => __( '1/8 A4, direct print, 1pc/page', 'packeta' ),
413 'directLabels' => false,
414 'maxOffset' => 0,
415 ],
416 'A7 on A4' => [
417 'name' => __( '1/8 A4, print on A4, 8pcs/page', 'packeta' ),
418 'directLabels' => false,
419 'maxOffset' => 7,
420 ],
421 '105x35mm on A4' => [
422 'name' => __( '105x35mm, print on A4, 16 pcs/page', 'packeta' ),
423 'directLabels' => false,
424 'maxOffset' => 15,
425 ],
426 'A8 on A8' => [
427 'name' => __( '1/16 A4, direct print, 1pc/page', 'packeta' ),
428 'directLabels' => false,
429 'maxOffset' => 0,
430 ],
431 ];
432 }
433
434 /**
435 * Gets maximum offset for selected packeta labels format.
436 *
437 * @param string $format Selected format.
438 *
439 * @return int
440 */
441 public function getLabelMaxOffset( string $format ): int {
442 if ( '' === $format ) {
443 return 0;
444 }
445 $availableFormats = $this->getLabelFormats();
446
447 return $availableFormats[ $format ]['maxOffset'];
448 }
449
450 /**
451 * Gets list of packeta labels for select creation.
452 *
453 * @return array
454 */
455 public function getPacketaLabelFormats(): array {
456 $availableFormats = $this->getLabelFormats();
457
458 return array_filter( array_combine( array_keys( $availableFormats ), array_column( $availableFormats, 'name' ) ) );
459 }
460
461 /**
462 * Gets list of carrier labels for select creation.
463 *
464 * @return array
465 */
466 public function getCarrierLabelFormat(): array {
467 $availableFormats = $this->getLabelFormats();
468 $carrierLabelFormats = [];
469 foreach ( $availableFormats as $format => $formatData ) {
470 if ( true === $formatData['directLabels'] ) {
471 $carrierLabelFormats[ $format ] = $formatData['name'];
472 }
473 }
474
475 return $carrierLabelFormats;
476 }
477
478 /**
479 * Tells if widget should open automatically.
480 *
481 * @return bool
482 */
483 public function shouldWidgetOpenAutomatically(): bool {
484 $value = $this->get( 'widget_auto_open' );
485 if ( null !== $value ) {
486 return (bool) $value;
487 }
488
489 return self::WIDGET_AUTO_OPEN_DEFAULT;
490 }
491
492 /**
493 * Auto order status change on packet submit enabled.
494 *
495 * @return bool
496 */
497 public function isOrderStatusAutoChangeEnabled(): bool {
498 $orderStatusAutoChange = $this->get( 'order_status_auto_change' );
499 if ( null !== $orderStatusAutoChange ) {
500 return (bool) $orderStatusAutoChange;
501 }
502
503 return self::ORDER_STATUS_AUTO_CHANGE_DEFAULT;
504 }
505
506 /**
507 * Auto order status change on packet auto submit at frontend enabled.
508 *
509 * @return bool
510 */
511 public function isOrderStatusAutoChangeForAutoSubmitAtFrontendEnabled(): bool {
512 if ( false === $this->isOrderStatusAutoChangeEnabled() ) {
513 return false;
514 }
515
516 $orderStatusAutoChnageForAutoSubmitAtFrontend = $this->get( 'order_status_auto_change_for_auto_submit_at_frontend' );
517 if ( null !== $orderStatusAutoChnageForAutoSubmitAtFrontend ) {
518 return (bool) $orderStatusAutoChnageForAutoSubmitAtFrontend;
519 }
520
521 return self::ORDER_STATUS_AUTO_CHANGE_FOR_AUTO_SUBMIT_AT_FRONTEND_DEFAULT;
522 }
523
524 /**
525 * Tells auto order status, if it is valid, otherwise empty string.
526 *
527 * @return string
528 */
529 public function getValidAutoOrderStatus(): string {
530 $autoOrderStatus = $this->getAutoOrderStatus();
531 if ( wc_is_order_status( $autoOrderStatus ) ) {
532 return $autoOrderStatus;
533 }
534
535 return self::AUTO_ORDER_STATUS_DEFAULT;
536 }
537
538 /**
539 * Tells auto order status.
540 *
541 * @return string|null
542 */
543 public function getAutoOrderStatus(): ?string {
544 return $this->get( self::AUTO_ORDER_STATUS );
545 }
546
547 /**
548 * Performs replacements needed by Nette form to pass validation, see https://github.com/dg/nette-component-model/blob/master/src/ComponentModel/Container.php#L50 .
549 * There may be an edge case where a replacement causes a conflict with another method. We do not address this issue yet.
550 *
551 * @param string $id Payment gateway id.
552 *
553 * @return string
554 */
555 public function sanitizePaymentGatewayId( string $id ): string {
556 return preg_replace( '/\W/', '_', $id );
557 }
558
559 }
560