Created
June 8, 2011 20:21
-
-
Save gfmurphy/1015298 to your computer and use it in GitHub Desktop.
Nostalgia
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
function validate_forms() { | |
global $wpdb, $current_user, $user_ID; | |
$any_bad_inputs = false; | |
$bad_input_message = ''; | |
if(is_numeric($_POST['card_number']) === false) { | |
$_SESSION['wpsc_gateway_error_messages']['card_number'] = __('Please enter a valid card number.', 'wpsc'); | |
} | |
if ( isset( $_POST['card_number1'] ) && isset( $_POST['card_number2'] ) && isset( $_POST['card_number3'] ) && isset( $_POST['card_number4'] ) ) { | |
if ( $_POST['card_number1'] != '' && $_POST['card_number2'] != '' && $_POST['card_number3'] != '' && $_POST['card_number4'] != '' && is_numeric( $_POST['card_number1'] ) && is_numeric( $_POST['card_number2'] ) && is_numeric( $_POST['card_number3'] ) && is_numeric( $_POST['card_number4'] ) ) { | |
$_SESSION['wpsc_gateway_error_messages']['card_number'] = ''; | |
} else { | |
$any_bad_inputs = true; | |
$bad_input = true; | |
$_SESSION['wpsc_gateway_error_messages']['card_number'] = __( 'Please enter a valid card number.', 'wpsc' ); | |
$_SESSION['wpsc_checkout_saved_values']['card_number'] = ''; | |
} | |
} | |
if ( isset( $_POST['expiry'] ) ) { | |
if ( !empty($_POST['expiry']['month']) && !empty($_POST['expiry']['month']) && is_numeric( $_POST['expiry']['month'] ) && is_numeric( $_POST['expiry']['year'] ) ) { | |
$_SESSION['wpsc_gateway_error_messages']['expdate'] = ''; | |
} else { | |
$any_bad_inputs = true; | |
$bad_input = true; | |
$_SESSION['wpsc_gateway_error_messages']['expdate'] = __( 'Please enter a valid expiry date.', 'wpsc' ); | |
$_SESSION['wpsc_checkout_saved_values']['expdate'] = ''; | |
} | |
} | |
if ( isset( $_POST['card_code'] ) ) { | |
if ( empty($_POST['card_code']) || (!is_numeric( $_POST['card_code'] )) ) { | |
$any_bad_inputs = true; | |
$bad_input = true; | |
$_SESSION['wpsc_gateway_error_messages']['card_code'] = __( 'Please enter a valid CVV.', 'wpsc' ); | |
$_SESSION['wpsc_checkout_saved_values']['card_code'] = ''; | |
} else { | |
$_SESSION['wpsc_gateway_error_messages']['card_code'] = ''; | |
} | |
} | |
if ( isset( $_POST['cctype'] ) ) { | |
if ( $_POST['cctype'] == '' ) { | |
$any_bad_inputs = true; | |
$bad_input = true; | |
$_SESSION['wpsc_gateway_error_messages']['cctype'] = __( 'Please enter a valid CVV.', 'wpsc' ); | |
$_SESSION['wpsc_checkout_saved_values']['cctype'] = ''; | |
} else { | |
$_SESSION['wpsc_gateway_error_messages']['cctype'] = ''; | |
} | |
} | |
if ( isset( $_POST['log'] ) || isset( $_POST['pwd'] ) || isset( $_POST['user_email'] ) ) { | |
$results = wpsc_add_new_user( $_POST['log'], $_POST['pwd'], $_POST['user_email'] ); | |
$_SESSION['wpsc_checkout_user_error_messages'] = array( ); | |
if ( is_callable( array( $results, "get_error_code" ) ) && $results->get_error_code() ) { | |
foreach ( $results->get_error_codes() as $code ) { | |
foreach ( $results->get_error_messages( $code ) as $error ) { | |
$_SESSION['wpsc_checkout_user_error_messages'][] = $error; | |
} | |
$any_bad_inputs = true; | |
} | |
} | |
if ( $results->ID > 0 ) { | |
$our_user_id = $results->ID; | |
} else { | |
$any_bad_inputs = true; | |
$our_user_id = ''; | |
} | |
} | |
if ( isset( $our_user_id ) && $our_user_id < 1 ) { | |
$our_user_id = $user_ID; | |
} | |
// check we have a user id | |
if ( isset( $our_user_id ) && $our_user_id > 0 ) { | |
$user_ID = $our_user_id; | |
} | |
//Basic Form field validation for billing and shipping details | |
foreach ( $this->checkout_items as $form_data ) { | |
$value = ''; | |
if( isset( $_POST['collected_data'][$form_data->id] ) ) | |
$value = $_POST['collected_data'][$form_data->id]; | |
$_SESSION['wpsc_checkout_saved_values'][$form_data->id] = $value; | |
$bad_input = false; | |
if ( ($form_data->mandatory == 1) || ($form_data->type == "coupon") ) { | |
// dirty hack | |
if ( $form_data->unique_name == 'billingstate' && empty( $value ) ) { | |
$billing_country_id = $wpdb->get_var( "SELECT `" . WPSC_TABLE_CHECKOUT_FORMS . "`.`id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `unique_name` = 'billingcountry' AND active = '1' " ); | |
$value = $_POST['collected_data'][$billing_country_id][1]; | |
} | |
switch ( $form_data->type ) { | |
case "email": | |
if ( !preg_match( "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-.]+\.[a-zA-Z]{2,5}$/", $value ) ) { | |
$any_bad_inputs = true; | |
$bad_input = true; | |
} | |
break; | |
case "delivery_country": | |
case "country": | |
case "heading": | |
break; | |
case "select": | |
if ( $value == '-1' ) { | |
$any_bad_inputs = true; | |
$bad_input = true; | |
} | |
break; | |
default: | |
if ( $value == null ) { | |
$any_bad_inputs = true; | |
$bad_input = true; | |
} | |
break; | |
} | |
if ( $bad_input === true ) { | |
$_SESSION['wpsc_checkout_error_messages'][$form_data->id] = sprintf(__( 'Please enter a valid <span class="wpsc_error_msg_field_name">%s</span>.', 'wpsc' ), esc_attr($form_data->name) ); | |
$_SESSION['wpsc_checkout_saved_values'][$form_data->id] = ''; | |
} | |
} | |
} | |
if ( ($any_bad_inputs == false) && ($user_ID > 0) ) | |
update_user_meta($user_ID, 'wpshpcrt_usr_profile', $_POST['collected_data']); | |
$states = array( 'is_valid' => !$any_bad_inputs, 'error_messages' => $bad_input_message ); | |
$states = apply_filters('wpsc_checkout_form_validation', $states); | |
return $states; | |
} |
Author
gfmurphy
commented
Jun 8, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment