Filter uncanny-automator

automator_send_webhook_default_response

Filters the default response before sending a webhook, allowing modification of the webhook URL and arguments.

add_filter( 'automator_send_webhook_default_response', $callback, 10, 3 );

Description

Fires after the webhook response is generated but before it's returned for default POST requests. Developers can modify the webhook response, the webhook URL, or the request arguments. Use with caution to avoid disrupting webhook functionality.


Usage

add_filter( 'automator_send_webhook_default_response', 'your_function_name', 10, 3 );

Parameters

$webhook_url (mixed)
This parameter holds the URL to which the webhook will be sent.
$webhook_url (mixed)
This parameter represents the URL to which the webhook will be sent.
$args (mixed)
This parameter contains the arguments that will be sent with the webhook request.

Return Value

The filtered value.


Examples

/**
 * Modify the default webhook response for POST requests.
 *
 * This example demonstrates how to intercept the response from a POST webhook,
 * check if it's a successful response (HTTP status code 2xx), and log a message
 * if the webhook failed to send data.
 *
 * @param WP_Error|array $response       The response from wp_safe_remote_post().
 * @param string         $webhook_url    The URL of the webhook.
 * @param array          $args           The arguments used for the webhook request.
 * @return WP_Error|array The modified or original response.
 */
add_filter( 'automator_send_webhook_default_response', function( $response, $webhook_url, $args ) {
	// Check if the response indicates an error.
	if ( is_wp_error( $response ) ) {
		// Log the error for debugging purposes.
		error_log( 'Automator Webhook POST Error: ' . $response->get_error_message() . ' for URL: ' . $webhook_url );
	} else {
		// Get the HTTP status code from the response.
		$http_code = wp_remote_retrieve_response_code( $response );

		// Check if the HTTP code is not a success code (2xx).
		if ( $http_code < 200 || $http_code >= 300 ) {
			// Log a warning if the webhook request was not successful.
			error_log( 'Automator Webhook POST Failed (HTTP ' . $http_code . '): ' . $webhook_url . ' with args: ' . print_r( $args, true ) );
		}
	}

	// Always return the response, whether modified or original.
	return $response;
}, 10, 3 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/core/lib/webhooks/class-automator-send-webhook.php:903

public static function call_webhook( $webhook_url, $args, $request_type = 'POST' ) {

		if ( ! self::validate_webhook_url( $webhook_url ) ) {
			return new WP_Error(
				'invalid_webhook_url',
				sprintf(
					'The webhook URL "%s" is not valid. Please ensure it is a publicly accessible URL and does not point to a private or local network address.',
					esc_url( $webhook_url )
				)
			);
		}

		$request_type = sanitize_text_field( wp_unslash( $request_type ) );

		switch ( $request_type ) {
			case 'POST':
				$response = wp_safe_remote_post( $webhook_url, $args );
				break;
			case 'GET':
				$url      = add_query_arg( $args['body'], $webhook_url );
				$response = wp_safe_remote_get( $url, $args );
				break;
			case 'HEAD':
				$response = wp_safe_remote_head( $webhook_url, $args );
				break;
			case 'PUT':
			case 'PATCH':
			case 'DELETE':
			case 'OPTIONS':
				$args['method'] = $request_type;
				$response       = wp_safe_remote_request( $webhook_url, $args );
				break;
			default:
				$response = apply_filters(
					'automator_send_webhook_default_response',
					wp_safe_remote_post( $webhook_url, $args ),
					$webhook_url,
					$args
				);
				break;
		}

		return $response;
	}

Scroll to Top