• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 18790791685

24 Oct 2025 08:03PM UTC coverage: 83.207% (-1.4%) from 84.575%
18790791685

push

github

actions-user
release: merge develop into master for v2.5.0

2 of 4 new or added lines in 2 files covered. (50.0%)

189 existing lines in 10 files now uncovered.

16143 of 19401 relevant lines covered (83.21%)

257.79 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() {
613✔
22
                register_graphql_mutation(
613✔
23
                        'deleteUser',
613✔
24
                        [
613✔
25
                                'inputFields'         => self::get_input_fields(),
613✔
26
                                'outputFields'        => self::get_output_fields(),
613✔
27
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
613✔
28
                        ]
613✔
29
                );
613✔
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() {
613✔
38
                return [
613✔
39
                        'id'         => [
613✔
40
                                'type'        => [
613✔
41
                                        'non_null' => 'ID',
613✔
42
                                ],
613✔
43
                                'description' => static function () {
613✔
44
                                        return __( 'The ID of the user you want to delete', 'wp-graphql' );
15✔
45
                                },
613✔
46
                        ],
613✔
47
                        'reassignId' => [
613✔
48
                                'type'        => 'ID',
613✔
49
                                'description' => static function () {
613✔
50
                                        return __( 'Reassign posts and links to new User ID.', 'wp-graphql' );
15✔
51
                                },
613✔
52
                        ],
613✔
53
                ];
613✔
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() {
613✔
62
                return [
613✔
63
                        'deletedId' => [
613✔
64
                                'type'        => 'ID',
613✔
65
                                'description' => static function () {
613✔
66
                                        return __( 'The ID of the user that you just deleted', 'wp-graphql' );
15✔
67
                                },
613✔
68
                                'resolve'     => static function ( $payload ) {
613✔
69
                                        $deleted = (object) $payload['user'];
1✔
70
                                        return ( ! empty( $deleted->ID ) ) ? Relay::toGlobalId( 'user', $deleted->ID ) : null;
1✔
71
                                },
613✔
72
                        ],
613✔
73
                        'user'      => [
613✔
74
                                'type'        => 'User',
613✔
75
                                'description' => static function () {
613✔
76
                                        return __( 'The deleted user object', 'wp-graphql' );
15✔
77
                                },
613✔
78
                                'resolve'     => static function ( $payload ) {
613✔
79
                                        return new User( $payload['user'] );
2✔
80
                                },
613✔
81
                        ],
613✔
82
                ];
613✔
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() {
613✔
91
                return static function ( $input ) {
613✔
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✔
UNCOV
136
                                require_once ABSPATH . 'wp-admin/includes/user.php';
×
137
                        }
138

139
                        if ( is_multisite() ) {
2✔
140

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

148
                                // only include these files for multisite requests
149
                                if ( ! function_exists( 'wpmu_delete_user' ) ) {
2✔
UNCOV
150
                                        require_once ABSPATH . 'wp-admin/includes/ms.php';
×
151
                                }
152
                                if ( ! function_exists( 'remove_user_from_blog' ) ) {
2✔
UNCOV
153
                                        require_once ABSPATH . 'wp-admin/includes/ms-functions.php';
×
154
                                }
155

156
                                $blog_id = get_current_blog_id();
2✔
157

158
                                // remove the user from the blog and reassign their posts
159
                                remove_user_from_blog( $user_id, $blog_id, $reassign_id );
2✔
160

161
                                // delete the user
162
                                $deleted_user = wpmu_delete_user( $user_id );
2✔
163
                        } else {
UNCOV
164
                                $deleted_user = wp_delete_user( $user_id, $reassign_id );
×
165
                        }
166

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

171
                        return [
2✔
172
                                'user' => $user_before_delete,
2✔
173
                        ];
2✔
174
                };
613✔
175
        }
176
}
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