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

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