Skip to content

Commit

Permalink
Initial implementation of Root Entry points for GuestCommenter (prev …
Browse files Browse the repository at this point in the history
…CommentAuthor)
  • Loading branch information
moonmeister committed Oct 13, 2022
1 parent b1e1b3a commit 8020c21
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 66 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
4 changes: 2 additions & 2 deletions src/AppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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 @@ -102,7 +102,7 @@ public function __construct() {
* Create a list of loaders to be available in AppContext
*/
$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';
}
}
18 changes: 10 additions & 8 deletions src/Data/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use WPGraphQL\Data\Connection\UserRoleConnectionResolver;
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 +73,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 @@ -607,8 +609,8 @@ public static function resolve_node_type( $node ) {
case $node instanceof Plugin:
$type = 'Plugin';
break;
case $node instanceof CommentAuthor:
$type = 'CommentAuthor';
case $node instanceof GuestCommenter:
$type = 'GuestCommenter';
break;
case $node instanceof Menu:
$type = 'Menu';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
namespace WPGraphQL\Data\Loader;

use Exception;
use WPGraphQL\Model\CommentAuthor;
use WPGraphQL\Model\GuestCommenter;

/**
* Class CommentAuthorLoader
* Class GuestCommenterLoader
*
* @package WPGraphQL\Data\Loader
*/
class CommentAuthorLoader extends AbstractDataLoader {
class GuestCommenterLoader extends AbstractDataLoader {

/**
* @param mixed $entry The User Role object
* @param mixed $key The Key to identify the user role by
* @param mixed $entry The Comment object
* @param mixed $key The Key to identify the comment by
*
* @return mixed|CommentAuthor
* @return mixed|GuestCommenter
* @throws Exception
*/
protected function get_model( $entry, $key ) {

if ( ! $entry instanceof \WP_Comment ) {
if ( ! $entry instanceof \WP_Comment && ! empty( $entry->user_id )) {
return null;
}

return new CommentAuthor( $entry );
return new GuestCommenter( $entry );
}

/**
Expand All @@ -44,6 +44,7 @@ public function loadKeys( array $keys ) {
'number' => count( $keys ),
'no_found_rows' => true,
'count' => false,
'user_id' => 0,
];

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Model/CommentAuthor.php → src/Model/GuestCommenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use WP_Comment;

/**
* Class CommentAuthor - Models the CommentAuthor object
* Class GuestCommenter - Models the GuestCommenter object
*
* @property string $id
* @property int $databaseId
Expand All @@ -17,24 +17,24 @@
*
* @package WPGraphQL\Model
*/
class CommentAuthor extends Model {
class GuestCommenter extends Model {

/**
* Stores the comment author to be modeled
* Stores the guest commenter to be modeled
*
* @var WP_Comment $data The raw data passed to he model
*/
protected $data;

/**
* CommentAuthor constructor.
* GuestCommenter constructor.
*
* @param WP_Comment $comment_author The incoming comment author array to be modeled
* @param WP_Comment $guest_commenter The incoming guest commenter array to be modeled
*
* @throws Exception
*/
public function __construct( WP_Comment $comment_author ) {
$this->data = $comment_author;
public function __construct( WP_Comment $guest_commenter ) {
$this->data = $guest_commenter;
parent::__construct();
}

Expand All @@ -49,7 +49,7 @@ protected function init() {

$this->fields = [
'id' => function () {
return ! empty( $this->data->comment_ID ) ? Relay::toGlobalId( 'comment_author', $this->data->comment_ID ) : null;
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;
Expand Down
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 guest commenter's ID, then the
// current user is the guest 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
40 changes: 40 additions & 0 deletions src/Type/Enum/GuestCommenterIdTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace WPGraphQL\Type\Enum;

class GuestCommenterIdTypeEnum {

/**
* Register the Enum used for setting the field to identify User nodes by
*
* @return void
*/
public static function register_type() {
register_graphql_enum_type(
'GuestCommenterIdTypeEnum',
[
'description' => __( 'The Type of Identifier used to fetch a single GuestCommenter node. To be used along with the "id" field. Default is "ID".', 'wp-graphql' ),
'values' => self::get_values(),
]
);
}

/**
* Returns the values for the Enum.
*
* @return array
*/
public static function get_values() {
return [
'ID' => [
'name' => 'ID',
'value' => 'global_id',
'description' => __( 'The hashed Global ID', 'wp-graphql' ),
],
'DATABASE_ID' => [
'name' => 'DATABASE_ID',
'value' => 'database_id',
'description' => __( 'The Database ID for the node', 'wp-graphql' ),
]
];
}
}

0 comments on commit 8020c21

Please sign in to comment.