Filter uncanny-automator

automator_escape_matching_characters

Filters the characters to be escaped in automation actions, allowing customization of the escape set.

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

Description

Filters the characters to be escaped within the `string_contains` trigger condition's regular expression. This allows developers to customize which special characters are escaped when matching string content, providing greater flexibility in defining trigger conditions.


Usage

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

Return Value

The filtered value.


Examples

add_filter(
	'automator_escape_matching_characters',
	function( $characters_to_escape ) {
		/**
		 * Example: Prevent escaping of specific characters within a string_contains condition.
		 *
		 * This filter hook is used to modify the set of characters that are escaped
		 * before being used in a regular expression for the 'string_contains' notation.
		 * In this example, we are adding the exclamation mark (!) to the list of characters
		 * that should *not* be escaped, allowing it to be treated as a literal character
		 * in the regex if it's part of the `where` value.
		 */

		// Add '!' to the list of characters to escape.
		// This means that if '!' is present in the $where parameter, it won't be escaped
		// and will be treated literally by preg_match.
		$characters_to_escape .= '!';

		return $characters_to_escape;
	},
	10,
	1
);

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:365

private function conditions_matched( $notation, $where, $condition, $recipe_id ) {

		// Process non-integer inputs.
		if ( ! empty( $this->get_compare() ) && ( ! is_numeric( $where ) || ! is_numeric( $condition ) ) ) {

			$condition_matched = ( $where === $condition );

			$assert_message = gettype( $where ) . ":{$where} {$notation} " . gettype( $condition ) . ":$condition | Result: " . ( $condition_matched ? 'Matched' : 'Failed' );

			// Special string_contains notation.
			// @since 4.4
			if ( 'string_contains' === $notation ) {

				$add_slashes = apply_filters( 'automator_escape_matching_characters', '/._-:\' );

				$condition_matched = preg_match( '/(' . addcslashes( $where, $add_slashes ) . ')/i', $condition );

				$this->push_log( 'Asserting ' . $assert_message, 'assertions', $recipe_id );

				return $condition_matched;

			}

			$this->push_log( 'Asserting (non-numeric)' . $assert_message, 'assertions', $recipe_id );

			return $condition_matched;

		}

		// Otherwise, process with equality sign.
		$condition_matched = $this->match_condition( $notation, $where, $condition );

		/**
		 * Allow external developers to manipulate the true/false based on specific criteria.
		 *
		 * WARNING: THIS IS A CORE FUNCTIONALITY THAT MATCHES A VALUE BASED ON A CRITERIA, E.X.,
		 * A USER ENTERED A SPECIFIC VALUE IN A FIELD.
		 * OVERRIDING THE LOGIC INCORRECTLY WILL RESULT IN FAILED RECIPE RUNS. USE IT WISELY!
		 *
		 * add_filter( 'automator_trigger_filter_condition_matched', function($condition_matched, $notation, $where, $condition, $recipe_id) {
		 *     // doing something;
		 *     return $condition_matched; // Must return boolean True or False.
		 * }, 99, 5 );
		 *
		 * @since v4.8
		 * @author Saad S.
		 * @author Joseph G.
		 */
		$condition_matched = apply_filters( 'automator_trigger_filter_condition_matched_' . $recipe_id, $condition_matched, $notation, $where, $condition, $recipe_id );

		$this->push_log(
			'Asserting field option with value ' . gettype( $where ) . ":{$where} {$notation} " .
			'given value ' . gettype( $condition ) . ":$condition | Result: " .
			( $condition_matched ? 'Matched (with ' . gettype( $condition ) . ' data type converted to int)' : 'Failed' ),
			'assertions',
			$recipe_id
		);

		return $condition_matched;

	}

Scroll to Top