PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.7
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.7
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / elementor / wp-one-package / src / Connect / Components / Handler.php

Handler.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.7, at vendor/elementor/wp-one-package/src/Connect/Components/Handler.php

131 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ElementorOne\Connect\Components;
4
5 use ElementorOne\Connect\Classes\GrantTypes;
6 use ElementorOne\Connect\Classes\Utils;
7 use ElementorOne\Connect\Facade;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Class Handler
15 */
16 class Handler {
17
18 const SCOPE_SHARE_USAGE_DATA = 'share_usage_data';
19
20 /**
21 * Facade instance
22 * @var Facade
23 */
24 private Facade $facade;
25
26 /**
27 * Handler constructor
28 * @param Facade $facade
29 */
30 public function __construct( Facade $facade ) {
31 $this->facade = $facade;
32
33 add_action( 'admin_init', [ $this, 'handle_auth_code' ] );
34 }
35
36 /**
37 * Should handle auth code
38 * @return bool
39 */
40 private function should_handle_auth_code(): bool {
41 global $plugin_page;
42
43 $admin_page = $this->facade->get_config( 'admin_page' );
44 $page_slug = explode( 'page=', $admin_page );
45
46 $is_connect_admin_page = false;
47
48 if ( ! empty( $page_slug[1] ) && $page_slug[1] === $plugin_page ) {
49 $is_connect_admin_page = true;
50 }
51
52 if ( ! $is_connect_admin_page && $admin_page === $plugin_page ) {
53 $is_connect_admin_page = true;
54 }
55
56 if ( ! $is_connect_admin_page ) {
57 return false;
58 }
59
60 if ( ! isset( $_GET['code'] ) || ! isset( $_GET['state'] ) ) {
61 return false;
62 }
63
64 return true;
65 }
66
67 /**
68 * Validate nonce
69 * @param string $state
70 * @return void
71 */
72 private function validate_nonce( $state ) {
73 if ( ! wp_verify_nonce( $state, $this->facade->get_config( 'state_nonce' ) ) ) {
74 // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
75 wp_die( 'Invalid state<p><a href="' . esc_url( $this->facade->utils()->get_admin_url() ) . '">' . esc_html__( '&laquo; Back' ) . '</a></p>' );
76 }
77 }
78
79 /**
80 * Handle auth code
81 * @return void
82 */
83 public function handle_auth_code() {
84 if ( ! $this->should_handle_auth_code() ) {
85 return;
86 }
87
88 $code = sanitize_text_field( wp_unslash( $_GET['code'] ?? '' ) );
89 $state = sanitize_text_field( wp_unslash( $_GET['state'] ?? '' ) );
90
91 $this->validate_nonce( $state );
92
93 try {
94 [ 'access_token' => $access_token ] = $this->facade->service()->get_token( GrantTypes::AUTHORIZATION_CODE, $code );
95 $this->facade->data()->set_share_usage_data( $this->is_share_usage_scope_granted( $access_token ) ? 'yes' : 'no' );
96 $this->facade->data()->set_owner_user_id( get_current_user_id() );
97 $this->facade->data()->set_home_url();
98 } catch ( \Throwable $th ) {
99 $this->facade->logger()->error( 'Unable to handle auth code: ' . $th->getMessage() );
100
101 do_action( 'elementor_one/connect_fail', $this->facade );
102 do_action(
103 'elementor_one/' . $this->facade->get_config( 'app_prefix' ) . '_connect_fail',
104 $this->facade
105 );
106
107 wp_safe_redirect( $this->facade->utils()->get_admin_url() );
108 exit;
109 }
110
111 do_action( 'elementor_one/connected', $this->facade );
112 do_action( 'elementor_one/' . $this->facade->get_config( 'app_prefix' ) . '_connected', $this->facade );
113
114 wp_safe_redirect( $this->facade->utils()->get_admin_url() );
115 exit;
116 }
117
118 /**
119 * Check if share usage scope is granted
120 * @param string $access_token
121 * @return bool
122 */
123 private function is_share_usage_scope_granted( string $access_token ): bool {
124 $jwt_payload = Utils::decode_jwt( $access_token );
125 if ( $jwt_payload ) {
126 return in_array( self::SCOPE_SHARE_USAGE_DATA, $jwt_payload['scp'] ?? [], true );
127 }
128 return false;
129 }
130 }
131