| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Type\ObjectType; |
| 4 |
|
| 5 |
use WPGraphQL\AppContext; |
| 6 |
use WPGraphQL\Model\Comment as CommentModel; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Comment |
| 10 |
* |
| 11 |
* @package WPGraphQL\Type\Object |
| 12 |
*/ |
| 13 |
class Comment { |
| 14 |
|
| 15 |
/** |
| 16 |
* Register Comment Type |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public static function register_type() { |
| 21 |
register_graphql_object_type( |
| 22 |
'Comment', |
| 23 |
[ |
| 24 |
'description' => static function () { |
| 25 |
return __( 'A response or reaction to content submitted by users. Comments are typically associated with a specific content entry.', 'wp-graphql' ); |
| 26 |
}, |
| 27 |
'model' => CommentModel::class, |
| 28 |
'interfaces' => [ 'Node', 'DatabaseIdentifier', 'UniformResourceIdentifiable' ], |
| 29 |
'connections' => [ |
| 30 |
'author' => [ |
| 31 |
'toType' => 'Commenter', |
| 32 |
'description' => static function () { |
| 33 |
return __( 'The author of the comment', 'wp-graphql' ); |
| 34 |
}, |
| 35 |
'oneToOne' => true, |
| 36 |
'edgeFields' => [ |
| 37 |
'email' => [ |
| 38 |
'type' => 'String', |
| 39 |
'description' => static function () { |
| 40 |
return __( 'Email address representing the author for this particular comment', 'wp-graphql' ); |
| 41 |
}, |
| 42 |
'resolve' => static function ( $edge ) { |
| 43 |
return $edge['source']->commentAuthorEmail ?: null; |
| 44 |
}, |
| 45 |
], |
| 46 |
'ipAddress' => [ |
| 47 |
'type' => 'String', |
| 48 |
'description' => static function () { |
| 49 |
return __( 'IP address of the author at the time of making this comment.', 'wp-graphql' ); |
| 50 |
}, |
| 51 |
'resolve' => static function ( $edge ) { |
| 52 |
return $edge['source']->authorIp ?: null; |
| 53 |
}, |
| 54 |
], |
| 55 |
'name' => [ |
| 56 |
'type' => 'String', |
| 57 |
'description' => static function () { |
| 58 |
return __( 'The display name of the comment author for this particular comment', 'wp-graphql' ); |
| 59 |
}, |
| 60 |
'resolve' => static function ( $edge ) { |
| 61 |
return $edge['source']->commentAuthor; |
| 62 |
}, |
| 63 |
], |
| 64 |
'url' => [ |
| 65 |
'type' => 'String', |
| 66 |
'description' => static function () { |
| 67 |
return __( 'The url entered for the comment author on this particular comment', 'wp-graphql' ); |
| 68 |
}, |
| 69 |
'resolve' => static function ( $edge ) { |
| 70 |
return $edge['source']->commentAuthorUrl ?: null; |
| 71 |
}, |
| 72 |
], |
| 73 |
], |
| 74 |
'resolve' => static function ( $comment, $_args, AppContext $context ) { |
| 75 |
$node = null; |
| 76 |
|
| 77 |
// try and load the user node |
| 78 |
if ( ! empty( $comment->userId ) ) { |
| 79 |
$node = $context->get_loader( 'user' )->load( absint( $comment->userId ) ); |
| 80 |
} |
| 81 |
|
| 82 |
// If no node is loaded, fallback to the |
| 83 |
// public comment author data |
| 84 |
if ( ! $node || ( true === $node->isPrivate ) ) { |
| 85 |
$node = ! empty( $comment->commentId ) ? $context->get_loader( 'comment_author' )->load( $comment->commentId ) : null; |
| 86 |
} |
| 87 |
|
| 88 |
return [ |
| 89 |
'node' => $node, |
| 90 |
'source' => $comment, |
| 91 |
]; |
| 92 |
}, |
| 93 |
], |
| 94 |
], |
| 95 |
'fields' => static function () { |
| 96 |
return [ |
| 97 |
'agent' => [ |
| 98 |
'type' => 'String', |
| 99 |
'description' => static function () { |
| 100 |
return __( 'User agent (browser or client) used to post the comment.', 'wp-graphql' ); |
| 101 |
}, |
| 102 |
], |
| 103 |
'approved' => [ |
| 104 |
'type' => 'Boolean', |
| 105 |
'description' => static function () { |
| 106 |
return __( 'The approval status of the comment.', 'wp-graphql' ); |
| 107 |
}, |
| 108 |
'deprecationReason' => __( 'Deprecated in favor of the `status` field', 'wp-graphql' ), |
| 109 |
'resolve' => static function ( $comment ) { |
| 110 |
return 'approve' === $comment->status; |
| 111 |
}, |
| 112 |
], |
| 113 |
'authorIp' => [ |
| 114 |
'type' => 'String', |
| 115 |
'deprecationReason' => __( 'Use the ipAddress field on the edge between the comment and author', 'wp-graphql' ), |
| 116 |
'description' => static function () { |
| 117 |
return __( 'IP address for the author at the time of commenting.', 'wp-graphql' ); |
| 118 |
}, |
| 119 |
], |
| 120 |
'commentId' => [ |
| 121 |
'type' => 'Int', |
| 122 |
'description' => static function () { |
| 123 |
return __( 'ID for the comment, unique among comments.', 'wp-graphql' ); |
| 124 |
}, |
| 125 |
'deprecationReason' => static function () { |
| 126 |
return __( 'Deprecated in favor of databaseId', 'wp-graphql' ); |
| 127 |
}, |
| 128 |
], |
| 129 |
'content' => [ |
| 130 |
'type' => 'String', |
| 131 |
'description' => static function () { |
| 132 |
return __( 'Content of the comment.', 'wp-graphql' ); |
| 133 |
}, |
| 134 |
'args' => [ |
| 135 |
'format' => [ |
| 136 |
'type' => 'PostObjectFieldFormatEnum', |
| 137 |
'description' => static function () { |
| 138 |
return __( 'Format of the field output', 'wp-graphql' ); |
| 139 |
}, |
| 140 |
], |
| 141 |
], |
| 142 |
'resolve' => static function ( \WPGraphQL\Model\Comment $comment, $args ) { |
| 143 |
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) { |
| 144 |
return isset( $comment->contentRaw ) ? $comment->contentRaw : null; |
| 145 |
} else { |
| 146 |
return isset( $comment->contentRendered ) ? $comment->contentRendered : null; |
| 147 |
} |
| 148 |
}, |
| 149 |
], |
| 150 |
'date' => [ |
| 151 |
'type' => 'String', |
| 152 |
'description' => static function () { |
| 153 |
return __( 'Date the comment was posted in local time.', 'wp-graphql' ); |
| 154 |
}, |
| 155 |
], |
| 156 |
'dateGmt' => [ |
| 157 |
'type' => 'String', |
| 158 |
'description' => static function () { |
| 159 |
return __( 'Date the comment was posted in GMT.', 'wp-graphql' ); |
| 160 |
}, |
| 161 |
], |
| 162 |
'id' => [ |
| 163 |
'description' => static function () { |
| 164 |
return __( 'The globally unique identifier for the comment object', 'wp-graphql' ); |
| 165 |
}, |
| 166 |
], |
| 167 |
'isRestricted' => [ |
| 168 |
'type' => 'Boolean', |
| 169 |
'description' => static function () { |
| 170 |
return __( 'Whether the object is restricted from the current viewer', 'wp-graphql' ); |
| 171 |
}, |
| 172 |
], |
| 173 |
'karma' => [ |
| 174 |
'type' => 'Int', |
| 175 |
'description' => static function () { |
| 176 |
return __( 'Karma value for the comment.', 'wp-graphql' ); |
| 177 |
}, |
| 178 |
], |
| 179 |
'link' => [ |
| 180 |
'type' => 'String', |
| 181 |
'description' => static function () { |
| 182 |
return __( 'The permalink of the comment', 'wp-graphql' ); |
| 183 |
}, |
| 184 |
], |
| 185 |
'parentId' => [ |
| 186 |
'type' => 'ID', |
| 187 |
'description' => static function () { |
| 188 |
return __( 'The globally unique identifier of the parent comment node.', 'wp-graphql' ); |
| 189 |
}, |
| 190 |
], |
| 191 |
'parentDatabaseId' => [ |
| 192 |
'type' => 'Int', |
| 193 |
'description' => static function () { |
| 194 |
return __( 'The database id of the parent comment node or null if it is the root comment', 'wp-graphql' ); |
| 195 |
}, |
| 196 |
], |
| 197 |
'status' => [ |
| 198 |
'type' => 'CommentStatusEnum', |
| 199 |
'description' => static function () { |
| 200 |
return __( 'The approval status of the comment.', 'wp-graphql' ); |
| 201 |
}, |
| 202 |
], |
| 203 |
'type' => [ |
| 204 |
'type' => 'String', |
| 205 |
'description' => static function () { |
| 206 |
return __( 'Type of comment.', 'wp-graphql' ); |
| 207 |
}, |
| 208 |
], |
| 209 |
]; |
| 210 |
}, |
| 211 |
] |
| 212 |
); |
| 213 |
} |
| 214 |
} |
| 215 |
|