Skip to content

Commit

Permalink
Merge 58fb78e into 49a9df7
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Apr 23, 2019
2 parents 49a9df7 + 58fb78e commit 635642d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 28 deletions.
9 changes: 9 additions & 0 deletions src/data/connection/class-coupon-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ 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,
);

/**
Expand All @@ -96,6 +97,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 there's no orderby params in the inputArgs, set order based on the first/last argument
*/
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
25 changes: 17 additions & 8 deletions src/data/connection/class-product-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ 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_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,
'ignore_sticky_posts' => true,
);

/**
Expand All @@ -94,6 +95,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 @@ -200,7 +209,7 @@ public function get_query() {
* @return array
*/
public function get_items() {
return ! empty( $this->query->posts ) ? $this->query->posts : [];
return ! empty( $this->query->posts ) ? $this->query->posts : array();
}

/**
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
26 changes: 19 additions & 7 deletions src/type/enum/class-wc-connection-orderby-enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,46 @@ public static function register() {
array(
'description' => __( 'Field to order the connection by', 'wp-graphql-woocommerce' ),
'values' => array(
'SLUG' => array(
'SLUG' => array(
'value' => 'post_name',
'description' => __( 'Order by slug', 'wp-graphql-woocommerce' ),
),
'MODIFIED' => array(
'MODIFIED' => array(
'value' => 'post_modified',
'description' => __( 'Order by last modified date', 'wp-graphql-woocommerce' ),
),
'DATE' => array(
'DATE' => array(
'value' => 'post_date',
'description' => __( 'Order by publish date', 'wp-graphql-woocommerce' ),
),
'PARENT' => array(
'PARENT' => array(
'value' => 'post_parent',
'description' => __( 'Order by parent ID', 'wp-graphql-woocommerce' ),
),
'IN' => array(
'IN' => array(
'value' => 'post__in',
'description' => __( 'Preserve the ID order given in the IN array', 'wp-graphql-woocommerce' ),
),
'NAME_IN' => array(
'NAME_IN' => array(
'value' => 'post_name__in',
'description' => __( 'Preserve slug order given in the NAME_IN array', 'wp-graphql-woocommerce' ),
),
'MENU_ORDER' => array(
'MENU_ORDER' => array(
'value' => 'menu_order',
'description' => __( 'Order by the menu order value', 'wp-graphql-woocommerce' ),
),
'PRICE' => array(
'value' => '_price',
'description' => __( 'Order by the menu order value', 'wp-graphql-woocommerce' ),
),
'REGULAR_PRICE' => array(
'value' => '_regular_price',
'description' => __( 'Order by the menu order value', 'wp-graphql-woocommerce' ),
),
'SALE_PRICE' => array(
'value' => '_sale_price',
'description' => __( 'Order by the menu order value', 'wp-graphql-woocommerce' ),
),
),
)
);
Expand Down

0 comments on commit 635642d

Please sign in to comment.