Merge pull request #15600 from woocommerce/improvement/15596
Add checks in get_orders_generate_customer_meta_query
This commit is contained in:
commit
3132d641e3
|
@ -208,6 +208,7 @@ class WC_Data_Store_WP {
|
||||||
|
|
||||||
$skipped_values = array( '', array(), null );
|
$skipped_values = array( '', array(), null );
|
||||||
$wp_query_args = array(
|
$wp_query_args = array(
|
||||||
|
'errors' => array(),
|
||||||
'meta_query' => array(),
|
'meta_query' => array(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -389,11 +389,17 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
|
||||||
);
|
);
|
||||||
foreach ( $values as $value ) {
|
foreach ( $values as $value ) {
|
||||||
if ( is_array( $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 ) ) {
|
} elseif ( is_email( $value ) ) {
|
||||||
$meta_query['customer_emails']['value'][] = sanitize_email( $value );
|
$meta_query['customer_emails']['value'][] = sanitize_email( $value );
|
||||||
} else {
|
} elseif ( is_numeric( $value ) ) {
|
||||||
$meta_query['customer_ids']['value'][] = strval( absint( $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'] ) {
|
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'] );
|
$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'] ) {
|
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 ) {
|
public function query( $query_vars ) {
|
||||||
$args = $this->get_wp_query_args( $query_vars );
|
$args = $this->get_wp_query_args( $query_vars );
|
||||||
|
|
||||||
|
if ( ! empty( $args['errors'] ) ) {
|
||||||
|
$query = (object) array(
|
||||||
|
'posts' => array(),
|
||||||
|
'found_posts' => 0,
|
||||||
|
'max_num_pages' => 0,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
$query = new WP_Query( $args );
|
$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 ) );
|
$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'] ) {
|
if ( isset( $query_vars['paginate'] ) && $query_vars['paginate'] ) {
|
||||||
|
|
|
@ -80,6 +80,27 @@ class WC_Tests_Customer_Functions extends WC_Unit_Test_Case {
|
||||||
$this->assertEquals( 0, $linked );
|
$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.
|
* Test wc_paying_customer.
|
||||||
*
|
*
|
||||||
|
|
|
@ -650,6 +650,18 @@ class WC_Tests_Order_Functions extends WC_Unit_Test_Case {
|
||||||
$orders = wc_get_orders( array( 'customer' => $customer2->get_id(), 'return' => 'ids' ) );
|
$orders = wc_get_orders( array( 'customer' => $customer2->get_id(), 'return' => 'ids' ) );
|
||||||
$expected = array( $order2->get_id() );
|
$expected = array( $order2->get_id() );
|
||||||
$this->assertEquals( $expected, $orders );
|
$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' => array( '' ) ) );
|
||||||
|
$this->assertEmpty( $orders );
|
||||||
|
|
||||||
|
$orders = wc_get_orders( array( 'customer' => 'doesnt@exist.com' ) );
|
||||||
|
$this->assertEmpty( $orders );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue