PluginProbe
WPGraphQL / trunk
WPGraphQL vtrunk
2.22.3 2.22.2 2.22.1 2.22.0 2.21.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.15.0 2.14.1 2.14.0 2.13.0 2.2.0 2.3.0 2.3.3 2.3.6 2.3.8 2.5.0 2.5.1 2.5.2 2.5.3 All 177 releases
wp-graphql / src / Mutation / CommentUpdate.php

CommentUpdate.php in WPGraphQL trunk, at src/Mutation/CommentUpdate.php

149 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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() {
24 register_graphql_mutation(
25 'updateComment',
26 [
27 'inputFields' => self::get_input_fields(),
28 'outputFields' => self::get_output_fields(),
29 'mutateAndGetPayload' => self::mutate_and_get_payload(),
30 ]
31 );
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() {
40 return array_merge(
41 CommentCreate::get_input_fields(),
42 [
43 'id' => [
44 'type' => [
45 'non_null' => 'ID',
46 ],
47 'description' => static function () {
48 return __( 'The ID of the comment being updated.', 'wp-graphql' );
49 },
50 ],
51 ]
52 );
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() {
61 return CommentCreate::get_output_fields();
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() {
70 return static function ( $input, AppContext $context, ResolveInfo $info ) {
71 // Get the database ID for the comment.
72 $comment_id = ! empty( $input['id'] ) ? Utils::get_database_id_from_id( $input['id'] ) : null;
73
74 // Get the args from the existing comment.
75 $comment_args = ! empty( $comment_id ) ? get_comment( $comment_id, ARRAY_A ) : null;
76
77 if ( empty( $comment_id ) || empty( $comment_args ) ) {
78 throw new UserError( esc_html__( 'The Comment could not be updated', 'wp-graphql' ) );
79 }
80
81 $user_id = $comment_args['user_id'] ?? null;
82 $raw_comment_args = $comment_args;
83
84 // Prevent comment updates by default.
85 $not_allowed = true;
86
87 // If the current user can moderate comments, allow.
88 if ( current_user_can( 'moderate_comments' ) ) {
89 $not_allowed = false;
90 } else {
91 // If the current user is the comment author, allow (but not for status changes).
92 $current_user_id = absint( get_current_user_id() );
93 if ( 0 !== $current_user_id && absint( $user_id ) === $current_user_id ) {
94 $not_allowed = false;
95 }
96 }
97
98 if ( true === $not_allowed ) {
99 throw new UserError( esc_html__( 'Sorry, you are not allowed to update this comment.', 'wp-graphql' ) );
100 }
101
102 // Only users with moderate_comments may change moderation status (status/approved).
103 $attempting_status_change = isset( $input['status'] ) || array_key_exists( 'approved', $input );
104 if ( $attempting_status_change && ! current_user_can( 'moderate_comments' ) ) {
105 throw new UserError( esc_html__( 'Sorry, you are not allowed to change the moderation status of this comment.', 'wp-graphql' ) );
106 }
107
108 /**
109 * Map all of the args from GraphQL to WordPress friendly args array
110 */
111 CommentMutation::prepare_comment_object( $input, $comment_args, 'update', true );
112
113 // If there are no changes between the existing comment and the incoming comment
114 if ( $comment_args === $raw_comment_args ) {
115 throw new UserError( esc_html__( 'No changes have been provided to the comment.', 'wp-graphql' ) );
116 }
117
118 /**
119 * Update comment
120 * $success int 1 on success and 0 on fail
121 */
122 $success = wp_update_comment( $comment_args, true );
123
124 /**
125 * Throw an exception if the comment failed to be created
126 */
127 if ( is_wp_error( $success ) ) {
128 throw new UserError( esc_html( $success->get_error_message() ) );
129 }
130
131 /**
132 * This updates additional data not part of the comments table ( commentmeta, other relations, etc )
133 *
134 * The input for the commentMutation will be passed, along with the $new_comment_id for the
135 * comment that was created so that relations can be set, meta can be updated, etc.
136 */
137 CommentMutation::update_additional_comment_data( $comment_id, $input, 'create', $context, $info );
138
139 /**
140 * Return the comment object
141 */
142 return [
143 'id' => $comment_id,
144 'success' => (bool) $success,
145 ];
146 };
147 }
148 }
149