Last active
December 26, 2019 18:47
-
-
Save saleebm/4797350cf191f3cea3beb57f8498a6fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use GraphQL\Error\UserError; | |
use WPGraphQL\Data\UserMutation; | |
use WPGraphQL\JWT_Authentication\Auth; | |
use WPGraphQL\Model\User; | |
use WPGraphQL\Mutation\UserRegister; | |
use WPGraphQL\Types; | |
use WPGraphQL\WooCommerce\Data\Mutation\Customer_Mutation; | |
use WPGraphQL\WooCommerce\Model\Customer; | |
class Customer_Register_Proper | |
{ | |
public static function register_mutation() | |
{ | |
register_graphql_mutation( | |
'registerCustomerProper', | |
[ | |
'inputFields' => self::get_input_fields(), | |
'outputFields' => self::get_output_fields(), | |
'mutateAndGetPayload' => self::mutate_and_get_payload(), | |
] | |
); | |
} | |
public static function get_input_fields() | |
{ | |
$input_fields = array_merge( | |
UserRegister::get_input_fields(), | |
array( | |
'billing' => array( | |
'type' => 'CustomerAddressInput', | |
'description' => __( | |
'Customer billing information', | |
'wp-graphql-woocommerce' | |
), | |
), | |
'shipping' => array( | |
'type' => 'CustomerAddressInput', | |
'description' => __( | |
'Customer shipping address', | |
'wp-graphql-woocommerce' | |
), | |
), | |
'shippingSameAsBilling' => array( | |
'type' => 'Boolean', | |
'description' => __( | |
'Customer shipping is identical to billing address', | |
'wp-graphql-woocommerce' | |
), | |
), | |
) | |
); | |
/** | |
* make sure we don't allow input for role or roles | |
*/ | |
unset($input_fields['role'], $input_fields['roles']); | |
return $input_fields; | |
} | |
public static function get_output_fields() | |
{ | |
return array( | |
'customer' => array( | |
'type' => 'Customer', | |
'resolve' => static function ($payload) { | |
return new Customer($payload['id']); | |
}, | |
), | |
'viewer' => array( | |
'type' => 'User', | |
'resolve' => static function ($payload) { | |
$user = get_user_by('ID', $payload['id']); | |
return new User($user); | |
}, | |
), | |
'authToken' => array( | |
'type' => Types::string(), | |
'description' => __( | |
'JWT Token that can be used in future requests for Authentication', | |
'wp-graphql-woocommerce' | |
), | |
'resolve' => function ($payload) { | |
$user = get_user_by('ID', $payload['id']); | |
$token = Auth::get_token($user); | |
if (is_wp_error($token)) { | |
throw new UserError($token->get_error_message()); | |
} | |
return $token; | |
}, | |
), | |
'refreshToken' => array( | |
'type' => Types::string(), | |
'description' => __( | |
'A JWT token that can be used in future requests to get a refreshed jwtAuthToken. If the refresh token used in a request is revoked or otherwise invalid, a valid Auth token will NOT be issued in the response headers.', | |
'wp-graphql-woocommerce' | |
), | |
'resolve' => function ($payload) { | |
$user = get_user_by('ID', $payload['id']); | |
$refresh_token = Auth::get_refresh_token($user); | |
if (is_wp_error($refresh_token)) { | |
throw new UserError( | |
$refresh_token->get_error_message() | |
); | |
} | |
return $refresh_token; | |
}, | |
), | |
); | |
} | |
public static function mutate_and_get_payload() | |
{ | |
return static function ( | |
$input, | |
$context, | |
$info | |
) { | |
if (empty($input['username'])) { | |
throw new UserError( | |
__('A username was not provided.', 'wp-graphql') | |
); | |
} | |
if (empty($input['email'])) { | |
throw new UserError( | |
__( | |
'An email address was not provided.', | |
'wp-graphql' | |
) | |
); | |
} | |
if (empty($input['password'])) { | |
throw new UserError( | |
__( | |
'A password was not provided.', | |
'wp-graphql' | |
) | |
); | |
} | |
/** | |
* Map all of the args from GQL to WP friendly | |
*/ | |
$user_args = UserMutation::prepare_user_object( | |
$input, | |
'registerCustomerProper' | |
); | |
/** | |
* merge with customer mutation props, ie Billing | |
*/ | |
$user_args = array_merge( | |
$user_args, | |
Customer_Mutation::prepare_customer_props( | |
$input, | |
'registerCustomerProper' | |
) | |
); | |
/** | |
* Create the user using native WooCommerce function | |
*/ | |
$user_id = wc_create_new_customer( | |
$user_args['user_email'], | |
$user_args['user_login'], | |
$user_args['user_pass'], | |
$user_args | |
); | |
/** | |
* Throw an exception if the user failed to register | |
*/ | |
if (is_wp_error($user_id)) { | |
$error_message = $user_id->get_error_message(); | |
if ( ! empty($error_message)) { | |
throw new UserError(esc_html($error_message)); | |
} | |
throw new UserError( | |
__( | |
'The user failed to register but no error was provided', | |
'wp-graphql' | |
) | |
); | |
} | |
/** | |
* If the $post_id is empty, we should throw an exception | |
*/ | |
if (empty($user_id)) { | |
throw new UserError( | |
__('The object failed to create', 'wp-graphql') | |
); | |
} | |
/** | |
* Update additional user data | |
*/ | |
UserMutation::update_additional_user_object_data( | |
$user_id, | |
$input, | |
'registerCustomerProper', | |
$context, | |
$info | |
); | |
/** | |
* If the client isn't already authenticated, set the state in the current session to | |
* the user they just registered. This is mostly so that they can get a response from | |
* the mutation about the user they just registered after the user object passes | |
* through the user model. | |
*/ | |
if ( ! is_user_logged_in()) { | |
wp_set_current_user($user_id); | |
} | |
return [ | |
'id' => $user_id, | |
]; | |
}; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment