| 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() { |
| 18 |
register_graphql_mutation( |
| 19 |
'updateUser', |
| 20 |
[ |
| 21 |
'inputFields' => self::get_input_fields(), |
| 22 |
'outputFields' => self::get_output_fields(), |
| 23 |
'mutateAndGetPayload' => self::mutate_and_get_payload(), |
| 24 |
] |
| 25 |
); |
| 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() { |
| 34 |
return array_merge( |
| 35 |
[ |
| 36 |
'id' => [ |
| 37 |
'type' => [ |
| 38 |
'non_null' => 'ID', |
| 39 |
], |
| 40 |
// translators: the placeholder is the name of the type of post object being updated |
| 41 |
'description' => static function () { |
| 42 |
return __( 'The ID of the user', 'wp-graphql' ); |
| 43 |
}, |
| 44 |
], |
| 45 |
], |
| 46 |
UserCreate::get_input_fields() |
| 47 |
); |
| 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() { |
| 56 |
return UserCreate::get_output_fields(); |
| 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() { |
| 65 |
return static function ( $input, AppContext $context, ResolveInfo $info ) { |
| 66 |
// Get the user ID. |
| 67 |
$user_id = Utils::get_database_id_from_id( $input['id'] ); |
| 68 |
|
| 69 |
if ( empty( $user_id ) ) { |
| 70 |
throw new UserError( esc_html__( 'The user ID passed is invalid', 'wp-graphql' ) ); |
| 71 |
} |
| 72 |
$existing_user = get_user_by( 'ID', $user_id ); |
| 73 |
|
| 74 |
/** |
| 75 |
* If there's no existing user, throw an exception |
| 76 |
*/ |
| 77 |
if ( false === $existing_user ) { |
| 78 |
throw new UserError( esc_html__( 'A user could not be updated with the provided ID', 'wp-graphql' ) ); |
| 79 |
} |
| 80 |
|
| 81 |
if ( ! current_user_can( 'edit_user', $existing_user->ID ) ) { |
| 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' ) ) { |
| 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', $user_id ); |
| 91 |
$user_args['ID'] = $user_id; |
| 92 |
|
| 93 |
/** |
| 94 |
* Update the user |
| 95 |
*/ |
| 96 |
$updated_user_id = wp_update_user( $user_args ); |
| 97 |
|
| 98 |
/** |
| 99 |
* Throw an exception if the post failed to create |
| 100 |
*/ |
| 101 |
if ( is_wp_error( $updated_user_id ) ) { |
| 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 ) ) { |
| 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 ); |
| 121 |
|
| 122 |
/** |
| 123 |
* Return the new user ID |
| 124 |
*/ |
| 125 |
return [ |
| 126 |
'id' => $updated_user_id, |
| 127 |
'user' => $context->get_loader( 'user' )->load_deferred( $updated_user_id ), |
| 128 |
]; |
| 129 |
}; |
| 130 |
} |
| 131 |
} |
| 132 |
|