@@ -35,7 +35,7 @@ public function __construct() {
3535
3636 add_filter ( 'user_has_cap ' , array ( $ this , 'handle_caps ' ), 10 , 3 );
3737 add_filter ( 'editable_roles ' , array ( $ this , 'editable_roles ' ) );
38-
38+ add_filter ( ' rest_user_query ' , array ( $ this , ' filter_rest_user_query ' ), 10 , 2 );
3939 }
4040
4141 /**
@@ -98,7 +98,45 @@ public function editable_roles( $all_roles ) {
9898 }
9999
100100 return $ all_roles ;
101+ }
102+
103+ /**
104+ * Filter the WP_User_Query args to ensure that instructors can only see their students
105+ *
106+ * @since [version]
107+ *
108+ * @param array $args WP_User_Query args.
109+ * @param WP_REST_Request $request Request object.
110+ * @return array
111+ */
112+ public function filter_rest_user_query ( $ args , $ request ) {
113+
114+ $ user = wp_get_current_user ();
115+
116+ if ( ! $ user ) {
117+ return $ args ;
118+ }
119+
120+ if ( ! in_array ( 'instructor ' , $ user ->roles , true ) ) {
121+ return $ args ;
122+ }
123+
124+ $ instructor = llms_get_instructor ( $ user );
101125
126+ if ( ! $ instructor ) {
127+ return $ args ;
128+ }
129+
130+ $ student_query = $ instructor ->get_students ( array ( 'statuses ' => array ( 'enrolled ' ) ) );
131+ $ students = $ student_query ->get_results ();
132+
133+ if ( empty ( $ students ) ) {
134+ $ args ['include ' ] = array ( 0 );
135+ } else {
136+ $ args ['include ' ] = wp_list_pluck ( $ students , 'id ' );
137+ }
138+
139+ return $ args ;
102140 }
103141
104142 /**
@@ -137,7 +175,6 @@ public function edit_others_lms_content( $allcaps, $cap, $args ) {
137175 }
138176
139177 return $ allcaps ;
140-
141178 }
142179
143180 /**
@@ -165,7 +202,6 @@ public static function get_editable_roles() {
165202 );
166203
167204 return $ roles ;
168-
169205 }
170206
171207 /**
@@ -198,10 +234,10 @@ private function handle_cap_view_grades( $allcaps, $args ) {
198234 return $ allcaps ;
199235 }
200236
201- $ requested_cap = $ args [0 ];
202- $ current_user_id = intval ( $ args [1 ] );
237+ $ requested_cap = $ args [0 ];
238+ $ current_user_id = intval ( $ args [1 ] );
203239 $ requested_user_id = intval ( $ args [2 ] );
204- $ post_id = isset ( $ args [3 ] ) ? intval ( $ args [3 ] ) : false ;
240+ $ post_id = isset ( $ args [3 ] ) ? intval ( $ args [3 ] ) : false ;
205241
206242 // Administrators and LMS managers explicitly have the cap so we don't need to perform any further checks.
207243 if ( ! empty ( $ allcaps [ $ requested_cap ] ) ) {
@@ -222,7 +258,6 @@ private function handle_cap_view_grades( $allcaps, $args ) {
222258 }
223259
224260 return $ allcaps ;
225-
226261 }
227262
228263 /**
@@ -295,7 +330,6 @@ public function handle_caps( $allcaps, $cap, $args ) {
295330 }
296331
297332 return $ allcaps ;
298-
299333 }
300334
301335 /**
@@ -308,7 +342,6 @@ public function handle_caps( $allcaps, $cap, $args ) {
308342 public static function is_current_user_instructor () {
309343
310344 return ( current_user_can ( 'lifterlms_instructor ' ) && current_user_can ( 'list_users ' ) && ! current_user_can ( 'manage_lifterlms ' ) );
311-
312345 }
313346
314347 /**
@@ -384,7 +417,6 @@ protected function user_can_manage_user( $user_id, $edit_id ) {
384417 }
385418
386419 return false ;
387-
388420 }
389421
390422 /**
@@ -396,14 +428,11 @@ protected function user_can_manage_user( $user_id, $edit_id ) {
396428 * @param int $requested_user_id WP User ID of the user the action will be performed on.
397429 * @return bool Returns true if the user has the student, false if it doesn't
398430 */
399- protected function instructor_has_student ( $ current_user_id , $ requested_user_id )
400- {
431+ protected function instructor_has_student ( $ current_user_id , $ requested_user_id ) {
401432
402433 $ instructor = llms_get_instructor ( $ current_user_id );
403434 return $ instructor && $ instructor ->has_student ( $ requested_user_id );
404-
405435 }
406-
407436}
408437
409438return new LLMS_User_Permissions ();
0 commit comments