Created
November 28, 2023 21:06
-
-
Save Lonsdale201/3f1fa827344b87e987df104237b43dbf to your computer and use it in GitHub Desktop.
Custom Macro - Learndash - Current user courses with selectable progress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// place the code in the child theme functions.php or a custom code snippets plugin. | |
// how to use | |
// Create a new Post Query in the Query builder. Select the post type: Course | |
// Go to the Meta Query section | |
// click the Add new button | |
// in the VALUE field choose the Dynamic tag icon, and select LD Current User Courses and status | |
// Choose what type of progress you want to show | |
// In the operator field select the "in the list" option | |
// HELP IMAGE : https://prnt.sc/sIGKdV44yi51 | |
// DONE | |
add_action( 'jet-engine/register-macros', function() { | |
class LD_Current_User_Courses_and_Status extends \Jet_Engine_Base_Macros { | |
public function macros_tag() { | |
return 'ld_current_user_courses_and_status'; | |
} | |
public function macros_name() { | |
return 'LD Current User Courses and Status'; | |
} | |
public function macros_args() { | |
return array( | |
'course_status' => array( | |
'label' => 'Course Status', | |
'type' => 'select', | |
'options' => array( | |
'all' => 'All', | |
'not_started'=> 'Not Started', | |
'in_progress'=> 'In Progress', | |
'completed' => 'Completed' | |
), | |
'default' => 'all' | |
), | |
); | |
} | |
public function macros_callback( $args = array() ) { | |
$user_id = get_current_user_id(); | |
if ( !$user_id ) { | |
return 'User is not logged in'; | |
} | |
$course_status = !empty( $args['course_status'] ) ? $args['course_status'] : 'all'; | |
$courses = ld_get_mycourses($user_id); | |
if ( empty($courses) ) { | |
return 'User has no course access'; | |
} | |
$filtered_courses = array(); | |
foreach ( $courses as $course_id ) { | |
$progress = learndash_course_progress( array( 'user_id' => $user_id, 'course_id' => $course_id, 'array' => true ) ); | |
if ($course_status == 'completed' && learndash_course_completed($user_id, $course_id)) { | |
$filtered_courses[] = $course_id; | |
} elseif ($course_status == 'in_progress' && !empty($progress) && $progress['completed'] > 0 && $progress['completed'] < 100) { | |
$filtered_courses[] = $course_id; | |
} elseif ($course_status == 'not_started' && (empty($progress) || $progress['completed'] == 0)) { | |
$filtered_courses[] = $course_id; | |
} elseif ($course_status == 'all') { | |
$filtered_courses[] = $course_id; | |
} | |
} | |
if ( empty($filtered_courses) ) { | |
return 'No courses found for selected status'; | |
} | |
return implode( ',', $filtered_courses ); | |
} | |
} | |
new LD_Current_User_Courses_and_Status(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment