Skip to content

Commit eaef11f

Browse files
authored
Merge pull request #3208 from jasonbahl/feat/expose-commenter-edge-fields
feat: expose commenter edge fields
2 parents 929f109 + 22793d1 commit eaef11f

File tree

7 files changed

+146
-15
lines changed

7 files changed

+146
-15
lines changed

access-functions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,3 +967,13 @@ static function ( \WPGraphQL\Admin\AdminNotices $admin_notices ) use ( $slug, $c
967967
}
968968
);
969969
}
970+
971+
/**
972+
* Get the admin notices registered for the WPGraphQL plugin screens
973+
*
974+
* @return array|mixed[][] An array of admin notices
975+
*/
976+
function get_graphql_admin_notices() {
977+
$admin_notices = \WPGraphQL\Admin\AdminNotices::get_instance();
978+
return $admin_notices->get_admin_notices();
979+
}

src/Admin/Admin.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public function init() {
4343
$this->admin_enabled = apply_filters( 'graphql_show_admin', true );
4444
$this->graphiql_enabled = apply_filters( 'graphql_enable_graphiql', get_graphql_setting( 'graphiql_enabled', true ) );
4545

46-
$admin_notices = new AdminNotices();
47-
$admin_notices->init();
46+
AdminNotices::get_instance();
4847

4948
// This removes the menu page for WPGraphiQL as it's now built into WPGraphQL
5049
if ( $this->graphiql_enabled ) {

src/Admin/AdminNotices.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
*/
1616
class AdminNotices {
1717

18+
/**
19+
* Stores the singleton instance of the class
20+
*
21+
* @var self|null
22+
*/
23+
private static $instance = null;
24+
1825
/**
1926
* Stores the admin notices to display
2027
*
@@ -27,6 +34,34 @@ class AdminNotices {
2734
*/
2835
protected $dismissed_notices = [];
2936

37+
/**
38+
* Private constructor to prevent direct instantiation
39+
*/
40+
private function __construct() {
41+
// Initialize the class (can move code from init() here if desired)
42+
}
43+
44+
/**
45+
* Prevent cloning the instance
46+
*/
47+
public function __clone() {}
48+
49+
/**
50+
* Prevent unserializing the instance
51+
*/
52+
public function __wakeup() {}
53+
54+
/**
55+
* Get the singleton instance of the class
56+
*/
57+
public static function get_instance(): self {
58+
if ( null === self::$instance ) {
59+
self::$instance = new self();
60+
self::$instance->init();
61+
}
62+
return self::$instance;
63+
}
64+
3065
/**
3166
* Initialize the Admin Notices class
3267
*/
@@ -206,8 +241,8 @@ public function add_notification_bubble(): void {
206241

207242
foreach ( $menu as $key => $item ) {
208243
if ( 'graphiql-ide' === $item[2] ) {
209-
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
210-
$menu[ $key ][0] .= ' <span class="update-plugins count-' . absint( $notice_count ) . '>"><span class="plugin-count">' . absint( $notice_count ) . '</span></span>';
244+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
245+
$menu[ $key ][0] .= ' <span class="update-plugins count-' . absint( $notice_count ) . '"><span class="plugin-count">' . absint( $notice_count ) . '</span></span>';
211246
break;
212247
}
213248
}
@@ -366,4 +401,4 @@ public function handle_dismissal_of_notice(): void {
366401
wp_safe_redirect( remove_query_arg( [ 'wpgraphql_disable_notice_nonce', 'wpgraphql_disable_notice' ] ) );
367402
exit();
368403
}
369-
}
404+
}

src/Model/Comment.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* @property string $authorIp
1818
* @property string $comment_author
1919
* @property string $comment_author_url
20+
* @property string $commentAuthor
21+
* @property string $commentAuthorUrl
2022
* @property string $commentAuthorEmail
2123
* @property string $contentRaw
2224
* @property string $contentRendered
@@ -60,6 +62,9 @@ public function __construct( WP_Comment $comment ) {
6062
'type',
6163
'commentedOnId',
6264
'comment_post_ID',
65+
'commentAuthorUrl',
66+
'commentAuthorEmail',
67+
'commentAuthor',
6368
'approved',
6469
'status',
6570
'comment_parent_id',
@@ -122,7 +127,7 @@ protected function init() {
122127
return ! empty( $this->data->comment_ID ) ? $this->data->comment_ID : 0;
123128
},
124129
'commentAuthorEmail' => function () {
125-
return ! empty( $this->data->comment_author_email ) ? $this->data->comment_author_email : 0;
130+
return current_user_can( 'moderate_comments' ) && ! empty( $this->data->comment_author_email ) ? $this->data->comment_author_email : null;
126131
},
127132
'comment_ID' => function () {
128133
return ! empty( $this->data->comment_ID ) ? absint( $this->data->comment_ID ) : 0;
@@ -140,10 +145,16 @@ protected function init() {
140145
return ! empty( $this->comment_parent_id ) ? Relay::toGlobalId( 'comment', $this->data->comment_parent ) : null;
141146
},
142147
'comment_author' => function () {
143-
return ! empty( $this->data->comment_author ) ? absint( $this->data->comment_author ) : null;
148+
return ! empty( $this->data->comment_author ) ? $this->data->comment_author : null;
144149
},
145150
'comment_author_url' => function () {
146-
return ! empty( $this->data->comment_author_url ) ? absint( $this->data->comment_author_url ) : null;
151+
return ! empty( $this->data->comment_author_url ) ? $this->data->comment_author_url : null;
152+
},
153+
'commentAuthor' => function () {
154+
return ! empty( $this->data->comment_author ) ? $this->data->comment_author : null;
155+
},
156+
'commentAuthorUrl' => function () {
157+
return ! empty( $this->data->comment_author_url ) ? $this->data->comment_author_url : null;
147158
},
148159
'authorIp' => function () {
149160
return ! empty( $this->data->comment_author_IP ) ? $this->data->comment_author_IP : null;

src/Type/ObjectType/Comment.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,36 @@ public static function register_type() {
2929
'toType' => 'Commenter',
3030
'description' => __( 'The author of the comment', 'wp-graphql' ),
3131
'oneToOne' => true,
32+
'edgeFields' => [
33+
'email' => [
34+
'type' => 'String',
35+
'description' => __( 'The email address representing the author for this particular comment', 'wp-graphql' ),
36+
'resolve' => static function ( $edge ) {
37+
return $edge['source']->commentAuthorEmail ?: null;
38+
},
39+
],
40+
'ipAddress' => [
41+
'type' => 'String',
42+
'description' => __( 'IP address of the author at the time of making this comment. This field is equivalent to WP_Comment->comment_author_IP and the value matching the "comment_author_IP" column in SQL.', 'wp-graphql' ),
43+
'resolve' => static function ( $edge ) {
44+
return $edge['source']->authorIp ?: null;
45+
},
46+
],
47+
'name' => [
48+
'type' => 'String',
49+
'description' => __( 'The display name of the comment author for this particular comment', 'wp-graphql' ),
50+
'resolve' => static function ( $edge ) {
51+
return $edge['source']->commentAuthor;
52+
},
53+
],
54+
'url' => [
55+
'type' => 'String',
56+
'description' => __( 'The url entered for the comment author on this particular comment', 'wp-graphql' ),
57+
'resolve' => static function ( $edge ) {
58+
return $edge['source']->commentAuthorUrl ?: null;
59+
},
60+
],
61+
],
3262
'resolve' => static function ( $comment, $_args, AppContext $context ) {
3363
$node = null;
3464

@@ -64,8 +94,9 @@ public static function register_type() {
6494
},
6595
],
6696
'authorIp' => [
67-
'type' => 'String',
68-
'description' => __( 'IP address for the author. This field is equivalent to WP_Comment->comment_author_IP and the value matching the "comment_author_IP" column in SQL.', 'wp-graphql' ),
97+
'type' => 'String',
98+
'deprecationReason' => __( 'Use the ipAddress field on the edge between the comment and author', 'wp-graphql' ),
99+
'description' => __( 'IP address for the author at the time of commenting. This field is equivalent to WP_Comment->comment_author_IP and the value matching the "comment_author_IP" column in SQL.', 'wp-graphql' ),
69100
],
70101
'commentId' => [
71102
'type' => 'Int',

tests/wpunit/AdminNoticesTest.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public function tearDown(): void {
2121
* Test initialization of admin notices.
2222
*/
2323
public function testInit(): void {
24-
$adminNotices = new AdminNotices();
25-
$adminNotices->init();
24+
AdminNotices::get_instance();
2625

2726
// Assertions to ensure actions are hooked correctly
2827
// This might require functional testing or integration testing setup
@@ -33,7 +32,7 @@ public function testInit(): void {
3332
* Test adding and retrieving admin notices.
3433
*/
3534
public function testAddAndGetAdminNotices(): void {
36-
$adminNotices = new AdminNotices();
35+
$adminNotices = AdminNotices::get_instance();
3736

3837
$slug = 'test-notice';
3938
$config = [
@@ -49,11 +48,35 @@ public function testAddAndGetAdminNotices(): void {
4948
$this->assertSame($config, $notices[$slug]);
5049
}
5150

51+
/**
52+
* Test adding and retrieving admin notices.
53+
*/
54+
public function testGetAdminNotices(): void {
55+
$adminNotices = AdminNotices::get_instance();
56+
57+
$slug = 'test-notice';
58+
$config = [
59+
'message' => 'Test Notice Message',
60+
'type' => 'warning',
61+
'is_dismissable' => true
62+
];
63+
64+
$adminNotices->add_admin_notice($slug, $config);
65+
66+
$notices = $adminNotices->get_admin_notices();
67+
$this->assertArrayHasKey($slug, $notices);
68+
$this->assertSame($config, $notices[$slug]);
69+
70+
$get_admin_notices = get_graphql_admin_notices();
71+
72+
$this->assertSame($get_admin_notices, $notices);
73+
}
74+
5275
/**
5376
* Test removing admin notices.
5477
*/
5578
public function testRemoveAdminNotices(): void {
56-
$adminNotices = new AdminNotices();
79+
$adminNotices = AdminNotices::get_instance();
5780

5881
$slug = 'test-notice';
5982
$config = ['message' => 'Test Notice Message'];

tests/wpunit/CommentObjectQueriesTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,16 +465,21 @@ public function testCommentQueryHiddenFields( $user, $should_display ) {
465465
'comment_post_ID' => $post_id,
466466
'comment_content' => 'Admin Comment',
467467
'comment_author_email' => '[email protected]',
468+
'comment_author_url' => 'https://example.com',
468469
'comment_author_IP' => '127.0.0.1',
469470
'comment_agent' => 'Admin Agent',
471+
'comment_author' => 'Admin Name',
472+
470473
];
471474
$admin_comment = $this->createCommentObject( $admin_args );
472475
$subscriber_args = [
473476
'comment_post_ID' => $post_id,
474477
'comment_content' => 'Subscriber Comment',
475-
'comment_author_email' => '[email protected]',
478+
'comment_author_email' => '[email protected]',
479+
'comment_author_url' => 'https://example.com',
476480
'comment_author_IP' => '127.0.0.1',
477481
'comment_agent' => 'Subscriber Agent',
482+
'comment_author' => 'Subscriber Name',
478483
];
479484
$subscriber_comment = $this->createCommentObject( $subscriber_args );
480485

@@ -497,6 +502,14 @@ public function testCommentQueryHiddenFields( $user, $should_display ) {
497502
}
498503
}
499504
}
505+
author {
506+
name
507+
email
508+
url
509+
node {
510+
__typename
511+
}
512+
}
500513
}
501514
}
502515
';
@@ -513,6 +526,10 @@ public function testCommentQueryHiddenFields( $user, $should_display ) {
513526
];
514527
$subscriber_actual = $this->graphql( compact( 'query', 'variables' ) );
515528

529+
codecept_debug( [
530+
'$subscriber_actual' => $subscriber_actual,
531+
]);
532+
516533
$this->assertArrayNotHasKey( 'errors', $admin_actual );
517534
$this->assertArrayNotHasKey( 'errors', $subscriber_actual );
518535

@@ -526,12 +543,17 @@ public function testCommentQueryHiddenFields( $user, $should_display ) {
526543
$this->assertEquals( $expected_link, $admin_actual['data']['comment']['link'] );
527544
$this->assertEquals( str_ireplace( home_url(), '', $expected_link ), $admin_actual['data']['comment']['uri'] );
528545

546+
$this->assertEquals( $subscriber_args['comment_author'], $subscriber_actual['data']['comment']['author']['name'] );
547+
$this->assertEquals( $subscriber_args['comment_author_url'], $subscriber_actual['data']['comment']['author']['url'] );
548+
529549
if ( true === $should_display ) {
530550
$this->assertNotNull( $admin_actual['data']['comment']['authorIp'] );
531551
$this->assertNotNull( $admin_actual['data']['comment']['agent'] );
552+
$this->assertSame( $subscriber_args['comment_author_email'], $subscriber_actual['data']['comment']['author']['email'] );
532553
} else {
533554
$this->assertNull( $admin_actual['data']['comment']['authorIp'] );
534555
$this->assertNull( $admin_actual['data']['comment']['agent'] );
556+
$this->assertNull( $subscriber_actual['data']['comment']['author']['email'] );
535557
}
536558
}
537559

0 commit comments

Comments
 (0)