PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.0
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.0
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / rest-api / v1 / frontend / class-lp-rest-profile-controller.php

class-lp-rest-profile-controller.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.0, at inc/rest-api/v1/frontend/class-lp-rest-profile-controller.php

600 lines 17.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use LearnPress\Helpers\Template;
4 use LearnPress\Models\UserModel;
5
6 class LP_REST_Profile_Controller extends LP_Abstract_REST_Controller {
7 public function __construct() {
8 $this->namespace = 'lp/v1';
9 $this->rest_base = 'profile';
10
11 parent::__construct();
12 }
13
14 public function register_routes() {
15 $this->routes = array(
16 'student/statistic' => array(
17 array(
18 'methods' => WP_REST_Server::READABLE,
19 'callback' => array( $this, 'student_statistics' ),
20 'permission_callback' => array( $this, 'check_permission' ),
21 ),
22 ),
23 'instructor/statistic' => array(
24 array(
25 'methods' => WP_REST_Server::READABLE,
26 'callback' => array( $this, 'instructor_statistics' ),
27 'permission_callback' => '__return_true',
28 ),
29 ),
30 'course-tab' => array(
31 array(
32 'methods' => WP_REST_Server::READABLE,
33 'callback' => array( $this, 'course_tab' ),
34 'permission_callback' => array( $this, 'check_permission' ),
35 ),
36 ),
37 /*'course-attend' => array(
38 array(
39 'methods' => WP_REST_Server::CREATABLE,
40 'callback' => array( $this, 'course_attend' ),
41 'permission_callback' => array( $this, 'check_permission' ),
42 ),
43 ),*/
44 'get-avatar' => array(
45 array(
46 'methods' => WP_REST_Server::READABLE,
47 'callback' => array( $this, 'get_avatar' ),
48 'permission_callback' => function () {
49 return (bool) get_current_user_id();
50 },
51 ),
52 ),
53 'upload-avatar' => array(
54 array(
55 'methods' => WP_REST_Server::CREATABLE,
56 'callback' => array( $this, 'upload_avatar' ),
57 'permission_callback' => function () {
58 return (bool) get_current_user_id();
59 },
60 ),
61 ),
62 'remove-avatar' => array(
63 array(
64 'methods' => WP_REST_Server::CREATABLE,
65 'callback' => array( $this, 'remove_avatar' ),
66 'permission_callback' => function () {
67 return (bool) get_current_user_id();
68 },
69 ),
70 ),
71 'cover-image' => array(
72 array(
73 'methods' => WP_REST_Server::CREATABLE,
74 'callback' => array( $this, 'handle_cover_image' ),
75 'permission_callback' => function () {
76 return get_current_user_id();
77 },
78 ),
79 ),
80 );
81
82 parent::register_routes();
83 }
84
85 /**
86 * Check permission
87 *
88 * @param $request
89 *
90 * @return bool
91 */
92 public function check_permission( $request ): bool {
93 $user_id = $request->get_param( 'userID' );
94
95 if ( empty( $user_id ) ) {
96 return false;
97 }
98
99 /*$profile = learn_press_get_profile( $user_id );
100
101 if ( ! $profile->current_user_can( 'view-tab-my-courses' ) ) {
102 return false;
103 }*/
104
105 return true;
106 }
107
108 public function get_avatar( WP_REST_Request $request ) {
109 $response = new LP_REST_Response();
110
111 $thumb_size = learn_press_get_avatar_thumb_size();
112
113 $profile = LP_Profile::instance( get_current_user_id() );
114 $avatar_url = $profile->get_upload_profile_src();
115
116 $response->data->width = $thumb_size['width'];
117 $response->data->height = $thumb_size['height'];
118 $response->data->url = $avatar_url ? $avatar_url : '';
119
120 return rest_ensure_response( $response );
121 }
122
123 public function upload_avatar( WP_REST_Request $request ) {
124 $file_base64 = $request->get_param( 'file' );
125 $response = new LP_REST_Response();
126
127 try {
128 $user_id = get_current_user_id();
129 if ( ! $user_id ) {
130 throw new Exception( __( 'User not found', 'learnpress' ) );
131 }
132
133 if ( empty( $file_base64 ) ) {
134 throw new Exception( __( 'File not found', 'learnpress' ) );
135 }
136
137 $upload_dir = learn_press_user_profile_picture_upload_dir( true );
138
139 $target_dir = LP_WP_Filesystem::instance()->is_dir( $upload_dir['path'] );
140
141 if ( ! $target_dir ) {
142 wp_mkdir_p( $upload_dir['path'] );
143 }
144
145 if ( ! LP_WP_Filesystem::instance()->is_writable( $upload_dir['path'] ) ) {
146 throw new Exception( __( 'The upload directory is not writable', 'learnpress' ) );
147 }
148
149 // Delete old image if exists
150 $path_img = get_user_meta( $user_id, '_lp_profile_picture', true );
151
152 if ( $path_img ) {
153 $path = $upload_dir['basedir'] . '/' . $path_img;
154
155 if ( file_exists( $path ) ) {
156 LP_WP_Filesystem::instance()->unlink( $path );
157 }
158 }
159
160 $file_name = md5( $user_id . microtime( true ) ) . '.png';
161
162 $file_base64 = str_replace( 'data:image/png;base64,', '', $file_base64 );
163 $file_base64 = base64_decode( $file_base64 );
164
165 $put_content = LP_WP_Filesystem::instance()->put_contents( $upload_dir['path'] . '/' . $file_name, $file_base64 );
166
167 if ( ! $put_content ) {
168 throw new Exception( __( 'Cannot write the file', 'learnpress' ) );
169 }
170
171 update_user_meta( $user_id, '_lp_profile_picture', $upload_dir['subdir'] . '/' . $file_name );
172 do_action( 'learnpress/rest/frontend/profile/upload_avatar', $user_id );
173
174 $response->status = 'success';
175 $response->message = __( 'Avatar updated', 'learnpress' );
176 } catch ( \Throwable $th ) {
177 $response->message = $th->getMessage();
178 }
179
180 return rest_ensure_response( $response );
181 }
182
183 public function remove_avatar( WP_REST_Request $request ) {
184 $response = new LP_REST_Response();
185
186 try {
187 $user_id = get_current_user_id();
188
189 if ( ! $user_id ) {
190 throw new Exception( esc_html__( 'The user is invalid', 'learnpress' ) );
191 }
192
193 $upload_dir = learn_press_user_profile_picture_upload_dir( true );
194
195 $target_dir = LP_WP_Filesystem::instance()->is_dir( $upload_dir['path'] );
196
197 if ( ! $target_dir ) {
198 wp_mkdir_p( $upload_dir['path'] );
199 }
200
201 if ( ! LP_WP_Filesystem::instance()->is_writable( $upload_dir['path'] ) ) {
202 throw new Exception( __( 'The upload directory is not writable', 'learnpress' ) );
203 }
204
205 $path_img = get_user_meta( $user_id, '_lp_profile_picture', true );
206
207 if ( $path_img ) {
208 $path = $upload_dir['basedir'] . '/' . $path_img;
209
210 if ( file_exists( $path ) ) {
211 LP_WP_Filesystem::instance()->unlink( $path );
212
213 $response->status = 'success';
214 $response->message = esc_html__( 'The profile picture has been removed successfully', 'learnpress' );
215 }
216 }
217 } catch ( \Throwable $th ) {
218 $response->message = $th->getMessage();
219 }
220
221 return rest_ensure_response( $response );
222 }
223
224 /**
225 * Statistics of a student.
226 *
227 * @param WP_REST_Request $request
228 *
229 * @return WP_Error|WP_HTTP_Response|WP_REST_Response
230 */
231 public function student_statistics( WP_REST_Request $request ) {
232 $user_id = (int) $request->get_param( 'userID' );
233 $response = new LP_REST_Response();
234 $response->data = '';
235
236 try {
237 if ( empty( $user_id ) ) {
238 throw new Exception( esc_html__( 'No user ID found!', 'learnpress' ) );
239 }
240
241 $userModel = UserModel::find( $user_id, true );
242 if ( ! $userModel ) {
243 throw new Exception( esc_html__( 'The user does not exist!', 'learnpress' ) );
244 }
245
246 // Check permission
247 $profile = LP_Profile::instance( $user_id );
248 if ( ! $profile->current_user_can( 'view-tab-my-courses' ) ) {
249 throw new Exception( esc_html__( 'You do not have permission to view this statistic!', 'learnpress' ) );
250 }
251
252 $statistic = $userModel->get_student_statistic();
253 $data = apply_filters(
254 'learn-press/profile/student-statistics/info',
255 [
256 'enrolled_courses' => [
257 'title' => __( 'Total enrolled courses', 'learnpress' ),
258 'label' => __( 'Enrolled Course', 'learnpress' ),
259 'count' => $statistic['enrolled_courses'] ?? 0,
260 ],
261 'in_progress_course' => [
262 'title' => __( 'Total course is in progress', 'learnpress' ),
263 'label' => __( 'Inprogress Course', 'learnpress' ),
264 'count' => $statistic['in_progress_course'] ?? 0,
265 ],
266 'finished_courses' => [
267 'title' => __( 'Total courses finished', 'learnpress' ),
268 'label' => __( 'Finished Course', 'learnpress' ),
269 'count' => $statistic['finished_courses'] ?? 0,
270 ],
271 'passed_courses' => [
272 'title' => __( 'Total courses passed', 'learnpress' ),
273 'label' => __( 'Passed Course', 'learnpress' ),
274 'count' => $statistic['passed_courses'] ?? 0,
275 ],
276 'failed_courses' => [
277 'title' => __( 'Total courses failed', 'learnpress' ),
278 'label' => __( 'Failed Course', 'learnpress' ),
279 'count' => $statistic['failed_courses'] ?? 0,
280 ],
281 ]
282 );
283
284 ob_start();
285 Template::instance()->get_frontend_template(
286 'profile/tabs/statistics/student-statistics.php',
287 compact( 'data' )
288 );
289 $response->data = ob_get_clean();
290 $response->status = 'success';
291 } catch ( Exception $e ) {
292 ob_end_clean();
293 $response->message = $e->getMessage();
294 }
295
296 return rest_ensure_response( $response );
297 }
298
299 /**
300 * Statistics of an instructor.
301 *
302 * @param WP_REST_Request $request
303 *
304 * @return WP_Error|WP_HTTP_Response|WP_REST_Response
305 */
306 public function instructor_statistics( WP_REST_Request $request ) {
307 $user_id = $request->get_param( 'userID' );
308 $response = new LP_REST_Response();
309 $response->data = '';
310
311 try {
312 if ( empty( $user_id ) ) {
313 throw new Exception( esc_html__( 'No user ID found!', 'learnpress' ) );
314 }
315
316 $userModel = UserModel::find( $user_id, true );
317 if ( ! $userModel ) {
318 throw new Exception( esc_html__( 'The user does not exist!', 'learnpress' ) );
319 }
320
321 $profile = LP_Profile::instance( $user_id );
322 if ( $profile instanceof WP_Error ) {
323 throw new Exception( $profile->get_error_message() );
324 }
325
326 // Check permission
327 if ( ! $profile->current_user_can( 'view-tab-courses' ) ) {
328 throw new Exception( esc_html__( 'You do not have permission to view this statistic!', 'learnpress' ) );
329 }
330
331 $statistic = $userModel->get_instructor_statistic();
332
333 $data = apply_filters(
334 'learn-press/profile/instructor-statistics/info',
335 [
336 'total_course' => [
337 'title' => __( 'Total Course', 'learnpress' ),
338 'label' => __( 'Total Course', 'learnpress' ),
339 'count' => $statistic['total_course'] ?? 0,
340 ],
341 'published_course' => [
342 'title' => __( 'Published Course', 'learnpress' ),
343 'label' => __( 'Published Course', 'learnpress' ),
344 'count' => $statistic['published_course'] ?? 0,
345 ],
346 'pending_course' => [
347 'title' => __( 'Pending Course', 'learnpress' ),
348 'label' => __( 'Pending Course', 'learnpress' ),
349 'count' => $statistic['pending_course'] ?? 0,
350 ],
351 'total_student' => [
352 'title' => __( 'Total Student', 'learnpress' ),
353 'label' => __( 'Total Student', 'learnpress' ),
354 'count' => $statistic['total_student'] ?? 0,
355 ],
356 'student_completed' => [
357 'title' => __( 'Student Completed', 'learnpress' ),
358 'label' => __( 'Student Completed', 'learnpress' ),
359 'count' => $statistic['student_completed'] ?? 0,
360 ],
361 'student_in_progress' => [
362 'title' => __( 'Student In-progress', 'learnpress' ),
363 'label' => __( 'Student In-progress', 'learnpress' ),
364 'count' => $statistic['student_in_progress'] ?? 0,
365 ],
366 ]
367 );
368
369 ob_start();
370 Template::instance()->get_frontend_template(
371 'profile/tabs/statistics/instructor-statistics.php',
372 compact( 'data' )
373 );
374 $response->data = ob_get_clean();
375 $response->status = 'success';
376 } catch ( Exception $e ) {
377 ob_end_clean();
378 $response->message = $e->getMessage();
379 }
380
381 return rest_ensure_response( $response );
382 }
383
384 public function course_tab( $request ) {
385 $params = $request->get_params();
386 $user_id = $params['userID'] ?? get_current_user_id();
387 $status = $params['status'] ?? '';
388 $paged = $params['paged'] ?? 1;
389 $query_type = $params['query'] ?? '';
390 $layout = $params['layout'] ?? 'grid';
391 $response = new LP_REST_Response();
392
393 try {
394 if ( empty( $user_id ) ) {
395 throw new Exception( esc_html__( 'No user ID found!', 'learnpress' ) );
396 }
397
398 $profile = LP_Profile::instance( $user_id );
399 if ( 'purchased' === $query_type ) {
400 if ( ! $profile->current_user_can( 'view-tab-my-courses' ) ) {
401 throw new Exception( esc_html__( 'You do not have permission to view this tab!', 'learnpress' ) );
402 }
403 } elseif ( 'own' === $query_type ) {
404 if ( ! $profile->current_user_can( 'view-tab-courses' ) ) {
405 throw new Exception( esc_html__( 'You do not have permission to view this tab!', 'learnpress' ) );
406 }
407 } else {
408 throw new Exception( esc_html__( 'Request invalid!', 'learnpress' ) );
409 }
410
411 $query = $profile->query_courses(
412 $query_type,
413 apply_filters(
414 'learnpress/rest/frontend/profile/course_tab/query',
415 array(
416 'status' => $status,
417 'limit' => LP_Settings::get_option( 'archive_course_limit', 6 ),
418 'paged' => $paged,
419 ),
420 $request
421 )
422 );
423
424 // LP_User_Item_Course.
425 $course_item_objects = ! empty( $query->get_items() ) ? $query->get_items() : false;
426
427 if ( empty( $course_item_objects ) ) {
428 throw new Exception( esc_html__( 'No Course available!', 'learnpress' ) );
429 }
430
431 $course_ids = array_map(
432 function ( $course_object ) {
433 return ! is_object( $course_object ) ? absint( $course_object ) : $course_object->get_id();
434 },
435 $course_item_objects
436 );
437
438 if ( empty( $course_ids ) ) {
439 throw new Exception( esc_html__( 'No Course IDs available!', 'learnpress' ) );
440 }
441
442 $user = learn_press_get_user( $user_id );
443
444 if ( empty( $user ) ) {
445 throw new Exception( esc_html__( 'No User available!', 'learnpress' ) );
446 }
447
448 do_action( 'learnpress/rest/frontend/profile/course_tab', $params );
449
450 $num_pages = $query->get_pages();
451 $current_page = $query->get_paged();
452
453 $template = $layout === 'grid' ? 'profile/tabs/courses/course-grid' : 'profile/tabs/courses/course-list';
454
455 $response->data = learn_press_get_template_content(
456 $template,
457 array(
458 'user' => $user,
459 'course_ids' => $course_ids,
460 'num_pages' => max( absint( $num_pages ), 1 ),
461 'current_page' => absint( $current_page ),
462 )
463 );
464 $response->status = 'success';
465
466 } catch ( Exception $e ) {
467 $response->message = $e->getMessage();
468 }
469
470 return $response;
471 }
472
473 /**
474 * Get course's user attend
475 *
476 * @param WP_REST_Request $request
477 *
478 * @return LP_REST_Response
479 * @since 4.1.5
480 * @version 1.0.0
481 * @author tungnx
482 */
483 /*public function course_attend( WP_REST_Request $request ): LP_REST_Response {
484 $params = $request->get_params();
485 $user_id = get_current_user_id();
486 $status = $params['status'] ?? '';
487 $paged = $params['paged'] ?? 1;
488 $layout = $params['layout'] ?? 'grid';
489 $response = new LP_REST_Response();
490
491 try {
492 if ( ! $user_id ) {
493 throw new Exception( __( 'The user is invalid', 'learnpress' ) );
494 }
495
496 $filter = new LP_User_Items_Filter();
497 $filter->limit = LP_Settings::get_option( 'archive_course_limit', 6 );
498 $filter->user_id = $user_id;
499 $total_rows = 0;
500 $courses = LP_User_Item_Course::get_user_courses( $filter, $total_rows );
501 $response->data->courses = $courses;
502 $response->data->total_pages = LP_Database::get_total_pages( $filter->limit, $total_rows );
503 } catch ( Throwable $e ) {
504 $response->message = $e->getMessage();
505 }
506
507 return $response;
508 }*/
509
510 /**
511 * API upload cover image profile.
512 *
513 * @param WP_REST_Request $request
514 *
515 * @return LP_REST_Response
516 * @since 4.2.7.2
517 * @version 1.0.0
518 */
519 public function handle_cover_image( WP_REST_Request $request ): LP_REST_Response {
520 $files = $request->get_file_params();
521 $action = $request->get_param( 'action' );
522 $response = new LP_REST_Response();
523 $upload_dir = learn_press_user_profile_picture_upload_dir();
524
525 try {
526 $user_id = get_current_user_id();
527 if ( ! $user_id ) {
528 throw new Exception( __( 'User is invalid!', 'learnpress' ) );
529 }
530
531 $user = UserModel::find( $user_id, true );
532 if ( ! $user ) {
533 throw new Exception( __( 'User is invalid!', 'learnpress' ) );
534 }
535
536 if ( $action === 'remove' ) {
537 $user->delete_cover_image();
538
539 $response->status = 'success';
540 $response->message = __( 'Cover image has been removed successfully', 'learnpress' );
541 $response->data->action = $action;
542
543 return $response;
544 }
545
546 if ( empty( $files ) || empty( $files['image'] ) ) {
547 throw new Exception( __( 'File is invalid!', 'learnpress' ) );
548 }
549
550 $cover_image_file = $files['image'];
551 $cover_image_name = $cover_image_file['name'];
552 $check_type = wp_check_filetype( $cover_image_name );
553
554 // Only allow image type
555 $image_types_allow = [ 'image/jpeg', 'image/png', 'image/webp' ];
556 if ( ! $check_type['type'] || ! in_array( $check_type['type'], $image_types_allow ) ) {
557 throw new Exception( __( 'File type is not allowed', 'learnpress' ) );
558 }
559
560 $cover_dir_path = $upload_dir['path'] . '/' . 'cover-image/';
561 $target_dir = LP_WP_Filesystem::instance()->is_dir( $cover_dir_path );
562 if ( ! $target_dir ) {
563 wp_mkdir_p( $cover_dir_path );
564 }
565
566 if ( ! LP_WP_Filesystem::instance()->is_writable( $cover_dir_path ) ) {
567 throw new Exception( __( 'The upload directory is not writable', 'learnpress' ) );
568 }
569
570 // Delete old image if exists
571 $user->delete_cover_image();
572
573 $file_name = md5( $user_id . microtime( true ) ) . '.' . $check_type['ext'];
574 $file_img_cer_blob = LP_WP_Filesystem::instance()->file_get_contents( $cover_image_file['tmp_name'] );
575 $put_content = LP_WP_Filesystem::instance()->put_contents(
576 $cover_dir_path . '/' . $file_name,
577 $file_img_cer_blob
578 );
579 if ( ! $put_content ) {
580 throw new Exception( __( 'Cannot write the file', 'learnpress' ) );
581 }
582
583 $upload_subdir = $upload_dir['subdir'] . '/' . 'cover-image/';
584 $path_save = $upload_subdir . $file_name;
585 $user->set_cover_image_url( $path_save );
586
587 do_action( 'learnpress/rest/frontend/profile/upload_cover_image', $user_id );
588
589 $response->status = 'success';
590 $response->message = __( 'Cover image is updated', 'learnpress' );
591 $response->data->url = $user->get_cover_image_url();
592 $response->data->action = $action;
593 } catch ( Throwable $th ) {
594 $response->message = $th->getMessage();
595 }
596
597 return $response;
598 }
599 }
600