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

313 lines 10.2 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 // Use client-provided current_url instead of HTTP_REFERER for reliability
41 $current_url = $this->get_param( 'current_url', '' );
42
43 $url = $this->http()->google_auth_url( $redirect_to, $current_url );
44 return [
45 'status' => 'success',
46 'url' => $url
47 ];
48 }
49
50 public function pricing(){
51 $data = get_transient( "templately_subscriptions" );
52
53 if( is_array( $data ) && ! empty( $data ) ) {
54 return $data;
55 }
56
57 $query = 'id, price, name, discounted_price, type, sites, coupon';
58 $response = $this->http()->query(
59 'subscriptionPlans',
60 $query
61 )->post();
62
63 set_transient( "templately_subscriptions", $response, WEEK_IN_SECONDS );
64
65 return $response;
66 }
67
68 public function login() {
69 $errors = [];
70 $_ip = Helper::get_ip();
71 $_site_url = home_url( '/' );
72
73 $global_signin = (bool) $this->get_param( 'global_signin', false );
74 $viaAPI = (bool) $this->get_param( 'viaAPI', false );
75 $email = $this->get_param( 'email', '', 'sanitize_email' );
76 $password = $this->get_param( 'password' );
77
78 $funcArgs = [
79 'ip' => $_ip,
80 'site_url' => $_site_url
81 ];
82
83 $postArgs = [];
84
85 if ( $viaAPI ) {
86 $api_key = $this->get_param( 'api_key' );
87 $funcArgs['api_key'] = $api_key;
88
89 if ( empty( $api_key ) ) {
90 $errors['api_key'] = __( 'API Key field cannot be empty.', 'templately' );
91 }
92 } else {
93 $funcArgs['email'] = $email;
94 $funcArgs['password'] = addcslashes( $password, '"' );
95
96 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
97 $errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' );
98 }
99
100 if ( empty( $password ) ) {
101 $errors['password'] = __( 'Password field cannot be empty.', 'templately' );
102 }
103 }
104
105 if ( ! empty( $errors ) ) {
106 return $this->error( 'login_error', $errors, 'login', 400 );
107 }
108
109 $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 } }';
110
111 $response = $this->http()->mutation(
112 $viaAPI ? 'connectWithApiKey' : 'connect',
113 $query,
114 $funcArgs
115 )->post($postArgs);
116
117 if ( is_wp_error( $response ) ) {
118 return $response;
119 }
120
121 if ( empty( $response['user']['api_key'] ) ) {
122 return $this->error( 'login_error', $response['message'] ?? __('Invalid API key.', 'templately'), 'login', 400 );
123 }
124
125 $options = $this->utils( 'options' );
126 $options->use_current_user( true );
127
128 try {
129 return $this->store_connection( $response, $global_signin, $_ip, $_site_url );
130 } finally {
131 $options->use_current_user( false );
132 }
133 }
134
135 /**
136 * Persist an authenticated connection against the acting user.
137 *
138 * @param array $response Cloud response, already validated.
139 * @param bool $global_signin Whether the user asked to sign in globally.
140 * @param string $_ip Request IP, echoed back into the profile.
141 * @param string $_site_url Site URL, echoed back into the profile.
142 *
143 * @return array
144 */
145 private function store_connection( $response, $global_signin, $_ip, $_site_url ) {
146
147 if ( $global_signin && ! Login::is_globally_signed() ) {
148 Options::set_global_login();
149 }
150
151 if ( ! empty( $response['user']['api_key'] ) ) {
152 $this->utils( 'options' )->set( 'api_key', $response['user']['api_key'] );
153 unset( $response['user']['api_key'] );
154 }
155
156 $meta = [
157 'is_globally_signed' => Login::is_globally_signed(),
158 'signed_as_global' => Login::signed_as_global()
159 ];
160
161 if ( ! empty( $response['user']['my_cloud']['last_pushed'] ) ) {
162 $_cloud_activity = unserialize( $response['user']['my_cloud']['last_pushed'] );
163 $this->utils( 'options' )->set( 'cloud_activity', $_cloud_activity );
164 $meta['cloud_activity'] = $_cloud_activity;
165 unset( $response['user']['my_cloud']['last_pushed'] );
166 }
167
168 if ( ! empty( $response['user']['favourites'] ) ) {
169 $_favourites = $this->utils( 'helper' )->normalizeFavourites( $response['user']['favourites'] );
170 $this->utils( 'options' )->set( 'favourites', $_favourites );
171
172 unset( $response['user']['favourites'] );
173 $meta['favourites'] = $_favourites;
174 }
175
176 if ( ! empty( $response['user']['reviews'] ) ) {
177 $_reviews = $this->utils( 'helper' )->normalizeReviews( $response['user']['reviews'] );
178 $this->utils( 'options' )->set( 'reviews', $_reviews );
179
180 unset( $response['user']['reviews'] );
181 $meta['reviews'] = $_reviews;
182 }
183
184 if(Helper::is_dev_api()){
185 $response['user']['is_dev_api'] = true;
186 }
187
188 if(! empty( $response['user'] ) && is_array($response['user'])){
189 $response['user']['ip'] = $_ip;
190 $response['user']['site_url'] = base64_encode( $_site_url );
191 }
192
193 $this->utils( 'options' )->set( 'user', $response['user'] );
194 $response['user']['meta'] = $this->user_meta( $meta );
195
196 return $response;
197 }
198
199 public function logout() {
200 $response = $this->http()->mutation(
201 'disconnect',
202 'status, message, data',
203 [
204 'api_key' => $this->api_key,
205 "site_url" => home_url( '/' )
206 ]
207 )->post();
208
209 if ( is_wp_error( $response ) ) {
210 return $response;
211 }
212
213 if ( ! isset( $response['status'] ) || $response['status'] !== 'success' ) {
214 return $this->error( 'logout_error', $response['message'], 'logout', 404 );
215 }
216
217 // Remove All Metas
218 $global_user = $this->delete();
219
220 $response = [
221 'status' => 'success',
222 'message' => __( 'Logged out.', 'templately' )
223 ];
224
225 if ( ! empty( $global_user ) ) {
226 $response['global_user'] = $global_user;
227 }
228
229 return $response;
230 }
231
232 public function delete(){
233 $this->utils( 'options' )
234 ->remove( 'user' )
235 ->remove( 'favourites' )
236 ->remove( 'reviews' )
237 ->remove( 'cloud_activity' )
238 ->remove( 'api_key' )
239 ->remove( 'global_login' )
240 ->remove( 'total_download_counts' )
241 ->remove( 'templates_in_clouds' );
242
243 if ( $this->utils( 'options' )->who_am_i() === 'global' ) {
244 $this->utils( 'options' )->remove_global_login();
245 }
246
247 $global_user_id = $this->utils( 'options' )->is_global();
248 $global_user = null;
249
250 if ( $global_user_id !== $this->utils( 'options' )->current_user_id() ) {
251 $global_user = $this->utils( 'options' )->get( 'user', false, $global_user_id );
252
253 if ( ! empty( $global_user ) ) {
254 if ( is_array( $global_user ) ) {
255 unset( $global_user['api_key'] );
256 }
257
258 $global_user['meta'] = $this->user_meta();
259 }
260 }
261
262 return $global_user;
263 }
264
265 public static function is_signed(): array {
266 $_response = [
267 'status' => 'success'
268 ];
269
270 $_user = ( new static )->utils( 'options' )->get( 'user', null );
271
272 if ( ! is_null( $_user ) ) {
273 // Profiles stored before 3.7.1 may still carry the cloud API key.
274 if ( is_array( $_user ) ) {
275 unset( $_user['api_key'] );
276 }
277
278 $_user['meta'] = self::get_instance()->user_meta();
279 }
280
281 if ( empty( $_user ) ) {
282 $_response['status'] = 'error';
283 }
284
285 $_response['user'] = $_user;
286
287 return $_response;
288 }
289
290 public function user_meta( $meta = [] ): array {
291 $_meta = [
292 'link_account' => self::utils( 'options' )->link_account(),
293 'unlink_account' => self::utils( 'options' )->unlink_account(),
294 'is_globally_signed' => Login::is_globally_signed(),
295 'signed_as_global' => Login::signed_as_global(),
296 'starred' => self::utils( 'options' )->get( 'favourites' ),
297 'reviews' => self::utils( 'options' )->get( 'reviews' ),
298 'cloud_activity' => self::utils( 'options' )->get( 'cloud_activity' ),
299 'has_api' => rest_sanitize_boolean( self::utils( 'options' )->get( 'api_key' ) )
300 ];
301
302 return array_merge( $_meta, $meta );
303 }
304
305 public static function is_globally_signed(): bool {
306 return rest_sanitize_boolean( ( new static )->utils( 'options' )->is_globally_signed() );
307 }
308
309 public static function signed_as_global(): bool {
310 return rest_sanitize_boolean( ( new static )->utils( 'options' )->signed_as_global() );
311 }
312 }
313