-
Notifications
You must be signed in to change notification settings - Fork 441
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
base: develop
Are you sure you want to change the base?
Changes from all commits
aca653e
3428adb
fe89f33
5fc4f15
b8a280a
aac0bf2
a6cf76c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ), | ||
|
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 [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ), | ||
], | ||
]; | ||
} | ||
} |
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'; | ||
} | ||
} |
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; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 : ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid too many |
||
}, | ||
]; | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
There was a problem hiding this comment.
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.