one-click-accessibility/modules/connect/components/handler.php
Nirbhay Singh 9056e59ea8
[APP 705] add connect module, settings and notification component (#112)
* Initial refactor commit

*  Added build and tests CI/CD

* update: add src for admin settings

* update: incorrect constant names

* update: namespace

* add: accessibility settings

* update: webpack to output files inside a folder

* update: build output folders

* update: removed commented code

* update: npm scripts

* add: webpack config

* add: hooks

* update: move admin setting to the module folder

* update: assets loading logic

* update: add rule to move jsx props to multiline imporving readability

* add: connect modal

* update: hooks import for better readability

* update: replace functions with hooks

* add: connect module

* add: settings and get settings route

* add: hooks and contexts to get settings

* add: hooks

* add: notification component

* add: data api

* add: settings provider and connect settings

* add: husky

* fix: formatting and text-domain

* update: filter names

* fix: hook import

* add: set function for settings

* add: prop-types package

* update: refactor notification component and context

* update: remove filter for authorize url

* update: imports and exports of hooks

* update: plugin settings context filename and relevant imports

---------

Co-authored-by: Ohad <ohad@elementor.com>
2024-11-13 14:30:35 +02:00

91 lines
1.9 KiB
PHP

<?php
namespace EA11y\Modules\Connect\Components;
use EA11y\Modules\Connect\Classes\{
Config,
Data,
GrantTypes,
Service,
Utils,
};
use EA11y\Classes\Logger;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Handler
*/
class Handler {
private function should_handle_auth_code(): bool {
global $plugin_page;
$page_slug = explode( 'page=', Config::ADMIN_PAGE );
$is_connect_admin_page = false;
if ( ! empty( $page_slug[1] ) && $page_slug[1] === $plugin_page ) {
$is_connect_admin_page = true;
}
if ( ! $is_connect_admin_page && Config::ADMIN_PAGE === $plugin_page ) {
$is_connect_admin_page = true;
}
if ( ! $is_connect_admin_page ) {
return false;
}
$code = $_GET['code'] ?? null;
$state = $_GET['state'] ?? null;
if ( empty( $code ) || empty( $state ) ) {
return false;
}
return true;
}
private function validate_nonce( $state ) {
if ( ! wp_verify_nonce( $state, Config::STATE_NONCE ) ) {
wp_die( 'Invalid state' );
}
}
public function handle_auth_code() {
if ( ! $this->should_handle_auth_code() ) {
return;
}
$code = sanitize_text_field( $_GET['code'] );
$state = sanitize_text_field( $_GET['state'] );
// Check if the state is valid
$this->validate_nonce( $state );
try {
// Exchange the code for an access token and store it
Service::get_token( GrantTypes::AUTHORIZATION_CODE, $code ); // Makes sure we won't stick in the mismatch limbo
Data::set_home_url();
do_action( 'on_connect_' . Config::APP_PREFIX . '_connected' ); // Redirect to the redirect URI
} catch ( Throwable $t ) {
Logger::error( 'Unable to handle auth code: ' . $t->getMessage() );
}
wp_redirect( Utils::get_redirect_uri() );
exit;
}
/**
* Handler constructor.
*/
public function __construct() {
add_action( 'admin_init', [ $this, 'handle_auth_code' ] );
}
}