ulgm_orphaned_codes_purged
Fires after orphaned coupon codes are purged, providing the number affected and the code group ID.
add_action( 'ulgm_orphaned_codes_purged', $callback, 10, 2 );
Description
Fires after orphaned codes within a specific group (or all groups if $code_group_id is 0) are reset to 'available' status. Developers can use this hook to perform additional cleanup or logging related to the purged codes. The $affected parameter contains the number of codes updated, and $code_group_id specifies the group ID.
Usage
add_action( 'ulgm_orphaned_codes_purged', 'your_function_name', 10, 2 );
Parameters
-
$affected(mixed) - The `$affected` parameter contains the number of orphaned codes that were purged.
-
$code_group_id(mixed) - This parameter contains the number of orphaned codes that were affected (purged) by the action.
Examples
// Hook into the 'ulgm_orphaned_codes_purged' action to log purged codes.
// This function will be executed after orphaned codes have been identified and reset.
add_action( 'ulgm_orphaned_codes_purged', function( $affected, $code_group_id ) {
// Check if any codes were actually affected by the purge.
if ( $affected > 0 ) {
// Format a message indicating the number of codes purged and the group ID they belonged to.
$message = sprintf(
__( 'Successfully purged %1$d orphaned codes. Group ID: %2$d.', 'your-text-domain' ),
$affected,
$code_group_id
);
// Log the message to the WordPress debug log for administrative review.
// In a real-world scenario, you might also want to notify an administrator,
// display a notice to the user, or store this information in a custom log table.
error_log( $message );
} else {
// If no codes were affected, log a message indicating this.
// This can be helpful for debugging or confirming that the purge process ran without issues.
$message = sprintf(
__( 'Orphaned code purge completed. No codes were affected for Group ID: %1$d.', 'your-text-domain' ),
$code_group_id
);
error_log( $message );
}
}, 10, 2 ); // 10 is the priority, 2 is the number of arguments accepted by the callback function.
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/group-management/class-group-management-helpers.php:1987
public static function purge_orphaned_codes( int $code_group_id = 0 ) {
global $wpdb;
$tbl = $wpdb->prefix . ulgm()->db->tbl_group_codes;
$group_clause = $code_group_id > 0
? $wpdb->prepare( 'AND c.group_id = %d', $code_group_id )
: '';
$affected = $wpdb->query(
"UPDATE {$tbl} c
LEFT JOIN {$wpdb->users} u ON u.ID = c.student_id
SET c.code_status = 'available',
c.student_id = NULL,
c.user_email = NULL,
c.first_name = NULL,
c.last_name = NULL,
c.used_date = NULL,
c.ld_group_id = NULL
WHERE c.student_id IS NOT NULL
AND u.ID IS NULL
{$group_clause}"
);
if ( false === $affected ) {
error_log( sprintf( 'purge_orphaned_codes failed: %s', $wpdb->last_error ) );
return false;
}
if ( $affected > 0 ) {
do_action( 'ulgm_orphaned_codes_purged', $affected, $code_group_id );
}
return $affected;
}