fixed some coding standards in includes/class-wc-form-handler.php

This commit is contained in:
claudiosmweb 2014-02-07 16:35:30 -02:00
parent e0b729d273
commit d3f09d45b0
1 changed files with 92 additions and 54 deletions

View File

@ -1,6 +1,8 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Handle frontend forms
@ -42,18 +44,21 @@ class WC_Form_Handler {
public function save_address() {
global $woocommerce, $wp;
if ( 'POST' !== strtoupper( $_SERVER[ 'REQUEST_METHOD' ] ) )
if ( 'POST' !== strtoupper( $_SERVER[ 'REQUEST_METHOD' ] ) ) {
return;
}
if ( empty( $_POST[ 'action' ] ) || ( 'edit_address' !== $_POST[ 'action' ] ) )
if ( empty( $_POST[ 'action' ] ) || ( 'edit_address' !== $_POST[ 'action' ] ) ) {
return;
}
wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-edit_address' );
$user_id = get_current_user_id();
if ( $user_id <= 0 )
if ( $user_id <= 0 ) {
return;
}
$load_address = isset( $wp->query_vars['edit-address'] ) ? sanitize_key( $wp->query_vars['edit-address'] ) : 'billing';
@ -61,8 +66,9 @@ class WC_Form_Handler {
foreach ( $address as $key => $field ) {
if ( ! isset( $field['type'] ) )
if ( ! isset( $field['type'] ) ) {
$field['type'] = 'text';
}
// Get Value
switch ( $field['type'] ) {
@ -78,8 +84,9 @@ class WC_Form_Handler {
$_POST[ $key ] = apply_filters( 'woocommerce_process_myaccount_field_' . $key, $_POST[ $key ] );
// Validation: Required fields
if ( ! empty( $field['required'] ) && empty( $_POST[ $key ] ) )
if ( ! empty( $field['required'] ) && empty( $_POST[ $key ] ) ) {
wc_add_notice( $field['label'] . ' ' . __( 'is a required field.', 'woocommerce' ), 'error' );
}
// Validation rules
if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) {
@ -88,23 +95,25 @@ class WC_Form_Handler {
case 'postcode' :
$_POST[ $key ] = strtoupper( str_replace( ' ', '', $_POST[ $key ] ) );
if ( ! WC_Validation::is_postcode( $_POST[ $key ], $_POST[ $load_address . '_country' ] ) ) :
if ( ! WC_Validation::is_postcode( $_POST[ $key ], $_POST[ $load_address . '_country' ] ) ) {
wc_add_notice( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ), 'error' );
else :
} 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 ] ) )
if ( ! WC_Validation::is_phone( $_POST[ $key ] ) ) {
wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid phone number.', 'woocommerce' ), 'error' );
}
break;
case 'email' :
$_POST[ $key ] = strtolower( $_POST[ $key ] );
if ( ! is_email( $_POST[ $key ] ) )
if ( ! is_email( $_POST[ $key ] ) ) {
wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid email address.', 'woocommerce' ), 'error' );
}
break;
}
}
@ -113,8 +122,9 @@ class WC_Form_Handler {
if ( wc_notice_count( 'error' ) == 0 ) {
foreach ( $address as $key => $field )
foreach ( $address as $key => $field ) {
update_user_meta( $user_id, $key, $_POST[ $key ] );
}
wc_add_notice( __( 'Address changed successfully.', 'woocommerce' ) );
@ -130,11 +140,13 @@ class WC_Form_Handler {
*/
public function save_account_details() {
if ( 'POST' !== strtoupper( $_SERVER[ 'REQUEST_METHOD' ] ) )
if ( 'POST' !== strtoupper( $_SERVER[ 'REQUEST_METHOD' ] ) ) {
return;
}
if ( empty( $_POST[ 'action' ] ) || ( 'save_account_details' !== $_POST[ 'action' ] ) )
if ( empty( $_POST[ 'action' ] ) || ( 'save_account_details' !== $_POST[ 'action' ] ) ) {
return;
}
wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-save_account_details' );
@ -145,8 +157,9 @@ class WC_Form_Handler {
$user->ID = (int) get_current_user_id();
$current_user = get_user_by( 'id', $user->ID );
if ( $user->ID <= 0 )
if ( $user->ID <= 0 ) {
return;
}
$account_first_name = ! empty( $_POST[ 'account_first_name' ] ) ? wc_clean( $_POST[ 'account_first_name' ] ) : '';
$account_last_name = ! empty( $_POST[ 'account_last_name' ] ) ? wc_clean( $_POST[ 'account_last_name' ] ) : '';
@ -159,30 +172,34 @@ class WC_Form_Handler {
$user->user_email = $account_email;
$user->display_name = $user->first_name;
if ( $pass1 )
$user->user_pass = $pass1;
if ( $pass1 ) {
$user->user_pass = $pass1;
}
if ( empty( $account_first_name ) || empty( $account_last_name ) )
if ( empty( $account_first_name ) || empty( $account_last_name ) ) {
wc_add_notice( __( 'Please enter your name.', 'woocommerce' ), 'error' );
}
if ( empty( $account_email ) || ! is_email( $account_email ) )
if ( empty( $account_email ) || ! is_email( $account_email ) ) {
wc_add_notice( __( 'Please provide a valid email address.', 'woocommerce' ), 'error' );
elseif ( email_exists( $account_email ) && $account_email !== $current_user->user_email )
} elseif ( email_exists( $account_email ) && $account_email !== $current_user->user_email ) {
wc_add_notice( __( 'This email address is already registered.', 'woocommerce' ), 'error' );
}
if ( ! empty( $pass1 ) && empty( $pass2 ) )
if ( ! empty( $pass1 ) && empty( $pass2 ) ) {
wc_add_notice( __( 'Please re-enter your password.', 'woocommerce' ), 'error' );
elseif ( ! empty( $pass1 ) && $pass1 !== $pass2 )
} elseif ( ! empty( $pass1 ) && $pass1 !== $pass2 ) {
wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
}
// Allow plugins to return their own errors.
do_action_ref_array( 'user_profile_update_errors', array ( &$errors, $update, &$user ) );
if ( $errors->get_error_messages() )
foreach( $errors->get_error_messages() as $error )
if ( $errors->get_error_messages() ) {
foreach ( $errors->get_error_messages() as $error ) {
wc_add_notice( $error, 'error' );
}
}
if ( wc_notice_count( 'error' ) == 0 ) {
@ -208,8 +225,9 @@ class WC_Form_Handler {
exit;
}
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) )
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
define( 'WOOCOMMERCE_CHECKOUT', true );
}
$woocommerce_checkout = WC()->checkout();
$woocommerce_checkout->process_checkout();
@ -234,14 +252,18 @@ class WC_Form_Handler {
if ( $order->id == $order_id && $order->order_key == $order_key && in_array( $order->status, array( 'pending', 'failed' ) ) ) {
// Set customer location to order location
if ( $order->billing_country )
if ( $order->billing_country ) {
WC()->customer->set_country( $order->billing_country );
if ( $order->billing_state )
}
if ( $order->billing_state ) {
WC()->customer->set_state( $order->billing_state );
if ( $order->billing_postcode )
}
if ( $order->billing_postcode ) {
WC()->customer->set_postcode( $order->billing_postcode );
if ( $order->billing_city )
}
if ( $order->billing_city ) {
WC()->customer->set_city( $order->billing_city );
}
// Update payment method
if ( $order->needs_payment() ) {
@ -252,8 +274,9 @@ class WC_Form_Handler {
// Update meta
update_post_meta( $order_id, '_payment_method', $payment_method );
if ( isset( $available_gateways[ $payment_method ] ) )
if ( isset( $available_gateways[ $payment_method ] ) ) {
$payment_method_title = $available_gateways[ $payment_method ]->get_title();
}
update_post_meta( $order_id, '_payment_method_title', $payment_method_title);
@ -266,7 +289,7 @@ class WC_Form_Handler {
$result = $available_gateways[ $payment_method ]->process_payment( $order_id );
// Redirect to success/confirmation/payment page
if ( $result['result'] == 'success' ) {
if ( 'success' == $result['result'] ) {
wp_redirect( $result['redirect'] );
exit;
}
@ -361,13 +384,14 @@ class WC_Form_Handler {
$_product = $values['data'];
// Skip product if no updated quantity was posted
if ( ! isset( $cart_totals[ $cart_item_key ]['qty'] ) )
if ( ! isset( $cart_totals[ $cart_item_key ]['qty'] ) ) {
continue;
}
// Sanitize
$quantity = apply_filters( 'woocommerce_stock_amount_cart_item', apply_filters( 'woocommerce_stock_amount', preg_replace( "/[^0-9\.]/", "", $cart_totals[ $cart_item_key ]['qty'] ) ), $cart_item_key );
$quantity = apply_filters( 'woocommerce_stock_amount_cart_item', apply_filters( 'woocommerce_stock_amount', preg_replace( "/[^0-9\.]/", '', $cart_totals[ $cart_item_key ]['qty'] ) ), $cart_item_key );
if ( "" === $quantity || $quantity == $values['quantity'] )
if ( '' === $quantity || $quantity == $values['quantity'] )
continue;
// Update cart validation
@ -379,8 +403,9 @@ class WC_Form_Handler {
$passed_validation = false;
}
if ( $passed_validation )
if ( $passed_validation ) {
WC()->cart->set_quantity( $cart_item_key, $quantity, false );
}
$cart_updated = true;
}
@ -408,8 +433,9 @@ class WC_Form_Handler {
public function order_again() {
// Nothing to do
if ( ! isset( $_GET['order_again'] ) || ! is_user_logged_in() || ! wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-order_again' ) )
if ( ! isset( $_GET['order_again'] ) || ! is_user_logged_in() || ! wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-order_again' ) ) {
return;
}
// Clear current cart
WC()->cart->empty_cart();
@ -417,16 +443,19 @@ class WC_Form_Handler {
// Load the previous order - Stop if the order does not exist
$order = new WC_Order( absint( $_GET['order_again'] ) );
if ( empty( $order->id ) )
if ( empty( $order->id ) ) {
return;
}
if ( $order->status != 'completed' )
if ( 'completed' != $order->status ) {
return;
}
// Make sure the user is allowed to order again. By default it check if the
// previous order belonged to the current user.
if ( !current_user_can( 'order_again', $order->id ) )
if ( ! current_user_can( 'order_again', $order->id ) ) {
return;
}
// Copy products from the order to the cart
foreach ( $order->get_items() as $item ) {
@ -440,13 +469,15 @@ class WC_Form_Handler {
foreach ( $item['item_meta'] as $meta_name => $meta_value ) {
if ( taxonomy_is_product_attribute( $meta_name ) ) {
$variations[ $meta_name ] = $meta_value[0];
} elseif( meta_is_product_attribute( $meta_name, $meta_value, $product_id ) ) {
} elseif ( meta_is_product_attribute( $meta_name, $meta_value, $product_id ) ) {
$variations[ $meta_name ] = $meta_value[0];
}
}
// Add to cart validation
if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data ) ) continue;
if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data ) ) {
continue;
}
WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations, $cart_item_data );
}
@ -503,8 +534,9 @@ class WC_Form_Handler {
* @param bool $url (default: false)
*/
public function add_to_cart_action( $url = false ) {
if ( empty( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) )
if ( empty( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) {
return;
}
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
$was_added_to_cart = false;
@ -531,8 +563,9 @@ class WC_Form_Handler {
// Verify all attributes
foreach ( $attributes as $attribute ) {
if ( ! $attribute['is_variation'] )
if ( ! $attribute['is_variation'] ) {
continue;
}
$taxonomy = 'attribute_' . sanitize_title( $attribute['name'] );
@ -547,8 +580,9 @@ class WC_Form_Handler {
// Allow if valid
if ( $valid_value == '' || $valid_value == $value ) {
if ( $attribute['is_taxonomy'] )
if ( $attribute['is_taxonomy'] ) {
$variations[ $taxonomy ] = $value;
}
else {
// For custom attributes, get the name from the slug
$options = array_map( 'trim', explode( WC_DELIMITER, $attribute['value'] ) );
@ -592,8 +626,9 @@ class WC_Form_Handler {
$quantity_set = false;
foreach ( $_REQUEST['quantity'] as $item => $quantity ) {
if ( $quantity <= 0 )
if ( $quantity <= 0 ) {
continue;
}
$quantity_set = true;
@ -740,8 +775,9 @@ class WC_Form_Handler {
* Handle reset password form
*/
public function process_reset_password() {
if ( ! isset( $_POST['wc_reset_password'] ) )
if ( ! isset( $_POST['wc_reset_password'] ) ) {
return;
}
// process lost password form
if ( isset( $_POST['user_login'] ) ) {
@ -751,7 +787,7 @@ class WC_Form_Handler {
}
// process reset password form
if( isset( $_POST['password_1'] ) && isset( $_POST['password_2'] ) && isset( $_POST['reset_key'] ) && isset( $_POST['reset_login'] ) ) {
if ( isset( $_POST['password_1'] ) && isset( $_POST['password_2'] ) && isset( $_POST['reset_key'] ) && isset( $_POST['reset_login'] ) ) {
// verify reset key again
$user = WC_Shortcode_My_Account::check_password_reset_key( $_POST['reset_key'], $_POST['reset_login'] );
@ -764,23 +800,25 @@ class WC_Form_Handler {
wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-reset_password' );
if( empty( $_POST['password_1'] ) || empty( $_POST['password_2'] ) ) {
if ( empty( $_POST['password_1'] ) || empty( $_POST['password_2'] ) ) {
wc_add_notice( __( 'Please enter your password.', 'woocommerce' ), 'error' );
$args['form'] = 'reset_password';
}
if( $_POST[ 'password_1' ] !== $_POST[ 'password_2' ] ) {
if ( $_POST[ 'password_1' ] !== $_POST[ 'password_2' ] ) {
wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' );
$args['form'] = 'reset_password';
}
$errors = new WP_Error();
do_action( 'validate_password_reset', $errors, $user );
if ( $errors->get_error_messages() )
foreach( $errors->get_error_messages() as $error )
if ( $errors->get_error_messages() ) {
foreach ( $errors->get_error_messages() as $error ) {
wc_add_notice( $error, 'error');
}
}
if( 0 == wc_notice_count( 'error' ) ) {
if ( 0 == wc_notice_count( 'error' ) ) {
WC_Shortcode_My_Account::reset_password( $user, wc_clean( $_POST['password_1'] ) );