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