ulc_codes_group_modified
Fires after a codes group has been modified in the WordPress admin.
add_action( 'ulc_codes_group_modified', $callback, 10, 2 );
Description
Fires after a codes group has been modified in the admin area. Developers can use this hook to perform custom actions or updates based on the modified group's ID and the data used for the update, such as updating related settings or triggering notifications.
Usage
add_action( 'ulc_codes_group_modified', 'your_function_name', 10, 2 );
Parameters
-
$group_id(mixed) - The `$group_id` parameter represents the unique identifier of the codes group that has been modified.
-
$update_data(mixed) - The `$group_id` parameter contains the unique identifier of the code group that has been modified.
Examples
add_action( 'ulc_codes_group_modified', 'my_ulc_handle_group_modification', 10, 2 );
/**
* Example callback function for the ulc_codes_group_modified action hook.
* This function logs the group ID and the data used to update it to the WordPress debug log.
*
* @param int $group_id The ID of the modified codes group.
* @param array $update_data An associative array containing the data used for the update.
*/
function my_ulc_handle_group_modification( $group_id, $update_data ) {
if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
return; // Only log if WP_DEBUG is enabled.
}
// Sanitize and prepare data for logging.
$log_message = sprintf(
'User modified codes group with ID: %1$d. Update data: %2$s',
absint( $group_id ),
print_r( array_map( 'sanitize_text_field', $update_data ), true ) // Sanitize array values before logging.
);
// Use WordPress's built-in error logging function.
error_log( $log_message );
}
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/admin/class-generate-codes.php:275
public function process_form_edit() {
$group_id = absint( SharedFunctionality::ulc_filter_input( 'group_id', INPUT_POST ) );
$dependency = sanitize_text_field( SharedFunctionality::ulc_filter_input( 'dependency', INPUT_POST ) ); // learndash, automator.
//LearnDash specific
$ld_type = 'default';
if ( SharedFunctionality::ulc_filter_has_var( 'learndash-content', INPUT_POST ) ) {
if ( 'learndash-courses' === SharedFunctionality::ulc_filter_input( 'learndash-content', INPUT_POST ) ) {
$ld_type = 'course';
} elseif ( 'learndash-groups' === SharedFunctionality::ulc_filter_input( 'learndash-content', INPUT_POST ) ) {
$ld_type = 'group';
}
}
$coupon_for = 'automator' === $dependency ? $dependency : $ld_type;
$coupon_courses = ! empty( SharedFunctionality::ulc_filter_input_array( 'learndash-courses', INPUT_POST ) ) ? (array) SharedFunctionality::ulc_filter_input_array( 'learndash-courses', INPUT_POST ) : array();
$coupon_group = ! empty( SharedFunctionality::ulc_filter_input_array( 'learndash-groups', INPUT_POST ) ) ? (array) SharedFunctionality::ulc_filter_input_array( 'learndash-groups', INPUT_POST ) : array();
$coupon_paid_unpaid = SharedFunctionality::ulc_filter_has_var( 'learndash-code-type', INPUT_POST ) ? SharedFunctionality::ulc_filter_input( 'learndash-code-type', INPUT_POST ) : 'default'; // paid, unpaid, default.
$coupon_max_usage = sanitize_text_field( SharedFunctionality::ulc_filter_input( 'coupon-max-usage', INPUT_POST ) );
$expiry_date = sanitize_text_field( SharedFunctionality::ulc_filter_input( 'expiry-date', INPUT_POST ) );
$expiry_time = sanitize_text_field( SharedFunctionality::ulc_filter_input( 'expiry-time', INPUT_POST ) );
$expiry = '0000-00-00 00:00:00';
if ( ! empty( $expiry_date ) ) {
if ( ! empty( $expiry_time ) ) {
$expiry = date_i18n( 'Y-m-d H:i:s', strtotime( $expiry_date . ' ' . $expiry_time ) );
} else {
$expiry = date_i18n( 'Y-m-d 23:59:59', strtotime( $expiry_date ) );
}
}
if ( 'course' === $coupon_for ) {
$linked_to = (array) $coupon_courses;
} elseif ( 'group' === $coupon_for ) {
$linked_to = (array) $coupon_group;
} else {
$linked_to = array();
}
global $wpdb;
if ( 'automator' === (string) $dependency ) {
// If the batch is linked with a Woo Product, do not allow max usage to be changed.
$product_id = SharedFunctionality::get_products_by_batch_id( $group_id );
if ( ! empty( $product_id ) || 0 !== absint( $product_id ) ) {
$coupon_max_usage = 1;
}
}
$update_data = array(
'code_for' => $coupon_for,
'paid_unpaid' => 'automator' === $dependency ? '' : $coupon_paid_unpaid,
'expire_date' => $expiry,
'linked_to' => maybe_serialize( $linked_to ),
'issue_max_count' => $coupon_max_usage,
);
$update_sanity = array(
//'%s',
'%s',
'%s',
'%s',
'%s',
'%d',
);
$wpdb->update( $wpdb->prefix . Config::$tbl_groups,
$update_data,
array( 'ID' => $group_id ),
$update_sanity,
array( '%d' ) );
do_action( 'ulc_codes_group_modified', $group_id, $update_data );
wp_safe_redirect( admin_url( 'admin.php?page=uncanny-learndash-codes&message=Codes+Modified' ) );
exit();
}