Filter uncanny-automator

automator_api_last_response

Filters the last API response before it's returned, allowing modification based on the request and parameters.

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

Description

Fires after `wp_remote_request` completes but before the API response is handled. Developers can modify the `$last_response` object, the `$request` arguments, or the `$params` array to alter or inspect the raw API response before it's processed further by the core.


Usage

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

Parameters

$last_response (mixed)
This parameter contains the last API response received by the automator, allowing for modifications or inspection before it's returned.
$request (mixed)
This parameter contains the response from the last API call made by the Automator.
$params (mixed)
This parameter contains the details of the API request being made, including the method and URL.

Return Value

The filtered value.


Examples

/**
 * Example: Log the details of the last API response if it indicates an error.
 * This filter hook allows us to intercept the response from wp_remote_request
 * before it's finalized and potentially modify it or perform actions based on it.
 */
add_filter( 'automator_api_last_response', function( $last_response, $request, $params ) {

	// Check if the response is a WP_Error object, indicating an issue.
	if ( is_wp_error( $last_response ) ) {
		// Log the error details for debugging purposes.
		error_log( sprintf(
			'Automator API Error: URL: %s, Request Args: %s, WP Error: %s',
			$params['url'],
			json_encode( $request ),
			$last_response->get_error_message()
		) );
	} else {
		// Optionally, you could inspect the HTTP status code here.
		$http_status = wp_remote_retrieve_response_code( $last_response );
		if ( $http_status >= 400 ) {
			error_log( sprintf(
				'Automator API Non-Success Response: URL: %s, Status: %d, Response: %s',
				$params['url'],
				$http_status,
				wp_remote_retrieve_body( $last_response )
			) );
		}
	}

	// Always return the original or modified $last_response to maintain the workflow.
	return $last_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/classes/class-api-server.php:459

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