PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 2.2.7
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v2.2.7
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.2.7, at includes/API/Login.php

236 lines 7.4 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 $_route = $request->get_route();
13 if ( '/templately/v1/login' === $_route ) {
14 return true;
15 }
16
17 if ( '/templately/v1/pricing' === $_route ) {
18 return true;
19 }
20
21 return parent::permission_check( $request );
22 }
23
24 public function register_routes() {
25 $this->post( 'login', [$this, 'login'] );
26 $this->post( 'logout', [$this, 'logout'] );
27 $this->get( 'is-signed', [$this, 'is_signed'] );
28 $this->get( 'pricing', [$this, 'pricing'] );
29 }
30
31 public function pricing(){
32 $data = get_transient( "templately_subscriptions" );
33
34 if( is_array( $data ) && ! empty( $data ) ) {
35 return $data;
36 }
37
38 $query = 'id, price, name, discounted_price, type, sites';
39 $response = $this->http()->query(
40 'subscriptionPlans',
41 $query
42 )->post();
43
44 set_transient( "templately_subscriptions", $response, WEEK_IN_SECONDS );
45
46 return $response;
47 }
48
49 public function login() {
50 $errors = [];
51 $_ip = Helper::get_ip();
52 $_site_url = home_url( '/' );
53
54 $global_signin = (bool) $this->get_param( 'global_signin', false );
55 $viaAPI = (bool) $this->get_param( 'viaAPI', false );
56 $email = $this->get_param( 'email', '', 'sanitize_email' );
57 $password = $this->get_param( 'password' );
58
59 $funcArgs = [
60 'ip' => $_ip,
61 'site_url' => $_site_url
62 ];
63
64 if ( $viaAPI ) {
65 $api_key = $this->get_param( 'api_key' );
66 $funcArgs['api_key'] = $api_key;
67
68 if ( empty( $api_key ) ) {
69 $errors['api_key'] = __( 'API Key field cannot be empty.', 'templately' );
70 }
71 } else {
72 $funcArgs['email'] = $email;
73 $funcArgs['password'] = addcslashes( $password, '"' );
74
75 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
76 $errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' );
77 }
78
79 if ( empty( $password ) ) {
80 $errors['password'] = __( 'Password field cannot be empty.', 'templately' );
81 }
82 }
83
84 if ( ! empty( $errors ) ) {
85 return $this->error( 'login_error', $errors, 'login', 400 );
86 }
87
88 $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 }, show_notice }';
89
90 $response = $this->http()->mutation(
91 $viaAPI ? 'connectWithApiKey' : 'connect',
92 $query,
93 $funcArgs
94 )->post();
95
96 if ( is_wp_error( $response ) ) {
97 return $response;
98 }
99
100 if ( $global_signin && ! Login::is_globally_signed() ) {
101 Options::set_global_login();
102 }
103
104 if ( ! empty( $response['user']['api_key'] ) ) {
105 $this->utils( 'options' )->set( 'api_key', $response['user']['api_key'] );
106 unset( $response['user']['api_key'] );
107 }
108
109 $meta = [
110 'is_globally_signed' => Login::is_globally_signed(),
111 'signed_as_global' => Login::signed_as_global()
112 ];
113
114 if ( ! empty( $response['user']['my_cloud']['last_pushed'] ) ) {
115 $_cloud_activity = unserialize( $response['user']['my_cloud']['last_pushed'] );
116 $this->utils( 'options' )->set( 'cloud_activity', $_cloud_activity );
117 $meta['cloud_activity'] = $_cloud_activity;
118 unset( $response['user']['my_cloud']['last_pushed'] );
119 }
120
121 if ( ! empty( $response['user']['favourites'] ) ) {
122 $_favourites = $this->utils( 'helper' )->normalizeFavourites( $response['user']['favourites'] );
123 $this->utils( 'options' )->set( 'favourites', $_favourites );
124
125 unset( $response['user']['favourites'] );
126 $meta['favourites'] = $_favourites;
127 }
128
129 $this->utils( 'options' )->set( 'user', $response['user'] );
130 $response['user']['meta'] = $this->user_meta( $meta );
131
132 return $response;
133 }
134
135 public function logout() {
136 $response = $this->http()->mutation(
137 'disconnect',
138 'status, message, data',
139 [
140 'api_key' => $this->api_key,
141 "site_url" => home_url( '/' )
142 ]
143 )->post();
144
145 if ( is_wp_error( $response ) ) {
146 return $response;
147 }
148
149 if ( ! isset( $response['status'] ) || $response['status'] !== 'success' ) {
150 return $this->error( 'logout_error', $response['message'], 'logout', 404 );
151 }
152
153 // Remove All Metas
154 $global_user = $this->delete();
155
156 $response = [
157 'status' => 'success',
158 'message' => __( 'Logged out.', 'templately' )
159 ];
160
161 if ( ! empty( $global_user ) ) {
162 $response['global_user'] = $global_user;
163 }
164
165 return $response;
166 }
167
168 public function delete(){
169 $this->utils( 'options' )
170 ->remove( 'user' )
171 ->remove( 'favourites' )
172 ->remove( 'cloud_activity' )
173 ->remove( 'api_key' )
174 ->remove( 'global_login' )
175 ->remove( 'templates_in_clouds' );
176
177 if ( $this->utils( 'options' )->whoami() === 'global' ) {
178 $this->utils( 'options' )->remove_global_login();
179 }
180
181 $global_user_id = $this->utils( 'options' )->is_global();
182 $global_user = null;
183
184 if ( $global_user_id !== $this->utils( 'options' )->current_user_id() ) {
185 $global_user = $this->utils( 'options' )->get( 'user', false, $global_user_id );
186
187 if ( ! empty( $global_user ) ) {
188 $global_user['meta'] = $this->user_meta();
189 }
190 }
191
192 return $global_user;
193 }
194
195 public static function is_signed() {
196 $_response = [
197 'status' => 'success'
198 ];
199
200 $_user = ( new static )->utils( 'options' )->get( 'user', null );
201
202 if ( ! is_null( $_user ) ) {
203 $_user['meta'] = self::get_instance()->user_meta();
204 }
205
206 if ( empty( $_user ) ) {
207 $_response['status'] = 'error';
208 }
209
210 $_response['user'] = $_user;
211
212 return $_response;
213 }
214
215 public function user_meta( $meta = [] ) {
216 $_meta = [
217 'link_account' => self::utils( 'options' )->link_account(),
218 'unlink_account' => self::utils( 'options' )->unlink_account(),
219 'is_globally_signed' => Login::is_globally_signed(),
220 'signed_as_global' => Login::signed_as_global(),
221 'starred' => self::utils( 'options' )->get( 'favourites' ),
222 'cloud_activity' => self::utils( 'options' )->get( 'cloud_activity' ),
223 'has_api' => rest_sanitize_boolean( self::utils( 'options' )->get( 'api_key' ) )
224 ];
225
226 return array_merge( $_meta, $meta );
227 }
228
229 public static function is_globally_signed() {
230 return rest_sanitize_boolean( ( new static )->utils( 'options' )->is_globally_signed() );
231 }
232
233 public static function signed_as_global() {
234 return rest_sanitize_boolean( ( new static )->utils( 'options' )->signed_as_global() );
235 }
236 }