| 1 |
<?php |
| 2 |
|
| 3 |
use LearnPress\Helpers\Template; |
| 4 |
use LearnPress\Models\UserModel; |
| 5 |
use LearnPress\TemplateHooks\Instructor\ListInstructorsTemplate; |
| 6 |
|
| 7 |
/** |
| 8 |
* REST API LP Instructor. |
| 9 |
* |
| 10 |
* @class LP_REST_Instructor_Controller |
| 11 |
* @author thimpress |
| 12 |
* @version 1.0.0 |
| 13 |
*/ |
| 14 |
class LP_REST_Instructor_Controller extends LP_Abstract_REST_Controller { |
| 15 |
public function __construct() { |
| 16 |
$this->namespace = 'lp/v1'; |
| 17 |
$this->rest_base = 'instructors'; |
| 18 |
|
| 19 |
parent::__construct(); |
| 20 |
} |
| 21 |
|
| 22 |
public function register_routes() { |
| 23 |
$this->routes = array( |
| 24 |
array( |
| 25 |
'methods' => WP_REST_Server::READABLE, |
| 26 |
'callback' => array( $this, 'list_instructors' ), |
| 27 |
'args' => array( |
| 28 |
'posts_per_page' => array( |
| 29 |
'required' => false, |
| 30 |
'type' => 'integer', |
| 31 |
'description' => 'The posts per page must be an integer', |
| 32 |
), |
| 33 |
'page' => array( |
| 34 |
'required' => false, |
| 35 |
'type' => 'integer', |
| 36 |
'description' => 'The page must be an integer', |
| 37 |
), |
| 38 |
), |
| 39 |
'permission_callback' => '__return_true', |
| 40 |
), |
| 41 |
); |
| 42 |
|
| 43 |
parent::register_routes(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get list instructor attend |
| 48 |
* |
| 49 |
* @param WP_REST_Request $request |
| 50 |
* |
| 51 |
* @return LP_REST_Response |
| 52 |
*/ |
| 53 |
public function list_instructors( WP_REST_Request $request ): LP_REST_Response { |
| 54 |
$response = new LP_REST_Response(); |
| 55 |
|
| 56 |
try { |
| 57 |
$params = $request->get_params(); |
| 58 |
$roles_query = [ LP_TEACHER_ROLE ]; |
| 59 |
if ( 'yes' === LP_Settings::get_option( 'show_admin_on_list_instructors', 'yes' ) ) { |
| 60 |
$roles_query[] = ADMIN_ROLE; |
| 61 |
} |
| 62 |
$args = apply_filters( |
| 63 |
'learnpress/instructor-list/args', |
| 64 |
array( |
| 65 |
'fields' => [ 'ID' ], |
| 66 |
'number' => LP_Settings::get_option( 'instructor_per_page', 12 ), |
| 67 |
'paged' => $params['paged'] ?? 1, |
| 68 |
'orderby' => $params['orderby'] ?? 'display_name', |
| 69 |
'order' => $params['order'] ?? 'asc', |
| 70 |
'role__in' => $roles_query, |
| 71 |
) |
| 72 |
); |
| 73 |
|
| 74 |
$query = new WP_User_Query( $args ); |
| 75 |
|
| 76 |
$instructors = $query->get_results(); |
| 77 |
//Content |
| 78 |
ob_start(); |
| 79 |
if ( empty( $instructors ) ) { |
| 80 |
echo '<li class="no-instructor">' . __( 'No instructor found!', 'learnpress' ) . '</li>'; |
| 81 |
} else { |
| 82 |
/** |
| 83 |
* @var LP_User $instructor |
| 84 |
*/ |
| 85 |
$instructors_template = ListInstructorsTemplate::instance(); |
| 86 |
foreach ( $instructors as $instructor_obj ) { |
| 87 |
$instructor = UserModel::find( $instructor_obj->ID, true ); |
| 88 |
echo $instructors_template->instructor_item( $instructor ); |
| 89 |
} |
| 90 |
|
| 91 |
$instructor_total = $query->get_total(); |
| 92 |
|
| 93 |
// Paginate |
| 94 |
$response->data->pagination = $instructors_template->instructors_pagination( |
| 95 |
$args['paged'], |
| 96 |
$args['number'], |
| 97 |
$instructor_total |
| 98 |
); |
| 99 |
} |
| 100 |
$response->data->content = ob_get_clean(); |
| 101 |
|
| 102 |
$response->status = 'success'; |
| 103 |
} catch ( Throwable $e ) { |
| 104 |
ob_end_clean(); |
| 105 |
$response->status = 'error'; |
| 106 |
$response->message = $e->getMessage(); |
| 107 |
} |
| 108 |
|
| 109 |
return $response; |
| 110 |
} |
| 111 |
} |
| 112 |
|