From 2900d9d33040e8dc4c44df8d6779fa97f840982c Mon Sep 17 00:00:00 2001 From: claudiosmweb Date: Tue, 11 Feb 2014 17:42:22 -0200 Subject: [PATCH 1/4] Removed wp_verify_nonce in wc_create_new_customer The check is already done in includes/class-wc-form-handler.php 843 and includes/class-wc-checkout.php 398 On the registration made during checkout caused the error: PHP Notice: Undefined index: register in woocommerce/includes/wc-customer-functions.php on line 53 --- includes/wc-customer-functions.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/includes/wc-customer-functions.php b/includes/wc-customer-functions.php index 7f30c7b2bde..2bef21d74e5 100644 --- a/includes/wc-customer-functions.php +++ b/includes/wc-customer-functions.php @@ -48,8 +48,6 @@ function wc_create_new_customer( $email, $username = '', $password = '' ) { if ( email_exists( $email ) ) return new WP_Error( "registration-error", __( "An account is already registered with your email address. Please login.", "woocommerce" ) ); - wp_verify_nonce( $_POST['register'], 'woocommerce-register' ); - // Handle username creation if ( get_option( 'woocommerce_registration_generate_username' ) == 'no' || ! empty( $username ) ) { From b68873a15bb13df8e9dce411692dcf031a7443b3 Mon Sep 17 00:00:00 2001 From: claudiosmweb Date: Tue, 11 Feb 2014 17:50:55 -0200 Subject: [PATCH 2/4] prevents automatic password generation when making the registration in the My Account page, closes #4672 --- includes/wc-customer-functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/wc-customer-functions.php b/includes/wc-customer-functions.php index 2bef21d74e5..092d3ab764c 100644 --- a/includes/wc-customer-functions.php +++ b/includes/wc-customer-functions.php @@ -73,7 +73,7 @@ function wc_create_new_customer( $email, $username = '', $password = '' ) { } // Handle password creation - if ( get_option( 'woocommerce_registration_generate_password' ) == 'yes' && empty( $password ) ) { + if ( get_option( 'woocommerce_registration_generate_password' ) == 'yes' && empty( $password ) && ! isset( $_POST['register'] ) ) { $password = wp_generate_password(); $password_generated = true; } elseif ( empty( $password ) ) { From fe74ffc8825e5fd133243e981d6a363336089691 Mon Sep 17 00:00:00 2001 From: claudiosmweb Date: Tue, 11 Feb 2014 17:51:10 -0200 Subject: [PATCH 3/4] fixed the indentation in includes/wc-customer-functions.php --- includes/wc-customer-functions.php | 140 ++++++++++++++--------------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/includes/wc-customer-functions.php b/includes/wc-customer-functions.php index 092d3ab764c..88d33c86860 100644 --- a/includes/wc-customer-functions.php +++ b/includes/wc-customer-functions.php @@ -92,17 +92,17 @@ function wc_create_new_customer( $email, $username = '', $password = '' ) { if ( $validation_errors->get_error_code() ) return $validation_errors; - $new_customer_data = apply_filters( 'woocommerce_new_customer_data', array( - 'user_login' => $username, - 'user_pass' => $password, - 'user_email' => $email, - 'role' => 'customer' - ) ); + $new_customer_data = apply_filters( 'woocommerce_new_customer_data', array( + 'user_login' => $username, + 'user_pass' => $password, + 'user_email' => $email, + 'role' => 'customer' + ) ); - $customer_id = wp_insert_user( $new_customer_data ); + $customer_id = wp_insert_user( $new_customer_data ); - if ( is_wp_error( $customer_id ) ) - return new WP_Error( "registration-error", '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Couldn’t register you… please contact us if you continue to have problems.', 'woocommerce' ) ); + if ( is_wp_error( $customer_id ) ) + return new WP_Error( "registration-error", '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Couldn’t register you… please contact us if you continue to have problems.', 'woocommerce' ) ); do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated ); @@ -131,14 +131,14 @@ function wc_set_customer_auth_cookie( $customer_id ) { */ function wc_update_new_customer_past_orders( $customer_id ) { - $customer = get_user_by( 'id', absint( $customer_id ) ); + $customer = get_user_by( 'id', absint( $customer_id ) ); - $customer_orders = get_posts( array( - 'numberposts' => -1, - 'post_type' => 'shop_order', - 'post_status' => 'publish', - 'fields' => 'ids', - 'meta_query' => array( + $customer_orders = get_posts( array( + 'numberposts' => -1, + 'post_type' => 'shop_order', + 'post_status' => 'publish', + 'fields' => 'ids', + 'meta_query' => array( array( 'key' => '_customer_user', 'value' => array( 0, '' ), @@ -149,16 +149,16 @@ function wc_update_new_customer_past_orders( $customer_id ) { 'value' => $customer->user_email, ) ), - ) ); + ) ); - $linked = 0; - $complete = 0; + $linked = 0; + $complete = 0; - if ( $customer_orders ) - foreach ( $customer_orders as $order_id ) { - update_post_meta( $order_id, '_customer_user', $customer->ID ); + if ( $customer_orders ) + foreach ( $customer_orders as $order_id ) { + update_post_meta( $order_id, '_customer_user', $customer->ID ); - $order_status = wp_get_post_terms( $order_id, 'shop_order_status' ); + $order_status = wp_get_post_terms( $order_id, 'shop_order_status' ); if ( $order_status ) { $order_status = current( $order_status ); @@ -168,16 +168,16 @@ function wc_update_new_customer_past_orders( $customer_id ) { if ( $order_status == 'completed' ) $complete ++; - $linked ++; - } + $linked ++; + } - if ( $complete ) { - update_user_meta( $customer_id, 'paying_customer', 1 ); - update_user_meta( $customer_id, '_order_count', '' ); - update_user_meta( $customer_id, '_money_spent', '' ); - } + if ( $complete ) { + update_user_meta( $customer_id, 'paying_customer', 1 ); + update_user_meta( $customer_id, '_order_count', '' ); + update_user_meta( $customer_id, '_money_spent', '' ); + } - return $linked; + return $linked; } /** @@ -267,62 +267,62 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) { */ function wc_customer_has_capability( $allcaps, $caps, $args ) { if ( isset( $caps[0] ) ) { - switch ( $caps[0] ) { + switch ( $caps[0] ) { - case 'view_order': - $user_id = $args[1]; - $order = new WC_Order( $args[2] ); + case 'view_order': + $user_id = $args[1]; + $order = new WC_Order( $args[2] ); - if ( $user_id == $order->user_id ) - $allcaps['view_order'] = true; + if ( $user_id == $order->user_id ) + $allcaps['view_order'] = true; - break; + break; - case 'pay_for_order': - $user_id = $args[1]; - $order_id = isset($args[2]) ? $args[2] : null; + case 'pay_for_order': + $user_id = $args[1]; + $order_id = isset($args[2]) ? $args[2] : null; - // When no order ID, we assume it's a new order - // and thus, customer can pay for it - if (!$order_id) { - $allcaps['pay_for_order'] = true; - break; - } + // When no order ID, we assume it's a new order + // and thus, customer can pay for it + if (!$order_id) { + $allcaps['pay_for_order'] = true; + break; + } - $order = new WC_Order( $order_id ); + $order = new WC_Order( $order_id ); - if ( $user_id == $order->user_id ) - $allcaps['pay_for_order'] = true; + if ( $user_id == $order->user_id ) + $allcaps['pay_for_order'] = true; - break; + break; - case 'order_again': - $user_id = $args[1]; - $order = new WC_Order( $args[2] ); + case 'order_again': + $user_id = $args[1]; + $order = new WC_Order( $args[2] ); - if ( $user_id == $order->user_id ) - $allcaps['order_again'] = true; + if ( $user_id == $order->user_id ) + $allcaps['order_again'] = true; - break; + break; - case 'cancel_order': - $user_id = $args[1]; - $order = new WC_Order( $args[2] ); + case 'cancel_order': + $user_id = $args[1]; + $order = new WC_Order( $args[2] ); - if ( $user_id == $order->user_id ) - $allcaps['cancel_order'] = true; + if ( $user_id == $order->user_id ) + $allcaps['cancel_order'] = true; - break; + break; - case 'download_file': - $user_id = $args[1]; - $download = $args[2]; + case 'download_file': + $user_id = $args[1]; + $download = $args[2]; - if ( $user_id == $download->user_id ) - $allcaps['download_file'] = true; + if ( $user_id == $download->user_id ) + $allcaps['download_file'] = true; - break; - } + break; + } } return $allcaps; } From 2b3ce42284e96b8feee94f0aef0dd374ad0373b9 Mon Sep 17 00:00:00 2001 From: claudiosmweb Date: Tue, 11 Feb 2014 17:57:15 -0200 Subject: [PATCH 4/4] fixed some coding standards in includes/wc-customer-functions.php --- includes/wc-customer-functions.php | 156 ++++++++++++++++------------- 1 file changed, 86 insertions(+), 70 deletions(-) diff --git a/includes/wc-customer-functions.php b/includes/wc-customer-functions.php index 88d33c86860..967f6eafd2f 100644 --- a/includes/wc-customer-functions.php +++ b/includes/wc-customer-functions.php @@ -7,10 +7,12 @@ * @author WooThemes * @category Core * @package WooCommerce/Functions - * @version 2.1.0 + * @version 2.1.0 */ -if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly +if ( ! defined( 'ABSPATH' ) ) { + exit; // Exit if accessed directly +} /** * Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from seeing the admin bar @@ -22,7 +24,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly * @return bool */ function wc_disable_admin_bar( $show_admin_bar ) { - if ( apply_filters( 'woocommerce_disable_admin_bar', get_option( 'woocommerce_lock_down_admin', "yes" ) == "yes" ) && ! ( current_user_can('edit_posts') || current_user_can('manage_woocommerce') ) ) { + if ( apply_filters( 'woocommerce_disable_admin_bar', get_option( 'woocommerce_lock_down_admin', 'yes' ) == 'yes' ) && ! ( current_user_can( 'edit_posts' ) || current_user_can( 'manage_woocommerce' ) ) ) { $show_admin_bar = false; } @@ -42,22 +44,25 @@ add_filter( 'show_admin_bar', 'wc_disable_admin_bar', 10, 1 ); function wc_create_new_customer( $email, $username = '', $password = '' ) { // Check the e-mail address - if ( empty( $email ) || ! is_email( $email ) ) - return new WP_Error( "registration-error", __( "Please provide a valid email address.", "woocommerce" ) ); + if ( empty( $email ) || ! is_email( $email ) ) { + return new WP_Error( 'registration-error', __( 'Please provide a valid email address.', 'woocommerce' ) ); + } - if ( email_exists( $email ) ) - return new WP_Error( "registration-error", __( "An account is already registered with your email address. Please login.", "woocommerce" ) ); + if ( email_exists( $email ) ) { + return new WP_Error( 'registration-error', __( 'An account is already registered with your email address. Please login.', 'woocommerce' ) ); + } // Handle username creation - if ( get_option( 'woocommerce_registration_generate_username' ) == 'no' || ! empty( $username ) ) { + if ( 'no' == get_option( 'woocommerce_registration_generate_username' ) || ! empty( $username ) ) { $username = sanitize_user( $username ); - if ( empty( $username ) || ! validate_username( $username ) ) - return new WP_Error( "registration-error", __( "Please enter a valid account username.", "woocommerce" ) ); + if ( empty( $username ) || ! validate_username( $username ) ) { + return new WP_Error( 'registration-error', __( 'Please enter a valid account username.', 'woocommerce' ) ); + } if ( username_exists( $username ) ) - return new WP_Error( "registration-error", __( "An account is already registered with that username. Please choose another.", "woocommerce" ) ); + return new WP_Error( 'registration-error', __( 'An account is already registered with that username. Please choose another.', 'woocommerce' ) ); } else { $username = sanitize_user( current( explode( '@', $email ) ) ); @@ -73,11 +78,11 @@ function wc_create_new_customer( $email, $username = '', $password = '' ) { } // Handle password creation - if ( get_option( 'woocommerce_registration_generate_password' ) == 'yes' && empty( $password ) && ! isset( $_POST['register'] ) ) { + if ( 'yes' == get_option( 'woocommerce_registration_generate_password' ) && empty( $password ) && ! isset( $_POST['register'] ) ) { $password = wp_generate_password(); $password_generated = true; } elseif ( empty( $password ) ) { - return new WP_Error( "registration-error", __( "Please enter an account password.", "woocommerce" ) ); + return new WP_Error( 'registration-error', __( 'Please enter an account password.', 'woocommerce' ) ); } else { $password_generated = false; } @@ -101,8 +106,9 @@ function wc_create_new_customer( $email, $username = '', $password = '' ) { $customer_id = wp_insert_user( $new_customer_data ); - if ( is_wp_error( $customer_id ) ) - return new WP_Error( "registration-error", '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Couldn’t register you… please contact us if you continue to have problems.', 'woocommerce' ) ); + if ( is_wp_error( $customer_id ) ) { + return new WP_Error( 'registration-error', '' . __( 'ERROR', 'woocommerce' ) . ': ' . __( 'Couldn’t register you… please contact us if you continue to have problems.', 'woocommerce' ) ); + } do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated ); @@ -223,11 +229,13 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) { $emails[] = $user->user_email; } - if ( is_email( $customer_email ) ) + if ( is_email( $customer_email ) ) { $emails[] = $customer_email; + } - if ( sizeof( $emails ) == 0 ) + if ( sizeof( $emails ) == 0 ) { return false; + } $completed = get_term_by( 'slug', 'completed', 'shop_order_status' ); $processing = get_term_by( 'slug', 'processing', 'shop_order_status' ); @@ -266,64 +274,72 @@ function wc_customer_bought_product( $customer_email, $user_id, $product_id ) { * @return bool */ function wc_customer_has_capability( $allcaps, $caps, $args ) { - if ( isset( $caps[0] ) ) { - switch ( $caps[0] ) { + if ( isset( $caps[0] ) ) { + switch ( $caps[0] ) { - case 'view_order': - $user_id = $args[1]; - $order = new WC_Order( $args[2] ); + case 'view_order' : + $user_id = $args[1]; + $order = new WC_Order( $args[2] ); - if ( $user_id == $order->user_id ) - $allcaps['view_order'] = true; + if ( $user_id == $order->user_id ) { + $allcaps['view_order'] = true; + } - break; + break; - case 'pay_for_order': - $user_id = $args[1]; - $order_id = isset($args[2]) ? $args[2] : null; + case 'pay_for_order' : + $user_id = $args[1]; + $order_id = isset( $args[2] ) ? $args[2] : null; - // When no order ID, we assume it's a new order - // and thus, customer can pay for it - if (!$order_id) { - $allcaps['pay_for_order'] = true; - break; + // When no order ID, we assume it's a new order + // and thus, customer can pay for it + if ( ! $order_id ) { + $allcaps['pay_for_order'] = true; + + break; + } + + $order = new WC_Order( $order_id ); + + if ( $user_id == $order->user_id ) { + $allcaps['pay_for_order'] = true; + } + + break; + + case 'order_again' : + $user_id = $args[1]; + $order = new WC_Order( $args[2] ); + + if ( $user_id == $order->user_id ) { + $allcaps['order_again'] = true; + } + + break; + + case 'cancel_order' : + $user_id = $args[1]; + $order = new WC_Order( $args[2] ); + + if ( $user_id == $order->user_id ) { + $allcaps['cancel_order'] = true; + } + + break; + + case 'download_file' : + $user_id = $args[1]; + $download = $args[2]; + + if ( $user_id == $download->user_id ) { + $allcaps['download_file'] = true; + } + + break; } - - $order = new WC_Order( $order_id ); - - if ( $user_id == $order->user_id ) - $allcaps['pay_for_order'] = true; - - break; - - case 'order_again': - $user_id = $args[1]; - $order = new WC_Order( $args[2] ); - - if ( $user_id == $order->user_id ) - $allcaps['order_again'] = true; - - break; - - case 'cancel_order': - $user_id = $args[1]; - $order = new WC_Order( $args[2] ); - - if ( $user_id == $order->user_id ) - $allcaps['cancel_order'] = true; - - break; - - case 'download_file': - $user_id = $args[1]; - $download = $args[2]; - - if ( $user_id == $download->user_id ) - $allcaps['download_file'] = true; - - break; } - } - return $allcaps; + + return $allcaps; } -add_filter( 'user_has_cap', 'wc_customer_has_capability', 10, 3); + +add_filter( 'user_has_cap', 'wc_customer_has_capability', 10, 3 );