PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.4.1
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.4.1
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 / modules / connect / components / handler.php

handler.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.4.1, at modules/connect/components/handler.php

82 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Modules\Connect\Components;
4
5 use ImageOptimization\Modules\Connect\Classes\{
6 Config,
7 GrantTypes,
8 Service,
9 Utils,
10 };
11 use ImageOptimization\Classes\Services\Client;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 /**
18 * Class Handler
19 */
20 class Handler {
21 private function should_handle_auth_code(): bool {
22 global $plugin_page;
23
24 $page_slug = explode( 'page=', Config::ADMIN_PAGE );
25
26 $is_connect_admin_page = false;
27
28 if ( ! empty( $page_slug[1] ) && $page_slug[1] === $plugin_page ) {
29 $is_connect_admin_page = true;
30 }
31
32 if ( ! $is_connect_admin_page && Config::ADMIN_PAGE === $plugin_page ) {
33 $is_connect_admin_page = true;
34 }
35
36 if ( ! $is_connect_admin_page ) {
37 return false;
38 }
39
40 $code = $_GET['code'] ?? null;
41 $state = $_GET['state'] ?? null;
42
43 if ( empty( $code ) || empty( $state ) ) {
44 return false;
45 }
46
47 return true;
48 }
49
50 private function validate_nonce( $state ) {
51 if ( ! wp_verify_nonce( $state, Config::STATE_NONCE ) ) {
52 wp_die( 'Invalid state' );
53 }
54 }
55
56 public function handle_auth_code() {
57 if ( ! $this->should_handle_auth_code() ) {
58 return;
59 }
60
61 $code = sanitize_text_field( $_GET['code'] );
62 $state = sanitize_text_field( $_GET['state'] );
63
64 // Check if the state is valid
65 $this->validate_nonce( $state );
66
67 // Exchange the code for an access token and store it
68 Service::get_token( GrantTypes::AUTHORIZATION_CODE, $code );
69
70 // Redirect to the redirect URI
71 wp_redirect( Utils::get_redirect_uri() );
72 exit;
73 }
74
75 /**
76 * Handler constructor.
77 */
78 public function __construct() {
79 add_action( 'admin_init', [ $this, 'handle_auth_code' ] );
80 }
81 }
82