Filter Since 4.15.1 uncanny-automator

automator_recipe_filters_get_where_values_zero_as_any

Ability for Trigger to overwrite this option in case zero is a valid option. Filters whether zero should be treated as any value for automator recipe filters, allowing triggers to override this behavior.

add_filter( 'automator_recipe_filters_get_where_values_zero_as_any', $callback, 10, 2 );

Description

Allow triggers to override the default behavior for zero values in filter conditions. By default, zero is treated as "any". This filter enables triggers to specify if zero is a valid, distinct option for a particular filter, maintaining backwards compatibility while offering greater flexibility.


Usage

add_filter( 'automator_recipe_filters_get_where_values_zero_as_any', 'your_function_name', 10, 2 );

Parameters

$trigger_id (mixed)
- **$recipe_id** `mixed`
$trigger_meta (mixed)
- **$where_value** `mixed`

Return Value

The filtered value.


Examples

/**
 * Example: Customize how the '0' value is treated for the 'User logged in' trigger.
 * By default, if $where_value is 0, it's treated as "any". This filter allows
 * overriding that behavior if, for a specific trigger, 0 has a distinct meaning.
 *
 * For instance, let's say for a hypothetical "User logged in more than X times" trigger,
 * a value of 0 means "any user" (the default behavior). But if we want to specifically
 * target users who logged in *exactly* 0 times (which is unlikely but for example's sake),
 * we could use this filter.
 */
add_filter(
	'automator_recipe_filters_get_where_values_zero_as_any',
	function ( $use_zero_as_any, $trigger_id, $recipe_id, $trigger_meta, $where_value ) {
		// Example: For a hypothetical "User has a specific number of posts" trigger (let's imagine its ID is 'user_post_count').
		// If the trigger_id matches, and the $where_value is 0, we might want to treat 0 as an exact match rather than 'any'.
		// In this specific scenario, if a user has 0 posts, that's a valid, specific count.
		// If it's not this specific trigger, we fall back to the default behavior.
		if ( $trigger_id === 'user_post_count' && $where_value === 0 ) {
			// Setting this to false means that '0' will be treated as the literal value '0',
			// and not as a wildcard for 'any'.
			return false;
		}

		// For all other triggers, or if $where_value is not 0,
		// maintain the default behavior.
		return $use_zero_as_any;
	},
	10, // Priority
	5  // Number of accepted 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-trigger-recipe-filters.php:488

protected function get_where_values( $trigger_id, $recipe_id ) {

		// Reset the values. Hooks that executed multiple times can fill the where values.
		$this->actual_where_values = array();

		foreach ( $this->get_where_conditions() as $i => $trigger_option_code ) {

			$trigger_meta = Automator()->get->meta_from_recipes( $this->get_recipes(), $trigger_option_code );

			$where_value = $trigger_meta[ $recipe_id ][ $trigger_id ];

			$this->push_log( "with trigger id: {$trigger_id} from option_code: {$trigger_option_code}", 'recipe_trigger_field_values', $recipe_id );

			/**
			 * Ability for Trigger to overwrite this option in case zero is a valid option.
			 *
			 * @since 4.15.1
			 */
			$use_zero_as_any = apply_filters(
				'automator_recipe_filters_get_where_values_zero_as_any',
				true,
				$trigger_id,
				$recipe_id,
				$trigger_meta,
				$where_value
			);

			// Determine whethere to use zero as any and if the where_value is zero or not.
			$where_value_is_zero_as_any = ( true === $use_zero_as_any )
				// Make sure to check if where value is numeric as intval will cast string to 0.
				&& ( is_numeric( $where_value ) && 0 === intval( $where_value ) );

			if ( $where_value_is_zero_as_any || intval( - 1 ) === intval( $where_value ) ) {

				// Make the where value equals to match condition automatically so it becomes valid.
				$where_value = $this->match_conditions[ $i ];

				$this->push_log = sprintf( 'Comparing %s:%s as "Any"', gettype( $this->match_conditions[ $i ] ), $this->match_conditions[ $i ], 'any_assertions' );

			}

			$this->actual_where_values[] = $this->value_format( $where_value, $this->get_conditions_format( $i ) );

		}

		return $this->actual_where_values;

	}

Scroll to Top