Filter
uncanny-automator
automator_edd_validate_trigger_code_parse
Filters EDD trigger code before parsing to allow custom validation and manipulation of parsed pieces.
add_filter( 'automator_edd_validate_trigger_code_parse', $callback, 10, 1 );
Description
Filters the array of valid trigger codes used for parsing Easy Digital Downloads trigger data. Developers can add custom trigger codes to validate them before they are parsed by the Automator plugin.
Usage
add_filter( 'automator_edd_validate_trigger_code_parse', 'your_function_name', 10, 1 );
Parameters
-
$pieces(mixed) - This parameter is an array that initially contains the string 'EDD_ANON_PURCHASE', used to define validation rules for EDD trigger codes.
Return Value
The filtered value.
Examples
/**
* Example of how to hook into 'automator_edd_validate_trigger_code_parse'.
* This example adds a custom validation to ensure that if a specific token
* related to anonymous purchases is used, it also checks for a required parameter.
*
* @param array $trigger_validations The array of trigger validations.
* @param array $validation_args An array containing arguments for the validation.
* Expected keys: 'pieces', 'recipe_id', 'trigger_data', 'user_id'.
*
* @return array The modified array of trigger validations.
*/
add_filter( 'automator_edd_validate_trigger_code_parse', function( $trigger_validations, $validation_args ) {
// Ensure we have the necessary arguments for validation.
if ( ! isset( $validation_args['pieces'] ) || ! isset( $validation_args['trigger_data'] ) ) {
return $trigger_validations;
}
$pieces = $validation_args['pieces'];
$trigger_data = $validation_args['trigger_data'];
// Let's assume 'EDD_ANON_PURCHASE' is a special token type.
// We want to ensure that if this token is being parsed, it has a specific expected parameter.
// For this example, let's say the second piece (index 1) must be a specific string, like 'guest'.
if ( in_array( 'EDD_ANON_PURCHASE', $trigger_validations ) && isset( $pieces[1] ) ) {
// If the token is 'EDD_ANON_PURCHASE' and the second piece is NOT 'guest',
// then we consider this an invalid parsing for this specific trigger.
// We can remove the 'EDD_ANON_PURCHASE' validation to signify it failed.
if ( $pieces[1] !== 'guest' ) {
$trigger_validations = array_diff( $trigger_validations, array( 'EDD_ANON_PURCHASE' ) );
}
}
return $trigger_validations;
}, 10, 2 );
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/edd/tokens/edd-tokens.php:82
*/
public function parse_edd_tokens( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {
if ( ! is_array( $pieces ) || ! isset( $pieces[1] ) || ! isset( $pieces[2] ) ) {
return $value;
}
$trigger_meta_validations = apply_filters(
'automator_edd_validate_trigger_code_parse',
array( 'EDD_ANON_PURCHASE' ),
array(
'pieces' => $pieces,
'recipe_id' => $recipe_id,
'trigger_data' => $trigger_data,
'user_id' => $user_id,