From f6ec784447a226fe989f54ec8dbc764d9cb195b1 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Mon, 22 Apr 2019 19:52:05 -0400 Subject: [PATCH 1/2] initial commit --- .../class-coupon-connection-resolver.php | 44 ++++++++++++++ .../class-order-connection-resolver.php | 43 +++++++++++++ .../class-product-connection-resolver.php | 60 ++++++++++++++++--- .../class-refund-connection-resolver.php | 51 ++++++++++++++++ 4 files changed, 190 insertions(+), 8 deletions(-) diff --git a/src/data/connection/class-coupon-connection-resolver.php b/src/data/connection/class-coupon-connection-resolver.php index 5d391e263..ec5e94077 100644 --- a/src/data/connection/class-coupon-connection-resolver.php +++ b/src/data/connection/class-coupon-connection-resolver.php @@ -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, ); /** @@ -96,6 +97,49 @@ 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 ) ) ? '>' : '<'; + + /** + * Map the orderby inputArgs to the WP_Query + */ + if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) { + $query_args['orderby'] = array(); + foreach ( $this->args['where']['orderby'] as $orderby_input ) { + /** + * These orderby options should not include the order parameter. + */ + if ( in_array( + $orderby_input['field'], + array( 'post__in', 'post_name__in', 'post_parent__in' ), + true + ) ) { + $query_args['orderby'] = esc_sql( $orderby_input['field'] ); + } elseif ( ! empty( $orderby_input['field'] ) ) { + $query_args['orderby'] = array( + esc_sql( $orderby_input['field'] ) => esc_sql( $orderby_input['order'] ), + ); + } + } + } + + /** + * 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( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) { + $query_args['orderby'] = array( + 'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'], // WPCS: slow query ok. + ); + unset( $query_args['order'] ); + $query_args['meta_type'] = 'NUMERIC'; + } + /** * If there's no orderby params in the inputArgs, set order based on the first/last argument */ diff --git a/src/data/connection/class-order-connection-resolver.php b/src/data/connection/class-order-connection-resolver.php index 40350bf24..15291b0e7 100644 --- a/src/data/connection/class-order-connection-resolver.php +++ b/src/data/connection/class-order-connection-resolver.php @@ -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'; } @@ -115,6 +123,41 @@ public function get_query_args() { } } + /** + * Map the orderby inputArgs to the WP_Query + */ + if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) { + $query_args['orderby'] = array(); + foreach ( $this->args['where']['orderby'] as $orderby_input ) { + /** + * These orderby options should not include the order parameter. + */ + if ( in_array( + $orderby_input['field'], + array( 'post__in', 'post_name__in', 'post_parent__in' ), + true + ) ) { + $query_args['orderby'] = esc_sql( $orderby_input['field'] ); + } elseif ( ! empty( $orderby_input['field'] ) ) { + $query_args['orderby'] = array( + esc_sql( $orderby_input['field'] ) => esc_sql( $orderby_input['order'] ), + ); + } + } + } + + /** + * 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( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) { + $query_args['orderby'] = array( + 'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'], // WPCS: slow query ok. + ); + unset( $query_args['order'] ); + $query_args['meta_type'] = 'NUMERIC'; + } + /** * If there's no orderby params in the inputArgs, set order based on the first/last argument */ diff --git a/src/data/connection/class-product-connection-resolver.php b/src/data/connection/class-product-connection-resolver.php index f03a2d505..9099ec5d5 100644 --- a/src/data/connection/class-product-connection-resolver.php +++ b/src/data/connection/class-product-connection-resolver.php @@ -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, ); /** @@ -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 ) { @@ -151,6 +160,41 @@ public function get_query_args() { } } + /** + * Map the orderby inputArgs to the WP_Query + */ + if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) { + $query_args['orderby'] = array(); + foreach ( $this->args['where']['orderby'] as $orderby_input ) { + /** + * These orderby options should not include the order parameter. + */ + if ( in_array( + $orderby_input['field'], + array( 'post__in', 'post_name__in', 'post_parent__in' ), + true + ) ) { + $query_args['orderby'] = esc_sql( $orderby_input['field'] ); + } elseif ( ! empty( $orderby_input['field'] ) ) { + $query_args['orderby'] = array( + esc_sql( $orderby_input['field'] ) => esc_sql( $orderby_input['order'] ), + ); + } + } + } + + /** + * 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( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) { + $query_args['orderby'] = array( + 'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'], // WPCS: slow query ok. + ); + unset( $query_args['order'] ); + $query_args['meta_type'] = 'NUMERIC'; + } + if ( isset( $query_args['post__in'] ) && empty( $query_args['post__in'] ) ) { $query_args['post__in'] = array( '0' ); } @@ -200,7 +244,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(); } /** diff --git a/src/data/connection/class-refund-connection-resolver.php b/src/data/connection/class-refund-connection-resolver.php index ee6abaeb8..050ae5100 100644 --- a/src/data/connection/class-refund-connection-resolver.php +++ b/src/data/connection/class-refund-connection-resolver.php @@ -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'; } @@ -122,6 +138,41 @@ public function get_query_args() { break; } + /** + * Map the orderby inputArgs to the WP_Query + */ + if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) { + $query_args['orderby'] = array(); + foreach ( $this->args['where']['orderby'] as $orderby_input ) { + /** + * These orderby options should not include the order parameter. + */ + if ( in_array( + $orderby_input['field'], + array( 'post__in', 'post_name__in', 'post_parent__in' ), + true + ) ) { + $query_args['orderby'] = esc_sql( $orderby_input['field'] ); + } elseif ( ! empty( $orderby_input['field'] ) ) { + $query_args['orderby'] = array( + esc_sql( $orderby_input['field'] ) => esc_sql( $orderby_input['order'] ), + ); + } + } + } + + /** + * 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( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) { + $query_args['orderby'] = array( + 'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'], // WPCS: slow query ok. + ); + unset( $query_args['order'] ); + $query_args['meta_type'] = 'NUMERIC'; + } + /** * If there's no orderby params in the inputArgs, set order based on the first/last argument */ From 58fb78ef5ce205de2eb576eefe649f2d996e2809 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Tue, 23 Apr 2019 00:48:45 -0400 Subject: [PATCH 2/2] sorting fields added to cpt connections` --- .../class-coupon-connection-resolver.php | 35 ------------------- .../class-order-connection-resolver.php | 35 ------------------- .../class-product-connection-resolver.php | 35 ------------------- .../class-refund-connection-resolver.php | 35 ------------------- .../trait-wc-connection-resolver.php | 21 +++++------ .../enum/class-wc-connection-orderby-enum.php | 26 ++++++++++---- 6 files changed, 27 insertions(+), 160 deletions(-) diff --git a/src/data/connection/class-coupon-connection-resolver.php b/src/data/connection/class-coupon-connection-resolver.php index ec5e94077..4525c127a 100644 --- a/src/data/connection/class-coupon-connection-resolver.php +++ b/src/data/connection/class-coupon-connection-resolver.php @@ -105,41 +105,6 @@ public function get_query_args() { $query_args['graphql_cursor_offset'] = $cursor_offset; $query_args['graphql_cursor_compare'] = ( ! empty( $last ) ) ? '>' : '<'; - /** - * Map the orderby inputArgs to the WP_Query - */ - if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) { - $query_args['orderby'] = array(); - foreach ( $this->args['where']['orderby'] as $orderby_input ) { - /** - * These orderby options should not include the order parameter. - */ - if ( in_array( - $orderby_input['field'], - array( 'post__in', 'post_name__in', 'post_parent__in' ), - true - ) ) { - $query_args['orderby'] = esc_sql( $orderby_input['field'] ); - } elseif ( ! empty( $orderby_input['field'] ) ) { - $query_args['orderby'] = array( - esc_sql( $orderby_input['field'] ) => esc_sql( $orderby_input['order'] ), - ); - } - } - } - - /** - * 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( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) { - $query_args['orderby'] = array( - 'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'], // WPCS: slow query ok. - ); - unset( $query_args['order'] ); - $query_args['meta_type'] = 'NUMERIC'; - } - /** * If there's no orderby params in the inputArgs, set order based on the first/last argument */ diff --git a/src/data/connection/class-order-connection-resolver.php b/src/data/connection/class-order-connection-resolver.php index 15291b0e7..6e0bf88c9 100644 --- a/src/data/connection/class-order-connection-resolver.php +++ b/src/data/connection/class-order-connection-resolver.php @@ -123,41 +123,6 @@ public function get_query_args() { } } - /** - * Map the orderby inputArgs to the WP_Query - */ - if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) { - $query_args['orderby'] = array(); - foreach ( $this->args['where']['orderby'] as $orderby_input ) { - /** - * These orderby options should not include the order parameter. - */ - if ( in_array( - $orderby_input['field'], - array( 'post__in', 'post_name__in', 'post_parent__in' ), - true - ) ) { - $query_args['orderby'] = esc_sql( $orderby_input['field'] ); - } elseif ( ! empty( $orderby_input['field'] ) ) { - $query_args['orderby'] = array( - esc_sql( $orderby_input['field'] ) => esc_sql( $orderby_input['order'] ), - ); - } - } - } - - /** - * 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( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) { - $query_args['orderby'] = array( - 'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'], // WPCS: slow query ok. - ); - unset( $query_args['order'] ); - $query_args['meta_type'] = 'NUMERIC'; - } - /** * If there's no orderby params in the inputArgs, set order based on the first/last argument */ diff --git a/src/data/connection/class-product-connection-resolver.php b/src/data/connection/class-product-connection-resolver.php index 9099ec5d5..d7c7b0ef7 100644 --- a/src/data/connection/class-product-connection-resolver.php +++ b/src/data/connection/class-product-connection-resolver.php @@ -160,41 +160,6 @@ public function get_query_args() { } } - /** - * Map the orderby inputArgs to the WP_Query - */ - if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) { - $query_args['orderby'] = array(); - foreach ( $this->args['where']['orderby'] as $orderby_input ) { - /** - * These orderby options should not include the order parameter. - */ - if ( in_array( - $orderby_input['field'], - array( 'post__in', 'post_name__in', 'post_parent__in' ), - true - ) ) { - $query_args['orderby'] = esc_sql( $orderby_input['field'] ); - } elseif ( ! empty( $orderby_input['field'] ) ) { - $query_args['orderby'] = array( - esc_sql( $orderby_input['field'] ) => esc_sql( $orderby_input['order'] ), - ); - } - } - } - - /** - * 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( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) { - $query_args['orderby'] = array( - 'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'], // WPCS: slow query ok. - ); - unset( $query_args['order'] ); - $query_args['meta_type'] = 'NUMERIC'; - } - if ( isset( $query_args['post__in'] ) && empty( $query_args['post__in'] ) ) { $query_args['post__in'] = array( '0' ); } diff --git a/src/data/connection/class-refund-connection-resolver.php b/src/data/connection/class-refund-connection-resolver.php index 050ae5100..47b4cdaf1 100644 --- a/src/data/connection/class-refund-connection-resolver.php +++ b/src/data/connection/class-refund-connection-resolver.php @@ -138,41 +138,6 @@ public function get_query_args() { break; } - /** - * Map the orderby inputArgs to the WP_Query - */ - if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) { - $query_args['orderby'] = array(); - foreach ( $this->args['where']['orderby'] as $orderby_input ) { - /** - * These orderby options should not include the order parameter. - */ - if ( in_array( - $orderby_input['field'], - array( 'post__in', 'post_name__in', 'post_parent__in' ), - true - ) ) { - $query_args['orderby'] = esc_sql( $orderby_input['field'] ); - } elseif ( ! empty( $orderby_input['field'] ) ) { - $query_args['orderby'] = array( - esc_sql( $orderby_input['field'] ) => esc_sql( $orderby_input['order'] ), - ); - } - } - } - - /** - * 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( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) { - $query_args['orderby'] = array( - 'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'], // WPCS: slow query ok. - ); - unset( $query_args['order'] ); - $query_args['meta_type'] = 'NUMERIC'; - } - /** * If there's no orderby params in the inputArgs, set order based on the first/last argument */ diff --git a/src/data/connection/trait-wc-connection-resolver.php b/src/data/connection/trait-wc-connection-resolver.php index 7c892a6ab..809beb4a6 100644 --- a/src/data/connection/trait-wc-connection-resolver.php +++ b/src/data/connection/trait-wc-connection-resolver.php @@ -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. @@ -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'] ), @@ -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']; } diff --git a/src/type/enum/class-wc-connection-orderby-enum.php b/src/type/enum/class-wc-connection-orderby-enum.php index 53fd97900..af0c24de0 100644 --- a/src/type/enum/class-wc-connection-orderby-enum.php +++ b/src/type/enum/class-wc-connection-orderby-enum.php @@ -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' ), + ), ), ) );