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

wp-graphql / wp-graphql / 14716683875

28 Apr 2025 07:58PM UTC coverage: 84.287% (+1.6%) from 82.648%
14716683875

push

github

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

15905 of 18870 relevant lines covered (84.29%)

257.23 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
class Comment extends Model {
40

41
        /**
42
         * Stores the incoming WP_Comment object to be modeled
43
         *
44
         * @var \WP_Comment $data
45
         */
46
        protected $data;
47

48
        /**
49
         * Comment constructor.
50
         *
51
         * @param \WP_Comment $comment The incoming WP_Comment to be modeled
52
         */
53
        public function __construct( WP_Comment $comment ) {
41✔
54
                $allowed_restricted_fields = [
41✔
55
                        'approved',
41✔
56
                        'comment_parent_id',
41✔
57
                        'comment_post_ID',
41✔
58
                        'commentAuthor',
41✔
59
                        'commentAuthorEmail',
41✔
60
                        'commentAuthorUrl',
41✔
61
                        'commentedOnId',
41✔
62
                        'commentId',
41✔
63
                        'contentRendered',
41✔
64
                        'databaseId',
41✔
65
                        'date',
41✔
66
                        'dateGmt',
41✔
67
                        'id',
41✔
68
                        'ID',
41✔
69
                        'isRestricted',
41✔
70
                        'karma',
41✔
71
                        'link',
41✔
72
                        'parentDatabaseId',
41✔
73
                        'parentId',
41✔
74
                        'status',
41✔
75
                        'type',
41✔
76
                        'uri',
41✔
77
                        'userId',
41✔
78
                ];
41✔
79

80
                $this->data = $comment;
41✔
81
                $owner      = ! empty( $comment->user_id ) ? absint( $comment->user_id ) : null;
41✔
82
                parent::__construct( 'moderate_comments', $allowed_restricted_fields, $owner );
41✔
83
        }
84

85
        /**
86
         * {@inheritDoc}
87
         */
88
        protected function is_private() {
41✔
89
                if ( empty( $this->data->comment_post_ID ) ) {
41✔
90
                        return true;
×
91
                }
92

93
                // if the current user is the author of the comment, the comment should not be private
94
                if ( 0 !== wp_get_current_user()->ID && absint( $this->data->user_id ) === absint( wp_get_current_user()->ID ) ) {
41✔
95
                        return false;
18✔
96
                }
97

98
                $commented_on = get_post( (int) $this->data->comment_post_ID );
29✔
99

100
                if ( ! $commented_on instanceof \WP_Post ) {
29✔
101
                        return true;
×
102
                }
103

104
                // A comment is considered private if it is attached to a private post.
105
                if ( true === ( new Post( $commented_on ) )->is_private() ) {
29✔
106
                        return true;
2✔
107
                }
108

109
                if ( 0 === absint( $this->data->comment_approved ) && ! current_user_can( 'moderate_comments' ) ) {
27✔
110
                        return true;
2✔
111
                }
112

113
                return false;
26✔
114
        }
115

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

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

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

179
                                        return '1' === $this->data->comment_approved ? 'approve' : 'hold';
11✔
180
                                },
40✔
181
                                'type'               => function () {
40✔
182
                                        return ! empty( $this->data->comment_type ) ? $this->data->comment_type : null;
×
183
                                },
40✔
184
                                'uri'                => function () {
40✔
185
                                        $uri = $this->link;
4✔
186

187
                                        return ! empty( $uri ) ? str_ireplace( home_url(), '', $uri ) : null;
4✔
188
                                },
40✔
189
                                'userId'             => function () {
40✔
190
                                        return ! empty( $this->data->user_id ) ? absint( $this->data->user_id ) : null;
9✔
191
                                },
40✔
192

193
                                // Aliases.
194
                                'comment_author'     => function () {
40✔
195
                                        return $this->commentAuthor;
×
196
                                },
40✔
197
                                'comment_author_url' => function () {
40✔
198
                                        return $this->commentAuthorUrl;
×
199
                                },
40✔
200
                                'comment_ID'         => function () {
40✔
201
                                        return $this->databaseId;
1✔
202
                                },
40✔
203
                                'comment_parent_id'  => function () {
40✔
204
                                        return $this->parentDatabaseId;
3✔
205
                                },
40✔
206

207
                                // Deprecated fields.
208
                                'approved'           => function () {
40✔
209
                                        _doing_it_wrong( __METHOD__, 'The approved field is deprecated in favor of `status`', '1.13.0' );
×
210
                                        return ! empty( $this->data->comment_approved ) && 'hold' !== $this->data->comment_approved;
×
211
                                },
40✔
212
                                'commentId'          => function () {
40✔
213
                                        return $this->databaseId;
13✔
214
                                },
40✔
215
                        ];
40✔
216
                }
217
        }
218
}
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