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 / CommentDelete.php

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

145 lines 3.9 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 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() {
18 register_graphql_mutation(
19 'deleteComment',
20 [
21 'inputFields' => self::get_input_fields(),
22 'outputFields' => self::get_output_fields(),
23 'mutateAndGetPayload' => self::mutate_and_get_payload(),
24 ]
25 );
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() {
34 return [
35 'id' => [
36 'type' => [
37 'non_null' => 'ID',
38 ],
39 'description' => static function () {
40 return __( 'The deleted comment ID', 'wp-graphql' );
41 },
42 ],
43 'forceDelete' => [
44 'type' => 'Boolean',
45 'description' => static function () {
46 return __( 'Whether the comment should be force deleted instead of being moved to the trash', 'wp-graphql' );
47 },
48 ],
49 ];
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() {
58 return [
59 'deletedId' => [
60 'type' => 'Id',
61 'description' => static function () {
62 return __( 'The deleted comment ID', 'wp-graphql' );
63 },
64 'resolve' => static function ( $payload ) {
65 $deleted = (object) $payload['commentObject'];
66
67 return ! empty( $deleted->comment_ID ) ? Relay::toGlobalId( 'comment', $deleted->comment_ID ) : null;
68 },
69 ],
70 'comment' => [
71 'type' => 'Comment',
72 'description' => static function () {
73 return __( 'The deleted comment object', 'wp-graphql' );
74 },
75 'resolve' => static function ( $payload ) {
76 return $payload['commentObject'] ? $payload['commentObject'] : null;
77 },
78 ],
79 ];
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() {
88 return static function ( $input ) {
89 // Get the database ID for the comment.
90 $comment_id = Utils::get_database_id_from_id( $input['id'] );
91
92 // Get the post object before deleting it.
93 $comment_before_delete = ! empty( $comment_id ) ? get_comment( $comment_id ) : false;
94
95 if ( empty( $comment_before_delete ) ) {
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;
103
104 // Prevent comment deletions by default
105 $not_allowed = true;
106
107 // If the current user can moderate comments proceed
108 if ( current_user_can( 'moderate_comments' ) ) {
109 $not_allowed = false;
110 } else {
111 // Get the current user id
112 $current_user_id = absint( get_current_user_id() );
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 ) {
116 $not_allowed = false;
117 }
118 }
119
120 /**
121 * If the mutation has been prevented
122 */
123 if ( true === $not_allowed ) {
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'];
131
132 $comment_before_delete = new Comment( $comment_before_delete );
133
134 /**
135 * Delete the comment
136 */
137 wp_delete_comment( (int) $comment_id, $force_delete );
138
139 return [
140 'commentObject' => $comment_before_delete,
141 ];
142 };
143 }
144 }
145