Filter
uncanny-automator
automator_log_api_responses
Filters the API response data before it is logged in the Automator logs.
add_filter( 'automator_log_api_responses', $callback, 10, 1 );
Description
Filters the API response before it's logged. Developers can use this to modify, sanitize, or prevent logging of specific API responses, impacting what data is stored for debugging and auditing purposes within the Automator logs.
Usage
add_filter( 'automator_log_api_responses', 'your_function_name', 10, 1 );
Parameters
-
$response(mixed) - This parameter is a boolean value that determines whether the API response should be logged.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to filter the API response before it's logged.
* This function might be used to sanitize or modify the response data.
*
* @param mixed $value The default value passed to the filter (usually false).
* @param mixed $response The actual API response received.
* @return mixed The modified or original API response.
*/
function my_automator_filter_api_response( $value, $response ) {
// Check if the response is a WordPress WP_Error object and modify it if necessary.
if ( is_wp_error( $response ) ) {
// For example, we might want to log a simplified error message.
return array(
'error_code' => $response->get_error_code(),
'error_message' => $response->get_error_message(),
'data' => $response->get_error_data(),
);
}
// If the response is a JSON string, we might want to decode it to inspect it.
if ( is_string( $response ) ) {
$decoded_response = json_decode( $response, true );
if ( json_last_error() === JSON_ERROR_NONE ) {
// If it was valid JSON, return the decoded array.
return $decoded_response;
}
// If it wasn't valid JSON, return the original string.
return $response;
}
// For other types of responses, you might have different logic.
// For this example, we'll just return the response as is if it's not an error or a string.
return $response;
}
add_filter( 'automator_log_api_responses', 'my_automator_filter_api_response', 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:681
public function maybe_log_action( $params, $request, $response ) {
if ( ! isset( $params['action'] ) ) {
return;
}
$credits = $this->get_response_credits( $response );
$log = array(
'type' => 'action',
'recipe_log_id' => $params['action']['recipe_log_id'],
'item_log_id' => isset( $params['action']['action_log_id'] ) ? $params['action']['action_log_id'] : '',
'endpoint' => $params['endpoint'],
'params' => maybe_serialize( $params ),
'request' => maybe_serialize( $request ),
'response' => maybe_serialize( apply_filters( 'automator_log_api_responses', false, $response ) ),
'balance' => isset( $credits['balance'] ) ? $credits['balance'] : null,
'price' => isset( $credits['price'] ) ? $credits['price'] : null,
'status' => $this->get_response_code( $response ),
'time_spent' => isset( $params['time_spent'] ) ? $params['time_spent'] : 0,
);
return $this->add_log( $log );
}