PluginProbe
Elementor Website Builder – more than just a page builder / 3.30.0-dev3
Elementor Website Builder – more than just a page builder v3.30.0-dev3
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / common / modules / connect / apps / base-app.php

base-app.php in Elementor Website Builder – more than just a page builder 3.30.0-dev3, at core/common/modules/connect/apps/base-app.php

864 lines 20.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Common\Modules\Connect\Apps;
3
4 use Elementor\Core\Admin\Admin_Notices;
5 use Elementor\Core\Common\Modules\Connect\Admin;
6 use Elementor\Core\Utils\Collection;
7 use Elementor\Core\Utils\Http;
8 use Elementor\Core\Utils\Str;
9 use Elementor\Plugin;
10 use Elementor\Tracker;
11 use Elementor\Utils;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 abstract class Base_App {
18
19 const OPTION_NAME_PREFIX = 'elementor_connect_';
20
21 const OPTION_CONNECT_SITE_KEY = self::OPTION_NAME_PREFIX . 'site_key';
22
23 const SITE_URL = 'https://my.elementor.com/connect/v1';
24
25 const API_URL = 'https://my.elementor.com/api/connect/v1';
26
27 const HTTP_RETURN_TYPE_OBJECT = 'object';
28 const HTTP_RETURN_TYPE_ARRAY = 'array';
29
30 protected $data = [];
31
32 protected $auth_mode = '';
33
34 /**
35 * @var Http
36 */
37 protected $http;
38
39 /**
40 * @since 2.3.0
41 * @access protected
42 * @abstract
43 * TODO: make it public.
44 */
45 abstract protected function get_slug();
46
47 /**
48 * @since 2.8.0
49 * @access public
50 * TODO: make it abstract.
51 */
52 public function get_title() {
53 return $this->get_slug();
54 }
55
56 /**
57 * @since 2.3.0
58 * @access protected
59 * @abstract
60 */
61 abstract protected function update_settings();
62
63 /**
64 * @since 2.3.0
65 * @access public
66 * @static
67 */
68 public static function get_class_name() {
69 return get_called_class();
70 }
71
72 /**
73 * @access public
74 * @abstract
75 */
76 public function render_admin_widget() {
77 // PHPCS - the method get_title return a plain string.
78 echo '<h2>' . $this->get_title() . '</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
79
80 if ( $this->is_connected() ) {
81 $remote_user = $this->get( 'user' );
82 $title = sprintf(
83 /* translators: %s: Remote user. */
84 esc_html__( 'Connected as %s', 'elementor' ),
85 '<strong>' . esc_html( $remote_user->email ) . '</strong>'
86 );
87 $label = esc_html__( 'Disconnect', 'elementor' );
88 $url = $this->get_admin_url( 'disconnect' );
89 $attr = '';
90
91 printf(
92 '%s <a %s href="%s">%s</a>',
93 // PHPCS - the variable $title is already escaped above.
94 $title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
95 // PHPCS - the variable $attr is a plain string.
96 $attr, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
97 esc_attr( $url ),
98 esc_html( $label )
99 );
100 } else {
101 echo 'Not Connected';
102 }
103
104 echo '<hr>';
105
106 $this->print_app_info();
107
108 if ( current_user_can( 'manage_options' ) ) {
109 printf( '<div><a href="%s">%s</a></div>', esc_url( $this->get_admin_url( 'reset' ) ), esc_html__( 'Reset Data', 'elementor' ) );
110 }
111
112 echo '<hr>';
113 }
114
115
116 /**
117 * @since 2.3.0
118 * @access protected
119 */
120 protected function get_option_name() {
121 return static::OPTION_NAME_PREFIX . $this->get_slug();
122 }
123
124 /**
125 * @since 2.3.0
126 * @access public
127 */
128 public function admin_notice() {
129 $notices = $this->get( 'notices' );
130
131 if ( ! $notices ) {
132 return;
133 }
134
135 $this->print_notices( $notices );
136
137 $this->delete( 'notices' );
138 }
139
140
141 public function get_app_token_from_cli_token( $cli_token ) {
142 $response = $this->request( 'get_app_token_from_cli_token', [
143 'cli_token' => $cli_token,
144 ] );
145
146 if ( is_wp_error( $response ) ) {
147 // PHPCS - the variable $response does not contain a user input value.
148 wp_die( $response, $response->get_error_message() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
149 }
150
151 // Use state as usual.
152 $_REQUEST['state'] = $this->get( 'state' );
153 $_REQUEST['code'] = $response->code;
154 }
155 /**
156 * @since 2.3.0
157 * @access public
158 */
159 public function action_authorize() {
160 if ( $this->is_connected() ) {
161 $this->add_notice( esc_html__( 'Already connected.', 'elementor' ), 'info' );
162 $this->redirect_to_admin_page();
163 return;
164 }
165
166 $this->set_client_id();
167 $this->set_request_state();
168
169 $this->redirect_to_remote_authorize_url();
170 }
171
172 public function action_reset() {
173 if ( current_user_can( 'manage_options' ) ) {
174 delete_option( 'elementor_remote_info_library' );
175 }
176
177 $this->redirect_to_admin_page();
178 }
179
180 /**
181 * @since 2.3.0
182 * @access public
183 */
184 public function action_get_token() {
185 if ( $this->is_connected() ) {
186 $this->redirect_to_admin_page();
187 }
188
189 //phpcs:ignore WordPress.Security.NonceVerification.Recommended - The user as been authorized before in 'connect'.
190 $state = Utils::get_super_global_value( $_REQUEST, 'state' );
191
192 if ( $state !== $this->get( 'state' ) ) {
193 $this->add_notice( 'Get Token: Invalid Request.', 'error' );
194 $this->redirect_to_admin_page();
195 }
196
197 $response = $this->request( 'get_token', [
198 'grant_type' => 'authorization_code',
199 'code' => Utils::get_super_global_value( $_REQUEST, 'code' ), //phpcs:ignore WordPress.Security.NonceVerification.Recommended
200 'redirect_uri' => rawurlencode( $this->get_admin_url( 'get_token' ) ),
201 'client_id' => $this->get( 'client_id' ),
202 ] );
203
204 if ( is_wp_error( $response ) ) {
205 $notice = 'Cannot Get Token:' . $response->get_error_message();
206 $this->add_notice( $notice, 'error' );
207 $this->redirect_to_admin_page();
208 }
209
210 $this->delete( 'state' );
211 $this->set( (array) $response );
212
213 if ( ! empty( $response->data_share_opted_in ) && current_user_can( 'manage_options' ) ) {
214 Tracker::set_opt_in( true );
215 }
216
217 $this->after_connect();
218
219 // Add the notice *after* the method `after_connect`, so an app can redirect without the notice.
220 $this->add_notice( esc_html__( 'Connected successfully.', 'elementor' ) );
221
222 $this->redirect_to_admin_page();
223 }
224
225 /**
226 * @since 2.3.0
227 * @access public
228 */
229 public function action_disconnect() {
230 if ( $this->is_connected() ) {
231 $this->disconnect();
232 $this->add_notice( esc_html__( 'Disconnected successfully.', 'elementor' ) );
233 }
234
235 $this->redirect_to_admin_page();
236 }
237
238 /**
239 * @since 2.8.0
240 * @access public
241 */
242 public function action_reconnect() {
243 $this->disconnect();
244
245 $this->action_authorize();
246 }
247
248 /**
249 * @since 2.3.0
250 * @access public
251 */
252 public function get_admin_url( $action, $params = [] ) {
253 $params = [
254 'app' => $this->get_slug(),
255 'action' => $action,
256 'nonce' => wp_create_nonce( $this->get_slug() . $action ),
257 ] + $params;
258
259 $admin_url = Str::encode_idn_url( get_admin_url() );
260 $admin_url .= 'admin.php?page=' . Admin::PAGE_ID;
261
262 return add_query_arg( $params, $admin_url );
263 }
264
265 /**
266 * @since 2.3.0
267 * @access public
268 */
269 public function is_connected() {
270 return (bool) $this->get( 'access_token' );
271 }
272
273 /**
274 * @since 2.3.0
275 * @access protected
276 */
277 protected function init() {}
278
279 /**
280 * @since 2.3.0
281 * @access protected
282 */
283 protected function init_data() {}
284
285 /**
286 * @since 2.3.0
287 * @access protected
288 */
289 protected function after_connect() {}
290
291 /**
292 * @since 2.3.0
293 * @access public
294 */
295 public function get( $key, $default = null ) {
296 $this->init_data();
297
298 return isset( $this->data[ $key ] ) ? $this->data[ $key ] : $default;
299 }
300
301 /**
302 * @since 2.3.0
303 * @access protected
304 */
305 protected function set( $key, $value = null ) {
306 $this->init_data();
307
308 if ( is_array( $key ) ) {
309 $this->data = array_replace_recursive( $this->data, $key );
310 } else {
311 $this->data[ $key ] = $value;
312 }
313
314 $this->update_settings();
315 }
316
317 /**
318 * @since 2.3.0
319 * @access protected
320 */
321 protected function delete( $key = null ) {
322 $this->init_data();
323
324 if ( $key ) {
325 unset( $this->data[ $key ] );
326 } else {
327 $this->data = [];
328 }
329
330 $this->update_settings();
331 }
332
333 /**
334 * @since 2.3.0
335 * @access protected
336 */
337 protected function add( $key, $value, $default = '' ) {
338 $new_value = $this->get( $key, $default );
339
340 if ( is_array( $new_value ) ) {
341 $new_value[] = $value;
342 } elseif ( is_string( $new_value ) ) {
343 $new_value .= $value;
344 } elseif ( is_numeric( $new_value ) ) {
345 $new_value += $value;
346 }
347
348 $this->set( $key, $new_value );
349 }
350
351 /**
352 * @since 2.3.0
353 * @access protected
354 */
355 protected function add_notice( $content, $type = 'success' ) {
356 $this->add( 'notices', compact( 'content', 'type' ), [] );
357 }
358
359 /**
360 * @param $action
361 * @param array $request_body
362 * @param false $as_array
363 *
364 * @return mixed|\WP_Error
365 */
366 protected function request( $action, $request_body = [], $as_array = false ) {
367 $request_body = $this->get_connect_info() + $request_body;
368
369 return $this->http_request(
370 'POST',
371 $action,
372 [
373 'timeout' => 25,
374 'body' => $request_body,
375 'headers' => $this->is_connected() ?
376 [ 'X-Elementor-Signature' => $this->generate_signature( $request_body ) ] :
377 [],
378 ],
379 [
380 'return_type' => $as_array ? static::HTTP_RETURN_TYPE_ARRAY : static::HTTP_RETURN_TYPE_OBJECT,
381 ]
382 );
383 }
384
385 /**
386 * Get Base Connect Info
387 *
388 * Returns an array of connect info.
389 *
390 * @return array
391 */
392 protected function get_base_connect_info() {
393 return [
394 'app' => $this->get_slug(),
395 'access_token' => $this->get( 'access_token' ),
396 'client_id' => $this->get( 'client_id' ),
397 'local_id' => get_current_user_id(),
398 'site_key' => $this->get_site_key(),
399 'home_url' => trailingslashit( home_url() ),
400 ];
401 }
402
403 /**
404 * Get all the connect information
405 *
406 * @return array
407 */
408 protected function get_connect_info() {
409 $connect_info = $this->get_base_connect_info();
410
411 $additional_info = [];
412
413 /**
414 * Additional connect info.
415 *
416 * Filters the connection information when connecting to Elementor servers.
417 * This hook can be used to add more information or add more data.
418 *
419 * @param array $additional_info Additional connecting information array.
420 * @param Base_App $this The base app instance.
421 */
422 $additional_info = apply_filters( 'elementor/connect/additional-connect-info', $additional_info, $this );
423
424 return array_merge( $connect_info, $additional_info );
425 }
426
427 /**
428 * @param $endpoint
429 *
430 * @return array
431 */
432 protected function generate_authentication_headers( $endpoint ) {
433 $connect_info = ( new Collection( $this->get_connect_info() ) )
434 ->map_with_keys( function ( $value, $key ) {
435 // For bc `get_connect_info` returns the connect info with underscore,
436 // headers with underscore are not valid, so all the keys with underscore will be replaced to hyphen.
437 return [ str_replace( '_', '-', $key ) => $value ];
438 } )
439 ->replace_recursive( [ 'endpoint' => $endpoint ] )
440 ->sort_keys();
441
442 return $connect_info
443 ->merge( [ 'X-Elementor-Signature' => $this->generate_signature( $connect_info->all() ) ] )
444 ->all();
445 }
446
447 /**
448 * Send an http request
449 *
450 * @param $method
451 * @param $endpoint
452 * @param array $args
453 * @param array $options
454 *
455 * @return mixed|\WP_Error
456 */
457 protected function http_request( $method, $endpoint, $args = [], $options = [] ) {
458 $options = wp_parse_args( $options, [
459 'return_type' => static::HTTP_RETURN_TYPE_OBJECT,
460 ] );
461
462 $args = array_replace_recursive( [
463 'headers' => $this->is_connected() ? $this->generate_authentication_headers( $endpoint ) : [],
464 'method' => $method,
465 'timeout' => 10,
466 ], $args );
467
468 $response = $this->http->request_with_fallback(
469 $this->get_generated_urls( $endpoint ),
470 $args
471 );
472
473 if ( is_wp_error( $response ) && empty( $options['with_error_data'] ) ) {
474 // PHPCS - the variable $response does not contain a user input value.
475 wp_die( $response, [ 'back_link' => true ] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
476 }
477
478 $body = wp_remote_retrieve_body( $response );
479 $response_code = (int) wp_remote_retrieve_response_code( $response );
480
481 if ( ! $response_code ) {
482 return new \WP_Error( 500, 'No Response' );
483 }
484
485 // Server sent a success message without content.
486 if ( 'null' === $body ) {
487 $body = true;
488 }
489
490 $body = json_decode( $body, static::HTTP_RETURN_TYPE_ARRAY === $options['return_type'] );
491
492 if ( false === $body ) {
493 return new \WP_Error( 422, 'Wrong Server Response' );
494 }
495
496 if ( 201 === $response_code ) {
497 return $body;
498 }
499
500 if ( 200 !== $response_code ) {
501 // In case $as_array = true.
502 $body = (object) $body;
503
504 $message = isset( $body->message ) ? $body->message : wp_remote_retrieve_response_message( $response );
505 $code = (int) ( isset( $body->code ) ? $body->code : $response_code );
506
507 if ( ! $code ) {
508 $code = $response_code;
509 }
510
511 if ( 401 === $code ) {
512 $this->delete();
513
514 $should_retry = ! in_array( $this->auth_mode, [ 'xhr', 'cli' ], true );
515
516 if ( $should_retry ) {
517 $this->action_authorize();
518 }
519 }
520
521 if ( isset( $options['with_error_data'] ) && true === $options['with_error_data'] ) {
522 return new \WP_Error( $code, $message, $body );
523 }
524
525 return new \WP_Error( $code, $message );
526 }
527
528 return $body;
529 }
530
531 /**
532 * Create a signature for the http request
533 *
534 * @param array $payload
535 *
536 * @return false|string
537 */
538 private function generate_signature( $payload = [] ) {
539 return hash_hmac(
540 'sha256',
541 wp_json_encode( $payload, JSON_NUMERIC_CHECK ),
542 $this->get( 'access_token_secret' )
543 );
544 }
545
546 /**
547 * @since 2.3.0
548 * @access protected
549 */
550 protected function get_api_url() {
551 return static::API_URL . '/' . $this->get_slug();
552 }
553 /**
554 * @since 2.3.0
555 * @access protected
556 */
557 protected function get_remote_site_url() {
558 return static::SITE_URL . '/' . $this->get_slug();
559 }
560
561 /**
562 * @since 2.3.0
563 * @access protected
564 */
565 protected function get_remote_authorize_url() {
566 $redirect_uri = $this->get_auth_redirect_uri();
567
568 $allowed_query_params_to_propagate = [
569 'utm_source',
570 'utm_medium',
571 'utm_campaign',
572 'utm_term',
573 'utm_content',
574 'source',
575 'screen_hint',
576 ];
577
578 $query_params = ( new Collection( $_GET ) ) // phpcs:ignore
579 ->only( $allowed_query_params_to_propagate )
580 ->merge( [
581 'action' => 'authorize',
582 'response_type' => 'code',
583 'client_id' => $this->get( 'client_id' ),
584 'auth_secret' => $this->get( 'auth_secret' ),
585 'state' => $this->get( 'state' ),
586 'redirect_uri' => rawurlencode( $redirect_uri ),
587 'may_share_data' => current_user_can( 'manage_options' ) && ! Tracker::is_allow_track(),
588 'reconnect_nonce' => wp_create_nonce( $this->get_slug() . 'reconnect' ),
589 ] );
590
591 $utm_campaign = get_transient( 'elementor_core_campaign' );
592
593 if ( ! empty( $utm_campaign ) ) {
594 foreach ( [ 'source', 'medium', 'campaign' ] as $key ) {
595 if ( ! empty( $utm_campaign[ $key ] ) ) {
596 $query_params->offsetSet( 'utm_' . $key, $utm_campaign[ $key ] );
597 }
598 }
599 }
600
601 return add_query_arg( $query_params->all(), $this->get_remote_site_url() );
602 }
603
604 /**
605 * @since 2.3.0
606 * @access protected
607 */
608 protected function redirect_to_admin_page( $url = '' ) {
609 if ( ! $url ) {
610 $url = Admin::$url;
611 }
612
613 switch ( $this->auth_mode ) {
614 case 'popup':
615 $this->print_popup_close_script( $url );
616 break;
617
618 case 'cli':
619 case 'rest':
620 $this->admin_notice();
621 die;
622
623 default:
624 wp_safe_redirect( $url );
625 die;
626 }
627 }
628
629 /**
630 * @since 2.3.0
631 * @access protected
632 */
633 protected function set_client_id() {
634 $source = Utils::get_super_global_value( $_REQUEST, 'source' ) ?? ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not required here.
635 $response = $this->request(
636 'get_client_id',
637 [
638 'source' => esc_attr( $source ),
639 ]
640 );
641
642 if ( is_wp_error( $response ) ) {
643 // PHPCS - the variable $response does not contain a user input value.
644 wp_die( $response, $response->get_error_message() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
645 }
646
647 $this->set( 'client_id', $response->client_id );
648 $this->set( 'auth_secret', $response->auth_secret );
649 }
650
651 /**
652 * @since 2.3.0
653 * @access protected
654 */
655 protected function set_request_state() {
656 $this->set( 'state', wp_generate_password( 12, false ) );
657 }
658
659 protected function get_popup_success_event_data() {
660 return [];
661 }
662
663 /**
664 * @since 2.3.0
665 * @access protected
666 */
667 protected function print_popup_close_script( $url ) {
668 $data = $this->get_popup_success_event_data();
669
670 ?>
671 <script>
672 if ( opener && opener !== window ) {
673 opener.jQuery( 'body' ).trigger(
674 'elementor/connect/success/<?php echo esc_attr( Utils::get_super_global_value( $_REQUEST, 'callback_id' ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not required here. ?>',
675 <?php echo wp_json_encode( $data ); ?>
676 );
677
678 opener.dispatchEvent( new CustomEvent( 'elementor/connect/success' ),
679 <?php echo wp_json_encode( $data ); ?>
680 );
681
682 window.close();
683 opener.focus();
684 } else {
685 location = '<?php echo esc_url( $url ); ?>';
686 }
687 </script>
688 <?php
689 die;
690 }
691
692 /**
693 * @since 2.3.0
694 * @access protected
695 */
696 protected function disconnect() {
697 if ( $this->is_connected() ) {
698 // Try update the server, but not needed to handle errors.
699 $this->request( 'disconnect' );
700 }
701
702 $this->delete();
703 }
704
705 /**
706 * @since 2.3.0
707 * @access protected
708 */
709 public function get_site_key() {
710 $site_key = get_option( static::OPTION_CONNECT_SITE_KEY );
711
712 if ( ! $site_key ) {
713 $site_key = md5( uniqid( wp_generate_password() ) );
714 update_option( static::OPTION_CONNECT_SITE_KEY, $site_key );
715 }
716
717 return $site_key;
718 }
719
720 protected function redirect_to_remote_authorize_url() {
721 switch ( $this->auth_mode ) {
722 case 'cli':
723 case 'rest':
724 $this->get_app_token_from_cli_token( Utils::get_super_global_value( $_REQUEST, 'token' ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not required here.
725 return;
726 default:
727 wp_redirect( $this->get_remote_authorize_url() ); //phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- Safe redirect is used here.
728 die;
729 }
730 }
731
732 protected function get_auth_redirect_uri() {
733 $redirect_uri = $this->get_admin_url( 'get_token' );
734
735 switch ( $this->auth_mode ) {
736 case 'popup':
737 $redirect_uri = add_query_arg( [
738 'mode' => 'popup',
739 'callback_id' => esc_attr( Utils::get_super_global_value( $_REQUEST, 'callback_id' ) ), //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not required here.
740 ], $redirect_uri );
741 break;
742 }
743
744 return $redirect_uri;
745 }
746
747
748 protected function print_notices( $notices ) {
749 switch ( $this->auth_mode ) {
750 case 'cli':
751 foreach ( $notices as $notice ) {
752 printf( '[%s] %s', wp_kses_post( $notice['type'] ), wp_kses_post( $notice['content'] ) );
753 }
754 break;
755
756 case 'rest':
757 // After `wp_send_json` the script will die.
758 $this->delete( 'notices' );
759 wp_send_json( $notices );
760 break;
761
762 default:
763 /**
764 * @var Admin_Notices $admin_notices
765 */
766 $admin_notices = Plugin::$instance->admin->get_component( 'admin-notices' );
767
768 foreach ( $notices as $notice ) {
769 $options = [
770 'description' => wp_kses_post( wpautop( $notice['content'] ) ),
771 'type' => $notice['type'],
772 'icon' => false,
773 ];
774
775 $admin_notices->print_admin_notice( $options );
776 }
777 }
778 }
779
780 protected function get_app_info() {
781 return [];
782 }
783
784 protected function print_app_info() {
785 $app_info = $this->get_app_info();
786
787 foreach ( $app_info as $key => $item ) {
788 if ( $item['value'] ) {
789 $status = 'Exist';
790 $color = 'green';
791 } else {
792 $status = 'Empty';
793 $color = 'red';
794 }
795
796 // PHPCS - the values of $item['label'], $color, $status are plain strings.
797 printf( '%s: <strong style="color:%s">%s</strong><br>', $item['label'], $color, $status ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
798 }
799 }
800
801 private function get_generated_urls( $endpoint ) {
802 $base_urls = $this->get_api_url();
803
804 if ( ! is_array( $base_urls ) ) {
805 $base_urls = [ $base_urls ];
806 }
807
808 return array_map( function ( $base_url ) use ( $endpoint ) {
809 return trailingslashit( $base_url ) . $endpoint;
810 }, $base_urls );
811 }
812
813 private function init_auth_mode() {
814 $is_rest = defined( 'REST_REQUEST' ) && REST_REQUEST;
815 $is_ajax = wp_doing_ajax();
816
817 if ( $is_rest || $is_ajax ) {
818 // Set default to 'xhr' if rest or ajax request.
819 $this->set_auth_mode( 'xhr' );
820 }
821
822 $mode = Utils::get_super_global_value( $_REQUEST, 'mode' );
823
824 if ( $mode ) {
825 $allowed_auth_modes = [
826 'popup',
827 ];
828
829 if ( defined( 'WP_CLI' ) && WP_CLI ) {
830 $allowed_auth_modes[] = 'cli';
831 }
832
833 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
834 $allowed_auth_modes[] = 'rest';
835 }
836
837 if ( in_array( $mode, $allowed_auth_modes, true ) ) {
838 $this->set_auth_mode( $mode );
839 }
840 }
841 }
842
843 public function set_auth_mode( $mode ) {
844 $this->auth_mode = $mode;
845 }
846
847 /**
848 * @since 2.3.0
849 * @access public
850 */
851 public function __construct() {
852 add_action( 'admin_notices', [ $this, 'admin_notice' ] );
853
854 $this->init_auth_mode();
855
856 $this->http = new Http();
857
858 /**
859 * Allow extended apps to customize the __construct without call parent::__construct.
860 */
861 $this->init();
862 }
863 }
864