Action
uncanny-automator
automator_webhook_action_completed
Fires after a webhook action has successfully completed, providing its results, user, and arguments.
add_action( 'automator_webhook_action_completed', $callback, 10, 5 );
Description
Fired after a webhook action is successfully completed within the Automator. Developers can use this hook to perform custom logic, logging, or trigger further actions based on the webhook's execution. It receives the webhook request body, the HTTP response, the user ID, original action arguments, and the webhook action object itself.
Usage
add_action( 'automator_webhook_action_completed', 'your_function_name', 10, 5 );
Parameters
-
$body(mixed) - The `$body` parameter contains the raw data received in the webhook request.
-
$response(mixed) - The `$body` parameter contains the raw body of the incoming webhook request.
-
$user_id(mixed) - This parameter contains the response object returned from the webhook request.
-
$do_action_args(mixed) - The ID of the user who triggered the webhook action.
-
$this(mixed) - This parameter contains the arguments passed to the `do_action` call, which are essential for understanding the context of the completed webhook action.
Examples
<?php
/**
* Example of how to hook into the automator_webhook_action_completed action.
* This function will be triggered after a webhook action has been completed within the Automator plugin.
* It logs the completion status and potentially updates user meta based on the response.
*/
add_action( 'automator_webhook_action_completed', 'my_automator_webhook_action_completed_handler', 10, 5 );
function my_automator_webhook_action_completed_handler( $body, $response, $user_id, $do_action_args, $automator_instance ) {
// Ensure we have a valid user ID.
if ( ! $user_id ) {
return;
}
// Example: Log the webhook response body and status to user meta for debugging or tracking.
// In a real-world scenario, you might want to parse the $response and $body to extract specific data.
$response_data = [
'timestamp' => current_time( 'mysql' ),
'webhook_body' => $body,
'webhook_response' => $response,
'do_action_args' => $do_action_args, // Might contain details about the action that was just completed.
];
// Retrieve existing logs or initialize an empty array.
$existing_logs = get_user_meta( $user_id, 'automator_webhook_logs', true );
if ( ! is_array( $existing_logs ) ) {
$existing_logs = [];
}
// Add the new log entry. Limit the number of logs to prevent database bloat.
$existing_logs[] = $response_data;
if ( count( $existing_logs ) > 50 ) { // Keep the last 50 logs.
$existing_logs = array_slice( $existing_logs, -50 );
}
update_user_meta( $user_id, 'automator_webhook_logs', $existing_logs );
// Example: If the webhook response indicates a specific success condition, perform another action.
// This is a hypothetical example. You'd need to know the structure of your $response.
if ( isset( $response['status'] ) && $response['status'] === 'success' && isset( $response['data']['user_level_update'] ) ) {
$new_level = $response['data']['user_level_update'];
update_user_meta( $user_id, 'user_level', $new_level );
// Potentially trigger another notification or action for the user.
error_log( "User {$user_id} level updated to {$new_level} via webhook." );
}
// The '$automator_instance' parameter is a reference to the object that triggered this hook.
// You could potentially use it to access other methods or properties of the Automator plugin's webhook handler.
// For example:
// error_log( 'Webhook action completed by instance: ' . get_class( $automator_instance ) );
}
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/lib/recipe-parts/trait-webhooks.php:223
src/core/lib/recipe-parts/trait-webhooks.php:231
// Marks the action as completed with notice.
$action_data['complete_with_notice'] = true;
// Complete the action.
Automator()->complete->action( $user_id, $action_data, $recipe_id, $validated['error_message'] );
// Log the webhook request.
$this->log_webhook_request( $args, $webhook_url, $response, $action_data );
// Invoke the action hook.
do_action( 'automator_webhook_action_completed', $body, $response, $user_id, $do_action_args, $this );
return;
}
Automator()->complete->action( $user_id, $action_data, $recipe_id );