PluginProbe
WPGraphQL / trunk
WPGraphQL vtrunk
2.22.3 2.22.2 2.22.1 2.22.0 2.21.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.15.0 2.14.1 2.14.0 2.13.0 2.2.0 2.3.0 2.3.3 2.3.6 2.3.8 2.5.0 2.5.1 2.5.2 2.5.3 All 177 releases
wp-graphql / src / Model / Comment.php

Comment.php in WPGraphQL trunk, at src/Model/Comment.php

213 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPGraphQL\Model;
4
5 use GraphQLRelay\Relay;
6 use WP_Comment;
7
8 /**
9 * Class Comment - Models data for Comments
10 *
11 * @property ?string $agent
12 * @property ?string $authorIp
13 * @property ?string $commentAuthor
14 * @property ?string $commentAuthorEmail
15 * @property ?string $commentAuthorUrl
16 * @property ?string $contentRaw
17 * @property ?string $contentRendered
18 * @property int $databaseId
19 * @property ?string $date
20 * @property ?string $dateGmt
21 * @property ?string $id
22 * @property ?string $karma
23 * @property ?string $link
24 * @property int $parentDatabaseId
25 * @property string $parentId
26 * @property ?string $status
27 * @property ?string $type
28 * @property ?string $uri
29 * @property ?int $userId
30 *
31 * Aliases:
32 * @property ?string $comment_author
33 * @property ?string $comment_author_url
34 * @property int $comment_ID
35 * @property int $comment_parent_id
36 *
37 * @package WPGraphQL\Model
38 *
39 * @extends \WPGraphQL\Model\Model<\WP_Comment>
40 */
41 class Comment extends Model {
42 /**
43 * Comment constructor.
44 *
45 * @param \WP_Comment $comment The incoming WP_Comment to be modeled
46 */
47 public function __construct( WP_Comment $comment ) {
48 $allowed_restricted_fields = [
49 'approved',
50 'comment_parent_id',
51 'comment_post_ID',
52 'commentAuthor',
53 'commentAuthorEmail',
54 'commentAuthorUrl',
55 'commentedOnId',
56 'commentId',
57 'contentRendered',
58 'databaseId',
59 'date',
60 'dateGmt',
61 'id',
62 'ID',
63 'isRestricted',
64 'karma',
65 'link',
66 'parentDatabaseId',
67 'parentId',
68 'status',
69 'type',
70 'uri',
71 'userId',
72 ];
73
74 $this->data = $comment;
75 $owner = ! empty( $comment->user_id ) ? absint( $comment->user_id ) : null;
76 parent::__construct( 'moderate_comments', $allowed_restricted_fields, $owner );
77 }
78
79 /**
80 * {@inheritDoc}
81 */
82 protected function is_private() {
83 if ( empty( $this->data->comment_post_ID ) ) {
84 return true;
85 }
86
87 // if the current user is the author of the comment, the comment should not be private
88 if ( 0 !== wp_get_current_user()->ID && absint( $this->data->user_id ) === absint( wp_get_current_user()->ID ) ) {
89 return false;
90 }
91
92 $commented_on = get_post( (int) $this->data->comment_post_ID );
93
94 if ( ! $commented_on instanceof \WP_Post ) {
95 return true;
96 }
97
98 // A comment is considered private if it is attached to a private post.
99 if ( true === ( new Post( $commented_on ) )->is_private() ) {
100 return true;
101 }
102
103 if ( 0 === absint( $this->data->comment_approved ) && ! current_user_can( 'moderate_comments' ) ) {
104 return true;
105 }
106
107 return false;
108 }
109
110 /**
111 * {@inheritDoc}
112 */
113 protected function init() {
114 if ( empty( $this->fields ) ) {
115 $this->fields = [
116 'agent' => function () {
117 return ! empty( $this->data->comment_agent ) ? $this->data->comment_agent : null;
118 },
119 'authorIp' => function () {
120 return ! empty( $this->data->comment_author_IP ) ? $this->data->comment_author_IP : null;
121 },
122 'commentAuthor' => function () {
123 return ! empty( $this->data->comment_author ) ? $this->data->comment_author : null;
124 },
125 'commentAuthorEmail' => function () {
126 return current_user_can( 'moderate_comments' ) && ! empty( $this->data->comment_author_email ) ? $this->data->comment_author_email : null;
127 },
128 'commentAuthorUrl' => function () {
129 return ! empty( $this->data->comment_author_url ) ? $this->data->comment_author_url : null;
130 },
131 'comment_post_ID' => function () {
132 return ! empty( $this->data->comment_post_ID ) ? absint( $this->data->comment_post_ID ) : null;
133 },
134 'contentRaw' => function () {
135 return ! empty( $this->data->comment_content ) ? $this->data->comment_content : null;
136 },
137 'contentRendered' => function () {
138 $content = ! empty( $this->data->comment_content ) ? $this->data->comment_content : null;
139
140 return $this->html_entity_decode( apply_filters( 'comment_text', $content, $this->data ), 'contentRendered', false );
141 },
142 'databaseId' => function () {
143 return ! empty( $this->data->comment_ID ) ? (int) $this->data->comment_ID : 0;
144 },
145 'date' => function () {
146 return ! empty( $this->data->comment_date ) ? $this->data->comment_date : null;
147 },
148 'dateGmt' => function () {
149 return ! empty( $this->data->comment_date_gmt ) ? $this->data->comment_date_gmt : null;
150 },
151 'id' => function () {
152 return ! empty( $this->databaseId ) ? Relay::toGlobalId( 'comment', (string) $this->databaseId ) : null;
153 },
154 'karma' => function () {
155 return ! empty( $this->data->comment_karma ) ? $this->data->comment_karma : null;
156 },
157 'link' => function () {
158 $link = get_comment_link( $this->data );
159
160 return ! empty( $link ) ? urldecode( $link ) : null;
161 },
162 'parentDatabaseId' => function () {
163 return ! empty( $this->data->comment_parent ) ? absint( $this->data->comment_parent ) : 0;
164 },
165 'parentId' => function () {
166 return ! empty( $this->parentDatabaseId ) ? Relay::toGlobalId( 'comment', (string) $this->parentDatabaseId ) : null;
167 },
168 'status' => function () {
169 if ( ! is_numeric( $this->data->comment_approved ) ) {
170 return $this->data->comment_approved;
171 }
172
173 return '1' === $this->data->comment_approved ? 'approve' : 'hold';
174 },
175 'type' => function () {
176 return ! empty( $this->data->comment_type ) ? $this->data->comment_type : null;
177 },
178 'uri' => function () {
179 $uri = $this->link;
180
181 return ! empty( $uri ) ? str_ireplace( home_url(), '', $uri ) : null;
182 },
183 'userId' => function () {
184 return ! empty( $this->data->user_id ) ? absint( $this->data->user_id ) : null;
185 },
186
187 // Aliases.
188 'comment_author' => function () {
189 return $this->commentAuthor;
190 },
191 'comment_author_url' => function () {
192 return $this->commentAuthorUrl;
193 },
194 'comment_ID' => function () {
195 return $this->databaseId;
196 },
197 'comment_parent_id' => function () {
198 return $this->parentDatabaseId;
199 },
200
201 // Deprecated fields.
202 'approved' => function () {
203 _doing_it_wrong( __METHOD__, 'The approved field is deprecated in favor of `status`', '1.13.0' );
204 return ! empty( $this->data->comment_approved ) && 'hold' !== $this->data->comment_approved;
205 },
206 'commentId' => function () {
207 return $this->databaseId;
208 },
209 ];
210 }
211 }
212 }
213