Skip to content

Commit

Permalink
feat: new GuestCommenter and entry points without changing CommentAuthor
Browse files Browse the repository at this point in the history
  • Loading branch information
moonmeister committed Oct 13, 2022
1 parent b1e1b3a commit aca653e
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bin/run-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ case "$subcommand" in
WP_VERSION=${WP_VERSION} PHP_VERSION=${PHP_VERSION} docker compose up app
;;
t )
docker-compose run --rm \
docker compose run --rm \
-e COVERAGE=${COVERAGE-} \
-e USING_XDEBUG=${USING_XDEBUG-} \
-e DEBUG=${DEBUG-} \
Expand Down
2 changes: 2 additions & 0 deletions src/AppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GraphQL\Error\UserError;
use WP_User;
use WPGraphQL\Data\Loader\CommentAuthorLoader;
use WPGraphQL\Data\Loader\GuestCommenterLoader;
use WPGraphQL\Data\Loader\CommentLoader;
use WPGraphQL\Data\Loader\EnqueuedScriptLoader;
use WPGraphQL\Data\Loader\EnqueuedStylesheetLoader;
Expand Down Expand Up @@ -103,6 +104,7 @@ public function __construct() {
*/
$loaders = [
'comment_author' => new CommentAuthorLoader( $this ),
'guest_commenter' => new GuestCommenterLoader( $this ),
'comment' => new CommentLoader( $this ),
'enqueued_script' => new EnqueuedScriptLoader( $this ),
'enqueued_stylesheet' => new EnqueuedStylesheetLoader( $this ),
Expand Down
96 changes: 96 additions & 0 deletions src/Connection/GuestCommenters.php
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' ),
// ],
];
}
}
31 changes: 31 additions & 0 deletions src/Data/Connection/GuestCommenterConnectionResolver.php
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';
}
}
13 changes: 8 additions & 5 deletions src/Data/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use WPGraphQL\Model\Avatar;
use WPGraphQL\Model\Comment;
use WPGraphQL\Model\CommentAuthor;
use WPGraphQL\Model\GuestCommenter;
use WPGraphQL\Model\Menu;
use WPGraphQL\Model\Plugin;
use WPGraphQL\Model\Post;
Expand Down Expand Up @@ -73,16 +74,18 @@ public static function resolve_comment( $id, $context ) {
/**
* Retrieves a WP_Comment object for the ID that gets passed
*
* @param int $comment_id The ID of the comment the comment author is associated with.
* @param int $comment_id The ID of the comment the guest commenter is associated with.
*
* @return mixed|CommentAuthor|null
* @return mixed|GuestCommenter|null
* @throws Exception Throws Exception.
*/
public static function resolve_comment_author( int $comment_id ) {
public static function resolve_guest_commenter( int $comment_id ) {

$comment_author = get_comment( $comment_id );
$comment = get_comment( $comment_id );

return ! empty( $comment_author ) ? new CommentAuthor( $comment_author ) : null;


return ! empty( $comment ) && empty( $comment->user_id ) ? new GuestCommenter( $comment ) : null;
}

/**
Expand Down
62 changes: 62 additions & 0 deletions src/Data/Loader/GuestCommenterLoader.php
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;
}

}
70 changes: 70 additions & 0 deletions src/Model/GuestCommenter.php
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 : '';
},
];

}
}
}
4 changes: 2 additions & 2 deletions src/Mutation/CommentDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public static function mutate_and_get_payload() {
} else {
// Get the current user id
$current_user_id = absint( get_current_user_id() );
// If the current user ID is the same as the comment author's ID, then the
// current user is the comment author and can delete the comment
// If the current user ID is the same as the commenter's ID, then the
// current user is the commenter and can delete the comment
if ( 0 !== $current_user_id && absint( $user_id ) === $current_user_id ) {
$not_allowed = false;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Registry/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use InvalidArgumentException;
use WPGraphQL\Connection\GuestCommenters;
use WPGraphQL\Connection\Comments;
use WPGraphQL\Connection\MenuItems;
use WPGraphQL\Connection\PostObjects;
Expand Down Expand Up @@ -45,6 +46,7 @@
use WPGraphQL\Type\Enum\TaxonomyIdTypeEnum;
use WPGraphQL\Type\Enum\TermNodeIdTypeEnum;
use WPGraphQL\Type\Enum\UserNodeIdTypeEnum;
use WPGraphQL\Type\Enum\GuestCommenterIdTypeEnum;
use WPGraphQL\Type\Enum\UsersConnectionOrderbyEnum;
use WPGraphQL\Type\Input\UsersConnectionOrderbyInput;
use WPGraphQL\Type\InterfaceType\CommenterInterface;
Expand Down Expand Up @@ -98,7 +100,7 @@
use WPGraphQL\Type\Input\PostObjectsConnectionOrderbyInput;
use WPGraphQL\Type\ObjectType\Avatar;
use WPGraphQL\Type\ObjectType\Comment;
use WPGraphQL\Type\ObjectType\CommentAuthor;
use WPGraphQL\Type\ObjectType\GuestCommenter;
use WPGraphQL\Type\ObjectType\MediaDetails;
use WPGraphQL\Type\ObjectType\MediaItemMeta;
use WPGraphQL\Type\ObjectType\MediaSize;
Expand Down Expand Up @@ -281,7 +283,7 @@ public function init_type_registry( TypeRegistry $type_registry ) {
RootMutation::register_type();
Avatar::register_type();
Comment::register_type();
CommentAuthor::register_type();
GuestCommenter::register_type();
ContentTemplate::register_content_template_types();
EnqueuedStylesheet::register_type();
EnqueuedScript::register_type();
Expand All @@ -301,6 +303,7 @@ public function init_type_registry( TypeRegistry $type_registry ) {
UserRole::register_type();

AvatarRatingEnum::register_type();
GuestCommenterIdTypeEnum::register_type();
CommentsConnectionOrderbyEnum::register_type();
CommentNodeIdTypeEnum::register_type();
ContentNodeIdTypeEnum::register_type();
Expand Down Expand Up @@ -342,6 +345,7 @@ public function init_type_registry( TypeRegistry $type_registry ) {
/**
* Register core connections
*/
GuestCommenters::register_connections();
Comments::register_connections();
MenuItems::register_connections();
PostObjects::register_connections();
Expand Down

0 comments on commit aca653e

Please sign in to comment.