This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unset default customer state if it doesn't match country (#8460)
* Unset default state * add controller for customers * rename validation file * explain fix inline * address feedback * revert back state logic * Update src/StoreApi/Utilities/ValidationUtils.php Co-authored-by: Mike Jolley <mike.jolley@me.com> --------- Co-authored-by: Mike Jolley <mike.jolley@me.com>
- Loading branch information
1 parent
72cd6d2
commit d2c331d
Showing
5 changed files
with
93 additions
and
61 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
namespace Automattic\WooCommerce\StoreApi\Utilities; | ||
|
||
/** | ||
* ValidationUtils class. | ||
* Helper class which validates and update customer info. | ||
*/ | ||
class ValidationUtils { | ||
/** | ||
* Get list of states for a country. | ||
* | ||
* @param string $country Country code. | ||
* @return array Array of state names indexed by state keys. | ||
*/ | ||
public function get_states_for_country( $country ) { | ||
return $country ? array_filter( (array) \wc()->countries->get_states( $country ) ) : []; | ||
} | ||
|
||
/** | ||
* Validate provided state against a countries list of defined states. | ||
* | ||
* If there are no defined states for a country, any given state is valid. | ||
* | ||
* @param string $state State name or code (sanitized). | ||
* @param string $country Country code. | ||
* @return boolean Valid or not valid. | ||
*/ | ||
public function validate_state( $state, $country ) { | ||
$states = $this->get_states_for_country( $country ); | ||
|
||
if ( count( $states ) && ! in_array( \wc_strtoupper( $state ), array_map( '\wc_strtoupper', array_keys( $states ) ), true ) ) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
/** | ||
* Format a state based on the country. If country has defined states, will return a valid upper case state code. | ||
* | ||
* @param string $state State name or code (sanitized). | ||
* @param string $country Country code. | ||
* @return string | ||
*/ | ||
public function format_state( $state, $country ) { | ||
$states = $this->get_states_for_country( $country ); | ||
|
||
if ( count( $states ) ) { | ||
$state = \wc_strtoupper( $state ); | ||
$state_values = array_map( '\wc_strtoupper', array_flip( array_map( '\wc_strtoupper', $states ) ) ); | ||
|
||
if ( isset( $state_values[ $state ] ) ) { | ||
// Convert to state code if a state name was provided. | ||
return $state_values[ $state ]; | ||
} | ||
} | ||
|
||
return $state; | ||
} | ||
} |