Action uncanny-automator

ulc_codes_group_generated

Fires after a batch of codes is generated and inserted into the database.

add_action( 'ulc_codes_group_generated', $callback, 10, 2 );

Description

Fired after a new group of Uncanny Codes are generated and added to a batch. Developers can use this hook to perform custom actions with the generated codes, such as logging or triggering other integrations. It passes the batch ID and a status indicating successful insertion.


Usage

add_action( 'ulc_codes_group_generated', 'your_function_name', 10, 2 );

Parameters

$batch_id (mixed)
This parameter contains the ID of the batch of codes that was just generated.
$inserted (mixed)
This parameter contains the ID of the batch of codes that was generated.

Examples

<?php
/**
 * Example callback function for the 'ulc_codes_group_generated' action hook.
 * This function logs information about a newly generated batch of codes.
 *
 * @param int   $batch_id  The ID of the batch for which codes were generated.
 * @param array $inserted  An array of codes that were successfully inserted into the batch.
 */
function my_uncanny_codes_batch_generated_logger( $batch_id, $inserted ) {
	// Basic check to ensure we have valid data before proceeding.
	if ( ! is_numeric( $batch_id ) || empty( $inserted ) ) {
		error_log( 'my_uncanny_codes_batch_generated_logger: Received invalid data. Batch ID: ' . var_export( $batch_id, true ) . ', Inserted: ' . var_export( $inserted, true ) );
		return;
	}

	// Get the batch details to include in the log.
	// Assuming a function like get_post() or a custom CPT lookup exists for batches.
	// Replace with actual function if 'uncanny-codes' uses custom post types or a different data structure.
	$batch = get_post( $batch_id ); // Example if codes are stored as a custom post type.
	$batch_title = $batch ? $batch->post_title : 'Unknown Batch';

	// Log the event.
	$log_message = sprintf(
		'Uncanny Codes: A new batch of codes has been generated for "%s" (Batch ID: %d). %d codes were inserted.',
		$batch_title,
		$batch_id,
		count( $inserted )
	);

	// For demonstration, we'll use WordPress's built-in error logging.
	// In a real-world scenario, you might use a more sophisticated logging library
	// or a dedicated logging plugin.
	error_log( $log_message );

	// Optionally, you could perform other actions here, such as:
	// - Notifying administrators.
	// - Triggering further automated processes based on the number of codes generated.
	// - Adding data to a custom analytics table.

	// Example: If you wanted to check if a specific number of codes were generated
	// and take action.
	if ( count( $inserted ) < 10 ) {
		error_log( 'Uncanny Codes Warning: Batch ' . $batch_id . ' has fewer than 10 codes generated.' );
		// You could potentially send an email alert here.
	}
}

// Register the action hook callback.
// The third parameter '2' indicates that this callback function accepts two arguments: $batch_id and $inserted.
add_action( 'ulc_codes_group_generated', 'my_uncanny_codes_batch_generated_logger', 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/uncanny-codes/actions/uc-add-batch-codes.php:153

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 );
	}


Scroll to Top