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

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

113 lines 2.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\AppContext;
8 use WPGraphQL\Utils\Utils;
9
10 /**
11 * Class CommentRestore
12 *
13 * @package WPGraphQL\Mutation
14 */
15 class CommentRestore {
16 /**
17 * Registers the CommentRestore mutation.
18 *
19 * @return void
20 */
21 public static function register_mutation() {
22 register_graphql_mutation(
23 'restoreComment',
24 [
25 'inputFields' => self::get_input_fields(),
26 'outputFields' => self::get_output_fields(),
27 'mutateAndGetPayload' => self::mutate_and_get_payload(),
28 ]
29 );
30 }
31
32 /**
33 * Defines the mutation input field configuration.
34 *
35 * @return array<string,array<string,mixed>>
36 */
37 public static function get_input_fields() {
38 return [
39 'id' => [
40 'type' => [
41 'non_null' => 'ID',
42 ],
43 'description' => static function () {
44 return __( 'The ID of the comment to be restored', 'wp-graphql' );
45 },
46 ],
47 ];
48 }
49
50 /**
51 * Defines the mutation output field configuration.
52 *
53 * @return array<string,array<string,mixed>>
54 */
55 public static function get_output_fields() {
56 return [
57 'restoredId' => [
58 'type' => 'Id',
59 'description' => static function () {
60 return __( 'The ID of the restored comment', 'wp-graphql' );
61 },
62 'resolve' => static function ( $payload ) {
63 $restore = (object) $payload['commentObject'];
64
65 return ! empty( $restore->comment_ID ) ? Relay::toGlobalId( 'comment', $restore->comment_ID ) : null;
66 },
67 ],
68 'comment' => [
69 'type' => 'Comment',
70 'description' => static function () {
71 return __( 'The restored comment object', 'wp-graphql' );
72 },
73 'resolve' => static function ( $payload, $args, AppContext $context ) {
74 if ( ! isset( $payload['commentObject']->comment_ID ) || ! absint( $payload['commentObject']->comment_ID ) ) {
75 return null;
76 }
77 return $context->get_loader( 'comment' )->load_deferred( absint( $payload['commentObject']->comment_ID ) );
78 },
79 ],
80 ];
81 }
82
83 /**
84 * Defines the mutation data modification closure.
85 *
86 * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
87 */
88 public static function mutate_and_get_payload() {
89 return static function ( $input ) {
90 // Stop now if a user isn't allowed to delete the comment.
91 if ( ! current_user_can( 'moderate_comments' ) ) {
92 throw new UserError( esc_html__( 'Sorry, you are not allowed to restore this comment.', 'wp-graphql' ) );
93 }
94
95 // Get the database ID for the comment.
96 $comment_id = Utils::get_database_id_from_id( $input['id'] );
97
98 if ( false === $comment_id ) {
99 throw new UserError( esc_html__( 'Sorry, you are not allowed to restore this comment.', 'wp-graphql' ) );
100 }
101
102 // Delete the comment.
103 wp_untrash_comment( $comment_id );
104
105 $comment = get_comment( $comment_id );
106
107 return [
108 'commentObject' => $comment,
109 ];
110 };
111 }
112 }
113