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