Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APP-721] Render widget and global settings #124

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 12 additions & 4 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();
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,14 +157,12 @@ 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 );

if ( false === $body ) {
return new WP_Error( 422, 'Wrong Server Response' );
}
Expand All @@ -167,7 +175,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',
'Core',
];
}
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 @@ -11,6 +11,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
Expand Down
73 changes: 73 additions & 0 deletions modules/settings/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,85 @@ public function on_connect(): void {
Data::set_subscription_id( $register_response->id );
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 );
}
}

/**
* 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.
*
Expand Down
66 changes: 66 additions & 0 deletions modules/widget/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace EA11y\Modules\Widget;

use EA11y\Classes\Module_Base;
use EA11y\Modules\Connect\Module as Connect;
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 {

if ( ! Connect::is_connected() ) {
return;
}

$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


if ( ! isset( $plan_data->public_api_key ) ) {
return;
}

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
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