Filter
uncanny-automator
ulc_automatr_codes_group_args
Filters the arguments used to group Uncanny Codes automation codes before they are processed.
add_filter( 'ulc_automatr_codes_group_args', $callback, 10, 3 );
Description
Filters the arguments used to generate and group Uncanny Codes for a batch. Modify `$args`, `$batch_id`, or `$parsed` to customize code generation settings like character type, length, or prefixes/suffixes. Useful for programmatically altering how codes are created within a batch.
Usage
add_filter( 'ulc_automatr_codes_group_args', 'your_function_name', 10, 3 );
Parameters
-
$args(mixed) - This parameter contains an array of arguments passed to the `ulc_automatr_codes_group` function, likely for configuring how codes are grouped or processed.
-
$batch_id(mixed) - This parameter contains an array of arguments passed to the `ulc_automatr_codes_group_args` filter, likely used for customizing how groups of codes are processed or generated.
-
$parsed(mixed) - This parameter contains the ID of the batch of codes being processed.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to modify the arguments passed to the ulc_automatr_codes_group_args filter.
* This example will ensure that all generated codes have a minimum length of 12 characters
* and adds a custom prefix if one isn't already set.
*
* @param array $args The arguments array for code generation.
* @param int $batch_id The ID of the batch the codes are being generated for.
* @param array $parsed The parsed data related to the code generation.
* @return array The modified arguments array.
*/
add_filter( 'ulc_automatr_codes_group_args', function( $args, $batch_id, $parsed ) {
// Ensure the code length is at least 12 characters.
if ( isset( $args['code_length'] ) && $args['code_length'] < 12 ) {
$args['code_length'] = 12;
} elseif ( ! isset( $args['code_length'] ) ) {
// If code_length is not set at all, default to 12.
$args['code_length'] = 12;
}
// Add a custom prefix if one doesn't exist.
if ( empty( $args['prefix'] ) ) {
$args['prefix'] = 'CUSTOM-';
}
// You could also conditionally modify other arguments based on $batch_id or $parsed data.
// For instance, if a specific batch_id requires a different character set:
// if ( $batch_id === 123 ) {
// $args['character_type'] = 'numeric';
// }
return $args;
}, 10, 3 );
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/actions/uc-add-batch-codes.php:148
protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
$batch_id = isset( $parsed[ $this->get_action_meta() ] ) ? absint( wp_strip_all_tags( $parsed[ $this->get_action_meta() ] ) ) : 0;
$number_of_codes = isset( $parsed['UCNUMBERS'] ) ? absint( sanitize_text_field( $parsed['UCNUMBERS'] ) ) : 0;
$prefix = isset( $parsed['UCADDPREFIX'] ) ? sanitize_text_field( $parsed['UCADDPREFIX'] ) : '';
$suffix = isset( $parsed['UCADDUSFFIX'] ) ? sanitize_text_field( $parsed['UCADDUSFFIX'] ) : '';
if ( $batch_id <= 0 || $number_of_codes <= 0 ) {
Automator()->complete->action( $user_id, $action_data, $recipe_id, esc_html__( 'Invalid request.', 'uncanny-automator' ) );
return;
}
$group_details = uncanny_learndash_codesDatabase::get_group_details( $batch_id );
// Check if batch is valid.
if ( empty( $group_details ) ) {
Automator()->complete->action( $user_id, $action_data, $recipe_id, esc_html__( 'Invalid batch provided.', 'uncanny-automator' ) );
return;
}
$generation_type = $group_details['generation_type'];
$dashes = $group_details['dashes'];
$character_type = $group_details['character_type'];
// Use batch defaults when prefix/suffix are empty (Yoda style).
if ( empty( $prefix ) && ! empty( $group_details['prefix'] ) ) {
$prefix = $group_details['prefix'];
}
if ( empty( $suffix ) && ! empty( $group_details['suffix'] ) ) {
$suffix = $group_details['suffix'];
}
// Calculate code_length from existing batch codes if available.
$code_length = $this->get_batch_code_length( $batch_id, $prefix, $suffix, $dashes );
$args = array(
'generation_type' => $generation_type,
'coupon_amount' => (int) $number_of_codes,
'custom_codes' => '',
'dashes' => $dashes,
'prefix' => $prefix,
'suffix' => $suffix,
'code_length' => $code_length,
'character_type' => $character_type,
);
$args = apply_filters( 'ulc_automatr_codes_group_args', $args, $batch_id, $parsed );
$inserted = uncanny_learndash_codesDatabase::add_codes_to_batch( $batch_id, array(), $args );
if ( $inserted ) {
do_action( 'ulc_codes_group_generated', $batch_id, $inserted );
}
Automator()->complete->action( $user_id, $action_data, $recipe_id );
}