automator_wpdm_validate_trigger_meta_pieces_common
Filters download manager trigger meta pieces for validation before saving, allowing modification of validation arguments.
add_filter( 'automator_wpdm_validate_trigger_meta_pieces_common', $callback, 10, 1 );
Description
This filter allows developers to validate specific pieces of meta data for WP Download Manager triggers within AutomatorWP. It's invoked during the saving process of trigger data. Developers can modify the validation array to add or alter validation rules for trigger meta, ensuring data integrity.
Usage
add_filter( 'automator_wpdm_validate_trigger_meta_pieces_common', 'your_function_name', 10, 1 );
Parameters
-
$args(mixed) - This parameter contains an array of specific codes that identify a file download event.
Return Value
The filtered value.
Examples
/**
* Filters the common trigger meta pieces for WP Download Manager to add custom validation.
*
* This example demonstrates how to add a custom validation check for a specific
* trigger, ensuring that only allowed 'codes' are processed.
*
* @param array $trigger_meta_validations An array of validation codes.
* @param array $args The arguments passed to the filter.
* Expected to contain 'entry_args' with a 'code' key.
*
* @return array The modified array of validation codes.
*/
function my_custom_wpdm_trigger_validation( $trigger_meta_validations, $args ) {
// Ensure we have the necessary arguments to proceed
if ( ! isset( $args['entry_args']['code'] ) ) {
return $trigger_meta_validations;
}
$current_code = $args['entry_args']['code'];
// Example: Add a new custom validation code if it's not already present
// and only if the current code is one we want to process further.
$custom_code_to_add = 'MY_CUSTOM_DOWNLOAD_EVENT';
if ( $current_code === 'SPECIFIC_FILE_DOWNLOADED_CODE' && !in_array( $custom_code_to_add, $trigger_meta_validations ) ) {
$trigger_meta_validations[] = $custom_code_to_add;
}
// Example: Remove a validation code if it's not supposed to be handled
// by this specific integration or process.
$code_to_remove = 'SOME_OTHER_WPJM_CODE';
if ( ( $key = array_search( $code_to_remove, $trigger_meta_validations ) ) !== false ) {
unset( $trigger_meta_validations[ $key ] );
}
// Always return the (potentially modified) array.
return $trigger_meta_validations;
}
add_filter( 'automator_wpdm_validate_trigger_meta_pieces_common', 'my_custom_wpdm_trigger_validation', 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/wp-download-manager/tokens/wpdm-tokens.php:36
src/integrations/wp-download-manager/tokens/wpdm-tokens.php:62
src/integrations/wp-download-manager/tokens/wpdm-tokens.php:181
public function save_token_data( $args, $trigger ) {
if ( ! isset( $args['trigger_args'] ) || ! isset( $args['entry_args']['code'] ) ) {
return;
}
$trigger_meta_validations = apply_filters(
'automator_wpdm_validate_trigger_meta_pieces_common',
array( 'SPECIFIC_FILE_DOWNLOADED_CODE' ),
$args
);
if ( in_array( $args['entry_args']['code'], $trigger_meta_validations ) ) {
$package = array_shift( $args['trigger_args'] );
$trigger_log_entry = $args['trigger_entry'];
if ( ! empty( $package ) ) {
Automator()->db->token->save( 'package_data', maybe_serialize( $package ), $trigger_log_entry );
}
}
}