From 58fb78ef5ce205de2eb576eefe649f2d996e2809 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Tue, 23 Apr 2019 00:48:45 -0400 Subject: [PATCH] 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' ), + ), ), ) );