one-click-accessibility/modules/connect/classes/service.php
VasylD beb8deda2e
[APP-907] Accessibility assistant (#326)
* PHPCS and remediation POC WIP

* WIP Wizard module

* Added scanner module

* [APP-929][APP-930] Add initial setup for wizard, added btn to the topbar (#230)

* [APP-929][APP-930]

* [APP-929][APP-930]

* update logic

* update init app

* [APP-0000] store plan scope after register/switch

* change return of load

* add header

* Move into shadow dom, add context, add headers components

* add UI components

* add UI components

* add UI components

* merge current develop

* merge current develop

* Add AltText Form

* Add scroll to current element

* Align with current design, add loader, added preview for svg

* Align with current design, add loader, added preview for svg

* Resolve comments

* Resolve comments

* Resolve comments

* [APP-934] add submit logic (#259)

* [APP-934] add submit logic

* [APP-934] add submit logic

* [APP-934] add submit logic

* [APP-934] add submit logic

* Added replace remediation action

* Add submit logic

* Add submit alt text logic, generate AI alt text

* Add AI generate request, add convert from SVG to png base64, added manual fix block

* Add AI generate request, add convert from SVG to png base64, added manual fix block

* Add texts, add remediation submit, fix logic to store remediation

* Add texts, add remediation submit, fix logic to store remediation

* Add texts, add remediation submit, fix logic to store remediation

* Add texts, add remediation submit, fix logic to store remediation

* Add texts, add remediation submit, fix logic to store remediation

* Add texts, add remediation submit, fix logic to store remediation

---------

Co-authored-by: Raz Ohad <admin@bainternet.info>

* Add texts, add remediation submit, fix logic to store remediation

* [APP-0000] add store request to the Service API (#268)

* [APP-0000] add store request to the Service API

* [APP-0000] add store request to the Service API

* [APP-0000] add store request to the Service API

* [APP-0000] add store request to the Service API

* [APP-0000] add request to store scanner summary

* [APP-0000] add request to store scanner summary

* [APP-0000] add request to store scanner summary

* [APP-1432] fix with AI flow (#275)

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1432] fix with AI flow

* [APP-1446] sidebar menu update (#276)

* update: simplify menu

* add: headings and scanner menu options

* [APP-945] add column for tables (#282)

* [APP-945] add column for tables

* [APP-945] add column for tables

* [APP-945] add column for tables

* [APP-945] add column for tables

* [APP-1447] Updated quota bar designs (#278)

* update: quota bar designs

* fix: the popover was partially visible

* update: AI credits name

* update: usage calculation logic

* add: quota indicators

* update: quotas tooltip text

* update: quotas notices text

* update: make toggle icon dynamic

* fix: @elementor/ui imports

* Update modules/settings/assets/js/layouts/quota-bar.js

Co-authored-by: VasylD <vasyld@elementor.red>

* update: styling of component

---------

Co-authored-by: VasylD <vasyld@elementor.red>

* [APP-1507] add UI changes (#286)

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1507] add UI changes

* [APP-1387] add mixpanel events (#288)

* [APP-1387] add mixpanel events

* [APP-1387] add mixpanel events

* New: Add the a11y assistant dashboard's UI [APP-1445] (#289)

* [APP-1513] add disconnected msg (#290)

* [APP-1513] add disconnected msg

* [APP-1513] add disconnected msg

* [APP-1513] add disconnected msg

* [APP-1547][APP-1548][APP-948] update alt text view, fix list issues (#291)

* [APP-1547][APP-1548][APP-948] update alt text view, fix list issues, add rest route for update status

* [APP-1547][APP-1548][APP-948] update alt text view, fix list issues, add rest route for update status

* fix: infotip icon padding and font style (#293)

* fix: infotip icon padding and font style

* fix: infotip icon padding and font style

* update: caniuse database

* [APP-1525] add edit mode (#294)

* [APP-1525] add edit mode

* [APP-1525] add edit mode

* [APP-1525] add edit mode

* Bug/app 1544 (#295)

* Fix: Resolve QA comments [APP-1544]

* New: Add beta tags [APP-1555]

* New: Add the "no results" state [APP-1544]

* [APP-1554] rename violations (#297)

* [APP-1512] add backend logic for remediation management (#298)

* [APP-1512] add backend logic for remediation management

* [APP-1512] add backend logic for remediation management

* Update modules/remediation/database/remediation-entry.php

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* Update modules/remediation/rest/items.php

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* [APP-1512] add backend logic for remediation management

---------

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* Fix: Resolve QA comments [n/a] (#299)

* [APP-1512] add FE remediation management (#300)

* [APP-1512] add backend logic for remediation management

* [APP-1512] add backend logic for remediation management

* Update modules/remediation/database/remediation-entry.php

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* Update modules/remediation/rest/items.php

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* [APP-1512] add backend logic for remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

* [APP-1512] add FE remediation management

---------

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* [APP-1609] fix tooltip

* [APP-1609] fix resolved btn click

* [APP-1401] clear cache on update (#302)

* [APP-1401] clear cache on update

* [APP-1401] clear cache on update

* [APP-1401] clear cache on update

* [APP-1401] clear cache on update

* [APP-1401] clear cache on update

* update with latest develop branch

* fix copies

* [APP-1619] add mixpanel event (#303)

* [APP-1619] add mixpanel event

* [APP-1619] add mixpanel event

* [APP-1593] Add locked variant for quota bar (#301)

* update: add locked version of quota status bar

* fix: text color for infotip

* fix: width issues

* fix: add check for 0

* [APP-1637][APP-1643] add UI fixes, add excluded rules array (#307)

* fix: make sidebar collapsible (#306)

* Merge current dev

* [APP-1603] Add dashboard menu (#309)

* [APP-1603] Add dashboard menu

* [APP-1603] Add dashboard menu

* [APP-1603] Add dashboard menu

* [APP-1603] Add dashboard menu

* Bug/app 1607 (#311)

* Fix: Update stats calculation logic [APP-1607]

* Fix: Set fixed width for the results table [APP-1650]

* Fix: Update filter rules [APP-1634]

* New: Add the no search results state [APP-1651]

* Fix: Convert indents [n/a]

* [APP-1670] add menu on Manage Fixes (#315)

* [APP-1670] add menu on Manage Fixes

* [APP-1670] update menu

* [APP-1611] fix comments

* fix: direction of snippet (#313)

* [APP-1561] New menu layout (#308)

* update: app menu and layout

* merge: latest changes from feature/remediation

* add: alert indicator to the closed sidebar

* fix: page layout for statement page

* update: menu display names

* fix: topbar menu layout

* update: sidebar menu width

* update: sidebar menu width

* fix: popup menu layout

* add: hover action to the toggle button

* update: my account menu

* fix: quota indicator for closed sidebar

* fix: icon alignments

* fix: scroll behaviour

* fix: page scroll behaviour

* fix: popup menu hover state

* update: quota bar and group layouts

* add: tooltips to the menu items

* update: make scans page fixed height and scrollable

* update: styles with theme references and added new styled components

* fix: make sidebar smoother

* update: accessibility page heading

* Merge latest develop

* [APP-1611] trigger save entry for clean cache (#314)

* [APP-1611] trigger save entry for clean cache

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-0000] call trigger save async (#319)

* [APP-1611] trigger save entry for clean cache

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-0000] call trigger save async

* fix: scrollbar issues (#318)

* [APP-1615] add remediation for wp image (#321)

* POC - FrontEnd remediations to handle Dynamic Data [APP-1644] (#312)

* POC - FrontEnd remediations to handle Dynamic Data

* Update modules/remediation/components/remediation-runner.php

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* added MutationObserver to cover elements that are added later on the page

runs until remediations are all done, and disconnect

* added $use_frontend flag instead of using "false"

* Update modules/remediation/components/remediation-runner.php

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* WIP on poc/fe-remediations

* [APP-0000] call trigger save async

* [APP-1644] apply FE remediation

* [APP-1644] apply FE remediation

* [APP-1644] apply FE remediation

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Small fixes

* Small fixes

* Small fixes

* Small fixes

---------

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>
Co-authored-by: vasyldinets <vasyld@elementor.red>

* [APP-1611] Fix cache issue (#324)

* [APP-1611] trigger save entry for clean cache

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-1611] fix comments

* [APP-0000] call trigger save async

* [APP-1611] clear cache

* [APP-1657] upgrade cta (#323)

* update: added logic to show button based on plan

* Update modules/settings/assets/js/components/quota-bar/quota-bar-group.js

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* fix: eslint error

---------

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* [APP-1658] Update accessibility admin column design (#322)

* update: wpadmin column layout

* update: move icon to assets

* Fix: Resolve remediation encoding issue [APP-1646] (#325)

---------

Co-authored-by: Ohad <ohad@elementor.com>
Co-authored-by: Raz Ohad <admin@bainternet.info>
Co-authored-by: Nirbhay Singh <121793120+nirbhayel@users.noreply.github.com>
Co-authored-by: Pavlo Kniazevych <139438463+pkniazevych@users.noreply.github.com>
Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>
2025-07-07 16:25:11 +07:00

304 lines
7.4 KiB
PHP

<?php
namespace EA11y\Modules\Connect\Classes;
use EA11y\Classes\Logger;
use EA11y\Modules\Connect\Classes\Exceptions\Service_Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class Service
*/
class Service {
const REFRESH_TOKEN_LOCK = '_connect_refresh_token';
const APP_TYPE = 'APP_ACCESS';
/**
* Registers new client and returns client ID
*
* @return string
* @throws Service_Exception
*/
public static function register_client(): string {
$clients_url = Utils::get_clients_url();
if ( ! $clients_url ) {
throw new Service_Exception( 'Missing client registration URL' );
}
$client_data = self::request( $clients_url, [
'method' => 'POST',
'headers' => [
'Content-Type' => 'application/json',
],
'body' => wp_json_encode([
'redirect_uri' => Utils::get_redirect_uri(),
'app_type' => Config::APP_TYPE,
]),
], 201 );
$client_id = $client_data['client_id'] ?? null;
$client_secret = $client_data['client_secret'] ?? null;
Data::set_client_id( $client_id );
Data::set_client_secret( $client_secret );
Data::set_home_url();
return $client_id;
}
/**
* Deactivate license
*
* @return void
* @throws Service_Exception
*/
public static function deactivate_license(): void {
$client_id = Data::get_client_id();
if ( ! $client_id ) {
throw new Service_Exception( 'Missing client ID' );
}
$deactivation_url = Utils::get_deactivation_url( $client_id );
if ( ! $deactivation_url ) {
throw new Service_Exception( 'Missing deactivation URL' );
}
// ensure a fresh token
self::refresh_token();
$access_token = Data::get_access_token();
if ( ! $access_token ) {
throw new Service_Exception( 'Missing access token' );
}
self::request($deactivation_url, [
'method' => 'DELETE',
'headers' => [
'Authorization' => "Bearer {$access_token}",
],
], 204);
Data::set_connect_mode_data( Data::ACCESS_TOKEN, null );
}
/**
* disconnect
*
* @return void
* @throws Service_Exception
*/
public static function disconnect(): void {
$sessions_url = Utils::get_sessions_url();
if ( ! $sessions_url ) {
throw new Service_Exception( 'Missing sessions URL' );
}
$access_token = Data::get_access_token();
if ( ! $access_token ) {
throw new Service_Exception( 'Missing access token' );
}
self::request( $sessions_url, [
'method' => 'DELETE',
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$access_token}",
],
], 204 );
Data::clear_session();
}
/**
* disconnect
*
* @return void
* @throws Service_Exception
*/
public static function reconnect(): void {
$sessions_url = Utils::get_sessions_url();
if ( ! $sessions_url ) {
throw new Service_Exception( 'Missing sessions URL' );
}
Data::clear_session();
}
/**
* Get token & optionally save to user
*
* @param string $grant_type
*
* @param string|null $credential
* @param bool|null $update
*
* @return array
* @throws Service_Exception
*/
public static function get_token( string $grant_type, ?string $credential = null, ?bool $update = true ) : array {
$token_url = Utils::get_token_url();
if ( ! $token_url ) {
throw new Service_Exception( 'Missing token URL' );
}
$client_id = Data::get_client_id();
$client_secret = Data::get_client_secret();
if ( empty( $client_id ) || empty( $client_secret ) ) {
throw new Service_Exception( 'Missing client ID or secret' );
}
$body = [
'grant_type' => $grant_type,
'redirect_uri' => Utils::get_redirect_uri(),
];
switch ( $grant_type ) {
case GrantTypes::AUTHORIZATION_CODE:
$body['code'] = $credential;
break;
case GrantTypes::REFRESH_TOKEN:
$body[ GrantTypes::REFRESH_TOKEN ] = $credential;
break;
case GrantTypes::CLIENT_CREDENTIALS:
$body['redirect_uri'] = Utils::get_redirect_uri( Data::get_home_url() );
break;
default:
throw new Service_Exception( 'Invalid grant type' );
}
$data = self::request( $token_url, [
'method' => 'POST',
'headers' => [
'x-elementor-apps' => Config::APP_NAME,
'Authorization' => 'Basic ' . base64_encode( "{$client_id}:{$client_secret}" ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
],
'body' => $body,
] );
if ( $update ) {
Data::set_connect_mode_data( Data::TOKEN_ID, $data['id_token'] ?? null );
Data::set_connect_mode_data( Data::ACCESS_TOKEN, $data['access_token'] ?? null );
Data::set_connect_mode_data( Data::REFRESH_TOKEN, $data['refresh_token'] ?? null );
Data::set_connect_mode_data( Data::OPTION_OWNER_USER_ID, get_current_user_id() ?? null );
}
return $data;
}
public static function jwt_decode( $payload ) : string {
static $jwks = null;
$jwks_url = Utils::get_jwks_url();
if ( ! $jwks_url ) {
return __( 'Missing JWKS URL', 'pojo-accessibility' );
}
if ( ! $jwks ) {
$jwks = self::request($jwks_url, [
'method' => 'GET',
]);
}
if ( ! class_exists( 'JWT' ) ) {
require_once EA11Y_PATH . 'vendor/autoload.php';
if ( ! class_exists( 'JWT' ) ) {
return __( 'JWT class not found', 'pojo-accessibility' );
}
}
try {
$decoded = \Firebase\JWT\JWT::decode( $payload, \Firebase\JWT\JWK::parseKeySet( $jwks ) );
return wp_json_encode( $decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
} catch ( \Throwable $th ) {
if ( $th instanceof \Firebase\JWT\ExpiredException ) {
self::get_token( GrantTypes::REFRESH_TOKEN, Data::get_refresh_token() );
return self::jwt_decode( $payload );
}
return $th->getMessage();
}
}
/**
* @param string $url
* @param array $args
* @param int $valid_response_code
*
* @return array|null
* @throws Service_Exception
*/
public static function request( string $url, array $args, int $valid_response_code = 200 ) : ?array {
$args['timeout'] = 30;
$args['headers'] = array_replace_recursive( [
'x-elementor-app-type' => self::APP_TYPE,
], $args['headers'] ?? [] );
$response = wp_remote_request( $url, $args );
if ( is_wp_error( $response ) ) {
Logger::error( $response->get_error_message() );
throw new Service_Exception( esc_html( $response->get_error_message() ) );
}
if ( wp_remote_retrieve_response_code( $response ) !== $valid_response_code ) {
Logger::error( 'Invalid status code ' . wp_remote_retrieve_response_code( $response ) );
throw new Service_Exception( esc_html( wp_remote_retrieve_body( $response ) ) );
}
return json_decode( wp_remote_retrieve_body( $response ), true );
}
/**
* @throws Service_Exception
*/
public static function refresh_token() {
self::get_token( GrantTypes::CLIENT_CREDENTIALS, null . true );
}
/**
* @throws Service_Exception
*/
public static function update_redirect_uri(): void {
$client_id = Data::get_client_id();
if ( ! $client_id ) {
throw new Service_Exception( 'Missing client ID' );
}
$client_patch_url = Utils::get_clients_patch_url( $client_id );
[ 'access_token' => $access_token ] = self::get_token(
GrantTypes::CLIENT_CREDENTIALS,
null,
false
);
self::request( $client_patch_url, [
'method' => 'PATCH',
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$access_token}",
],
'body' => wp_json_encode( [
'redirect_uri' => Utils::get_redirect_uri(),
] ),
] );
self::refresh_token();
Data::set_home_url();
}
}