From f1a9dbc69ea587bfd1ea4bcb5f2881a0cd51c15b Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Tue, 13 Jun 2017 13:32:21 -0700 Subject: [PATCH 1/3] Extra checks and tests --- .../reports/class-wc-report-customer-list.php | 15 ++++++---- .../data-stores/class-wc-data-store-wp.php | 3 +- .../class-wc-order-data-store-cpt.php | 29 ++++++++++++++++--- includes/wc-user-functions.php | 11 +++++-- tests/unit-tests/customer/functions.php | 21 ++++++++++++++ tests/unit-tests/order/functions.php | 9 ++++++ 6 files changed, 75 insertions(+), 13 deletions(-) diff --git a/includes/admin/reports/class-wc-report-customer-list.php b/includes/admin/reports/class-wc-report-customer-list.php index e01bedfe5e2..60a4f9c9ebc 100644 --- a/includes/admin/reports/class-wc-report-customer-list.php +++ b/includes/admin/reports/class-wc-report-customer-list.php @@ -164,11 +164,16 @@ class WC_Report_Customer_List extends WP_List_Table { 'action' => "view", ); - $orders = wc_get_orders( array( - 'limit' => 1, - 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ), - 'customer' => array( array( 0, $user->user_email ) ), - ) ); + if ( is_email( $user->user_email ) ) { + $orders = wc_get_orders( array( + 'limit' => 1, + 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ), + 'billing_email' => $user->user_email, + 'customer_id' => 0, + ) ); + } else { + $orders = array(); + } if ( $orders ) { $actions['link'] = array( diff --git a/includes/data-stores/class-wc-data-store-wp.php b/includes/data-stores/class-wc-data-store-wp.php index cc0d53aa832..ca00fc01373 100644 --- a/includes/data-stores/class-wc-data-store-wp.php +++ b/includes/data-stores/class-wc-data-store-wp.php @@ -208,7 +208,8 @@ class WC_Data_Store_WP { $skipped_values = array( '', array(), null ); $wp_query_args = array( - 'meta_query' => array(), + 'errors' => array(), + 'meta_query' => array(), ); foreach ( $query_vars as $key => $value ) { diff --git a/includes/data-stores/class-wc-order-data-store-cpt.php b/includes/data-stores/class-wc-order-data-store-cpt.php index 65e17c1530c..b5eae530775 100644 --- a/includes/data-stores/class-wc-order-data-store-cpt.php +++ b/includes/data-stores/class-wc-order-data-store-cpt.php @@ -389,11 +389,17 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement ); foreach ( $values as $value ) { if ( is_array( $value ) ) { - $meta_query[] = $this->get_orders_generate_customer_meta_query( $value, 'and' ); + $query_part = $this->get_orders_generate_customer_meta_query( $value, 'and' ); + if ( is_wp_error( $query_part ) ) { + return $query_part; + } + $meta_query[] = $query_part; } elseif ( is_email( $value ) ) { $meta_query['customer_emails']['value'][] = sanitize_email( $value ); - } else { + } elseif ( is_numeric( $value ) ) { $meta_query['customer_ids']['value'][] = strval( absint( $value ) ); + } else { + return new WP_Error( 'woocommerce_query_invalid', __( 'Invalid customer query.', 'woocommerce' ), $values ); } } @@ -648,7 +654,12 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement if ( isset( $query_vars['customer'] ) && '' !== $query_vars['customer'] && array() !== $query_vars['customer'] ) { $values = is_array( $query_vars['customer'] ) ? $query_vars['customer'] : array( $query_vars['customer'] ); - $wp_query_args['meta_query'][] = $this->get_orders_generate_customer_meta_query( $values ); + $customer_query = $this->get_orders_generate_customer_meta_query( $values ); + if ( is_wp_error( $customer_query) ) { + $wp_query_args['errors'][] = $customer_query; + } else { + $wp_query_args['meta_query'][] = $customer_query; + } } if ( ! isset( $query_vars['paginate'] ) || ! $query_vars['paginate'] ) { @@ -669,7 +680,17 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement */ public function query( $query_vars ) { $args = $this->get_wp_query_args( $query_vars ); - $query = new WP_Query( $args ); + + if ( ! empty( $args['errors'] ) ) { + $query = (object) array( + 'posts' => array(), + 'found_posts' => 0, + 'max_num_pages' => 0, + ); + } else { + $query = new WP_Query( $args ); + } + $orders = ( isset( $query_vars['return'] ) && 'ids' === $query_vars['return'] ) ? $query->posts : array_filter( array_map( 'wc_get_order', $query->posts ) ); if ( isset( $query_vars['paginate'] ) && $query_vars['paginate'] ) { diff --git a/includes/wc-user-functions.php b/includes/wc-user-functions.php index 1b41bdcdf5e..ea520367d49 100644 --- a/includes/wc-user-functions.php +++ b/includes/wc-user-functions.php @@ -140,10 +140,15 @@ function wc_update_new_customer_past_orders( $customer_id ) { $linked = 0; $complete = 0; $customer = get_user_by( 'id', absint( $customer_id ) ); + if ( ! is_email( $customer->user_email ) ) { + return $linked; + } + $customer_orders = wc_get_orders( array( - 'limit' => -1, - 'customer' => array( array( 0, $customer->user_email ) ), - 'return' => 'ids', + 'limit' => -1, + 'billing_email' => $customer->user_email, + 'customer_id' => 0, + 'return' => 'ids', ) ); if ( ! empty( $customer_orders ) ) { diff --git a/tests/unit-tests/customer/functions.php b/tests/unit-tests/customer/functions.php index 808171eb90b..4b60fc33cb8 100644 --- a/tests/unit-tests/customer/functions.php +++ b/tests/unit-tests/customer/functions.php @@ -80,6 +80,27 @@ class WC_Tests_Customer_Functions extends WC_Unit_Test_Case { $this->assertEquals( 0, $linked ); } + /** + * Test wc_update_new_customer_past_orders with invalid or changed email. + * + * @since 3.1 + */ + function test_wc_update_new_customer_past_orders_invalid_changed_email() { + $customer_id = wc_create_new_customer( 'test@example.com', 'testuser', 'testpassword' ); + $order1 = new WC_Order; + $order1->set_billing_email( 'test@example.com' ); + $order1->set_status( 'completed' ); + $order1->save(); + + wp_update_user( array( 'ID' => $customer_id, 'user_email' => 'invalid' ) ); + $linked = wc_update_new_customer_past_orders( $customer_id ); + $this->assertEquals( 0, $linked ); + + wp_update_user( array( 'ID' => $customer_id, 'user_email' => 'new@example.com' ) ); + $linked = wc_update_new_customer_past_orders( $customer_id ); + $this->assertEquals( 0, $linked ); + } + /** * Test wc_paying_customer. * diff --git a/tests/unit-tests/order/functions.php b/tests/unit-tests/order/functions.php index fdf9b41547f..adae5019c3f 100644 --- a/tests/unit-tests/order/functions.php +++ b/tests/unit-tests/order/functions.php @@ -650,6 +650,15 @@ class WC_Tests_Order_Functions extends WC_Unit_Test_Case { $orders = wc_get_orders( array( 'customer' => $customer2->get_id(), 'return' => 'ids' ) ); $expected = array( $order2->get_id() ); $this->assertEquals( $expected, $orders ); + + $orders = wc_get_orders( array( 'customer' => 'invalid' ) ); + $this->assertEmpty( $orders ); + + $orders = wc_get_orders( array( 'customer' => array( 'invalid' ) ) ); + $this->assertEmpty( $orders ); + + $orders = wc_get_orders( array( 'customer' => 'doesnt@exist.com' ) ); + $this->assertEmpty( $orders ); } /** From 489e67dbe122bbc8223cc98f7f89d79596d6a558 Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Tue, 13 Jun 2017 13:37:40 -0700 Subject: [PATCH 2/3] Remove unnecessary checks --- .../reports/class-wc-report-customer-list.php | 15 +++++---------- includes/wc-user-functions.php | 11 +++-------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/includes/admin/reports/class-wc-report-customer-list.php b/includes/admin/reports/class-wc-report-customer-list.php index 60a4f9c9ebc..e01bedfe5e2 100644 --- a/includes/admin/reports/class-wc-report-customer-list.php +++ b/includes/admin/reports/class-wc-report-customer-list.php @@ -164,16 +164,11 @@ class WC_Report_Customer_List extends WP_List_Table { 'action' => "view", ); - if ( is_email( $user->user_email ) ) { - $orders = wc_get_orders( array( - 'limit' => 1, - 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ), - 'billing_email' => $user->user_email, - 'customer_id' => 0, - ) ); - } else { - $orders = array(); - } + $orders = wc_get_orders( array( + 'limit' => 1, + 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ), + 'customer' => array( array( 0, $user->user_email ) ), + ) ); if ( $orders ) { $actions['link'] = array( diff --git a/includes/wc-user-functions.php b/includes/wc-user-functions.php index ea520367d49..1b41bdcdf5e 100644 --- a/includes/wc-user-functions.php +++ b/includes/wc-user-functions.php @@ -140,15 +140,10 @@ function wc_update_new_customer_past_orders( $customer_id ) { $linked = 0; $complete = 0; $customer = get_user_by( 'id', absint( $customer_id ) ); - if ( ! is_email( $customer->user_email ) ) { - return $linked; - } - $customer_orders = wc_get_orders( array( - 'limit' => -1, - 'billing_email' => $customer->user_email, - 'customer_id' => 0, - 'return' => 'ids', + 'limit' => -1, + 'customer' => array( array( 0, $customer->user_email ) ), + 'return' => 'ids', ) ); if ( ! empty( $customer_orders ) ) { From 62064d56f84151393a718f848f2be2fefc4e3614 Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Tue, 13 Jun 2017 14:00:12 -0700 Subject: [PATCH 3/3] Add edge case test --- includes/data-stores/class-wc-order-data-store-cpt.php | 2 +- tests/unit-tests/order/functions.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/includes/data-stores/class-wc-order-data-store-cpt.php b/includes/data-stores/class-wc-order-data-store-cpt.php index b5eae530775..a6dd29170d6 100644 --- a/includes/data-stores/class-wc-order-data-store-cpt.php +++ b/includes/data-stores/class-wc-order-data-store-cpt.php @@ -655,7 +655,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement if ( isset( $query_vars['customer'] ) && '' !== $query_vars['customer'] && array() !== $query_vars['customer'] ) { $values = is_array( $query_vars['customer'] ) ? $query_vars['customer'] : array( $query_vars['customer'] ); $customer_query = $this->get_orders_generate_customer_meta_query( $values ); - if ( is_wp_error( $customer_query) ) { + if ( is_wp_error( $customer_query ) ) { $wp_query_args['errors'][] = $customer_query; } else { $wp_query_args['meta_query'][] = $customer_query; diff --git a/tests/unit-tests/order/functions.php b/tests/unit-tests/order/functions.php index adae5019c3f..64cae00d685 100644 --- a/tests/unit-tests/order/functions.php +++ b/tests/unit-tests/order/functions.php @@ -657,6 +657,9 @@ class WC_Tests_Order_Functions extends WC_Unit_Test_Case { $orders = wc_get_orders( array( 'customer' => array( 'invalid' ) ) ); $this->assertEmpty( $orders ); + $orders = wc_get_orders( array( 'customer' => array( '' ) ) ); + $this->assertEmpty( $orders ); + $orders = wc_get_orders( array( 'customer' => 'doesnt@exist.com' ) ); $this->assertEmpty( $orders ); }