From 4eb1396e8598ae7462fd0e9e4814bc5e3e4c640b Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Thu, 27 Apr 2017 14:09:10 -0700 Subject: [PATCH] Offload more to subclasses --- .../abstracts/abstract-wc-object-query.php | 18 ++-- includes/class-wc-order-query.php | 102 ++++++++++-------- .../data-stores/class-wc-data-store-wp.php | 18 +--- .../class-wc-order-data-store-cpt.php | 71 +++++++++++- 4 files changed, 138 insertions(+), 71 deletions(-) diff --git a/includes/abstracts/abstract-wc-object-query.php b/includes/abstracts/abstract-wc-object-query.php index 7f0ac8eba9b..0b1bbdf9722 100644 --- a/includes/abstracts/abstract-wc-object-query.php +++ b/includes/abstracts/abstract-wc-object-query.php @@ -29,6 +29,10 @@ abstract class WC_Object_Query { $this->query_vars = wp_parse_args( $args, $this->get_default_query_vars() ); } + public function get_query_vars() { + return $this->query_vars; + } + /** * Get the value of a query variable. * @param string $query_var Query variable to get value for. @@ -60,23 +64,17 @@ abstract class WC_Object_Query { return array( 'name' => '', 'parent' => '', - 'parent__in' => array(), - 'parent__not_in' => array(), - 'in' => array(), - 'not_in' => array(), + 'parent_exclude' => '', + 'exclude' => '', - 'status' => array( 'publish', 'pending', 'draft', 'future', 'private', 'inherit' ), - - 'per_page' => -1, + 'limit' => -1, 'page' => '', 'offset' => '', + 'paginate' => false, 'order' => 'DESC', 'orderby' => 'date', - 'date_before' => '', - 'date_after' => '', - 'return' => 'objects', ); } diff --git a/includes/class-wc-order-query.php b/includes/class-wc-order-query.php index d5d88d33763..dd9365f8b3f 100644 --- a/includes/class-wc-order-query.php +++ b/includes/class-wc-order-query.php @@ -21,51 +21,59 @@ class WC_Order_Query extends WC_Object_Query { return array_merge( parent::get_default_query_vars(), array( - 'status' => array_keys( wc_get_order_statuses() ), - 'type' => wc_get_order_types( 'view-orders' ), - 'currency' => '', - 'version' => '', - 'prices_include_tax' => '', - 'date_created' => '', - 'date_modified' => '', - 'discount_total' => '', - 'discount_tax' => '', - 'shipping_total' => '', - 'shipping_tax' => '', - 'cart_tax' => '', - 'total' => '', - 'total_tax' => '', - 'customer_id' => '', - 'order_key' => '', - 'billing_first_name' => '', - 'billing_last_name' => '', - 'billing_company' => '', - 'billing_address_1' => '', - 'billing_address_2' => '', - 'billing_city' => '', - 'billing_state' => '', - 'billing_postcode' => '', - 'billing_country' => '', - 'billing_email' => '', - 'billing_phone' => '', - 'shipping_first_name' => '', - 'shipping_last_name' => '', - 'shipping_company' => '', - 'shipping_address_1' => '', - 'shipping_address_2' => '', - 'shipping_city' => '', - 'shipping_state' => '', - 'shipping_postcode' => '', - 'shipping_country' => '', - 'payment_method' => '', - 'payment_method_title' => '', - 'transaction_id' => '', - 'customer_ip_address' => '', - 'customer_user_agent' => '', - 'created_via' => '', - 'customer_note' => '', - 'date_completed' => '', - 'date_paid' => '', + 'status' => array_keys( wc_get_order_statuses() ), + 'type' => wc_get_order_types( 'view-orders' ), + 'currency' => '', + 'version' => '', + 'prices_include_tax' => '', + 'date_created' => '', + 'date_created_before' => '', + 'date_created_after' => '', + 'date_modified' => '', + 'date_modified_before' => '', + 'date_modified_after' => '', + 'date_completed' => '', + 'date_completed_before' => '', + 'date_completed_after' => '', + 'date_paid' => '', + 'date_paid_before' => '', + 'date_paid_after' => '', + 'discount_total' => '', + 'discount_tax' => '', + 'shipping_total' => '', + 'shipping_tax' => '', + 'cart_tax' => '', + 'total' => '', + 'total_tax' => '', + 'customer_id' => '', + 'order_key' => '', + 'billing_first_name' => '', + 'billing_last_name' => '', + 'billing_company' => '', + 'billing_address_1' => '', + 'billing_address_2' => '', + 'billing_city' => '', + 'billing_state' => '', + 'billing_postcode' => '', + 'billing_country' => '', + 'billing_email' => '', + 'billing_phone' => '', + 'shipping_first_name' => '', + 'shipping_last_name' => '', + 'shipping_company' => '', + 'shipping_address_1' => '', + 'shipping_address_2' => '', + 'shipping_city' => '', + 'shipping_state' => '', + 'shipping_postcode' => '', + 'shipping_country' => '', + 'payment_method' => '', + 'payment_method_title' => '', + 'transaction_id' => '', + 'customer_ip_address' => '', + 'customer_user_agent' => '', + 'created_via' => '', + 'customer_note' => '', ) ); } @@ -75,8 +83,8 @@ class WC_Order_Query extends WC_Object_Query { * @return array of WC_Order objects */ public function get_orders() { - $args = apply_filters( 'woocommerce_order_query_args', $this->query_vars ); - $results = WC_Data_Store::load( 'order' )->query( $this->query_vars ); + $args = apply_filters( 'woocommerce_order_query_args', $this->get_query_vars() ); + $results = WC_Data_Store::load( 'order' )->query( $this->get_query_vars() ); return apply_filters( 'woocommerce_order_query', $results, $args ); } } diff --git a/includes/data-stores/class-wc-data-store-wp.php b/includes/data-stores/class-wc-data-store-wp.php index 464970f4375..c28c81ceb53 100644 --- a/includes/data-stores/class-wc-data-store-wp.php +++ b/includes/data-stores/class-wc-data-store-wp.php @@ -205,8 +205,7 @@ class WC_Data_Store_WP { $skipped_values = array( '', array(), null ); $wp_query_args = array( - 'meta_query' => array(), - 'date_query' => array(), + 'meta_query' => array() ); foreach( $query_vars as $key => $value ) { @@ -221,26 +220,19 @@ class WC_Data_Store_WP { 'value' => $value, 'compare' => '=', ); - // Other vars get mapped to a 'post_*' or just left alone. + // Other vars get mapped to wp_query args or just left alone. } else { $key_mapping = array ( 'parent' => 'post_parent', - 'parent__in' => 'post_parent__in', - 'parent__not_in' => 'post_parent__not_in', - 'in' => 'post__in', - 'not_in' => 'post__not_in', - 'status' => 'post_status', - 'per_page' => 'posts_per_page', + 'parent_exclude' => 'post_parent__not_in', + 'exclude' => 'post__not_in', + 'limit' => 'posts_per_page', 'type' => 'post_type', 'return' => 'fields', ); if ( isset( $key_mapping[ $key ] ) ) { $wp_query_args[ $key_mapping[ $key ] ] = $value; - } elseif ( ! empty( $query_vars['date_before'] ) ) { - $wp_query_args['date_query']['before'] = $query_vars['date_before']; - } elseif ( ! empty( $query_vars['date_after'] ) ) { - $wp_query_args['date_query']['after'] = $query_vars['date_after']; } else { $wp_query_args[ $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 fa25c0250b6..7ac0cba8740 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,75 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement } } - return parent::get_wp_query_args( $query_vars ); + $wp_query_args = parent::get_wp_query_args( $query_vars ); + if ( ! isset( $wp_query_args['date_query' ) ) { + $wp_query_args['date_query'] = array(); + } + if ( ! isset( $wp_query_args['meta_query'] ) ) { + $wp_query_args['meta_query'] = array(); + } + + if ( isset( $query_vars[ 'date_created_before'] ) && '' !== $query_vars['date_created_before'] ) { + $wp_query_args['date_query'][] = array( + 'column' => 'post_date', + 'before' => $query_vars['date_created_before'] + ); + } + + if ( isset( $query_vars[ 'date_created_after'] ) && '' !== $query_vars[ 'date_created_after' ] ) { + $wp_query_args['date_query'][] = array( + 'column' => 'post_date', + 'after' => $query_vars['date_created_before'] + ); + } + + if ( isset( $query_vars[ 'date_modified_before'] ) && '' !== $query_vars[ 'date_modified_before' ] ) { + $wp_query_args['date_query'][] = array( + 'column' => 'post_modified', + 'after' => $query_vars['date_modified_before'] + ); + } + + if ( isset( $query_vars[ 'date_modified_after'] ) && '' !== $query_vars[ 'date_modified_after' ] ) { + $wp_query_args['date_query'][] = array( + 'column' => 'post_modified', + 'after' => $query_vars['date_modified_after'] + ); + } + + if ( isset( $query_vars[ 'date_completed_before' ] ) && '' !== $query_vars[ 'date_completed_before' ] && strtotime( $query_vars[ 'date_completed_before' ] ) ) { + $wp_query_args['meta_query'][] = array( + 'key' => '_date_completed', + 'value' => strtotime( $query_vars[ 'date_completed_before' ] ), + 'compare' => '<', + ); + } + + if ( isset( $query_vars[ 'date_completed_after' ] ) && '' !== $query_vars[ 'date_completed_after' ] && strtotime( $query_vars[ 'date_completed_after' ] ) ) { + $wp_query_args['meta_query'][] = array( + 'key' => '_date_completed', + 'value' => strtotime( $query_vars[ 'date_completed_after' ] ), + 'compare' => '>', + ); + } + + if ( isset( $query_vars[ 'date_paid_before' ] ) && '' !== $query_vars[ 'date_paid_before' ] && strtotime( $query_vars[ 'date_paid_before' ] ) ) { + $wp_query_args['meta_query'][] = array( + 'key' => '_date_paid', + 'value' => strtotime( $query_vars[ 'date_paid_before' ] ), + 'compare' => '<', + ); + } + + if ( isset( $query_vars[ 'date_paid_after' ] ) && '' !== $query_vars[ 'date_paid_after' ] && strtotime( $query_vars[ 'date_paid_after' ] ) ) { + $wp_query_args['meta_query'][] = array( + 'key' => '_date_paid', + 'value' => strtotime( $query_vars[ 'date_paid_after' ] ), + 'compare' => '>', + ); + } + + return apply_filters( 'woocommerce_get_order_wp_query_args', $wp_query_args, $query_vars ); } /** @@ -671,6 +739,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement if ( isset( $query_vars['return'] ) && 'ids' === $query_vars['return'] ) { return $query->posts; } + // paged? return array_map( 'wc_get_order', $query->posts ); } }