Offload more to subclasses

This commit is contained in:
claudiulodro 2017-04-27 14:09:10 -07:00
parent db3de7375d
commit 4eb1396e85
4 changed files with 138 additions and 71 deletions

View File

@ -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',
);
}

View File

@ -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 );
}
}

View File

@ -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;
}

View File

@ -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 );
}
}