Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update phone validation regex to accept only numbers and correct amount - Fix "must consist of only numbers" validation error message to be specific #112

Open
wants to merge 40 commits into
base: develop
Choose a base branch
from
Open
Changes from 6 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
065a027
Update phone validation regex to accept only numbers and correct amount
MaxwellGarceau Jan 3, 2025
c4a7d56
Strip whitespace from beginning, middle, and end of phone number
MaxwellGarceau Jan 3, 2025
37b58b1
Add custom message for not enough digits
MaxwellGarceau Jan 4, 2025
ff888a8
Refactor if else ladder to switch statement for readability
MaxwellGarceau Jan 4, 2025
232852f
Add back translators. Fix linting.
MaxwellGarceau Jan 4, 2025
891f9e9
Add comment
MaxwellGarceau Jan 4, 2025
f203326
Replace arrow function with anonymous function for php compatibility
MaxwellGarceau Jan 7, 2025
23a2876
Add phpunit, wp_mock, and scaffold
MaxwellGarceau Jan 7, 2025
b5ef904
Modularize validate phone number
MaxwellGarceau Jan 7, 2025
9613ca8
Rename function to follow 10up namespace standards
MaxwellGarceau Jan 7, 2025
363cd6a
Move address validation to validation module
MaxwellGarceau Jan 7, 2025
6184f01
Rename mailchimp_sf_merge_validate_address to merge_validate_address
MaxwellGarceau Jan 7, 2025
4caf3cb
Debug phpunit tests
MaxwellGarceau Jan 7, 2025
d77ec3e
Rename test
MaxwellGarceau Jan 7, 2025
9a68d59
Modularize location detection class
MaxwellGarceau Jan 7, 2025
546d766
Add commands to lint for compatibility and autofix lint errors
MaxwellGarceau Jan 7, 2025
d81e0c3
Add helper function to create WP_Error
MaxwellGarceau Jan 8, 2025
6d436bf
Define MCSF_DIR and MCSF_URL constants for testing
MaxwellGarceau Jan 8, 2025
3ab8df7
First pass of us phone number validation tests
MaxwellGarceau Jan 8, 2025
9d265e4
Refactor merge validation into a class for testing
MaxwellGarceau Jan 8, 2025
ae3cf49
Add WP_Error factory for ease of unit testing
MaxwellGarceau Jan 8, 2025
1a3f9a2
Update testInvalidPhoneNumbers with WP_Error factory
MaxwellGarceau Jan 8, 2025
86ad9ac
Refactor validation to use class constants for error codes
MaxwellGarceau Jan 8, 2025
0f4dfa3
Update tests to use WP_Error factory logic
MaxwellGarceau Jan 8, 2025
c0e0e74
Remove test for long phone numbers
MaxwellGarceau Jan 8, 2025
37cdcae
Adding more testing cases for invalid phone numbers and too short num…
MaxwellGarceau Jan 8, 2025
8e87e49
Fix invalid number test case
MaxwellGarceau Jan 8, 2025
018228b
Declare strict types in validate
MaxwellGarceau Jan 8, 2025
c7dc9e8
Fix lint error
MaxwellGarceau Jan 8, 2025
7643c4e
Formatting adjustments
MaxwellGarceau Jan 8, 2025
0348809
Add phpcompatibility most recent sniffs
MaxwellGarceau Jan 8, 2025
37ffabf
Fix bug causing form to not submit on empty US phone numbers
MaxwellGarceau Jan 8, 2025
1b71c59
Phone number should be exactly 12 digits
MaxwellGarceau Jan 8, 2025
bc69c44
Update tests to ensure empty and falsy inputs return null
MaxwellGarceau Jan 8, 2025
cfabc94
Add back original validation functions as deprecated
MaxwellGarceau Jan 9, 2025
f020f91
Add docblock comment to deprecated-functions.php
MaxwellGarceau Jan 9, 2025
ecf6a54
Fix deprecation warnings on test
MaxwellGarceau Jan 9, 2025
2fcb0da
Add _deprecate_function to mailchimp_sf_merge_validate_phone and mail…
MaxwellGarceau Jan 15, 2025
e01d999
Add mailchimp_sf_where_am_i back as a deprecated function
MaxwellGarceau Jan 15, 2025
d479813
Fix lint
MaxwellGarceau Jan 15, 2025
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
53 changes: 41 additions & 12 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -1046,26 +1046,55 @@ function mailchimp_sf_merge_submit( $mv ) {
*
* @param array $opt_val Option value
* @param array $data Data
* @return void
* @return array|WP_Error
*/
function mailchimp_sf_merge_validate_phone( $opt_val, $data ) {
dkotter marked this conversation as resolved.
Show resolved Hide resolved
// This filters out all 'falsey' elements
// Filter out falsy values
$opt_val = array_filter( $opt_val );
// If they weren't all empty

// If they were all empty
if ( ! $opt_val ) {
return;
return $opt_val;
}

// Trim whitespace
$opt_val = array_map( 'trim', $opt_val ); // Beginning and end
$opt_val = array_map( fn( $s ) => preg_replace( '/\s/', '', $s ), $opt_val ); // Middle
MaxwellGarceau marked this conversation as resolved.
Show resolved Hide resolved

// Format number for validation
$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;
switch ( true ) {
/**
* Phone number must be 12 characters long
* 10 digits [0-9] and 2 dashes "-"
*/
case strlen( $opt_val ) < 12:
$message = sprintf(
/* translators: %s: field name */
esc_html__( '%s must contain the correct amount of digits', 'mailchimp' ),
esc_html( $data['name'] )
);
$opt_val = new WP_Error( 'mc_phone_validation', $message );
break;

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

/**
* No issues, pass validation
*/
default:
break;
}

return $opt_val;
Expand Down
Loading