uo_course_timer_add_timer
Fires after a course timer is added to provide access to course, post, and timer interval data.
add_action( 'uo_course_timer_add_timer', $callback, 10, 3 );
Description
Fires when a new course timer is added. Developers can use this hook to perform custom actions, such as logging timer creations, triggering notifications, or modifying timer settings before it's saved. It provides the course ID, post ID, and timer interval for context.
Usage
add_action( 'uo_course_timer_add_timer', 'your_function_name', 10, 3 );
Parameters
-
$course_ID(mixed) - This parameter contains the unique identifier of the course for which the timer is being added.
-
$post_ID(mixed) - This parameter contains the ID of the course for which the timer is being added or managed.
-
$timer_interval(mixed) - This parameter is the ID of the post associated with the course timer.
Examples
// Hook into the uo_course_timer_add_timer action to log when a timer interval is added.
add_action( 'uo_course_timer_add_timer', 'my_custom_course_timer_log', 10, 3 );
/**
* Logs information when a course timer interval is added.
*
* @param int $course_ID The ID of the course.
* @param int $post_ID The ID of the post within the course.
* @param int $timer_interval The amount of time added to the timer.
*/
function my_custom_course_timer_log( $course_ID, $post_ID, $timer_interval ) {
// Get the current user ID.
$current_user_id = get_current_user_id();
// Construct a unique meta key for the user's timer for this course and post.
$meta_key = 'uo_timer_' . $course_ID . '_' . $post_ID;
// Retrieve the current timer value for the user.
$current_timer_value = get_user_meta( $current_user_id, $meta_key, true );
// If the timer value is empty, initialize it to 0 before adding the interval.
if ( '' === $current_timer_value ) {
$current_timer_value = 0;
}
// Calculate the new timer value.
$new_timer_value = $current_timer_value + $timer_interval;
// Log the action to the WordPress debug log for development purposes.
// In a production environment, you might consider a more sophisticated logging mechanism or disable this.
if ( WP_DEBUG === true ) {
error_log( sprintf(
'User %d had timer updated for Course ID %d and Post ID %d. Interval added: %d seconds. New total time: %d seconds.',
$current_user_id,
$course_ID,
$post_ID,
$timer_interval,
$new_timer_value
) );
}
// You could also perform other actions here, such as:
// - Sending a notification to the user.
// - Updating a custom database table with timer events.
// - Triggering other related WordPress actions.
}
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/course-timer.php:1263
public static function add_timer( $data ) {
$return_object = array();
$current_user_id = get_current_user_id();
// validate inputs
$course_ID = absint( $data['course_ID'] );
$post_ID = absint( $data['post_ID'] );
// if any of the values are 0 then they didn't validate, storage is not possible
if ( 0 === $course_ID || 0 === $post_ID ) {
$return_object['success'] = false;
$return_object['message'] = esc_attr__( 'One or more the the fields did not validate as a absolute integer', 'uncanny-pro-toolkit' );
$return_object['fields']['course_ID'] = $data['course_ID'];
$return_object['fields']['post_ID'] = $data['post_ID'];
return $return_object;
}
if( true !== self::is_course_timer_enabled( $course_ID ) ) {
$return_object['success'] = false;
$return_object['message'] = esc_attr__( 'Disabled', 'uncanny-pro-toolkit' );
$return_object['fields']['course_ID'] = $data['course_ID'];
$return_object['fields']['post_ID'] = $data['post_ID'];
return $return_object;
}
$meta_key = 'uo_timer_' . $course_ID . '_' . $post_ID;
$timer = get_user_meta( $current_user_id, $meta_key, true );
// Set timer default
if ( '' === $timer ) {
$timer = 0;
}
// Amount of time added every call // JS interval must match
$timer_interval = self::get_settings_value( 'uo_timer_interval', __CLASS__ );
// Set timer default, minimum is 20 seconds
if ( '' === $timer_interval || (int) $timer_interval < 5 ) {
$timer_interval = 20;
}
$timer += $timer_interval;
update_user_meta( $current_user_id, $meta_key, $timer );
do_action( 'uo_course_timer_add_timer', $course_ID, $post_ID, $timer_interval );
$return_object['success'] = true;
$return_object['time'] = $timer;
global $uo_time_pre;
$time_post = microtime( true );
$exec_time = $time_post - $uo_time_pre;
$return_object['exec_time'] = $exec_time;
return $return_object;
}