-
Notifications
You must be signed in to change notification settings - Fork 41
[APP-721] Render widget and global settings #124
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
Changes from 2 commits
7d2c821
3b29e7b
95b4ee1
d5d84e5
41172de
770ee52
ef59dc9
b7afde2
ac56f0f
e4030dd
1969be2
008de11
b87ad0d
445a9cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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 ); | ||
| } | ||
|
||
|
|
||
| if ( false === $body ) { | ||
| return new WP_Error( 422, 'Wrong Server Response' ); | ||
|
|
@@ -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; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ public static function get_module_list(): array { | |
| 'Legacy', | ||
| 'Connect', | ||
| 'Settings', | ||
| 'Widget', | ||
| ]; | ||
| } | ||
|
|
||
|
|
||
| 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' ) | ||
| ] | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's stick to the agreements we've had.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ] ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.