Action uncanny-automator

automator_closures_completed

Fires after all closures for a recipe have been successfully completed for a user.

add_action( 'automator_closures_completed', $callback, 10, 3 );

Description

Fires after all closures within a recipe have been successfully completed. Developers can use this hook to perform custom actions, log recipe completion details, or trigger further processes based on the completed recipe and user. The `$args` parameter may only contain information from the last executed trigger.


Usage

add_action( 'automator_closures_completed', 'your_function_name', 10, 3 );

Parameters

$recipe_id (mixed)
The ID of the recipe that has just completed its closures.
$user_id (mixed)
The ID of the recipe that has just completed its closures.
$args (mixed)
This parameter contains the ID of the user for whom the recipe completed.

Examples

/**
 * Example function to hook into the 'automator_closures_completed' action.
 * This function logs the completion of a recipe for a specific user,
 * and potentially updates some user meta based on the arguments passed.
 *
 * @param int   $recipe_id The ID of the completed recipe.
 * @param int   $user_id   The ID of the user for whom the recipe completed.
 * @param array $args      Additional arguments passed with the action, potentially containing trigger details.
 */
function my_automator_recipe_completed_handler( $recipe_id, $user_id, $args ) {
	// Log the recipe completion for debugging or auditing purposes.
	error_log( sprintf( 'Automator Recipe #%d completed for User ID: %d.', $recipe_id, $user_id ) );

	// Example: Update user meta to track completed recipes.
	// This is a hypothetical scenario. In a real plugin, you might store
	// more specific information about the completed recipe or its triggers.
	if ( $user_id && $recipe_id ) {
		$completed_recipes = get_user_meta( $user_id, 'automator_completed_recipes', true );

		// Ensure $completed_recipes is an array.
		if ( ! is_array( $completed_recipes ) ) {
			$completed_recipes = array();
		}

		// Add the current recipe ID if it's not already present.
		if ( ! in_array( $recipe_id, $completed_recipes, true ) ) {
			$completed_recipes[] = $recipe_id;
			update_user_meta( $user_id, 'automator_completed_recipes', $completed_recipes );
		}
	}

	// Example of using the $args. If the last trigger info is available and relevant,
	// you might parse it for specific data to further process.
	if ( isset( $args['trigger_data'] ) && is_array( $args['trigger_data'] ) ) {
		// Hypothetically, if $args['trigger_data'] contained a 'points_awarded' key.
		if ( isset( $args['trigger_data']['points_awarded'] ) ) {
			$current_points = get_user_meta( $user_id, 'user_points', true );
			if ( ! $current_points ) {
				$current_points = 0;
			}
			update_user_meta( $user_id, 'user_points', (int) $current_points + (int) $args['trigger_data']['points_awarded'] );
			error_log( sprintf( 'User ID %d awarded %d points from recipe %d.', $user_id, $args['trigger_data']['points_awarded'], $recipe_id ) );
		}
	}
}
add_action( 'automator_closures_completed', 'my_automator_recipe_completed_handler', 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/lib/process/class-automator-recipe-process-complete.php:1221

public function closures( $recipe_id = null, $user_id = null, $recipe_log_id = null, $args = array(), $recipe_closure_data = null ) {

		if ( null === $recipe_closure_data ) {
			$recipe_closure_data = Automator()->get_recipe_data( AUTOMATOR_POST_TYPE_CLOSURE, $recipe_id, array(), true );
		}

		$log_args = array();

		foreach ( $recipe_closure_data as $closure_data ) {

			$closure_code                  = $closure_data['meta']['code'];
			$closure_status                = $closure_data['post_status'];
			$closure_data['recipe_log_id'] = $recipe_log_id;
			$closure_integration           = Automator()->get->closure_integration_from_closure_code( $closure_code );

			// The log arguments.
			$log_args = array(
				'user_id'                 => $user_id,
				'automator_closure_id'    => $closure_data['ID'],
				'automator_recipe_id'     => $recipe_id,
				'automator_recipe_log_id' => $recipe_log_id,
				'completed'               => Automator_Status::COMPLETED,
			);

			// The log meta args.
			$log_meta_args = array(
				'user_id'              => $user_id,
				'automator_closure_id' => $closure_data['ID'],
			);

			if ( 1 === Automator()->plugin_status->get( $closure_integration ) && 'publish' === $closure_status ) {

				// Log the entry before doing a redirect.
				$log_id = Automator()->db->closure->add_entry( $log_args );

				// The plugin for this action is active .. execute
				$closure_execution_function = Automator()->get->closure_execution_function_from_closure_code( $closure_code );

				// Log a meta.
				if ( false !== $log_id ) {
					$args['closure_log_id']                    = $log_id;
					$log_meta_args['automator_closure_log_id'] = $log_id;
					Automator()->db->closure->add_entry_meta( $log_meta_args, 'closure_data', wp_json_encode( $closure_data ) );
				}

				call_user_func_array(
					$closure_execution_function,
					array(
						$user_id,
						$closure_data,
						$recipe_id,
						$args,
					)
				);

			} else {

				// The plugin for this action is NOT active
				Automator()->wp_error->add_error( 'complete_closures', 'ERROR: You are trying to complete ' . $closure_code . ' and the plugin ' . $closure_integration . ' is not active.', $this );

				// Do not log error in closures for now.
			}
		}

		do_action_deprecated(
			'uap_closures_completed',
			array(
				$recipe_id,
				$user_id,
				$args,
			),
			'3.0',
			'automator_closures_completed'
		);

		do_action( 'automator_closures_completed', $recipe_id, $user_id, $args );

		return true;
	}

Scroll to Top