Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Root Entry points for GuestCommenter #2577

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 \
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker changed how compose is executed. I'm not sure if I was the only one having the issue, but if y'all aren't having issues this probably doesn't need to carry over.

-e COVERAGE=${COVERAGE-} \
-e USING_XDEBUG=${USING_XDEBUG-} \
-e DEBUG=${DEBUG-} \
Expand Down
4 changes: 3 additions & 1 deletion src/AppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use WPGraphQL\Data\Loader\CommentLoader;
use WPGraphQL\Data\Loader\EnqueuedScriptLoader;
use WPGraphQL\Data\Loader\EnqueuedStylesheetLoader;
use WPGraphQL\Data\Loader\GuestCommenterLoader;
use WPGraphQL\Data\Loader\PluginLoader;
use WPGraphQL\Data\Loader\PostObjectLoader;
use WPGraphQL\Data\Loader\PostTypeLoader;
Expand Down Expand Up @@ -106,8 +107,9 @@ public function __construct() {
'comment' => new CommentLoader( $this ),
'enqueued_script' => new EnqueuedScriptLoader( $this ),
'enqueued_stylesheet' => new EnqueuedStylesheetLoader( $this ),
'plugin' => new PluginLoader( $this ),
'guest_commenter' => new GuestCommenterLoader( $this ),
'nav_menu_item' => new PostObjectLoader( $this ),
'plugin' => new PluginLoader( $this ),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was cosmetic to keep with the alphabetical list.

'post' => new PostObjectLoader( $this ),
'post_type' => new PostTypeLoader( $this ),
'taxonomy' => new TaxonomyLoader( $this ),
Expand Down
85 changes: 85 additions & 0 deletions src/Connection/GuestCommenters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace WPGraphQL\Connection;

use Exception;
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 [
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

'authorEmail' => [
'type' => 'String',
'description' => __( 'Guest commenter email address.', 'wp-graphql' ),
],
'authorUrl' => [
'type' => 'String',
'description' => __( 'Guest commenter domain', 'wp-graphql' ),
],
'orderby' => [
'type' => 'GuestCommenterOrderbyEnum',
'description' => __( 'Field to order the comments by.', 'wp-graphql' ),
],
'order' => [
'type' => 'OrderEnum',
'description' => __( 'The cardinality of the order of the connection', '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';
}
}
16 changes: 11 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 Expand Up @@ -610,6 +613,9 @@ public static function resolve_node_type( $node ) {
case $node instanceof CommentAuthor:
$type = 'CommentAuthor';
break;
case $node instanceof GuestCommenter:
$type = 'GuestCommenter';
break;
case $node instanceof Menu:
$type = 'Menu';
break;
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.


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( 'guest_commenter', $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 : '';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid too many return statements within this method.

},
];

}
}
}
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cosmetic. I think I was trying to keep consistent language through out the code base.

// 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
10 changes: 10 additions & 0 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,9 +46,12 @@
use WPGraphQL\Type\Enum\TaxonomyIdTypeEnum;
use WPGraphQL\Type\Enum\TermNodeIdTypeEnum;
use WPGraphQL\Type\Enum\UserNodeIdTypeEnum;
use WPGraphQL\Type\Enum\GuestCommenterIdTypeEnum;
use WPGraphQL\Type\Enum\GuestCommenterOrderbyEnum;
use WPGraphQL\Type\Enum\UsersConnectionOrderbyEnum;
use WPGraphQL\Type\Input\UsersConnectionOrderbyInput;
use WPGraphQL\Type\InterfaceType\CommenterInterface;
use WPGraphQL\Type\InterfaceType\CommentAuthorInterface;
use WPGraphQL\Type\InterfaceType\ContentNode;
use WPGraphQL\Type\InterfaceType\ContentTemplate;
use WPGraphQL\Type\InterfaceType\DatabaseIdentifier;
Expand Down Expand Up @@ -99,6 +103,7 @@
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 @@ -254,6 +259,7 @@ public function init_type_registry( TypeRegistry $type_registry ) {
// Register Interfaces.
Node::register_type();
CommenterInterface::register_type( $type_registry );
CommentAuthorInterface::register_type( $type_registry );
ContentNode::register_type( $type_registry );
ContentTemplate::register_type();
DatabaseIdentifier::register_type();
Expand Down Expand Up @@ -282,6 +288,7 @@ public function init_type_registry( TypeRegistry $type_registry ) {
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 +308,8 @@ public function init_type_registry( TypeRegistry $type_registry ) {
UserRole::register_type();

AvatarRatingEnum::register_type();
GuestCommenterIdTypeEnum::register_type();
GuestCommenterOrderbyEnum::register_type();
CommentsConnectionOrderbyEnum::register_type();
CommentNodeIdTypeEnum::register_type();
ContentNodeIdTypeEnum::register_type();
Expand Down Expand Up @@ -342,6 +351,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