• 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

92.77
/src/Mutation/UserDelete.php
1
<?php
2

3
namespace WPGraphQL\Mutation;
4

5
use GraphQL\Error\UserError;
6
use GraphQLRelay\Relay;
7
use WPGraphQL\Model\User;
8
use WPGraphQL\Utils\Utils;
9

10
/**
11
 * Class UserDelete
12
 *
13
 * @package WPGraphQL\Mutation
14
 */
15
class UserDelete {
16
        /**
17
         * Registers the CommentCreate mutation.
18
         *
19
         * @return void
20
         */
21
        public static function register_mutation() {
593✔
22
                register_graphql_mutation(
593✔
23
                        'deleteUser',
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 user you want to delete', 'wp-graphql' );
15✔
45
                                },
593✔
46
                        ],
593✔
47
                        'reassignId' => [
593✔
48
                                'type'        => 'ID',
593✔
49
                                'description' => static function () {
593✔
50
                                        return __( 'Reassign posts and links to new User ID.', 'wp-graphql' );
15✔
51
                                },
593✔
52
                        ],
593✔
53
                ];
593✔
54
        }
55

56
        /**
57
         * Defines the mutation output field configuration.
58
         *
59
         * @return array<string,array<string,mixed>>
60
         */
61
        public static function get_output_fields() {
593✔
62
                return [
593✔
63
                        'deletedId' => [
593✔
64
                                'type'        => 'ID',
593✔
65
                                'description' => static function () {
593✔
66
                                        return __( 'The ID of the user that you just deleted', 'wp-graphql' );
15✔
67
                                },
593✔
68
                                'resolve'     => static function ( $payload ) {
593✔
69
                                        $deleted = (object) $payload['user'];
1✔
70
                                        return ( ! empty( $deleted->ID ) ) ? Relay::toGlobalId( 'user', $deleted->ID ) : null;
1✔
71
                                },
593✔
72
                        ],
593✔
73
                        'user'      => [
593✔
74
                                'type'        => 'User',
593✔
75
                                'description' => static function () {
593✔
76
                                        return __( 'The deleted user object', 'wp-graphql' );
15✔
77
                                },
593✔
78
                                'resolve'     => static function ( $payload ) {
593✔
79
                                        return new User( $payload['user'] );
2✔
80
                                },
593✔
81
                        ],
593✔
82
                ];
593✔
83
        }
84

85
        /**
86
         * Defines the mutation data modification closure.
87
         *
88
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
89
         */
90
        public static function mutate_and_get_payload() {
593✔
91
                return static function ( $input ) {
593✔
92
                        // Get the user ID.
93
                        $user_id = Utils::get_database_id_from_id( $input['id'] );
3✔
94

95
                        if ( empty( $user_id ) ) {
3✔
96
                                throw new UserError( esc_html__( 'The user ID passed is invalid', 'wp-graphql' ) );
1✔
97
                        }
98

99
                        if ( ! current_user_can( 'delete_users', $user_id ) ) {
3✔
100
                                throw new UserError( esc_html__( 'Sorry, you are not allowed to delete users.', 'wp-graphql' ) );
1✔
101
                        }
102

103
                        /**
104
                         * Retrieve the user object before it's deleted
105
                         */
106
                        $user_before_delete = get_user_by( 'id', $user_id );
2✔
107

108
                        /**
109
                         * Throw an error if the user we are trying to delete doesn't exist
110
                         */
111
                        if ( false === $user_before_delete ) {
2✔
112
                                throw new UserError( esc_html__( 'Could not find an existing user to delete', 'wp-graphql' ) );
1✔
113
                        }
114

115
                        /**
116
                         * Get the user to reassign posts to.
117
                         */
118
                        $reassign_id = 0;
2✔
119
                        if ( ! empty( $input['reassignId'] ) ) {
2✔
120
                                $reassign_id = Utils::get_database_id_from_id( $input['reassignId'] );
1✔
121

122
                                if ( empty( $reassign_id ) ) {
1✔
123
                                        throw new UserError( esc_html__( 'The user ID passed to `reassignId` is invalid', 'wp-graphql' ) );
×
124
                                }
125
                                /**
126
                         * Retrieve the user object before it's deleted
127
                         */
128
                                $reassign_user = get_user_by( 'id', $reassign_id );
1✔
129

130
                                if ( false === $reassign_user ) {
1✔
131
                                        throw new UserError( esc_html__( 'Could not find the existing user to reassign.', 'wp-graphql' ) );
1✔
132
                                }
133
                        }
134

135
                        if ( ! function_exists( 'wp_delete_user' ) ) {
2✔
136
                                // @phpstan-ignore requireOnce.fileNotFound
137
                                require_once ABSPATH . 'wp-admin/includes/user.php';
×
138
                        }
139

140
                        if ( is_multisite() ) {
2✔
141

142
                                /**
143
                                 * If wpmu_delete_user() or remove_user_from_blog() doesn't exist yet,
144
                                 * load the files in which each is defined. I think we need to
145
                                 * load this manually here because WordPress only uses this
146
                                 * function on the user edit screen normally.
147
                                 */
148

149
                                // only include these files for multisite requests
150
                                if ( ! function_exists( 'wpmu_delete_user' ) ) {
2✔
151
                                        // @phpstan-ignore requireOnce.fileNotFound
152
                                        require_once ABSPATH . 'wp-admin/includes/ms.php';
×
153
                                }
154
                                if ( ! function_exists( 'remove_user_from_blog' ) ) {
2✔
155
                                        // @phpstan-ignore requireOnce.fileNotFound
156
                                        require_once ABSPATH . 'wp-admin/includes/ms-functions.php';
×
157
                                }
158

159
                                $blog_id = get_current_blog_id();
2✔
160

161
                                // remove the user from the blog and reassign their posts
162
                                remove_user_from_blog( $user_id, $blog_id, $reassign_id );
2✔
163

164
                                // delete the user
165
                                $deleted_user = wpmu_delete_user( $user_id );
2✔
166
                        } else {
167
                                $deleted_user = wp_delete_user( $user_id, $reassign_id );
×
168
                        }
169

170
                        if ( true !== $deleted_user ) {
2✔
171
                                throw new UserError( esc_html__( 'Could not delete the user.', 'wp-graphql' ) );
×
172
                        }
173

174
                        return [
2✔
175
                                'user' => $user_before_delete,
2✔
176
                        ];
2✔
177
                };
593✔
178
        }
179
}
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