PluginProbe
Packeta / 1.6.0
Packeta v1.6.0
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.6.0, at src/Packetery/Module/Options/Provider.php

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