Filter uncanny-automator

automator_conditionally_matched_recipes

Return us filtered recipe ids after validating trigger conditions. Filters recipe IDs after validating trigger conditions are met for a recipe.

add_filter( 'automator_conditionally_matched_recipes', $callback, 10, 1 );

Description

Fires after recipe trigger conditions are validated. Developers can filter the array of matched recipe IDs, excluding recipes that don't meet specific criteria. This hook is a deprecated alternative to overriding `validate_conditions()` and using `find_all()`.


Usage

add_filter( 'automator_conditionally_matched_recipes', 'your_function_name', 10, 1 );

Parameters

$this (mixed)
- **$this** `mixed`

Return Value

The filtered value.


Examples

<?php
/**
 * Example of filtering conditionally matched recipe IDs.
 *
 * This callback demonstrates how to modify the array of recipe IDs that
 * have met the trigger conditions. In this specific example, we're
 * further filtering the recipes to only include those that have a
 * specific meta key set, simulating an additional check after the
 * initial condition validation.
 *
 * @param array $matched_recipe_ids An array of recipe IDs that have conditionally matched.
 * @param object $trigger_object The object representing the trigger.
 * @return array The filtered array of recipe IDs.
 */
add_filter( 'automator_conditionally_matched_recipes', function( $matched_recipe_ids, $trigger_object ) {

	// If there are no matched recipes, return early.
	if ( empty( $matched_recipe_ids ) ) {
		return $matched_recipe_ids;
	}

	// Get all recipe objects that were matched.
	// This assumes the $trigger_object has a method like 'get_recipes_by_ids'
	// that returns full recipe objects. Adapt this if your trigger object
	// works differently.
	$all_matched_recipes = $trigger_object->get_recipes_by_ids( $matched_recipe_ids );

	// Initialize an array to store IDs that pass our custom filter.
	$filtered_recipe_ids = [];

	// Iterate through each recipe object.
	foreach ( $all_matched_recipes as $recipe_id => $recipe_object ) {
		// Example custom condition: Check if the recipe has a specific meta key set.
		// Replace 'your_custom_meta_key' with the actual meta key you want to check.
		// This is just a hypothetical example; the actual logic would depend on
		// how your recipes store custom metadata.
		if ( get_post_meta( $recipe_id, 'your_custom_meta_key', true ) !== '' ) {
			$filtered_recipe_ids[] = $recipe_id;
		}
	}

	// Return the IDs of recipes that passed both the default and our custom filter.
	return $filtered_recipe_ids;

}, 10, 2 ); // The priority is 10, and it accepts 2 arguments.

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/triggers/trait-triggers.php:169

* @deprecated v4.3 | Use $this->validate_conditions() override with $this->find_all()
			 */
			$this->trigger_conditions( $args );

			/**
			 * Return us filtered recipe ids after validating trigger conditions.
			 */
			$matched_recipe_ids = apply_filters( 'automator_conditionally_matched_recipes', $this->validate_conditions( $args ), $this );
			/**
			 * Trigger failed to satisfy conditions. bailing...
			 */
			if ( empty( $matched_recipe_ids ) ) {
				return false;
			}

Scroll to Top