PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 2.0.6
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v2.0.6
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 3.1.10 All 111 releases
templately / includes / API / Login.php

Login.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 2.0.6, at includes/API/Login.php

181 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\API;
4
5 use WP_REST_Request;
6 use Templately\Utils\Helper;
7 use Templately\Utils\Options;
8
9 class Login 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( 'login', [ $this, 'login' ] );
17 $this->post( 'logout', [ $this, 'logout' ] );
18 $this->get( 'is-signed', [ $this, 'is_signed' ] );
19 }
20
21 public function login(){
22 $errors = [];
23 $_ip = Helper::get_ip();
24 $_site_url = home_url( '/' );
25
26 $global_signin = (bool) $this->get_param( 'global_signin', false );
27 $viaAPI = (bool) $this->get_param( 'viaAPI', false );
28 $email = $this->get_param( 'email', '', 'sanitize_email' );
29 $password = $this->get_param( 'password' );
30
31 $funcArgs = [
32 'ip' => $_ip,
33 'site_url' => $_site_url,
34 ];
35
36 if( $viaAPI ) {
37 $api_key = $this->get_param( 'api_key' );
38 $funcArgs['api_key'] = $api_key;
39
40 if ( empty( $api_key ) ) {
41 $errors['api_key'] = __( 'API Key field cannot be empty.', 'templately' );
42 }
43 } else {
44 $funcArgs['email'] = $email;
45 $funcArgs['password'] = $password;
46
47 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
48 $errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' );
49 }
50
51 if ( empty( $password ) ) {
52 $errors['password'] = __( 'Password field cannot be empty.', 'templately' );
53 }
54 }
55
56 if( ! empty( $errors ) ) {
57 return $this->error( 'login_error', $errors, 'login', 400 );
58 }
59
60 $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 }, favourites{ id, type } }';
61
62 $response = $this->http()->mutation(
63 $viaAPI ? 'connectWithApiKey' : 'connect',
64 $query,
65 $funcArgs
66 )->post();
67
68 if( is_wp_error( $response ) ) {
69 return $response;
70 }
71
72 if ( $global_signin && ! Login::is_globally_signed() ) {
73 Options::set_global_login();
74 }
75
76 if ( ! empty( $response['user']['api_key'] ) ) {
77 $this->utils('options')->set( 'api_key', $response['user']['api_key'] );
78 unset( $response['user']['api_key'] );
79 }
80
81 $meta = [
82 'is_globally_signed' => Login::is_globally_signed(),
83 'signed_as_global' => Login::signed_as_global(),
84 ];
85
86 if( ! empty( $response['user']['my_cloud']['last_pushed'] ) ) {
87 $_cloud_activity = unserialize( $response['user']['my_cloud']['last_pushed'] );
88 $this->utils('options')->set( 'cloud_activity', $_cloud_activity );
89 $meta['cloud_activity'] = $_cloud_activity;
90 unset( $response['user']['my_cloud']['last_pushed'] );
91 }
92
93 if( ! empty( $response['user']['favourites'] ) ) {
94 $_favourites = $this->utils('helper')->normalizeFavourites( $response['user']['favourites'] );
95 $this->utils('options')->set('favourites', $_favourites);
96
97 unset( $response['user']['favourites'] );
98 $meta['favourites'] = $_favourites;
99 }
100
101 $this->utils('options')->set('user', $response['user']);
102 $response['user']['meta'] = $this->user_meta( $meta );
103
104 return $response;
105 }
106
107 public function logout(){
108 // Remove All Metas
109 $this->utils('options')
110 ->remove('user')
111 ->remove('favourites')
112 ->remove('cloud_activity')
113 ->remove('api_key')
114 ->remove('global_login');
115
116 if( $this->utils('options')->whoami() === 'global' ) {
117 $this->utils('options')->remove_global_login();
118 }
119
120 $global_user_id = $this->utils('options')->is_global();
121 if( $global_user_id !== $this->utils('options')->current_user_id() ) {
122 $global_user = $this->utils('options')->get( 'user', false, $global_user_id );
123
124 if( ! empty( $global_user ) ) {
125 $global_user['meta'] = $this->user_meta();
126 }
127 }
128
129 $response = [
130 'status' => 'success',
131 'message' => __( 'Logged out.', 'templately' ),
132 ];
133
134 if( ! empty( $global_user ) ) {
135 $response['global_user'] = $global_user;
136 }
137
138 return $response;
139 }
140
141 public static function is_signed(){
142 $_response = [
143 'status' => 'success'
144 ];
145
146 $_user = (new static)->utils( 'options' )->get('user', null);
147
148 if( ! is_null( $_user ) ) {
149 $_user['meta'] = self::get_instance()->user_meta();
150 }
151
152 if( empty( $_user ) ) {
153 $_response['status'] = 'error';
154 }
155
156 $_response['user'] = $_user;
157
158 return $_response;
159 }
160
161 public function user_meta( $meta = [] ){
162 $_meta = [
163 'link_account' => self::utils( 'options' )->link_account(),
164 'unlink_account' => self::utils( 'options' )->unlink_account(),
165 'is_globally_signed' => Login::is_globally_signed(),
166 'signed_as_global' => Login::signed_as_global(),
167 'starred' => self::utils('options')->get('favourites'),
168 'cloud_activity' => self::utils( 'options' )->get('cloud_activity'),
169 'has_api' => rest_sanitize_boolean( self::utils( 'options' )->get('api_key' ) ),
170 ];
171
172 return array_merge( $_meta, $meta );
173 }
174
175 public static function is_globally_signed(){
176 return rest_sanitize_boolean( ( new static )->utils('options')->is_globally_signed() );
177 }
178 public static function signed_as_global(){
179 return rest_sanitize_boolean( ( new static )->utils('options')->signed_as_global() );
180 }
181 }