• 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

85.19
/src/Mutation/UserUpdate.php
1
<?php
2
namespace WPGraphQL\Mutation;
3

4
use GraphQL\Error\UserError;
5
use GraphQL\Type\Definition\ResolveInfo;
6
use WPGraphQL\AppContext;
7
use WPGraphQL\Data\UserMutation;
8
use WPGraphQL\Utils\Utils;
9

10
class UserUpdate {
11
        /**
12
         * Registers the CommentCreate mutation.
13
         *
14
         * @return void
15
         * @throws \Exception
16
         */
17
        public static function register_mutation() {
593✔
18
                register_graphql_mutation(
593✔
19
                        'updateUser',
593✔
20
                        [
593✔
21
                                'inputFields'         => self::get_input_fields(),
593✔
22
                                'outputFields'        => self::get_output_fields(),
593✔
23
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
593✔
24
                        ]
593✔
25
                );
593✔
26
        }
27

28
        /**
29
         * Defines the mutation input field configuration.
30
         *
31
         * @return array<string,array<string,mixed>>
32
         */
33
        public static function get_input_fields() {
593✔
34
                return array_merge(
593✔
35
                        [
593✔
36
                                'id' => [
593✔
37
                                        'type'        => [
593✔
38
                                                'non_null' => 'ID',
593✔
39
                                        ],
593✔
40
                                        // translators: the placeholder is the name of the type of post object being updated
41
                                        'description' => static function () {
593✔
42
                                                return __( 'The ID of the user', 'wp-graphql' );
15✔
43
                                        },
593✔
44
                                ],
593✔
45
                        ],
593✔
46
                        UserCreate::get_input_fields()
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 UserCreate::get_output_fields();
593✔
57
        }
58

59
        /**
60
         * Defines the mutation data modification closure.
61
         *
62
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
63
         */
64
        public static function mutate_and_get_payload() {
593✔
65
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
593✔
66
                        // Get the user ID.
67
                        $user_id = Utils::get_database_id_from_id( $input['id'] );
2✔
68

69
                        if ( empty( $user_id ) ) {
2✔
70
                                throw new UserError( esc_html__( 'The user ID passed is invalid', 'wp-graphql' ) );
1✔
71
                        }
72
                        $existing_user = get_user_by( 'ID', $user_id );
2✔
73

74
                        /**
75
                         * If there's no existing user, throw an exception
76
                         */
77
                        if ( false === $existing_user ) {
2✔
78
                                throw new UserError( esc_html__( 'A user could not be updated with the provided ID', 'wp-graphql' ) );
1✔
79
                        }
80

81
                        if ( ! current_user_can( 'edit_user', $existing_user->ID ) ) {
2✔
82
                                throw new UserError( esc_html__( 'You do not have the appropriate capabilities to perform this action', 'wp-graphql' ) );
×
83
                        }
84

85
                        if ( isset( $input['roles'] ) && ! current_user_can( 'edit_users' ) ) {
2✔
86
                                unset( $input['roles'] );
×
87
                                throw new UserError( esc_html__( 'You do not have the appropriate capabilities to perform this action', 'wp-graphql' ) );
×
88
                        }
89

90
                        $user_args       = UserMutation::prepare_user_object( $input, 'updateUser' );
2✔
91
                        $user_args['ID'] = $user_id;
2✔
92

93
                        /**
94
                         * Update the user
95
                         */
96
                        $updated_user_id = wp_update_user( $user_args );
2✔
97

98
                        /**
99
                         * Throw an exception if the post failed to create
100
                         */
101
                        if ( is_wp_error( $updated_user_id ) ) {
2✔
102
                                $error_message = $updated_user_id->get_error_message();
×
103
                                if ( ! empty( $error_message ) ) {
×
104
                                        throw new UserError( esc_html( $error_message ) );
×
105
                                } else {
106
                                        throw new UserError( esc_html__( 'The user failed to update but no error was provided', 'wp-graphql' ) );
×
107
                                }
108
                        }
109

110
                        /**
111
                         * If the $updated_user_id is empty, we should throw an exception
112
                         */
113
                        if ( empty( $updated_user_id ) ) {
2✔
114
                                throw new UserError( esc_html__( 'The user failed to update', 'wp-graphql' ) );
×
115
                        }
116

117
                        /**
118
                         * Update additional user data
119
                         */
120
                        UserMutation::update_additional_user_object_data( $updated_user_id, $input, 'updateUser', $context, $info );
2✔
121

122
                        /**
123
                         * Return the new user ID
124
                         */
125
                        return [
1✔
126
                                'id'   => $updated_user_id,
1✔
127
                                'user' => $context->get_loader( 'user' )->load_deferred( $updated_user_id ),
1✔
128
                        ];
1✔
129
                };
593✔
130
        }
131
}
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