Action uncanny-learndash-groups

ulgm_keys_added_to_group

Fires when new key codes are successfully added to a specific group.

add_action( 'ulgm_keys_added_to_group', $callback, 10, 3 );

Description

Fires after new codes are successfully added to a group. Developers can use this hook to perform actions related to the added codes, such as logging, triggering notifications, or updating related data. The hook provides the group attributes, the added codes, and the group ID.


Usage

add_action( 'ulgm_keys_added_to_group', 'your_function_name', 10, 3 );

Parameters

$attr (mixed)
This parameter contains attributes related to the code group being created or modified.
$codes (mixed)
This parameter contains attributes or metadata related to the group to which the codes are being added.
$code_group_id (mixed)
This parameter contains an array of codes that have been added to a group.

Examples

// Hook into the 'ulgm_keys_added_to_group' action to perform additional tasks after codes are added to a group.
// This example demonstrates how to log the added codes and group information to the WordPress debug log.
add_action( 'ulgm_keys_added_to_group', 'my_custom_ulgm_codes_added_handler', 10, 3 );

/**
 * Custom handler for the ulgm_keys_added_to_group action.
 *
 * @param mixed $attr         The attributes associated with the code group.
 * @param mixed $codes        An array or single value of codes added.
 * @param mixed $code_group_id The ID of the code group.
 */
function my_custom_ulgm_codes_added_handler( $attr, $codes, $code_group_id ) {
	// Ensure WP_DEBUG is enabled to see log messages.
	if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
		// Log the details of the action.
		error_log( "ULGM: Codes added to group. Group ID: " . print_r( $code_group_id, true ) );
		error_log( "ULGM: Group Attributes: " . print_r( $attr, true ) );
		error_log( "ULGM: Codes added: " . print_r( $codes, true ) );

		// Example of further processing: You might want to update user meta,
		// send a notification, or trigger another process based on the added codes.
		// For instance, if $codes contains specific types of codes, you could act on them.

		if ( is_array( $codes ) ) {
			foreach ( $codes as $code ) {
				// Example: If a code starts with 'premium_', do something specific.
				if ( is_string( $code ) && strpos( $code, 'premium_' ) === 0 ) {
					error_log( "ULGM: Premium code detected: " . $code );
					// Add your specific logic here for premium codes.
				}
			}
		}
	}
}

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/classes/handlers/class-group-management-db-handler.php:235

public function add_codes( $attr, $codes, $code_group_id = null ) {

		if ( ! ulgm()->db->if_table_exists( ulgm()->db->tbl_group_codes ) ) {
			ulgm()->db->create_tables();
		}

		if ( null === $code_group_id ) {
			$code_group_id = $this->add_code_group( $attr );
		}

		if ( empty( $codes ) ) {
			// no codes found.
			return $code_group_id;
		}
		global $wpdb;

		$chunk = 1001;

		if ( count( $codes ) > $chunk ) {
			$chunks = array_chunk( $codes, $chunk );
			if ( $chunks ) {
				$add_codes = array();
				foreach ( $chunks as $chunk ) {
					if ( is_array( $chunk ) ) {
						foreach ( $chunk as $co ) {
							$add_codes[] = '(' . absint( $code_group_id ) . " ,'$co', '" . SharedFunctions::$available_status . "' )";
						}
					} else {
						$add_codes[] = '(' . absint( $code_group_id ) . " ,'$chunk')";
					}
				}
				if ( $add_codes ) {
					$wpdb->query( "INSERT INTO $wpdb->prefix" . ulgm()->db->tbl_group_codes . ' (`group_id`, `code`, `code_status`) VALUES ' . implode( ',', $add_codes ) );
				}
			}
		} else {
			$add_codes = array();
			if ( $codes ) {
				foreach ( $codes as $chunk ) {
					$add_codes[] = '(' . absint( $code_group_id ) . ",'$chunk', '" . SharedFunctions::$available_status . "' )";
				}
				if ( $add_codes ) {
					$wpdb->query( "INSERT INTO $wpdb->prefix" . ulgm()->db->tbl_group_codes . ' (`group_id`, `code`, `code_status`) VALUES ' . implode( ',', $add_codes ) );
				}
			}
		}

		do_action( 'ulgm_keys_added_to_group', $attr, $codes, $code_group_id );

		return $code_group_id;
	}

Scroll to Top