LLMS_Student::get_enrollments( string $post_type = 'course', array $args = array() )
Retrieve IDs of user’s enrollments by post type (and additional criteria)
Parameters Parameters
- $post_type
-
(string) (Optional) name of the post type (course|membership)
Default value: 'course'
- $args
-
(array) (Optional) query arguments @arg int $limit number of courses to return @arg string $orderby table reference and field to order results by @arg string $order result order (DESC, ASC) @arg int $skip number of results to skip for pagination purposes @arg string $status filter results by enrollment status, "any", "enrolled", "cancelled", or "expired"
Default value: array()
Return Return
(array) "results" will contain an array of course ids "more" will contain a boolean determining whether or not more courses are available beyond supplied limit/skip criteria "found" will contain the total possible FOUND_ROWS() for the query
Source Source
File: includes/models/model.llms.student.php
public function get_enrollments( $post_type = 'course', $args = array() ) {
global $wpdb;
$args = wp_parse_args(
$args,
array(
'limit' => 20,
'orderby' => 'upm.updated_date',
'order' => 'DESC',
'skip' => 0,
'status' => 'any', // Any, enrolled, cancelled, expired.
)
);
// Prefix membership.
if ( 'membership' === $post_type ) {
$post_type = 'llms_membership';
}
// Sanitize order & orderby.
$args['orderby'] = preg_replace( '/[^a-zA-Z_.]/', '', $args['orderby'] );
$args['order'] = preg_replace( '/[^a-zA-Z_.]/', '', $args['order'] );
// Allow "short" orderby's to be passed in without a table reference.
switch ( $args['orderby'] ) {
case 'date':
$args['orderby'] = 'upm.updated_date';
break;
case 'order':
$args['orderby'] = 'p.menu_order';
break;
case 'title':
$args['orderby'] = 'p.post_title';
break;
}
// Prepare additional status AND clauses.
if ( 'any' !== $args['status'] ) {
$status = $wpdb->prepare(
"
AND upm.meta_value = %s
AND upm.updated_date = (
SELECT MAX( upm2.updated_date )
FROM {$wpdb->prefix}lifterlms_user_postmeta AS upm2
WHERE upm2.meta_key = '_status'
AND upm2.user_id = %d
AND upm2.post_id = upm.post_id
)",
$args['status'],
$this->get_id()
);
} else {
$status = '';
}
$from_where = "FROM {$wpdb->prefix}lifterlms_user_postmeta AS upm
JOIN {$wpdb->posts} AS p ON p.ID = upm.post_id
WHERE p.post_type = %s
AND p.post_status = 'publish'
AND upm.meta_key = '_status'
AND upm.user_id = %d
{$status}";
$prepare_args = array( $post_type, $this->get_id() );
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$found = absint(
$wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(DISTINCT upm.post_id) {$from_where}",
$prepare_args
)
)
); // db call ok; no-cache ok.
$query = $wpdb->get_results(
$wpdb->prepare(
"SELECT DISTINCT upm.post_id AS id
{$from_where}
ORDER BY {$args['orderby']} {$args['order']}
LIMIT %d, %d;
",
array_merge(
$prepare_args,
array( $args['skip'], $args['limit'] )
)
),
'OBJECT_K'
); // db call ok; no-cache ok.
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
return array(
'found' => $found,
'limit' => $args['limit'],
'more' => ( $found > ( ( $args['skip'] / $args['limit'] + 1 ) * $args['limit'] ) ),
'skip' => $args['skip'],
'results' => array_keys( $query ),
);
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |