Warning: This method has been deprecated. LLMS_AJAX_Handler::query_students() is deprecated in favor of the REST API list students endpoint instead.

LLMS_AJAX_Handler::query_students()

Retrieve Students.


Description Description

Used by Select2 AJAX functions to load paginated student results. Also allows querying by: first name last name email.


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class.llms.ajax.handler.php

399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
*
 * @return void
 */
public static function query_students() {
 
    _deprecated_function( __METHOD__, '6.2.0', 'the REST API list students endpoint' );
 
    // Grab the search term if it exists.
    $term = array_key_exists( 'term', $_REQUEST ) ? llms_filter_input_sanitize_string( INPUT_POST, 'term', array( FILTER_FLAG_NO_ENCODE_QUOTES ) ) : '';
 
    $page = array_key_exists( 'page', $_REQUEST ) ? llms_filter_input( INPUT_POST, 'page', FILTER_SANITIZE_NUMBER_INT ) : 0;
 
    $enrolled_in     = array_key_exists( 'enrolled_in', $_REQUEST ) ? sanitize_text_field( wp_unslash( $_REQUEST['enrolled_in'] ) ) : null;
    $not_enrolled_in = array_key_exists( 'not_enrolled_in', $_REQUEST ) ? sanitize_text_field( wp_unslash( $_REQUEST['not_enrolled_in'] ) ) : null;
 
    $roles = array_key_exists( 'roles', $_REQUEST ) ? sanitize_text_field( wp_unslash( $_REQUEST['roles'] ) ) : null;
 
    global $wpdb;
 
    $limit = 30;
    $start = $limit * $page;
 
    $vars = array();
 
    $roles_sql = '';
    if ( $roles ) {
        $roles = explode( ',', $roles );
        $roles = array_map( 'trim', $roles );
        $total = count( $roles );
        foreach ( $roles as $i => $role ) {
            $roles_sql .= "roles.meta_value LIKE '%s'";
            $vars[]     = '%"' . $role . '"%';
            if ( $total > 1 && $i + 1 !== $total ) {
                $roles_sql .= ' OR ';
            }
        }
 
        $roles_sql = "JOIN $wpdb->usermeta AS roles
                        ON $wpdb->users.ID = roles.user_id
                       AND roles.meta_key = '{$wpdb->prefix}capabilities'
                       AND ( $roles_sql )
                    ";
    }
 
    // there was a search query.
    if ( $term ) {
 
        // email only.
        if ( false !== strpos( $term, '@' ) ) {
 
            $query = "SELECT
                          ID AS id
                        , user_email AS email
                        , display_name AS name
                      FROM $wpdb->users
                      $roles_sql
                      WHERE user_email LIKE '%s'
                      ORDER BY display_name
                      LIMIT %d, %d;";
 
            $vars = array_merge(
                $vars,
                array(
                    '%' . $term . '%',
                    $start,
                    $limit,
                )
            );
 
        } elseif ( false !== strpos( $term, ' ' ) ) {
 
            $term = explode( ' ', $term );
 
            $query = "SELECT
                          users.ID AS id
                        , users.user_email AS email
                        , users.display_name AS name
                      FROM $wpdb->users AS users
                      $roles_sql
                      LEFT JOIN wp_usermeta AS fname ON fname.user_id = users.ID
                      LEFT JOIN wp_usermeta AS lname ON lname.user_id = users.ID
                      WHERE ( fname.meta_key = 'first_name' AND fname.meta_value LIKE '%s' )
                        AND ( lname.meta_key = 'last_name' AND lname.meta_value LIKE '%s' )
                      ORDER BY users.display_name
                      LIMIT %d, %d;";
 
            $vars = array_merge(
                $vars,
                array(
                    '%' . $term[0] . '%', // first name.
                    '%' . $term[1] . '%', // last name.
                    $start,
                    $limit,
                )
            );
 
            // search for login, display name, or email.
        } else {
 
            $query = "SELECT
                          ID AS id
                        , user_email AS email
                        , display_name AS name
                      FROM $wpdb->users
                      $roles_sql
                      WHERE
                        user_email LIKE '%s'
                        OR user_login LIKE '%s'
                        OR display_name LIKE '%s'
                      ORDER BY display_name
                      LIMIT %d, %d;";
 
            $vars = array_merge(
                $vars,
                array(
                    '%' . $term . '%',
                    '%' . $term . '%',
                    '%' . $term . '%',
                    $start,
                    $limit,
                )
            );
 
        }
    } else {
 
        $query = "SELECT
                      ID AS id
                    , user_email AS email
                    , display_name AS name
                  FROM $wpdb->users
                  $roles_sql
                  ORDER BY display_name
                  LIMIT %d, %d;";
 
        $vars = array_merge(
            $vars,
            array(
                $start,
                $limit,
            )
        );
 
    }
 
    $res = $wpdb->get_results( $wpdb->prepare( $query, $vars ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
 
    if ( $enrolled_in ) {
 
        $checks = explode( ',', $enrolled_in );
        $checks = array_map( 'trim', $checks );
 
        // Loop through each user.
        foreach ( $res as $key => $user ) {
 
            // Loop through each check -- this is an OR relationship situation.
            foreach ( $checks as $id ) {
 
                // If the user is enrolled break to the next user, they can stay.
                if ( llms_is_user_enrolled( $user->id, $id ) ) {
 
                    continue 2;
 
                }
            }
 
            // If we get here that means the user isn't enrolled in any of the check posts remove them from the results.
            unset( $res[ $key ] );
        }
    }
 
    if ( $not_enrolled_in ) {
 
        $checks = explode( ',', $enrolled_in );
        $checks = array_map( 'trim', $checks );
 
        // Loop through each user.
        foreach ( $res as $key => $user ) {
 
            // Loop through each check -- this is an OR relationship situation.
            // If the user is enrolled in any of the courses they need to be filtered out.
            foreach ( $checks as $id ) {
 
                // If the user is enrolled break remove them and break to the next user.
                if ( llms_is_user_enrolled( $user->id, $id ) ) {
 
                    unset( $res[ $key ] );
                    continue 2;
 
                }
            }
        }
    }
 
    echo json_encode(
        array(
            'items'   => $res,
            'more'    => count( $res ) === $limit,
            'success' => true,
        )
    );


Top ↑

Changelog Changelog

Changelog
Version Description
6.2.0 LLMS_AJAX_Handler::query_students() is deprecated in favor of the REST API list students endpoint.
5.9.0 Stop using deprecated FILTER_SANITIZE_STRING.
5.5.0 Do not encode quotes when sanitizing search term.
3.14.2 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.