Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination fix for CPT-backed CRUD objects connections #36

Merged
merged 12 commits into from
Apr 24, 2019
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
12 changes: 8 additions & 4 deletions src/connection/class-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,19 @@ public static function get_connection_args() {
return array_merge(
self::get_shared_connection_args(),
array(
'statuses' => array(
'type' => array( 'list_of' => 'String' ),
'statuses' => array(
'type' => array( 'list_of' => 'OrderStatusEnum' ),
'description' => __( 'Limit result set to orders assigned a specific status.', 'wp-graphql-woocommerce' ),
),
'customerId' => array(
'customerId' => array(
'type' => 'Int',
'description' => __( 'Limit result set to orders assigned a specific customer.', 'wp-graphql-woocommerce' ),
),
'productId' => array(
'customersIn' => array(
'type' => array( 'list_of' => 'Int' ),
'description' => __( 'Limit result set to orders assigned a specific group of customers.', 'wp-graphql-woocommerce' ),
),
'productId' => array(
'type' => 'Int',
'description' => __( 'Limit result set to orders assigned a specific product.', 'wp-graphql-woocommerce' ),
),
Expand Down
16 changes: 8 additions & 8 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 All @@ -248,7 +248,7 @@ public static function get_connection_args() {

if ( wc_tax_enabled() ) {
$args['taxClass'] = array(
'type' => 'String',
'type' => 'TaxClassEnum',
'description' => __( 'Limit result set to products with a specific tax class.', 'wp-graphql-woocommerce' ),
);
}
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
81 changes: 50 additions & 31 deletions src/data/connection/class-order-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,36 @@ public function get_query_args() {

// Set the $query_args based on various defaults and primary input $args.
$query_args = array(
'post_type' => 'shop_order',
'no_rows_found' => true,
'fields' => 'ids',
'posts_per_page' => min( max( absint( $first ), absint( $last ), 10 ), $this->query_amount ) + 1,
'post_type' => 'shop_order',
'no_rows_found' => true,
'return' => 'ids',
'limit' => min( max( absint( $first ), absint( $last ), 10 ), $this->query_amount ) + 1,
);

/**
* 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
*/
$input_fields = [];
$input_fields = array();
if ( ! empty( $this->args['where'] ) ) {
$input_fields = $this->sanitize_input_fields( $this->args['where'] );
}
Expand All @@ -94,10 +114,6 @@ public function get_query_args() {
$query_args = array_merge( $query_args, $input_fields );
}

if ( empty( $query_args['post_status'] ) ) {
$query_args['post_status'] = 'any';
}

if ( true === is_object( $this->source ) ) {
switch ( true ) {
case is_a( $this->source, Customer::class ):
Expand Down Expand Up @@ -139,10 +155,10 @@ public function get_query_args() {
/**
* Executes query
*
* @return \WP_Query
* @return \WC_Order_Query
*/
public function get_query() {
return new \WP_Query( $this->get_query_args() );
return new \WC_Order_Query( $this->get_query_args() );
}

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

/**
Expand All @@ -168,30 +184,33 @@ public function sanitize_input_fields( array $where_args ) {
global $wpdb;
$args = $this->sanitize_shared_input_fields( $where_args );

$key_mapping = array(
'post_parent' => 'parent',
'post_parent__not_in' => 'parent_exclude',
'post__not_in' => 'exclude',
);

foreach ( $key_mapping as $key => $field ) {
if ( isset( $args[ $key ] ) ) {
$args[ $field ] = $args[ $key ];
unset( $args[ $key ] );
}
}

if ( ! empty( $where_args['statuses'] ) ) {
$args['post_status'] = array();
foreach ( $where_args['statuses'] as $status ) {
if ( in_array( $status, wc_graphql_get_order_statuses(), true ) ) {
$args['post_status'][] = 'wc-' . $status;
} elseif ( 'any' === $status ) {
// Set status to "any" and short-circuit out.
$args['post_status'] = 'any';
break;
} else {
$args['post_status'][] = $status;
}
if ( 1 === count( $where_args ) ) {
$args['status'] = $where_args['statuses'][0];
} else {
$args['status'] = $where_args['statuses'];
}
}

if ( ! empty( $where_args['customerId'] ) ) {
if ( ! empty( $args['meta_query'] ) ) {
$args['meta_query'] = array(); // WPCS: slow query ok.
}
$args['meta_query'][] = array(
'key' => '_customer_user',
'value' => $where_args['customerId'],
'type' => 'NUMERIC',
);
$args['customer_id'] = $where_args['customerId'];
}

if ( ! empty( $where_args['customersIn'] ) ) {
$args['customer'] = $where_args['customersIn'];
}

// Search by product.
Expand Down
Loading