Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Apr 22, 2019
1 parent 49a9df7 commit f6ec784
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 8 deletions.
44 changes: 44 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,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
*/
Expand Down
43 changes: 43 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 All @@ -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
*/
Expand Down
60 changes: 52 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 @@ -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' );
}
Expand Down Expand Up @@ -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();
}

/**
Expand Down
51 changes: 51 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 Expand Up @@ -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
*/
Expand Down

0 comments on commit f6ec784

Please sign in to comment.