PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.31.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.31.2
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Controllers / Rest / VerificationCodeController.php
VerificationCodeController.php
118 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Controllers\Rest;
4
5 use SureCart\Models\User;
6 use SureCart\Models\VerificationCode;
7
8 /**
9 * Handle verification code requests through the REST API
10 */
11 class VerificationCodeController extends RestController {
12 /**
13 * Class to make the requests.
14 *
15 * @var string
16 */
17 protected $class = VerificationCode::class;
18
19 /**
20 * Create model.
21 *
22 * @param \WP_REST_Request $request Rest Request.
23 *
24 * @return \WP_REST_Response|\WP_Error
25 */
26 public function create( \WP_REST_Request $request ) {
27 $model = $this->middleware( new $this->class(), $request );
28 if ( is_wp_error( $model ) ) {
29 return $model;
30 }
31
32 $user = $this->getUser( $request->get_param( 'login' ) );
33
34 // bail if no user.
35 if ( ! $user ) {
36 return new \WP_Error( 'user_not_found', __( 'The user could not be found.', 'surecart' ) );
37 }
38
39 return $model->where( $request->get_query_params() )->create(
40 [
41 'email' => $user->user_email,
42 ]
43 );
44 }
45
46 /**
47 * Verify a verification code
48 *
49 * @param \WP_REST_Request $request Rest Request.
50 *
51 * @return \SureCart\Models\VerificationCode|\WP_Error
52 */
53 public function verify( \WP_REST_Request $request ) {
54 // run middleware.
55 $model = $this->middleware( new $this->class(), $request );
56
57 // bail if error.
58 if ( is_wp_error( $model ) ) {
59 return $model;
60 }
61
62 $user = $this->getUser( $request->get_param( 'login' ) );
63 if ( ! $user ) {
64 return new \WP_Error(
65 'invalid_email',
66 __( 'There is no account with that username or email address.', 'surecart' )
67 );
68 }
69
70 // verify the code.
71 $verify = $model->where( $request->get_query_params() )->verify(
72 [
73 'email' => $user->user_email,
74 'code' => $request->get_param( 'code' ),
75 ]
76 );
77
78 // check for errors.
79 if ( is_wp_error( $verify ) ) {
80 return $verify;
81 }
82
83 // code is invalid or not verified.
84 if ( empty( $verify->verified ) ) {
85 return new \WP_Error( 'invalid_code', __( 'Invalid verification code', 'surecart' ) );
86 }
87
88 // get the user based on the login value.
89 $user = $this->getUser( $request->get_param( 'login' ) );
90
91 // bail if no user.
92 if ( ! $user ) {
93 return new \WP_Error( 'user_not_found', __( 'The user could not be found.', 'surecart' ) );
94 }
95
96 // login the user.
97 $logged_in = $user->login();
98
99 if ( is_wp_error( $logged_in ) ) {
100 return $logged_in;
101 }
102
103 // return the model.
104 return $verify;
105 }
106
107 /**
108 * Get the user from the login.
109 *
110 * @param string $login An email or login.
111 *
112 * @return \SureCart\Models\User|false
113 */
114 public function getUser( $login ) {
115 return User::getUserby( strpos( $login ?? '', '@' ) ? 'email' : 'login', $login );
116 }
117 }
118