Filter uncanny-toolkit-pro

uo_user_time_total

Filters the total time a user has spent on the site before it's displayed.

add_filter( 'uo_user_time_total', $callback, 10, 2 );

Description

Fires when displaying the total course time for a user via the `[uo_time_total]` shortcode. Developers can filter the returned total time (in seconds) or the user ID before it's displayed. Ensures a valid user ID is always passed.


Usage

add_filter( 'uo_user_time_total', 'your_function_name', 10, 2 );

Parameters

$user_ID (mixed)
This parameter represents the ID of the user whose total course time is being retrieved.
$user_ID (mixed)
This parameter holds the ID of the user for whom the total course time is being calculated.

Return Value

The filtered value.


Examples

/**
 * Filters the total time for a user to display it in a human-readable format.
 *
 * @param int    $user_time_in_seconds The total time in seconds for the user.
 * @param int    $user_ID            The ID of the user.
 * @return string A human-readable string representing the user's total time.
 */
add_filter( 'uo_user_time_total', function( $user_time_in_seconds, $user_ID ) {
	// Check if the user time is a valid number and greater than 0.
	if ( is_numeric( $user_time_in_seconds ) && $user_time_in_seconds > 0 ) {
		// Convert seconds into hours, minutes, and seconds.
		$hours   = floor( $user_time_in_seconds / 3600 );
		$minutes = floor( ( $user_time_in_seconds % 3600 ) / 60 );
		$seconds = $user_time_in_seconds % 60;

		// Build the human-readable string.
		$time_string_parts = array();
		if ( $hours > 0 ) {
			$time_string_parts[] = sprintf( _n( '%d hour', '%d hours', $hours, 'your-text-domain' ), $hours );
		}
		if ( $minutes > 0 ) {
			$time_string_parts[] = sprintf( _n( '%d minute', '%d minutes', $minutes, 'your-text-domain' ), $minutes );
		}
		if ( $seconds > 0 || empty( $time_string_parts ) ) { // Show seconds if there are any, or if time is 0 to show "0 seconds"
			$time_string_parts[] = sprintf( _n( '%d second', '%d seconds', $seconds, 'your-text-domain' ), $seconds );
		}

		return implode( ' ', $time_string_parts );
	} else {
		// Return an empty string if no valid time is found.
		return '';
	}
}, 10, 2 ); // Priority 10, accepting 2 arguments ($user_time_in_seconds, $user_ID)

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:2095

public static function shortcode_uo_time_total( $attributes ) {

		$request = shortcode_atts( array(
			'user-id' => '',
		), $attributes );

		if ( '' === $request['user-id'] ) {
			$request['user-id'] = get_current_user_id();
		}

		$user_ID = absint( $request['user-id'] );
		if ( $user_ID > 0 ) {
			return apply_filters( 'uo_user_time_total', self::get_user_time_in_seconds( $user_ID ), $user_ID );
		} else {
			return '';
		}
	}

Scroll to Top