| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class OptionsPage |
| 4 |
* |
| 5 |
* @package Packetery\Options |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Carrier; |
| 11 |
|
| 12 |
use Packetery\Core\Entity\Carrier; |
| 13 |
use Packetery\Core\Helper; |
| 14 |
use Packetery\Core\Rounder; |
| 15 |
use Packetery\Module\FormFactory; |
| 16 |
use Packetery\Module\FormValidators; |
| 17 |
use Packetery\Module\MessageManager; |
| 18 |
use Packetery\Module\Options\FeatureFlagManager; |
| 19 |
use PacketeryLatte\Engine; |
| 20 |
use PacketeryNette\Forms\Container; |
| 21 |
use PacketeryNette\Forms\Form; |
| 22 |
use PacketeryNette\Http\Request; |
| 23 |
|
| 24 |
/** |
| 25 |
* Class OptionsPage |
| 26 |
* |
| 27 |
* @package Packetery\Options |
| 28 |
*/ |
| 29 |
class OptionsPage { |
| 30 |
|
| 31 |
public const FORM_FIELD_NAME = 'name'; |
| 32 |
public const SLUG = 'packeta-country'; |
| 33 |
public const MINIMUM_CHECKED_VENDORS = 2; |
| 34 |
|
| 35 |
/** |
| 36 |
* PacketeryLatte_engine. |
| 37 |
* |
| 38 |
* @var Engine PacketeryLatte engine. |
| 39 |
*/ |
| 40 |
private $latteEngine; |
| 41 |
|
| 42 |
/** |
| 43 |
* Carrier repository. |
| 44 |
* |
| 45 |
* @var EntityRepository Carrier repository. |
| 46 |
*/ |
| 47 |
private $carrierRepository; |
| 48 |
|
| 49 |
/** |
| 50 |
* Form factory. |
| 51 |
* |
| 52 |
* @var FormFactory Form factory. |
| 53 |
*/ |
| 54 |
private $formFactory; |
| 55 |
|
| 56 |
/** |
| 57 |
* PacketeryNette Request. |
| 58 |
* |
| 59 |
* @var Request PacketeryNette Request. |
| 60 |
*/ |
| 61 |
private $httpRequest; |
| 62 |
|
| 63 |
/** |
| 64 |
* CountryListingPage. |
| 65 |
* |
| 66 |
* @var CountryListingPage CountryListingPage. |
| 67 |
*/ |
| 68 |
private $countryListingPage; |
| 69 |
|
| 70 |
/** |
| 71 |
* Message manager. |
| 72 |
* |
| 73 |
* @var MessageManager |
| 74 |
*/ |
| 75 |
private $messageManager; |
| 76 |
|
| 77 |
/** |
| 78 |
* Internal pickup points config. |
| 79 |
* |
| 80 |
* @var PacketaPickupPointsConfig |
| 81 |
*/ |
| 82 |
private $pickupPointsConfig; |
| 83 |
|
| 84 |
/** |
| 85 |
* Feature flag. |
| 86 |
* |
| 87 |
* @var FeatureFlagManager |
| 88 |
*/ |
| 89 |
private $featureFlag; |
| 90 |
|
| 91 |
/** |
| 92 |
* Plugin constructor. |
| 93 |
* |
| 94 |
* @param Engine $latteEngine PacketeryLatte_engine. |
| 95 |
* @param EntityRepository $carrierRepository Carrier repository. |
| 96 |
* @param FormFactory $formFactory Form factory. |
| 97 |
* @param Request $httpRequest PacketeryNette Request. |
| 98 |
* @param CountryListingPage $countryListingPage CountryListingPage. |
| 99 |
* @param MessageManager $messageManager Message manager. |
| 100 |
* @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config. |
| 101 |
* @param FeatureFlagManager $featureFlag Feature flag. |
| 102 |
*/ |
| 103 |
public function __construct( |
| 104 |
Engine $latteEngine, |
| 105 |
EntityRepository $carrierRepository, |
| 106 |
FormFactory $formFactory, |
| 107 |
Request $httpRequest, |
| 108 |
CountryListingPage $countryListingPage, |
| 109 |
MessageManager $messageManager, |
| 110 |
PacketaPickupPointsConfig $pickupPointsConfig, |
| 111 |
FeatureFlagManager $featureFlag |
| 112 |
) { |
| 113 |
$this->latteEngine = $latteEngine; |
| 114 |
$this->carrierRepository = $carrierRepository; |
| 115 |
$this->formFactory = $formFactory; |
| 116 |
$this->httpRequest = $httpRequest; |
| 117 |
$this->countryListingPage = $countryListingPage; |
| 118 |
$this->messageManager = $messageManager; |
| 119 |
$this->pickupPointsConfig = $pickupPointsConfig; |
| 120 |
$this->featureFlag = $featureFlag; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Registers WP callbacks. |
| 125 |
*/ |
| 126 |
public function register(): void { |
| 127 |
add_submenu_page( |
| 128 |
\Packetery\Module\Options\Page::SLUG, |
| 129 |
__( 'Carrier settings', 'packeta' ), |
| 130 |
__( 'Carrier settings', 'packeta' ), |
| 131 |
'manage_options', |
| 132 |
self::SLUG, |
| 133 |
array( |
| 134 |
$this, |
| 135 |
'render', |
| 136 |
), |
| 137 |
10 |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Creates settings form. |
| 143 |
* |
| 144 |
* @param array $carrierData Carrier data. |
| 145 |
* |
| 146 |
* @return Form |
| 147 |
*/ |
| 148 |
private function createForm( array $carrierData ): Form { |
| 149 |
$optionId = OptionPrefixer::getOptionId( $carrierData['id'] ); |
| 150 |
|
| 151 |
$form = $this->formFactory->create( $optionId ); |
| 152 |
|
| 153 |
$form->addCheckbox( |
| 154 |
'active', |
| 155 |
__( 'Active carrier', 'packeta' ) . ':' |
| 156 |
); |
| 157 |
|
| 158 |
$form->addText( self::FORM_FIELD_NAME, __( 'Display name', 'packeta' ) . ':' ) |
| 159 |
->setRequired(); |
| 160 |
|
| 161 |
$carrierOptions = get_option( $optionId ); |
| 162 |
if ( $this->featureFlag->isSplitActive() ) { |
| 163 |
$vendorCheckboxes = $this->getVendorCheckboxesConfig( $carrierData['id'], ( $carrierOptions ? $carrierOptions : null ) ); |
| 164 |
if ( $vendorCheckboxes ) { |
| 165 |
$vendorsContainer = $form->addContainer( 'vendor_groups' ); |
| 166 |
foreach ( $vendorCheckboxes as $checkboxConfig ) { |
| 167 |
$checkboxControl = $vendorsContainer->addCheckbox( $checkboxConfig['group'], $checkboxConfig['name'] ); |
| 168 |
if ( true === $checkboxConfig['disabled'] ) { |
| 169 |
$checkboxControl->setDisabled()->setOmitted( false ); |
| 170 |
} |
| 171 |
if ( true === $checkboxConfig['default'] ) { |
| 172 |
$checkboxControl->setDefaultValue( true ); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
$weightLimits = $form->addContainer( 'weight_limits' ); |
| 179 |
if ( empty( $carrierData['weight_limits'] ) ) { |
| 180 |
$this->addWeightLimit( $weightLimits, 0 ); |
| 181 |
} else { |
| 182 |
foreach ( $carrierData['weight_limits'] as $index => $limit ) { |
| 183 |
$this->addWeightLimit( $weightLimits, $index ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
$carrier = $this->carrierRepository->getAnyById( (string) $carrierData['id'] ); |
| 188 |
|
| 189 |
if ( null !== $carrier && $carrier->supportsCod() ) { |
| 190 |
$form->addText( 'default_COD_surcharge', __( 'Default COD surcharge', 'packeta' ) . ':' ) |
| 191 |
->setRequired( false ) |
| 192 |
->addRule( Form::FLOAT ) |
| 193 |
->addRule( Form::MIN, null, 0 ); |
| 194 |
|
| 195 |
$surchargeLimits = $form->addContainer( 'surcharge_limits' ); |
| 196 |
if ( ! empty( $carrierData['surcharge_limits'] ) ) { |
| 197 |
foreach ( $carrierData['surcharge_limits'] as $index => $limit ) { |
| 198 |
$this->addSurchargeLimit( $surchargeLimits, $index ); |
| 199 |
} |
| 200 |
} |
| 201 |
$roundingOptions = [ |
| 202 |
Rounder::DONT_ROUND => __( 'No rounding', 'packeta' ), |
| 203 |
Rounder::ROUND_DOWN => __( 'Always round down', 'packeta' ), |
| 204 |
Rounder::ROUND_UP => __( 'Always round up', 'packeta' ), |
| 205 |
]; |
| 206 |
$form->addSelect( 'cod_rounding', __( 'COD rounding', 'packeta' ) . ':', $roundingOptions ) |
| 207 |
->setDefaultValue( Rounder::DONT_ROUND ); |
| 208 |
} |
| 209 |
|
| 210 |
$item = $form->addText( 'free_shipping_limit', __( 'Free shipping limit', 'packeta' ) . ':' ); |
| 211 |
$item->addRule( $form::FLOAT, __( 'Please enter a valid decimal number.', 'packeta' ) ); |
| 212 |
|
| 213 |
$couponFreeShipping = $form->addContainer( 'coupon_free_shipping' ); |
| 214 |
$couponFreeShipping->addCheckbox( 'active', __( 'Apply free shipping coupon', 'packeta' ) ); |
| 215 |
$couponFreeShipping->addCheckbox( 'allow_for_fees', __( 'Apply free shipping coupon for fees', 'packeta' ) ) |
| 216 |
->addConditionOn( $form['coupon_free_shipping']['active'], Form::FILLED ) |
| 217 |
->toggle( $this->createCouponFreeShippingForFeesContainerId( $form ) ); |
| 218 |
|
| 219 |
$form->addHidden( 'id' )->setRequired(); |
| 220 |
$form->addSubmit( 'save' ); |
| 221 |
|
| 222 |
if ( false === $carrier->hasPickupPoints() && in_array( $carrier->getCountry(), Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) { |
| 223 |
$addressValidationOptions = [ |
| 224 |
'none' => __( 'No address validation', 'packeta' ), |
| 225 |
'optional' => __( 'Optional address validation', 'packeta' ), |
| 226 |
'required' => __( 'Required address validation', 'packeta' ), |
| 227 |
]; |
| 228 |
$form->addSelect( 'address_validation', __( 'Address validation', 'packeta' ) . ':', $addressValidationOptions ) |
| 229 |
->setDefaultValue( 'none' ); |
| 230 |
} |
| 231 |
|
| 232 |
if ( $carrier->supportsAgeVerification() ) { |
| 233 |
$form->addText( 'age_verification_fee', __( 'Age verification fee', 'packeta' ) . ':' ) |
| 234 |
->setRequired( false ) |
| 235 |
->addRule( Form::FLOAT ) |
| 236 |
->addRule( Form::MIN, null, 0 ); |
| 237 |
} |
| 238 |
|
| 239 |
$form->onValidate[] = [ $this, 'validateOptions' ]; |
| 240 |
$form->onSuccess[] = [ $this, 'updateOptions' ]; |
| 241 |
|
| 242 |
if ( false === $carrierOptions ) { |
| 243 |
$carrierOptions = [ |
| 244 |
'id' => $carrierData['id'], |
| 245 |
self::FORM_FIELD_NAME => $carrierData['name'], |
| 246 |
]; |
| 247 |
} |
| 248 |
$form->setDefaults( $carrierOptions ); |
| 249 |
|
| 250 |
return $form; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Creates form toggle ID for coupon free shipping. |
| 255 |
* |
| 256 |
* @param Form $form Form. |
| 257 |
* |
| 258 |
* @return string |
| 259 |
*/ |
| 260 |
private function createCouponFreeShippingForFeesContainerId( Form $form ): string { |
| 261 |
return sprintf( '%s_apply_free_shipping_coupon_allow_for_fees', $form->getName() ); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Creates settings form. |
| 266 |
* |
| 267 |
* @param array $carrierData Carrier data. |
| 268 |
* |
| 269 |
* @return Form |
| 270 |
*/ |
| 271 |
private function createFormTemplate( array $carrierData ): Form { |
| 272 |
$optionId = OptionPrefixer::getOptionId( $carrierData['id'] ); |
| 273 |
|
| 274 |
$form = $this->formFactory->create( $optionId . '_template' ); |
| 275 |
|
| 276 |
$weightLimitsTemplate = $form->addContainer( 'weight_limits' ); |
| 277 |
$this->addWeightLimit( $weightLimitsTemplate, 0 ); |
| 278 |
|
| 279 |
$surchargeLimitsTemplate = $form->addContainer( 'surcharge_limits' ); |
| 280 |
$this->addSurchargeLimit( $surchargeLimitsTemplate, 0 ); |
| 281 |
|
| 282 |
return $form; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Validates options. |
| 287 |
* |
| 288 |
* @param Form $form Form. |
| 289 |
*/ |
| 290 |
public function validateOptions( Form $form ): void { |
| 291 |
if ( $form->hasErrors() ) { |
| 292 |
add_settings_error( '', '', esc_attr( __( 'Some carrier data is invalid', 'packeta' ) ) ); |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
$options = $form->getValues( 'array' ); |
| 297 |
|
| 298 |
if ( $this->featureFlag->isSplitActive() ) { |
| 299 |
$checkedVendors = $this->getCheckedVendors( $options ); |
| 300 |
if ( |
| 301 |
isset( $options['vendor_groups'] ) && |
| 302 |
count( $options['vendor_groups'] ) >= self::MINIMUM_CHECKED_VENDORS && |
| 303 |
count( $checkedVendors ) < self::MINIMUM_CHECKED_VENDORS |
| 304 |
) { |
| 305 |
$vendorMessage = __( 'Check at least two types of pickup points or use a carrier which delivers to the desired pickup point type.', 'packeta' ); |
| 306 |
add_settings_error( 'vendor_groups', 'vendor_groups', esc_attr( $vendorMessage ) ); |
| 307 |
$form->addError( $vendorMessage ); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
$this->checkOverlapping( |
| 312 |
$form, |
| 313 |
$options, |
| 314 |
'weight_limits', |
| 315 |
'weight', |
| 316 |
__( 'Weight rules are overlapping, please fix them.', 'packeta' ) |
| 317 |
); |
| 318 |
if ( isset( $options['surcharge_limits'] ) ) { |
| 319 |
$this->checkOverlapping( |
| 320 |
$form, |
| 321 |
$options, |
| 322 |
'surcharge_limits', |
| 323 |
'order_price', |
| 324 |
__( 'Surcharge rules are overlapping, please fix them.', 'packeta' ) |
| 325 |
); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Saves carrier options. onSuccess callback. |
| 331 |
* |
| 332 |
* @param Form $form Form. |
| 333 |
* |
| 334 |
* @return void |
| 335 |
*/ |
| 336 |
public function updateOptions( Form $form ): void { |
| 337 |
$options = $form->getValues( 'array' ); |
| 338 |
$newVendors = $this->getCheckedVendors( $options ); |
| 339 |
if ( $newVendors ) { |
| 340 |
$options['vendor_groups'] = $newVendors; |
| 341 |
} |
| 342 |
|
| 343 |
$options = $this->mergeNewLimits( $options, 'weight_limits' ); |
| 344 |
$options = $this->sortLimits( $options, 'weight_limits', 'weight' ); |
| 345 |
if ( isset( $options['surcharge_limits'] ) ) { |
| 346 |
$options = $this->mergeNewLimits( $options, 'surcharge_limits' ); |
| 347 |
$options = $this->sortLimits( $options, 'surcharge_limits', 'order_price' ); |
| 348 |
} |
| 349 |
|
| 350 |
update_option( OptionPrefixer::getOptionId( $options['id'] ), $options ); |
| 351 |
$this->messageManager->flash_message( __( 'Settings saved', 'packeta' ), MessageManager::TYPE_SUCCESS, MessageManager::RENDERER_PACKETERY, 'carrier-country' ); |
| 352 |
|
| 353 |
if ( wp_safe_redirect( |
| 354 |
add_query_arg( |
| 355 |
[ |
| 356 |
'page' => self::SLUG, |
| 357 |
'code' => $this->httpRequest->getQuery( 'code' ), |
| 358 |
], |
| 359 |
get_admin_url( null, 'admin.php' ) |
| 360 |
), |
| 361 |
303 |
| 362 |
) ) { |
| 363 |
exit; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Renders page. |
| 369 |
*/ |
| 370 |
public function render(): void { |
| 371 |
$countryIso = $this->httpRequest->getQuery( 'code' ); |
| 372 |
if ( $countryIso ) { |
| 373 |
$countryCarriers = $this->carrierRepository->getByCountryIncludingNonFeed( $countryIso ); |
| 374 |
$carriersData = []; |
| 375 |
$post = $this->httpRequest->getPost(); |
| 376 |
foreach ( $countryCarriers as $carrier ) { |
| 377 |
if ( ! empty( $post ) && $post['id'] === $carrier->getId() ) { |
| 378 |
$formTemplate = $this->createFormTemplate( $post ); |
| 379 |
$form = $this->createForm( $post ); |
| 380 |
if ( $form->isSubmitted() ) { |
| 381 |
$form->fireEvents(); |
| 382 |
} |
| 383 |
} else { |
| 384 |
$carrierData = $carrier->__toArray(); |
| 385 |
$options = get_option( OptionPrefixer::getOptionId( $carrier->getId() ) ); |
| 386 |
if ( false !== $options ) { |
| 387 |
$carrierData += $options; |
| 388 |
} |
| 389 |
$formTemplate = $this->createFormTemplate( $carrierData ); |
| 390 |
$form = $this->createForm( $carrierData ); |
| 391 |
} |
| 392 |
|
| 393 |
$carriersData[] = [ |
| 394 |
'form' => $form, |
| 395 |
'formTemplate' => $formTemplate, |
| 396 |
'carrier' => $carrier, |
| 397 |
'couponFreeShippingForFeesContainerId' => $this->createCouponFreeShippingForFeesContainerId( $form ), |
| 398 |
]; |
| 399 |
} |
| 400 |
|
| 401 |
$this->latteEngine->render( |
| 402 |
PACKETERY_PLUGIN_DIR . '/template/carrier/country.latte', |
| 403 |
[ |
| 404 |
'forms' => $carriersData, |
| 405 |
'country_iso' => $countryIso, |
| 406 |
'globalCurrency' => get_woocommerce_currency_symbol(), |
| 407 |
'flashMessages' => $this->messageManager->renderToString( MessageManager::RENDERER_PACKETERY, 'carrier-country' ), |
| 408 |
'translations' => [ |
| 409 |
'cannotUseThisCarrierBecauseRequiresCustomsDeclaration' => __( 'This carrier cannot be used, because it requires a customs declaration.', 'packeta' ), |
| 410 |
'delete' => __( 'Delete', 'packeta' ), |
| 411 |
'weightRules' => __( 'Weight rules', 'packeta' ), |
| 412 |
'addWeightRule' => __( 'Add weight rule', 'packeta' ), |
| 413 |
'codSurchargeRules' => __( 'COD surcharge rules', 'packeta' ), |
| 414 |
'addCodSurchargeRule' => __( 'Add COD surcharge rule', 'packeta' ), |
| 415 |
'afterExceedingThisAmountShippingIsFree' => __( 'After exceeding this amount, shipping is free.', 'packeta' ), |
| 416 |
'addressValidationDescription' => __( 'Customer address validation.', 'packeta' ), |
| 417 |
'roundingDescription' => __( 'COD rounding for submitting data to Packeta', 'packeta' ), |
| 418 |
'saveChanges' => __( 'Save changes', 'packeta' ), |
| 419 |
'packeta' => __( 'Packeta', 'packeta' ), |
| 420 |
// translators: %s is country code. |
| 421 |
'title' => sprintf( __( 'Country options: %s', 'packeta' ), strtoupper( $countryIso ) ), |
| 422 |
'noKnownCarrierForThisCountry' => __( 'No carriers available for this country.', 'packeta' ), |
| 423 |
'ageVerificationSupportedNotification' => __( 'When shipping via this carrier, you can order the Age Verification service. The service will get ordered automatically if there is at least 1 product in the order with the age verification setting.', 'packeta' ), |
| 424 |
'carrierDoesNotSupportCod' => __( 'This carrier does not support COD payment.', 'packeta' ), |
| 425 |
'allowedPickupPointTypes' => __( 'Pickup point types.', 'packeta' ), |
| 426 |
'checkAtLeastTwo' => __( 'Check at least two types of pickup points or use a carrier which delivers to the desired pickup point type.', 'packeta' ), |
| 427 |
], |
| 428 |
] |
| 429 |
); |
| 430 |
} else { |
| 431 |
$this->countryListingPage->render(); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Transforms new_ keys to common numeric. |
| 437 |
* |
| 438 |
* @param array $options Options to merge. |
| 439 |
* @param string $limitsContainer Container id. |
| 440 |
* |
| 441 |
* @return array |
| 442 |
*/ |
| 443 |
private function mergeNewLimits( array $options, string $limitsContainer ): array { |
| 444 |
$newOptions = []; |
| 445 |
if ( isset( $options[ $limitsContainer ] ) ) { |
| 446 |
foreach ( $options[ $limitsContainer ] as $key => $option ) { |
| 447 |
if ( is_int( $key ) ) { |
| 448 |
$newOptions[ $key ] = $option; |
| 449 |
} |
| 450 |
if ( 0 === strpos( (string) $key, 'new_' ) ) { |
| 451 |
$newOptions[] = $option; |
| 452 |
} |
| 453 |
} |
| 454 |
$options[ $limitsContainer ] = $newOptions; |
| 455 |
} |
| 456 |
|
| 457 |
return $options; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Checks rules overlapping. |
| 462 |
* |
| 463 |
* @param Form $form Form. |
| 464 |
* @param array $options Form data. |
| 465 |
* @param string $limitsContainer Container id. |
| 466 |
* @param string $limitKey Rule id. |
| 467 |
* @param string $overlappingMessage Error message. |
| 468 |
* |
| 469 |
* @return void |
| 470 |
*/ |
| 471 |
private function checkOverlapping( Form $form, array $options, string $limitsContainer, string $limitKey, string $overlappingMessage ): void { |
| 472 |
$limits = array_column( $options[ $limitsContainer ], $limitKey ); |
| 473 |
if ( count( array_unique( $limits, SORT_NUMERIC ) ) !== count( $limits ) ) { |
| 474 |
add_settings_error( $limitsContainer, $limitsContainer, esc_attr( $overlappingMessage ) ); |
| 475 |
$form->addError( $overlappingMessage ); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* Sorts rules. |
| 481 |
* |
| 482 |
* @param array $options Form data. |
| 483 |
* @param string $limitsContainer Container id. |
| 484 |
* @param string $limitKey Rule id. |
| 485 |
* |
| 486 |
* @return array |
| 487 |
*/ |
| 488 |
private function sortLimits( array $options, string $limitsContainer, string $limitKey ): array { |
| 489 |
$limits = array_column( $options[ $limitsContainer ], $limitKey ); |
| 490 |
array_multisort( $limits, SORT_ASC, $options[ $limitsContainer ] ); |
| 491 |
|
| 492 |
return $options; |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* Adds limit fields to form. |
| 497 |
* |
| 498 |
* @param Container $weightLimits Container. |
| 499 |
* @param int|string $index Index. |
| 500 |
* |
| 501 |
* @return void |
| 502 |
*/ |
| 503 |
private function addWeightLimit( Container $weightLimits, $index ): void { |
| 504 |
$limit = $weightLimits->addContainer( (string) $index ); |
| 505 |
$item = $limit->addText( 'weight', __( 'Weight up to', 'packeta' ) . ':' ); |
| 506 |
$item->setRequired(); |
| 507 |
$item->addRule( Form::FLOAT, __( 'Please enter a valid decimal number.', 'packeta' ) ); |
| 508 |
// translators: %d is numeric threshold. |
| 509 |
$item->addRule( [ FormValidators::class, 'greaterThan' ], __( 'Enter number greater than %d', 'packeta' ), 0.0 ); |
| 510 |
|
| 511 |
$item->addFilter( |
| 512 |
function ( float $value ) { |
| 513 |
return Helper::simplifyWeight( $value ); |
| 514 |
} |
| 515 |
); |
| 516 |
// translators: %d is numeric threshold. |
| 517 |
$item->addRule( [ FormValidators::class, 'greaterThan' ], __( 'Enter number greater than %d', 'packeta' ), 0.0 ); |
| 518 |
|
| 519 |
$item = $limit->addText( 'price', __( 'Price', 'packeta' ) . ':' ); |
| 520 |
$item->setRequired(); |
| 521 |
$item->addRule( Form::FLOAT, __( 'Please enter a valid decimal number.', 'packeta' ) ); |
| 522 |
$item->addRule( Form::MIN, null, 0 ); |
| 523 |
} |
| 524 |
|
| 525 |
/** |
| 526 |
* Adds limit fields to form. |
| 527 |
* |
| 528 |
* @param Container $surchargeLimits Container. |
| 529 |
* @param int|string $index Index. |
| 530 |
* |
| 531 |
* @return void |
| 532 |
*/ |
| 533 |
private function addSurchargeLimit( Container $surchargeLimits, $index ): void { |
| 534 |
$limit = $surchargeLimits->addContainer( (string) $index ); |
| 535 |
$item = $limit->addText( 'order_price', __( 'Order price up to', 'packeta' ) . ':' ); |
| 536 |
$item->setRequired(); |
| 537 |
$item->addRule( Form::FLOAT, __( 'Please enter a valid decimal number.', 'packeta' ) ); |
| 538 |
$item->addRule( Form::MIN, null, 0 ); |
| 539 |
$item->addCondition( Form::MAX, 0 ) |
| 540 |
->addCondition( Form::MIN, 0 ) |
| 541 |
// translators: %d is the value. |
| 542 |
->addRule( Form::BLANK, __( 'Value must not be %d', 'packeta' ), 0 ); |
| 543 |
|
| 544 |
$item = $limit->addText( 'surcharge', __( 'Surcharge', 'packeta' ) . ':' ); |
| 545 |
$item->setRequired(); |
| 546 |
$item->addRule( Form::FLOAT, __( 'Please enter a valid decimal number.', 'packeta' ) ); |
| 547 |
$item->addRule( Form::MIN, null, 0 ); |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* Creates link to page. |
| 552 |
* |
| 553 |
* @param string|null $countryCode Country code. |
| 554 |
* |
| 555 |
* @return string |
| 556 |
*/ |
| 557 |
public function createUrl( ?string $countryCode = null ): string { |
| 558 |
$params = [ |
| 559 |
'page' => self::SLUG, |
| 560 |
]; |
| 561 |
|
| 562 |
if ( null !== $countryCode ) { |
| 563 |
$params['code'] = $countryCode; |
| 564 |
} |
| 565 |
|
| 566 |
return add_query_arg( |
| 567 |
$params, |
| 568 |
admin_url( 'admin.php' ) |
| 569 |
); |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Gets checked vendors. |
| 574 |
* |
| 575 |
* @param array $options Form options. |
| 576 |
* |
| 577 |
* @return array |
| 578 |
*/ |
| 579 |
private function getCheckedVendors( array $options ): array { |
| 580 |
$vendorCodes = []; |
| 581 |
if ( ! empty( $options['vendor_groups'] ) ) { |
| 582 |
$vendorCodes = $options['vendor_groups']; |
| 583 |
} |
| 584 |
|
| 585 |
$newVendors = []; |
| 586 |
foreach ( $vendorCodes as $vendorId => $isChecked ) { |
| 587 |
if ( ! $isChecked ) { |
| 588 |
continue; |
| 589 |
} |
| 590 |
$newVendors[] = $vendorId; |
| 591 |
} |
| 592 |
|
| 593 |
return $newVendors; |
| 594 |
} |
| 595 |
|
| 596 |
/** |
| 597 |
* Gets available vendors for compound carrier. |
| 598 |
* |
| 599 |
* @param string $id Compound carrier id. |
| 600 |
* |
| 601 |
* @return array|null |
| 602 |
*/ |
| 603 |
private function getAvailableVendors( $id ): ?array { |
| 604 |
if ( ! $this->pickupPointsConfig->isCompoundCarrierId( $id ) ) { |
| 605 |
return null; |
| 606 |
} |
| 607 |
|
| 608 |
$compoundCarriers = $this->pickupPointsConfig->getCompoundCarriers(); |
| 609 |
foreach ( $compoundCarriers as $compoundCarrier ) { |
| 610 |
if ( $id === $compoundCarrier->getId() ) { |
| 611 |
return $compoundCarrier->getVendorCodes(); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
return null; |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
* Gets configuration of vendor checkboxes. |
| 620 |
* |
| 621 |
* @param string $carrierId Carrier id. |
| 622 |
* @param array|null $carrierOptions Carrier options. |
| 623 |
* |
| 624 |
* @return array |
| 625 |
*/ |
| 626 |
private function getVendorCheckboxesConfig( string $carrierId, ?array $carrierOptions ): array { |
| 627 |
$availableVendors = $this->getAvailableVendors( $carrierId ); |
| 628 |
if ( null === $availableVendors ) { |
| 629 |
return []; |
| 630 |
} |
| 631 |
|
| 632 |
$vendorCheckboxes = []; |
| 633 |
$vendorCarriers = $this->pickupPointsConfig->getVendorCarriers(); |
| 634 |
foreach ( $availableVendors as $vendorId ) { |
| 635 |
$vendorProvider = $vendorCarriers[ $vendorId ]; |
| 636 |
$checkbox = [ |
| 637 |
'group' => $vendorProvider->getGroup(), |
| 638 |
'name' => $vendorProvider->getName(), |
| 639 |
'disabled' => null, |
| 640 |
'default' => null, |
| 641 |
]; |
| 642 |
$hasLowCountAvailable = count( $availableVendors ) <= self::MINIMUM_CHECKED_VENDORS; |
| 643 |
if ( $hasLowCountAvailable ) { |
| 644 |
$checkbox['disabled'] = true; |
| 645 |
} |
| 646 |
$hasGroupSettingsSaved = isset( $carrierOptions['vendor_groups'] ); |
| 647 |
$hasTheGroupAllowed = ( |
| 648 |
$hasGroupSettingsSaved && |
| 649 |
in_array( $vendorProvider->getGroup(), $carrierOptions['vendor_groups'], true ) |
| 650 |
); |
| 651 |
if ( ! $hasGroupSettingsSaved || $hasLowCountAvailable || $hasTheGroupAllowed ) { |
| 652 |
$checkbox['default'] = true; |
| 653 |
} |
| 654 |
$vendorCheckboxes[] = $checkbox; |
| 655 |
} |
| 656 |
|
| 657 |
return $vendorCheckboxes; |
| 658 |
} |
| 659 |
|
| 660 |
} |
| 661 |
|