• 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

92.97
/src/Mutation/CommentCreate.php
1
<?php
2

3
namespace WPGraphQL\Mutation;
4

5
use GraphQL\Error\UserError;
6
use GraphQL\Type\Definition\ResolveInfo;
7
use WPGraphQL\AppContext;
8
use WPGraphQL\Data\CommentMutation;
9

10
class CommentCreate {
11
        /**
12
         * Registers the CommentCreate mutation.
13
         *
14
         * @return void
15
         * @throws \Exception
16
         */
17
        public static function register_mutation() {
593✔
18
                register_graphql_mutation(
593✔
19
                        'createComment',
593✔
20
                        [
593✔
21
                                'inputFields'         => self::get_input_fields(),
593✔
22
                                'outputFields'        => self::get_output_fields(),
593✔
23
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
593✔
24
                        ]
593✔
25
                );
593✔
26
        }
27

28
        /**
29
         * Defines the mutation input field configuration.
30
         *
31
         * @return array<string,array<string,mixed>>
32
         */
33
        public static function get_input_fields() {
593✔
34
                return [
593✔
35
                        'approved'    => [
593✔
36
                                'type'              => 'String',
593✔
37
                                'description'       => static function () {
593✔
38
                                        return __( 'The approval status of the comment.', 'wp-graphql' );
15✔
39
                                },
593✔
40
                                'deprecationReason' => static function () {
593✔
41
                                        return __( 'Deprecated in favor of the status field', 'wp-graphql' );
15✔
42
                                },
593✔
43
                        ],
593✔
44
                        'author'      => [
593✔
45
                                'type'        => 'String',
593✔
46
                                'description' => static function () {
593✔
47
                                        return __( 'The name of the comment\'s author.', 'wp-graphql' );
15✔
48
                                },
593✔
49
                        ],
593✔
50
                        'authorEmail' => [
593✔
51
                                'type'        => 'String',
593✔
52
                                'description' => static function () {
593✔
53
                                        return __( 'The email of the comment\'s author.', 'wp-graphql' );
15✔
54
                                },
593✔
55
                        ],
593✔
56
                        'authorUrl'   => [
593✔
57
                                'type'        => 'String',
593✔
58
                                'description' => static function () {
593✔
59
                                        return __( 'The url of the comment\'s author.', 'wp-graphql' );
15✔
60
                                },
593✔
61
                        ],
593✔
62
                        'commentOn'   => [
593✔
63
                                'type'        => 'Int',
593✔
64
                                'description' => static function () {
593✔
65
                                        return __( 'The database ID of the post object the comment belongs to.', 'wp-graphql' );
15✔
66
                                },
593✔
67
                        ],
593✔
68
                        'content'     => [
593✔
69
                                'type'        => 'String',
593✔
70
                                'description' => static function () {
593✔
71
                                        return __( 'Content of the comment.', 'wp-graphql' );
15✔
72
                                },
593✔
73
                        ],
593✔
74
                        'date'        => [
593✔
75
                                'type'        => 'String',
593✔
76
                                'description' => static function () {
593✔
77
                                        return __( 'The date of the object. Preferable to enter as year/month/day ( e.g. 01/31/2017 ) as it will rearrange date as fit if it is not specified. Incomplete dates may have unintended results for example, "2017" as the input will use current date with timestamp 20:17 ', 'wp-graphql' );
15✔
78
                                },
593✔
79
                        ],
593✔
80
                        'parent'      => [
593✔
81
                                'type'        => 'ID',
593✔
82
                                'description' => static function () {
593✔
83
                                        return __( 'Parent comment ID of current comment.', 'wp-graphql' );
15✔
84
                                },
593✔
85
                        ],
593✔
86
                        'status'      => [
593✔
87
                                'type'        => 'CommentStatusEnum',
593✔
88
                                'description' => static function () {
593✔
89
                                        return __( 'The approval status of the comment', 'wp-graphql' );
15✔
90
                                },
593✔
91
                        ],
593✔
92
                        'type'        => [
593✔
93
                                'type'        => 'String',
593✔
94
                                'description' => static function () {
593✔
95
                                        return __( 'Type of comment.', 'wp-graphql' );
15✔
96
                                },
593✔
97
                        ],
593✔
98
                ];
593✔
99
        }
100

101
        /**
102
         * Defines the mutation output field configuration.
103
         *
104
         * @return array<string,array<string,mixed>>
105
         */
106
        public static function get_output_fields() {
593✔
107
                return [
593✔
108
                        'comment' => [
593✔
109
                                'type'        => 'Comment',
593✔
110
                                'description' => static function () {
593✔
111
                                        return __( 'The comment that was created', 'wp-graphql' );
15✔
112
                                },
593✔
113
                                'resolve'     => static function ( $payload, $args, AppContext $context ) {
593✔
114
                                        if ( ! isset( $payload['id'] ) || ! absint( $payload['id'] ) ) {
6✔
115
                                                return null;
×
116
                                        }
117

118
                                        return $context->get_loader( 'comment' )->load_deferred( absint( $payload['id'] ) );
6✔
119
                                },
593✔
120
                        ],
593✔
121
                        /**
122
                         * Comments can be created by non-authenticated users, but if the comment is not approved
123
                         * the user will not have access to the comment in response to the mutation.
124
                         *
125
                         * This field allows for the mutation to respond with a success message that the
126
                         * comment was indeed created, even if it cannot be returned in the response to respect
127
                         * server privacy.
128
                         *
129
                         * If the success comes back as true, the client can then use that response to
130
                         * dictate if they should use the input values as an optimistic response to the mutation
131
                         * and store in the cache, localStorage, cookie or whatever else so that the
132
                         * client can see their comment while it's still pending approval.
133
                         */
134
                        'success' => [
593✔
135
                                'type'        => 'Boolean',
593✔
136
                                'description' => static function () {
593✔
137
                                        return __( 'Whether the mutation succeeded. If the comment is not approved, the server will not return the comment to a non authenticated user, but a success message can be returned if the create succeeded, and the client can optimistically add the comment to the client cache', 'wp-graphql' );
15✔
138
                                },
593✔
139
                        ],
593✔
140
                ];
593✔
141
        }
142

143
        /**
144
         * Defines the mutation data modification closure.
145
         *
146
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
147
         */
148
        public static function mutate_and_get_payload() {
593✔
149
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
593✔
150

151
                        /**
152
                         * Throw an exception if there's no input
153
                         */
154
                        if ( ( empty( $input ) || ! is_array( $input ) ) ) {
4✔
155
                                throw new UserError( esc_html__( 'Mutation not processed. There was no input for the mutation or the comment_object was invalid', 'wp-graphql' ) );
×
156
                        }
157

158
                        $commented_on = get_post( absint( $input['commentOn'] ) );
4✔
159

160
                        if ( empty( $commented_on ) ) {
4✔
161
                                throw new UserError( esc_html__( 'The ID of the node to comment on is invalid', 'wp-graphql' ) );
×
162
                        }
163

164
                        /**
165
                         * Stop if post not open to comments
166
                         */
167
                        if ( empty( $input['commentOn'] ) || 'closed' === $commented_on->comment_status ) {
4✔
168
                                throw new UserError( esc_html__( 'Sorry, this post is closed to comments at the moment', 'wp-graphql' ) );
×
169
                        }
170

171
                        if ( '1' === get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
4✔
172
                                throw new UserError( esc_html__( 'This site requires you to be logged in to leave a comment', 'wp-graphql' ) );
1✔
173
                        }
174

175
                        /**
176
                         * Map all of the args from GraphQL to WordPress friendly args array
177
                         */
178
                        $comment_args = [
3✔
179
                                'comment_author_url' => '',
3✔
180
                                'comment_type'       => '',
3✔
181
                                'comment_parent'     => 0,
3✔
182
                                'user_id'            => 0,
3✔
183
                                'comment_date'       => gmdate( 'Y-m-d H:i:s' ),
3✔
184
                        ];
3✔
185

186
                        CommentMutation::prepare_comment_object( $input, $comment_args, 'createComment' );
3✔
187

188
                        /**
189
                         * Insert the comment and retrieve the ID
190
                         */
191
                        $comment_id = wp_new_comment( $comment_args, true );
3✔
192

193
                        /**
194
                         * Throw an exception if the comment failed to be created
195
                         */
196
                        if ( is_wp_error( $comment_id ) ) {
3✔
197
                                $error_message = $comment_id->get_error_message();
×
198
                                if ( ! empty( $error_message ) ) {
×
199
                                        throw new UserError( esc_html( $error_message ) );
×
200
                                } else {
201
                                        throw new UserError( esc_html__( 'The object failed to create but no error was provided', 'wp-graphql' ) );
×
202
                                }
203
                        }
204

205
                        /**
206
                         * If the $comment_id is empty, we should throw an exception
207
                         */
208
                        if ( empty( $comment_id ) ) {
3✔
209
                                throw new UserError( esc_html__( 'The object failed to create', 'wp-graphql' ) );
×
210
                        }
211

212
                        /**
213
                         * This updates additional data not part of the comments table ( commentmeta, other relations, etc )
214
                         *
215
                         * The input for the commentMutation will be passed, along with the $new_comment_id for the
216
                         * comment that was created so that relations can be set, meta can be updated, etc.
217
                         */
218
                        CommentMutation::update_additional_comment_data( $comment_id, $input, 'createComment', $context, $info );
3✔
219

220
                        /**
221
                         * Return the comment object
222
                         */
223
                        return [
3✔
224
                                'id'      => $comment_id,
3✔
225
                                'success' => true,
3✔
226
                        ];
3✔
227
                };
593✔
228
        }
229
}
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