Created
March 17, 2020 12:53
-
-
Save gaiqus/0668b656f621cd03d959343d2b70bf70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Can validate field by comparing value with other field. | |
*/ | |
class WPDesk_FCF_Validation_Confirm_Field { | |
/** | |
* Field to compare with validated field. | |
* | |
* @var string | |
*/ | |
private $field_to_compare; | |
/** | |
* WPDesk_FCF_Validation_Confirm_Field constructor. | |
* | |
* @param string $field_to_compare . | |
*/ | |
public function __construct( $field_to_compare ) { | |
$this->field_to_compare = $field_to_compare; | |
} | |
public function hooks() { | |
add_filter( 'flexible_checkout_fields_custom_validation', array( $this, 'register_custom_validation' ) ); | |
} | |
/** | |
* Register custom validation. | |
* | |
* @param array $custom_validation . | |
* | |
* @return array | |
*/ | |
public function register_custom_validation( $custom_validation ) { | |
$custom_validation[ 'field_confirmation_' . $this->field_to_compare ] = array( | |
'label' => sprintf( __( 'Compare with %1$s', 'wpdesk' ), $this->field_to_compare ), | |
'callback' => array( $this, 'validate' ) | |
); | |
return $custom_validation; | |
} | |
/** | |
* Validate. | |
* | |
* @param string $field_label Field label. | |
* @param string $value Field value. | |
*/ | |
public function validate( $field_label, $value ) { | |
$field_to_compare_value = sanitize_text_field( $_POST[ $this->field_to_compare ] ); | |
$valid = $field_to_compare_value === $value; | |
if ( ! $valid ) { | |
wc_add_notice( sprintf( __( 'Invalid %1$s value.', 'wpdesk' ), '<strong>' . $field_label . '</strong>' ), 'error' ); | |
} | |
} | |
} | |
$fcf_validation_confirm_field = new WPDesk_FCF_Validation_Confirm_Field( 'billing_email' ); | |
$fcf_validation_confirm_field->hooks(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment