Skip to content

Commit

Permalink
sorting fields added to cpt connections`
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Apr 23, 2019
1 parent f6ec784 commit 58fb78e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 160 deletions.
35 changes: 0 additions & 35 deletions src/data/connection/class-coupon-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
35 changes: 0 additions & 35 deletions src/data/connection/class-order-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
35 changes: 0 additions & 35 deletions src/data/connection/class-product-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
Expand Down
35 changes: 0 additions & 35 deletions src/data/connection/class-refund-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
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 58fb78e

Please sign in to comment.