Skip to content

Commit 4adbad7

Browse files
added snippets (#5)
1 parent 185b587 commit 4adbad7

4 files changed

+114
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ Please include the following header format when submitting a snippet
2727
```
2828
/**
2929
* Describe what the snippet does in one sentence. (i.e. Add a checkbox to the checkout page.)
30-
*
30+
*
3131
* Learn more at: https://lifterlms.com/link-to-content-if-available-or-remove-this-line/
32-
*
32+
*
3333
* You can add this recipe to your site by creating a custom plugin
3434
* or using the Code Snippets plugin available for free in the WordPress repository.
3535
* Read this companion documentation for step-by-step directions on either method.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Add custom columns and output data on the Courses > Students reporting for course export.
4+
*
5+
* You can add this recipe to your site by creating a custom plugin
6+
* or using the Code Snippets plugin available for free in the WordPress repository.
7+
* Read this companion documentation for step-by-step directions on either method.
8+
* https://lifterlms.com/docs/adding-custom-code/
9+
*/
10+
/**
11+
* Output data for a custom column on the student reporting for course export.
12+
*
13+
* @param string $value Default value being output
14+
* @param string $key Name of the custom field being output
15+
* @param obj $student LLMS_Student for the row
16+
* @param string $context Output context "display" or "export"
17+
* @return mixed
18+
*/
19+
function course_students_table_column_data( $value, $key, $data, $context ) {
20+
21+
if ( 'test' === $key ) {
22+
$value = 'Some Test';
23+
}
24+
25+
return $value;
26+
27+
}
28+
add_filter( 'llms_table_get_data_course-students', 'course_students_table_column_data', 10, 4 );
29+
30+
/**
31+
* Add custom columns to the course students reporting table.
32+
*
33+
* @param array $cols Columns.
34+
* @return array $cols Columns.
35+
*/
36+
function course_students_table_columns( $cols ) {
37+
38+
$cols['test'] = array(
39+
'title' => __( 'Test Test', 'lifterlms' ),
40+
'export_only' => false, // set to false to show display screens but include in exports
41+
'exportable' => true, // set to false to exclude from exports
42+
);
43+
44+
return $cols;
45+
}
46+
add_filter( 'llms_table_get_course-students_columns', 'course_students_table_columns', 10 );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Get all the course IDs ordered by enrolled students in descending order.
4+
*
5+
* You can add this recipe to your site by creating a custom plugin
6+
* or using the Code Snippets plugin available for free in the WordPress repository.
7+
* Read this companion documentation for step-by-step directions on either method.
8+
* https://lifterlms.com/docs/adding-custom-code/
9+
*/
10+
function llms_get_courses_ids_order_by_enrollments() {
11+
// Get all the course IDs.
12+
$course_ids = get_posts(
13+
array(
14+
'post_type' => 'course',
15+
'posts_per_page' => -1,
16+
'fields' => 'ids',
17+
)
18+
);
19+
20+
// Get the enrolled students count for each course.
21+
$enrollment_counts = array();
22+
foreach ( $course_ids as $course_id ) {
23+
$course = new LLMS_Course( $course_id );
24+
$enrollment_counts[ $course_id ] = $course->get_student_count();
25+
}
26+
27+
// Sort the array in descending order.
28+
arsort( $enrollment_counts );
29+
30+
return $enrollment_counts;
31+
}
32+
$desc_sorted_course_ids = llms_get_courses_ids_order_by_enrollments();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Hiding paid access plans for students enrolled in particular memberships.
4+
*
5+
* You can add this recipe to your site by creating a custom plugin
6+
* or using the Code Snippets plugin available for free in the WordPress repository.
7+
* Read this companion documentation for step-by-step directions on either method.
8+
* https://lifterlms.com/docs/adding-custom-code/
9+
*/
10+
function llms_product_is_purchasable( $purchasable, $product ) {
11+
12+
$user_id = get_current_user_id();
13+
$free_access_plans = $product->get_access_plans( true );
14+
15+
// Get membership restrictions.
16+
$membership_restrictions = array();
17+
foreach ( $free_access_plans as $plan ) {
18+
$membership_restrictions[] = $plan->get( 'availability_restrictions' );
19+
}
20+
21+
// Merging the array.
22+
$membership_restrictions = array_merge( ...$membership_restrictions );
23+
24+
// Check if a user is enrolled in the memberships.
25+
foreach ( $membership_restrictions as $membership_id ) {
26+
if ( llms_is_user_enrolled( $user_id, $membership_id ) ) {
27+
$purchasable = false;
28+
}
29+
}
30+
31+
return $purchasable;
32+
}
33+
34+
add_filter( 'llms_product_is_purchasable', 'llms_product_is_purchasable', 10, 2 );

0 commit comments

Comments
 (0)