PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.4
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.4, at vendor/elementor/wp-one-package/src/Connect/Components/Handler.php

128 lines 3.2 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 // Check if the state is valid
92 $this->validate_nonce( $state );
93
94 try {
95 // Exchange the code for an access token and store it
96 [ 'access_token' => $access_token ] = $this->facade->service()->get_token( GrantTypes::AUTHORIZATION_CODE, $code );
97 $this->facade->data()->set_share_usage_data( $this->is_share_usage_scope_granted( $access_token ) ? 'yes' : 'no' );
98 $this->facade->data()->set_owner_user_id( get_current_user_id() );
99 $this->facade->data()->set_home_url();
100 } catch ( \Throwable $th ) {
101 $this->facade->logger()->error( 'Unable to handle auth code: ' . $th->getMessage() );
102 }
103
104 // Trigger the connected event for all apps
105 do_action( 'elementor_one/connected', $this->facade );
106
107 // Trigger the connected event for the app prefix
108 do_action( 'elementor_one/' . $this->facade->get_config( 'app_prefix' ) . '_connected', $this->facade );
109
110 wp_safe_redirect( $this->facade->utils()->get_admin_url() );
111
112 exit;
113 }
114
115 /**
116 * Check if share usage scope is granted
117 * @param string $access_token
118 * @return bool
119 */
120 private function is_share_usage_scope_granted( string $access_token ): bool {
121 $jwt_payload = Utils::decode_jwt( $access_token );
122 if ( $jwt_payload ) {
123 return in_array( self::SCOPE_SHARE_USAGE_DATA, $jwt_payload['scp'] ?? [], true );
124 }
125 return false;
126 }
127 }
128