From 7d2c821c5ad3c53cfa168194c1fcc30d99fb5495 Mon Sep 17 00:00:00 2001 From: Nirbhay Date: Tue, 24 Dec 2024 17:42:25 +0530 Subject: [PATCH] add: webhook endpoint --- classes/services/client.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/classes/services/client.php b/classes/services/client.php index 58e34a8..dc15464 100644 --- a/classes/services/client.php +++ b/classes/services/client.php @@ -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;