1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
<?php
namespace WPGraphQL\Type\User\Mutation;
use GraphQL\Error\UserError;
use WPGraphQL\Types;
class UserMutation {
private static $input_fields = [];
public static function input_fields() {
if ( empty( self::$input_fields ) ) {
$input_fields = [
'password' => [
'type' => Types::string(),
'description' => __( 'A string that contains the plain text password for the user.', 'wp-graphql' ),
],
'nicename' => [
'type' => Types::string(),
'description' => __( 'A string that contains a URL-friendly name for the user. The default is the user\'s username.', 'wp-graphql' ),
],
'websiteUrl' => [
'type' => Types::string(),
'description' => __( 'A string containing the user\'s URL for the user\'s web site.', 'wp-grapql' ),
],
'email' => [
'type' => Types::string(),
'description' => __( 'A string containing the user\'s email address.', 'wp-graphql' ),
],
'displayName' => [
'type' => Types::string(),
'description' => __( 'A string that will be shown on the site. Defaults to user\'s username. It is likely that you will want to change this, for both appearance and security through obscurity (that is if you dont use and delete the default admin user).', 'wp-graphql' ),
],
'nickname' => [
'type' => Types::string(),
'description' => __( 'The user\'s nickname, defaults to the user\'s username.', 'wp-graphql' ),
],
'firstName' => [
'type' => Types::string(),
'description' => __( ' The user\'s first name.', 'wp-graphql' ),
],
'lastName' => [
'type' => Types::string(),
'description' => __( 'The user\'s last name.', 'wp-graphql' ),
],
'description' => [
'type' => Types::string(),
'description' => __( 'A string containing content about the user.', 'wp-graphql' ),
],
'richEditing' => [
'type' => Types::string(),
'description' => __( 'A string for whether to enable the rich editor or not. False if not empty.', 'wp-graphql' ),
],
'registered' => [
'type' => Types::string(),
'description' => __( 'The date the user registered. Format is Y-m-d H:i:s.', 'wp-graphql' ),
],
'roles' => [
'type' => Types::list_of( Types::string() ),
'description' => __( 'An array of roles to be assigned to the user.', 'wp-graphql' ),
],
'jabber' => [
'type' => Types::string(),
'description' => __( 'User\'s Jabber account.', 'wp-graphql' ),
],
'aim' => [
'type' => Types::string(),
'description' => __( 'User\'s AOL IM account.', 'wp-graphql' ),
],
'yim' => [
'type' => Types::string(),
'description' => __( 'User\'s Yahoo IM account.', 'wp-graphql' ),
],
'locale' => [
'type' => Types::string(),
'description' => __( 'User\'s locale.', 'wp-graphql' ),
],
];
self::$input_fields = apply_filters( 'graphql_user_mutation_input_fields', $input_fields );
}
return ( ! empty( self::$input_fields ) ) ? self::$input_fields : null;
}
public static function prepare_user_object( $input, $mutation_name ) {
$insert_user_args = [];
if ( ! empty( $input['password'] ) ) {
$insert_user_args['user_pass'] = $input['password'];
} else {
$insert_user_args['user_pass'] = null;
}
if ( ! empty( $input['username'] ) ) {
$insert_user_args['user_login'] = $input['username'];
}
if ( ! empty( $input['nicename'] ) ) {
$insert_user_args['user_nicename'] = $input['nicename'];
}
if ( ! empty( $input['websiteUrl'] ) ) {
$insert_user_args['user_url'] = esc_url( $input['websiteUrl'] );
}
if ( ! empty( $input['email'] ) ) {
if ( false === is_email( apply_filters( 'pre_user_email', $input['email'] ) ) ) {
throw new UserError( __( 'The email address you are trying to use is invalid', 'graphql' ) );
}
$insert_user_args['user_email'] = $input['email'];
}
if ( ! empty( $input['displayName'] ) ) {
$insert_user_args['display_name'] = $input['displayName'];
}
if ( ! empty( $input['nickname'] ) ) {
$insert_user_args['nickname'] = $input['nickname'];
}
if ( ! empty( $input['firstName'] ) ) {
$insert_user_args['first_name'] = $input['firstName'];
}
if ( ! empty( $input['lastName'] ) ) {
$insert_user_args['last_name'] = $input['lastName'];
}
if ( ! empty( $input['description'] ) ) {
$insert_user_args['description'] = $input['description'];
}
if ( ! empty( $input['richEditing'] ) ) {
$insert_user_args['rich_editing'] = $input['richEditing'];
}
if ( ! empty( $input['registered'] ) ) {
$insert_user_args['user_registered'] = $input['registered'];
}
if ( ! empty( $input['roles'] ) ) {
$insert_user_args['role'] = $input['roles'][0];
}
if ( ! empty( $input['locale'] ) ) {
$insert_user_args['locale'] = $input['locale'];
}
$insert_user_args = apply_filters( 'graphql_user_insert_post_args', $insert_user_args, $input, $mutation_name );
return $insert_user_args;
}
public static function update_additional_user_object_data( $new_user_id, $input, $mutation_name ) {
$roles = ! empty( $input['roles'] ) ? $input['roles'] : [];
self::add_user_roles( $new_user_id, $roles );
do_action( 'graphql_user_object_mutation_update_additional_data', $new_user_id, $input, $mutation_name );
}
private static function add_user_roles( $user_id, $roles ) {
if ( empty( $roles ) || ! is_array( $roles ) ) {
return;
}
$user = get_user_by( 'ID', $user_id );
if ( false !== $user ) {
foreach ( $roles as $role ) {
self::verify_user_role( $role );
$user->add_role( $role );
}
}
}
private static function verify_user_role( $role ) {
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/admin.php';
}
$editable_roles = get_editable_roles();
if ( empty( $editable_roles[ $role ] ) ) {
throw new UserError( sprintf( __( 'Sorry, you are not allowed to give this the following role: %s.', 'wp-graphql' ), $role ) );
} else {
return true;
}
}
}