PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9.1
4.4.9.1 4.4.9 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 All 141 releases
learnpress / inc / user / lp-user-functions.php

lp-user-functions.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.9.1, at inc/user/lp-user-functions.php

1,575 lines 39.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use LearnPress\Models\UserItems\UserItemModel;
4 use LearnPress\Models\UserModel;
5 use LearnPress\Services\UserService;
6
7 /**
8 * Common functions to process actions about user
9 *
10 * @author ThimPress
11 * @package LearnPress/Functions/User
12 * @version 1.0
13 */
14 function learn_press_get_user_profile_tabs() {
15 return LP_Profile::instance()->get_tabs();
16 }
17
18 /**
19 * Ensure new users receive a public slug.
20 *
21 * @param int $user_id
22 *
23 * @return void
24 */
25 function learn_press_maybe_generate_user_pretty_slug_on_register( int $user_id ) {
26 $userModel = UserModel::find( $user_id, true );
27 if ( ! $userModel ) {
28 return;
29 }
30
31 $userModel->generate_pretty_slug();
32 }
33
34 //add_action( 'user_register', 'learn_press_maybe_generate_user_pretty_slug_on_register' );
35
36 /**
37 * Delete user data by user ID
38 *
39 * @param int $user_id
40 * @param int $course_id
41 */
42 function learn_press_delete_user_data( $user_id, $course_id = 0 ) {
43 global $wpdb;
44 // TODO: Should be deleted user's order and order data???
45
46 $query_args = array( $user_id );
47
48 if ( $course_id ) {
49 $query_args[] = $course_id;
50 }
51
52 $query = $wpdb->prepare(
53 "
54 SELECT user_item_id
55 FROM {$wpdb->prefix}learnpress_user_items
56 WHERE user_id = %d
57 " . ( $course_id ? ' AND item_id = %d' : '' ) . '
58 ',
59 $query_args
60 );
61
62 // delete all courses user has enrolled
63 $query = $wpdb->prepare(
64 "
65 DELETE FROM {$wpdb->prefix}learnpress_user_items
66 WHERE user_id = %d
67 " . ( $course_id ? ' AND item_id = %d' : '' ) . '
68 ',
69 $query_args
70 );
71
72 @$wpdb->query( $query );
73 }
74
75 /**
76 * Get current user ID
77 *
78 * @return int|string LP_User is int, LP_User_Guest is string
79 */
80 function learn_press_get_current_user_id() {
81 $user = learn_press_get_current_user();
82
83 if ( ! $user ) {
84 return 0;
85 }
86
87 return $user->get_id();
88 }
89
90 /**
91 * Get the user by $user_id passed. If $user_id is NULL, get current user.
92 * If current user is not logged in, return a GUEST user
93 *
94 * @param bool $create_temp - Optional. Create temp user if user is not logged in.
95 *
96 * @return LP_User|LP_User_Guest
97 * @editor tungnx
98 * @modify 4.1.4
99 * @version 1.0.1
100 */
101 function learn_press_get_current_user( $create_temp = true ) {
102 $user_id = get_current_user_id();
103
104 if ( $user_id ) {
105 return learn_press_get_user( $user_id );
106 }
107
108 // Return LP_User_Guest
109 return learn_press_get_user( 0 );
110 }
111
112 if ( ! function_exists( 'learn_press_get_user' ) ) {
113 /**
114 * Get user by ID. Return false if the user does not exist.
115 * If user_id = 0, return a guest user.
116 *
117 * @param int $user_id
118 * @param bool $current
119 *
120 * @return LP_User|LP_User_Guest|false
121 * @since 3.0.0
122 * @version 4.0.1
123 */
124 function learn_press_get_user( $user_id = 0, $current = false, $force_new = false ) {
125 $userClass = $user_id && get_user_by( 'ID', $user_id ) ? 'LP_User' : ( $user_id == 0 ? 'LP_User_Guest' : false );
126 if ( false === $userClass ) {
127 return false;
128 }
129
130 return new $userClass( $user_id );
131 }
132 }
133
134 /**
135 * Add roles and add capabilities for roles
136 */
137 function learn_press_add_user_roles() {
138 $item_types = [
139 LP_COURSE_CPT,
140 LP_LESSON_CPT,
141 LP_ORDER_CPT,
142 ];
143
144 UserService::instance()->add_capabilities_for_roles();
145
146 foreach ( $item_types as $item_type ) {
147 UserService::instance()->add_capabilities_for_roles( $item_type );
148 }
149 }
150
151 /**
152 * Get queried user in profile link
153 *
154 * @return false|WP_User
155 * @since 3.0.0
156 * @deprecated 4.2.9.1
157 */
158 function learn_press_get_profile_user() {
159 return LP_Profile::instance()->get_user_current();
160
161 return LP_Profile::get_queried_user();
162 }
163
164
165 /**
166 * Add instructor registration button to register page and admin bar
167 */
168 function learn_press_user_become_teacher_registration_form() {
169 if ( LP_Settings::instance()->get( 'instructor_registration' ) != 'yes' ) {
170 return;
171 }
172 ?>
173 <p>
174 <label for="become_teacher">
175 <input type="checkbox" name="become_teacher" id="become_teacher">
176 <?php esc_html_e( 'Want to become an instructor?', 'learnpress' ); ?>
177 </label>
178 </p>
179 <?php
180 }
181
182 add_action( 'register_form', 'learn_press_user_become_teacher_registration_form' );
183
184 /**
185 * Update data into table learnpress_user_items.
186 *
187 * @param array $fields - Fields and values to be updated.
188 * Format: array(
189 * field_name_1 => value 1,
190 * field_name_2 => value 2,
191 * ....
192 * field_name_n => value n
193 * )
194 * @param mixed $where - Optional. Fields with values for conditional update with the same format of $fields.
195 * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0).
196 * @param bool $update_extra_fields_as_meta - Optional. Update extra fields as item meta (since 3.1.0).
197 *
198 * @return mixed
199 */
200 function learn_press_update_user_item_field( array $fields = array(), $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) {
201 global $wpdb;
202
203 // Table fields format.
204 $table_fields = array(
205 'user_item_id' => '%d',
206 'user_id' => '%d',
207 'item_id' => '%d',
208 'ref_id' => '%d',
209 'start_time' => '%s',
210 'end_time' => '%s',
211 'access_level' => '%d',
212 'graduation' => '%s',
213 'item_type' => '%s',
214 'status' => '%s',
215 'ref_type' => '%s',
216 'parent_id' => '%d',
217 );
218
219 /**
220 * Validate item status
221 */
222 if ( ! empty( $fields['item_id'] ) && ! empty( $fields['status'] ) ) {
223 $item_type = learn_press_get_post_type( $fields['item_id'] );
224
225 if ( LP_COURSE_CPT === $item_type ) {
226 if ( 'completed' === $fields['status'] ) {
227 $fields['status'] = 'finished';
228 }
229 } elseif ( 'finished' === $fields['status'] ) {
230 $fields['status'] = 'completed';
231 }
232 }
233
234 $data = array();
235 $data_format = array();
236 foreach ( $fields as $field => $value ) {
237 if ( ! empty( $table_fields[ $field ] ) ) {
238 $data[ $field ] = $value;
239
240 // Do not format the date-time field if it's value is NULL
241 if ( in_array( $field, array( 'start_time', 'end_time' ) ) && empty( $value ) ) {
242 $data[ $field ] = null;
243 $data_format[] = '';
244 } else {
245 if ( $value instanceof LP_Datetime ) {
246 $data[ $field ] = $value->toSql();
247 }
248 $data_format[] = $table_fields[ $field ];
249 }
250 }
251 }
252
253 if ( ! empty( $fields['user_item_id'] ) ) {
254 $where = wp_parse_args(
255 $where,
256 array( 'user_item_id' => $fields['user_item_id'] )
257 );
258 }
259
260 // Set where and where format
261 $where_format = array();
262 if ( is_array( $where ) && ! empty( $where ) ) {
263 if ( empty( $where['user_id'] ) ) {
264 $where['user_id'] = ! empty( $fields['user_id'] ) ? $fields['user_id'] : learn_press_get_current_user_id();
265 }
266
267 foreach ( $where as $field => $value ) {
268 if ( isset( $table_fields[ $field ] ) ) {
269 $where_format[] = $table_fields[ $field ];
270 }
271 }
272 }
273
274 if ( ! $data ) {
275 return false;
276 }
277
278 $inserted = false;
279 $updated = false;
280
281 // Update to database.
282 if ( $where ) {
283 $updated = $wpdb->update(
284 $wpdb->learnpress_user_items,
285 $data,
286 $where,
287 $data_format,
288 $where_format
289 );
290 } else {
291 // Insert to database.
292 if ( $wpdb->insert(
293 $wpdb->learnpress_user_items,
294 $data,
295 $data_format
296 ) ) {
297 $inserted = $wpdb->insert_id;
298 }
299 }
300
301 if ( $updated && ! empty( $where['user_item_id'] ) ) {
302 $inserted = $where['user_item_id'];
303 }
304
305 /**
306 * @var object|bool $updated_item
307 */
308 $updated_item = false;
309
310 // Get the item we just have updated or inserted.
311 if ( $inserted ) {
312 $updated_item = learn_press_get_user_item( $inserted );
313 } elseif ( $updated ) {
314 $updated_item = learn_press_get_user_item( $where );
315 }
316
317 // Clear caches.
318 if ( $updated_item ) {
319 // Clear cache total students enrolled.
320 if ( isset( $updated_item->item_type )
321 && LP_COURSE_CPT === $updated_item->item_type
322 && isset( $updated_item->item_id ) ) {
323 $lp_course_cache = new LP_Course_Cache( true );
324 $lp_course_cache->clean_total_students_enrolled( $updated_item->item_id );
325 $lp_course_cache->clean_total_students_enrolled_or_purchased( $updated_item->item_id );
326 // Clear cache count students many courses.
327 $lp_courses_cache = new LP_Courses_Cache( true );
328 $lp_courses_cache->clear_cache_on_group( LP_Courses_Cache::KEYS_COUNT_STUDENT_COURSES );
329 }
330 // Clear cache user item.
331 $lp_user_items_cache = new LP_User_Items_Cache();
332 $lp_user_items_cache->clean_user_item(
333 array(
334 $updated_item->user_id,
335 $updated_item->item_id,
336 $updated_item->item_type,
337 )
338 );
339 // Clear cache userItemModel
340 $userItemModel = UserItemModel::find_user_item(
341 $updated_item->user_id,
342 $updated_item->item_id,
343 $updated_item->item_type,
344 $updated_item->ref_id,
345 $updated_item->ref_type,
346 true
347 );
348 $userItemModel->clean_caches();
349 }
350
351 do_action( 'learn-press/updated-user-item-field', $updated_item );
352
353 /**
354 * If there is some fields does not contain in the main table
355 * then consider update them as metadata.
356 *
357 * @comment by tungnx - 4.1.7.3
358 */
359 /*
360 if ( $updated_item && $update_extra_fields_as_meta ) {
361 $extra_fields = array_diff_key( $fields, $table_fields );
362 if ( $extra_fields ) {
363 foreach ( $extra_fields as $meta_key => $meta_value ) {
364 if ( $meta_value == 'user_item_id' ) {
365 continue;
366 }
367
368 if ( $meta_value === false ) {
369 learn_press_delete_user_item_meta( $updated_item->user_item_id, $meta_key );
370 } else {
371
372 if ( empty( $meta_value ) ) {
373 $meta_value = '';
374 }
375 learn_press_update_user_item_meta( $updated_item->user_item_id, $meta_key, $meta_value );
376 }
377 }
378 }
379 }*/
380
381 // do_action( 'learn-press/updated-user-item-meta', $updated_item );
382
383 return $updated_item;
384 }
385
386 /**
387 * Get user item row(s) from user items table by multiple WHERE conditional
388 *
389 * @param array|int $where
390 * @param bool $single
391 *
392 * @return array
393 */
394 function learn_press_get_user_item( $where, $single = true ) {
395 global $wpdb;
396
397 // Table fields
398 $table_fields = array(
399 'user_item_id' => '%d',
400 'user_id' => '%d',
401 'item_id' => '%d',
402 'ref_id' => '%d',
403 'start_time' => '%s',
404 'end_time' => '%s',
405 'item_type' => '%s',
406 'status' => '%s',
407 'ref_type' => '%s',
408 'parent_id' => '%d',
409 );
410
411 // If $where is a number consider we are searching the record with unique user_item_id
412 if ( is_numeric( $where ) ) {
413 $where = array( 'user_item_id' => $where );
414 }
415
416 $where_str = array();
417 foreach ( $where as $field => $value ) {
418 if ( ! empty( $table_fields[ $field ] ) ) {
419 $where_str[] = "{$field} = " . $table_fields[ $field ];
420 }
421 }
422 $item = false;
423
424 if ( $where_str ) {
425 $query = $wpdb->prepare(
426 "
427 SELECT *
428 FROM {$wpdb->prefix}learnpress_user_items
429 WHERE " . join( ' AND ', $where_str ) . '
430 ORDER BY user_item_id DESC
431 ',
432 $where
433 );
434 if ( $single || ! empty( $where['user_item_id'] ) ) {
435 $item = $wpdb->get_row( $query );
436 } else {
437 $item = $wpdb->get_results( $query );
438 }
439 }
440
441 return $item;
442 }
443
444 /**
445 * Get user item meta from user_itemmeta table
446 *
447 * @param int $user_item_id .
448 * @param string $meta_key .
449 * @param bool $single .
450 *
451 * @return mixed
452 */
453 function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) {
454 $meta = false;
455 if ( metadata_exists( 'learnpress_user_item', $user_item_id, $meta_key ) ) {
456 $meta = get_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $single );
457 }
458
459 return $meta;
460 }
461
462 /**
463 * Add user item meta into table user_itemmeta
464 *
465 * @param int $user_item_id
466 * @param string $meta_key
467 * @param mixed $meta_value
468 * @param string $prev_value
469 *
470 * @return false|int
471 */
472 function learn_press_add_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) {
473 return add_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value );
474 }
475
476 /**
477 * Update user item meta to table user_itemmeta
478 *
479 * @param int $user_item_id
480 * @param string $meta_key
481 * @param mixed $meta_value
482 * @param string $prev_value
483 *
484 * @return bool|int
485 */
486 function learn_press_update_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) {
487 return update_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value );
488 }
489
490
491 /**
492 * Update user item meta to table user_itemmeta
493 *
494 * @param int $object_id
495 * @param string $meta_key
496 * @param mixed $meta_value
497 * @param bool $delete_all
498 *
499 * @return bool|int
500 */
501 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
502 return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all );
503 }
504
505 if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) {
506 /**
507 * Filter the avatar
508 *
509 * @param string $avatar
510 * @param string $id_or_email
511 * @param array $args
512 *
513 * @return string
514 * @since 1.0.0
515 * @version 1.0.2
516 */
517 function learn_press_pre_get_avatar_callback( $avatar, $id_or_email = '', $args = array() ) {
518 try {
519 if ( ( isset( $args['gravatar'] ) && $args['gravatar'] ) || ( $args['default'] && $args['force_default'] ) ) {
520 return $avatar;
521 }
522
523 $user_id = 0;
524
525 /**
526 * Get the ID of user from $id_or_email
527 */
528 if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) {
529 $user = get_user_by( 'email', $id_or_email );
530 if ( $user ) {
531 $user_id = $user->ID;
532 }
533 } elseif ( is_numeric( $id_or_email ) ) {
534 $user_id = $id_or_email;
535 } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) {
536 $user_id = $id_or_email->user_id;
537 } elseif ( $id_or_email instanceof WP_Comment ) {
538 $user = get_user_by( 'email', $id_or_email->comment_author_email );
539 if ( $user ) {
540 $user_id = $user->ID;
541 }
542 }
543
544 if ( ! $user_id ) {
545 return $avatar;
546 }
547
548 $user = UserModel::find( $user_id, true );
549 if ( ! $user ) {
550 return $avatar;
551 }
552
553 $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
554 if ( $args['class'] ) {
555 if ( is_array( $args['class'] ) ) {
556 $class = array_merge( $class, $args['class'] );
557 } else {
558 $class[] = $args['class'];
559 }
560 }
561
562 $avatar_url = $user->get_avatar_url();
563 $html_img = sprintf(
564 "<img src='%s' srcset='%s' class='%s' height='%d' width='%d' decoding='async'/>",
565 esc_url( $avatar_url ),
566 esc_url( $avatar_url ) . ' 2x',
567 esc_attr( implode( ' ', $class ) ),
568 (int) $args['height'],
569 (int) $args['width']
570 );
571
572 return $html_img;
573 } catch ( Throwable $e ) {
574 error_log( $e->getMessage() );
575
576 return $avatar;
577 }
578 }
579 }
580 add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 999, 3 );
581
582
583 function learn_press_user_profile_picture_upload_dir( $width_user = true ) {
584 $upload_dir = wp_upload_dir();
585 $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user );
586 if ( $width_user ) {
587 $subdir .= '/' . get_current_user_id();
588 }
589 $subdir = '/' . $subdir;
590
591 if ( ! empty( $upload_dir['subdir'] ) ) {
592 $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
593 $u_path = str_replace( '\\', '/', $upload_dir['path'] );
594
595 $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path );
596 $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] );
597 } else {
598 $upload_dir['path'] = $upload_dir['path'] . $subdir;
599 $upload_dir['url'] = $upload_dir['url'] . $subdir;
600 }
601
602 $upload_dir['subdir'] = $subdir;
603
604 // Point path/url to main site if we are in multisite
605 if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
606 foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) {
607 $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] );
608 }
609 }
610
611 return $upload_dir;
612 }
613
614 /*
615 add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 );
616 function _learn_press_before_purchase_course_handler( $course_id, $cart ) {
617 // Redirect to login page if user is not logged in
618 if ( ! is_user_logged_in() ) {
619 $return_url = esc_url_raw( add_query_arg( $_POST, get_the_permalink( $course_id ) ) );
620 $return_url = apply_filters( 'learn_press_purchase_course_login_redirect_return_url', $return_url );
621 $redirect = apply_filters(
622 'learn_press_purchase_course_login_redirect',
623 learn_press_get_login_url( $return_url )
624 );
625 if ( $redirect !== false ) {
626 learn_press_add_message( __( 'Please log in to enroll in this course', 'learnpress' ) );
627
628 if ( learn_press_is_ajax() ) {
629 learn_press_send_json(
630 array(
631 'redirect' => $redirect,
632 'result' => 'success',
633 )
634 );
635 } else {
636 wp_redirect( $redirect );
637 exit();
638 }
639 }
640 } else {
641 $user = learn_press_get_current_user();
642 $redirect = false;
643 if ( $user->has_finished_course( $course_id ) ) {
644 learn_press_add_message( __( 'You have already finished the course', 'learnpress' ) );
645 $redirect = true;
646 } elseif ( $user->has_purchased_course( $course_id ) ) {
647 learn_press_add_message( __( 'You have already enrolled in this course', 'learnpress' ) );
648 $redirect = true;
649 }
650 if ( $redirect ) {
651 wp_redirect( get_the_permalink( $course_id ) );
652 exit();
653 }
654 }
655 }*/
656
657 /*
658 function learn_press_user_is( $role, $user_id = 0 ) {
659 if ( ! $user_id ) {
660 $user = learn_press_get_current_user();
661 } else {
662 $user = learn_press_get_user( $user_id );
663 }
664 if ( $role == 'admin' ) {
665 return $user->is_admin();
666 }
667 if ( $role == 'instructor' ) {
668 return $user->is_instructor();
669 }
670
671 return $role;
672 }*/
673
674 /*
675 function learn_press_profile_tab_edit_content( $current, $tab, $user ) {
676 learn_press_get_template(
677 'profile/tabs/edit.php',
678 array(
679 'user' => $user,
680 'current' => $current,
681 'tab' => $tab,
682 )
683 );
684 }*/
685
686 /*
687 function learn_press_update_user_option( $name, $value, $id = 0 ) {
688 if ( ! $id ) {
689 $id = get_current_user_id();
690 }
691 $key = 'learnpress_user_options';
692 $options = get_user_option( $key, $id );
693 $options[ $name ] = $value;
694 update_user_option( $id, $key, $options, true );
695 }*/
696
697 /*
698 function learn_press_delete_user_option( $name, $id = 0 ) {
699 if ( ! $id ) {
700 $id = get_current_user_id();
701 }
702 $key = 'learnpress_user_options';
703 $options = get_user_option( $key, $id );
704 if ( is_array( $options ) && array_key_exists( $name, $options ) ) {
705 unset( $options[ $name ] );
706 update_user_option( $id, $key, $options, true );
707
708 return true;
709 }
710
711 return false;
712 }*/
713
714 /*
715 function learn_press_get_user_option( $name, $id = 0 ) {
716 if ( ! $id ) {
717 $id = get_current_user_id();
718 }
719 $key = 'learnpress_user_options';
720 $options = get_user_option( $key, $id );
721 if ( is_array( $options ) && array_key_exists( $name, $options ) ) {
722 return $options[ $name ];
723 }
724
725 return false;
726 }*/
727
728 /**
729 * Update user basic information.
730 *
731 * @param bool $wp_error - Optional. Return WP_Error object in case updating failed.
732 *
733 * @return bool|mixed|WP_Error
734 */
735 function learn_press_update_user_profile_basic_information( $wp_error = false ) {
736 $user_id = get_current_user_id();
737
738 if ( ! $user_id ) {
739 return new WP_Error( 2, 'The user is invalid' );
740 }
741
742 $update_data = array(
743 'ID' => $user_id,
744 'first_name' => LP_Request::get_param( 'first_name' ),
745 'last_name' => LP_Request::get_param( 'last_name' ),
746 'description' => LP_Request::get_param( 'description', '', 'html' ),
747 'display_name' => LP_Request::get_param( 'account_display_name' ),
748 'user_email' => LP_Request::get_param( 'account_email' ),
749 );
750
751 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
752 $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' );
753
754 $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta );
755
756 // Update for social.
757 $socials = LP_Request::get_param( 'user_profile_social' );
758 $extra_data = get_user_meta( $user_id, '_lp_extra_info', true );
759
760 if ( ! empty( $extra_data ) ) {
761 $socials = array_merge( $extra_data, $socials );
762 }
763
764 update_user_meta( $user_id, '_lp_extra_info', $socials );
765
766 if ( is_wp_error( $return ) ) {
767 return $wp_error ? $return : false;
768 }
769
770 return $return;
771 }
772
773 /**
774 * Update new password.
775 *
776 * @version 1.0.1
777 */
778 function learn_press_update_user_profile_change_password() {
779 try {
780 $user = wp_get_current_user();
781 if ( ! $user ) {
782 throw new Exception( __( 'The user is invalid', 'learnpress' ) );
783 }
784
785 $old_pass = LP_Request::get_param( 'pass0', '', 'text', 'post' );
786 if ( empty( $old_pass ) ) {
787 throw new Exception( __( 'Enter the old password!', 'learnpress' ) );
788 }
789
790 $check_old_pass = wp_check_password( $old_pass, $user->data->user_pass, $user->ID );
791 if ( ! $check_old_pass ) {
792 throw new Exception( __( 'The old password is incorrect!', 'learnpress' ) );
793 }
794
795 $new_pass = LP_Request::get_param( 'pass1', '', 'text', 'post' );
796 $new_pass_confirm = LP_Request::get_param( 'pass2', '', 'text', 'post' );
797 if ( empty( $new_pass ) || empty( $new_pass_confirm )
798 || $new_pass != $new_pass_confirm ) {
799 throw new Exception( __( 'Incorrect confirmation password!', 'learnpress' ) );
800 }
801
802 // Update user password
803 // wp_set_password( $new_pass, $user->ID );
804 $update_data = array(
805 'user_pass' => $new_pass,
806 'ID' => $user->ID,
807 );
808
809 $rs = wp_update_user( $update_data );
810 if ( is_wp_error( $rs ) ) {
811 throw new Exception( $rs->get_error_message() );
812 }
813 } catch ( Exception $ex ) {
814 return new WP_Error( 'error_lp_profile_update_pass', $ex->getMessage() );
815 }
816
817 return true;
818 }
819
820 function learn_press_get_avatar_thumb_size() {
821 $option = LP_Settings::get_option(
822 'avatar_dimensions',
823 array(
824 'width' => 250,
825 'height' => 250,
826 )
827 );
828
829 if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) {
830 $option = array(
831 'width' => 250,
832 'height' => 250,
833 );
834 }
835
836 // For option get_avatar_url
837 $option['size'] = $option['width'];
838
839 return $option;
840 }
841
842 function learn_press_get_course_thumbnail_dimensions() {
843 $option = LP_Settings::get_option(
844 'course_thumbnail_dimensions',
845 array(
846 'width' => 500,
847 'height' => 300,
848 )
849 );
850
851 if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) {
852 $option = array(
853 'width' => 500,
854 'height' => 300,
855 );
856 }
857
858 return $option;
859 }
860
861 /*
862 function learn_press_get_user_avatar( $user_id = 0, $size = '' ) {
863 $user = learn_press_get_user( $user_id );
864
865 return $user->get_profile_picture( '', $size );
866 }*/
867
868 /**
869 * Get profile instance for an user to view.
870 *
871 * @param int $for_user
872 *
873 * @return LP_Profile|WP_Error
874 */
875 function learn_press_get_profile( $for_user = 0 ) {
876 return LP_Profile::instance( $for_user );
877 }
878
879 /*
880 function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) {
881 global $wpdb;
882
883 settype( $item_id, 'array' );
884
885 $format = array_fill( 0, sizeof( $item_id ), '%d' );
886 $where = '';
887
888 $args = array( $user_id );
889 $args = array_merge( $args, $item_id );
890
891 if ( $course_id ) {
892 $args[] = $course_id;
893 $where = 'AND ref_id = %d';
894 }
895
896 if ( $include_course ) {
897 $where .= ' OR ( item_id = %d AND item_type = %s )';
898 $args[] = $course_id;
899 $args[] = LP_COURSE_CPT;
900 }
901
902 $query = $wpdb->prepare(
903 "
904 DELETE
905 FROM {$wpdb->learnpress_user_items}
906 WHERE user_id = %d
907 AND ( item_id IN(" . join( ',', $format ) . ")
908 $where )
909 ",
910 $args
911 );
912 }*/
913
914 /**
915 * Get user profile link
916 *
917 * @param int $user_id
918 * @param null $tab
919 *
920 * @return mixed|string
921 */
922 function learn_press_user_profile_link( $user_id = 0, $tab = '' ) {
923 if ( ! $user_id ) {
924 $user_id = get_current_user_id();
925 }
926
927 $user = learn_press_get_user( $user_id );
928
929 if ( ! $user ) {
930 return '';
931 }
932
933 global $wp_query;
934 $wp_user = get_userdata( $user_id );
935 if ( ! $wp_user instanceof WP_User ) {
936 return '';
937 }
938
939 $user_model = new UserModel( $wp_user->data );
940 $args = array(
941 'user' => $user_model->get_slug_link(),
942 );
943
944 if ( $tab ) {
945 $args['tab'] = $tab;
946 }
947
948 /**
949 * If no tab is selected in profile and is current user
950 * then no need the username in profile link.
951 */
952 if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) {
953 //unset( $args['user'] );
954 }
955
956 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
957 if ( $profile_link ) {
958 if ( get_option( 'permalink_structure' ) ) {
959 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
960 } else {
961 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
962 }
963 } else {
964 $url = get_author_posts_url( $user_id );
965 }
966
967 return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab );
968 }
969
970 /*
971 function learn_press_create_user_item( $args = array(), $wp_error = false ) {
972 global $wpdb;
973
974 $defaults = array(
975 'user_id' => get_current_user_id(),
976 'item_id' => '',
977 'start_time' => time(),
978 'end_time' => '',
979 'graduation' => '',
980 'item_type' => '',
981 'status' => '',
982 'ref_id' => 0,
983 'ref_type' => '',
984 'parent_id' => 0,
985 'create_meta' => array(),
986 );
987
988 $item_data = wp_parse_args( $args, $defaults );
989
990 // Validate item_id and post type
991 if ( empty( $item_data['item_id'] ) ) {
992 if ( $wp_error ) {
993 return new WP_Error( 'invalid_item_id', __( 'Invalid item id.', 'learnpress' ) );
994 }
995
996 return 0;
997 }
998
999 $post_type = learn_press_get_post_type( $item_data['item_id'] );
1000 if ( empty( $item_data['item_type'] ) && $post_type ) {
1001 $item_data['item_type'] = $post_type;
1002 }
1003
1004 // Get id and type of ref if they are null
1005 if ( ! empty( $item_data['parent_id'] ) && ( empty( $item_data['ref_id'] ) || ( empty( $item_data['ref_type'] ) ) ) ) {
1006 $parent = $wpdb->get_row(
1007 $wpdb->prepare(
1008 "SELECT * FROM {$wpdb->learnpress_user_items} WHERE %d",
1009 $item_data['parent_id']
1010 )
1011 );
1012
1013 if ( $parent ) {
1014 if ( empty( $item_data['ref_id'] ) ) {
1015 $item_data['ref_id'] = $parent->item_id;
1016 }
1017
1018 if ( empty( $item_data['ref_type'] ) ) {
1019 $item_data['ref_type'] = $parent->item_type;
1020 }
1021 }
1022 }
1023
1024 // Filter
1025 $item_data = apply_filters( 'learn-press/create-user-item-data', $item_data );
1026 if ( ! $item_data ) {
1027 if ( $wp_error ) {
1028 return new WP_Error( 'invalid_item_data', __( 'Invalid item data.', 'learnpress' ) );
1029 }
1030
1031 return 0;
1032 }
1033
1034 do_action( 'learn-press/before-create-user-item', $item_data );
1035
1036 $create_meta = ! empty( $item_data['create_meta'] ) ? $item_data['create_meta'] : false;
1037
1038 if ( $create_meta ) {
1039 unset( $item_data['create_meta'] );
1040 }
1041
1042 $user_item = new LP_User_Item( $item_data );
1043 $result = $user_item->update();
1044 if ( ! $result || is_wp_error( $result ) ) {
1045 if ( $wp_error && is_wp_error( $result ) ) {
1046 return $result;
1047 }
1048
1049 return 0;
1050 }
1051
1052 do_action( 'learn-press/created-user-item', $user_item, $item_data );
1053
1054 $create_meta = apply_filters( 'learn-press/create-user-item-meta', $create_meta, $item_data );
1055 if ( ! $create_meta ) {
1056 return $user_item;
1057 }
1058
1059 do_action( 'learn-press/before-create-user-item-meta', $create_meta );
1060
1061 foreach ( $create_meta as $key => $value ) {
1062 learn_press_update_user_item_meta( $user_item->get_user_item_id(), $key, $value );
1063 }
1064
1065 do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta );
1066
1067 return $user_item;
1068 }*/
1069
1070 /*
1071 function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) {
1072 global $wpdb;
1073
1074 $query = $wpdb->prepare( "SELECT user_item_id FROM $wpdb->learnpress_user_items WHERE ref_id=%d AND item_id=%d", $course_id, $quiz_id );
1075 $col = $wpdb->get_col( $query );
1076
1077 if ( ! empty( $col ) ) {
1078 return $col;
1079 } else {
1080 return false;
1081 }
1082 }*/
1083
1084 /**
1085 * Prepares list of questions for rest api.
1086 *
1087 * @param int[] $question_ids
1088 * @param array $args
1089 *
1090 * @return array
1091 * @since 3.3.0
1092 */
1093 function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ): array {
1094 if ( is_numeric( $args ) ) {
1095
1096 } else {
1097 $args = wp_parse_args(
1098 $args,
1099 array(
1100 'instant_hint' => true,
1101 'instant_check' => true,
1102 'quiz_status' => '',
1103 'checked_questions' => array(),
1104 'hinted_questions' => array(),
1105 'answered' => array(),
1106 'show_correct_review' => true,
1107 )
1108 );
1109 }
1110
1111 $checkedQuestions = $args['checked_questions'];
1112 $hintedQuestions = $args['hinted_questions'];
1113 $instantHint = $args['instant_hint'];
1114 $instantCheck = $args['instant_check'];
1115 $quizStatus = $args['quiz_status'];
1116 $answered = $args['answered'];
1117 $status = $args['status'] ?? '';
1118 $questions = array();
1119
1120 if ( $question_ids ) {
1121 foreach ( $question_ids as $id ) {
1122 $question = learn_press_get_question( $id );
1123 $hasHint = false;
1124 $hasExplanation = false;
1125 $canCheck = false;
1126 $hinted = false;
1127 $checked = false;
1128 $theHint = $question->get_hint();
1129 $theExplanation = '';
1130
1131 if ( $instantCheck || $status == 'completed' ) {
1132 $theExplanation = $question->get_explanation();
1133 $checked = in_array( $id, $checkedQuestions );
1134 $hasExplanation = (bool) $theExplanation;
1135 }
1136
1137 $mark = $question->get_mark() ? $question->get_mark() : 1;
1138
1139 $questionData = array(
1140 'object' => $question,
1141 'id' => absint( $id ),
1142 'title' => $question->get_title(),
1143 'type' => $question->get_type(),
1144 'point' => $mark,
1145 );
1146
1147 $content = $question->get_content();
1148 if ( $content ) {
1149 $questionData['content'] = $content;
1150 }
1151
1152 if ( $theHint ) {
1153 $questionData['hint'] = $theHint;
1154 }
1155
1156 if ( $status == 'completed' || ( $checked && $theExplanation ) ) {
1157 $questionData['explanation'] = $theExplanation;
1158 }
1159
1160 if ( $hasExplanation ) {
1161 $questionData['has_explanation'] = $hasExplanation;
1162
1163 if ( $checked ) {
1164 $questionData['explanation'] = $theExplanation;
1165 }
1166 }
1167
1168 $with_true_or_false = ( $checked || ( $quizStatus == 'completed' && $args['show_correct_review'] ) );
1169
1170 // if ( $question->is_support( 'answer-options' ) ) {
1171 $questionData['options'] = learn_press_get_question_options_for_js(
1172 $question,
1173 array(
1174 'include_is_true' => $with_true_or_false,
1175 'answer' => $answered[ $id ]['answered'] ?? '',
1176 )
1177 );
1178 // }
1179
1180 $questions[] = $questionData;
1181 }
1182
1183 /**
1184 * Remove answered
1185 */
1186 if ( $quizStatus !== 'completed' ) {
1187 if ( $checkedQuestions && $quizStatus ) {
1188
1189 $omitIds = array_diff( $question_ids, $checkedQuestions );
1190
1191 if ( $omitIds ) {
1192 foreach ( $omitIds as $omitId ) {
1193 if ( ! empty( $answered[ $omitId ] ) ) {
1194 unset( $answered[ $omitId ] );
1195 }
1196 }
1197 }
1198 }
1199 }
1200 }
1201
1202 return apply_filters( 'learn-press/list-questions-data', $questions );
1203 }
1204
1205 /**
1206 * Update extra profile data upon update user.
1207 *
1208 * @param int $user_id
1209 *
1210 * @since 4.0.0
1211 */
1212 function learn_press_update_extra_user_profile_fields( $user_id ) {
1213 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1214 return;
1215 }
1216
1217 if ( array_key_exists( 'lp_user_slug', $_POST ) ) {
1218 $userModel = UserModel::find( $user_id, true );
1219 if ( ! $userModel ) {
1220 return;
1221 }
1222
1223 $slug_result = $userModel->update_user_nicename( (string) $_POST['lp_user_slug'] );
1224 if ( is_wp_error( $slug_result ) ) {
1225 wp_safe_redirect(
1226 add_query_arg(
1227 'lp-message',
1228 urlencode( $slug_result->get_error_message() ),
1229 wp_get_referer()
1230 )
1231 );
1232 die();
1233 }
1234 }
1235
1236 if ( isset( $_POST['_lp_extra_info'] ) ) {
1237 $extra_info = LP_Request::get_param( '_lp_extra_info', array(), '', 'post' );
1238 update_user_meta( $user_id, '_lp_extra_info', $extra_info );
1239 }
1240 }
1241
1242 add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' );
1243 add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' );
1244
1245 /**
1246 * Get extra profile info data
1247 *
1248 * @param int $user_id
1249 *
1250 * @return array
1251 * @since 4.0.1
1252 */
1253 function learn_press_get_user_extra_profile_info( $user_id = 0 ) {
1254 if ( ! $user_id ) {
1255 $user_id = get_current_user_id();
1256 }
1257
1258 $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id );
1259 $social_fields = learn_press_social_profiles();
1260
1261 $user_socials = array();
1262 foreach ( $social_fields as $key => $label ) {
1263 $key = sanitize_key( $key );
1264 $user_socials[ $key ] = '';
1265 if ( is_array( $extra_profile_info ) && isset( $extra_profile_info[ $key ] ) ) {
1266 $user_socials[ $key ] = esc_url_raw( $extra_profile_info[ $key ] );
1267 }
1268 }
1269
1270 return apply_filters( 'learn-press/user-extra-profile-info', $user_socials, $user_id );
1271 }
1272
1273 /**
1274 * @return array
1275 * @since 3.x.x
1276 * @version 4.0.2
1277 */
1278 function learn_press_social_profiles() {
1279 return apply_filters(
1280 'learn-press/social-profiles',
1281 array(
1282 'facebook' => learn_press_social_profile_name( 'facebook' ),
1283 'twitter' => learn_press_social_profile_name( 'twitter' ),
1284 'youtube' => learn_press_social_profile_name( 'youtube' ),
1285 'linkedin' => learn_press_social_profile_name( 'linkedin' ),
1286 )
1287 );
1288 }
1289
1290 function learn_press_social_profile_name( $key ) {
1291 $name = '';
1292 switch ( $key ) {
1293 case 'facebook':
1294 $name = esc_html__( 'Facebook Profile', 'learnpress' );
1295 break;
1296 case 'twitter':
1297 $name = esc_html__( 'X Profile', 'learnpress' );
1298 break;
1299 case 'youtube':
1300 $name = esc_html__( 'Youtube Channel', 'learnpress' );
1301 break;
1302 case 'linkedin':
1303 $name = esc_html__( 'Linkedin Profile', 'learnpress' );
1304 break;
1305 default:
1306 $name = ucfirst( $key );
1307 }
1308
1309 return apply_filters( 'learn-press/social-profile-name', $name, $key );
1310 }
1311
1312 function lp_add_default_fields( $fields ) {
1313 $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' );
1314
1315 if ( $first_name === 'yes' ) {
1316 ?>
1317 <li class="form-field">
1318 <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label>
1319 <input id="reg_first_name" name="reg_first_name" type="text"
1320 placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1321 value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1322 </li>
1323 <?php
1324 }
1325
1326 $last_name = LP_Settings::instance()->get( 'enable_register_last_name' );
1327
1328 if ( $last_name === 'yes' ) {
1329 ?>
1330 <li class="form-field">
1331 <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label>
1332 <input id="reg_last_name" name="reg_last_name" type="text"
1333 placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1334 value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1335 </li>
1336 <?php
1337 }
1338
1339 $display_name = LP_Settings::instance()->get( 'enable_register_display_name' );
1340
1341 if ( $display_name === 'yes' ) {
1342 ?>
1343 <li class="form-field">
1344 <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label>
1345 <input id="reg_display_name" name="reg_display_name" type="text"
1346 placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1347 value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1348 </li>
1349 <?php
1350 }
1351 }
1352
1353 add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' );
1354
1355 function lp_custom_register_fields_display() {
1356 ?>
1357 <?php $custom_fields = LP_Profile::get_register_fields_custom(); ?>
1358
1359 <?php if ( $custom_fields ) : ?>
1360 <?php foreach ( $custom_fields as $custom_field ) : ?>
1361 <?php
1362 $cf_class = '';
1363 if ( $custom_field['required'] == 'yes' ) {
1364 $cf_class = ' required';
1365 ?>
1366 <style>
1367 .required label {
1368 font-weight: bold;
1369 }
1370
1371 .required label:after {
1372 content: ' *';
1373 display: inline;
1374 }
1375 </style>
1376 <?php
1377 }
1378
1379 if ( isset( $custom_field['id'] ) ) {
1380 ?>
1381 <?php $value = $custom_field['id']; ?>
1382
1383 <li class="form-field<?php echo esc_attr( $cf_class ); ?>">
1384 <?php
1385 switch ( $custom_field['type'] ) {
1386 case 'text':
1387 case 'number':
1388 case 'email':
1389 case 'url':
1390 case 'tel':
1391 ?>
1392 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1393 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1394 type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1395 value=""/>
1396 <?php
1397 break;
1398 case 'textarea':
1399 ?>
1400 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1401 <textarea name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"></textarea>
1402 <?php
1403 break;
1404 case 'checkbox':
1405 ?>
1406 <label>
1407 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1408 type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1409 <?php echo esc_html( $custom_field['name'] ); ?>
1410 </label>
1411 <?php
1412 break;
1413 }
1414 ?>
1415 </li>
1416 <?php } ?>
1417 <?php endforeach; ?>
1418 <?php endif; ?>
1419 <?php
1420 }
1421
1422 add_action( 'learn-press/after-form-register-fields', 'lp_custom_register_fields_display' );
1423
1424 /**
1425 * Custom register fields
1426 *
1427 * @param [type] $user_id
1428 *
1429 * @return void
1430 */
1431 function lp_user_custom_register_fields( $user_id, $fields = array() ) {
1432 if ( ! empty( $fields ) ) {
1433 update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $fields ) );
1434 } elseif ( isset( $_POST['_lp_custom_register'] ) ) {
1435 update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ) );
1436 }
1437 }
1438
1439 add_action( 'personal_options_update', 'lp_user_custom_register_fields' );
1440 add_action( 'edit_user_profile_update', 'lp_user_custom_register_fields' );
1441
1442 function lp_get_user_custom_register_fields( $user_id = 0 ) {
1443 if ( ! $user_id ) {
1444 $user_id = get_current_user_id();
1445 }
1446
1447 $register_fields = get_the_author_meta( '_lp_custom_register', $user_id );
1448 $defaults = lp_get_user_custom_fields();
1449
1450 $extra_profile_info = wp_parse_args( $register_fields, $defaults );
1451
1452 return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id );
1453 }
1454
1455 function lp_get_user_custom_fields() {
1456 $custom_fields = LP_Settings::get_option( 'register_profile_fields', array() );
1457
1458 $output = array();
1459
1460 if ( $custom_fields ) {
1461 foreach ( $custom_fields as $field ) {
1462 $output[ $field['id'] ] = '';
1463 }
1464 }
1465
1466 return $output;
1467 }
1468
1469 /**
1470 * Check extra user data is a social profile.
1471 *
1472 * @param $key
1473 *
1474 * @return bool
1475 * @since 4.0.0
1476 * @deprecated 4.3.2
1477 */
1478 function learn_press_is_social_profile( $key ) {
1479 _deprecated_function( __FUNCTION__, '4.3.2' );
1480
1481 return true;
1482
1483 $is_socials = learn_press_social_profiles();
1484
1485 return in_array( $key, $is_socials );
1486 }
1487
1488 /**
1489 * Get extra profile fields will be registered in backend profile.
1490 *
1491 * @return array
1492 * @since 4.0.0
1493 * @deprecated 4.3.2
1494 */
1495 function learn_press_get_user_extra_profile_fields() {
1496 _deprecated_function( __FUNCTION__, '4.3.2' );
1497 return array();
1498
1499 $socials = learn_press_social_profiles();
1500
1501 return $socials;
1502
1503 $fields = array();
1504
1505 foreach ( $socials as $social ) {
1506 $fields[ $social ] = learn_press_social_profile_name( $social );
1507 }
1508
1509 return apply_filters( 'learn-press/user-extra-profile-fields', $fields );
1510 }
1511
1512 /**
1513 * Show courses user enrolled on backend
1514 *
1515 * @param $user
1516 *
1517 * @return void
1518 */
1519 function learn_press_user_profile_data( $user ) {
1520 if ( ! is_admin() ) {
1521 return;
1522 }
1523
1524 learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) );
1525 learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) );
1526 }
1527
1528 add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 );
1529 add_action( 'show_user_profile', 'learn_press_user_profile_data', 1000 );
1530
1531 /*
1532 function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) {
1533 if ( empty( $user_id ) ) {
1534 return false;
1535 }
1536
1537 $args = array(
1538 'author' => $user_id,
1539 'posts_per_page' => - 1,
1540 'post_type' => $post_type,
1541 'post_status' => 'any',
1542 );
1543
1544 $posts = get_posts( $args );
1545
1546 $output = array(
1547 'all' => count( $posts ),
1548 'publish' => array(),
1549 'pending' => array(),
1550 );
1551
1552 $pending = $public = array();
1553
1554 if ( ! empty( $posts ) ) {
1555 foreach ( $posts as $post ) {
1556 switch ( $post->post_status ) {
1557 case 'pending':
1558 $pending[] = $post;
1559 break;
1560 case 'publish':
1561 $public[] = $post;
1562 break;
1563 default:
1564 break;
1565 }
1566 }
1567 }
1568
1569 return array(
1570 'all' => count( $posts ),
1571 'publish' => count( $public ),
1572 'pending' => count( $pending ),
1573 );
1574 }*/
1575