Filter uncanny-toolkit-pro

uo_time_lesson

Filters the lesson time before it's displayed, allowing modification of the raw time, lesson post, and user ID.

add_filter( 'uo_time_lesson', $callback, 10, 4 );

Description

Allows modification of lesson time data for the `uo_time_lesson` shortcode. Filter `$uo_time_lesson` with `$raw_time` and lesson details. Use `$user_ID` and `$lesson_post` for context. Useful for custom lesson time display or data retrieval.


Usage

add_filter( 'uo_time_lesson', 'your_function_name', 10, 4 );

Parameters

$uo_time_lesson (mixed)
This parameter contains the formatted lesson time, which can be modified by filters.
$raw_time (mixed)
This parameter contains the formatted lesson time output by the `uo_time_lesson` filter, allowing for modifications to how the lesson time is displayed.
$lesson_post (mixed)
This parameter holds the raw time value related to the lesson.
$user_ID (mixed)
This parameter contains the WordPress post object for the current lesson.

Return Value

The filtered value.


Examples

/**
 * Example of a filter for the 'uo_time_lesson' hook.
 * This function modifies the lesson time by adding a bonus if the user has completed a specific lesson.
 *
 * @param string $uo_time_lesson The formatted lesson time string.
 * @param int    $raw_time       The total lesson time in seconds.
 * @param WP_Post $lesson_post    The WP_Post object for the lesson.
 * @param int    $user_ID        The ID of the current user.
 * @return string The modified formatted lesson time.
 */
function my_custom_uo_time_lesson_filter( $uo_time_lesson, $raw_time, $lesson_post, $user_ID ) {
    // Check if the current lesson is 'Advanced Module 1' and if the user has completed it.
    // Replace 'advanced-module-1' with the actual lesson slug if needed.
    if ( $lesson_post->post_name === 'advanced-module-1' ) {
        // Assuming you have a function to check lesson completion status.
        // Replace 'my_custom_check_lesson_completion' with your actual function.
        if ( my_custom_check_lesson_completion( $lesson_post->ID, $user_ID ) ) {
            // Add a bonus time of 15 minutes (900 seconds).
            $bonus_time_seconds = 900;
            $raw_time += $bonus_time_seconds;

            // Re-convert the total time to a readable format.
            $uo_time_lesson = self::convert_second_to_time( $raw_time ); // Assuming convert_second_to_time is accessible in this context or defined globally/in another class.

            // You might want to log this change or add a notification.
            error_log( "Bonus time added to lesson {$lesson_post->ID} for user {$user_ID}." );
        }
    }

    return $uo_time_lesson;
}
add_filter( 'uo_time_lesson', 'my_custom_uo_time_lesson_filter', 10, 4 );

// Dummy function for demonstration purposes. Replace with your actual completion checking logic.
function my_custom_check_lesson_completion( $lesson_id, $user_id ) {
    // In a real scenario, you would query the database or use a LearnDash function
    // to determine if the user has completed the lesson.
    // For this example, let's assume a user with ID 5 has completed 'advanced-module-1'.
    if ( $user_id === 5 && $lesson_id === 123 ) { // Replace 123 with the actual ID of 'advanced-module-1'.
        return true;
    }
    return false;
}

// Dummy function for demonstration purposes. Replace with your actual time conversion logic.
// This should ideally be the same function used in the original source.
if ( ! class_exists( 'YourCourseTimerClass' ) ) {
    class YourCourseTimerClass {
        public static function convert_second_to_time( $seconds ) {
            if ( $seconds < 60 ) {
                return sprintf( '%ds', $seconds );
            } elseif ( $seconds < 3600 ) {
                return sprintf( '%dm %ds', floor( $seconds / 60 ), $seconds % 60 );
            } else {
                return sprintf( '%dh %dm %ds', floor( $seconds / 3600 ), floor( ( $seconds % 3600 ) / 60 ), $seconds % 60 );
            }
        }
    }
    // Alias to make the example self-contained if self:: is used.
    if (!function_exists('self')) {
        function self() {} // Placeholder if not in a class context for the dummy
    }
    // If the original convert_second_to_time is a static method of CourseTimer,
    // then the filter function might need to be defined within a class or
    // call the static method appropriately. For simplicity, using a standalone
    // class or assuming global access for the example.
    // In a real plugin, you'd likely extend or hook into the existing class.
    class CourseTimer { // Assuming this is the class from the source context
        public static function convert_second_to_time( $seconds ) {
            if ( $seconds < 60 ) {
                return sprintf( '%ds', $seconds );
            } elseif ( $seconds < 3600 ) {
                return sprintf( '%dm %ds', floor( $seconds / 60 ), $seconds % 60 );
            } else {
                return sprintf( '%dh %dm %ds', floor( $seconds / 3600 ), floor( ( $seconds % 3600 ) / 60 ), $seconds % 60 );
            }
        }
    }
    // Update the filter to use the correct static call if it's within a class context
    // or ensure the function is available globally.
    // For this example, let's assume it's available via CourseTimer::convert_second_to_time
    // and adjust the filter function definition.

    function my_custom_uo_time_lesson_filter_updated( $uo_time_lesson, $raw_time, $lesson_post, $user_ID ) {
        if ( $lesson_post->post_name === 'advanced-module-1' ) {
            if ( my_custom_check_lesson_completion( $lesson_post->ID, $user_ID ) ) {
                $bonus_time_seconds = 900;
                $raw_time += $bonus_time_seconds;
                $uo_time_lesson = CourseTimer::convert_second_to_time( $raw_time );
                error_log( "Bonus time added to lesson {$lesson_post->ID} for user {$user_ID}." );
            }
        }
        return $uo_time_lesson;
    }
    // Remove the old hook and add the updated one if necessary.
    remove_filter( 'uo_time_lesson', 'my_custom_uo_time_lesson_filter', 10, 4 );
    add_filter( 'uo_time_lesson', 'my_custom_uo_time_lesson_filter_updated', 10, 4 );
}

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

public static function shortcode_uo_time_lesson( $attributes ) {

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

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

		$user_ID     = absint( $request['user-id'] );
		$post_ID     = absint( $request['lesson-id'] );
		$raw_time    = '';
		$lesson_post = 0;
		if ( '' == $request['lesson-id'] ) {
			global $post;

			if ( ( ! is_404() && ! is_archive() && is_a( $post, 'WP_Post' ) ) ) {
				if ( 'sfwd-lessons' == $post->post_type ) {
					$lesson_post = $post;
				}
			}

		} elseif ( $post_ID ) {
			$requested_post = get_post( $post_ID );

			if ( is_a( $requested_post, 'WP_Post' ) ) {
				if ( 'sfwd-lessons' == $requested_post->post_type ) {
					$lesson_post = $requested_post;
				}
			}
		}

		if ( is_a( $lesson_post, 'WP_Post' ) && $user_ID > 0 ) {

			$raw_time = (int) self::get_uo_time( $lesson_post, $user_ID, true );

			$course_id = learndash_get_course_id( $lesson_post->ID );

			$quizzes = learndash_get_lesson_quiz_list( $lesson_post, null, $course_id );

			if ( ! empty( $quizzes ) ) {
				foreach ( $quizzes as $quiz ) {
					if ( is_a( $quiz['post'], 'WP_Post' ) ) {
						$quiz_time = self::get_uo_time( $quiz['post'], $user_ID, true );
						if ( '' !== $quiz_time ) {
							$raw_time = $raw_time + $quiz_time;
						}
					}
				}
			}

			$topics = learndash_course_get_topics(
				$course_id,
				$lesson_post->ID,
				array(
					'return_type' => 'WP_Post',
					'per_page'    => 0,
				)
			);

			if ( ! empty( $topics ) ) {
				foreach ( $topics as $topic ) {
					if ( is_a( $topic, 'WP_Post' ) ) {
						$topic_time = self::get_topic_time( $topic, $user_ID );
						if ( '' !== $topic_time ) {
							$raw_time = $raw_time + $topic_time;
						}
					}
				}
			}

			$uo_time_lesson = self::convert_second_to_time( $raw_time );

			return apply_filters( 'uo_time_lesson', $uo_time_lesson, $raw_time, $lesson_post, $user_ID );
		}

		return $raw_time;
	}

Scroll to Top