Filter uncanny-automator

automator_uncannycodes_validate_triggers_for_code_token

Filters triggers for code token validation, allowing customization of accepted trigger types.

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

Description

Filters the array of valid Uncanny Codes trigger codes used in Automator. Developers can add or remove trigger codes from this list to customize which Uncanny Codes triggers are recognized and processed by the plugin. This hook fires during the validation of trigger codes for Uncanny Codes integrations.


Usage

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

Parameters

$args (mixed)
This parameter contains an array of strings representing specific token types or validation categories related to Uncanny Codes.

Return Value

The filtered value.


Examples

add_filter(
	'automator_uncannycodes_validate_triggers_for_code_token',
	'my_custom_uncanny_codes_trigger_validation',
	10,
	2
);

/**
 * Custom validation for Uncanny Codes triggers to include a new trigger code.
 *
 * This function extends the default list of trigger codes that the Uncanny Automator
 * plugin checks for when handling Uncanny Codes integrations. It adds a new,
 * custom trigger code 'MY_CUSTOM_CODE_REDEMPTION' to the list of validations.
 *
 * @param array $trigger_meta_validations The array of trigger codes to validate.
 * @param array $args                     The arguments passed to the filter.
 * @return array                          The modified array of trigger codes.
 */
function my_custom_uncanny_codes_trigger_validation( $trigger_meta_validations, $args ) {
	// Check if the arguments are structured as expected before proceeding.
	if ( ! isset( $args['triggers_meta']['code'] ) ) {
		return $trigger_meta_validations;
	}

	// Define our custom trigger code.
	$custom_trigger_code = 'MY_CUSTOM_CODE_REDEMPTION';

	// Add our custom trigger code to the array if it's not already present.
	if ( ! in_array( $custom_trigger_code, $trigger_meta_validations, true ) ) {
		$trigger_meta_validations[] = $custom_trigger_code;
	}

	// Return the updated array of trigger meta validations.
	return $trigger_meta_validations;
}

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/integrations/uncanny-codes/tokens/uc-tokens.php:61

public function uc_codes_code_token( $tokens = array(), $args = array() ) {

		$trigger_code = $args['triggers_meta']['code'];

		$trigger_meta_validations = apply_filters(
			'automator_uncannycodes_validate_triggers_for_code_token',
			array( 'CODEREDEEMED', 'UCBATCH', 'UCPREFIX', 'UCSUFFIX' ),
			$args
		);

		if ( in_array( $trigger_code, $trigger_meta_validations, true ) ) {

			$fields = array(
				array(
					'tokenId'         => 'CODE_REDEEMED',
					'tokenName'       => esc_html__( 'Code', 'uncanny-automator' ),
					'tokenType'       => 'text',
					'tokenIdentifier' => $trigger_code,
				),
				array(
					'tokenId'         => 'REMAINING_CODES',
					'tokenName'       => esc_html__( 'Remaining codes', 'uncanny-automator' ),
					'tokenType'       => 'text',
					'tokenIdentifier' => $trigger_code,
				),
				array(
					'tokenId'         => 'TOTAL_CODES',
					'tokenName'       => esc_html__( 'Total codes', 'uncanny-automator' ),
					'tokenType'       => 'text',
					'tokenIdentifier' => $trigger_code,
				),
			);

			$tokens = array_merge( $tokens, $fields );
		}

		return $tokens;

	}

Scroll to Top