class-wc-rest-user-exists-controller.php
1.61 KB
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
<?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,
]
);
}
}