PluginProbe ʕ •ᴥ•ʔ
CookieYes – Cookie Banner for Cookie Consent (Easy to setup GDPR/CCPA Compliant Cookie Notice) / 3.5.0
CookieYes – Cookie Banner for Cookie Consent (Easy to setup GDPR/CCPA Compliant Cookie Notice) v3.5.0
3.5.1 3.5.0 3.4.2 trunk 1.0.1 1.0.3 1.2 1.2.1 1.2.2 1.3 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 3.0.0 3.0.1 3.0.2 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.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.2.0 3.2.1 3.2.10 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.3.9.1 3.4.0 3.4.1
cookie-law-info / legacy / includes / class-cookie-law-info-cookieyes.php
cookie-law-info / legacy / includes Last commit date
class-cookie-law-info-activator.php 1 month ago class-cookie-law-info-cookieyes.php 1 month ago class-cookie-law-info-deactivator.php 1 month ago class-cookie-law-info-i18n.php 1 month ago class-cookie-law-info-languages.php 1 month ago class-cookie-law-info-loader.php 1 month ago class-cookie-law-info-review-request.php 1 month ago class-cookie-law-info.php 1 month ago index.php 1 month ago
class-cookie-law-info-cookieyes.php
1213 lines
1 <?php
2 /**
3 * Cookieyes Integration
4 *
5 * @version 1.9.6
6 * @package CookieLawInfo
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12 if ( ! class_exists( 'Cookie_Law_Info_Cookieyes' ) ) {
13 /**
14 * Cookieyes library
15 */
16 class Cookie_Law_Info_Cookieyes {
17 /**
18 * CookieYes options
19 *
20 * @var array
21 */
22 protected $cookieyes_options;
23 /**
24 * Current webstite URL
25 *
26 * @var string
27 */
28 protected $website_url;
29 /**
30 * CookieYes token
31 *
32 * @var string
33 */
34 protected $token;
35 /**
36 * CookieYes connection status
37 *
38 * @var bool
39 */
40 protected $ckyes_status;
41 /**
42 * Allowed CookieYes ajax actions
43 *
44 * @var array
45 */
46 protected $ckyes_actions;
47 /**
48 * Email of the user
49 *
50 * @var string
51 */
52 public $ckyes_scan_data;
53
54 public $user_email;
55 /**
56 * Current module name
57 *
58 * @var [type]
59 */
60 public $module_id;
61
62 const API_BASE_PATH = 'https://app.cookieyes.com/api/wp-basic/v1/';
63
64 const EC_WT_CKYES_CONNECTION_FAILED = 100;
65 const EC_WT_CKYES_INVALID_CREDENTIALS = 101;
66 const EC_WT_CKYES_ALREADY_EXIST = 102;
67 const EC_WT_CKYES_LICENSE_NOT_ACTIVATED = 103;
68 const EC_WT_CKYES_SCAN_LIMIT_REACHED = 104;
69 const EC_WT_CKYES_DISCONNECTED = 105;
70 const EC_WT_CKYES_ACTIVE_SCAN = 106;
71 const EC_WT_CKYES_PENDING_VERIFICATION = 107;
72 const EC_WT_CKYES_NOT_REGISTERED = 108;
73 const EC_WT_CKYES_EMAIL_ALREADY_VERIFIED = 109;
74
75 const WT_CKYES_CONNECTION_SUCCESS = 200;
76 const WT_CKYES_SCAN_INITIATED = 201;
77 const WT_CKYES_PWD_RESET_SENT = 202;
78 const WT_CKYES_EMAIL_VERIFICATION_SENT = 203;
79 const WT_CKYES_ABORT_SUCCESSFUL = 204;
80
81 /**
82 * Initialize CookieYes scanner library
83 */
84 public function __construct() {
85 $this->ckyes_actions = $this->get_ckyes_actions();
86 $this->module_id = 'cookieyes';
87
88 add_action( 'init', array( $this, 'init' ) );
89 add_action( 'wp_ajax_cookieyes_ajax_main_controller', array( $this, 'ajax_main_controller' ), 10, 0 );
90 add_action( 'wt_cli_after_advanced_settings', array( $this, 'ckyes_settings' ), 11 );
91 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
92 }
93 /**
94 * Initialize CookieYes actions
95 *
96 * @return void
97 */
98 public function init() {
99 add_action( 'admin_footer', array( $this, 'ckyes_forms' ) );
100 add_filter( 'wt_cli_enable_ckyes_branding', array( $this, 'show_ckyes_branding' ) );
101 add_filter( 'wt_cli_ckyes_account_widget', array( $this, 'add_ckyes_account_widget' ) );
102 }
103 /**
104 * Return supported ajax actions
105 *
106 * @return array
107 */
108 public function get_ckyes_actions() {
109 return array(
110 'register',
111 'login',
112 'reset_password',
113 'connect_disconnect',
114 'resend_email',
115 'delete_account',
116 );
117 }
118 /**
119 * AJAX main controller
120 *
121 * @return void
122 */
123 public function ajax_main_controller() {
124 check_ajax_referer( $this->module_id, '_wpnonce' );
125 if ( ! current_user_can( 'manage_options' ) ) {
126 wp_die( esc_html__( 'You do not have sufficient permission to perform this operation', 'cookie-law-info' ) );
127 }
128 if ( isset( $_POST['sub_action'] ) ) {
129
130 $sub_action = sanitize_text_field( wp_unslash( $_POST['sub_action'] ) ); // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
131
132 if ( in_array( $sub_action, $this->ckyes_actions, true ) && method_exists( $this, $sub_action ) ) {
133
134 $response = $this->{$sub_action}();
135 $data = array();
136 $status = ( isset( $response['status'] ) ? $response['status'] : false );
137 $status_code = ( isset( $response['code'] ) ? $response['code'] : '' );
138 $message = ( isset( $response['message'] ) ? $response['message'] : false );
139 $html = ( isset( $response['html'] ) ? $response['html'] : false );
140 $data['status'] = $status;
141 if ( ! empty( $status_code ) ) {
142 $data['code'] = $status_code;
143 $data['html'] = $html;
144 if ( false === $message ) {
145 $data['message'] = $this->get_ckyes_message( $status_code );
146 } else {
147 $data['message'] = $message;
148 }
149 }
150 if ( true === $status ) {
151 wp_send_json_success( $data );
152 }
153 wp_send_json_error( $data );
154 }
155 }
156 $data['message'] = __( 'Invalid request', 'cookie-law-info' );
157 wp_send_json_error( $data );
158 exit();
159 }
160 /**
161 * CookieYes account status widget
162 *
163 * @return string
164 */
165 public function add_ckyes_account_widget() {
166 if ( $this->get_cookieyes_status() === false || $this->get_cookieyes_status() === 0 ) {
167 return;
168 }
169 if ( $this->get_cookieyes_status() === false ) {
170 return;
171 }
172 $ckyes_account_status_text = __( 'Connected to CookieYes', 'cookie-law-info' );
173 $ckyes_account_action = 'disconnect';
174 $ckyes_account_action_text = __( 'Disconnect', 'cookie-law-info' );
175 $image_directory = CLI_PLUGIN_URL . 'admin/images/';
176 $ckyes_account_status_icon = $image_directory . 'add.svg';
177
178 if ( $this->get_cookieyes_status() === 0 ) {
179 $ckyes_account_action = 'connect';
180 $ckyes_account_action_text = '';
181 $ckyes_account_status_icon = $image_directory . 'remove.svg';
182 $ckyes_account_status_text = __( 'Disconnected from CookieYes', 'cookie-law-info' );
183 }
184 $html = '<span class="wt-cli-ckyes-account-widget-container">';
185 $html .= '<span class="wt-cli-ckyes-status-icon"><img src="' . $ckyes_account_status_icon . '" style="max-width:100%; " alt=""></span>';
186 $html .= '<span class="wt-cli-ckyes-status-text">' . $ckyes_account_status_text . '</span>';
187 $html .= '<span><a href="#" class="wt-cli-ckyes-account-action" data-action="' . $ckyes_account_action . '">' . $ckyes_account_action_text . '</a></span>';
188 $html .= '</span>';
189 return $html;
190 }
191 /**
192 * Enqueue the javascript file for CookieYes API
193 *
194 * @return void
195 */
196 public function enqueue_scripts() {
197
198 $allowed_pages = apply_filters( 'wt_cli_ckyes_allowed_pages', array( 'cookie-law-info-cookie-scaner', 'cookie-law-info' ) );
199 if ( isset( $_GET['post_type'] ) && CLI_POST_TYPE === $_GET['post_type'] && isset( $_GET['page'] ) && in_array( $_GET['page'], $allowed_pages, true ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
200 $params = array(
201 'nonce' => wp_create_nonce( esc_html( $this->module_id ) ),
202 'ajax_url' => admin_url( 'admin-ajax.php' ),
203 'messages' => array(
204 'error' => __( 'Invalid request', 'cookie-law-info' ),
205 'delete_success' => __( 'Successfully deleted!', 'cookie-law-info' ),
206 'delete_failed' => __( 'Delete failed, please try again later', 'cookie-law-info' ),
207 ),
208 );
209 wp_enqueue_script( 'cookie-law-info-ckyes-admin', CLI_PLUGIN_URL . 'admin/js/cookie-law-info-ckyes.js', array( 'cookie-law-info' ), CLI_VERSION, true );
210 wp_localize_script( 'cookie-law-info-ckyes-admin', 'ckyes_admin', $params );
211 }
212 }
213 /**
214 * Login and password reset HTML for the scanner
215 *
216 * @return void
217 */
218 public function ckyes_forms() {
219 $allowed_pages = apply_filters( 'wt_cli_ckyes_allowed_pages', array( 'cookie-law-info-cookie-scaner' ) );
220
221 if ( isset( $_GET['post_type'] ) && CLI_POST_TYPE === $_GET['post_type'] && isset( $_GET['page'] ) && in_array( $_GET['page'], $allowed_pages, true ) ) : // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
222 ?>
223 <style>
224 .wt-cli-ckyes-login-icon>.dashicons {
225 font-size: 50px;
226 width: initial;
227 height: initial;
228 }
229
230 .wt-cli-ckyes-login-icon {
231 width: 80px;
232 height: 80px;
233 margin: 0 auto;
234 display: flex;
235 align-items: center;
236 justify-content: center;
237 border-radius: 50%;
238 background: #f2f2f2;
239 }
240
241 .wt-cli-form-input {
242 display: block;
243 width: 100%;
244 height: 45px;
245 border: 1px solid #4041424a !important;
246 margin-top: 10px;
247 }
248
249 .wt-cli-action-container {
250 display: flex;
251 align-items: center;
252 justify-content: space-between;
253 margin-top: 10px;
254 }
255
256 button.wt-cli-action.button {
257 padding: 2px 24px;
258 min-width: 100px;
259 font-weight: 500;
260 }
261
262 #wt-cli-ckyes-modal-login {
263 padding: 45px 25px;
264 width: 430px;
265 }
266 </style>
267 <div class='wt-cli-modal' id='wt-cli-ckyes-modal-password-reset'>
268 <div class="wt-cli-modal-header">
269 <h4><?php echo esc_html__( 'Reset Password', 'cookie-law-info' ); // phpcs:ignore WordPress.Security.EscapeOutput ?></h4>
270 </div>
271 <div class="wt-cli-modal-body">
272 <form id="wt-cli-ckyes-form-password-reset">
273 <input type="email" name="ckyes-reset-email" class="wt-cli-form-input" placeholder="<?php echo esc_attr__( 'Email', 'cookie-law-info' ); ?>" value="<?php echo esc_attr( $this->get_user_email() ); ?>" />
274 <div class="wt-cli-action-container">
275 <button id="wt-cli-ckyes-password-reset-btn" class="wt-cli-action button button-primary"><?php echo esc_html__( 'Send password reset email', 'cookie-law-info' ); ?></button>
276 </div>
277
278 </form>
279 </div>
280 </div>
281 <div class='wt-cli-modal' id='wt-cli-ckyes-modal-register'>
282 <span class="wt-cli-modal-js-close">×</span>
283 <div class="wt-cli-modal-header"><h4><?php echo esc_html__( 'Welcome to CookieYes', 'cookie-law-info' ); ?></h4></div>
284 <div class="wt-cli-modal-body">
285 <p><?php echo esc_html__( 'Enter your email to create an account with CookieYes. By clicking “Connect”, your CookieYes account will be created automatically and you can start scanning your website for cookies right away!', 'cookie-law-info' ); // phpcs:ignore WordPress.Security.EscapeOutput ?></p>
286 <form id="wt-cli-ckyes-form-register">
287 <input type="email" name="ckyes-email" class="wt-cli-form-input" placeholder="<?php echo esc_attr__( 'Email', 'cookie-law-info' ); ?>" value = "<?php echo esc_attr( $this->get_user_email() ); ?>" />
288 <div class="wt-cli-action-container">
289 <div class="wt-cli-action-group">
290 <button id="wt-cli-ckyes-register-btn" class="wt-cli-action button button-primary"><?php echo esc_html__( 'Connect', 'cookie-law-info' ); ?></button>
291 </div>
292 </div>
293 </form>
294 </div>
295 </div>
296 <?php
297 endif;
298 }
299 /**
300 * CookieYes message handler
301 *
302 * @param int $msg_code Message code.
303 * @return string
304 */
305 public function get_ckyes_message( $msg_code ) {
306 switch ( $msg_code ) {
307 case self::EC_WT_CKYES_CONNECTION_FAILED:
308 $msg = __( 'Could not establish connection with scanner! please try again later', 'cookie-law-info' );
309 break;
310 case self::EC_WT_CKYES_INVALID_CREDENTIALS:
311 $msg = __( 'Invalid credentials', 'cookie-law-info' );
312 break;
313 case self::EC_WT_CKYES_ALREADY_EXIST:
314 $msg = __( 'You already have an account with CookieYes.', 'cookie-law-info' );
315 break;
316 case self::EC_WT_CKYES_LICENSE_NOT_ACTIVATED:
317 $msg = __( 'License is not activated, please activate your license and try again', 'cookie-law-info' );
318 break;
319 case self::EC_WT_CKYES_DISCONNECTED:
320 $msg = __( 'Disconnected with cookieyes, please connect and scan again', 'cookie-law-info' );
321 break;
322 case self::EC_WT_CKYES_SCAN_LIMIT_REACHED:
323 $msg = __( 'Your monthly scan limit is reached please try again later', 'cookie-law-info' );
324 break;
325 case self::EC_WT_CKYES_ACTIVE_SCAN:
326 $msg = __( 'A scanning is already in progress please try again after some time', 'cookie-law-info' );
327 break;
328 case self::WT_CKYES_CONNECTION_SUCCESS:
329 $msg = __( 'Successfully connected with CookieYes', 'cookie-law-info' );
330 break;
331 case self::WT_CKYES_PWD_RESET_SENT:
332 $msg = __( 'A password reset message has been sent to your email address. Click the link in the email to reset your password', 'cookie-law-info' );
333 break;
334 case self::WT_CKYES_EMAIL_VERIFICATION_SENT:
335 $msg = __( 'A email verification link has been sent to your email address. Click the link in the email to verify your account', 'cookie-law-info' );
336 break;
337 case self::EC_WT_CKYES_EMAIL_ALREADY_VERIFIED:
338 $msg = __( 'Email has already verified', 'cookie-law-info' );
339 break;
340 default:
341 $msg = '';
342 break;
343 }
344 return $msg;
345 }
346 /**
347 * Return the current user email
348 *
349 * @return string
350 */
351 public function get_user_email() {
352 if ( ! $this->user_email ) {
353 $cookieyes_options = $this->get_cookieyes_options();
354 $this->user_email = ( isset( $cookieyes_options['email'] ) ? $cookieyes_options['email'] : '' );
355 }
356 return sanitize_email( $this->user_email );
357 }
358 /**
359 * Get CookieYes access token
360 *
361 * @return string
362 */
363 public function get_access_token() {
364 if ( ! $this->token ) {
365 $cookieyes_options = $this->get_cookieyes_options();
366 $this->token = ( isset( $cookieyes_options['token'] ) ? $cookieyes_options['token'] : '' );
367 }
368 return $this->token;
369 }
370 /**
371 * Save access token
372 *
373 * @param string $token Token received from the CookieYes.
374 * @return void
375 * @throws Exception Error message.
376 */
377 public function set_access_token( $token ) {
378 if ( is_string( $token ) ) {
379 $json = json_decode( $token, true );
380 if ( $json ) {
381 $token = $json;
382 } else {
383 // assume $token is just the token string.
384 $token = array(
385 'access_token' => $token,
386 );
387 }
388 }
389 if ( null === $token ) {
390 throw new Exception( esc_html__( 'Invalid json token', 'cookie-law-info' ) );
391 }
392 if ( ! isset( $token['access_token'] ) ) {
393 throw new Exception( esc_html__( 'Invalid token format', 'cookie-law-info' ) );
394 }
395 $this->token = $token;
396 }
397 /**
398 * Reset the token values
399 *
400 * @return void
401 */
402 public function reset_token() {
403 delete_option( 'wt_cli_cookieyes_options' );
404 }
405 /**
406 * Cookieyes options like status, access token etc
407 *
408 * @return array
409 */
410 public function get_cookieyes_options() {
411 if ( ! $this->cookieyes_options ) {
412 $cky_license = array(
413 'status' => 0,
414 'token' => '',
415 'email' => '',
416 );
417 $cookieyes_options = get_option( 'wt_cli_cookieyes_options', false );
418 if ( false !== $cookieyes_options && is_array( $cookieyes_options ) ) {
419
420 $cky_license['status'] = intval( isset( $cookieyes_options['status'] ) ? $cookieyes_options['status'] : 0 );
421 $cky_license['token'] = isset( $cookieyes_options['token'] ) ? $cookieyes_options['token'] : '';
422 $cky_license['email'] = isset( $cookieyes_options['email'] ) ? $cookieyes_options['email'] : '';
423 } else {
424 return false;
425 }
426 $this->cookieyes_options = $cky_license;
427 }
428
429 return $this->cookieyes_options;
430 }
431 /**
432 * Return current status of the CookieYes
433 *
434 * @return bool
435 */
436 public function get_cookieyes_status() {
437
438 if ( ! $this->ckyes_status ) {
439 $cookieyes_options = $this->get_cookieyes_options();
440 $this->ckyes_status = ( isset( $cookieyes_options['status'] ) ? intval( $cookieyes_options['status'] ) : false );
441 }
442 return $this->ckyes_status;
443 }
444 /**
445 * Save CookieYes options
446 *
447 * @param array $options options.
448 * @return void
449 */
450 public function set_cookieyes_options( $options ) {
451 $cky_license = array(
452 'status' => 0,
453 'token' => '',
454 'email' => '',
455 );
456 $this->ckyes_status = $cky_license['status'] = ( isset( $options['status'] ) ? intval( $options['status'] ) : 0 );
457 $this->token = $cky_license['token'] = isset( $options['token'] ) ? sanitize_text_field( $options['token'] ) : '';
458 $this->user_email = $cky_license['email'] = isset( $options['email'] ) ? sanitize_email( $options['email'] ) : '';
459
460 update_option( 'wt_cli_cookieyes_options', $cky_license );
461 }
462 /**
463 * Returns API base path
464 *
465 * @return string
466 */
467 public function get_base_path() {
468 return self::API_BASE_PATH;
469 }
470 /**
471 * Returns current website URL
472 *
473 * @return string
474 */
475 public function get_website_url() {
476 if ( ! $this->website_url ) {
477 $this->website_url = home_url();
478 }
479 return $this->website_url;
480 }
481 /**
482 * Parse data from a remote request response.
483 *
484 * @param array $raw_response raw response from a remote request.
485 * @return array
486 */
487 public function parse_raw_response( $raw_response ) {
488
489 $response_code = wp_remote_retrieve_response_code( $raw_response );
490 if ( 200 !== $response_code ) {
491 if ( 401 === $response_code ) {
492 $this->reset_token();
493 }
494 return false;
495 }
496 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
497 return $response;
498 }
499 /**
500 * Returns default response code and message
501 *
502 * @return array
503 */
504 public function get_default_response() {
505 $api_response = array(
506 'status' => false,
507 'code' => 100,
508 );
509 return $api_response;
510 }
511 /**
512 * Do a remote a request
513 *
514 * @param string $request_type Request type POST. GET, PUT etc.
515 * @param string $endpoint API end point.
516 * @param boolean $body Request body.
517 * @param boolean $auth_token Bearer token.
518 * @return array
519 */
520 public function wt_remote_request( $request_type = 'GET', $endpoint = '', $body = false, $auth_token = false ) {
521
522 $request_args = array(
523 'timeout' => 60,
524 'headers' => array(),
525 );
526 $request_args['headers']['Content-Type'] = 'application/json';
527 $request_args['headers']['Accept'] = 'application/json';
528
529 if ( false !== $body ) {
530 $request_args['body'] = json_encode( $body );
531 }
532 if ( false !== $auth_token ) {
533 $request_args['headers']['Authorization'] = 'Bearer ' . $auth_token;
534 }
535 // Request types.
536 switch ( $request_type ) {
537 case 'GET':
538 $raw_response = wp_remote_get(
539 $endpoint,
540 $request_args
541 );
542 break;
543
544 case 'PUT':
545 case 'POST':
546 $raw_response = wp_remote_post(
547 $endpoint,
548 $request_args
549 );
550 break;
551 default:
552 break;
553 }
554 if ( $raw_response ) {
555 $response = $this->parse_raw_response( $raw_response );
556 return $response;
557 }
558 return false;
559 }
560 /**
561 * Register API , create an account with CookieYes
562 *
563 * @return array
564 */
565 public function register() {
566 check_ajax_referer( $this->module_id, '_wpnonce' );
567
568 $api_response = $this->get_default_response();
569 $endpoint = $this->get_base_path() . 'users/register';
570
571 $url = $this->get_website_url();
572 $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
573 $this->user_email = $email;
574 if ( empty( $email ) || empty( $url ) ) {
575 $api_response['code'] = 101;
576 return $api_response;
577 }
578 $request_body = array(
579 'email' => $email,
580 'url' => $url,
581 );
582 $response = $this->wt_remote_request( 'POST', $endpoint, $request_body );
583 if ( isset( $response ) && is_array( $response ) ) {
584 if ( isset( $response['token'] ) ) {
585 $cky_options = array(
586 'status' => 2, // Waiting for email verification.
587 'token' => $response['token'],
588 'email' => $this->get_user_email(),
589 );
590 $this->set_cookieyes_options( $cky_options );
591 $api_response['status'] = true;
592 $api_response['code'] = self::WT_CKYES_EMAIL_VERIFICATION_SENT;
593 $api_response['html'] = $this->get_email_verification_html();
594
595 } else {
596 if ( isset( $response['status'] ) && $response['status'] == 'error' ) {
597 if ( isset( $response['error_code'] ) && $response['error_code'] == 1002 ) {
598 $api_response['status'] = false;
599 $api_response['code'] = self::EC_WT_CKYES_ALREADY_EXIST;
600 $api_response['html'] = $this->get_login_html();
601 }
602 }
603 }
604 }
605 return $api_response;
606 }
607 /**
608 * Login API
609 *
610 * @return array
611 */
612 public function login() {
613
614 check_ajax_referer( $this->module_id, '_wpnonce' );
615 $api_response = $this->get_default_response();
616 $endpoint = $this->get_base_path() . 'users/login';
617
618 $url = $this->get_website_url();
619 $email = $this->get_user_email();
620
621 $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : $email;
622 $password = isset( $_POST['password'] ) ? $_POST['password'] : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
623
624 if ( empty( $email ) || empty( $url ) || empty( $password ) ) {
625 $api_response['code'] = 101;
626 return $api_response;
627 }
628 $request_body = array(
629 'email' => $email,
630 'url' => $url,
631 'password' => $password,
632 );
633 $response = $this->wt_remote_request( 'POST', $endpoint, $request_body );
634
635 if ( isset( $response ) && is_array( $response ) ) {
636
637 if ( isset( $response['status'] ) && 'error' === $response['status'] ) {
638
639 if ( isset( $response['error_code'] ) ) {
640 if ( 1003 == $response['error_code'] ) {
641 $api_response['code'] = 101;
642 } elseif ( 1012 == $response['error_code'] && isset( $response['error_message'] ) ) {
643 $api_response['code'] = 100;
644 $api_response['message'] = $response['error_message'];
645 }
646 }
647 } else {
648 if ( isset( $response['token'] ) ) {
649 $cky_options = array(
650 'status' => true,
651 'token' => $response['token'],
652 'email' => $this->get_user_email(),
653 );
654 $this->set_cookieyes_options( $cky_options );
655 $this->set_ckyes_branding_default();
656 $api_response['status'] = true;
657 $api_response['code'] = 200;
658 }
659 }
660 }
661 return $api_response;
662 }
663 /**
664 * Retrieve next scan ID from CookieYes.
665 *
666 * @param int $total_urls Total URLs to be scanned.
667 * @return array
668 */
669 public function get_next_scan_id( $total_urls ) {
670
671 $api_response = array(
672 'status' => false,
673 'code' => 100,
674 'scan_id' => '',
675 'scan_token' => '',
676 );
677 if ( $this->get_cookieyes_status() === 1 || $this->get_cookieyes_status() === 2 ) {
678
679 $token = $this->get_access_token();
680 if ( empty( $token ) ) {
681 return $api_response;
682 }
683 $endpoint = $this->get_base_path() . 'scan/create';
684 $request_body = array(
685 'page_limit' => $total_urls,
686 'scan_result_token' => $this->set_ckyes_scan_instance(),
687 );
688
689 $response = $this->wt_remote_request( 'POST', $endpoint, $request_body, $token );
690 if ( isset( $response ) && is_array( $response ) ) {
691
692 if ( isset( $response['status'] ) && $response['status'] === 'error' ) {
693 if ( isset( $response['error_code'] ) ) {
694 if ( $response['error_code'] == 1005 ) {
695 $response = $this->refresh_scan_token();
696 } elseif ( $response['error_code'] == 1007 ) {
697 $api_response['code'] = self::EC_WT_CKYES_PENDING_VERIFICATION;
698 }
699 }
700 }
701 if ( isset( $response['scan_id'] ) && $response['scan_token'] ) {
702 if ( $this->get_cookieyes_status() === 2 ) { // If email verified then set to activate state.
703 $this->change_status( true );
704 $this->set_ckyes_branding_default();
705
706 }
707 $api_response['status'] = true;
708 $api_response['scan_id'] = $response['scan_id'];
709 $api_response['scan_token'] = $response['scan_token'];
710 $api_response['code'] = self::WT_CKYES_SCAN_INITIATED;
711 }
712 } else {
713 return $api_response;
714 }
715 } else {
716 $api_response['code'] = self::EC_WT_CKYES_DISCONNECTED;
717 }
718 return $api_response;
719 }
720 /**
721 * Reset password API
722 *
723 * @return array
724 */
725 public function reset_password() {
726 check_ajax_referer( $this->module_id, '_wpnonce' );
727 $api_response = $this->get_default_response();
728
729 $endpoint = $this->get_base_path() . 'password/reset';
730
731 $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
732
733 if ( empty( $email ) ) {
734 $api_response['code'] = 101;
735 return $api_response;
736 }
737 $request_body = array(
738 'email' => $email,
739 );
740 $response = $this->wt_remote_request( 'POST', $endpoint, $request_body );
741 if ( isset( $response ) && is_array( $response ) ) {
742 if ( isset( $response['status'] ) && 'success' === $response['status'] ) {
743
744 $api_response['status'] = true;
745 $api_response['code'] = 202;
746 }
747 }
748
749 return $api_response;
750 }
751 /**
752 * Resend email verification
753 *
754 * @return array
755 */
756 public function resend_email() {
757 $api_response = $this->get_default_response();
758 $token = $this->get_access_token();
759 if ( empty( $token ) ) {
760 return $api_response;
761 }
762 $endpoint = $this->get_base_path() . 'users/resend-verification-email';
763 $response = $this->wt_remote_request( 'POST', $endpoint, false, $token );
764
765 if ( isset( $response ) && is_array( $response ) ) {
766 if ( isset( $response['status'] ) && 'resend_email_verification' === $response['status'] ) {
767 $api_response['status'] = true;
768 $api_response['code'] = self::WT_CKYES_EMAIL_VERIFICATION_SENT;
769 $api_response['html'] = $this->get_email_verification_html( false, false );
770 } elseif ( isset( $response['status'] ) && 'already_verified' === $response['status'] ) {
771 $api_response['status'] = false;
772 $api_response['code'] = self::EC_WT_CKYES_EMAIL_ALREADY_VERIFIED;
773 }
774 }
775 return $api_response;
776 }
777 /**
778 * Change connection status with CookieYes
779 *
780 * @return array
781 */
782 public function connect_disconnect() {
783 check_ajax_referer( $this->module_id, '_wpnonce' );
784 $api_response = array(
785 'status' => false,
786 'code' => 100,
787 'message' => '',
788 );
789 $message = __( 'Successfully disconnected with Cookieyes', 'cookie-law-info' );
790 $action = isset( $_POST['account_action'] ) ? sanitize_text_field( wp_unslash( $_POST['account_action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
791 if ( empty( $action ) ) {
792 $api_response['message'] = __( 'Could not identify the action', 'cookie-law-info' );
793 return $api_response;
794 }
795 if ( 'connect' === $action ) {
796 $this->change_status( true );
797 $message = __( 'Successfully connected with Cookieyes', 'cookie-law-info' );
798 } else {
799 $this->change_status( false );
800 }
801 $api_response['status'] = true;
802 $api_response['message'] = $message;
803 return $api_response;
804 }
805 /**
806 * Connect with CookieYes
807 *
808 * @return array
809 */
810 public function ckyes_connect() {
811 $api_response = array(
812 'status' => false,
813 );
814 $this->change_status( true );
815 $api_response['status'] = true;
816 return $api_response;
817 }
818 /**
819 * Change status
820 *
821 * @param boolean $status current status.
822 * @return void
823 */
824 public function change_status( $status = false ) {
825 $ckye_status = 0;
826 if ( true === $status ) {
827 $ckye_status = 1;
828 }
829 $ckyes_options = $this->get_cookieyes_options();
830 $ckyes_options['status'] = $ckye_status;
831 $this->set_cookieyes_options( $ckyes_options );
832 }
833 /**
834 * Refresh the current scan token
835 *
836 * @return array
837 */
838 protected function refresh_scan_token() {
839
840 $token = $this->get_access_token();
841
842 if ( empty( $token ) ) {
843 return false;
844 }
845 $endpoint = $this->get_base_path() . 'scan/token';
846 $response = $this->wt_remote_request( 'GET', $endpoint, false, $token );
847 return $response;
848 }
849 /**
850 * Return the current scanning status
851 *
852 * @param int $scan_id CookieYes scan ID.
853 * @return array
854 */
855 protected function get_scan_status( $scan_id ) {
856
857 $token = $this->get_access_token();
858
859 if ( empty( $token ) ) {
860 return false;
861 }
862 $endpoint = $this->get_base_path() . 'scan/' . $scan_id . '/status';
863 $response = $this->wt_remote_request( 'GET', $endpoint, false, $token );
864 return $response;
865 }
866 /**
867 * Return the final san results
868 *
869 * @param int $scan_id CookieYes scan ID.
870 * @return array
871 */
872 protected function get_scan_results( $scan_id ) {
873
874 $token = $this->get_access_token();
875
876 if ( empty( $token ) ) {
877 return false;
878 }
879 $endpoint = $this->get_base_path() . 'scan/' . $scan_id . '/result';
880
881 $response = $this->wt_remote_request( 'GET', $endpoint, false, $token );
882 return $response;
883 }
884 /**
885 * Add option to enable / disable cookeiyes branding on settings popup
886 *
887 * @since 1.9.6
888 * @access public
889 */
890 public function ckyes_settings() {
891
892 if ( $this->get_cookieyes_status() !== false ) : // wt_cli_temp_fix.
893
894 ?>
895 <table class="form-table">
896 <tr valign="top">
897 <th scope="row"></th>
898 <td>
899 <button class="wt-cli-ckyes-delete-btn button" data-action="show-prompt"><?php echo esc_html( __( 'Delete site data from CookieYes', 'cookie-law-info' ) ); ?></button>
900 </td>
901 </tr>
902 </table>
903 <div class='wt-cli-modal' id='wt-cli-ckyes-modal-delete-account'>
904 <span class="wt-cli-modal-js-close">×</span>
905 <div class="wt-cli-modal-header"><h4><?php echo esc_html__( 'Do you really want to delete your website from CookieYes', 'cookie-law-info' ); ?></h4></div>
906 <div class="wt-cli-modal-body">
907 <p><?php echo esc_html__( 'This action will clear all your website data from CookieYes. If you have multiple websites added to your CookieYes account, then only the data associated with this website get deleted. Otherwise, your entire account will be deleted.', 'cookie-law-info' ); ?></p>
908 <button class="wt-cli-action wt-cli-ckyes-delete-btn button button-primary" data-action="delete-account" ><?php echo esc_html__( 'Delete this website', 'cookie-law-info' ); ?></button>
909 </div>
910 </div>
911 <?php
912 endif;
913 }
914 /**
915 * CookieYes branding settings update
916 *
917 * @return void
918 */
919 public function ckyes_save_settings() {
920
921 if ( ! current_user_can( 'manage_options' ) ) {
922 wp_die( esc_html__( 'You do not have sufficient permission to perform this operation', 'cookie-law-info' ) ); // phpcs:ignore WordPress.Security.EscapeOutput
923 }
924 check_admin_referer( 'cookielawinfo-update-' . CLI_SETTINGS_FIELD );
925 if ( isset( $_POST['wt-cli-ckyes-branding'] ) && 'yes' === $_POST['wt-cli-ckyes-branding'] ) {
926 $this->set_ckyes_branding( true );
927 } else {
928 $this->set_ckyes_branding( false );
929 }
930 }
931 /**
932 * CookieYes branding status
933 *
934 * @return bool
935 */
936 public function get_ckyes_branding() {
937 $ckyes_branding = get_option( 'wt_cli_ckyes_branding', false );
938 if ( false !== $ckyes_branding ) {
939 return sanitize_text_field( $ckyes_branding );
940 }
941 return false;
942 }
943 /**
944 * Save CookieYes branding options
945 *
946 * @param string $value status value.
947 * @return void
948 */
949 public function set_ckyes_branding( $value ) {
950 if ( true === $value ) {
951 update_option( 'wt_cli_ckyes_branding', 'yes' );
952 } else {
953 update_option( 'wt_cli_ckyes_branding', 'no' );
954 }
955 }
956 /**
957 * Get CookieYes branding default options
958 *
959 * @return void
960 */
961 public function set_ckyes_branding_default() {
962 if ( $this->get_ckyes_branding() === false ) {
963 $this->set_ckyes_branding( true );
964 }
965 }
966 /**
967 * Show CookieYes branding logo on the settings page
968 *
969 * @return bool
970 */
971 public function show_ckyes_branding() {
972 if ( $this->get_ckyes_branding() === 'yes' && $this->get_cookieyes_status() === 1 ) {
973 return true;
974 }
975 return false;
976 }
977 /**
978 * Check if email verified or not
979 *
980 * @return array
981 */
982 public function check_email_verified() {
983
984 $response = $this->get_default_response();
985
986 if ( 2 === $this->get_cookieyes_status() ) {
987 $response['code'] = self::EC_WT_CKYES_PENDING_VERIFICATION;
988 $response['status'] = false;
989 $response['html'] = $this->get_email_verification_html( true );
990 } else {
991 $response = $this->ckyes_connect();
992 }
993 return $response;
994 }
995 /**
996 * Return email verification modal HTML
997 *
998 * @param boolean $pending Whether already email verification send or not.
999 * @param boolean $resend Disable resend.
1000 * @return string
1001 */
1002 public function get_email_verification_html( $pending = false, $resend = true ) {
1003
1004 $html = '';
1005 $resend_message = '';
1006 /* translators: %s: user email. */
1007 $message = sprintf( __( "We've sent an account verification link to the email address %s. Please click on the link given in email to verify your account with CookieYes.", 'cookie-law-info' ), esc_html( $this->get_user_email() ) );
1008
1009 if ( true === $resend ) {
1010 /* translators: %s: Resent link. */
1011 $resend_message = wp_kses(
1012 __( "If you didn't receive the email, click <a id='wt-cli-ckyes-email-resend-link' role='button'>here</a> to resend the verification email.", 'cookie-law-info' ),
1013 array(
1014 'a' => array(
1015 'href' => array(),
1016 'class' => array(),
1017 'id' => array(),
1018 'role' => array(),
1019 ),
1020 )
1021 );
1022 }
1023 $heading = __( 'Verification link sent', 'cookie-law-info' );
1024 if ( true === $pending ) {
1025 $heading = __( 'Pending email verification!', 'cookie-law-info' );
1026 }
1027 $html .= '<div class="wt-cli-ckyes-form-email-verify">';
1028 $html .= '<h4>' . $heading . '</h4>';
1029 $html .= '<div>' . $message . '</div>';
1030 $html .= '<div>' . $resend_message . '</div>';
1031 $html .= '</div>';
1032
1033 return $html;
1034 }
1035 /**
1036 * API request to abort the CookieYes scan.
1037 *
1038 * @param int $scan_id scan ID.
1039 * @return array
1040 */
1041 public function ckyes_abort_scan( $scan_id ) {
1042 $api_response = $this->get_default_response();
1043 $token = $this->get_access_token();
1044
1045 if ( empty( $token ) ) {
1046 return false;
1047 }
1048 $endpoint = $this->get_base_path() . 'scan/' . $scan_id . '/abort';
1049 $response = $this->wt_remote_request( 'POST', $endpoint, false, $token );
1050 if ( isset( $response['scan_result'] ) && 'cancelled' === $response['scan_result'] ) {
1051 $api_response['status'] = true;
1052 $api_response['code'] = self::WT_CKYES_ABORT_SUCCESSFUL;
1053 }
1054 return $api_response;
1055 }
1056 /**
1057 * Return login form HTML
1058 *
1059 * @return string
1060 */
1061 public function get_login_html() {
1062 $html = '';
1063 $html .= '<div class="wt-cli-modal-body">';
1064 $html .= '<div class="wt-cli-ckyes-login-icon">';
1065 $html .= '<span class="dashicons dashicons-admin-users"></span>';
1066 $html .= '</div>';
1067 /* translators: %s: user email address */
1068 $html .= '<h4>' . sprintf( __( 'Looks like you already have an account with CookieYes for email id %s, please login to continue.', 'cookie-law-info' ), esc_html( $this->get_user_email() ) ) . '</h4>';
1069 $html .= '<form id="wt-cli-ckyes-form-login">';
1070 $html .= '<div class="wt-cli-form-row">';
1071 $html .= '<input type="email" name="ckyes-email" class="wt-cli-form-input" placeholder="' . __( 'Email', 'cookie-law-info' ) . '" value="' . esc_attr( $this->get_user_email() ) . '"/>';
1072 $html .= '<input type="password" name="ckyes-password" class="wt-cli-form-input" placeholder="' . __( 'Password', 'cookie-law-info' ) . '" />';
1073 $html .= '</div>';
1074 $html .= '<p style="color: #757575">' . __( 'Please check if you have received an email with your password from CookieYes.', 'cookie-law-info' ) . '</p>';
1075 $html .= '<p style="color: #757575;">' . __( 'If you did not get the email, click “Reset password” to create a new password.', 'cookie-law-info' ) . '</p>';
1076 $html .= '<div class="wt-cli-action-container">';
1077 $html .= '<div class="wt-cli-action-group">';
1078 $html .= '<a href="#" id="wt-cli-ckyes-pwd-reset-link" class="wt-cli-action-link">' . __( 'Reset password', 'cookie-law-info' ) . '</a>';
1079 $html .= '</div>';
1080 $html .= '<div class="wt-cli-action-group">';
1081 $html .= '<button id="wt-cli-ckyes-login-btn" class="wt-cli-action button button-primary">' . __( 'Login', 'cookie-law-info' ) . '</button>';
1082 $html .= '</div>';
1083 $html .= '</div>';
1084 $html .= '</form>';
1085 $html .= '</div>';
1086 return $html;
1087
1088 }
1089 public function delete_account() {
1090
1091 $api_response = $this->get_default_response();
1092 if ( 1 === apply_filters( 'wt_cli_cookie_scan_status', 0 ) ) {
1093 $ckyes_scan_id = $this->get_ckyes_scan_id();
1094 if ( $ckyes_scan_id ) {
1095 $response = $this->ckyes_abort_scan( $ckyes_scan_id );
1096 $status = isset( $response['status'] ) ? $response['status'] : false;
1097 if ( false === $status ) {
1098 wp_send_json_error();
1099 }
1100 do_action( 'wt_cli_ckyes_abort_scan' );
1101 }
1102 }
1103 $this->delete_ckyes_account();
1104
1105 }
1106 public function delete_ckyes_account() {
1107 $api_response = $this->get_default_response();
1108 $token = $this->get_access_token();
1109
1110 if ( empty( $token ) ) {
1111 return $api_response;
1112 }
1113
1114 $endpoint = $this->get_base_path() . 'users/delete';
1115 $response = $this->wt_remote_request( 'POST', $endpoint, false, $token );
1116
1117 if ( isset( $response['status'] ) && 'deleted_successfully' === $response['status'] ) {
1118 $api_response['status'] = true;
1119 $this->reset_token();
1120 wp_send_json_success();
1121 }
1122 wp_send_json_error();
1123 }
1124 public function get_ckyes_scan_data() {
1125
1126 if ( ! $this->ckyes_scan_data ) {
1127 $scan_data = array(
1128 'scan_id' => 0,
1129 'scan_status' => '',
1130 'scan_token' => '',
1131 'scan_estimate' => '',
1132 );
1133 $ckyes_scan_data = get_option( 'wt_cli_ckyes_scan_options', false );
1134
1135 if ( $ckyes_scan_data !== false && is_array( $ckyes_scan_data ) ) {
1136
1137 $scan_data['scan_id'] = intval( isset( $ckyes_scan_data['scan_id'] ) ? $ckyes_scan_data['scan_id'] : 0 );
1138 $scan_data['scan_status'] = isset( $ckyes_scan_data['scan_status'] ) ? $ckyes_scan_data['scan_status'] : 0;
1139 $scan_data['scan_token'] = isset( $ckyes_scan_data['scan_token'] ) ? $ckyes_scan_data['scan_token'] : '';
1140 $scan_data['scan_estimate'] = isset( $ckyes_scan_data['scan_estimate'] ) ? $ckyes_scan_data['scan_estimate'] : 0;
1141 $scan_data['scan_instance'] = isset( $ckyes_scan_data['scan_instance'] ) ? $ckyes_scan_data['scan_instance'] : 0;
1142
1143 } else {
1144 return false;
1145 }
1146 $this->ckyes_scan_data = $scan_data;
1147 }
1148 return $this->ckyes_scan_data;
1149 }
1150 public function get_ckyes_scan_id() {
1151 $ckyes_scan_data = $this->get_ckyes_scan_data();
1152 return ( isset( $ckyes_scan_data['scan_id'] ) ? $ckyes_scan_data['scan_id'] : 0 );
1153 }
1154
1155 public function get_ckyes_scan_status() {
1156 $ckyes_scan_data = $this->get_ckyes_scan_data();
1157 return ( isset( $ckyes_scan_data['scan_status'] ) ? intval( $ckyes_scan_data['scan_status'] ) : 0 );
1158 }
1159
1160 public function get_ckyes_scan_token() {
1161 $ckyes_scan_data = $this->get_ckyes_scan_data();
1162 return ( isset( $ckyes_scan_data['scan_token'] ) ? $ckyes_scan_data['scan_token'] : '' );
1163 }
1164
1165 public function get_ckyes_scan_estimate() {
1166 $ckyes_scan_data = $this->get_ckyes_scan_data();
1167 return ( isset( $ckyes_scan_data['scan_estimate'] ) ? $ckyes_scan_data['scan_estimate'] : 0 );
1168 }
1169
1170 public function set_ckyes_scan_id( $value = 0 ) {
1171 $this->set_ckyes_scan_data( 'scan_id', $value );
1172 }
1173
1174 public function set_ckyes_scan_status( $value = 0 ) {
1175 $this->set_ckyes_scan_data( 'scan_status', $value );
1176 }
1177
1178 public function set_ckyes_scan_token( $value = '' ) {
1179 $this->set_ckyes_scan_data( 'scan_token', $value );
1180 }
1181
1182 public function set_ckyes_scan_estimate( $value = 0 ) {
1183 $this->set_ckyes_scan_data( 'scan_estimate', $value );
1184 }
1185
1186 public function set_ckyes_scan_data( $option_name, $value ) {
1187 $options = $this->get_ckyes_scan_data();
1188 if ($options !== false) {
1189 $options[ $option_name ] = $value;
1190 } else {
1191 $options = array($option_name => $value);
1192 }
1193 update_option( 'wt_cli_ckyes_scan_options', $options );
1194 $this->ckyes_scan_data = $options;
1195 }
1196 public function reset_scan_token() {
1197 delete_option( 'wt_cli_ckyes_scan_options' );
1198 }
1199
1200 public function set_ckyes_scan_instance() {
1201 $instance_id = 'wt-cli-scan-' . wp_create_nonce( $this->module_id );
1202 $instance_id = base64_encode( $instance_id );
1203 $this->set_ckyes_scan_data( 'scan_instance', $instance_id );
1204 return $instance_id;
1205 }
1206 public function get_ckyes_scan_instance() {
1207 $ckyes_scan_data = $this->get_ckyes_scan_data();
1208 return ( isset( $ckyes_scan_data['scan_instance'] ) ? $ckyes_scan_data['scan_instance'] : 0 );
1209 }
1210 }
1211 $settings_popup = new Cookie_Law_Info_Cookieyes();
1212 }
1213