Filter uncanny-automator-pro

automator_pro_webhook_rest_route_methods

Filters the allowed HTTP methods for Automator Pro webhook REST API routes.

add_filter( 'automator_pro_webhook_rest_route_methods', $callback, 10, 1 );

Description

Filters the allowed HTTP methods for webhook REST API routes. Developers can use this to restrict or expand accepted methods for specific webhook endpoints. This hook fires just before a REST route is registered, allowing dynamic method control.


Usage

add_filter( 'automator_pro_webhook_rest_route_methods', 'your_function_name', 10, 1 );

Parameters

$_route (mixed)
This parameter specifies the allowed HTTP methods for the webhook REST route.

Return Value

The filtered value.


Examples

/**
 * Example filter for automator_pro_webhook_rest_route_methods.
 *
 * This example demonstrates how to conditionally add or remove HTTP methods
 * for a specific webhook REST route. In this case, we'll allow DELETE
 * requests only for a route associated with deleting a specific item.
 *
 * @param array  $methods The current array of allowed HTTP methods.
 * @param string $_route The REST route being registered.
 * @return array The modified array of allowed HTTP methods.
 */
add_filter( 'automator_pro_webhook_rest_route_methods', function( $methods, $_route ) {

	// Define the specific route for which we want to add DELETE.
	$delete_route_identifier = '/uncanny-automator-pro/v1/webhooks/delete_item';

	// Check if the current route matches our target route.
	if ( strpos( $_route, $delete_route_identifier ) !== false ) {
		// Add the DELETE method if it's not already present.
		if ( ! in_array( 'DELETE', $methods ) ) {
			$methods[] = 'DELETE';
		}
	}

	// You could also conditionally remove methods if needed.
	// For example, to disallow GET requests for a sensitive route:
	// $sensitive_route_identifier = '/uncanny-automator-pro/v1/webhooks/sensitive_data';
	// if ( strpos( $_route, $sensitive_route_identifier ) !== false ) {
	// 	$methods = array_diff( $methods, array( 'GET' ) );
	// }

	// Return the potentially modified array of methods.
	return $methods;

}, 10, 2 ); // Priority 10, accepts 2 arguments.

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

uncanny-automator-pro/src/core/webhook/webhook-rest-handler.php:255

public static function register_rest_route( $_route, $hook = array() ) {
		register_rest_route(
			AUTOMATOR_REST_API_END_POINT,
			$_route,
			array(
				'methods'             => apply_filters(
					'automator_pro_webhook_rest_route_methods',
					array(
						'POST',
						'GET',
						'PUT',
					),
					$_route
				),
				'callback'            => array( __CLASS__, 'automator_webhook_rest_api_callback' ),
				'custom_headers'      => isset( $hook['custom_headers'] ) ? $hook['custom_headers'] : null,
				'permission_callback' => function ( $request ) {
					$attributes = $request->get_attributes();
					if ( empty( $attributes ) || ! isset( $attributes['custom_headers'] ) ) {
						return apply_filters( 'automator_pro_webhook_rest_route_permission_callback', true, $attributes, $request );
					}

					$request_headers = $request->get_headers();
					foreach ( $attributes['custom_headers'] as $header_name => $header_value ) {
						if ( ! isset( $request_headers[ $header_name ] ) ) {
							return apply_filters( 'automator_pro_webhook_rest_route_permission_callback', false, $attributes, $request );
						}
						if ( ! array_intersect( array( $header_value ), $request_headers[ $header_name ] ) ) {
							return apply_filters( 'automator_pro_webhook_rest_route_permission_callback', false, $attributes, $request );
						}
					}

					return apply_filters( 'automator_pro_webhook_rest_route_permission_callback', true, $attributes, $request );
				},
			)
		);
	}


Scroll to Top