• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 15710056976

17 Jun 2025 02:27PM UTC coverage: 84.17% (-0.1%) from 84.287%
15710056976

push

github

actions-user
release: merge develop into master for v2.3.3

15925 of 18920 relevant lines covered (84.17%)

258.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.08
/src/Model/Comment.php
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 ) {
41✔
48
                $allowed_restricted_fields = [
41✔
49
                        'approved',
41✔
50
                        'comment_parent_id',
41✔
51
                        'comment_post_ID',
41✔
52
                        'commentAuthor',
41✔
53
                        'commentAuthorEmail',
41✔
54
                        'commentAuthorUrl',
41✔
55
                        'commentedOnId',
41✔
56
                        'commentId',
41✔
57
                        'contentRendered',
41✔
58
                        'databaseId',
41✔
59
                        'date',
41✔
60
                        'dateGmt',
41✔
61
                        'id',
41✔
62
                        'ID',
41✔
63
                        'isRestricted',
41✔
64
                        'karma',
41✔
65
                        'link',
41✔
66
                        'parentDatabaseId',
41✔
67
                        'parentId',
41✔
68
                        'status',
41✔
69
                        'type',
41✔
70
                        'uri',
41✔
71
                        'userId',
41✔
72
                ];
41✔
73

74
                $this->data = $comment;
41✔
75
                $owner      = ! empty( $comment->user_id ) ? absint( $comment->user_id ) : null;
41✔
76
                parent::__construct( 'moderate_comments', $allowed_restricted_fields, $owner );
41✔
77
        }
78

79
        /**
80
         * {@inheritDoc}
81
         */
82
        protected function is_private() {
41✔
83
                if ( empty( $this->data->comment_post_ID ) ) {
41✔
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 ) ) {
41✔
89
                        return false;
18✔
90
                }
91

92
                $commented_on = get_post( (int) $this->data->comment_post_ID );
29✔
93

94
                if ( ! $commented_on instanceof \WP_Post ) {
29✔
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() ) {
29✔
100
                        return true;
2✔
101
                }
102

103
                if ( 0 === absint( $this->data->comment_approved ) && ! current_user_can( 'moderate_comments' ) ) {
27✔
104
                        return true;
2✔
105
                }
106

107
                return false;
26✔
108
        }
109

110
        /**
111
         * {@inheritDoc}
112
         */
113
        protected function init() {
40✔
114
                if ( empty( $this->fields ) ) {
40✔
115
                        $this->fields = [
40✔
116
                                'agent'              => function () {
40✔
117
                                        return ! empty( $this->data->comment_agent ) ? $this->data->comment_agent : null;
5✔
118
                                },
40✔
119
                                'authorIp'           => function () {
40✔
120
                                        return ! empty( $this->data->comment_author_IP ) ? $this->data->comment_author_IP : null;
4✔
121
                                },
40✔
122
                                'commentAuthor'      => function () {
40✔
123
                                        return ! empty( $this->data->comment_author ) ? $this->data->comment_author : null;
2✔
124
                                },
40✔
125
                                'commentAuthorEmail' => function () {
40✔
126
                                        return current_user_can( 'moderate_comments' ) && ! empty( $this->data->comment_author_email ) ? $this->data->comment_author_email : null;
2✔
127
                                },
40✔
128
                                'commentAuthorUrl'   => function () {
40✔
129
                                        return ! empty( $this->data->comment_author_url ) ? $this->data->comment_author_url : null;
2✔
130
                                },
40✔
131
                                'comment_post_ID'    => function () {
40✔
132
                                        return ! empty( $this->data->comment_post_ID ) ? absint( $this->data->comment_post_ID ) : null;
6✔
133
                                },
40✔
134
                                'contentRaw'         => function () {
40✔
135
                                        return ! empty( $this->data->comment_content ) ? $this->data->comment_content : null;
×
136
                                },
40✔
137
                                'contentRendered'    => function () {
40✔
138
                                        $content = ! empty( $this->data->comment_content ) ? $this->data->comment_content : null;
30✔
139

140
                                        return $this->html_entity_decode( apply_filters( 'comment_text', $content, $this->data ), 'contentRendered', false );
30✔
141
                                },
40✔
142
                                'databaseId'         => function () {
40✔
143
                                        return ! empty( $this->data->comment_ID ) ? (int) $this->data->comment_ID : 0;
40✔
144
                                },
40✔
145
                                'date'               => function () {
40✔
146
                                        return ! empty( $this->data->comment_date ) ? $this->data->comment_date : null;
17✔
147
                                },
40✔
148
                                'dateGmt'            => function () {
40✔
149
                                        return ! empty( $this->data->comment_date_gmt ) ? $this->data->comment_date_gmt : null;
1✔
150
                                },
40✔
151
                                'id'                 => function () {
40✔
152
                                        return ! empty( $this->databaseId ) ? Relay::toGlobalId( 'comment', (string) $this->databaseId ) : null;
40✔
153
                                },
40✔
154
                                'karma'              => function () {
40✔
155
                                        return ! empty( $this->data->comment_karma ) ? $this->data->comment_karma : null;
5✔
156
                                },
40✔
157
                                'link'               => function () {
40✔
158
                                        $link = get_comment_link( $this->data );
4✔
159

160
                                        return ! empty( $link ) ? urldecode( $link ) : null;
4✔
161
                                },
40✔
162
                                'parentDatabaseId'   => function () {
40✔
163
                                        return ! empty( $this->data->comment_parent ) ? absint( $this->data->comment_parent ) : 0;
3✔
164
                                },
40✔
165
                                'parentId'           => function () {
40✔
166
                                        return ! empty( $this->parentDatabaseId ) ? Relay::toGlobalId( 'comment', (string) $this->parentDatabaseId ) : null;
×
167
                                },
40✔
168
                                'status'             => function () {
40✔
169
                                        if ( ! is_numeric( $this->data->comment_approved ) ) {
12✔
170
                                                return $this->data->comment_approved;
3✔
171
                                        }
172

173
                                        return '1' === $this->data->comment_approved ? 'approve' : 'hold';
11✔
174
                                },
40✔
175
                                'type'               => function () {
40✔
176
                                        return ! empty( $this->data->comment_type ) ? $this->data->comment_type : null;
×
177
                                },
40✔
178
                                'uri'                => function () {
40✔
179
                                        $uri = $this->link;
4✔
180

181
                                        return ! empty( $uri ) ? str_ireplace( home_url(), '', $uri ) : null;
4✔
182
                                },
40✔
183
                                'userId'             => function () {
40✔
184
                                        return ! empty( $this->data->user_id ) ? absint( $this->data->user_id ) : null;
9✔
185
                                },
40✔
186

187
                                // Aliases.
188
                                'comment_author'     => function () {
40✔
189
                                        return $this->commentAuthor;
×
190
                                },
40✔
191
                                'comment_author_url' => function () {
40✔
192
                                        return $this->commentAuthorUrl;
×
193
                                },
40✔
194
                                'comment_ID'         => function () {
40✔
195
                                        return $this->databaseId;
1✔
196
                                },
40✔
197
                                'comment_parent_id'  => function () {
40✔
198
                                        return $this->parentDatabaseId;
3✔
199
                                },
40✔
200

201
                                // Deprecated fields.
202
                                'approved'           => function () {
40✔
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
                                },
40✔
206
                                'commentId'          => function () {
40✔
207
                                        return $this->databaseId;
13✔
208
                                },
40✔
209
                        ];
40✔
210
                }
211
        }
212
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc