• 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

94.34
/src/Mutation/CommentUpdate.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
use WPGraphQL\Utils\Utils;
10

11
/**
12
 * Class CommentUpdate
13
 *
14
 * @package WPGraphQL\Mutation
15
 */
16
class CommentUpdate {
17
        /**
18
         * Registers the CommentUpdate mutation.
19
         *
20
         * @return void
21
         * @throws \Exception
22
         */
23
        public static function register_mutation() {
593✔
24
                register_graphql_mutation(
593✔
25
                        'updateComment',
593✔
26
                        [
593✔
27
                                'inputFields'         => self::get_input_fields(),
593✔
28
                                'outputFields'        => self::get_output_fields(),
593✔
29
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
593✔
30
                        ]
593✔
31
                );
593✔
32
        }
33

34
        /**
35
         * Defines the mutation input field configuration.
36
         *
37
         * @return array<string,array<string,mixed>>
38
         */
39
        public static function get_input_fields() {
593✔
40
                return array_merge(
593✔
41
                        CommentCreate::get_input_fields(),
593✔
42
                        [
593✔
43
                                'id' => [
593✔
44
                                        'type'        => [
593✔
45
                                                'non_null' => 'ID',
593✔
46
                                        ],
593✔
47
                                        'description' => static function () {
593✔
48
                                                return __( 'The ID of the comment being updated.', 'wp-graphql' );
15✔
49
                                        },
593✔
50
                                ],
593✔
51
                        ]
593✔
52
                );
593✔
53
        }
54

55
        /**
56
         * Defines the mutation output field configuration.
57
         *
58
         * @return array<string,array<string,mixed>>
59
         */
60
        public static function get_output_fields() {
593✔
61
                return CommentCreate::get_output_fields();
593✔
62
        }
63

64
        /**
65
         * Defines the mutation data modification closure.
66
         *
67
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
68
         */
69
        public static function mutate_and_get_payload() {
593✔
70
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
593✔
71
                        // Get the database ID for the comment.
72
                        $comment_id = ! empty( $input['id'] ) ? Utils::get_database_id_from_id( $input['id'] ) : null;
4✔
73

74
                        // Get the args from the existing comment.
75
                        $comment_args = ! empty( $comment_id ) ? get_comment( $comment_id, ARRAY_A ) : null;
4✔
76

77
                        if ( empty( $comment_id ) || empty( $comment_args ) ) {
4✔
78
                                throw new UserError( esc_html__( 'The Comment could not be updated', 'wp-graphql' ) );
×
79
                        }
80

81
                        /**
82
                         * Map all of the args from GraphQL to WordPress friendly args array
83
                         */
84
                        $user_id          = $comment_args['user_id'] ?? null;
4✔
85
                        $raw_comment_args = $comment_args;
4✔
86
                        CommentMutation::prepare_comment_object( $input, $comment_args, 'update', true );
4✔
87

88
                        // Prevent comment deletions by default
89
                        $not_allowed = true;
4✔
90

91
                        // If the current user can moderate comments proceed
92
                        if ( current_user_can( 'moderate_comments' ) ) {
4✔
93
                                $not_allowed = false;
2✔
94
                        } else {
95
                                // Get the current user id
96
                                $current_user_id = absint( get_current_user_id() );
4✔
97
                                // If the current user ID is the same as the comment author's ID, then the
98
                                // current user is the comment author and can delete the comment
99
                                if ( 0 !== $current_user_id && absint( $user_id ) === $current_user_id ) {
4✔
100
                                        $not_allowed = false;
2✔
101
                                }
102
                        }
103

104
                        /**
105
                         * If the mutation has been prevented
106
                         */
107
                        if ( true === $not_allowed ) {
4✔
108
                                throw new UserError( esc_html__( 'Sorry, you are not allowed to update this comment.', 'wp-graphql' ) );
2✔
109
                        }
110

111
                        // If there are no changes between the existing comment and the incoming comment
112
                        if ( $comment_args === $raw_comment_args ) {
4✔
113
                                throw new UserError( esc_html__( 'No changes have been provided to the comment.', 'wp-graphql' ) );
×
114
                        }
115

116
                        /**
117
                         * Update comment
118
                         * $success   int   1 on success and 0 on fail
119
                         */
120
                        $success = wp_update_comment( $comment_args, true );
4✔
121

122
                        /**
123
                         * Throw an exception if the comment failed to be created
124
                         */
125
                        if ( is_wp_error( $success ) ) {
4✔
126
                                throw new UserError( esc_html( $success->get_error_message() ) );
×
127
                        }
128

129
                        /**
130
                         * This updates additional data not part of the comments table ( commentmeta, other relations, etc )
131
                         *
132
                         * The input for the commentMutation will be passed, along with the $new_comment_id for the
133
                         * comment that was created so that relations can be set, meta can be updated, etc.
134
                         */
135
                        CommentMutation::update_additional_comment_data( $comment_id, $input, 'create', $context, $info );
4✔
136

137
                        /**
138
                         * Return the comment object
139
                         */
140
                        return [
4✔
141
                                'id'      => $comment_id,
4✔
142
                                'success' => (bool) $success,
4✔
143
                        ];
4✔
144
                };
593✔
145
        }
146
}
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