| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\API; |
| 4 |
|
| 5 |
use Templately\Utils\Helper; |
| 6 |
use Templately\Utils\Options; |
| 7 |
use WP_REST_Request; |
| 8 |
|
| 9 |
class SignUp extends API { |
| 10 |
public function permission_check( WP_REST_Request $request ) { |
| 11 |
$this->request = $request; |
| 12 |
return true; |
| 13 |
} |
| 14 |
|
| 15 |
public function register_routes() { |
| 16 |
$this->post('signup', [ $this, 'create_account' ] ); |
| 17 |
} |
| 18 |
|
| 19 |
public function create_account() { |
| 20 |
$errors = []; |
| 21 |
$_ip = Helper::get_ip(); |
| 22 |
$_site_url = home_url( '/' ); |
| 23 |
|
| 24 |
$first_name = $this->get_param( 'first_name' ); |
| 25 |
$last_name = $this->get_param( 'last_name' ); |
| 26 |
$email = $this->get_param( 'email', '', 'sanitize_email' ); |
| 27 |
$password = $this->get_param( 'password' ); |
| 28 |
$confirm_password = $this->get_param( 'confirm_password' ); |
| 29 |
|
| 30 |
if ( empty( $first_name ) ) { |
| 31 |
$errors['first_name'] = __( 'First name cannot be empty.', 'templately' ); |
| 32 |
} |
| 33 |
if ( empty( $last_name ) ) { |
| 34 |
$errors['last_name'] = __( 'Last name cannot be empty.', 'templately' ); |
| 35 |
} |
| 36 |
if ( empty( $email ) ) { |
| 37 |
$errors['email'] = __( 'Email cannot be empty.', 'templately' ); |
| 38 |
} |
| 39 |
if ( $email && ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { |
| 40 |
$errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' ); |
| 41 |
} |
| 42 |
if ( empty( $password ) ) { |
| 43 |
$errors['password'] = __( 'Password cannot be empty.', 'templately' ); |
| 44 |
} |
| 45 |
if ( empty( $confirm_password ) ) { |
| 46 |
$errors['confirm_password'] = __( 'Confirm password cannot be empty.', 'templately' ); |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! empty( $password ) && ! empty( $confirm_password ) && $password !== $confirm_password ) { |
| 50 |
$errors['password_mismatched'] = __( 'Password and confirm password should be matched.', 'templately' ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( ! empty( $errors ) ) { |
| 54 |
return $this->error( 'signup_errors', $errors, 'signup', '400' ); |
| 55 |
} |
| 56 |
|
| 57 |
$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 }'; |
| 58 |
|
| 59 |
$response = $this->http()->mutation( |
| 60 |
'createUser', |
| 61 |
$query, |
| 62 |
[ |
| 63 |
'first_name' => $first_name, |
| 64 |
'last_name' => $last_name, |
| 65 |
'email' => $email, |
| 66 |
'password' => $password, |
| 67 |
'site_url' => $_site_url, |
| 68 |
'ip' => $_ip |
| 69 |
] |
| 70 |
)->post(); |
| 71 |
|
| 72 |
if( is_wp_error( $response ) ) { |
| 73 |
return $response; |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! Login::is_globally_signed() ) { |
| 77 |
Options::set_global_login(); |
| 78 |
} |
| 79 |
|
| 80 |
if ( ! empty( $response['user']['api_key'] ) ) { |
| 81 |
$this->utils('options')->set( 'api_key', $response['user']['api_key'] ); |
| 82 |
unset( $response['user']['api_key'] ); |
| 83 |
} |
| 84 |
|
| 85 |
$this->utils('options')->set( 'user', $response['user'] ); |
| 86 |
|
| 87 |
return $response; |
| 88 |
} |
| 89 |
} |