is_phone() 공개 정적인 메소드

Validates a phone number using a regular expression.
public static is_phone ( $phone ) : boolean
리턴 boolean
예제 #1
0
 /**
  * Data provider for test_is_phone.
  *
  * @since 2.4
  */
 public function data_provider_test_is_phone()
 {
     return array(array(true, WC_Validation::is_phone('+00 000 00 00 000')), array(true, WC_Validation::is_phone('+00-000-00-00-000')), array(true, WC_Validation::is_phone('(000) 00 00 000')), array(false, WC_Validation::is_phone('+00.000.00.00.000')), array(false, WC_Validation::is_phone('+00 aaa dd ee fff')));
 }
 /**
  * Save and and update a billing or shipping address if the
  * form was submitted through the user account page.
  */
 public static function save_address()
 {
     global $wp;
     if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
         return;
     }
     if (empty($_POST['action']) || 'edit_address' !== $_POST['action'] || empty($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-edit_address')) {
         return;
     }
     $user_id = get_current_user_id();
     if ($user_id <= 0) {
         return;
     }
     $load_address = isset($wp->query_vars['edit-address']) ? wc_edit_address_i18n(sanitize_title($wp->query_vars['edit-address']), true) : 'billing';
     $address = WC()->countries->get_address_fields(esc_attr($_POST[$load_address . '_country']), $load_address . '_');
     foreach ($address as $key => $field) {
         if (!isset($field['type'])) {
             $field['type'] = 'text';
         }
         // Get Value.
         switch ($field['type']) {
             case 'checkbox':
                 $_POST[$key] = (int) isset($_POST[$key]);
                 break;
             default:
                 $_POST[$key] = isset($_POST[$key]) ? wc_clean($_POST[$key]) : '';
                 break;
         }
         // Hook to allow modification of value.
         $_POST[$key] = apply_filters('woocommerce_process_myaccount_field_' . $key, $_POST[$key]);
         // Validation: Required fields.
         if (!empty($field['required']) && empty($_POST[$key])) {
             wc_add_notice(sprintf(__('%s is a required field.', 'woocommerce'), $field['label']), 'error');
         }
         if (!empty($_POST[$key])) {
             // 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]));
                             if (!WC_Validation::is_postcode($_POST[$key], $_POST[$load_address . '_country'])) {
                                 wc_add_notice(__('Please enter a valid postcode / ZIP.', 'woocommerce'), 'error');
                             } else {
                                 $_POST[$key] = wc_format_postcode($_POST[$key], $_POST[$load_address . '_country']);
                             }
                             break;
                         case 'phone':
                             $_POST[$key] = wc_format_phone_number($_POST[$key]);
                             if (!WC_Validation::is_phone($_POST[$key])) {
                                 wc_add_notice(sprintf(__('%s is not a valid phone number.', 'woocommerce'), '<strong>' . $field['label'] . '</strong>'), 'error');
                             }
                             break;
                         case 'email':
                             $_POST[$key] = strtolower($_POST[$key]);
                             if (!is_email($_POST[$key])) {
                                 wc_add_notice(sprintf(__('%s is not a valid email address.', 'woocommerce'), '<strong>' . $field['label'] . '</strong>'), 'error');
                             }
                             break;
                     }
                 }
             }
         }
     }
     do_action('woocommerce_after_save_address_validation', $user_id, $load_address, $address);
     if (0 === wc_notice_count('error')) {
         foreach ($address as $key => $field) {
             update_user_meta($user_id, $key, $_POST[$key]);
         }
         wc_add_notice(__('Address changed successfully.', 'woocommerce'));
         do_action('woocommerce_customer_save_address', $user_id, $load_address);
         wp_safe_redirect(wc_get_endpoint_url('edit-address', '', wc_get_page_permalink('myaccount')));
         exit;
     }
 }
 /**
  * Process the checkout after the confirm order button is pressed
  *
  * @access public
  * @return void
  */
 public function process_checkout()
 {
     global $wpdb, $current_user;
     wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-process_checkout');
     if (!defined('WOOCOMMERCE_CHECKOUT')) {
         define('WOOCOMMERCE_CHECKOUT', true);
     }
     // Prevent timeout
     @set_time_limit(0);
     do_action('woocommerce_before_checkout_process');
     if (sizeof(WC()->cart->get_cart()) == 0) {
         wc_add_notice(sprintf(__('Sorry, your session has expired. <a href="%s" class="wc-backward">Return to homepage</a>', 'woocommerce'), home_url()), 'error');
     }
     do_action('woocommerce_checkout_process');
     // Checkout fields (not defined in checkout_fields)
     $this->posted['terms'] = isset($_POST['terms']) ? 1 : 0;
     $this->posted['createaccount'] = isset($_POST['createaccount']) ? 1 : 0;
     $this->posted['payment_method'] = isset($_POST['payment_method']) ? stripslashes($_POST['payment_method']) : '';
     $this->posted['shipping_method'] = isset($_POST['shipping_method']) ? $_POST['shipping_method'] : '';
     $this->posted['ship_to_different_address'] = isset($_POST['ship_to_different_address']) ? true : false;
     if (isset($_POST['shiptobilling'])) {
         _deprecated_argument('WC_Checkout::process_checkout()', '2.1', 'The "shiptobilling" field is deprecated. THe template files are out of date');
         $this->posted['ship_to_different_address'] = $_POST['shiptobilling'] ? false : true;
     }
     // Ship to billing only option
     if (WC()->cart->ship_to_billing_address_only()) {
         $this->posted['ship_to_different_address'] = false;
     }
     // Update customer shipping and payment method to posted method
     $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
     if (isset($this->posted['shipping_method']) && is_array($this->posted['shipping_method'])) {
         foreach ($this->posted['shipping_method'] as $i => $value) {
             $chosen_shipping_methods[$i] = wc_clean($value);
         }
     }
     WC()->session->set('chosen_shipping_methods', $chosen_shipping_methods);
     WC()->session->set('chosen_payment_method', $this->posted['payment_method']);
     // Note if we skip shipping
     $skipped_shipping = false;
     // Get posted checkout_fields and do validation
     foreach ($this->checkout_fields as $fieldset_key => $fieldset) {
         // Skip shipping if not needed
         if ($fieldset_key == 'shipping' && ($this->posted['ship_to_different_address'] == false || !WC()->cart->needs_shipping())) {
             $skipped_shipping = true;
             continue;
         }
         // Ship account if not needed
         if ($fieldset_key == 'account' && (is_user_logged_in() || $this->must_create_account == false && empty($this->posted['createaccount']))) {
             continue;
         }
         foreach ($fieldset as $key => $field) {
             if (!isset($field['type'])) {
                 $field['type'] = 'text';
             }
             // Get Value
             switch ($field['type']) {
                 case "checkbox":
                     $this->posted[$key] = isset($_POST[$key]) ? 1 : 0;
                     break;
                 case "multiselect":
                     $this->posted[$key] = isset($_POST[$key]) ? implode(', ', array_map('wc_clean', $_POST[$key])) : '';
                     break;
                 case "textarea":
                     $this->posted[$key] = isset($_POST[$key]) ? wp_strip_all_tags(wp_check_invalid_utf8(stripslashes($_POST[$key]))) : '';
                     break;
                 default:
                     $this->posted[$key] = isset($_POST[$key]) ? wc_clean($_POST[$key]) : '';
                     break;
             }
             // Hooks to allow modification of value
             $this->posted[$key] = apply_filters('woocommerce_process_checkout_' . sanitize_title($field['type']) . '_field', $this->posted[$key]);
             $this->posted[$key] = apply_filters('woocommerce_process_checkout_field_' . $key, $this->posted[$key]);
             // Validation: Required fields
             if (isset($field['required']) && $field['required'] && empty($this->posted[$key])) {
                 wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is a required field.', 'woocommerce'), 'error');
             }
             if (!empty($this->posted[$key])) {
                 // 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]));
                                 if (!WC_Validation::is_postcode($this->posted[$key], $_POST[$fieldset_key . '_country'])) {
                                     wc_add_notice(__('Please enter a valid postcode/ZIP.', 'woocommerce'), 'error');
                                 } else {
                                     $this->posted[$key] = wc_format_postcode($this->posted[$key], $_POST[$fieldset_key . '_country']);
                                 }
                                 break;
                             case 'phone':
                                 $this->posted[$key] = wc_format_phone_number($this->posted[$key]);
                                 if (!WC_Validation::is_phone($this->posted[$key])) {
                                     wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'woocommerce'), 'error');
                                 }
                                 break;
                             case 'email':
                                 $this->posted[$key] = strtolower($this->posted[$key]);
                                 if (!is_email($this->posted[$key])) {
                                     wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce'), 'error');
                                 }
                                 break;
                             case 'state':
                                 // Get valid states
                                 $valid_states = WC()->countries->get_states($_POST[$fieldset_key . '_country']);
                                 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])];
                                 }
                                 // 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_notice('<strong>' . $field['label'] . '</strong> ' . __('is not valid. Please enter one of the following:', 'woocommerce') . ' ' . implode(', ', $valid_states), 'error');
                                     }
                                 }
                                 break;
                         }
                     }
                 }
             }
         }
     }
     // Update customer location to posted location so we can correctly check available shipping methods
     if (isset($this->posted['billing_country'])) {
         WC()->customer->set_country($this->posted['billing_country']);
     }
     if (isset($this->posted['billing_state'])) {
         WC()->customer->set_state($this->posted['billing_state']);
     }
     if (isset($this->posted['billing_postcode'])) {
         WC()->customer->set_postcode($this->posted['billing_postcode']);
     }
     // Shipping Information
     if (!$skipped_shipping) {
         // Update customer location to posted location so we can correctly check available shipping methods
         if (isset($this->posted['shipping_country'])) {
             WC()->customer->set_shipping_country($this->posted['shipping_country']);
         }
         if (isset($this->posted['shipping_state'])) {
             WC()->customer->set_shipping_state($this->posted['shipping_state']);
         }
         if (isset($this->posted['shipping_postcode'])) {
             WC()->customer->set_shipping_postcode($this->posted['shipping_postcode']);
         }
     } else {
         // Update customer location to posted location so we can correctly check available shipping methods
         if (isset($this->posted['billing_country'])) {
             WC()->customer->set_shipping_country($this->posted['billing_country']);
         }
         if (isset($this->posted['billing_state'])) {
             WC()->customer->set_shipping_state($this->posted['billing_state']);
         }
         if (isset($this->posted['billing_postcode'])) {
             WC()->customer->set_shipping_postcode($this->posted['billing_postcode']);
         }
     }
     // Update cart totals now we have customer address
     WC()->cart->calculate_totals();
     // Terms
     if (!isset($_POST['woocommerce_checkout_update_totals']) && empty($this->posted['terms']) && wc_get_page_id('terms') > 0) {
         wc_add_notice(__('You must accept our Terms &amp; Conditions.', 'woocommerce'), 'error');
     }
     if (WC()->cart->needs_shipping()) {
         if (!in_array(WC()->customer->get_shipping_country(), array_keys(WC()->countries->get_shipping_countries()))) {
             wc_add_notice(sprintf(__('Unfortunately <strong>we do not ship to %s</strong>. Please enter an alternative shipping address.', 'woocommerce'), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country()), 'error');
         }
         // Validate Shipping Methods
         $packages = WC()->shipping->get_packages();
         $this->shipping_methods = WC()->session->get('chosen_shipping_methods');
         foreach ($packages as $i => $package) {
             if (!isset($package['rates'][$this->shipping_methods[$i]])) {
                 wc_add_notice(__('Invalid shipping method.', 'woocommerce'), 'error');
                 $this->shipping_methods[$i] = '';
             }
         }
     }
     if (WC()->cart->needs_payment()) {
         // Payment Method
         $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
         if (!isset($available_gateways[$this->posted['payment_method']])) {
             $this->payment_method = '';
             wc_add_notice(__('Invalid payment method.', 'woocommerce'), 'error');
         } else {
             $this->payment_method = $available_gateways[$this->posted['payment_method']];
             $this->payment_method->validate_fields();
         }
     }
     // Action after validation
     do_action('woocommerce_after_checkout_validation', $this->posted);
     if (!isset($_POST['woocommerce_checkout_update_totals']) && wc_notice_count('error') == 0) {
         try {
             // Customer accounts
             $this->customer_id = apply_filters('woocommerce_checkout_customer_id', get_current_user_id());
             if (!is_user_logged_in() && ($this->must_create_account || !empty($this->posted['createaccount']))) {
                 $username = !empty($this->posted['account_username']) ? $this->posted['account_username'] : '';
                 $password = !empty($this->posted['account_password']) ? $this->posted['account_password'] : '';
                 $new_customer = wc_create_new_customer($this->posted['billing_email'], $username, $password);
                 if (is_wp_error($new_customer)) {
                     throw new Exception($new_customer->get_error_message());
                 }
                 $this->customer_id = $new_customer;
                 wc_set_customer_auth_cookie($this->customer_id);
                 // As we are now logged in, checkout will need to refresh to show logged in data
                 WC()->session->set('reload_checkout', true);
                 // Add customer info from other billing fields
                 if ($this->posted['billing_first_name'] && apply_filters('woocommerce_checkout_update_customer_data', true, $this)) {
                     $userdata = array('ID' => $this->customer_id, 'first_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '', 'last_name' => $this->posted['billing_last_name'] ? $this->posted['billing_last_name'] : '', 'display_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '');
                     wp_update_user(apply_filters('woocommerce_checkout_customer_userdata', $userdata, $this));
                 }
             }
             // Do a final stock check at this point
             $this->check_cart_items();
             // Abort if errors are present
             if (wc_notice_count('error') > 0) {
                 throw new Exception();
             }
             $order_id = $this->create_order();
             do_action('woocommerce_checkout_order_processed', $order_id, $this->posted);
             // Process payment
             if (WC()->cart->needs_payment()) {
                 // Store Order ID in session so it can be re-used after payment failure
                 WC()->session->order_awaiting_payment = $order_id;
                 // Process Payment
                 $result = $available_gateways[$this->posted['payment_method']]->process_payment($order_id);
                 // Redirect to success/confirmation/payment page
                 if ($result['result'] == 'success') {
                     $result = apply_filters('woocommerce_payment_successful_result', $result, $order_id);
                     if (is_ajax()) {
                         echo '<!--WC_START-->' . json_encode($result) . '<!--WC_END-->';
                         exit;
                     } else {
                         wp_redirect($result['redirect']);
                         exit;
                     }
                 }
             } else {
                 if (empty($order)) {
                     $order = new WC_Order($order_id);
                 }
                 // No payment was required for order
                 $order->payment_complete();
                 // Empty the Cart
                 WC()->cart->empty_cart();
                 // Get redirect
                 $return_url = $order->get_checkout_order_received_url();
                 // Redirect to success/confirmation/payment page
                 if (is_ajax()) {
                     echo '<!--WC_START-->' . json_encode(array('result' => 'success', 'redirect' => apply_filters('woocommerce_checkout_no_payment_needed_redirect', $return_url, $order))) . '<!--WC_END-->';
                     exit;
                 } else {
                     wp_safe_redirect(apply_filters('woocommerce_checkout_no_payment_needed_redirect', $return_url, $order));
                     exit;
                 }
             }
         } catch (Exception $e) {
             if (!empty($e)) {
                 wc_add_notice($e->getMessage(), 'error');
             }
         }
     }
     // endif
     // If we reached this point then there were errors
     if (is_ajax()) {
         ob_start();
         wc_print_notices();
         $messages = ob_get_clean();
         echo '<!--WC_START-->' . json_encode(array('result' => 'failure', 'messages' => $messages, 'refresh' => isset(WC()->session->refresh_totals) ? 'true' : 'false', 'reload' => isset(WC()->session->reload_checkout) ? 'true' : 'false')) . '<!--WC_END-->';
         unset(WC()->session->refresh_totals, WC()->session->reload_checkout);
         exit;
     }
 }
예제 #4
0
 /**
  * Validates the posted checkout data based on field properties.
  *
  * @since  2.7.0
  * @param  array $data An array of posted data.
  * @param  WP_Error $errors
  */
 protected function validate_posted_data(&$data, &$errors)
 {
     foreach ($this->get_checkout_fields() as $fieldset_key => $fieldset) {
         if ($this->maybe_skip_fieldset($fieldset_key, $data)) {
             continue;
         }
         foreach ($fieldset as $key => $field) {
             if (!isset($data[$key])) {
                 continue;
             }
             $required = !empty($field['required']);
             $format = array_filter(isset($field['validate']) ? (array) $field['validate'] : array());
             $field_label = isset($field['label']) ? $field['label'] : '';
             switch ($fieldset_key) {
                 case 'shipping':
                     /* translators: %s: field name */
                     $field_label = sprintf(__('Shipping %s', 'woocommerce'), strtolower($field_label));
                     break;
                 case 'billing':
                     /* translators: %s: field name */
                     $field_label = sprintf(__('Billing %s', 'woocommerce'), strtolower($field_label));
                     break;
             }
             if (in_array('postcode', $format)) {
                 $country = isset($data[$fieldset_key . '_country']) ? $data[$fieldset_key . '_country'] : WC()->customer->{"get_{$fieldset_key}_country"}();
                 $data[$key] = wc_format_postcode($data[$key], $country);
                 if ('' !== $data[$key] && !WC_Validation::is_postcode($data[$key], $country)) {
                     $errors->add('validation', __('Please enter a valid postcode / ZIP.', 'woocommerce'));
                 }
             }
             if (in_array('phone', $format)) {
                 $data[$key] = wc_format_phone_number($data[$key]);
                 if ('' !== $data[$key] && !WC_Validation::is_phone($data[$key])) {
                     /* translators: %s: phone number */
                     $errors->add('validation', sprintf(__('%s is not a valid phone number.', 'woocommerce'), '<strong>' . $field_label . '</strong>'));
                 }
             }
             if (in_array('email', $format)) {
                 $data[$key] = sanitize_email($data[$key]);
                 if ('' !== $data[$key] && !is_email($data[$key])) {
                     /* translators: %s: email address */
                     $errors->add('validation', sprintf(__('%s is not a valid email address.', 'woocommerce'), '<strong>' . $field_label . '</strong>'));
                 }
             }
             if ('' !== $data[$key] && in_array('state', $format)) {
                 $country = isset($data[$fieldset_key . '_country']) ? $data[$fieldset_key . '_country'] : WC()->customer->{"get_{$fieldset_key}_country"}();
                 $valid_states = WC()->countries->get_states($country);
                 if (!empty($valid_states) && is_array($valid_states) && sizeof($valid_states) > 0) {
                     $valid_state_values = array_flip(array_map('strtolower', $valid_states));
                     // Convert value to key if set
                     if (isset($valid_state_values[strtolower($data[$key])])) {
                         $data[$key] = $valid_state_values[strtolower($data[$key])];
                     }
                     if (!in_array($data[$key], array_keys($valid_states))) {
                         /* translators: 1: state field 2: valid states */
                         $errors->add('validation', sprintf(__('%1$s is not valid. Please enter one of the following: %2$s', 'woocommerce'), '<strong>' . $field_label . '</strong>', implode(', ', $valid_states)));
                     }
                 }
             }
             if ($required && '' === $data[$key]) {
                 /* translators: %s: field name */
                 $errors->add('required-field', apply_filters('woocommerce_checkout_required_field_notice', sprintf(__('%s is a required field.', 'woocommerce'), '<strong>' . $field_label . '</strong>'), $field_label));
             }
         }
     }
 }
예제 #5
0
 public static function require_billing_information($post_id, $post)
 {
     // must be shop order
     if ($post->post_type != 'shop_order') {
         return;
     }
     // only perform this check if the associated option is on
     if (self::$options->{'qsot-require-billing-information'} != 'yes') {
         return;
     }
     // only when the past is being saved in the admin
     if (!isset($_POST['action']) || $_POST['action'] != 'editpost') {
         return;
     }
     // load the order
     $order = wc_get_order($post_id);
     if (!is_object($order) || !isset($order->id)) {
         return;
     }
     // do not perform this check on cancelled orders, because they are irrelevant checks at that point
     if ('cancelled' == $order->get_status()) {
         return;
     }
     // ****** most of this is adapted from the checkout logic from WC2.3.x
     // get all the fields that we should be validating. derived from checkout process
     $fields = WC()->countries->get_address_fields(self::_get_value('_billing_country', $order), '_billing_');
     $errors = array();
     // cycle through each field, and validate the input
     foreach ($fields as $key => $field) {
         // make sure we have a field type
         if (!isset($field['type'])) {
             $field['type'] = 'text';
         }
         // find the submitted value of the field
         switch ($field['type']) {
             // checkboxes are on or off
             case 'checkbox':
                 $value = isset($_POST[$key]) ? 1 : 0;
                 break;
                 // multiselect boxes have multiple values that need cleaning
             // multiselect boxes have multiple values that need cleaning
             case 'multiselect':
                 $value = isset($_POST[$key]) ? implode(', ', array_map('wc_clean', $_POST[$key])) : '';
                 break;
                 // textareas allow for lots of text, so clean that up
             // textareas allow for lots of text, so clean that up
             case 'textarea':
                 $value = isset($_POST[$key]) ? wp_strip_all_tags(wp_check_invalid_utf8(stripslashes($_POST[$key]))) : '';
                 break;
                 // all other fields should be cleaned as well
             // all other fields should be cleaned as well
             default:
                 $value = isset($_POST[$key]) ? is_array($_POST[$key]) ? array_map('wc_clean', $_POST[$key]) : wc_clean($_POST[$key]) : '';
                 break;
         }
         // allow modification of resulting value
         $value = apply_filters('woocommerce_process_checkout_' . sanitize_title($field['type']) . '_field', $value);
         $value = apply_filters('woocommerce_process_checkout_field_' . $key, $value);
         // check required fields
         if (isset($field['required']) && $field['required'] && empty($value)) {
             $error[] = '<strong>' . $field['label'] . '</strong> ' . __('is a required field.', 'woocommerce');
         }
         // some non-empty fields need addtiional validation. handle that here
         if (!empty($value)) {
             // cycle through the rules
             if (isset($field['validate'])) {
                 foreach ($field['validate'] as $rule) {
                     // process each rule if it is in the list
                     switch ($rule) {
                         // postcodes vary from country to country
                         case 'postcode':
                             $value = strtoupper(str_replace(' ', '', $value));
                             if (!WC_Validation::is_postcode($value, $_POST[$key])) {
                                 $errors[] = __('Please enter a valid postcode/ZIP.', 'woocommerce');
                             }
                             break;
                             // phone digit count and format varies from country to country
                         // phone digit count and format varies from country to country
                         case 'phone':
                             $value = wc_format_phone_number($value);
                             if (!WC_Validation::is_phone($value)) {
                                 $errors[] = '<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'woocommerce');
                             }
                             break;
                             // validate email addresses
                         // validate email addresses
                         case 'email':
                             $value = strtolower($value);
                             if (!is_email($value)) {
                                 $errors[] = '<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce');
                             }
                             break;
                             // states cound be in different formats or have different values based on the country
                         // states cound be in different formats or have different values based on the country
                         case 'state':
                             $states = WC()->countries->get_states(self::_get_value('_billing_country', $order));
                             if (!empty($states) && is_array($states)) {
                                 $states = array_flip(array_map('strtolower', $states));
                                 // look up correct value if key exists
                                 if (isset($states[strtolower($value)])) {
                                     $value = $states[strtolower($value)];
                                 }
                             }
                             if (!empty($states) && is_array($states) && count($states) > 0) {
                                 if (!in_array($value, $states)) {
                                     $errors[] = '<strong>' . $field['label'] . '</strong> ' . strtolower($value) . ' ' . __('is not valid. Please enter one of the following:', 'woocommerce') . ' ' . implode(', ', $states) . '<pre>' . var_export($states, true) . '</pre>';
                                 }
                             }
                             break;
                     }
                 }
             }
         }
     }
     if (!empty($errors)) {
         self::_update_errors($errors, $post_id);
         self::_remove_recursive_filters();
         self::_disable_emails();
         do_action('qsot-before-guest-check-update-order-status', $post);
         // if the order is not pending, cancelled or failed, then update the state to pending, so that the admin knows that there is a problem
         if (!in_array($order->get_status(), array('pending', 'cancelled', 'failed'))) {
             $order->update_status('pending', __('Your current settings require you to provide most billing information for each order.', 'opentickets-community-edition'));
             // otherwise, just log a message saying that it is still messed up
         } else {
             $order->add_order_note(__('Your current settings require you to provide most billing information for each order.', 'opentickets-community-edition'), false);
         }
         self::_enable_emails();
         add_action('save_post', array(__CLASS__, 'enforce_non_guest_orders'), PHP_INT_MAX, 2);
     }
 }
        function save_address_book()
        {
            global $woocommerce;
            $this->load_cart_files();
            $checkout = new WC_Checkout();
            $user = wp_get_current_user();
            $address = $_POST['address'];
            $shipFields = $woocommerce->countries->get_address_fields($address['shipping_country'], 'shipping_');
            $errors = array();
            foreach ($shipFields as $key => $field) {
                if (isset($field['required']) && $field['required'] && empty($address[$key])) {
                    $errors[] = $key;
                }
                if (!empty($address[$key])) {
                    // Validation rules
                    if (!empty($field['validate']) && is_array($field['validate'])) {
                        foreach ($field['validate'] as $rule) {
                            switch ($rule) {
                                case 'postcode':
                                    $address[$key] = strtoupper(str_replace(' ', '', $address[$key]));
                                    if (!WC_Validation::is_postcode($address[$key], $address['shipping_country'])) {
                                        $errors[] = $key;
                                        wc_add_notice(__('Please enter a valid postcode/ZIP.', 'woocommerce'), 'error');
                                    } else {
                                        $address[$key] = wc_format_postcode($address[$key], $address['shipping_country']);
                                    }
                                    break;
                                case 'phone':
                                    $address[$key] = wc_format_phone_number($address[$key]);
                                    if (!WC_Validation::is_phone($address[$key])) {
                                        $errors[] = $key;
                                        if (function_exists('wc_add_notice')) {
                                            wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'woocommerce'), 'error');
                                        } else {
                                            $woocommerce->add_error('<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'woocommerce'));
                                        }
                                    }
                                    break;
                                case 'email':
                                    $address[$key] = strtolower($address[$key]);
                                    if (!is_email($address[$key])) {
                                        $errors[] = $key;
                                        if (function_exists('wc_add_notice')) {
                                            wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce'), 'error');
                                        } else {
                                            $woocommerce->add_error('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce'));
                                        }
                                    }
                                    break;
                                case 'state':
                                    // Get valid states
                                    $valid_states = WC()->countries->get_states($address['shipping_country']);
                                    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($address[$key])])) {
                                        $address[$key] = $valid_state_values[strtolower($address[$key])];
                                    }
                                    // Only validate if the country has specific state options
                                    if (is_array($valid_states) && sizeof($valid_states) > 0) {
                                        if (!in_array($address[$key], array_keys($valid_states))) {
                                            $errors[] = $key;
                                            if (function_exists('wc_add_notice')) {
                                                wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not valid. Please enter one of the following:', 'woocommerce') . ' ' . implode(', ', $valid_states), 'error');
                                            } else {
                                                $woocommerce->add_error('<strong>' . $field['label'] . '</strong> ' . __('is not valid. Please enter one of the following:', 'woocommerce') . ' ' . implode(', ', $valid_states));
                                            }
                                        }
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
            if (count($errors) > 0) {
                die(json_encode(array('ack' => 'ERR', 'errors' => $errors, 'message' => __('Please enter the complete address', 'wc_shipping_multiple_address'))));
            }
            $id = $_POST['id'];
            $addresses = $this->get_user_addresses($user);
            $redirect_url = isset($_POST['next']) ? $_POST['next'] : get_permalink(woocommerce_get_page_id('multiple_addresses'));
            if ($id >= 0) {
                $next = add_query_arg('updated', '1', $redirect_url);
            } else {
                $next = add_query_arg('new', '1', $redirect_url);
            }
            // address is unique, save!
            if ($id == -1) {
                $vals = '';
                foreach ($address as $key => $value) {
                    $vals .= $value;
                }
                $md5 = md5($vals);
                foreach ($addresses as $addr) {
                    $vals = '';
                    if (!is_array($addr)) {
                        continue;
                    }
                    foreach ($addr as $key => $value) {
                        $vals .= $value;
                    }
                    $addrMd5 = md5($vals);
                    if ($md5 == $addrMd5) {
                        // duplicate address!
                        die(json_encode(array('ack' => 'ERR', 'message' => __('Address is already in your address book', 'wc_shipping_multiple_address'))));
                    }
                }
                $addresses[] = $address;
            } else {
                $addresses[$id] = $address;
            }
            // update the default address and remove it from the $addresses array
            if ($user->ID > 0) {
                if ($id == 0) {
                    $default_address = $addresses[0];
                    unset($addresses[0]);
                    if ($default_address['shipping_address_1'] && $default_address['shipping_postcode']) {
                        update_user_meta($user->ID, 'shipping_first_name', $default_address['shipping_first_name']);
                        update_user_meta($user->ID, 'shipping_last_name', $default_address['shipping_last_name']);
                        update_user_meta($user->ID, 'shipping_company', $default_address['shipping_company']);
                        update_user_meta($user->ID, 'shipping_address_1', $default_address['shipping_address_1']);
                        update_user_meta($user->ID, 'shipping_address_2', $default_address['shipping_address_2']);
                        update_user_meta($user->ID, 'shipping_city', $default_address['shipping_city']);
                        update_user_meta($user->ID, 'shipping_state', $default_address['shipping_state']);
                        update_user_meta($user->ID, 'shipping_postcode', $default_address['shipping_postcode']);
                        update_user_meta($user->ID, 'shipping_country', $default_address['shipping_country']);
                    }
                    unset($addresses[0]);
                }
            }
            $this->save_user_addresses($user->ID, $addresses);
            foreach ($address as $key => $value) {
                $new_key = str_replace('shipping_', '', $key);
                $address[$new_key] = $value;
            }
            $formatted_address = wcms_get_formatted_address($address);
            $json_address = json_encode($address);
            if (!$formatted_address) {
                return;
            }
            if (isset($_POST['return']) && $_POST['return'] == 'list') {
                $html = '<option value="' . $id . '">' . $formatted_address . '</option>';
            } else {
                $html = '
                    <div class="account-address">
                        <address>' . $formatted_address . '</address>
                        <div style="display: none;">';
                ob_start();
                foreach ($shipFields as $key => $field) {
                    $val = isset($address[$key]) ? $address[$key] : '';
                    $key .= '_' . $id;
                    woocommerce_form_field($key, $field, $val);
                }
                do_action('woocommerce_after_checkout_shipping_form', $checkout);
                $html .= ob_get_clean();
                $html .= '
                            <input type="hidden" name="addresses[]" value="' . $id . '" />
                        </div>

                        <ul class="items-column" id="items_column_' . $id . '">
                            <li class="placeholder">Drag items here</li>
                        </ul>
                    </div>
                    ';
            }
            $return = json_encode(array('ack' => 'OK', 'id' => $id, 'html' => $html, 'return' => $_POST['return'], 'next' => $next));
            die($return);
        }
예제 #7
0
    /**
     * Edit subscription shipping address
     * 
     * @access public
     * @param int $subscription_id
     * @return void
     */
    public static function subscription_address($subscription_id)
    {
        if ($subscription = self::get_subscription($subscription_id)) {
            if (!$subscription->needs_shipping() || !apply_filters('subscriptio_allow_shipping_address_edit', true)) {
                self::redirect_to_subscription($subscription);
                return;
            }

            // Form submitted?
            if (isset($_POST['action']) && $_POST['action'] == 'subscriptio_edit_address') {

                // Validate address WooCommerce-style
                $address = WC()->countries->get_address_fields(esc_attr($_POST['shipping_country' ]), 'shipping_');

                foreach ($address as $key => $field) {

                    // Make sure we have field type before proceeding
                    $field['type'] = isset($field['type']) ? $field['type'] : 'text';

                    // Sanitize values
                    if ($field['type'] == 'checkbox') {
                        $_POST[$key] = isset($_POST[$key]) ? 1 : 0;
                    }
                    else {
                        $_POST[$key] = isset($_POST[$key]) ? wc_clean($_POST[$key]) : '';
                    }

                    // Required field empty?
                    if (!empty($field['required']) && empty($_POST[$key])) {
                        wc_add_notice($field['label'] . ' ' . __('is a required field.', 'subscriptio'), 'error');
                    }

                    // Validate field according to rules
                    if (!empty($field['validate']) && is_array($field['validate'])) {
                        foreach ($field['validate'] as $rule) {
                            if ($rule == 'postcode') {
                                $_POST[$key] = strtoupper(str_replace(' ', '', $_POST[$key]));

                                if (WC_Validation::is_postcode($_POST[$key], $_POST['shipping_country'])) {
                                    $_POST[$key] = wc_format_postcode($_POST[$key], $_POST['shipping_country']);
                                } else {
                                    wc_add_notice(__('Please enter a valid postcode/ZIP.', 'subscriptio'), 'error');
                                }
                            }
                            else if ($rule == 'phone') {
                                $_POST[$key] = wc_format_phone_number($_POST[$key]);

                                if (!WC_Validation::is_phone($_POST[$key])) {
                                    wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'subscriptio'), 'error');
                                }
                            }
                            else if ($rule == 'email') {
                                $_POST[$key] = strtolower($_POST[$key]);

                                if (!is_email($_POST[$key])) {
                                    wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce'), 'error');
                                }
                            }
                        }
                    }
                }

                // No errors in form?
                if (wc_notice_count('error') == 0) {

                    // Try to save address
                    if ($subscription->update_shipping_address($_POST, true, true)) {
                        wc_add_notice(__('Shipping address has been updated.', 'subscriptio'));
                    }

                    // Something went really wrong...
                    else {
                        wc_add_notice(__('Something went wrong...', 'subscriptio'), 'error');
                    }

                    // Redirect to subscription page
                    self::redirect_to_subscription($subscription);
                }
                else {
                    self::display_address_form($subscription);
                }
            }

            // Display form
            else {
                self::display_address_form($subscription);
            }
        }
    }