Overview
  • Namespace
  • Class

Namespaces

  • WPGraphQL
    • Data
    • Type
      • Avatar
      • Comment
        • Connection
      • CommentAuthor
      • EditLock
      • Enum
      • MediaItem
        • Mutation
      • Plugin
        • Connection
      • PostObject
        • Connection
        • Mutation
      • PostType
      • Setting
      • Taxonomy
      • TermObject
        • Connection
        • Mutation
      • Theme
        • Connection
      • Union
      • User
        • Connection
        • Mutation
    • Utils

Classes

  • WPGraphQL\AppContext
  • WPGraphQL\Data\Config
  • WPGraphQL\Data\ConnectionResolver
  • WPGraphQL\Data\DataSource
  • WPGraphQL\Data\Loader
  • WPGraphQL\Router
  • WPGraphQL\Type\Avatar\AvatarType
  • WPGraphQL\Type\Comment\CommentQuery
  • WPGraphQL\Type\Comment\CommentType
  • WPGraphQL\Type\Comment\Connection\CommentConnectionArgs
  • WPGraphQL\Type\Comment\Connection\CommentConnectionDefinition
  • WPGraphQL\Type\Comment\Connection\CommentConnectionResolver
  • WPGraphQL\Type\CommentAuthor\CommentAuthorQuery
  • WPGraphQL\Type\CommentAuthor\CommentAuthorType
  • WPGraphQL\Type\EditLock\EditLockType
  • WPGraphQL\Type\Enum\MediaItemStatusEnumType
  • WPGraphQL\Type\Enum\MimeTypeEnumType
  • WPGraphQL\Type\Enum\PostObjectFieldFormatEnumType
  • WPGraphQL\Type\Enum\PostStatusEnumType
  • WPGraphQL\Type\Enum\PostTypeEnumType
  • WPGraphQL\Type\Enum\RelationEnumType
  • WPGraphQL\Type\Enum\TaxonomyEnumType
  • WPGraphQL\Type\MediaItem\MediaItemType
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemCreate
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemDelete
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemMutation
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemUpdate
  • WPGraphQL\Type\Plugin\Connection\PluginConnectionDefinition
  • WPGraphQL\Type\Plugin\Connection\PluginConnectionResolver
  • WPGraphQL\Type\Plugin\PluginQuery
  • WPGraphQL\Type\Plugin\PluginType
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionArgs
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionArgsDateQuery
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionDefinition
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionResolver
  • WPGraphQL\Type\PostObject\Mutation\PostObjectCreate
  • WPGraphQL\Type\PostObject\Mutation\PostObjectDelete
  • WPGraphQL\Type\PostObject\Mutation\PostObjectMutation
  • WPGraphQL\Type\PostObject\Mutation\PostObjectUpdate
  • WPGraphQL\Type\PostObject\Mutation\TermObjectDelete
  • WPGraphQL\Type\PostObject\PostObjectQuery
  • WPGraphQL\Type\PostObject\PostObjectType
  • WPGraphQL\Type\PostType\PostTypeType
  • WPGraphQL\Type\RootMutationType
  • WPGraphQL\Type\RootQueryType
  • WPGraphQL\Type\Setting\SettingQuery
  • WPGraphQL\Type\Setting\SettingType
  • WPGraphQL\Type\Taxonomy\TaxonomyType
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionArgs
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionDefinition
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionResolver
  • WPGraphQL\Type\TermObject\Mutation\TermObjectCreate
  • WPGraphQL\Type\TermObject\Mutation\TermObjectMutation
  • WPGraphQL\Type\TermObject\Mutation\TermObjectUpdate
  • WPGraphQL\Type\TermObject\TermObjectQuery
  • WPGraphQL\Type\TermObject\TermObjectType
  • WPGraphQL\Type\Theme\Connection\ThemeConnectionDefinition
  • WPGraphQL\Type\Theme\Connection\ThemeConnectionResolver
  • WPGraphQL\Type\Theme\ThemeType
  • WPGraphQL\Type\Union\CommentAuthorUnionType
  • WPGraphQL\Type\Union\PostObjectUnionType
  • WPGraphQL\Type\Union\TermObjectUnionType
  • WPGraphQL\Type\User\Connection\UserConnectionArgs
  • WPGraphQL\Type\User\Connection\UserConnectionDefinition
  • WPGraphQL\Type\User\Connection\UserConnectionResolver
  • WPGraphQL\Type\User\Mutation\UserCreate
  • WPGraphQL\Type\User\Mutation\UserDelete
  • WPGraphQL\Type\User\Mutation\UserMutation
  • WPGraphQL\Type\User\Mutation\UserUpdate
  • WPGraphQL\Type\User\UserQuery
  • WPGraphQL\Type\User\UserType
  • WPGraphQL\Type\WPEnumType
  • WPGraphQL\Type\WPInputObjectType
  • WPGraphQL\Type\WPObjectType
  • WPGraphQL\Types
  • WPGraphQL\Utils\InstrumentSchema
  • WPGraphQL\WPSchema

Interfaces

  • WPGraphQL\Data\ConnectionResolverInterface
  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
 *
 * @package WPGraphQL\Type\User\Mutation
 */
class UserMutation {

    /**
     * Stores the input fields static definition
     *
     * @var array $input_fields
     * @access private
     */
    private static $input_fields = [];

    /**
     * Defines the accepted input arguments
     *
     * @return array|null
     * @access public
     */
    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' ),
                ],
            ];

            /**
             * Filters all of the fields available for input
             *
             * @var array $input_fields
             */
            self::$input_fields = apply_filters( 'graphql_user_mutation_input_fields', $input_fields );

        }

        return ( ! empty( self::$input_fields ) ) ? self::$input_fields : null;

    }

    /**
     * Maps the GraphQL input to a format that the WordPress functions can use
     *
     * @param array $input Data coming from the GraphQL mutation query input
     * @param string $mutation_name Name of the mutation being performed
     * @access public
     * @return array
     */
    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'] ) ) {
            /**
             * Pluck the first role out of the array since the insert and update functions only
             * allow one role to be set at a time. We will add all of the roles passed to the
             * mutation later on after the initial object has been created or updated.
             */
            $insert_user_args['role'] = $input['roles'][0];
        }

        if ( ! empty( $input['locale'] ) ) {
            $insert_user_args['locale'] = $input['locale'];
        }

        /**
         * Filters the mappings for input to arguments
         *
         * @var array  $insert_user_args The arguments to ultimately be passed to the WordPress function
         * @var array  $input            Input data from the GraphQL mutation
         * @var string $mutation_name    What user mutation is being performed for context
         */
        $insert_user_args = apply_filters( 'graphql_user_insert_post_args', $insert_user_args, $input, $mutation_name );

        return $insert_user_args;

    }

    /**
     * This updates additional data related to the user object after the initial mutation has happened
     *
     * @param int $new_user_id The ID of the user that was just updated
     * @param array $input The input data from the GraphQL query
     * @param string $mutation_name Name of the mutation currently being run
     */
    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 );

        /**
         * Run an action after the additional data has been updated. This is a great spot to hook into to
         * update additional data related to users, such as setting relationships, updating additional usermeta,
         * or sending emails to Kevin... whatever you need to do with the userObject.
         *
         * @param int           $new_post_id      The ID of the user being mutated
         * @param array         $input            The input for the mutation
         * @param string        $mutation_name    The name of the mutation (ex: create, update, delete)
         */
        do_action( 'graphql_user_object_mutation_update_additional_data', $new_user_id, $input, $mutation_name );

    }

    /**
     * Method to add user roles to a user object
     *
     * @param int $user_id The ID of the user
     * @param array $roles List of roles that need to get added to the user
     * @access private
     */
    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 );
            }
        }

    }

    /**
     * Method to check if the user role is valid, and if the current user has permission to add, or remove it from a user.
     *
     * @param string $role Name of the role trying to get added to a user object
     * @return bool
     * @throws \Exception
     * @access private
     */
    private static function verify_user_role( $role ) {

        /**
         * The function for this is only loaded on admin pages. See note: https://codex.wordpress.org/Function_Reference/get_editable_roles#Notes
         */
        if ( ! function_exists( 'get_editable_roles' ) ) {
            require_once ABSPATH . 'wp-admin/includes/admin.php';
        }

        $editable_roles = get_editable_roles();

        if ( empty( $editable_roles[ $role ] ) ) {
            // Translators: %s is the name of the role that can't be added to the user.
            throw new UserError( sprintf( __( 'Sorry, you are not allowed to give this the following role: %s.', 'wp-graphql' ), $role ) );
        } else {
            return true;
        }

    }

}
API documentation generated by ApiGen