Action Since 7.0 uncanny-automator

automator_recipe_item_add_complete

Fires after any recipe item is added. Fires after any Automator recipe item is successfully added, providing its details and the associated recipe.

add_action( 'automator_recipe_item_add_complete', $callback, 10, 6 );

Description

Fires after a recipe item is successfully added via the API. Developers can use this hook to perform custom actions based on the newly added item, its type, and the associated recipe. Access the item ID, code, type, service return data, and the full recipe object to integrate custom logic.


Usage

add_action( 'automator_recipe_item_add_complete', 'your_function_name', 10, 6 );

Parameters

$item_id (int|string)
The item ID.
$item_code (string)
The item code.
$recipe_id (int)
The recipe ID.
$item_type (string)
The item type (trigger, action, closure, filter_condition).
$item_data (array)
Service return data.
$recipe (object)
Recipe aggregate object.

Examples

/**
 * Example function hooked to 'automator_recipe_item_add_complete'.
 * This function demonstrates how to log information when a new recipe item is successfully added.
 *
 * @param int|string $item_id   The ID of the added recipe item.
 * @param string     $item_code The unique code identifying the type of recipe item (e.g., 'wordpress_post_published').
 * @param int        $recipe_id The ID of the recipe this item belongs to.
 * @param string     $item_type The type of the recipe item (e.g., 'trigger', 'action').
 * @param array      $item_data Additional data returned from the service that added the item.
 * @param object     $recipe    The full recipe object.
 */
function my_automator_log_recipe_item_addition( $item_id, $item_code, $recipe_id, $item_type, $item_data, $recipe ) {

	// Avoid logging for specific item types if not needed.
	if ( 'filter_condition' === $item_type ) {
		return;
	}

	// Construct a log message.
	$log_message = sprintf(
		'Recipe item added: ID %s, Code "%s", Type "%s", Recipe ID %d.',
		$item_id,
		$item_code,
		$item_type,
		$recipe_id
	);

	// Optionally, log the item data if it's not too verbose.
	if ( ! empty( $item_data ) ) {
		$log_message .= ' Item Data: ' . print_r( $item_data, true );
	}

	// Log the message using WordPress's logging capabilities or a custom logger.
	// For demonstration, we'll use error_log. In a real plugin, you might use a dedicated logging class.
	error_log( $log_message );

	// Example: Update a custom meta field on the recipe if the item is a trigger.
	if ( 'trigger' === $item_type ) {
		// Assuming $recipe object has a method or property to access its meta.
		// This is a hypothetical example, actual implementation depends on the $recipe object structure.
		if ( isset( $recipe->ID ) && $recipe->ID > 0 ) {
			update_post_meta( $recipe->ID, '_automator_last_trigger_added_timestamp', time() );
		}
	}
}

// Hook the function to the action.
// The '3' indicates that this callback accepts 3 arguments (the remaining are handled internally by the hook).
// The '10' is the default priority.
add_action( 'automator_recipe_item_add_complete', 'my_automator_log_recipe_item_add_addition', 10, 6 );

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/api/transports/restful/recipe/items/traits/hooks/trait-hook-add-operations.php:73

protected function dispatch_add_complete_hooks( array $item_data, object $recipe ): void {
		$type = $this->get_item_type();

		/**
		 * Fires after any recipe item is added.
		 *
		 * @since 7.0
		 *
		 * @param int|string $item_id   The item ID.
		 * @param string     $item_code The item code.
		 * @param int        $recipe_id The recipe ID.
		 * @param string     $item_type The item type (trigger, action, closure, filter_condition).
		 * @param array      $item_data Service return data.
		 * @param object     $recipe    Recipe aggregate object.
		 */
		do_action(
			'automator_recipe_item_add_complete',
			$this->get_item_id(),
			$this->get_item_code(),
			$this->get_recipe_id(),
			$type,
			$item_data,
			$recipe
		);

		/**
		 * Fires after a specific item type is added.
		 *
		 * @since 7.0
		 *
		 * @param int|string $item_id   The item ID.
		 * @param string     $item_code The item code.
		 * @param int        $recipe_id The recipe ID.
		 * @param array      $item_data Service return data.
		 * @param object     $recipe    Recipe aggregate object.
		 */
		do_action(
			"automator_recipe_{$type}_add_complete",
			$this->get_item_id(),
			$this->get_item_code(),
			$this->get_recipe_id(),
			$item_data,
			$recipe
		);
	}


Scroll to Top