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

wp-graphql / wp-graphql / #814

19 Sep 2024 05:27PM UTC coverage: 84.035% (-0.1%) from 84.174%
#814

push

php-coveralls

web-flow
Merge pull request #3215 from wp-graphql/release/v1.29.0

release: v1.29.0

47 of 55 new or added lines in 5 files covered. (85.45%)

27 existing lines in 1 file now uncovered.

12533 of 14914 relevant lines covered (84.04%)

308.34 hits per line

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

92.13
/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 int    $comment_ID
12
 * @property int    $comment_parent_id
13
 * @property int    $commentId
14
 * @property int    $parentDatabaseId
15
 * @property int    $userId
16
 * @property string $agent
17
 * @property string $authorIp
18
 * @property string $comment_author
19
 * @property string $comment_author_url
20
 * @property string $commentAuthor
21
 * @property string $commentAuthorUrl
22
 * @property string $commentAuthorEmail
23
 * @property string $contentRaw
24
 * @property string $contentRendered
25
 * @property string $date
26
 * @property string $dateGmt
27
 * @property string $id
28
 * @property string $karma
29
 * @property string $link
30
 * @property string $parentId
31
 * @property string $status
32
 * @property string $type
33
 * @property string $uri
34
 *
35
 * @package WPGraphQL\Model
36
 */
37
class Comment extends Model {
38

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

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

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

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

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

96
                $commented_on = get_post( (int) $this->data->comment_post_ID );
26✔
97

98
                if ( ! $commented_on instanceof \WP_Post ) {
26✔
99
                        return true;
×
100
                }
101

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

107
                if ( 0 === absint( $this->data->comment_approved ) && ! current_user_can( 'moderate_comments' ) ) {
24✔
108
                        return true;
1✔
109
                }
110

111
                return false;
23✔
112
        }
113

114
        /**
115
         * {@inheritDoc}
116
         */
117
        protected function init() {
118
                if ( empty( $this->fields ) ) {
36✔
119
                        $this->fields = [
36✔
120
                                'id'                 => function () {
36✔
121
                                        return ! empty( $this->data->comment_ID ) ? Relay::toGlobalId( 'comment', $this->data->comment_ID ) : null;
36✔
122
                                },
36✔
123
                                'commentId'          => function () {
36✔
124
                                        return ! empty( $this->data->comment_ID ) ? absint( $this->data->comment_ID ) : 0;
13✔
125
                                },
36✔
126
                                'databaseId'         => function () {
36✔
127
                                        return ! empty( $this->data->comment_ID ) ? $this->data->comment_ID : 0;
24✔
128
                                },
36✔
129
                                'commentAuthorEmail' => function () {
36✔
130
                                        return current_user_can( 'moderate_comments' ) && ! empty( $this->data->comment_author_email ) ? $this->data->comment_author_email : null;
2✔
131
                                },
36✔
132
                                'comment_ID'         => function () {
36✔
133
                                        return ! empty( $this->data->comment_ID ) ? absint( $this->data->comment_ID ) : 0;
1✔
134
                                },
36✔
135
                                'comment_post_ID'    => function () {
36✔
136
                                        return ! empty( $this->data->comment_post_ID ) ? absint( $this->data->comment_post_ID ) : null;
6✔
137
                                },
36✔
138
                                'comment_parent_id'  => function () {
36✔
139
                                        return ! empty( $this->data->comment_parent ) ? absint( $this->data->comment_parent ) : 0;
3✔
140
                                },
36✔
141
                                'parentDatabaseId'   => function () {
36✔
142
                                        return ! empty( $this->data->comment_parent ) ? absint( $this->data->comment_parent ) : 0;
×
143
                                },
36✔
144
                                'parentId'           => function () {
36✔
145
                                        return ! empty( $this->comment_parent_id ) ? Relay::toGlobalId( 'comment', $this->data->comment_parent ) : null;
×
146
                                },
36✔
147
                                'comment_author'     => function () {
36✔
NEW
148
                                        return ! empty( $this->data->comment_author ) ? $this->data->comment_author : null;
×
149
                                },
36✔
150
                                'comment_author_url' => function () {
36✔
NEW
151
                                        return ! empty( $this->data->comment_author_url ) ? $this->data->comment_author_url : null;
×
152
                                },
36✔
153
                                'commentAuthor'      => function () {
36✔
154
                                        return ! empty( $this->data->comment_author ) ? $this->data->comment_author : null;
2✔
155
                                },
36✔
156
                                'commentAuthorUrl'   => function () {
36✔
157
                                        return ! empty( $this->data->comment_author_url ) ? $this->data->comment_author_url : null;
2✔
158
                                },
36✔
159
                                'authorIp'           => function () {
36✔
160
                                        return ! empty( $this->data->comment_author_IP ) ? $this->data->comment_author_IP : null;
4✔
161
                                },
36✔
162
                                'date'               => function () {
36✔
163
                                        return ! empty( $this->data->comment_date ) ? $this->data->comment_date : null;
16✔
164
                                },
36✔
165
                                'dateGmt'            => function () {
36✔
166
                                        return ! empty( $this->data->comment_date_gmt ) ? $this->data->comment_date_gmt : null;
1✔
167
                                },
36✔
168
                                'contentRaw'         => function () {
36✔
169
                                        return ! empty( $this->data->comment_content ) ? $this->data->comment_content : null;
×
170
                                },
36✔
171
                                'contentRendered'    => function () {
36✔
172
                                        $content = ! empty( $this->data->comment_content ) ? $this->data->comment_content : null;
27✔
173

174
                                        return $this->html_entity_decode( apply_filters( 'comment_text', $content, $this->data ), 'contentRendered', false );
27✔
175
                                },
36✔
176
                                'karma'              => function () {
36✔
177
                                        return ! empty( $this->data->comment_karma ) ? $this->data->comment_karma : null;
5✔
178
                                },
36✔
179
                                'link'               => function () {
36✔
180
                                        $link = get_comment_link( $this->data );
4✔
181

182
                                        return ! empty( $link ) ? urldecode( $link ) : null;
4✔
183
                                },
36✔
184
                                'approved'           => function () {
36✔
185
                                        _doing_it_wrong( __METHOD__, 'The approved field is deprecated in favor of `status`', '1.13.0' );
×
186
                                        return ! empty( $this->data->comment_approved ) && 'hold' !== $this->data->comment_approved;
×
187
                                },
36✔
188
                                'status'             => function () {
36✔
189
                                        if ( ! is_numeric( $this->data->comment_approved ) ) {
9✔
190
                                                return $this->data->comment_approved;
1✔
191
                                        }
192

193
                                        return '1' === $this->data->comment_approved ? 'approve' : 'hold';
9✔
194
                                },
36✔
195
                                'agent'              => function () {
36✔
196
                                        return ! empty( $this->data->comment_agent ) ? $this->data->comment_agent : null;
5✔
197
                                },
36✔
198
                                'type'               => function () {
36✔
199
                                        return ! empty( $this->data->comment_type ) ? $this->data->comment_type : null;
×
200
                                },
36✔
201
                                'uri'                => function () {
36✔
202
                                        $uri = $this->link;
4✔
203

204
                                        return ! empty( $uri ) ? str_ireplace( home_url(), '', $uri ) : null;
4✔
205
                                },
36✔
206
                                'userId'             => function () {
36✔
207
                                        return ! empty( $this->data->user_id ) ? absint( $this->data->user_id ) : null;
8✔
208
                                },
36✔
209
                        ];
36✔
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

© 2026 Coveralls, Inc