PluginProbe
WDesignKit – AI Templates, Widget Builder & MCP Workflow / 2.7.0
WDesignKit – AI Templates, Widget Builder & MCP Workflow v2.7.0
2.7.0 2.6.6 2.6.5 2.6.4 2.6.3 2.6.2 2.6.1 2.6.0 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5.0 2.4.0 2.3.3 2.3.2 2.3.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 All 129 releases
wdesignkit / includes / admin / hooks / class-wdkit-login-ajax.php

class-wdkit-login-ajax.php in WDesignKit – AI Templates, Widget Builder & MCP Workflow 2.7.0, at includes/admin/hooks/class-wdkit-login-ajax.php

513 lines 15.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file that defines the core plugin class
4 *
5 * @link https://posimyth.com/
6 * @since 1.1.3
7 *
8 * @package Wdesignkit
9 * @subpackage Wdesignkit/includes
10 */
11
12 /**
13 * Exit if accessed directly.
14 * */
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 if ( ! class_exists( 'Wdkit_Login_Ajax' ) ) {
20
21 /**
22 * It is wdesignkit Main Class
23 *
24 * @since 1.1.1
25 */
26 class Wdkit_Login_Ajax {
27
28 /**
29 * Member Variable
30 *
31 * @var instance
32 */
33 private static $instance;
34
35 /**
36 * Member Variable
37 *
38 * @var staring $wdkit_api
39 */
40 public $wdkit_api = WDKIT_SERVER_API_URL . 'api/wp/';
41
42 /**
43 * Initiator
44 */
45 public static function get_instance() {
46 if ( ! isset( self::$instance ) ) {
47 self::$instance = new self();
48 }
49
50 return self::$instance;
51 }
52
53 /**
54 * Define the core functionality of the plugin.
55 */
56 public function __construct() {
57 add_filter( 'wp_wdkit_login_ajax', array( $this, 'wp_wdkit_login_ajax_call' ) );
58 }
59
60 /**
61 * Get Wdkit Api Call Ajax.
62 *
63 * @since 1.1.1
64 */
65 public function wp_wdkit_login_ajax_call( $type ) {
66
67 check_ajax_referer( 'wdkit_nonce', 'kit_nonce' );
68
69 if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
70 wp_send_json_error( array( 'content' => __( 'Insufficient permissions.', 'wdesignkit' ) ) );
71 }
72
73 if ( ! $type ) {
74 $this->wdkit_error_msg( __( 'Something went wrong.', 'wdesignkit' ) );
75 }
76
77 switch ( $type ) {
78 case 'wkit_login':
79 $response = $this->wdkit_login();
80 break;
81 case 'api_login':
82 $response = $this->wdkit_api_login();
83 break;
84 case 'social_login':
85 $response = $this->wdkit_social_login();
86 break;
87 case 'forgot_password':
88 $response = $this->wdkit_forgot_password();
89 break;
90 case 'wdkit_user_signup':
91 $data = $this->wdkit_user_signup();
92 break;
93 }
94
95 wp_send_json( $response );
96 // wp_die();
97 }
98
99 /**
100 *
101 * It is Use for user login with email and password.
102 *
103 * @since 1.0.0
104 */
105 public function wdkit_login() {
106 $user_email = isset( $_POST['user_email'] ) ? strtolower( sanitize_email( wp_unslash( $_POST['user_email'] ) ) ) : false;
107 $user_password = isset( $_POST['user_password'] ) ? sanitize_text_field( wp_unslash( $_POST['user_password'] ) ) : false;
108 $login_type = isset( $_POST['login_type'] ) ? sanitize_text_field( wp_unslash( $_POST['login_type'] ) ) : false;
109 $site_url = isset( $_POST['site_url'] ) ? esc_url_raw( wp_unslash( $_POST['site_url'] ) ) : '';
110
111 $user_key = wdesignkit_cloud_session_key( $user_email );
112 $response = '';
113
114 /* get_transient() already returns false once this expires, so there is nothing to
115 * clear beforehand - deleting it here unconditionally, before ever reading it, made
116 * the elseif below unreachable and turned every login into a fresh remote call. See
117 * ClickUp 14ynqxywnaf. */
118 $get_login = get_transient( 'wdkit_auth_' . $user_key );
119
120 if ( ! empty( $user_email ) && ! empty( $user_password ) && false === $get_login ) {
121 $response = WDesignKit_Data_Query::get_data(
122 'login',
123 array(
124 'user_email' => $user_email,
125 'password' => $user_password,
126 'site_url' => $site_url,
127 )
128 );
129
130 /* Normalised before any subscript: a failed request returns WP_Error, and in
131 * PHP 8 even empty() on it is a fatal "Cannot use object of type WP_Error as
132 * array". */
133 $response = is_array( $response ) ? $response : array();
134
135 if ( ! empty( $response ) && ! empty( $response['success'] ) ) {
136 if ( ! empty( $response['message'] ) && ! empty( $response['token'] ) ) {
137 if ( false === get_transient( 'wdkit_auth_' . $user_key ) ) {
138 $this->wdkit_set_time_out( $user_key, $user_email, $response['token'], $login_type );
139 }
140 }
141 }
142 } elseif ( ! empty( $get_login ) && ! empty( $get_login['token'] ) ) {
143 $response = array_merge(
144 array(
145 'success' => true,
146 'message' => esc_html__( 'Success! Login successful.', 'wdesignkit' ),
147 'description' => esc_html__( 'Login successful. Keep it up!', 'wdesignkit' ),
148 ),
149 $get_login
150 );
151 }
152
153 wp_send_json( $response );
154 wp_die();
155 }
156
157 /**
158 *
159 * This Function is used for Login with Api (token)
160 *
161 * @version 1.0.0
162 */
163 protected function wdkit_api_login() {
164 $user_token = isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '';
165 $login_type = isset( $_POST['login_type'] ) ? sanitize_text_field( wp_unslash( $_POST['login_type'] ) ) : '';
166
167 $site_url = isset( $_POST['site_url'] ) ? esc_url_raw( wp_unslash( $_POST['site_url'] ) ) : '';
168
169 if ( empty( $user_token ) ) {
170 $result = array(
171 'success' => false,
172 'token' => '',
173 'data' => array(
174 'message' => $this->e_msg_login,
175 'description' => $this->e_desc_login,
176 ),
177 );
178
179 wp_send_json( $result );
180 wp_die();
181 }
182
183 $array_data = array(
184 'token' => $user_token,
185 'site_url' => $site_url,
186 );
187
188 $response = $this->wkit_api_call( $array_data, 'login/api' );
189
190 $success = ! empty( $response['success'] ) ? is_bool( $response['success'] ) : false;
191
192 if ( empty( $success ) ) {
193 $result = array(
194 'data' => $response,
195 'token' => '',
196 'success' => false,
197 );
198
199 wp_send_json( $result );
200 wp_die();
201 }
202
203 $response = json_decode( wp_json_encode( $response['data'] ), true );
204 $user_email = ! empty( $response['user']['user_email'] ) ? sanitize_email( $response['user']['user_email'] ) : '';
205 $user_key = wdesignkit_cloud_session_key( $user_email );
206
207 $this->wdkit_set_time_out( $user_key, $user_email, $user_token, $login_type );
208
209 $result = array(
210 'success' => true,
211 'data' => $response,
212 'token' => $user_token,
213 );
214
215 wp_send_json( $result );
216 wp_die();
217 }
218
219 /**
220 *
221 * This Function is used for social Login
222 *
223 * @version 1.0.0
224 */
225 protected function wdkit_social_login() {
226 $user_state = isset( $_POST['state'] ) ? sanitize_text_field( wp_unslash( $_POST['state'] ) ) : '';
227 $login_type = isset( $_POST['login_type'] ) ? sanitize_text_field( wp_unslash( $_POST['login_type'] ) ) : '';
228
229 $site_url = isset( $_POST['site_url'] ) ? esc_url_raw( wp_unslash( $_POST['site_url'] ) ) : '';
230
231 $array_data = array(
232 'state' => $user_state,
233 'site_url' => $site_url,
234 );
235
236 $response = $this->wkit_api_call( $array_data, 'login/ip' );
237 $success = ! empty( $response['success'] ) ? $response['success'] : false;
238
239 if ( empty( $success ) ) {
240 $result = array(
241 'data' => $response,
242 'success' => false,
243 );
244
245 wp_send_json( $result );
246 wp_die();
247 }
248
249 $response = json_decode( wp_json_encode( $response['data'] ), true );
250 $user_email = ! empty( $response['user']['user_email'] ) ? sanitize_email( $response['user']['user_email'] ) : '';
251 $user_token = ! empty( $response['token'] ) ? sanitize_text_field( $response['token'] ) : '';
252 $user_key = wdesignkit_cloud_session_key( $user_email );
253
254 if ( ! empty( $response ) && ! empty( $user_token ) ) {
255 $this->wdkit_set_time_out( $user_key, $user_email, $user_token, $login_type );
256 }
257
258 $result = array(
259 'data' => $response,
260 'token' => $user_token,
261 );
262
263 wp_send_json( $result );
264 wp_die();
265 }
266
267 /**
268 *
269 * This Function is used for forgot password functionality
270 *
271 * @since 2.0.0
272 */
273 public function wdkit_forgot_password() {
274 $user_email = isset( $_POST['email'] ) ? strtolower( sanitize_email( wp_unslash( $_POST['email'] ) ) ) : false;
275 $site_url = isset( $_POST['site_url'] ) ? esc_url_raw( wp_unslash( $_POST['site_url'] ) ) : '';
276
277 if ( empty( $user_email ) ) {
278 $message = esc_html__( 'Please provide a valid email address.', 'wdesignkit' );
279 $description = esc_html__( 'Please provide a valid email address.', 'wdesignkit' );
280 $response = $this->wdkit_response( false, $message, $description );
281
282 wp_send_json( $response );
283 wp_die();
284 }
285
286 $array_data = array(
287 'email' => $user_email,
288 'site_url' => $site_url,
289 );
290
291 $response = $this->wkit_api_call( $array_data, 'password/forgot' );
292 $success = ! empty( $response['success'] ) ? $response['success'] : false;
293
294 if ( empty( $success ) ) {
295 $message = ! empty( $response['massage'] ) ? $response['massage'] : esc_html__( 'Failed to send reset email. Please try again.', 'wdesignkit' );
296 $description = ! empty( $response['massage'] ) ? $response['massage'] : esc_html__( 'Failed to send reset email. Please try again.', 'wdesignkit' );
297 $response = $this->wdkit_response( false, $message, $description );
298
299 wp_send_json( $response );
300 wp_die();
301 }
302
303 $message = ! empty( $response['data']->message ) ? $response['data']->message : esc_html__( 'Password reset email sent successfully!', 'wdesignkit' );
304 $description = ! empty( $response['data']->description ) ? $response['data']->description : esc_html__( 'Password reset email sent successfully!', 'wdesignkit' );
305 $success = ! empty( $response['data']->success ) ? $response['data']->success : false;
306
307 $response = $this->wdkit_response( $success, $message, $description );
308
309 wp_send_json( $response );
310 wp_die();
311 }
312
313 /**
314 * Handle user signup functionality
315 *
316 * @since 2.0.0
317 */
318 protected function wdkit_user_signup(){
319 $username = ! empty( $_POST['username'] ) ? sanitize_text_field($_POST['username']) : '';
320 $user_password = ! empty( $_POST['password'] ) ? sanitize_text_field($_POST['password']) : '';
321 $user_email = ! empty( $_POST['email'] ) ? sanitize_email($_POST['email']) : '';
322 $user_key = wdesignkit_cloud_session_key( $user_email );
323 $response = '';
324
325 /* Same reasoning as wdkit_login(): get_transient() already returns false once this
326 * expires, so an unconditional delete before ever reading it just made the elseif
327 * below unreachable. See ClickUp 14ynqxywnaf. */
328 $get_login = get_transient( 'wdkit_auth_' . $user_key );
329
330 if ( ! empty( $user_email ) && ! empty( $user_password ) && false === $get_login ) {
331 $signup_array = array(
332 'fullname' => $username,
333 'password' => $user_password,
334 'user_email' => $user_email,
335 );
336
337 $response = WDesignKit_Data_Query::get_data(
338 'signup',
339 $signup_array
340 );
341
342 /* Normalised before any subscript: a failed request returns WP_Error, and in
343 * PHP 8 even empty() on it is a fatal "Cannot use object of type WP_Error as
344 * array". */
345 $response = is_array( $response ) ? $response : array();
346
347 if ( ! empty( $response ) && ! empty( $response['success'] ) ) {
348 if ( ! empty( $response['message'] ) && ! empty( $response['token'] ) ) {
349 if ( false === get_transient( 'wdkit_auth_' . $user_key ) ) {
350 $this->wdkit_set_time_out( $user_key, $user_email, $response['token'], '' );
351 }
352 }
353 }
354 } elseif ( ! empty( $get_login ) && ! empty( $get_login['token'] ) ) {
355 $response = array_merge(
356 array(
357 'success' => true,
358 'message' => esc_html__( 'Success! Login successful.', 'wdesignkit' ),
359 'description' => esc_html__( 'Login successful. Keep it up!', 'wdesignkit' ),
360 ),
361 $get_login
362 );
363 }
364
365 wp_send_json( $response );
366 wp_die();
367 }
368
369 /* All below functions are helper functions for this file */
370
371 /**
372 *
373 * This Function is used for API call
374 *
375 * @since 1.0.0
376 *
377 * @param array $data give array.
378 * @param array $name store data.
379 */
380 protected function wkit_api_call( $data, $name ) {
381 $u_r_l = $this->wdkit_api;
382
383 if ( empty( $u_r_l ) ) {
384 return array(
385 'massage' => esc_html__( 'API Not Found', 'wdesignkit' ),
386 'success' => false,
387 );
388 }
389
390 $args = array(
391 'method' => 'POST',
392 'body' => $data,
393 'timeout' => 100,
394 );
395 $response = wp_remote_post( $u_r_l . $name, $args );
396
397 if ( is_wp_error( $response ) ) {
398 $error_message = $response->get_error_message();
399
400 /* Translators: %s is a placeholder for the error message */
401 $error_message = sprintf( esc_html__( 'API request error: %s', 'wdesignkit' ), esc_html( $error_message ) );
402
403 return array(
404 'massage' => $error_message,
405 'success' => false,
406 );
407 }
408
409 $status_code = wp_remote_retrieve_response_code( $response );
410 if ( 200 === $status_code ) {
411
412 return array(
413 'data' => json_decode( wp_remote_retrieve_body( $response ) ),
414 'massage' => esc_html__( 'Success', 'wdesignkit' ),
415 'status' => $status_code,
416 'success' => true,
417 );
418 }
419
420 $error_message = sprintf( 'Server error: %d', esc_html( $status_code ) );
421
422 if ( isset( $error_data->message ) ) {
423 $error_message .= ' (' . $error_data->message . ')';
424 }
425
426 return array(
427 'massage' => $error_message,
428 'status' => $status_code,
429 'success' => false,
430 );
431 }
432
433 /**
434 * This Function is used for API call
435 *
436 * @since 1.0.0
437 *
438 * @param string $user_key Dynamic key.
439 * @param string $user_email User email.
440 * @param string $token User token.
441 */
442 protected function wdkit_set_time_out( $user_key, $user_email, $token, $login_type = '' ) {
443
444 if ( 'normal' === $login_type ) {
445 set_transient(
446 'wdkit_auth_' . $user_key,
447 array(
448 'user_email' => sanitize_email( $user_email ),
449 'token' => $token,
450 ),
451 7776000
452 );
453 } else {
454 set_transient(
455 'wdkit_auth_' . $user_key,
456 array(
457 'user_email' => sanitize_email( $user_email ),
458 'token' => $token,
459 ),
460 86400
461 );
462 }
463
464 /* Points THIS WP user at the session just stored, so
465 * wdesignkit_mcp_find_auth_session()'s first (authoritative) lookup finds it
466 * directly instead of falling back to an email-match or a full-table "newest
467 * wins" scan - the fallback that a site with more than one cloud login stored
468 * (e.g. an old session from a previous account) can resolve to the wrong one,
469 * or fail outright once the winning transient's own token is stale. Without
470 * this pointer, every admin_init registration attempt after login reads no
471 * session at all and Wdkit_Import_Remote::register() reports "Not signed in."
472 * despite a valid transient sitting right there. The MCP ability login
473 * (wdesignkit_mcp_login()) already does this; this is the same plugin's
474 * older, still-active email/password/API-key/social login path, which never
475 * did. */
476 if ( function_exists( 'wdesignkit_mcp_remember_session' ) ) {
477 wdesignkit_mcp_remember_session( $user_key );
478 }
479
480 /* Register this site for website-queued imports with the session just stored. */
481 if ( class_exists( 'Wdkit_Import_Remote' ) ) {
482 Wdkit_Import_Remote::register_after_login();
483 }
484 }
485
486 /**
487 * Error JSON message
488 *
489 * @param array $data give array.
490 * @param string $status api code number.
491 * */
492 public function wdkit_error_msg( $data = null, $status = null ) {
493 wp_send_json_error( $data );
494 wp_die();
495 }
496
497 public function wdkit_response( $success, $message, $description, $data = [] ) {
498
499 $result = array(
500 'success' => $success,
501 'message' => $message,
502 'description' => $description,
503 'data' => $data,
504 );
505
506 return $result;
507 }
508
509 }
510
511 Wdkit_Login_Ajax::get_instance();
512 }
513