Filter uncanny-toolkit-pro

ld_topic_access_from__visible_after

Filters the topic access condition, determining if a topic is visible after a specific time.

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

Description

Filters the `$topic_access_from` value before it's used to determine if a user can access a topic. Developers can modify this value to implement custom topic access logic based on user or topic IDs. This hook fires late in the access check, allowing for fine-grained control over visibility.


Usage

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

Parameters

$topic_access_from (mixed)
This parameter represents the access restriction type for the topic, potentially inherited from the course.
$topic_id (mixed)
This parameter represents the access restrictions for the topic, inherited from its parent course.
$user_id (mixed)
This parameter provides the unique identifier for the topic being checked for access.

Return Value

The filtered value.


Examples

/**
 * Example of modifying the topic access time based on a specific date setting.
 *
 * This filter hook allows you to adjust when a topic becomes accessible to a user.
 * In this example, we're checking if the topic has a specific date set for visibility
 * and if the current time is before that date. If so, we return that specific date
 * as the access time.
 *
 * @param mixed  $topic_access_from The current topic access from timestamp.
 * @param int    $topic_id          The ID of the topic.
 * @param int    $user_id           The ID of the user.
 * @return mixed The modified topic access from timestamp, or null if access is denied.
 */
add_filter( 'ld_topic_access_from__visible_after', function( $topic_access_from, $topic_id, $user_id ) {

    // If $topic_access_from is already set and valid, we might want to respect that.
    // For this example, we'll prioritize the specific date if it exists and is in the future.
    if ( $topic_access_from !== null && is_numeric( $topic_access_from ) && $topic_access_from > time() ) {
        // Optionally, you could return $topic_access_from here if you want to allow
        // other filters to potentially override the specific date logic.
    }

    // Get the specific date setting for the topic
    $visible_after_specific_date = learndash_get_setting( $topic_id, 'visible_after_specific_date' );

    // Check if the specific date setting exists and is not empty
    if ( ! empty( $visible_after_specific_date ) ) {

        // If the date is not a numeric timestamp, try to convert it
        if ( ! is_numeric( $visible_after_specific_date ) ) {
            // learndash_get_timestamp_from_date_string expects a string and converts it to GMT.
            // The 'true' argument ensures it uses GMT.
            $visible_after_specific_date = learndash_get_timestamp_from_date_string( $visible_after_specific_date, true );
        }

        // Get the current server time as a Unix timestamp
        $current_time = time();

        // Check if the current time is before the specific date set for visibility
        if ( $current_time < $visible_after_specific_date ) {
            // If the current time is before the specific date, return that date as the access time.
            // This effectively makes the topic inaccessible until that date.
            // We also apply another filter for this specific scenario if needed by other plugins.
            return apply_filters( 'ld_topic_access_from__visible_after_specific_date_override', $visible_after_specific_date, $topic_id, $user_id );
        }
    }

    // If no specific date is set or the specific date has already passed,
    // return the original $topic_access_from value (which might be null or a timestamp).
    // This allows the default Learndash behavior or other filters to handle access.
    return $topic_access_from;

}, 10, 3 ); // Priority 10, accepts 3 arguments: $topic_access_from, $topic_id, $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/uncanny-drip-topics-by-group.php:720

public static function ld_topic_access_from_inherited_from_ld( $topic_id, $user_id, $course_id = null ) {
		$return = null;

		if ( is_null( $course_id ) ) {
			$course_id = learndash_get_course_id( $topic_id );
		}

		$courses_access_from = ld_course_access_from( $course_id, $user_id );
		if ( empty( $courses_access_from ) ) {
			$courses_access_from = learndash_user_group_enrolled_to_course_from( $user_id, $course_id );
		}

		$visible_after = learndash_get_setting( $topic_id, 'visible_after' );
		if ( $visible_after > 0 ) {

			// Adjust the Course acces from by the number of days. Use abs() to ensure no negative days.
			$topic_access_from = $courses_access_from + abs( $visible_after ) * 24 * 60 * 60;
			$topic_access_from = apply_filters( 'ld_topic_access_from__visible_after', $topic_access_from, $topic_id, $user_id );

			$current_timestamp = time();
			if ( $current_timestamp < $topic_access_from ) {
				$return = $topic_access_from;
			}

		} else {
			$visible_after_specific_date = learndash_get_setting( $topic_id, 'visible_after_specific_date' );
			if ( ! empty( $visible_after_specific_date ) ) {
				if ( ! is_numeric( $visible_after_specific_date ) ) {
					// If we a non-numberic value like a date stamp Y-m-d hh:mm:ss we want to convert it to a GMT timestamp
					$visible_after_specific_date = learndash_get_timestamp_from_date_string( $visible_after_specific_date, true );
				}

				$current_time = time();

				if ( $current_time < $visible_after_specific_date ) {
					$return = apply_filters( 'ld_topic_access_from__visible_after_specific_date', $visible_after_specific_date, $topic_id, $user_id );
				}
			}
		}

		return $return;
	}


Scroll to Top