• 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

95.71
/src/Mutation/CommentDelete.php
1
<?php
2

3
namespace WPGraphQL\Mutation;
4

5
use GraphQL\Error\UserError;
6
use GraphQLRelay\Relay;
7
use WPGraphQL\Model\Comment;
8
use WPGraphQL\Utils\Utils;
9

10
class CommentDelete {
11
        /**
12
         * Registers the CommentDelete mutation.
13
         *
14
         * @return void
15
         * @throws \Exception
16
         */
17
        public static function register_mutation() {
593✔
18
                register_graphql_mutation(
593✔
19
                        'deleteComment',
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
                        'id'          => [
593✔
36
                                'type'        => [
593✔
37
                                        'non_null' => 'ID',
593✔
38
                                ],
593✔
39
                                'description' => static function () {
593✔
40
                                        return __( 'The deleted comment ID', 'wp-graphql' );
15✔
41
                                },
593✔
42
                        ],
593✔
43
                        'forceDelete' => [
593✔
44
                                'type'        => 'Boolean',
593✔
45
                                'description' => static function () {
593✔
46
                                        return __( 'Whether the comment should be force deleted instead of being moved to the trash', 'wp-graphql' );
15✔
47
                                },
593✔
48
                        ],
593✔
49
                ];
593✔
50
        }
51

52
        /**
53
         * Defines the mutation output field configuration.
54
         *
55
         * @return array<string,array<string,mixed>>
56
         */
57
        public static function get_output_fields() {
593✔
58
                return [
593✔
59
                        'deletedId' => [
593✔
60
                                'type'        => 'Id',
593✔
61
                                'description' => static function () {
593✔
62
                                        return __( 'The deleted comment ID', 'wp-graphql' );
15✔
63
                                },
593✔
64
                                'resolve'     => static function ( $payload ) {
593✔
65
                                        $deleted = (object) $payload['commentObject'];
1✔
66

67
                                        return ! empty( $deleted->comment_ID ) ? Relay::toGlobalId( 'comment', $deleted->comment_ID ) : null;
1✔
68
                                },
593✔
69
                        ],
593✔
70
                        'comment'   => [
593✔
71
                                'type'        => 'Comment',
593✔
72
                                'description' => static function () {
593✔
73
                                        return __( 'The deleted comment object', 'wp-graphql' );
15✔
74
                                },
593✔
75
                                'resolve'     => static function ( $payload ) {
593✔
76
                                        return $payload['commentObject'] ? $payload['commentObject'] : null;
1✔
77
                                },
593✔
78
                        ],
593✔
79
                ];
593✔
80
        }
81

82
        /**
83
         * Defines the mutation data modification closure.
84
         *
85
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
86
         */
87
        public static function mutate_and_get_payload() {
593✔
88
                return static function ( $input ) {
593✔
89
                        // Get the database ID for the comment.
90
                        $comment_id = Utils::get_database_id_from_id( $input['id'] );
1✔
91

92
                        // Get the post object before deleting it.
93
                        $comment_before_delete = ! empty( $comment_id ) ? get_comment( $comment_id ) : false;
1✔
94

95
                        if ( empty( $comment_before_delete ) ) {
1✔
96
                                throw new UserError( esc_html__( 'The Comment could not be deleted', 'wp-graphql' ) );
×
97
                        }
98

99
                        /**
100
                         * Stop now if a user isn't allowed to delete the comment
101
                         */
102
                        $user_id = $comment_before_delete->user_id;
1✔
103

104
                        // Prevent comment deletions by default
105
                        $not_allowed = true;
1✔
106

107
                        // If the current user can moderate comments proceed
108
                        if ( current_user_can( 'moderate_comments' ) ) {
1✔
109
                                $not_allowed = false;
×
110
                        } else {
111
                                // Get the current user id
112
                                $current_user_id = absint( get_current_user_id() );
1✔
113
                                // If the current user ID is the same as the comment author's ID, then the
114
                                // current user is the comment author and can delete the comment
115
                                if ( 0 !== $current_user_id && absint( $user_id ) === $current_user_id ) {
1✔
116
                                        $not_allowed = false;
1✔
117
                                }
118
                        }
119

120
                        /**
121
                         * If the mutation has been prevented
122
                         */
123
                        if ( true === $not_allowed ) {
1✔
124
                                throw new UserError( esc_html__( 'Sorry, you are not allowed to delete this comment.', 'wp-graphql' ) );
×
125
                        }
126

127
                        /**
128
                         * Check if we should force delete or not
129
                         */
130
                        $force_delete = ! empty( $input['forceDelete'] ) && true === $input['forceDelete'];
1✔
131

132
                        $comment_before_delete = new Comment( $comment_before_delete );
1✔
133

134
                        /**
135
                         * Delete the comment
136
                         */
137
                        wp_delete_comment( (int) $comment_id, $force_delete );
1✔
138

139
                        return [
1✔
140
                                'commentObject' => $comment_before_delete,
1✔
141
                        ];
1✔
142
                };
593✔
143
        }
144
}
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