Filter
uncanny-automator-pro
automator_groundhogg_pro_validate_trigger_meta_pieces_common
Filters Groundhogg trigger meta pieces to validate common data before they are processed.
add_filter( 'automator_groundhogg_pro_validate_trigger_meta_pieces_common', $callback, 10, 1 );
Description
Filters the meta pieces used to validate Groundhogg trigger data. This hook allows developers to add or modify validation rules for common Groundhogg trigger meta, ensuring data integrity before processing. It's typically used within Groundhogg integrations to customize how trigger data is handled.
Usage
add_filter( 'automator_groundhogg_pro_validate_trigger_meta_pieces_common', 'your_function_name', 10, 1 );
Parameters
-
$pieces(mixed) - This parameter represents an array of token names that the automator will check for during validation, specifically looking for Groundhogg contact note tokens.
Return Value
The filtered value.
Examples
/**
* Custom validation logic for Groundhogg anonymous contact note trigger meta.
*
* This callback function intercepts the 'automator_groundhogg_pro_validate_trigger_meta_pieces_common'
* filter to add specific validation for the 'ANON_GH_CONTACT_NOTE' trigger meta.
* It ensures that if this meta is present, it contains a string value.
*
* @param array $pieces An array containing trigger meta information. Expected to be [0 => trigger_code, 1 => meta_key, 2 => meta_value].
* @param array $data An array containing contextual data for the validation.
* 'pieces' => The trigger meta pieces.
* 'recipe_id' => The ID of the current recipe.
* 'trigger_data' => The data for the current trigger.
* 'user_id' => The ID of the current user.
* @return array The modified $pieces array, with validation checks applied.
*/
add_filter( 'automator_groundhogg_pro_validate_trigger_meta_pieces_common', function( $pieces, $data ) {
// Ensure we have the expected structure for $pieces and $data.
if ( ! is_array( $pieces ) || count( $pieces ) < 3 || ! is_array( $data ) || ! isset( $data['pieces'] ) ) {
return $pieces;
}
// Extract relevant data from the $data array.
$trigger_meta_pieces = $data['pieces'];
// Check if the current meta key being processed is 'ANON_GH_CONTACT_NOTE'.
if ( isset( $trigger_meta_pieces[1] ) && 'ANON_GH_CONTACT_NOTE' === $trigger_meta_pieces[1] ) {
// Perform validation: ensure the meta value is a string.
if ( ! is_string( $trigger_meta_pieces[2] ) ) {
// In a real-world scenario, you might want to add an error message
// or log this issue. For this example, we'll just log it.
error_log( sprintf(
'Groundhogg Anonymous Contact Note Trigger Error: Meta value for recipe %d trigger %d is not a string. Value: %s',
$data['recipe_id'],
$data['trigger_data']['ID'], // Assuming trigger_data has an 'ID' key.
print_r( $trigger_meta_pieces[2], true )
) );
// Optionally, you could return false or an empty array to invalidate the trigger.
// For this example, we'll let it pass but log the issue.
}
}
// Always return the original or modified $pieces array.
return $pieces;
}, 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
uncanny-automator-pro/src/integrations/groundhogg/tokens/groundhogg-anon-tokens.php:175
public function gh_parse_tokens( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {
$piece_one = 'ANONGHTAGAPPLIED';
$piece_two = 'ANONGHTAGREMOVED';
global $wpdb;
if ( $pieces ) {
if ( in_array( $piece_one, $pieces ) || in_array( $piece_two, $pieces ) ) {
if ( $trigger_data ) {
foreach ( $trigger_data as $trigger ) {
$trigger_id = $trigger['ID'];
$meta_field = $pieces[2];
$meta_value = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->prefix}uap_trigger_log_meta WHERE meta_key = %s AND automator_trigger_id = %d ORDER BY ID DESC LIMIT 0,1", $meta_field, $trigger_id ) );
if ( ! empty( $meta_value ) ) {
$meta_value = maybe_unserialize( $meta_value );
}
if ( is_array( $meta_value ) ) {
$value = join( ', ', $meta_value );
} else {
$value = $meta_value;
}
}
}
}
}
if ( ! is_array( $pieces ) || ! isset( $pieces[1] ) || ! isset( $pieces[2] ) ) {
return $value;
}
$trigger_meta_validations = apply_filters(
'automator_groundhogg_pro_validate_trigger_meta_pieces_common',
array( 'ANON_GH_CONTACT_NOTE' ),
array(
'pieces' => $pieces,
'recipe_id' => $recipe_id,
'trigger_data' => $trigger_data,
'user_id' => $user_id,
'replace_args' => $replace_args,
)
);
if ( ! array_intersect( $trigger_meta_validations, $pieces ) ) {
return $value;
}
$to_replace = $pieces[2];
$contact_id = Automator()->db->token->get( 'contact_id', $replace_args );
$contact = new Contact( $contact_id );
$note = Automator()->db->token->get( 'note', $replace_args );
switch ( $to_replace ) {
case 'GH_CONTACT_FNAME':
$value = $contact->get_first_name();
break;
case 'GH_CONTACT_LNAME':
$value = $contact->get_last_name();
break;
case 'GH_CONTACT_EMAIL':
$value = $contact->get_email();
break;
case 'GH_CONTACT_ID':
$value = $contact_id;
break;
case 'GH_CONTACT_NOTE':
$value = $note;
break;
}
return $value;
}