Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
98 changes: 98 additions & 0 deletions modules/widget/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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 EA11y\Classes\Services\Client;
use Exception;

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

class Module extends Module_Base {

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

/**
* @return void
* @throws Exception
*/
public function get_widget() : void {
if ( ! Connect::is_connected() ) {
return;
}

$plan_data = Settings::get( Settings::PLAN_DATA );

if ( ! is_wp_error( $plan_data ) && ! empty( $plan_data ) && ! empty( $plan_data->public_api_key ) ) {

$widget_code = Utils::get_api_client()->make_request(
'GET',
'product/widget?api_key=' . $plan_data->public_api_key
);

if ( ! is_wp_error( $widget_code ) && ! empty( $widget_code ) ) {
wp_add_inline_script( 'ea11y-widget', $widget_code );
} else {
$this->show_widget_load_error();
Logger::error( esc_html( $widget_code->get_error_message() ) );
}

} else {
$this->show_widget_load_error();
Logger::error( esc_html( $plan_data->get_error_message() ) );
}
}

/**
* Show a console error when widget fails to load
* @return void
*/
private function show_widget_load_error () : void {
wp_add_inline_script('ea11y-widget', "console.error('Failed to load accessibility widget');");
}

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

wp_enqueue_script(
'ea11y-widget',
EA11Y_ASSETS_URL . '/js/widget.js',
[],
EA11Y_VERSION,
true
);

// fetch widget from service
$this->get_widget();

wp_localize_script(
'ea11y-widget',
'ea11yWidgetData',
[
'iconSettings' => wp_json_encode( get_option( 'ea11y_widget_icon_settings' ) ),
'menuSettings' => wp_json_encode( get_option( 'ea11y_widget_menu_settings' ) ),
'wpRestNonce' => wp_create_nonce( 'wp_rest' )
]
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's stick to the agreements we've had.
The widget expects an object named ea11yWidget with the field preview: true inside. It also doesn't need wpRestNonce, not sure if you keep it for some other piece of logic, but please move it elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated @pkniazevych

}

/**
* Module constructor.
*/
public function __construct() {
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_global_assets' ] );
}
}
Loading