ld_lesson_access_from__visible_after
Filters the lesson access visibility based on when it becomes available for a user.
add_filter( 'ld_lesson_access_from__visible_after', $callback, 10, 3 );
Description
This filter allows modification of the lesson access originating from, for example, course enrollment or group enrollment, before it's determined if the lesson is visible. Developers can adjust the access origin based on custom logic before the lesson's visibility is calculated.
Usage
add_filter( 'ld_lesson_access_from__visible_after', 'your_function_name', 10, 3 );
Parameters
-
$lesson_access_from(mixed) - This parameter represents the lesson access restriction data, which could be null or a specific value indicating the access source.
-
$lesson_id(mixed) - This parameter determines the visibility of the lesson based on access rules, potentially from a course or other access control mechanisms.
-
$user_id(mixed) - This parameter contains the ID of the lesson for which access is being checked.
Return Value
The filtered value.
Examples
/**
* Filter the lesson access timestamp to add a delay based on group enrollment.
*
* This function modifies the lesson access timestamp, making the lesson
* available a certain number of days after the user gains access to the course,
* potentially influenced by group enrollment.
*
* @param mixed $lesson_access_from The original lesson access timestamp.
* @param int $lesson_id The ID of the lesson.
* @param int $user_id The ID of the user.
* @return mixed The modified lesson access timestamp, or null if access is not yet granted.
*/
function my_custom_lesson_access_delay( $lesson_access_from, $lesson_id, $user_id ) {
// Ensure we have valid lesson and user IDs.
if ( ! $lesson_id || ! $user_id ) {
return $lesson_access_from;
}
// Get the course ID associated with the lesson.
$course_id = learndash_get_course_id( $lesson_id );
if ( ! $course_id ) {
return $lesson_access_from;
}
// Get the 'visible_after' setting for the lesson. This is the number of days after course access.
$visible_after_days = learndash_get_setting( $lesson_id, 'visible_after' );
// If 'visible_after' is set and is a positive number.
if ( $visible_after_days && absint( $visible_after_days ) > 0 ) {
// Calculate the timestamp when the user gained access to the course.
// This might come from direct enrollment, group enrollment, or other sources.
// Here we're assuming $lesson_access_from might already contain a course access timestamp if set directly.
// If not, we'd need to fetch the actual course access timestamp for the user.
// For this example, we'll simulate by using the current time if $lesson_access_from is not set,
// or if we want to ensure the delay is applied regardless of the original $lesson_access_from value.
$course_access_timestamp = time(); // Default to current time if no prior access recorded in $lesson_access_from.
if ( ! empty( $lesson_access_from ) && is_numeric( $lesson_access_from ) ) {
// If $lesson_access_from already represents a timestamp, we can use it as the base.
// However, for clarity and to ensure our 'visible_after' logic applies,
// we might want to recalculate based on actual course access.
// Let's assume for this example that the original $lesson_access_from, if numeric, is the course access time.
$course_access_timestamp = $lesson_access_from;
} else {
// If $lesson_access_from is null or not numeric, we might need to get the actual course enrollment/access time.
// This is a simplified example; in a real scenario, you might query for course completion, enrollment date, etc.
// For demonstration, we'll use the current time as the base for the delay calculation.
$course_access_timestamp = time();
}
// Calculate the delay in seconds.
$delay_seconds = absint( $visible_after_days ) * DAY_IN_SECONDS;
// The lesson should be visible after the course access timestamp plus the delay.
$new_lesson_access_from = $course_access_timestamp + $delay_seconds;
// Apply the filter to allow other plugins to modify this calculated timestamp.
// The original hook name is 'ld_lesson_access_from__visible_after'.
return apply_filters( 'ld_lesson_access_from__visible_after', $new_lesson_access_from, $lesson_id, $user_id );
}
// If 'visible_after' is not set or not a positive number, return the original value.
return $lesson_access_from;
}
add_filter( 'ld_lesson_access_from__visible_after', 'my_custom_lesson_access_delay', 10, 3 );
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-lessons-by-group.php:716
public static function ld_lesson_access_from_inherited_from_ld( $lesson_id, $user_id, $course_id = null ) {
$return = null;
if ( is_null( $course_id ) ) {
$course_id = learndash_get_course_id( $lesson_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( $lesson_id, 'visible_after' );
if ( $visible_after > 0 ) {
// Adjust the Course acces from by the number of days. Use abs() to ensure no negative days.
$lesson_access_from = $courses_access_from + abs( $visible_after ) * 24 * 60 * 60;
$lesson_access_from = apply_filters( 'ld_lesson_access_from__visible_after', $lesson_access_from, $lesson_id, $user_id );
$current_timestamp = time();
if ( $current_timestamp < $lesson_access_from ) {
$return = $lesson_access_from;
}
} else {
$visible_after_specific_date = learndash_get_setting( $lesson_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_lesson_access_from__visible_after_specific_date', $visible_after_specific_date, $lesson_id, $user_id );
}
}
}
return $return;
}