Skip to content

Commit

Permalink
Merge 3a1ca6c into 49a9df7
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Apr 24, 2019
2 parents 49a9df7 + 3a1ca6c commit 8596983
Show file tree
Hide file tree
Showing 24 changed files with 374 additions and 3,071 deletions.
2 changes: 2 additions & 0 deletions src/class-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Discount_Type;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Manage_Stock;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Order_Status;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Product_Types;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Stock_Status;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Tax_Class;
use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Tax_Status;
Expand Down Expand Up @@ -76,6 +77,7 @@ public static function graphql_register_types() {
Discount_Type::register();
Manage_Stock::register();
Order_Status::register();
Product_Types::register();
Stock_Status::register();
Tax_Class::register();
Tax_Status::register();
Expand Down
14 changes: 7 additions & 7 deletions src/connection/class-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ public static function get_connection_args() {
'description' => __( 'Limit result set to products assigned a specific status.', 'wp-graphql-woocommerce' ),
),
'type' => array(
'type' => 'String',
'type' => 'ProductTypesEnum',
'description' => __( 'Limit result set to products assigned a specific type.', 'wp-graphql-woocommerce' ),
),
'typeIn' => array(
'type' => array( 'list_of' => 'String' ),
'type' => array( 'list_of' => 'ProductTypesEnum' ),
'description' => __( 'Limit result set to products assigned to a group of specific types.', 'wp-graphql-woocommerce' ),
),
'typeNotIn' => array(
'type' => array( 'list_of' => 'String' ),
'type' => array( 'list_of' => 'ProductTypesEnum' ),
'description' => __( 'Limit result set to products not assigned to a group of specific types.', 'wp-graphql-woocommerce' ),
),
'sku' => array(
Expand Down Expand Up @@ -224,20 +224,20 @@ public static function get_connection_args() {
'type' => 'String',
'description' => __( 'Limit result set to products with a specific attribute term ID (required an assigned attribute).', 'wp-graphql-woocommerce' ),
),
'inStock' => array(
'type' => 'Boolean',
'stockStatus' => array(
'type' => 'StockStatusEnum',
'description' => __( 'Limit result set to products in stock or out of stock.', 'wp-graphql-woocommerce' ),
),
'onSale' => array(
'type' => 'Boolean',
'description' => __( 'Limit result set to products on sale.', 'wp-graphql-woocommerce' ),
),
'minPrice' => array(
'type' => 'String',
'type' => 'Float',
'description' => __( 'Limit result set to products based on a minimum price.', 'wp-graphql-woocommerce' ),
),
'maxPrice' => array(
'type' => 'String',
'type' => 'Float',
'description' => __( 'Limit result set to products based on a maximum price.', 'wp-graphql-woocommerce' ),
),
'search' => array(
Expand Down
21 changes: 21 additions & 0 deletions src/data/connection/class-coupon-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,29 @@ public function get_query_args() {
'no_rows_found' => true,
'fields' => 'ids',
'posts_per_page' => min( max( absint( $first ), absint( $last ), 10 ), $this->query_amount ) + 1,
'post_parent' => 0,
);

/**
* Set the graphql_cursor_offset which is used by Config::graphql_wp_query_cursor_pagination_support
* to filter the WP_Query to support cursor pagination
*/
$cursor_offset = $this->get_offset();
$query_args['graphql_cursor_offset'] = $cursor_offset;
$query_args['graphql_cursor_compare'] = ( ! empty( $last ) ) ? '>' : '<';

/**
* If the starting offset is not 0 sticky posts will not be queried as the automatic checks in wp-query don't
* trigger due to the page parameter not being set in the query_vars, fixes #732
*/
if ( 0 !== $cursor_offset ) {
$query_args['ignore_sticky_posts'] = true;
}
/**
* Pass the graphql $args to the WP_Query
*/
$query_args['graphql_args'] = $this->args;

/**
* Collect the input_fields and sanitize them to prepare them for sending to the WP_Query
*/
Expand Down
8 changes: 8 additions & 0 deletions src/data/connection/class-order-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public function get_query_args() {
$query_args = array_merge( $query_args, $input_fields );
}

/**
* Set the graphql_cursor_offset which is used by Config::graphql_wp_query_cursor_pagination_support
* to filter the WP_Query to support cursor pagination
*/
$cursor_offset = $this->get_offset();
$query_args['graphql_cursor_offset'] = $cursor_offset;
$query_args['graphql_cursor_compare'] = ( ! empty( $last ) ) ? '>' : '<';

if ( empty( $query_args['post_status'] ) ) {
$query_args['post_status'] = 'any';
}
Expand Down
109 changes: 71 additions & 38 deletions src/data/connection/class-product-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,49 @@ public function __construct( $source, $args, $context, $info ) {
* Set the post type for the resolver
*/
$this->post_type = 'product';

add_filter(
'woocommerce_product_data_store_cpt_get_products_query',
array( &$this, 'product_query_filter' ),
10,
2
);
/**
* Call the parent construct to setup class data
*/
parent::__construct( $source, $args, $context, $info );
}

/**
* Applies price meta_query args to product query args
*
* @param array $wp_query_args - Formatted query args.
* @param array $query_vars - Raw query args.
*
* @return array
*/
public function product_query_filter( $wp_query_args, $query_vars ) {
if ( empty( $query_vars['meta_query'] ) ) {
return $wp_query_args;
}

$price_meta_query = array_filter(
$query_vars['meta_query'],
function ( $query ) {
return ! empty( $query['key'] ) ? '_price' === $query['key'] : false;
}
);

if ( ! empty( $price_meta_query ) ) {
$wp_query_args['meta_query'] = array_merge( // WPCS: slow query ok.
$wp_query_args['meta_query'],
$price_meta_query
);
}

return $wp_query_args;
}

/**
* Confirms the uses has the privileges to query Products
*
Expand All @@ -73,13 +110,13 @@ public function get_query_args() {
// Set the $query_args based on various defaults and primary input $args.
$post_type_obj = get_post_type_object( 'product' );
$query_args = array(
'post_type' => 'product',
'post_parent' => 0,
'post_status' => current_user_can( $post_type_obj->cap->edit_posts ) ? 'any' : 'publish',
'perm' => 'readable',
'no_rows_found' => true,
'fields' => 'ids',
'posts_per_page' => min( max( absint( $first ), absint( $last ), 10 ), $this->query_amount ) + 1,
'post_parent' => 0,
'status' => current_user_can( $post_type_obj->cap->edit_posts ) ? 'any' : 'publish',
'perm' => 'readable',
'no_rows_found' => true,
'return' => 'ids',
'limit' => min( max( absint( $first ), absint( $last ), 10 ), $this->query_amount ) + 1,
'ignore_sticky_posts' => true,
);

/**
Expand All @@ -94,6 +131,14 @@ public function get_query_args() {
$query_args = array_merge( $query_args, $input_fields );
}

/**
* Set the graphql_cursor_offset which is used by Config::graphql_wp_query_cursor_pagination_support
* to filter the WP_Query to support cursor pagination
*/
$cursor_offset = $this->get_offset();
$query_args['graphql_cursor_offset'] = $cursor_offset;
$query_args['graphql_cursor_compare'] = ( ! empty( $last ) ) ? '>' : '<';

// Determine where we're at in the Graph and adjust the query context appropriately.
if ( true === is_object( $this->source ) ) {
switch ( true ) {
Expand Down Expand Up @@ -134,7 +179,7 @@ public function get_query_args() {
$query_args['post__in'] = isset( $query_args['post__in'] )
? array_intersect( $this->source->variation_ids, $query_args['post__in'] )
: $this->source->variation_ids;
$query_args['post_type'] = 'product_variation';
$query_args['type'] = 'variation';
}
break;

Expand All @@ -151,6 +196,11 @@ public function get_query_args() {
}
}

/**
* Pass the graphql $args to the WP_Query
*/
$query_args['graphql_args'] = $this->args;

if ( isset( $query_args['post__in'] ) && empty( $query_args['post__in'] ) ) {
$query_args['post__in'] = array( '0' );
}
Expand Down Expand Up @@ -191,7 +241,7 @@ public function get_query_args() {
* @return \WP_Query
*/
public function get_query() {
return new \WP_Query( $this->get_query_args() );
return new \WC_Product_Query( $this->get_query_args() );
}

/**
Expand All @@ -200,7 +250,7 @@ public function get_query() {
* @return array
*/
public function get_items() {
return ! empty( $this->query->posts ) ? $this->query->posts : [];
return ! empty( $this->query->get_products() ) ? $this->query->get_products() : array();
}

/**
Expand All @@ -221,7 +271,7 @@ public function sanitize_input_fields( array $where_args ) {
}

if ( ! empty( $where_args['status'] ) ) {
$args['post_status'] = $where_args['status'];
$args['status'] = $where_args['status'];
}

if ( ! empty( $where_args['search'] ) ) {
Expand Down Expand Up @@ -322,37 +372,23 @@ public function sanitize_input_fields( array $where_args ) {
);
}

if ( ! empty( $where_args['featured'] ) && is_bool( $where_args['featured'] ) ) {
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => true === $where_args['featured'] ? 'IN' : 'NOT IN',
);
}

if ( ! empty( $tax_query ) && 1 > count( $tax_query ) ) {
$tax_query['relation'] = 'AND';
}

if ( isset( $where_args['featured'] ) ) {
$args['featured'] = $where_args['featured'];
}

if ( ! empty( $tax_query ) ) {
$args['tax_query'] = $tax_query; // WPCS: slow query ok.
}

$meta_query = array();
if ( ! empty( $where_args['sku'] ) ) {
$skus = explode( ',', $where_args['sku'] );
if ( 1 < count( $skus ) ) {
$skus[] = $where_args['sku'];
}

$meta_query[] = array(
'key' => '_sku',
'value' => $skus,
'compare' => 'IN',
);
$args['sku'] = $where_args['sku'];
}

$meta_query = array();
if ( ! empty( $where_args['minPrice'] ) || ! empty( $where_args['maxPrice'] ) ) {
$current_min_price = isset( $where_args['minPrice'] )
? floatval( $where_args['minPrice'] )
Expand All @@ -373,17 +409,14 @@ public function sanitize_input_fields( array $where_args ) {
);
}

if ( ! empty( $where_args['inStock'] ) && is_bool( $where_args['inStock'] ) ) {
$meta_query[] = array(
'key' => '_stock_status',
'value' => true === $where_args['inStock'] ? 'instock' : 'outofstock',
);
}

if ( ! empty( $meta_query ) ) {
$args['meta_query'] = $meta_query; // WPCS: slow query ok.
}

if ( isset( $where_args['stockStatus'] ) ) {
$args['stock_status'] = $where_args['stockStatus'];
}

if ( ! empty( $where_args['onSale'] ) && is_bool( $where_args['onSale'] ) ) {
$on_sale_key = $where_args['onSale'] ? 'post__in' : 'post__not_in';
$on_sale_ids = \wc_get_product_ids_on_sale();
Expand Down
16 changes: 16 additions & 0 deletions src/data/connection/class-refund-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public function get_query_args() {
$query_args = array_merge( $query_args, $input_fields );
}

/**
* Set the graphql_cursor_offset which is used by Config::graphql_wp_query_cursor_pagination_support
* to filter the WP_Query to support cursor pagination
*/
$cursor_offset = $this->get_offset();
$query_args['graphql_cursor_offset'] = $cursor_offset;
$query_args['graphql_cursor_compare'] = ( ! empty( $last ) ) ? '>' : '<';

/**
* If the starting offset is not 0 sticky posts will not be queried as the automatic checks in wp-query don't
* trigger due to the page parameter not being set in the query_vars, fixes #732
*/
if ( 0 !== $cursor_offset ) {
$query_args['ignore_sticky_posts'] = true;
}

if ( empty( $query_args['post_status'] ) ) {
$query_args['post_status'] = 'any';
}
Expand Down
21 changes: 8 additions & 13 deletions src/data/connection/trait-wc-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function sanitize_input_fields( array $where_args ) {
* Map the orderby inputArgs to the WP_Query
*/
if ( ! empty( $where_args['orderby'] ) && is_array( $where_args['orderby'] ) ) {
$args['orderby'] = [];
$args['orderby'] = array();
foreach ( $where_args['orderby'] as $orderby_input ) {
/**
* These orderby options should not include the order parameter.
Expand All @@ -69,6 +69,13 @@ public function sanitize_input_fields( array $where_args ) {
true
) ) {
$args['orderby'] = esc_sql( $orderby_input['field'] );
} elseif ( in_array(
$orderby_input['field'],
array( '_price', '_regular_price', '_sale_price' ),
true
) ) {
$args['orderby'] = array( 'meta_value_num' => $orderby_input['order'] );
$args['meta_key'] = esc_sql( $orderby_input['field'] ); // WPCS: slow query ok.
} elseif ( ! empty( $orderby_input['field'] ) ) {
$args['orderby'] = array(
esc_sql( $orderby_input['field'] ) => esc_sql( $orderby_input['order'] ),
Expand All @@ -77,18 +84,6 @@ public function sanitize_input_fields( array $where_args ) {
}
}

/**
* Convert meta_value_num to seperate meta_value value field which our
* graphql_wp_term_query_cursor_pagination_support knowns how to handle
*/
if ( isset( $args['orderby'] ) && 'meta_value_num' === $args['orderby'] ) {
$args['orderby'] = array(
'meta_value' => empty( $args['order'] ) ? 'DESC' : $args['order'], // WPCS: slow query ok.
);
unset( $args['order'] );
$args['meta_type'] = 'NUMERIC';
}

if ( ! empty( $where_args['dateQuery'] ) ) {
$args['date_query'] = $where_args['dateQuery'];
}
Expand Down
3 changes: 3 additions & 0 deletions src/model/class-product-variation.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ protected function init() {
'regularPrice' => function() {
return ! empty( $this->data->get_regular_price() ) ? $this->data->get_regular_price() : null;
},
'type' => function() {
return ! empty( $this->data->get_type() ) ? $this->data->get_type() : null;
},
/**
* Connection resolvers fields
*
Expand Down

0 comments on commit 8596983

Please sign in to comment.