FluentCRM custom Regex Tag Matcher

I’ve got some free snippets that I will begin releasing. This one is for FluentCRM – it’s a custom condition that allows us to match tags through a REGEX


add_action('plugins_loaded', function () {
   new DVARegexCondition();
});

/* purpose: to apply a regex matcher to tags on a contact
   (C) DataVaultAlliance Holdings LLC, 2025
    written by: Dan Linstedt
    for: FluentCRM
    license - you may use this code freely, but not for packaging and redistribution
    in a commercial product.  The only company allowed to embed this code
    is wpManageNinjas if it were to be embedded in the core engine of FluentCRM
    this code is provided AS IS
*/

class DVARegexCondition
{
    public function __construct()
    {
        add_filter('fluentcrm_automation_condition_groups', array($this, 'addAutomationConditions'), 10, 2);
        add_filter('fluentcrm_automation_conditions_assess_regex', array($this, 'assessAutomationConditions'), 10, 5);
    }

    public function addAutomationConditions($groups, $funnel)
    {
        $customerItems = [
            [
                'value'             => 'tags_contain',
                'label'             => 'Tags Contain',
				'help'              => 'Please enter a valid PHP preg_match regex. do NOT include leading or trailing slashes. Each tag will be compared (case insensitive) to the regex pattern to return a true or false overall',
				'custom_operators' => [
                    'matches'  => 'matches'
                ],
                'type'              => 'text',
                'is_singular_value' => true,
                'disabled'          => false
            ]
        ];

        $groups['regex'] = [
            'label'    => 'Regex Match',
            'value'    => 'regex',
            'children' => $customerItems,
        ];

        return $groups;
    }

	//    public function assessAutomationConditions($result, $conditions, $subscriber)
	public function assessAutomationConditions($result, $conditions, $subscriber, $sequence, $funnelSubscriberId)
	{
		foreach ($conditions as $condition) {
            $prop = $condition['data_key'];
            $operator = $condition['operator'];
			$value    = $condition['data_value'];
			
			if ($prop === 'tags_contain' && !empty($value)) {
				$regex = $value;
				$pattern = "/$regex/i";

				if (@preg_match($pattern, '') === false) {
					return false; // Invalid regex
				}

				$tagNames = $subscriber->tags->pluck('title')->toArray();

				foreach ($tagNames as $tagName) {
					if (preg_match($pattern, $tagName)) {
						return true;
					}
				}

				return false; // No match found
			}
		}

		return $result; // fallback if no conditions matched
	}

}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *