Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions classes/services/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,19 @@ public static function get_site_info(): array {
'user_agent' => ! empty( $_SERVER['HTTP_USER_AGENT'] )
? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) )
: 'Unknown',
'webhook_url' => self::webhook_endpoint(),
];
}

/**
* Log update endpoint
* @return string
*/
private static function webhook_endpoint(): string {
$blog_id = get_current_blog_id();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$blog_id = get_current_blog_id();
$blog_id = get_current_blog_id();

return get_rest_url( $blog_id, 'a11y/v1/webhooks/common' );
}

public function make_request( $method, $endpoint, $body = [], array $headers = [], $send_json = false ) {
$headers = array_replace_recursive( [
'x-elementor-a11y' => EA11Y_VERSION,
Expand Down Expand Up @@ -147,13 +157,20 @@ protected function request( $method, $endpoint, $args = [] ) {
$body = true;
}

// Return with no content on successfull deletion of domain from service.
// Return with no content on successful deletion of domain from service.
if ( 204 === $response_code ) {
$body = true;
return $body;
}

$body = json_decode( $body );
/**
* product/widget endpoint returns javascript hence we
* escape decoding for the data received in body from
* this endpoint.
*/
if ( ! strpos( $endpoint, 'widget' ) ) {
$body = json_decode( $body );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this? we will never call the widget from the service client directly


if ( false === $body ) {
return new WP_Error( 422, 'Wrong Server Response' );
Expand All @@ -167,7 +184,7 @@ protected function request( $method, $endpoint, $args = [] ) {
return $this->request( $method, $endpoint, $args );
}

if ( ! in_array( $response_code, [ 200, 201 ] ) ) {
if ( ! in_array( $response_code, [ 200, 201 ], true ) ) {
// In case $as_array = true.
$message = $body->message ?? wp_remote_retrieve_response_message( $response );
$message = is_array( $message ) ? join( ', ', $message ) : $message;
Expand Down
1 change: 1 addition & 0 deletions includes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static function get_module_list(): array {
'Legacy',
'Connect',
'Settings',
'Widget',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const MyAccountMenu = () => {
const redirectToBilling = () => {
window.open(BILLING_LINK, '_blank').focus();
};

return (
<>
<List>
Expand Down
2 changes: 2 additions & 0 deletions modules/settings/classes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Settings {

public const IS_VALID_PLAN_DATA = 'ea11y_is_valid_plan_data';
public const PLAN_DATA = 'ea11y_plan_data';
public const WIDGET_ICON_SETTINGS = 'ea11y_widget_icon_settings';
public const WIDGET_MENU_SETTINGS = 'ea11y_widget_menu_settings';

/**
* Returns plugin settings data by option name typecasted to an appropriate data type.
Expand Down
145 changes: 109 additions & 36 deletions modules/settings/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,42 +115,115 @@ public function on_connect(): void {

if ( $register_response && ! is_wp_error( $register_response ) ) {
Data::set_subscription_id( $register_response->id );
update_option( Settings::PLAN_DATA, $register_response );
update_option( Settings::IS_VALID_PLAN_DATA, true );
update_option( Settings::PLAN_DATA, $register_response );
update_option( Settings::IS_VALID_PLAN_DATA, true );
$this->set_default_settings();
} else {
Logger::error( esc_html( $register_response->get_error_message() ) );
update_option( Settings::IS_VALID_PLAN_DATA, false );
}
}

/**
* Retry registering the site if it fails during connect.
*
* Set default values after successful registration.
* @return void
*/
private function set_default_settings() : void {
$widget_menu_settings = [
'content-adjustments' => [
'text-size' => true,
'line-height' => true,
'align-text' => true,
'readable-font' => true,
],
'color-adjustments' => [
'greyscale' => true,
'contrast' => true,
],
'orientation-adjustments' => [
'page-structure' => true,
'site-map' => true,
'reading-panel' => true,
'hide-images' => true,
'pause-animations' => true,
'highlight-links' => true,
],
];

$widget_icon_settings = [
'desktop' => [
'hidden' => false,
'enableExactPosition' => false,
'exactPosition' => [
'horizontal' => [
'direction' => 'to-left',
'value' => 10,
'unit' => 'px',
],
'vertical' => [
'direction' => 'higher',
'value' => 10,
'unit' => 'px',
],
],
'position' => 'top-left',
],
'mobile' => [
'hidden' => false,
'enableExactPosition' => false,
'exactPosition' => [
'horizontal' => [
'direction' => 'to-right',
'value' => 10,
'unit' => 'px',
],
'vertical' => [
'direction' => 'lower',
'value' => 10,
'unit' => 'px',
],
],
'position' => 'top-left',
],
];

if ( ! get_option( Settings::WIDGET_MENU_SETTINGS ) ) {
update_option( Settings::WIDGET_MENU_SETTINGS, $widget_menu_settings );
}

if ( ! get_option( Settings::WIDGET_ICON_SETTINGS ) ) {
update_option( Settings::WIDGET_ICON_SETTINGS, $widget_icon_settings );
}
}

/**
* Retry registering the site if it fails during connect.
*
* @param $current_screen
* @return void
*/
public function check_plan_data( $current_screen ) : void {
//TODO: Update page name
if ( 'toplevel_page_accessibility-settings-2' !== $current_screen->base ) {
return;
}

if ( Connect::is_connected() && get_option( Settings::PLAN_DATA ) === false ) {
$register_response = Utils::get_api_client()->make_request(
'POST',
'site/register'
);

if ( $register_response && ! is_wp_error( $register_response ) ) {
Data::set_subscription_id( $register_response->id );
update_option( Settings::PLAN_DATA, $register_response );
update_option( Settings::IS_VALID_PLAN_DATA, true );
} else {
Logger::error( esc_html( $register_response->get_error_message() ) );
update_option( Settings::IS_VALID_PLAN_DATA, false );
}
}
}
public function check_plan_data( $current_screen ) : void {
//TODO: Update page name
if ( 'toplevel_page_accessibility-settings-2' !== $current_screen->base ) {
return;
}

if ( Connect::is_connected() && get_option( Settings::PLAN_DATA ) === false ) {
$register_response = Utils::get_api_client()->make_request(
'POST',
'site/register'
);

if ( $register_response && ! is_wp_error( $register_response ) ) {
Data::set_subscription_id( $register_response->id );
update_option( Settings::PLAN_DATA, $register_response );
update_option( Settings::IS_VALID_PLAN_DATA, true );
} else {
Logger::error( esc_html( $register_response->get_error_message() ) );
update_option( Settings::IS_VALID_PLAN_DATA, false );
}
}
}

/**
* Register settings.
Expand All @@ -167,34 +240,34 @@ public function register_settings(): void {
'show_in_rest' => [
'schema' => [
'type' => 'object',
'additionalProperties' => true
'additionalProperties' => true,
],
]
],
],
'widget_icon_settings' => [
'type' => 'object',
'show_in_rest' => [
'schema' => [
'type' => 'object',
'additionalProperties' => true
'additionalProperties' => true,
],
]
],
],
'plan_data' => [
'type' => 'object',
'show_in_rest' => [
'schema' => [
'type' => 'object',
'additionalProperties' => true
'additionalProperties' => true,
],
]
],
],
'close_post_connect_modal' => [
'type' => 'boolean',
],
'hide_minimum_active_options_alert' => [
'type' => 'boolean',
]
'hide_minimum_active_options_alert' => [
'type' => 'boolean',
],
];

foreach ( $settings as $setting => $args ) {
Expand All @@ -215,6 +288,6 @@ public function __construct() {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'rest_api_init', [ $this, 'register_settings' ] );
add_action( 'on_connect_' . Config::APP_PREFIX . '_connected', [ $this, 'on_connect' ] );
add_action( 'current_screen', [ $this, 'check_plan_data' ] );
add_action( 'current_screen', [ $this, 'check_plan_data' ] );
}
}
60 changes: 60 additions & 0 deletions modules/widget/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace EA11y\Modules\Widget;

use EA11y\Classes\Module_Base;
use EA11y\Classes\Utils;
use EA11y\Modules\Connect\Module as Connect;
use EA11y\Classes\Logger;
use EA11y\Modules\Settings\Classes\Settings;
use Exception;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

class Module extends Module_Base {

public function get_name(): string {
return 'widget';
}

/**
* Enqueue scripts
*
* @return void
* @throws Exception
*/
public function enqueue_accessibility_widget () : void {

$plan_data = Settings::get( Settings::PLAN_DATA );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this should probably be above it


wp_enqueue_script(
'ea11y-widget',
$this->get_widget_url() .'?api_key=' . $plan_data->public_api_key,
[],
EA11Y_VERSION,
true
);

wp_localize_script(
'ea11y-widget',
'ea11yWidget',
[
'iconSettings' => get_option( 'ea11y_widget_icon_settings' ),
'menuSettings' => get_option( 'ea11y_widget_menu_settings' ),
]
);
}

private function get_widget_url() : string {
return apply_filters( 'ea11y_widget_url', '' ); // TODO: add public url
}

/**
* Module constructor.
*/
public function __construct() {
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_accessibility_widget' ] );
}
Comment on lines +63 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be enqueue only if connected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bainternet Should there be a check for plan data as well? If the pan data is not available, then the widget should not load.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes also the pr is not fixed in both comments

}
Loading