Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 85 additions & 5 deletions includes/class-mailchimp-form-submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,76 @@ public function handle_form_submission() {
return $message;
}

/**
* Validate phone
*
* @param array $opt_val Option value.
* @param array $data Data.
* @return string|WP_Error Option value or error.
*/
public function validate_phone( $opt_val, $data ) {
// This filters out all 'falsy' elements
$opt_val = array_filter( $opt_val );
// If they weren't all empty
if ( empty( $opt_val ) ) {
return '';
}

// Trim the phone number
$opt_val = array_map(
function ( $ele ) {
return preg_replace( '/\s+/', '', trim( $ele ) );
},
$opt_val
);

$opt_val = implode( '-', $opt_val );
if ( strlen( $opt_val ) !== 12 ) {
// translators: %s: field name
$message = sprintf( esc_html__( '%s should be 10 digits long.', 'mailchimp' ), esc_html( $data['name'] ) );
$error = new WP_Error( 'mc_phone_validation', $message );
return $error;
}

if ( ! preg_match( '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $opt_val ) ) {
/* translators: %s: field name */
$message = sprintf( esc_html__( '%s must consist of only numbers', 'mailchimp' ), esc_html( $data['name'] ) );
$error = new WP_Error( 'mc_phone_validation', $message );
return $error;
}

return $opt_val;
}

/**
* Validate address
*
* @param array $opt_val Option value
* @param array $data Data
* @return mixed
*/
public function validate_address( $opt_val, $data ) {
if ( true === (bool) $data['required'] ) {
if ( empty( $opt_val['addr1'] ) || empty( $opt_val['city'] ) ) {
/* translators: %s: field name */
$message = sprintf( esc_html__( '%s: Please enter a complete address.', 'mailchimp' ), esc_html( $data['name'] ) );
$error = new WP_Error( 'invalid_address_merge', $message );
return $error;
}
} elseif ( empty( $opt_val['addr1'] ) || empty( $opt_val['city'] ) ) {
return false;
}

$merge = new stdClass();
$merge->addr1 = $opt_val['addr1'];
$merge->addr2 = $opt_val['addr2'];
$merge->city = $opt_val['city'];
$merge->state = $opt_val['state'];
$merge->zip = $opt_val['zip'];
$merge->country = $opt_val['country'];
return $merge;
}

/**
* Prepare the merge fields body for the API request.
*
Expand All @@ -187,7 +257,7 @@ public function prepare_merge_fields_body( $merge_fields ) {
$opt = 'mc_mv_' . $tag;

// Skip if the field is not required and not submitted.
if ( 'Y' !== $merge_field['required'] && ! isset( $_POST[ $opt ] ) ) {
if ( true !== (bool) $merge_field['required'] && ! isset( $_POST[ $opt ] ) ) {
continue;
}

Expand All @@ -206,7 +276,7 @@ public function prepare_merge_fields_body( $merge_fields ) {
isset( $merge_field['options']['phone_format'] )
&& 'US' === $merge_field['options']['phone_format']
) {
$opt_val = mailchimp_sf_merge_validate_phone( $opt_val, $merge_field );
$opt_val = $this->validate_phone( $opt_val, $merge_field );
if ( is_wp_error( $opt_val ) ) {
return $opt_val;
}
Expand All @@ -221,7 +291,7 @@ public function prepare_merge_fields_body( $merge_fields ) {
*/
case 'address':
if ( is_array( $opt_val ) ) {
$validate = mailchimp_sf_merge_validate_address( $opt_val, $merge_field );
$validate = $this->validate_address( $opt_val, $merge_field );
if ( is_wp_error( $validate ) ) {
return $validate;
}
Expand Down Expand Up @@ -254,9 +324,9 @@ public function prepare_merge_fields_body( $merge_fields ) {
/**
* Required fields
*
* If the field is required and empty, return an error
* If the field is required and empty, +return an error
*/
if ( 'Y' === $merge_field['required'] && trim( $opt_val ) === '' ) {
if ( true === (bool) $merge_field['required'] && empty( $opt_val ) ) {
/* translators: %s: field name */
$message = sprintf( esc_html__( 'You must fill in %s.', 'mailchimp' ), esc_html( $merge_field['name'] ) );
$error = new WP_Error( 'missing_required_field', $message );
Expand Down Expand Up @@ -501,6 +571,16 @@ protected function validate_form_submission() {
return new WP_Error( 'spam', $spam_message );
}

// Early return if the email is not set
if ( empty( $_POST['mc_mv_EMAIL'] ) ) {
return new WP_Error( 'email_required', esc_html__( 'Please enter your email address.', 'mailchimp' ) );
}

// Check if the email is valid
if ( ! is_email( sanitize_email( wp_unslash( $_POST['mc_mv_EMAIL'] ) ) ) ) {
return new WP_Error( 'invalid_email', esc_html__( 'Please enter a valid email address.', 'mailchimp' ) );
}

/**
* Filter to allow for custom validation of the form submission.
*
Expand Down
33 changes: 33 additions & 0 deletions includes/mailchimp-deprecated-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,36 @@ function mailchimp_sf_merge_remove_empty( $merge ) {
$form_submission = new Mailchimp_Form_Submission();
return $form_submission->remove_empty_merge_fields( $merge );
}


/**
* Validate phone
*
* @deprecated x.x.x
*
* @param array $opt_val Option value.
* @param array $data Data.
* @return string|WP_Error Option value or error.
*/
function mailchimp_sf_merge_validate_phone( $opt_val, $data ): string|WP_Error {
_deprecated_function( __FUNCTION__, 'x.x.x', 'Mailchimp_Form_Submission::validate_phone()' );

$form_submission = new Mailchimp_Form_Submission();
return $form_submission->validate_phone( $opt_val, $data );
}

/**
* Validate address
*
* @deprecated x.x.x
*
* @param array $opt_val Option value.
* @param array $data Data.
* @return mixed
*/
function mailchimp_sf_merge_validate_address( $opt_val, $data ) {
_deprecated_function( __FUNCTION__, 'x.x.x', 'Mailchimp_Form_Submission::validate_address()' );

$form_submission = new Mailchimp_Form_Submission();
return $form_submission->validate_address( $opt_val, $data );
}
61 changes: 1 addition & 60 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ function mailchimp_sf_save_general_form_settings() {
if ( is_array( $mv ) ) {
foreach ( $mv as $mv_var ) {
$opt = 'mc_mv_' . $mv_var['tag'];
if ( isset( $_POST[ $opt ] ) || 'Y' === $mv_var['required'] ) {
if ( isset( $_POST[ $opt ] ) || true === (bool) $mv_var['required'] ) {
update_option( $opt, 'on' );
} else {
update_option( $opt, 'off' );
Expand Down Expand Up @@ -787,65 +787,6 @@ function mailchimp_sf_check_status( $endpoint ) {
return $subscriber['status'];
}

/**
* Validate phone
*
* @param array $opt_val Option value
* @param array $data Data
* @return void
*/
function mailchimp_sf_merge_validate_phone( $opt_val, $data ) {
// This filters out all 'falsey' elements
$opt_val = array_filter( $opt_val );
// If they weren't all empty
if ( ! $opt_val ) {
return;
}

$opt_val = implode( '-', $opt_val );
if ( strlen( $opt_val ) < 12 ) {
$opt_val = '';
}

if ( ! preg_match( '/[0-9]{0,3}-[0-9]{0,3}-[0-9]{0,4}/A', $opt_val ) ) {
/* translators: %s: field name */
$message = sprintf( esc_html__( '%s must consist of only numbers', 'mailchimp' ), esc_html( $data['name'] ) );
$error = new WP_Error( 'mc_phone_validation', $message );
return $error;
}

return $opt_val;
}

/**
* Validate address
*
* @param array $opt_val Option value
* @param array $data Data
* @return mixed
*/
function mailchimp_sf_merge_validate_address( $opt_val, $data ) {
if ( 'Y' === $data['required'] ) {
if ( empty( $opt_val['addr1'] ) || empty( $opt_val['city'] ) ) {
/* translators: %s: field name */
$message = sprintf( esc_html__( 'You must fill in %s.', 'mailchimp' ), esc_html( $data['name'] ) );
$error = new WP_Error( 'invalid_address_merge', $message );
return $error;
}
} elseif ( empty( $opt_val['addr1'] ) || empty( $opt_val['city'] ) ) {
return false;
}

$merge = new stdClass();
$merge->addr1 = $opt_val['addr1'];
$merge->addr2 = $opt_val['addr2'];
$merge->city = $opt_val['city'];
$merge->state = $opt_val['state'];
$merge->zip = $opt_val['zip'];
$merge->country = $opt_val['country'];
return $merge;
}

/**
* Verify key
*
Expand Down
Loading