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

	 *
	 * @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.