Filter uncanny-automator

automator_handle_webhook_request_by_endpoint_{$endpoint}

Filters webhook requests before they are handled, allowing modification of the request data based on the specific endpoint.

add_filter( 'automator_handle_webhook_request_by_endpoint_{$endpoint}', $callback, 10, 1 );

Description

Filter webhook requests based on the specific endpoint. Developers can hook into this to process custom webhook data, validate inputs, and return a `WP_REST_Response` object. This hook is crucial for integrating external services that send data to your WordPress site via webhooks.


Usage

add_filter( 'automator_handle_webhook_request_by_endpoint_{$endpoint}', 'your_function_name', 10, 1 );

Parameters

$request (mixed)
This parameter contains the incoming `WP_REST_Request` object from the webhook.

Return Value

The filtered value.


Examples

<?php
/**
 * Example callback for the 'automator_handle_webhook_request_by_endpoint_my_custom_endpoint' filter.
 *
 * This function demonstrates how to process a webhook request for a custom endpoint.
 * It expects the incoming request to contain a 'data' parameter with a specific format.
 *
 * @param WP_REST_Request $request The incoming WordPress REST API request object.
 * @return WP_REST_Response|WP_Error A successful response or an error object.
 */
add_filter( 'automator_handle_webhook_request_by_endpoint_my_custom_endpoint', function( WP_REST_Request $request ) {

    // Get the custom data payload from the request.
    $webhook_data = $request->get_param( 'data' );

    // Basic validation: Check if 'data' is provided and is an array.
    if ( ! is_array( $webhook_data ) ) {
        return new WP_Error(
            'automator_invalid_data',
            __( 'Invalid or missing data payload.', 'text-domain' ),
            array( 'status' => 400 )
        );
    }

    // Example: Process the data. Let's assume we expect a 'user_id' and 'action'.
    $user_id = isset( $webhook_data['user_id'] ) ? intval( $webhook_data['user_id'] ) : 0;
    $action  = isset( $webhook_data['action'] ) ? sanitize_text_field( $webhook_data['action'] ) : '';

    // Further validation: Ensure required fields are present.
    if ( $user_id === 0 || empty( $action ) ) {
        return new WP_Error(
            'automator_missing_required_fields',
            __( 'Required fields (user_id, action) are missing from the data payload.', 'text-domain' ),
            array( 'status' => 400 )
        );
    }

    // Simulate an action based on the webhook data.
    // In a real scenario, you might create a post, update a user meta, trigger an automation, etc.
    $message = sprintf(
        __( 'Received webhook for user ID %d with action "%s". Processing now...', 'text-domain' ),
        $user_id,
        $action
    );

    // For demonstration, we'll just return a success message.
    // In a real plugin, you would perform actual operations here.
    $response_data = array(
        'success' => true,
        'message' => $message,
        'received_data' => $webhook_data,
    );

    // Return a WP_REST_Response object for a successful request.
    return new WP_REST_Response( $response_data, 200 );

}, 10, 1 ); // Priority 10, accepts 1 argument.

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/services/app-integrations/action-manager.php:260

public function handle_webhook_request( WP_REST_Request $request ) {
		$endpoint = sanitize_key( $request->get_param( 'endpoint' ) );

		try {
			// Handle webhook request via endpoint filter.
			$response = apply_filters(
				"automator_handle_webhook_request_by_endpoint_{$endpoint}",
				$request
			);

			// Validate if valid integration was found and filter ran.
			if ( is_a( $response, 'WP_REST_Request' ) ) {
				throw new Exception( 'Webhook handler not found.' );
			}

			// Validate response is a valid WP_REST_Response.
			if ( ! is_a( $response, 'WP_REST_Response' ) ) {
				throw new Exception( 'Webhook handler response not valid.' );
			}

			return $response;
		} catch ( Exception $e ) {
			// Return error response as WP_REST_Response
			return new WP_REST_Response(
				array(
					'message' => $e->getMessage(),
				),
				200
			);
		}
	}

Scroll to Top