Filter
uncanny-automator
automator_recipe_main_object_loop_action_items
Filters the action items displayed within the main object loop of a recipe.
add_filter( 'automator_recipe_main_object_loop_action_items', $callback, 10, 4 );
Description
Filters the action items within a recipe's main object loop. Developers can modify, add, or remove action items before they are processed. This hook fires after action items are hydrated from their source but before they are assigned to the recipe object. It's crucial for custom action item manipulation.
Usage
add_filter( 'automator_recipe_main_object_loop_action_items', 'your_function_name', 10, 4 );
Parameters
-
$action_items(mixed) - This parameter holds an array of action items for the current recipe loop.
-
$recipe(mixed) - This parameter contains an array of action items that will be filtered or modified.
-
$this(mixed) - This parameter contains the recipe object currently being processed.
-
$actions(mixed) - This parameter represents the current instance of the `Loop_Action_Item_Structure` class.
Return Value
The filtered value.
Examples
/**
* Example function to modify action items within the recipe loop.
* This function might be used to add a custom action, remove a specific action,
* or alter properties of existing actions based on certain criteria.
*
* @param array $action_items An array of action objects for the current recipe loop.
* @param object $recipe The current recipe object.
* @param object $loop_context The context object for the current loop (likely the class instance).
* @param array $all_actions An array of all available action types.
*
* @return array The modified array of action items.
*/
function my_automator_modify_action_items( $action_items, $recipe, $loop_context, $all_actions ) {
// Example: If the recipe has a specific trigger, let's add a custom "Log Message" action.
// Replace 'my_custom_trigger_slug' with an actual trigger slug if you know it.
// Replace 'my_custom_log_action_type' with an actual action type if you know it.
$trigger_slug = ''; // You'd typically get this from the $recipe object.
if ( $recipe && isset( $recipe->triggers ) && ! empty( $recipe->triggers ) ) {
// Assuming the first trigger's slug is relevant for this example.
$trigger_slug = $recipe->triggers[0]->slug;
}
if ( 'my_custom_trigger_slug' === $trigger_slug ) {
// Create a new dummy action item. In a real scenario, you'd likely use
// a specific action class or method to create and populate it.
$new_action_data = array(
'ID' => 0, // New action, so no ID yet.
'post_title' => 'Log Custom Message',
'action_type' => 'my_custom_log_action_type', // The unique identifier for your custom action type.
'settings' => array(
'message' => 'This message is logged because the custom trigger was fired for recipe: ' . $recipe->title,
),
);
// You'd need to have a way to instantiate or get an action object.
// This is a placeholder and assumes a `Automator_Action_Factory` or similar exists.
// For this example, we'll simulate adding it directly if we don't have a factory.
// In a real plugin, you'd likely use a dedicated method to create the action object.
// $new_action_object = Automator_Action_Factory::create_action( 'my_custom_log_action_type', $new_action_data, $recipe );
// $action_items[] = $new_action_object;
// For demonstration purposes, let's assume we can create a simplified object.
// In reality, you'd hydrate it from the data.
$simplified_action_object = new stdClass();
$simplified_action_object->ID = 0;
$simplified_action_object->post_title = $new_action_data['post_title'];
$simplified_action_object->action_type = $new_action_data['action_type'];
$simplified_action_object->settings = $new_action_data['settings'];
$action_items[] = $simplified_action_object;
}
// Example: Remove a specific action if it exists and meets certain criteria.
foreach ( $action_items as $key => $action_item ) {
// Replace 'unwanted_action_slug' with an actual action slug to demonstrate removal.
if ( isset( $action_item->action_type ) && 'unwanted_action_slug' === $action_item->action_type ) {
// You might add further checks here, e.g., based on the recipe itself.
// if ( $recipe->ID === 123 ) {
unset( $action_items[ $key ] );
// }
}
}
// Re-index the array after unsetting to avoid issues if your logic depends on sequential keys.
return array_values( $action_items );
}
// Hook into the filter.
// The '10' is the priority, and '4' indicates that our callback function accepts 4 arguments.
add_filter( 'automator_recipe_main_object_loop_action_items', 'my_automator_modify_action_items', 10, 4 );
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/services/recipe/structure/actions/item/loop.php:174
private function hydrate_items() {
$actions = Automator()->get_recipe_data( AUTOMATOR_POST_TYPE_ACTION, self::$loop_id );
$config = self::$recipe->get_config();
$action_items = array();
foreach ( $actions as $action_item ) {
if ( isset( $config['publish_only'] ) && true === $config['publish_only'] ) {
if ( 'publish' !== $action_item['post_status'] ) {
continue; // Skip draft actions if config is set to publish only.
}
}
$action = new Action( self::$recipe );
$action->hydrate_from( $action_item );
$action_items[] = $action;
}
$action_items = apply_filters( 'automator_recipe_main_object_loop_action_items', $action_items, self::$recipe, $this, self::$actions );
$this->items = $action_items;
}
Internal Usage
Found in src/core/services/recipe/structure/pluggable/conditions-pluggable.php:28:
add_filter( 'automator_recipe_main_object_loop_action_items', array( $this, 'alter_loop_action_conditions' ), 10, 4 );