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