Skip to content

Commit

Permalink
Merge branch 'develop' into features/wp-graphql#156-optionsQueries
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Types.php
  • Loading branch information
jasonbahl committed Oct 27, 2017
2 parents 355b275 + df64459 commit f1f41d5
Show file tree
Hide file tree
Showing 12 changed files with 810 additions and 81 deletions.
35 changes: 23 additions & 12 deletions src/Data/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function __construct() {
}

/**
* This filters the WPQuery 'where' $args, enforcing the query to return results before or after the
* referenced cursor
* This filters the WPQuery 'where' $args, enforcing the query to return results before or
* after the referenced cursor
*
* @param string $where The WHERE clause of the query.
* @param \WP_Query $query The WP_Query instance (passed by reference).
Expand Down Expand Up @@ -115,6 +115,11 @@ public function graphql_wp_query_cursor_pagination_support( $where, \WP_Query $q
*/
public function graphql_wp_term_query_cursor_pagination_support( array $pieces, $taxonomies, $args ) {

/**
* Access the global $wpdb object
*/
global $wpdb;

if ( defined( 'GRAPHQL_REQUEST' ) && GRAPHQL_REQUEST && ! empty( $args['graphql_cursor_offset'] ) ) {

$cursor_offset = $args['graphql_cursor_offset'];
Expand All @@ -125,7 +130,7 @@ public function graphql_wp_term_query_cursor_pagination_support( array $pieces,
if ( is_integer( $cursor_offset ) && 0 < $cursor_offset ) {

$compare = ! empty( $args['graphql_cursor_compare'] ) ? $args['graphql_cursor_compare'] : '>';
$compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';
$compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';

$order_by = ! empty( $args['orderby'] ) ? $args['orderby'] : 'comment_date';
$order = ! empty( $args['order'] ) ? $args['order'] : 'DESC';
Expand All @@ -135,9 +140,9 @@ public function graphql_wp_term_query_cursor_pagination_support( array $pieces,
$cursor_term = get_term( $cursor_offset );

if ( ! empty( $cursor_term ) && ! empty( $cursor_term->name ) ) {
$pieces['where'] .= sprintf( " AND t.{$order_by} {$order_compare} '{$cursor_term->{$order_by}}'" );
$pieces['where'] .= $wpdb->prepare( " AND t.{$order_by} {$order_compare} %s", $cursor_term->{$order_by} );
} else {
$pieces['where'] .= sprintf( ' AND t.term_id %1$s %2$d', $compare, $cursor_offset );
$pieces['where'] .= $wpdb->prepare( ' AND t.term_id %1$s %2$d', $compare, $cursor_offset );
}
}
}
Expand All @@ -147,16 +152,21 @@ public function graphql_wp_term_query_cursor_pagination_support( array $pieces,
}

/**
* This returns a modified version of the $pieces of the comment query clauses if the request is a GRAPHQL_REQUEST
* and the query has a graphql_cursor_offset defined
* This returns a modified version of the $pieces of the comment query clauses if the request
* is a GRAPHQL_REQUEST and the query has a graphql_cursor_offset defined
*
* @param array $pieces A compacted array of comment query clauses.
* @param \WP_Comment_Query $query Current instance of WP_Comment_Query, passed by reference.
* @param array $pieces A compacted array of comment query clauses.
* @param \WP_Comment_Query $query Current instance of WP_Comment_Query, passed by reference.
*
* @return array $pieces
*/
public function graphql_wp_comments_query_cursor_pagination_support( array $pieces, \WP_Comment_Query $query ) {

/**
* Access the global $wpdb object
*/
global $wpdb;

if (
defined( 'GRAPHQL_REQUEST' ) && GRAPHQL_REQUEST &&
( is_array( $query->query_vars ) && array_key_exists( 'graphql_cursor_offset', $query->query_vars ) )
Expand All @@ -170,7 +180,7 @@ public function graphql_wp_comments_query_cursor_pagination_support( array $piec
if ( is_integer( $cursor_offset ) && 0 < $cursor_offset ) {

$compare = ! empty( $query->get( 'graphql_cursor_compare' ) ) ? $query->get( 'graphql_cursor_compare' ) : '>';
$compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';
$compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';

$order_by = ! empty( $query->query_vars['order_by'] ) ? $query->query_vars['order_by'] : 'comment_date';
$order = ! empty( $query->query_vars['order'] ) ? $query->query_vars['order'] : 'DESC';
Expand All @@ -179,12 +189,13 @@ public function graphql_wp_comments_query_cursor_pagination_support( array $piec
// Get the $cursor_post
$cursor_comment = get_comment( $cursor_offset );
if ( ! empty( $cursor_comment ) ) {
$pieces['where'] .= sprintf( " AND {$order_by} {$order_compare} '{$cursor_comment->{$order_by}}'" );
$pieces['where'] .= $wpdb->prepare( " AND {$order_by} {$order_compare} %s", $cursor_comment->{$order_by} );
} else {
$pieces['where'] .= sprintf( ' AND comment_ID %1$s %2$d', $compare, $cursor_offset );
$pieces['where'] .= $wpdb->prepare( ' AND comment_ID %1$s %2$d', $compare, $cursor_offset );
}
}
}

return $pieces;

}
Expand Down
55 changes: 41 additions & 14 deletions src/Data/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class DataSource {
* @param int $id ID of the comment we want to get the object for
*
* @return \WP_Comment object
* @throws \Exception
* @throws UserError
* @since 0.0.5
* @access public
*/
Expand All @@ -72,6 +72,22 @@ public static function resolve_comment( $id ) {

}

/**
* Retrieves a WP_Comment object for the ID that gets passed
*
* @param string $author_email The ID of the comment the comment author is associated with.
*
* @return array
* @throws
*/
public static function resolve_comment_author( $author_email ) {
global $wpdb;
$comment_author = $wpdb->get_row( $wpdb->prepare( "SELECT comment_author_email, comment_author, comment_author_url, comment_author_email from $wpdb->comments WHERE comment_author_email = %s LIMIT 1", esc_sql( $author_email ) ) );
$comment_author = ! empty( $comment_author ) ? ( array ) $comment_author : [];
$comment_author['is_comment_author'] = true;
return $comment_author;
}

/**
* Wrapper for the CommentsConnectionResolver class
*
Expand Down Expand Up @@ -159,7 +175,7 @@ public static function resolve_plugins_connection( $source, array $args, AppCont
* @param int $id ID of the post you are trying to retrieve
* @param string $post_type Post type the post is attached to
*
* @throws \Exception
* @throws UserError
* @since 0.0.5
* @return \WP_Post
* @access public
Expand Down Expand Up @@ -210,7 +226,7 @@ public static function resolve_post_objects_connection( $source, array $args, Ap
* @param string $post_type Name of the post type you want to retrieve the object for
*
* @return \WP_Post_Type object
* @throws \Exception
* @throws UserError
* @since 0.0.5
* @access public
*/
Expand Down Expand Up @@ -238,7 +254,7 @@ public static function resolve_post_type( $post_type ) {
* @param string $taxonomy Name of the taxonomy you want to retrieve the taxonomy object for
*
* @return \WP_Taxonomy object
* @throws \Exception
* @throws UserError
* @since 0.0.5
* @access public
*/
Expand Down Expand Up @@ -267,7 +283,7 @@ public static function resolve_taxonomy( $taxonomy ) {
* @param string $taxonomy Name of the taxonomy the term is in
*
* @return mixed
* @throws \Exception
* @throws UserError
* @since 0.0.5
* @access public
*/
Expand Down Expand Up @@ -307,7 +323,7 @@ public static function resolve_term_objects_connection( $source, array $args, $c
* @param string $stylesheet Directory name for the theme.
*
* @return \WP_Theme object
* @throws \Exception
* @throws UserError
* @since 0.0.5
* @access public
*/
Expand Down Expand Up @@ -342,7 +358,7 @@ public static function resolve_themes_connection( $source, array $args, $context
* @param int $id ID of the user you want the object for
*
* @return bool|\WP_User
* @throws \Exception
* @throws UserError
* @since 0.0.5
* @access public
*/
Expand Down Expand Up @@ -453,7 +469,7 @@ public static function get_allowed_setting_types() {
* an object that implements node to its type.
*
* @return array
* @throws \Exception
* @throws UserError
* @access public
*/
public static function get_node_definition() {
Expand Down Expand Up @@ -492,7 +508,6 @@ function( $global_id ) {
$allowed_taxonomies = \WPGraphQL::$allowed_taxonomies;

switch ( $id_components['type'] ) {

case in_array( $id_components['type'], $allowed_post_types, true ):
$node = self::resolve_post_object( $id_components['id'], $id_components['type'] );
break;
Expand All @@ -502,10 +517,13 @@ function( $global_id ) {
case 'comment':
$node = self::resolve_comment( $id_components['id'] );
break;
case 'commentAuthor':
$node = self::resolve_comment_author( $id_components['id'] );
break;
case 'plugin':
$node = self::resolve_plugin( $id_components['id'] );
break;
case 'post_type':
case 'postType':
$node = self::resolve_post_type( $id_components['id'] );
break;
case 'taxonomy':
Expand Down Expand Up @@ -566,7 +584,7 @@ function( $node ) {
$type = Types::term_object( $node->taxonomy );
break;
case $node instanceof \WP_Comment:
$type = Types::comment();;
$type = Types::comment();
break;
case $node instanceof \WP_Post_Type:
$type = Types::post_type();
Expand All @@ -582,12 +600,21 @@ function( $node ) {
break;
default:
$type = null;
break;
}

// Some nodes might return an array instead of an object
} elseif ( is_array( $node ) && array_key_exists( 'PluginURI', $node ) ) {
$type = Types::plugin();
} elseif ( is_array( $node ) ) {

switch ( $node ) {
case array_key_exists( 'PluginURI', $node ):
$type = Types::plugin();
break;
case array_key_exists( 'is_comment_author', $node ):
$type = Types::comment_author();
break;
default:
$type = null;
}
}

/**
Expand Down
Loading

0 comments on commit f1f41d5

Please sign in to comment.