-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new GuestCommenter and entry points without changing CommentAuthor
- Loading branch information
1 parent
b1e1b3a
commit aca653e
Showing
13 changed files
with
458 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace WPGraphQL\Connection; | ||
|
||
use WPGraphQL\Data\Connection\GuestCommenterConnectionResolver; | ||
|
||
|
||
/** | ||
* Class Comments | ||
* | ||
* This class organizes the registration of connections to Comments | ||
* | ||
* @package WPGraphQL\Connection | ||
*/ | ||
class GuestCommenters { | ||
|
||
/** | ||
* Register connections to Guest Commenters. | ||
* | ||
* Connections from Root Query to Guest Commenters . | ||
* | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public static function register_connections() { | ||
|
||
/** | ||
* Register connection from RootQuery to Guest Commenters | ||
*/ | ||
register_graphql_connection( self::get_connection_config() ); | ||
} | ||
|
||
/** | ||
* Given an array of $args, this returns the connection config, merging the provided args | ||
* with the defaults | ||
* | ||
* @param array $args | ||
* | ||
* @return array | ||
*/ | ||
public static function get_connection_config( $args = [] ) { | ||
$defaults = [ | ||
'fromType' => 'RootQuery', | ||
'toType' => 'GuestCommenter', | ||
'fromFieldName' => 'guestCommenters', | ||
'connectionArgs' => self::get_connection_args(), | ||
'resolve' => function ( $source, $args, $context, $info ) { | ||
|
||
$resolver = new GuestCommenterConnectionResolver( $source, $args, $context, $info ); | ||
|
||
return $resolver->get_connection(); | ||
|
||
}, | ||
]; | ||
|
||
return array_merge( $defaults, $args ); | ||
} | ||
|
||
/** | ||
* This returns the connection args for the Comment connection | ||
* | ||
* @return array | ||
*/ | ||
public static function get_connection_args() { | ||
return [ | ||
'authorEmail' => [ | ||
'type' => 'String', | ||
'description' => __( 'Guest commenter email address.', 'wp-graphql' ), | ||
], | ||
'authorIn' => [ | ||
'type' => [ | ||
'list_of' => 'ID', | ||
], | ||
'description' => __( 'Array of author IDs to include comments for.', 'wp-graphql' ), | ||
], | ||
'authorNotIn' => [ | ||
'type' => [ | ||
'list_of' => 'ID', | ||
], | ||
'description' => __( 'Array of author IDs to exclude comments for.', 'wp-graphql' ), | ||
], | ||
// 'orderby' => [ | ||
// 'type' => 'CommentsConnectionOrderbyEnum', | ||
// 'description' => __( 'Field to order the comments by.', 'wp-graphql' ), | ||
// ], | ||
// 'order' => [ | ||
// 'type' => 'OrderEnum', | ||
// 'description' => __( 'The cardinality of the order of the connection', 'wp-graphql' ), | ||
// ], | ||
// 'search' => [ | ||
// 'type' => 'String', | ||
// 'description' => __( 'Search term(s) to retrieve matching comments for.', 'wp-graphql' ), | ||
// ], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace WPGraphQL\Data\Connection; | ||
|
||
/** | ||
* Class CommentConnectionResolver | ||
* | ||
* @package WPGraphQL\Data\Connection | ||
*/ | ||
class GuestCommenterConnectionResolver extends CommentConnectionResolver { | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function get_query_args() { | ||
|
||
$query_args = parent::get_query_args(); | ||
|
||
$query_args['user_id'] = 0; | ||
|
||
return apply_filters( 'graphql_guest_commenter_connection_query_args', $query_args, $this->source, $this->args, $this->context, $this->info ); | ||
} | ||
|
||
/** | ||
* Return the name of the loader | ||
* | ||
* @return string | ||
*/ | ||
public function get_loader_name() { | ||
return 'guest_commenter'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
namespace WPGraphQL\Data\Loader; | ||
|
||
use Exception; | ||
use WPGraphQL\Model\GuestCommenter; | ||
|
||
/** | ||
* Class GuestCommenterLoader | ||
* | ||
* @package WPGraphQL\Data\Loader | ||
*/ | ||
class GuestCommenterLoader extends AbstractDataLoader { | ||
|
||
/** | ||
* @param mixed $entry The Comment object | ||
* @param mixed $key The Key to identify the comment by | ||
* | ||
* @return mixed|GuestCommenter | ||
* @throws Exception | ||
*/ | ||
protected function get_model( $entry, $key ) { | ||
|
||
if ( ! $entry instanceof \WP_Comment && ! empty( $entry->user_id )) { | ||
return null; | ||
} | ||
|
||
return new GuestCommenter( $entry ); | ||
} | ||
|
||
/** | ||
* @param array $keys | ||
* | ||
* @return array | ||
*/ | ||
public function loadKeys( array $keys ) { | ||
/** | ||
* Prepare the args for the query. We're provided a specific set of IDs of comments | ||
* so we want to query as efficiently as possible with as little overhead to get the comment | ||
* objects. No need to count the rows, etc. | ||
*/ | ||
$args = [ | ||
'comment__in' => $keys, | ||
'orderby' => 'comment__in', | ||
'number' => count( $keys ), | ||
'no_found_rows' => true, | ||
'count' => false, | ||
'user_id' => 0, | ||
]; | ||
|
||
/** | ||
* Execute the query. Call get_comments() to add them to the cache. | ||
*/ | ||
$query = new \WP_Comment_Query( $args ); | ||
$query->get_comments(); | ||
$loaded = []; | ||
foreach ( $keys as $key ) { | ||
$loaded[ $key ] = \WP_Comment::get_instance( $key ); | ||
} | ||
return $loaded; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace WPGraphQL\Model; | ||
|
||
use Exception; | ||
use GraphQLRelay\Relay; | ||
use WP_Comment; | ||
|
||
/** | ||
* Class GuestCommenter - Models the GuestCommenter object | ||
* | ||
* @property string $id | ||
* @property int $databaseId | ||
* @property string $name | ||
* @property string $email | ||
* @property string $url | ||
* | ||
* @package WPGraphQL\Model | ||
*/ | ||
class GuestCommenter extends Model { | ||
|
||
/** | ||
* Stores the guest commenter to be modeled | ||
* | ||
* @var WP_Comment $data The raw data passed to he model | ||
*/ | ||
protected $data; | ||
|
||
/** | ||
* GuestCommenter constructor. | ||
* | ||
* @param WP_Comment $guest_commenter The incoming guest commenter array to be modeled | ||
* | ||
* @throws Exception | ||
*/ | ||
public function __construct( WP_Comment $guest_commenter ) { | ||
$this->data = $guest_commenter; | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Initializes the object | ||
* | ||
* @return void | ||
*/ | ||
protected function init() { | ||
|
||
if ( empty( $this->fields ) ) { | ||
|
||
$this->fields = [ | ||
'id' => function () { | ||
return ! empty( $this->data->comment_ID ) ? Relay::toGlobalId( 'comment_author', $this->data->comment_ID ) : null; | ||
}, | ||
'databaseId' => function () { | ||
return ! empty( $this->data->comment_ID ) ? absint( $this->data->comment_ID ) : null; | ||
}, | ||
'name' => function () { | ||
return ! empty( $this->data->comment_author ) ? $this->data->comment_author : null; | ||
}, | ||
'email' => function () { | ||
return current_user_can( 'moderate_comments' ) && ! empty( $this->data->comment_author_email ) ? $this->data->comment_author_email : null; | ||
}, | ||
'url' => function () { | ||
return ! empty( $this->data->comment_author_url ) ? $this->data->comment_author_url : ''; | ||
}, | ||
]; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.