PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / auth / REST / SignUp.php

SignUp.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/auth/REST/SignUp.php

103 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Modules\Auth\REST;
4
5 use Templately\API\API;
6 use Templately\Utils\Helper;
7 use Templately\Utils\Options;
8 use WP_REST_Request;
9
10 class SignUp extends API {
11 public function permission_check( WP_REST_Request $request ) {
12 $this->request = $request;
13 return true;
14 }
15
16 public function register_routes() {
17 $this->post('signup', [ $this, 'create_account' ] );
18 }
19
20 public function create_account() {
21 $errors = [];
22 $_ip = Helper::get_ip();
23 $_site_url = home_url( '/' );
24
25 $first_name = $this->get_param( 'first_name' );
26 $last_name = $this->get_param( 'last_name' );
27 $email = $this->get_param( 'email', '', 'sanitize_email' );
28 $password = $this->get_param( 'password' );
29 $confirm_password = $this->get_param( 'confirm_password' );
30
31 if ( empty( $first_name ) ) {
32 $errors['first_name'] = __( 'First name cannot be empty.', 'templately' );
33 }
34 if ( empty( $last_name ) ) {
35 $errors['last_name'] = __( 'Last name cannot be empty.', 'templately' );
36 }
37 if ( empty( $email ) ) {
38 $errors['email'] = __( 'Email cannot be empty.', 'templately' );
39 }
40 if ( $email && ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
41 $errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' );
42 }
43 if ( empty( $password ) ) {
44 $errors['password'] = __( 'Password cannot be empty.', 'templately' );
45 }
46 if ( empty( $confirm_password ) ) {
47 $errors['confirm_password'] = __( 'Confirm password cannot be empty.', 'templately' );
48 }
49
50 if ( ! empty( $password ) && ! empty( $confirm_password ) && $password !== $confirm_password ) {
51 $errors['password_mismatched'] = __( 'Password and confirm password should be matched.', 'templately' );
52 }
53
54 if ( ! empty( $errors ) ) {
55 return $this->error( 'signup_errors', $errors, 'signup', '400' );
56 }
57
58 $query = 'status, message, user{ id, name, first_name, last_name, display_name, email, profile_photo, joined, is_verified, api_key, plan, plan_expire_at, my_cloud{ limit, usages, last_pushed }, show_notice }';
59
60 $response = $this->http()->mutation(
61 'createUser',
62 $query,
63 [
64 'first_name' => $first_name,
65 'last_name' => $last_name,
66 'email' => $email,
67 'password' => $password,
68 'site_url' => $_site_url,
69 'ip' => $_ip
70 ]
71 )->post();
72
73 if( is_wp_error( $response ) ) {
74 return $response;
75 }
76
77 $options = $this->utils( 'options' );
78 $options->use_current_user( true );
79
80 try {
81 if ( ! Login::is_globally_signed() ) {
82 Options::set_global_login();
83 }
84
85 if ( ! empty( $response['user']['api_key'] ) ) {
86 $options->set( 'api_key', $response['user']['api_key'] );
87 unset( $response['user']['api_key'] );
88 }
89
90 if ( ! empty( $response['user'] ) && is_array( $response['user'] ) ) {
91 $response['user']['site_url'] = base64_encode( $_site_url );
92 $response['user']['ip'] = $_ip;
93 }
94
95 $options->set( 'user', $response['user'] );
96 } finally {
97 $options->use_current_user( false );
98 }
99
100 return $response;
101 }
102 }
103