• 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

98.25
/src/Mutation/CommentRestore.php
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() {
593✔
22
                register_graphql_mutation(
593✔
23
                        'restoreComment',
593✔
24
                        [
593✔
25
                                'inputFields'         => self::get_input_fields(),
593✔
26
                                'outputFields'        => self::get_output_fields(),
593✔
27
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
593✔
28
                        ]
593✔
29
                );
593✔
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() {
593✔
38
                return [
593✔
39
                        'id' => [
593✔
40
                                'type'        => [
593✔
41
                                        'non_null' => 'ID',
593✔
42
                                ],
593✔
43
                                'description' => static function () {
593✔
44
                                        return __( 'The ID of the comment to be restored', 'wp-graphql' );
15✔
45
                                },
593✔
46
                        ],
593✔
47
                ];
593✔
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() {
593✔
56
                return [
593✔
57
                        'restoredId' => [
593✔
58
                                'type'        => 'Id',
593✔
59
                                'description' => static function () {
593✔
60
                                        return __( 'The ID of the restored comment', 'wp-graphql' );
15✔
61
                                },
593✔
62
                                'resolve'     => static function ( $payload ) {
593✔
63
                                        $restore = (object) $payload['commentObject'];
1✔
64

65
                                        return ! empty( $restore->comment_ID ) ? Relay::toGlobalId( 'comment', $restore->comment_ID ) : null;
1✔
66
                                },
593✔
67
                        ],
593✔
68
                        'comment'    => [
593✔
69
                                'type'        => 'Comment',
593✔
70
                                'description' => static function () {
593✔
71
                                        return __( 'The restored comment object', 'wp-graphql' );
15✔
72
                                },
593✔
73
                                'resolve'     => static function ( $payload, $args, AppContext $context ) {
593✔
74
                                        if ( ! isset( $payload['commentObject']->comment_ID ) || ! absint( $payload['commentObject']->comment_ID ) ) {
1✔
75
                                                return null;
×
76
                                        }
77
                                        return $context->get_loader( 'comment' )->load_deferred( absint( $payload['commentObject']->comment_ID ) );
1✔
78
                                },
593✔
79
                        ],
593✔
80
                ];
593✔
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() {
593✔
89
                return static function ( $input ) {
593✔
90
                        // Stop now if a user isn't allowed to delete the comment.
91
                        if ( ! current_user_can( 'moderate_comments' ) ) {
1✔
92
                                throw new UserError( esc_html__( 'Sorry, you are not allowed to restore this comment.', 'wp-graphql' ) );
1✔
93
                        }
94

95
                        // Get the database ID for the comment.
96
                        $comment_id = Utils::get_database_id_from_id( $input['id'] );
1✔
97

98
                        if ( false === $comment_id ) {
1✔
99
                                throw new UserError( esc_html__( 'Sorry, you are not allowed to restore this comment.', 'wp-graphql' ) );
1✔
100
                        }
101

102
                        // Delete the comment.
103
                        wp_untrash_comment( $comment_id );
1✔
104

105
                        $comment = get_comment( $comment_id );
1✔
106

107
                        return [
1✔
108
                                'commentObject' => $comment,
1✔
109
                        ];
1✔
110
                };
593✔
111
        }
112
}
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