Filter uncanny-automator

automator_call

Filters the request and parameters before an automator action is executed, allowing modification of data.

add_filter( 'automator_call', $callback, 10, 2 );

Description

Fires before making an external API request. Developers can modify the request arguments or parameters before they are sent. This allows for dynamic manipulation of the outgoing request, such as adding headers, modifying the body, or altering the URL. It's a crucial point for integrating with external services and customizing API interactions.


Usage

add_filter( 'automator_call', 'your_function_name', 10, 2 );

Parameters

$request (mixed)
This parameter contains the current request array, which is built up with various parameters before being passed to the API.
$params (mixed)
This parameter holds the details of the outgoing API request that is about to be made.

Return Value

The filtered value.


Examples

/**
 * Modify the request arguments before making an API call.
 *
 * This filter allows you to alter the arguments passed to wp_remote_request()
 * when the Automator plugin makes an outgoing API call. You can add custom headers,
 * modify request methods, or set other WP_Http arguments.
 *
 * @param array $request The current request arguments array.
 * @param array $params The original parameters passed to the API call, including 'url'.
 *
 * @return array The modified request arguments.
 */
add_filter( 'automator_call', function( $request, $params ) {

	// Example: Add a custom header for authentication.
	// In a real-world scenario, you'd likely fetch this token dynamically or from settings.
	$api_key = 'your_secret_api_key_here';
	$request['headers']['X-Custom-Auth'] = $api_key;

	// Example: Ensure the request method is POST if it's not already set.
	if ( ! isset( $request['method'] ) || empty( $request['method'] ) ) {
		$request['method'] = 'POST';
	}

	// Example: Add a timeout to the request.
	$request['timeout'] = 30; // Set timeout to 30 seconds.

	// It's crucial to return the modified request array.
	return $request;

}, 10, 2 );

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/classes/class-api-server.php:450

public static function call( $params ) {

		$api = self::get_instance();

		if ( empty( $params['method'] ) ) {
			throw new Exception( 'Request method is required', 500 );
		}

		if ( empty( $params['url'] ) ) {
			throw new Exception( 'URL is required', 500 );
		}

		$request = array();

		$request = $api->maybe_add_optional_params( $request, $params );

		$request = apply_filters( 'automator_call', $request, $params );

		$time_before = microtime( true );

		self::$last_response = wp_remote_request(
			$params['url'],
			$request
		);

		self::$last_response = apply_filters( 'automator_api_last_response', self::$last_response, $request, $params );

		do_action( 'automator_api_response', self::$last_response, $request, $params );

		$time_spent = round( ( microtime( true ) - $time_before ) * 1000 );

		$params['time_spent'] = $time_spent;

		$api_log_id = $api->maybe_log_action( $params, $request, self::$last_response );

		if ( is_wp_error( self::$last_response ) ) {
			throw new Exception( esc_html( 'WordPress was not able to make a request: ' . self::$last_response->get_error_message() ), 500 );
		}

		self::$last_response['api_log_id'] = $api_log_id;

		return self::$last_response;
	}


Scroll to Top