Skip to content

Commit

Permalink
add: webhook endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nirbhayel committed Dec 24, 2024
1 parent ab453fe commit 7d2c821
Showing 1 changed file with 20 additions and 3 deletions.
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();
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 );
}

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

0 comments on commit 7d2c821

Please sign in to comment.