| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\Services; |
| 4 |
|
| 5 |
use WC_Validation; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class ValidationService |
| 9 |
* |
| 10 |
* @package MultiSafepay\WooCommerce\Services |
| 11 |
*/ |
| 12 |
class ValidationService { |
| 13 |
|
| 14 |
/** |
| 15 |
* Validate a zip code using WooCommerce's validation method |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function validate_postcode(): void { |
| 20 |
check_ajax_referer( 'multisafepay_validator_nonce', 'security' ); |
| 21 |
|
| 22 |
$postcode = isset( $_POST['postcode'] ) ? sanitize_text_field( wp_unslash( $_POST['postcode'] ) ) : ''; |
| 23 |
$country = isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( $_POST['country'] ) ) : ''; |
| 24 |
|
| 25 |
// If WC_Validation is not available, consider it valid and finish |
| 26 |
if ( ! class_exists( 'WC_Validation' ) || ! method_exists( 'WC_Validation', 'is_postcode' ) ) { |
| 27 |
wp_send_json( |
| 28 |
array( |
| 29 |
'success' => true, |
| 30 |
'valid' => true, |
| 31 |
'error' => 'WC_Validation class not available', |
| 32 |
) |
| 33 |
); |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
// Use the WooCommerce validation class to validate the zip code |
| 38 |
$is_valid = WC_Validation::is_postcode( $postcode, $country ); |
| 39 |
|
| 40 |
wp_send_json( |
| 41 |
array( |
| 42 |
'success' => $is_valid, |
| 43 |
'valid' => $is_valid, |
| 44 |
'postcode' => $postcode, |
| 45 |
'country' => $country, |
| 46 |
) |
| 47 |
); |
| 48 |
} |
| 49 |
} |
| 50 |
|