automator_recipe_main_object_action_items
Conditions are implemented as post_meta instead of post type so we cannot retrieve the items directly from db as a part of query that looks up the child items of the recipe. We have to resolve the actions, and loops first, then modify the whole object to resolve the conditions. The conditions are hooked into 'automator_recipe_main_object_action_items'. We're querying the record from the database and wrap the action into its condition. Filters recipe action items to resolve conditions after actions and loops are processed.
add_filter( 'automator_recipe_main_object_action_items', $callback, 10, 2 );
Description
This filter allows modification of action items within a recipe, specifically to integrate conditions. Since conditions are stored as post meta, they aren't directly queryable with actions. Use this hook to resolve and wrap actions with their associated conditions before they are processed, enabling custom logic for condition management and retrieval.
Usage
add_filter( 'automator_recipe_main_object_action_items', 'your_function_name', 10, 2 );
Parameters
-
$action_items(mixed) - - **$recipe** `mixed`
-
$this(mixed)
Return Value
The filtered value.
Examples
/**
* Adds a custom action item to the Automator recipe based on specific conditions.
*
* This filter allows developers to programmatically add or modify action items
* within an Automator recipe's main object. In this example, we're adding a
* custom action if the recipe belongs to a specific user role.
*
* @param array $action_items The existing array of action items.
* @param object $recipe The current recipe object.
* @param object $current_service_instance The instance of the service running this filter.
*
* @return array The modified array of action items, potentially with a new custom item added.
*/
function my_custom_automator_action_item( $action_items, $recipe, $current_service_instance ) {
// Check if the recipe has a specific meta key indicating it's for a particular user role.
$recipe_user_role = $recipe->get_meta( 'recipe_for_user_role', true );
if ( 'editor' === $recipe_user_role ) {
// If the recipe is for the 'editor' role, add a custom action item.
// In a real scenario, this would be an instance of a custom action class.
// For demonstration, we'll use a simple array representation.
$action_items[] = array(
'id' => 'my_custom_editor_action',
'name' => __( 'Send Editor Notification', 'my-text-domain' ),
'description' => __( 'Sends a notification to editors when the recipe is triggered.', 'my-text-domain' ),
// Add any other necessary properties for your custom action.
);
}
// Always return the modified (or unmodified) $action_items array.
return $action_items;
}
// Add the filter to WordPress.
// The '10' is the priority, and '3' specifies that our callback function accepts 3 arguments.
add_filter( 'automator_recipe_main_object_action_items', 'my_custom_automator_action_item', 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/services/recipe/structure/actions/actions.php:92
private function fill_items() {
$recipe_id = self::$recipe->get_recipe_id();
$loop_db = new Loop_Db();
$actions = Automator()->get_recipe_data( AUTOMATOR_POST_TYPE_ACTION, $recipe_id );
$loops = $loop_db->find_recipe_loops( $recipe_id );
$action_items = array();
foreach ( $actions as $action_item ) {
$action = new ItemAction( self::$recipe );
$action->hydrate_from( $action_item );
$action_items[] = $action;
}
/**
* Conditions are implemented as post_meta instead of post type so we cannot retrieve
* the items directly from db as a part of query that looks up the child items of the recipe.
*
* We have to resolve the actions, and loops first, then modify the whole object
* to resolve the conditions.
*
* The conditions are hooked into 'automator_recipe_main_object_action_items'.
* We're querying the record from the database and wrap the action into its condition.
*
* @see Item/Conditions_Pluggable ~item/conditions-pluggable.php
*/
$items = apply_filters( 'automator_recipe_main_object_action_items', $action_items, self::$recipe, $this );
$recipe_closure = Automator()->get_recipe_closure( self::$recipe->get_recipe_id() );
if ( ! empty( $recipe_closure ) ) {
$items[] = new StructureClosure( self::$recipe, $recipe_closure );
}
// Loops
foreach ( $loops as $loop ) {
$items[] = new StructureActionsItemLoop( self::$recipe, $this, $loop['ID'] );
}
// End Loops.
// Sort by ui_order.
$items = json_decode( wp_json_encode( $items ), true );
usort( $items, array( $this, 'sort_by_ui_order' ) );
$this->items = $items;
}
Internal Usage
Found in src/core/services/recipe/structure/pluggable/conditions-pluggable.php:25:
add_filter( 'automator_recipe_main_object_action_items', array( $this, 'alter_recipe_object_conditions' ), 10, 3 );