Validate rules, instead of doing it by key. Closes #3792.
This commit is contained in:
parent
87d7fce7c3
commit
3ee6e2fa22
|
@ -482,53 +482,48 @@ class WC_Checkout {
|
|||
|
||||
if ( ! empty( $this->posted[ $key ] ) ) {
|
||||
|
||||
// Special handling for validation and formatting
|
||||
switch ( $key ) {
|
||||
case "billing_postcode" :
|
||||
case "shipping_postcode" :
|
||||
// Validation rules
|
||||
if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) {
|
||||
foreach ( $field['validate'] as $rule ) {
|
||||
switch ( $rule ) {
|
||||
case 'postcode' :
|
||||
$this->posted[ $key ] = strtoupper( str_replace( ' ', '', $this->posted[ $key ] ) );
|
||||
|
||||
$validate_against = $key == 'billing_postcode' ? 'billing_country' : 'shipping_country';
|
||||
$this->posted[ $key ] = strtoupper( str_replace( ' ', '', $this->posted[ $key ] ) );
|
||||
if ( ! WC_Validation::is_postcode( $this->posted[ $key ], $_POST[ $fieldset_key . '_country' ] ) ) :
|
||||
wc_add_error( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ) );
|
||||
else :
|
||||
$this->posted[ $key ] = wc_format_postcode( $this->posted[ $key ], $_POST[ $fieldset_key . '_country' ] );
|
||||
endif;
|
||||
break;
|
||||
case 'phone' :
|
||||
$this->posted[ $key ] = wc_format_phone_number( $this->posted[ $key ] );
|
||||
|
||||
if ( ! WC_Validation::is_postcode( $this->posted[ $key ], $_POST[ $validate_against ] ) )
|
||||
wc_add_error( '<strong>' . $field['label'] . '</strong> ' . sprintf( __( '(%s) is not a valid postcode/ZIP.', 'woocommerce' ), $this->posted[ $key ] ) );
|
||||
else
|
||||
$this->posted[ $key ] = wc_format_postcode( $this->posted[ $key ], $_POST[ $validate_against ] );
|
||||
if ( ! WC_Validation::is_phone( $this->posted[ $key ] ) )
|
||||
wc_add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid phone number.', 'woocommerce' ) );
|
||||
break;
|
||||
case 'email' :
|
||||
$this->posted[ $key ] = strtolower( $this->posted[ $key ] );
|
||||
|
||||
break;
|
||||
case "billing_state" :
|
||||
case "shipping_state" :
|
||||
if ( ! is_email( $this->posted[ $key ] ) )
|
||||
wc_add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid email address.', 'woocommerce' ) );
|
||||
break;
|
||||
case 'state' :
|
||||
// Get valid states
|
||||
$valid_states = $woocommerce->countries->get_states( $_POST[ $fieldset_key . '_country' ] );
|
||||
if ( $valid_states )
|
||||
$valid_state_values = array_flip( array_map( 'strtolower', $valid_states ) );
|
||||
|
||||
// Get valid states
|
||||
$validate_against = $key == 'billing_state' ? 'billing_country' : 'shipping_country';
|
||||
$valid_states = $woocommerce->countries->get_states( $_POST[ $validate_against ] );
|
||||
if ( $valid_states )
|
||||
$valid_state_values = array_flip( array_map( 'strtolower', $valid_states ) );
|
||||
// Convert value to key if set
|
||||
if ( isset( $valid_state_values[ strtolower( $this->posted[ $key ] ) ] ) )
|
||||
$this->posted[ $key ] = $valid_state_values[ strtolower( $this->posted[ $key ] ) ];
|
||||
|
||||
// Convert value to key if set
|
||||
if ( isset( $valid_state_values[ strtolower( $this->posted[ $key ] ) ] ) )
|
||||
$this->posted[ $key ] = $valid_state_values[ strtolower( $this->posted[ $key ] ) ];
|
||||
|
||||
// Only validate if the country has specific state options
|
||||
if ( $valid_states && sizeof( $valid_states ) > 0 )
|
||||
if ( ! in_array( $this->posted[ $key ], array_keys( $valid_states ) ) )
|
||||
wc_add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not valid. Please enter one of the following:', 'woocommerce' ) . ' ' . implode( ', ', $valid_states ) );
|
||||
|
||||
break;
|
||||
case "billing_phone" :
|
||||
|
||||
$this->posted[ $key ] = wc_format_phone_number( $this->posted[ $key ] );
|
||||
|
||||
if ( ! WC_Validation::is_phone( $this->posted[ $key ] ) )
|
||||
wc_add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid number.', 'woocommerce' ) );
|
||||
break;
|
||||
case "billing_email" :
|
||||
|
||||
$this->posted[ $key ] = strtolower( $this->posted[ $key ] );
|
||||
|
||||
if ( ! is_email( $this->posted[ $key ] ) )
|
||||
wc_add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid email address.', 'woocommerce' ) );
|
||||
break;
|
||||
// Only validate if the country has specific state options
|
||||
if ( $valid_states && sizeof( $valid_states ) > 0 )
|
||||
if ( ! in_array( $this->posted[ $key ], array_keys( $valid_states ) ) )
|
||||
wc_add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not valid. Please enter one of the following:', 'woocommerce' ) . ' ' . implode( ', ', $valid_states ) );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -696,56 +696,58 @@ class WC_Countries {
|
|||
public function get_default_address_fields() {
|
||||
$fields = array(
|
||||
'country' => array(
|
||||
'type' => 'country',
|
||||
'label' => __( 'Country', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
|
||||
'type' => 'country',
|
||||
'label' => __( 'Country', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
|
||||
),
|
||||
'first_name' => array(
|
||||
'label' => __( 'First Name', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-first' ),
|
||||
'label' => __( 'First Name', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-first' ),
|
||||
),
|
||||
'last_name' => array(
|
||||
'label' => __( 'Last Name', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-last' ),
|
||||
'clear' => true
|
||||
'label' => __( 'Last Name', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-last' ),
|
||||
'clear' => true
|
||||
),
|
||||
'company' => array(
|
||||
'label' => __( 'Company Name', 'woocommerce' ),
|
||||
'class' => array( 'form-row-wide' ),
|
||||
'label' => __( 'Company Name', 'woocommerce' ),
|
||||
'class' => array( 'form-row-wide' ),
|
||||
),
|
||||
'address_1' => array(
|
||||
'label' => __( 'Address', 'woocommerce' ),
|
||||
'placeholder' => _x( 'Street address', 'placeholder', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-wide', 'address-field' )
|
||||
'label' => __( 'Address', 'woocommerce' ),
|
||||
'placeholder' => _x( 'Street address', 'placeholder', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-wide', 'address-field' )
|
||||
),
|
||||
'address_2' => array(
|
||||
'placeholder' => _x( 'Apartment, suite, unit etc. (optional)', 'placeholder', 'woocommerce' ),
|
||||
'class' => array( 'form-row-wide', 'address-field' ),
|
||||
'required' => false
|
||||
'placeholder' => _x( 'Apartment, suite, unit etc. (optional)', 'placeholder', 'woocommerce' ),
|
||||
'class' => array( 'form-row-wide', 'address-field' ),
|
||||
'required' => false
|
||||
),
|
||||
'city' => array(
|
||||
'label' => __( 'Town / City', 'woocommerce' ),
|
||||
'placeholder' => __( 'Town / City', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-wide', 'address-field' )
|
||||
'label' => __( 'Town / City', 'woocommerce' ),
|
||||
'placeholder' => __( 'Town / City', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-wide', 'address-field' )
|
||||
),
|
||||
'state' => array(
|
||||
'type' => 'state',
|
||||
'label' => __( 'State / County', 'woocommerce' ),
|
||||
'placeholder' => __( 'State / County', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-first', 'address-field' )
|
||||
'type' => 'state',
|
||||
'label' => __( 'State / County', 'woocommerce' ),
|
||||
'placeholder' => __( 'State / County', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-first', 'address-field' ),
|
||||
'validate' => array( 'state' )
|
||||
),
|
||||
'postcode' => array(
|
||||
'label' => __( 'Postcode / Zip', 'woocommerce' ),
|
||||
'placeholder' => __( 'Postcode / Zip', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-last', 'address-field' ),
|
||||
'clear' => true
|
||||
'label' => __( 'Postcode / Zip', 'woocommerce' ),
|
||||
'placeholder' => __( 'Postcode / Zip', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-last', 'address-field' ),
|
||||
'clear' => true,
|
||||
'validate' => array( 'postcode' )
|
||||
),
|
||||
);
|
||||
|
||||
|
@ -1120,7 +1122,8 @@ class WC_Countries {
|
|||
'label' => __( 'Phone', 'woocommerce' ),
|
||||
'required' => true,
|
||||
'class' => array( 'form-row-last' ),
|
||||
'clear' => true
|
||||
'clear' => true,
|
||||
'validate' => array( 'phone' ),
|
||||
);
|
||||
|
||||
}
|
||||
|
|
|
@ -51,48 +51,69 @@ class WC_Form_Handler {
|
|||
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
if ( $user_id <= 0 ) return;
|
||||
if ( $user_id <= 0 )
|
||||
return;
|
||||
|
||||
$load_address = isset( $wp->query_vars['edit-address'] ) ? sanitize_key( $wp->query_vars['edit-address'] ) : 'billing';
|
||||
|
||||
$address = $woocommerce->countries->get_address_fields( esc_attr( $_POST[ $load_address . '_country' ] ), $load_address . '_' );
|
||||
|
||||
foreach ($address as $key => $field) :
|
||||
foreach ( $address as $key => $field ) {
|
||||
|
||||
if (!isset($field['type'])) $field['type'] = 'text';
|
||||
if ( ! isset( $field['type'] ) )
|
||||
$field['type'] = 'text';
|
||||
|
||||
// Get Value
|
||||
switch ($field['type']) :
|
||||
switch ( $field['type'] ) {
|
||||
case "checkbox" :
|
||||
$_POST[$key] = isset($_POST[$key]) ? 1 : 0;
|
||||
$_POST[ $key ] = isset( $_POST[ $key ] ) ? 1 : 0;
|
||||
break;
|
||||
default :
|
||||
$_POST[$key] = isset($_POST[$key]) ? woocommerce_clean($_POST[$key]) : '';
|
||||
$_POST[ $key ] = isset( $_POST[ $key ] ) ? woocommerce_clean( $_POST[ $key ] ) : '';
|
||||
break;
|
||||
endswitch;
|
||||
}
|
||||
|
||||
// Hook to allow modification of value
|
||||
$_POST[$key] = apply_filters('woocommerce_process_myaccount_field_' . $key, $_POST[$key]);
|
||||
$_POST[ $key ] = apply_filters( 'woocommerce_process_myaccount_field_' . $key, $_POST[ $key ] );
|
||||
|
||||
// Validation: Required fields
|
||||
if ( isset($field['required']) && $field['required'] && empty($_POST[$key]) ) wc_add_error( $field['label'] . ' ' . __( 'is a required field.', 'woocommerce' ) );
|
||||
if ( ! empty( $field['required'] ) && empty( $_POST[ $key ] ) )
|
||||
wc_add_error( $field['label'] . ' ' . __( 'is a required field.', 'woocommerce' ) );
|
||||
|
||||
// Postcode
|
||||
if ($key=='billing_postcode' || $key=='shipping_postcode') :
|
||||
if ( ! WC_Validation::is_postcode( $_POST[$key], $_POST[ $load_address . '_country' ] ) ) :
|
||||
wc_add_error( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ) );
|
||||
else :
|
||||
$_POST[$key] = wc_format_postcode( $_POST[$key], $_POST[ $load_address . '_country' ] );
|
||||
endif;
|
||||
endif;
|
||||
// Validation rules
|
||||
if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) {
|
||||
foreach ( $field['validate'] as $rule ) {
|
||||
switch ( $rule ) {
|
||||
case 'postcode' :
|
||||
$_POST[ $key ] = strtoupper( str_replace( ' ', '', $_POST[ $key ] ) );
|
||||
|
||||
endforeach;
|
||||
if ( ! WC_Validation::is_postcode( $_POST[ $key ], $_POST[ $load_address . '_country' ] ) ) :
|
||||
wc_add_error( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ) );
|
||||
else :
|
||||
$_POST[ $key ] = wc_format_postcode( $_POST[ $key ], $_POST[ $load_address . '_country' ] );
|
||||
endif;
|
||||
break;
|
||||
case 'phone' :
|
||||
$_POST[ $key ] = wc_format_phone_number( $_POST[ $key ] );
|
||||
|
||||
if ( ! WC_Validation::is_phone( $_POST[ $key ] ) )
|
||||
wc_add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid phone number.', 'woocommerce' ) );
|
||||
break;
|
||||
case 'email' :
|
||||
$_POST[ $key ] = strtolower( $_POST[ $key ] );
|
||||
|
||||
if ( ! is_email( $_POST[ $key ] ) )
|
||||
wc_add_error( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid email address.', 'woocommerce' ) );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( wc_error_count() == 0 ) {
|
||||
|
||||
foreach ($address as $key => $field) :
|
||||
update_user_meta( $user_id, $key, $_POST[$key] );
|
||||
endforeach;
|
||||
foreach ( $address as $key => $field )
|
||||
update_user_meta( $user_id, $key, $_POST[ $key ] );
|
||||
|
||||
wc_add_message( __( 'Address changed successfully.', 'woocommerce' ) );
|
||||
|
||||
|
|
|
@ -155,6 +155,31 @@ class WC_Shortcode_My_Account {
|
|||
|
||||
$address = $woocommerce->countries->get_address_fields( get_user_meta( get_current_user_id(), $load_address . '_country', true ), $load_address . '_' );
|
||||
|
||||
// Prepare values
|
||||
foreach ( $address as $key => $field ) {
|
||||
|
||||
$value = get_user_meta( get_current_user_id(), $key, true );
|
||||
|
||||
if ( ! $value ) {
|
||||
switch( $key ) {
|
||||
case 'billing_email' :
|
||||
case 'shipping_email' :
|
||||
$value = $current_user->user_email;
|
||||
break;
|
||||
case 'billing_country' :
|
||||
case 'shipping_country' :
|
||||
$value = $woocommerce->countries->get_base_country();
|
||||
break;
|
||||
case 'billing_state' :
|
||||
case 'shipping_state' :
|
||||
$value = $woocommerce->countries->get_base_state();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$address[ $key ]['value'] = apply_filters( 'woocommerce_my_account_edit_address_field_value', $value, $key, $load_address );
|
||||
}
|
||||
|
||||
woocommerce_get_template( 'myaccount/form-edit-address.php', array(
|
||||
'load_address' => $load_address,
|
||||
'address' => apply_filters( 'woocommerce_address_to_edit', $address )
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
*
|
||||
* @author WooThemes
|
||||
* @package WooCommerce/Templates
|
||||
* @version 1.6.4
|
||||
* @version 2.1.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
|
@ -18,9 +18,9 @@ get_currentuserinfo();
|
|||
|
||||
<?php wc_print_messages(); ?>
|
||||
|
||||
<?php if (!$load_address) : ?>
|
||||
<?php if ( ! $load_address ) : ?>
|
||||
|
||||
<?php woocommerce_get_template('myaccount/my-address.php'); ?>
|
||||
<?php woocommerce_get_template( 'myaccount/my-address.php' ); ?>
|
||||
|
||||
<?php else : ?>
|
||||
|
||||
|
@ -28,18 +28,11 @@ get_currentuserinfo();
|
|||
|
||||
<h3><?php echo apply_filters( 'woocommerce_my_account_edit_address_title', $page_title ); ?></h3>
|
||||
|
||||
<?php
|
||||
foreach ($address as $key => $field) :
|
||||
$value = (isset($_POST[$key])) ? $_POST[$key] : get_user_meta( get_current_user_id(), $key, true );
|
||||
<?php foreach ( $address as $key => $field ) : ?>
|
||||
|
||||
// Default values
|
||||
if (!$value && ($key=='billing_email' || $key=='shipping_email')) $value = $current_user->user_email;
|
||||
if (!$value && ($key=='billing_country' || $key=='shipping_country')) $value = $woocommerce->countries->get_base_country();
|
||||
if (!$value && ($key=='billing_state' || $key=='shipping_state')) $value = $woocommerce->countries->get_base_state();
|
||||
<?php woocommerce_form_field( $key, $field, ! empty( $_POST[ $key ] ) ? woocommerce_clean( $_POST[ $key ] ) : $field['value'] ); ?>
|
||||
|
||||
woocommerce_form_field( $key, $field, $value );
|
||||
endforeach;
|
||||
?>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<p>
|
||||
<input type="submit" class="button" name="save_address" value="<?php _e( 'Save Address', 'woocommerce' ); ?>" />
|
||||
|
|
Loading…
Reference in New Issue