-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring back the rest-user-exists-controller file (#6303)
Co-authored-by: Shendy <[email protected]>
- Loading branch information
1 parent
8242e7a
commit d45c765
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Significance: patch | ||
Type: fix | ||
Comment: Temporary fix to hide an error notice that's printed during the plugin upgrade | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* Class WC_REST_User_Exists_Controller | ||
* | ||
* @package WooCommerce\Payments\Admin | ||
*/ | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* REST controller to check if a user exists. | ||
*/ | ||
class WC_REST_User_Exists_Controller extends WP_REST_Controller { | ||
|
||
/** | ||
* Endpoint namespace. | ||
* | ||
* @var string | ||
*/ | ||
protected $namespace = 'wc/v3'; | ||
|
||
/** | ||
* Endpoint path. | ||
* | ||
* @var string | ||
*/ | ||
protected $rest_base = 'users/exists'; | ||
|
||
/** | ||
* Configure REST API routes. | ||
*/ | ||
public function register_routes() { | ||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base, | ||
[ | ||
'methods' => WP_REST_Server::CREATABLE, | ||
'callback' => [ $this, 'user_exists' ], | ||
'permission_callback' => '__return_true', | ||
'args' => [ | ||
'email' => [ | ||
'required' => true, | ||
'description' => __( 'Email address.', 'woocommerce-payments' ), | ||
'type' => 'string', | ||
'format' => 'email', | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Retrieve if a user exists by email address. | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* | ||
* @return WP_REST_Response | ||
*/ | ||
public function user_exists( WP_REST_Request $request ): WP_REST_Response { | ||
$email = $request->get_param( 'email' ); | ||
$email_exists = ! empty( email_exists( $email ) ); | ||
$message = null; | ||
|
||
if ( $email_exists ) { | ||
// Use this function to show the core error message. | ||
$error = wc_create_new_customer( $email ); | ||
$message = $error->get_error_message(); | ||
} | ||
|
||
return new WP_REST_Response( | ||
[ | ||
'user-exists' => $email_exists, | ||
'message' => $message, | ||
] | ||
); | ||
} | ||
} | ||
|