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

wp-graphql / wp-graphql / 13316763745

13 Feb 2025 08:45PM UTC coverage: 82.712% (-0.3%) from 83.023%
13316763745

push

github

web-flow
Merge pull request #3307 from wp-graphql/release/v2.0.0

release: v2.0.0

195 of 270 new or added lines in 20 files covered. (72.22%)

180 existing lines in 42 files now uncovered.

13836 of 16728 relevant lines covered (82.71%)

299.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.0
/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' => __( 'The ID of the user you want to delete', 'wp-graphql' ),
593✔
44
                        ],
593✔
45
                        'reassignId' => [
593✔
46
                                'type'        => 'ID',
593✔
47
                                'description' => __( 'Reassign posts and links to new User ID.', 'wp-graphql' ),
593✔
48
                        ],
593✔
49
                ];
593✔
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() {
593✔
58
                return [
593✔
59
                        'deletedId' => [
593✔
60
                                'type'        => 'ID',
593✔
61
                                'description' => __( 'The ID of the user that you just deleted', 'wp-graphql' ),
593✔
62
                                'resolve'     => static function ( $payload ) {
593✔
63
                                        $deleted = (object) $payload['user'];
1✔
64
                                        return ( ! empty( $deleted->ID ) ) ? Relay::toGlobalId( 'user', $deleted->ID ) : null;
1✔
65
                                },
593✔
66
                        ],
593✔
67
                        'user'      => [
593✔
68
                                'type'        => 'User',
593✔
69
                                'description' => __( 'The deleted user object', 'wp-graphql' ),
593✔
70
                                'resolve'     => static function ( $payload ) {
593✔
71
                                        return new User( $payload['user'] );
2✔
72
                                },
593✔
73
                        ],
593✔
74
                ];
593✔
75
        }
76

77
        /**
78
         * Defines the mutation data modification closure.
79
         *
80
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
81
         */
82
        public static function mutate_and_get_payload() {
593✔
83
                return static function ( $input ) {
593✔
84
                        // Get the user ID.
85
                        $user_id = Utils::get_database_id_from_id( $input['id'] );
3✔
86

87
                        if ( empty( $user_id ) ) {
3✔
88
                                throw new UserError( esc_html__( 'The user ID passed is invalid', 'wp-graphql' ) );
1✔
89
                        }
90

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

95
                        /**
96
                         * Retrieve the user object before it's deleted
97
                         */
98
                        $user_before_delete = get_user_by( 'id', $user_id );
2✔
99

100
                        /**
101
                         * Throw an error if the user we are trying to delete doesn't exist
102
                         */
103
                        if ( false === $user_before_delete ) {
2✔
104
                                throw new UserError( esc_html__( 'Could not find an existing user to delete', 'wp-graphql' ) );
1✔
105
                        }
106

107
                        /**
108
                         * Get the user to reassign posts to.
109
                         */
110
                        $reassign_id = 0;
2✔
111
                        if ( ! empty( $input['reassignId'] ) ) {
2✔
112
                                $reassign_id = Utils::get_database_id_from_id( $input['reassignId'] );
1✔
113

114
                                if ( empty( $reassign_id ) ) {
1✔
115
                                        throw new UserError( esc_html__( 'The user ID passed to `reassignId` is invalid', 'wp-graphql' ) );
×
116
                                }
117
                                /**
118
                         * Retrieve the user object before it's deleted
119
                         */
120
                                $reassign_user = get_user_by( 'id', $reassign_id );
1✔
121

122
                                if ( false === $reassign_user ) {
1✔
123
                                        throw new UserError( esc_html__( 'Could not find the existing user to reassign.', 'wp-graphql' ) );
1✔
124
                                }
125
                        }
126

127
                        if ( ! function_exists( 'wp_delete_user' ) ) {
2✔
128
                                // @phpstan-ignore requireOnce.fileNotFound
UNCOV
129
                                require_once ABSPATH . 'wp-admin/includes/user.php';
×
130
                        }
131

132
                        if ( is_multisite() ) {
2✔
133

134
                                /**
135
                                 * If wpmu_delete_user() or remove_user_from_blog() doesn't exist yet,
136
                                 * load the files in which each is defined. I think we need to
137
                                 * load this manually here because WordPress only uses this
138
                                 * function on the user edit screen normally.
139
                                 */
140

141
                                // only include these files for multisite requests
142
                                if ( ! function_exists( 'wpmu_delete_user' ) ) {
2✔
143
                                        // @phpstan-ignore requireOnce.fileNotFound
UNCOV
144
                                        require_once ABSPATH . 'wp-admin/includes/ms.php';
×
145
                                }
146
                                if ( ! function_exists( 'remove_user_from_blog' ) ) {
2✔
147
                                        // @phpstan-ignore requireOnce.fileNotFound
UNCOV
148
                                        require_once ABSPATH . 'wp-admin/includes/ms-functions.php';
×
149
                                }
150

151
                                $blog_id = get_current_blog_id();
2✔
152

153
                                // remove the user from the blog and reassign their posts
154
                                remove_user_from_blog( $user_id, $blog_id, $reassign_id );
2✔
155

156
                                // delete the user
157
                                $deleted_user = wpmu_delete_user( $user_id );
2✔
158
                        } else {
159
                                $deleted_user = wp_delete_user( $user_id, $reassign_id );
×
160
                        }
161

162
                        if ( true !== $deleted_user ) {
2✔
163
                                throw new UserError( esc_html__( 'Could not delete the user.', 'wp-graphql' ) );
×
164
                        }
165

166
                        return [
2✔
167
                                'user' => $user_before_delete,
2✔
168
                        ];
2✔
169
                };
593✔
170
        }
171
}
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

© 2026 Coveralls, Inc