Filter uncanny-toolkit-pro

uo_time_quiz

Filters the quiz post data before it's retrieved, allowing modifications based on the user.

add_filter( 'uo_time_quiz', $callback, 10, 3 );

Description

Filters the quiz post object before it's displayed by the `uo_time_quiz` shortcode. Developers can modify the `$quiz_post` data, such as its content or meta information, based on the `$user_ID`. This hook fires after the quiz post is retrieved and before it's rendered.


Usage

add_filter( 'uo_time_quiz', 'your_function_name', 10, 3 );

Parameters

$quiz_post (mixed)
This parameter likely represents the WordPress post object for the quiz.
$quiz_post (mixed)
This parameter contains the WordPress post object for the quiz being displayed.
$user_ID (mixed)
This parameter is used to retrieve the quiz post object, which contains all the details of the quiz.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to hook into the 'uo_time_quiz' filter.
 *
 * This filter allows modifying the calculated time information for a quiz
 * before it's displayed, or to conditionally display different information.
 *
 * For example, we might want to display a warning if a user has very little time left.
 */
add_filter( 'uo_time_quiz', 'my_custom_uo_time_quiz_logic', 10, 3 );

/**
 * Custom logic for the 'uo_time_quiz' filter.
 *
 * @param array $time_data  The calculated time data (likely an array with remaining time, etc.).
 * @param WP_Post $quiz_post The WordPress post object for the quiz.
 * @param int $user_ID The ID of the user taking the quiz.
 * @return array|string The modified time data, or a custom string to display.
 */
function my_custom_uo_time_quiz_logic( $time_data, $quiz_post, $user_ID ) {

	// Let's assume $time_data is an array containing at least 'remaining_seconds'.
	// If the user has less than 5 minutes (300 seconds) remaining, we'll add a warning.
	if ( isset( $time_data['remaining_seconds'] ) && $time_data['remaining_seconds'] < 300 ) {
		$quiz_title = get_the_title( $quiz_post );
		$user_info = get_userdata( $user_ID );
		$user_name = $user_info ? $user_info->display_name : 'User';

		$time_data['warning_message'] = sprintf(
			'<div class="quiz-time-warning" style="color: red; font-weight: bold;">%s, you have very little time left on the "%s" quiz! Please hurry!',
			esc_html( $user_name ),
			esc_html( $quiz_title )
		);
	}

	// If there's no time remaining at all, we might want to display a message
	// indicating the quiz is over and return an empty string to hide any time display.
	if ( isset( $time_data['remaining_seconds'] ) && $time_data['remaining_seconds'] <= 0 ) {
		return '<p>' . __( 'Quiz time has expired.', 'your-text-domain' ) . '</p>';
	}

	// Return the (potentially modified) time data.
	// The original function likely expects an array for display.
	return $time_data;
}
?>

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

public static function shortcode_uo_time_quiz( $attributes ) {

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

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

		$user_ID = absint( $request['user-id'] );
		$post_ID = absint( $request['quiz-id'] );

		$quiz_post = null;

		if ( '' == $request['quiz-id'] ) {
			global $post;

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

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

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

		if ( null !== $quiz_post && $user_ID > 0 ) {
			return apply_filters( 'uo_time_quiz', self::get_uo_time( $quiz_post, $user_ID ), $quiz_post, $user_ID );
		}

		return '';
	}

Scroll to Top