PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
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
← All changes | inc/user/lp-user-functions.php +348 -857 4.2.04.4.8 View file →
@@ -1,5 +1,10 @@
1 1 <?php
2 +
3 +use LearnPress\Models\UserItems\UserItemModel;
4 +use LearnPress\Models\UserModel;
5 +use LearnPress\Services\UserService;
6 +
2 7 /**
3 8 * Common functions to process actions about user
4 9 *
5 10 * @author ThimPress
@@ -5,14 +10,31 @@
5 10 * @author ThimPress
6 11 * @package LearnPress/Functions/User
7 12 * @version 1.0
8 13 */
9 -
10 14 function learn_press_get_user_profile_tabs() {
11 15 return LP_Profile::instance()->get_tabs();
12 16 }
13 17
14 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 +/**
15 37 * Delete user data by user ID
16 38 *
17 39 * @param int $user_id
18 40 * @param int $course_id
@@ -50,32 +72,13 @@
50 72 @$wpdb->query( $query );
51 73 }
52 74
53 75 /**
54 - * Get user_item_id field in table learnpress_user_items
55 - * with the user_id, item_id. If $course_id is not passed
56 - * then item_id is ID of a course. Otherwise, item_id is
57 - * ID of an item (like quiz/lesson).
58 - *
59 - * @param int $user_id
60 - * @param int $item_id
61 - * @param int $course_id
62 - *
63 - * @return bool
64 - * @editor tungnx
65 - * @reason this function only get cache, not handle get user_item_id
66 - * @deprecated 4.1.6.9
67 - */
68 -function learn_press_get_user_item_id( $user_id, $item_id, $course_id = 0 /* added 3.0.0 */ ) {
69 - return false;
70 -}
71 -
72 -/**
73 76 * Get current user ID
74 77 *
75 - * @return int
78 + * @return int|string LP_User is int, LP_User_Guest is string
76 79 */
77 -function learn_press_get_current_user_id(): int {
80 +function learn_press_get_current_user_id() {
78 81 $user = learn_press_get_current_user();
79 82
80 83 if ( ! $user ) {
81 84 return 0;
@@ -110,9 +113,9 @@
110 113 /**
111 114 * Get user by ID. Return false if the user does not exist.
112 115 * If user_id = 0, return a guest user.
113 116 *
114 - * @param int $user_id
117 + * @param int $user_id
115 118 * @param bool $current
116 119 *
117 120 * @return LP_User|LP_User_Guest|false
118 121 * @since 3.0.0
@@ -128,209 +131,32 @@
128 131 }
129 132 }
130 133
131 134 /**
132 - * Add more 2 user roles teacher and student
135 + * Add roles and add capabilities for roles
133 136 */
134 137 function learn_press_add_user_roles() {
138 + $item_types = [
139 + LP_COURSE_CPT,
140 + LP_LESSON_CPT,
141 + LP_ORDER_CPT,
142 + ];
135 143
136 - $settings = LP_Settings::instance();
137 -
138 - /* translators: user role */
139 - _x( 'LP Instructor', 'User role' );
140 -
141 - add_role(
142 - LP_TEACHER_ROLE,
143 - 'LP Instructor',
144 - array()
145 - );
146 -
147 - $course_cap = LP_COURSE_CPT . 's';
148 - $lesson_cap = LP_LESSON_CPT . 's';
149 - $order_cap = LP_ORDER_CPT . 's';
150 -
151 - $teacher = get_role( LP_TEACHER_ROLE );
152 - if ( $teacher ) {
153 - $teacher->add_cap( 'read_private_' . $course_cap );
154 - $teacher->add_cap( 'delete_published_' . $course_cap );
155 - $teacher->add_cap( 'edit_published_' . $course_cap );
156 - $teacher->add_cap( 'edit_' . $course_cap );
157 - $teacher->add_cap( 'delete_' . $course_cap );
158 - $teacher->add_cap( 'unfiltered_html' );
159 -
160 - $settings->get( 'required_review' );
161 -
162 - if ( $settings->get( 'required_review' ) == 'yes' ) {
163 - $teacher->remove_cap( 'publish_' . $course_cap );
164 - } else {
165 - $teacher->add_cap( 'publish_' . $course_cap );
166 - }
167 -
168 - $teacher->add_cap( 'read_private_' . $lesson_cap );
169 - $teacher->add_cap( 'delete_published_' . $lesson_cap );
170 - $teacher->add_cap( 'edit_published_' . $lesson_cap );
171 - $teacher->add_cap( 'edit_' . $lesson_cap );
172 - $teacher->add_cap( 'delete_' . $lesson_cap );
173 - $teacher->add_cap( 'publish_' . $lesson_cap );
174 - $teacher->add_cap( 'upload_files' );
175 - $teacher->add_cap( 'read' );
176 - $teacher->add_cap( 'edit_posts' );
144 + foreach ( $item_types as $item_type ) {
145 + UserService::instance()->add_capabilities_for_roles( $item_type );
177 146 }
178 -
179 - // administrator
180 - $admin = get_role( 'administrator' );
181 - if ( $admin ) {
182 - $admin->add_cap( 'read_private_' . $course_cap );
183 - $admin->add_cap( 'delete_' . $course_cap );
184 - $admin->add_cap( 'delete_published_' . $course_cap );
185 - $admin->add_cap( 'edit_' . $course_cap );
186 - $admin->add_cap( 'edit_published_' . $course_cap );
187 - $admin->add_cap( 'publish_' . $course_cap );
188 - $admin->add_cap( 'delete_private_' . $course_cap );
189 - $admin->add_cap( 'edit_private_' . $course_cap );
190 - $admin->add_cap( 'delete_others_' . $course_cap );
191 - $admin->add_cap( 'edit_others_' . $course_cap );
192 -
193 - $admin->add_cap( 'read_private_' . $lesson_cap );
194 - $admin->add_cap( 'delete_' . $lesson_cap );
195 - $admin->add_cap( 'delete_published_' . $lesson_cap );
196 - $admin->add_cap( 'edit_' . $lesson_cap );
197 - $admin->add_cap( 'edit_published_' . $lesson_cap );
198 - $admin->add_cap( 'publish_' . $lesson_cap );
199 - $admin->add_cap( 'delete_private_' . $lesson_cap );
200 - $admin->add_cap( 'edit_private_' . $lesson_cap );
201 - $admin->add_cap( 'delete_others_' . $lesson_cap );
202 - $admin->add_cap( 'edit_others_' . $lesson_cap );
203 -
204 - $admin->add_cap( 'delete_' . $order_cap );
205 - $admin->add_cap( 'delete_published_' . $order_cap );
206 - $admin->add_cap( 'edit_' . $order_cap );
207 - $admin->add_cap( 'edit_published_' . $order_cap );
208 - $admin->add_cap( 'publish_' . $order_cap );
209 - $admin->add_cap( 'delete_private_' . $order_cap );
210 - $admin->add_cap( 'edit_private_' . $order_cap );
211 - $admin->add_cap( 'delete_others_' . $order_cap );
212 - $admin->add_cap( 'edit_others_' . $order_cap );
213 - }
214 147 }
215 148
216 -add_action( 'init', 'learn_press_add_user_roles' );
217 -
218 149 /**
219 - * @param null $user_id
220 - * @param array $args
221 - *
222 - * @return mixed
223 - * @deprecated 4.1.7.3
224 - */
225 -/*function learn_press_get_user_questions( $user_id = null, $args = array() ) {
226 - if ( ! $user_id ) {
227 - $user_id = get_current_user_id();
228 - }
229 -
230 - return learn_press_get_user( $user_id )->get_questions( $args );
231 -}*/
232 -
233 -/**
234 - * Get the type of current user
235 - *
236 - * @param null $check_type
237 - *
238 - * @return bool|string
239 - */
240 -function learn_press_current_user_is( $check_type = null ) {
241 - global $current_user;
242 - $user_roles = $current_user->roles;
243 - $user_type = '';
244 -
245 - if ( in_array( 'lpr_teacher', $user_roles ) ) {
246 - $user_type = 'instructor';
247 - } elseif ( in_array( 'lp_teacher', $user_roles ) ) {
248 - $user_type = 'instructor';
249 - } elseif ( in_array( 'administrator', $user_roles ) ) {
250 - $user_type = 'administrator';
251 - }
252 -
253 - return $check_type ? $check_type == $user_type : $user_type;
254 -}
255 -
256 -function learn_press_user_has_roles( $roles, $user_id = null ) {
257 - $has_role = false;
258 - if ( ! $user_id ) {
259 - $user = wp_get_current_user();
260 - } else {
261 - $user = get_user_by( 'id', $user_id );
262 - }
263 - $available_roles = (array) $user->roles;
264 - if ( is_array( $roles ) ) {
265 - foreach ( $roles as $role ) {
266 - if ( in_array( $role, $available_roles ) ) {
267 - $has_role = true;
268 - break; // only need one of roles is in available
269 - }
270 - }
271 - } else {
272 - if ( in_array( $roles, $available_roles ) ) {
273 - $has_role = true;
274 - }
275 - }
276 -
277 - return $has_role;
278 -}
279 -
280 -function learn_press_current_user_can_view_profile_section( $section, $user ) {
281 - $current_user = wp_get_current_user();
282 - $view = true;
283 - if ( $user->get_data( 'user_login' ) != $current_user->user_login && $section == LP_Settings::instance()->get(
284 - 'profile_endpoints.profile-orders',
285 - 'profile-orders'
286 - ) ) {
287 - $view = false;
288 - }
289 -
290 - return apply_filters( 'learn_press_current_user_can_view_profile_section', $view, $section, $user );
291 -}
292 -
293 -/*function learn_press_profile_tab_courses_content( $current, $tab, $user ) {
294 - learn_press_get_template(
295 - 'profile/tabs/courses.php',
296 - array(
297 - 'user' => $user,
298 - 'current' => $current,
299 - 'tab' => $tab,
300 - )
301 - );
302 -}*/
303 -
304 -function learn_press_profile_tab_quizzes_content( $current, $tab, $user ) {
305 - learn_press_get_template(
306 - 'profile/tabs/quizzes.php',
307 - array(
308 - 'user' => $user,
309 - 'current' => $current,
310 - 'tab' => $tab,
311 - )
312 - );
313 -}
314 -
315 -function learn_press_profile_tab_orders_content( $current, $tab, $user ) {
316 - learn_press_get_template(
317 - 'profile/tabs/orders.php',
318 - array(
319 - 'user' => $user,
320 - 'current' => $current,
321 - 'tab' => $tab,
322 - )
323 - );
324 -}
325 -
326 -/**
327 150 * Get queried user in profile link
328 151 *
329 152 * @return false|WP_User
330 153 * @since 3.0.0
154 + * @deprecated 4.2.9.1
331 155 */
332 156 function learn_press_get_profile_user() {
157 + return LP_Profile::instance()->get_user_current();
158 +
333 159 return LP_Profile::get_queried_user();
334 160 }
335 161
336 162
@@ -363,14 +189,14 @@
363 189 * ....
364 190 * field_name_n => value n
365 191 * )
366 192 * @param mixed $where - Optional. Fields with values for conditional update with the same format of $fields.
367 - * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0).
368 - * @param bool $update_extra_fields_as_meta - Optional. Update extra fields as item meta (since 3.1.0).
193 + * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0).
194 + * @param bool $update_extra_fields_as_meta - Optional. Update extra fields as item meta (since 3.1.0).
369 195 *
370 196 * @return mixed
371 197 */
372 -function learn_press_update_user_item_field( array $fields = [], $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) {
198 +function learn_press_update_user_item_field( array $fields = array(), $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) {
373 199 global $wpdb;
374 200
375 201 // Table fields format.
376 202 $table_fields = array(
@@ -397,12 +223,10 @@
397 223 if ( LP_COURSE_CPT === $item_type ) {
398 224 if ( 'completed' === $fields['status'] ) {
399 225 $fields['status'] = 'finished';
400 226 }
401 - } else {
402 - if ( 'finished' === $fields['status'] ) {
227 + } elseif ( 'finished' === $fields['status'] ) {
403 228 $fields['status'] = 'completed';
404 - }
405 229 }
406 230 }
407 231
408 232 $data = array();
@@ -411,9 +235,9 @@
411 235 if ( ! empty( $table_fields[ $field ] ) ) {
412 236 $data[ $field ] = $value;
413 237
414 238 // Do not format the date-time field if it's value is NULL
415 - if ( in_array( $field, [ 'start_time', 'end_time' ] ) && empty( $value ) ) {
239 + if ( in_array( $field, array( 'start_time', 'end_time' ) ) && empty( $value ) ) {
416 240 $data[ $field ] = null;
417 241 $data_format[] = '';
418 242 } else {
419 243 if ( $value instanceof LP_Datetime ) {
@@ -487,14 +311,52 @@
487 311 } elseif ( $updated ) {
488 312 $updated_item = learn_press_get_user_item( $where );
489 313 }
490 314
315 + // Clear caches.
316 + if ( $updated_item ) {
317 + // Clear cache total students enrolled.
318 + if ( isset( $updated_item->item_type )
319 + && LP_COURSE_CPT === $updated_item->item_type
320 + && isset( $updated_item->item_id ) ) {
321 + $lp_course_cache = new LP_Course_Cache( true );
322 + $lp_course_cache->clean_total_students_enrolled( $updated_item->item_id );
323 + $lp_course_cache->clean_total_students_enrolled_or_purchased( $updated_item->item_id );
324 + // Clear cache count students many courses.
325 + $lp_courses_cache = new LP_Courses_Cache( true );
326 + $lp_courses_cache->clear_cache_on_group( LP_Courses_Cache::KEYS_COUNT_STUDENT_COURSES );
327 + }
328 + // Clear cache user item.
329 + $lp_user_items_cache = new LP_User_Items_Cache();
330 + $lp_user_items_cache->clean_user_item(
331 + array(
332 + $updated_item->user_id,
333 + $updated_item->item_id,
334 + $updated_item->item_type,
335 + )
336 + );
337 + // Clear cache userItemModel
338 + $userItemModel = UserItemModel::find_user_item(
339 + $updated_item->user_id,
340 + $updated_item->item_id,
341 + $updated_item->item_type,
342 + $updated_item->ref_id,
343 + $updated_item->ref_type,
344 + true
345 + );
346 + $userItemModel->clean_caches();
347 + }
348 +
349 + do_action( 'learn-press/updated-user-item-field', $updated_item );
350 +
491 351 /**
492 352 * If there is some fields does not contain in the main table
493 353 * then consider update them as metadata.
354 + *
494 355 * @comment by tungnx - 4.1.7.3
495 356 */
496 - /*if ( $updated_item && $update_extra_fields_as_meta ) {
357 + /*
358 + if ( $updated_item && $update_extra_fields_as_meta ) {
497 359 $extra_fields = array_diff_key( $fields, $table_fields );
498 360 if ( $extra_fields ) {
499 361 foreach ( $extra_fields as $meta_key => $meta_value ) {
500 362 if ( $meta_value == 'user_item_id' ) {
@@ -513,9 +375,9 @@
513 375 }
514 376 }
515 377 }*/
516 378
517 - do_action( 'learn-press/updated-user-item-meta', $updated_item );
379 + // do_action( 'learn-press/updated-user-item-meta', $updated_item );
518 380
519 381 return $updated_item;
520 382 }
521 383
@@ -522,9 +384,9 @@
522 384 /**
523 385 * Get user item row(s) from user items table by multiple WHERE conditional
524 386 *
525 387 * @param array|int $where
526 - * @param bool $single
388 + * @param bool $single
527 389 *
528 390 * @return array
529 391 */
530 392 function learn_press_get_user_item( $where, $single = true ) {
@@ -579,11 +441,11 @@
579 441
580 442 /**
581 443 * Get user item meta from user_itemmeta table
582 444 *
583 - * @param int $user_item_id .
445 + * @param int $user_item_id .
584 446 * @param string $meta_key .
585 - * @param bool $single .
447 + * @param bool $single .
586 448 *
587 449 * @return mixed
588 450 */
589 451 function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) {
@@ -597,11 +459,11 @@
597 459
598 460 /**
599 461 * Add user item meta into table user_itemmeta
600 462 *
601 - * @param int $user_item_id
463 + * @param int $user_item_id
602 464 * @param string $meta_key
603 - * @param mixed $meta_value
465 + * @param mixed $meta_value
604 466 * @param string $prev_value
605 467 *
606 468 * @return false|int
607 469 */
@@ -611,11 +473,11 @@
611 473
612 474 /**
613 475 * Update user item meta to table user_itemmeta
614 476 *
615 - * @param int $user_item_id
477 + * @param int $user_item_id
616 478 * @param string $meta_key
617 - * @param mixed $meta_value
479 + * @param mixed $meta_value
618 480 * @param string $prev_value
619 481 *
620 482 * @return bool|int
621 483 */
@@ -626,12 +488,12 @@
626 488
627 489 /**
628 490 * Update user item meta to table user_itemmeta
629 491 *
630 - * @param int $object_id
492 + * @param int $object_id
631 493 * @param string $meta_key
632 - * @param mixed $meta_value
633 - * @param bool $delete_all
494 + * @param mixed $meta_value
495 + * @param bool $delete_all
634 496 *
635 497 * @return bool|int
636 498 */
637 499 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
@@ -637,101 +499,8 @@
637 499 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
638 500 return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all );
639 501 }
640 502
641 -/**
642 - * Get temp users.
643 - *
644 - * @return array
645 - * @deprecated 4.1.6.9
646 - */
647 -/*function learn_press_get_temp_users() {
648 - return false;
649 - if ( false === ( $temp_users = LP_Object_Cache::get( 'learn-press/temp-users' ) ) ) {
650 - global $wpdb;
651 - $query = $wpdb->prepare(
652 - "
653 - SELECT ID
654 - FROM {$wpdb->users} u
655 - INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id AND um.meta_key = %s AND um.meta_value = %s
656 - LEFT JOIN {$wpdb->usermeta} um2 ON u.ID = um2.user_id AND um2.meta_key = %s
657 - ",
658 - '_lp_temp_user',
659 - 'yes',
660 - '_lp_expiration'
661 - );
662 -
663 - $temp_users = $wpdb->get_col( $query );
664 -
665 - LP_Object_Cache::set( 'learn-press/temp-users', $temp_users );
666 - }
667 -
668 - return $temp_users;
669 -}*/
670 -
671 -/**
672 - * Update field created_time after added user item meta
673 - *
674 - * @use updated_{meta_type}_meta hook
675 - *
676 - * @param $meta_id
677 - * @param $object_id
678 - * @param $meta_key
679 - * @param $_meta_value
680 - * @deprecated 4.1.7.3
681 - */
682 -/*function _learn_press_update_created_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
683 - global $wpdb;
684 - $wpdb->update(
685 - $wpdb->learnpress_user_itemmeta,
686 - array( 'create_time' => current_time( 'mysql' ) ),
687 - array( 'meta_id' => $meta_id ),
688 - array( '%s' ),
689 - array( '%d' )
690 - );
691 -}*/
692 -
693 -// add_action( 'added_learnpress_user_item_meta', '_learn_press_update_created_time_user_item_meta', 10, 4 );
694 -
695 -/**
696 - * Update field updated_time after updated user item meta
697 - *
698 - * @use updated_{meta_type}_meta hook
699 - *
700 - * @param $meta_id
701 - * @param $object_id
702 - * @param $meta_key
703 - * @param $_meta_value
704 - * @deprecated
705 - */
706 -/*function _learn_press_update_updated_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
707 - global $wpdb;
708 - $wpdb->update(
709 - $wpdb->learnpress_user_itemmeta,
710 - array( 'update_time' => current_time( 'mysql' ) ),
711 - array( 'meta_id' => $meta_id ),
712 - array( '%s' ),
713 - array( '%d' )
714 - );
715 -}*/
716 -
717 -// add_action( 'updated_learnpress_user_item_meta', '_learn_press_update_updated_time_user_item_meta', 10, 4 );
718 -
719 -/**
720 - * @param $status
721 - * @param int $quiz_id
722 - * @param int $user_id
723 - * @param int $course_id
724 - *
725 - * @return bool|mixed
726 - * @deprecated 4.1.7.3
727 - */
728 -/*function learn_press_user_has_quiz_status( $status, $quiz_id = 0, $user_id = 0, $course_id = 0 ) {
729 - $user = learn_press_get_user( $user_id );
730 -
731 - return $user->has_quiz_status( $status, $quiz_id, $course_id );
732 -}*/
733 -
734 503 if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) {
735 504 /**
736 505 * Filter the avatar
737 506 *
@@ -736,109 +505,105 @@
736 505 * Filter the avatar
737 506 *
738 507 * @param string $avatar
739 508 * @param string $id_or_email
740 - * @param array $size
509 + * @param array $args
741 510 *
742 511 * @return string
512 + * @since 1.0.0
513 + * @version 1.0.2
743 514 */
744 - function learn_press_pre_get_avatar_callback( $avatar, $id_or_email = '', $size = array() ) {
515 + function learn_press_pre_get_avatar_callback( $avatar, $id_or_email = '', $args = array() ) {
516 + try {
517 + if ( ( isset( $args['gravatar'] ) && $args['gravatar'] ) || ( $args['default'] && $args['force_default'] ) ) {
518 + return $avatar;
519 + }
745 520
746 - $profile = LP_Profile::instance();
521 + $user_id = 0;
747 522
748 - if ( ! $profile->is_enable_avatar() ) {
749 - return $avatar;
750 - }
523 + /**
524 + * Get the ID of user from $id_or_email
525 + */
526 + if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) {
527 + $user = get_user_by( 'email', $id_or_email );
528 + if ( $user ) {
529 + $user_id = $user->ID;
530 + }
531 + } elseif ( is_numeric( $id_or_email ) ) {
532 + $user_id = $id_or_email;
533 + } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) {
534 + $user_id = $id_or_email->user_id;
535 + } elseif ( $id_or_email instanceof WP_Comment ) {
536 + $user = get_user_by( 'email', $id_or_email->comment_author_email );
537 + if ( $user ) {
538 + $user_id = $user->ID;
539 + }
540 + }
751 541
752 - if ( ( isset( $size['gravatar'] ) && $size['gravatar'] ) || ( $size['default'] && $size['force_default'] ) ) {
753 - return $avatar;
754 - }
542 + if ( ! $user_id ) {
543 + return $avatar;
544 + }
755 545
756 - $user_id = 0;
757 -
758 - /**
759 - * Get the ID of user from $id_or_email
760 - */
761 - if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) {
762 - $user = get_user_by( 'email', $id_or_email );
763 - if ( $user ) {
764 - $user_id = $user->ID;
546 + $user = UserModel::find( $user_id, true );
547 + if ( ! $user ) {
548 + return $avatar;
765 549 }
766 - } elseif ( is_numeric( $id_or_email ) ) {
767 - $user_id = $id_or_email;
768 - } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) {
769 - $user_id = $id_or_email->user_id;
770 - } elseif ( $id_or_email instanceof WP_Comment ) {
771 - $user = get_user_by( 'email', $id_or_email->comment_author_email );
772 - if ( $user ) {
773 - $user_id = $user->ID;
774 - }
775 - }
776 550
777 - if ( ! $user_id ) {
778 - return $avatar;
779 - }
780 -
781 - $user = LP_User_Factory::get_user( $user_id );
782 -
783 - $profile_picture_src = $user->get_upload_profile_src();
784 - if ( $profile_picture_src ) {
785 - $setting_size = learn_press_get_avatar_thumb_size();
786 - $img_size = '';
787 -
788 - // Get avatar size
789 - if ( ! is_array( $size ) ) {
790 - if ( $size === 'thumbnail' ) {
791 - $img_size = '';
792 - $height = $setting_size['height'];
793 - $width = $setting_size['width'];
551 + $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
552 + if ( $args['class'] ) {
553 + if ( is_array( $args['class'] ) ) {
554 + $class = array_merge( $class, $args['class'] );
794 555 } else {
795 - $height = 250;
796 - $width = 250;
556 + $class[] = $args['class'];
797 557 }
798 - } else {
799 - $img_size = $size['size'];
800 - $height = $size['height'];
801 - $width = $size['width'];
802 558 }
803 559
804 - $avatar = '<img alt="' . esc_attr( $user->get_data( 'display_name' ) ) . '" src="' . esc_url_raw( $profile_picture_src ) . '" class="avatar avatar-' . $img_size . ' photo" height="' . $height . '" width="' . $width . '" />';
560 + $avatar_url = $user->get_avatar_url();
561 + $html_img = sprintf(
562 + "<img src='%s' srcset='%s' class='%s' height='%d' width='%d' decoding='async'/>",
563 + esc_url( $avatar_url ),
564 + esc_url( $avatar_url ) . ' 2x',
565 + esc_attr( implode( ' ', $class ) ),
566 + (int) $args['height'],
567 + (int) $args['width']
568 + );
569 +
570 + return $html_img;
571 + } catch ( Throwable $e ) {
572 + error_log( $e->getMessage() );
573 +
574 + return $avatar;
805 575 }
806 -
807 - return $avatar;
808 576 }
809 577 }
810 -add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 1, 5 );
578 +add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 999, 3 );
811 579
812 580
813 581 function learn_press_user_profile_picture_upload_dir( $width_user = true ) {
814 - static $upload_dir;
815 - if ( ! $upload_dir ) {
816 - $upload_dir = wp_upload_dir();
817 - $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user );
818 - if ( $width_user ) {
819 - $subdir .= '/' . get_current_user_id();
820 - }
821 - $subdir = '/' . $subdir;
582 + $upload_dir = wp_upload_dir();
583 + $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user );
584 + if ( $width_user ) {
585 + $subdir .= '/' . get_current_user_id();
586 + }
587 + $subdir = '/' . $subdir;
822 588
823 - if ( ! empty( $upload_dir['subdir'] ) ) {
824 - $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
825 - $u_path = str_replace( '\\', '/', $upload_dir['path'] );
589 + if ( ! empty( $upload_dir['subdir'] ) ) {
590 + $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
591 + $u_path = str_replace( '\\', '/', $upload_dir['path'] );
826 592
827 - $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path );
828 - $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] );
829 - } else {
830 - $upload_dir['path'] = $upload_dir['path'] . $subdir;
831 - $upload_dir['url'] = $upload_dir['url'] . $subdir;
832 - }
593 + $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path );
594 + $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] );
595 + } else {
596 + $upload_dir['path'] = $upload_dir['path'] . $subdir;
597 + $upload_dir['url'] = $upload_dir['url'] . $subdir;
598 + }
833 599
834 - $upload_dir['subdir'] = $subdir;
600 + $upload_dir['subdir'] = $subdir;
835 601
836 - // Point path/url to main site if we are in multisite
837 - if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
838 - foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) {
839 - $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] );
840 - }
602 + // Point path/url to main site if we are in multisite
603 + if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
604 + foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) {
605 + $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] );
841 606 }
842 607 }
843 608
844 609 return $upload_dir;
@@ -843,8 +608,9 @@
843 608
844 609 return $upload_dir;
845 610 }
846 611
612 +/*
847 613 add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 );
848 614 function _learn_press_before_purchase_course_handler( $course_id, $cart ) {
849 615 // Redirect to login page if user is not logged in
850 616 if ( ! is_user_logged_in() ) {
@@ -883,10 +649,11 @@
883 649 wp_redirect( get_the_permalink( $course_id ) );
884 650 exit();
885 651 }
886 652 }
887 -}
653 +}*/
888 654
655 +/*
889 656 function learn_press_user_is( $role, $user_id = 0 ) {
890 657 if ( ! $user_id ) {
891 658 $user = learn_press_get_current_user();
892 659 } else {
@@ -899,10 +666,11 @@
899 666 return $user->is_instructor();
900 667 }
901 668
902 669 return $role;
903 -}
670 +}*/
904 671
672 +/*
905 673 function learn_press_profile_tab_edit_content( $current, $tab, $user ) {
906 674 learn_press_get_template(
907 675 'profile/tabs/edit.php',
908 676 array(
@@ -910,25 +678,11 @@
910 678 'current' => $current,
911 679 'tab' => $tab,
912 680 )
913 681 );
914 -}
682 +}*/
915 683
916 -function learn_press_get_profile_endpoints() {
917 - $endpoints = (array) LP_Settings::instance()->get( 'profile_endpoints' );
918 - $tabs = LP_Profile::instance()->get_tabs();
919 - if ( $tabs ) {
920 - foreach ( $tabs as $slug => $info ) {
921 - if ( empty( $endpoints[ $slug ] ) ) {
922 - $endpoints[ $slug ] = $slug;
923 - }
924 - }
925 - }
926 -
927 - return apply_filters( 'learn_press_profile_tab_endpoints', $endpoints );
928 -}
929 -
930 -
684 +/*
931 685 function learn_press_update_user_option( $name, $value, $id = 0 ) {
932 686 if ( ! $id ) {
933 687 $id = get_current_user_id();
934 688 }
@@ -935,16 +689,11 @@
935 689 $key = 'learnpress_user_options';
936 690 $options = get_user_option( $key, $id );
937 691 $options[ $name ] = $value;
938 692 update_user_option( $id, $key, $options, true );
939 -}
693 +}*/
940 694
941 -/**
942 - * @param $name
943 - * @param int $id
944 - *
945 - * @return bool
946 - */
695 +/*
947 696 function learn_press_delete_user_option( $name, $id = 0 ) {
948 697 if ( ! $id ) {
949 698 $id = get_current_user_id();
950 699 }
@@ -957,16 +706,11 @@
957 706 return true;
958 707 }
959 708
960 709 return false;
961 -}
710 +}*/
962 711
963 -/**
964 - * @param $name
965 - * @param int $id
966 - *
967 - * @return bool
968 - */
712 +/*
969 713 function learn_press_get_user_option( $name, $id = 0 ) {
970 714 if ( ! $id ) {
971 715 $id = get_current_user_id();
972 716 }
@@ -976,9 +720,9 @@
976 720 return $options[ $name ];
977 721 }
978 722
979 723 return false;
980 -}
724 +}*/
981 725
982 726 /**
983 727 * Update user basic information.
984 728 *
@@ -994,13 +738,13 @@
994 738 }
995 739
996 740 $update_data = array(
997 741 'ID' => $user_id,
998 - 'first_name' => filter_input( INPUT_POST, 'first_name', FILTER_SANITIZE_STRING ),
999 - 'last_name' => filter_input( INPUT_POST, 'last_name', FILTER_SANITIZE_STRING ),
1000 - 'description' => filter_input( INPUT_POST, 'description', FILTER_SANITIZE_STRING ),
1001 - 'display_name' => filter_input( INPUT_POST, 'account_display_name', FILTER_SANITIZE_STRING ),
1002 - 'user_email' => filter_input( INPUT_POST, 'account_email', FILTER_SANITIZE_EMAIL ),
742 + 'first_name' => LP_Request::get_param( 'first_name' ),
743 + 'last_name' => LP_Request::get_param( 'last_name' ),
744 + 'description' => LP_Request::get_param( 'description', '', 'html' ),
745 + 'display_name' => LP_Request::get_param( 'account_display_name' ),
746 + 'user_email' => LP_Request::get_param( 'account_email' ),
1003 747 );
1004 748
1005 749 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
1006 750 $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' );
@@ -1007,9 +751,9 @@
1007 751
1008 752 $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta );
1009 753
1010 754 // Update for social.
1011 - $socials = LP_Request::get_array( 'user_profile_social' );
755 + $socials = LP_Request::get_param( 'user_profile_social' );
1012 756 $extra_data = get_user_meta( $user_id, '_lp_extra_info', true );
1013 757
1014 758 if ( ! empty( $extra_data ) ) {
1015 759 $socials = array_merge( $extra_data, $socials );
@@ -1025,55 +769,51 @@
1025 769 }
1026 770
1027 771 /**
1028 772 * Update new password.
773 + *
774 + * @version 1.0.1
1029 775 */
1030 -function learn_press_update_user_profile_change_password( $wp_error = false ) {
1031 - $user_id = get_current_user_id();
1032 -
1033 - if ( ! $user_id ) {
1034 - return new WP_Error( 2, 'The user is invalid' );
1035 - }
1036 -
1037 - $old_pass = filter_input( INPUT_POST, 'pass0' );
1038 - $check_old_pass = false;
1039 -
1040 - if ( $old_pass ) {
776 +function learn_press_update_user_profile_change_password() {
777 + try {
1041 778 $user = wp_get_current_user();
1042 - require_once ABSPATH . 'wp-includes/class-phpass.php';
1043 - $wp_hasher = new PasswordHash( 8, true );
779 + if ( ! $user ) {
780 + throw new Exception( __( 'The user is invalid', 'learnpress' ) );
781 + }
1044 782
1045 - if ( $wp_hasher->CheckPassword( $old_pass, $user->data->user_pass ) ) {
1046 - $check_old_pass = true;
783 + $old_pass = LP_Request::get_param( 'pass0', '', 'text', 'post' );
784 + if ( empty( $old_pass ) ) {
785 + throw new Exception( __( 'Enter the old password!', 'learnpress' ) );
1047 786 }
1048 - }
1049 787
1050 - try {
788 + $check_old_pass = wp_check_password( $old_pass, $user->data->user_pass, $user->ID );
1051 789 if ( ! $check_old_pass ) {
1052 790 throw new Exception( __( 'The old password is incorrect!', 'learnpress' ) );
1053 - } else {
1054 - $new_pass = filter_input( INPUT_POST, 'pass1' );
1055 - $new_pass2 = filter_input( INPUT_POST, 'pass2' );
791 + }
1056 792
1057 - if ( ! $new_pass || ! $new_pass2 || ( $new_pass != $new_pass2 ) ) {
1058 - throw new Exception( __( 'Incorrect confirmation password!', 'learnpress' ) );
1059 - } else {
1060 - $update_data = array(
1061 - 'user_pass' => $new_pass,
1062 - 'ID' => get_current_user_id(),
1063 - );
1064 - $return = wp_update_user( $update_data );
793 + $new_pass = LP_Request::get_param( 'pass1', '', 'text', 'post' );
794 + $new_pass_confirm = LP_Request::get_param( 'pass2', '', 'text', 'post' );
795 + if ( empty( $new_pass ) || empty( $new_pass_confirm )
796 + || $new_pass != $new_pass_confirm ) {
797 + throw new Exception( __( 'Incorrect confirmation password!', 'learnpress' ) );
798 + }
1065 799
1066 - if ( is_wp_error( $return ) ) {
1067 - return $wp_error ? $return : false;
1068 - }
800 + // Update user password
801 + // wp_set_password( $new_pass, $user->ID );
802 + $update_data = array(
803 + 'user_pass' => $new_pass,
804 + 'ID' => $user->ID,
805 + );
1069 806
1070 - return $return;
1071 - }
807 + $rs = wp_update_user( $update_data );
808 + if ( is_wp_error( $rs ) ) {
809 + throw new Exception( $rs->get_error_message() );
1072 810 }
1073 811 } catch ( Exception $ex ) {
1074 - return $wp_error ? new WP_Error( 'UPDATE_PROFILE_ERROR', $ex->getMessage() ) : false;
812 + return new WP_Error( 'error_lp_profile_update_pass', $ex->getMessage() );
1075 813 }
814 +
815 + return true;
1076 816 }
1077 817
1078 818 function learn_press_get_avatar_thumb_size() {
1079 819 $option = LP_Settings::get_option(
@@ -1090,8 +830,11 @@
1090 830 'height' => 250,
1091 831 );
1092 832 }
1093 833
834 + // For option get_avatar_url
835 + $option['size'] = $option['width'];
836 +
1094 837 return $option;
1095 838 }
1096 839
1097 840 function learn_press_get_course_thumbnail_dimensions() {
@@ -1112,36 +855,14 @@
1112 855
1113 856 return $option;
1114 857 }
1115 858
1116 -/**
1117 - * Set a fake cookie to
1118 - * @deprecated 4.2.0
1119 - */
1120 -function learn_press_set_user_cookie_for_guest() {
1121 - _deprecated_function( __METHOD__, '4.2.0' );
1122 - if ( ! is_admin() && ! headers_sent() ) {
1123 - $guest_key = '_wordpress_lp_guest';
1124 -
1125 - if ( is_user_logged_in() ) {
1126 - if ( ! empty( $_COOKIE[ $guest_key ] ) ) {
1127 - learn_press_remove_cookie( $guest_key );
1128 - }
1129 - } else {
1130 - if ( empty( $_COOKIE[ $guest_key ] ) ) {
1131 - learn_press_setcookie( $guest_key, md5( time() ), time() + 3600 );
1132 - }
1133 - }
1134 - }
1135 -}
1136 -
1137 -//add_action( 'wp', 'learn_press_set_user_cookie_for_guest' );
1138 -
859 +/*
1139 860 function learn_press_get_user_avatar( $user_id = 0, $size = '' ) {
1140 861 $user = learn_press_get_user( $user_id );
1141 862
1142 863 return $user->get_profile_picture( '', $size );
1143 -}
864 +}*/
1144 865
1145 866 /**
1146 867 * Get profile instance for an user to view.
1147 868 *
@@ -1152,16 +873,9 @@
1152 873 function learn_press_get_profile( $for_user = 0 ) {
1153 874 return LP_Profile::instance( $for_user );
1154 875 }
1155 876
1156 -/**
1157 - * Remove items from learnpress_user_items.
1158 - *
1159 - * @param int $user_id
1160 - * @param int $item_id
1161 - * @param int $course_id
1162 - * @param bool $include_course - Optional. If TRUE then remove course and it's items
1163 - */
877 +/*
1164 878 function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) {
1165 879 global $wpdb;
1166 880
1167 881 settype( $item_id, 'array' );
@@ -1184,44 +898,30 @@
1184 898 }
1185 899
1186 900 $query = $wpdb->prepare(
1187 901 "
1188 - DELETE
1189 - FROM {$wpdb->learnpress_user_items}
1190 - WHERE user_id = %d
1191 - AND ( item_id IN(" . join( ',', $format ) . ")
1192 - $where )
1193 - ",
902 + DELETE
903 + FROM {$wpdb->learnpress_user_items}
904 + WHERE user_id = %d
905 + AND ( item_id IN(" . join( ',', $format ) . ")
906 + $where )
907 + ",
1194 908 $args
1195 909 );
1196 -}
910 +}*/
1197 911
1198 912 /**
1199 913 * Get user profile link
1200 914 *
1201 - * @param int $user_id
915 + * @param int $user_id
1202 916 * @param null $tab
1203 917 *
1204 918 * @return mixed|string
1205 919 */
1206 -function learn_press_user_profile_link( $user_id = 0, $tab = null ) {
920 +function learn_press_user_profile_link( $user_id = 0, $tab = '' ) {
1207 921 if ( ! $user_id ) {
1208 922 $user_id = get_current_user_id();
1209 923 }
1210 - $user = false;
1211 - $deleted = in_array( $user_id, LP_User_Factory::$_deleted_users );
1212 - if ( ! $deleted ) {
1213 - if ( is_numeric( $user_id ) ) {
1214 - $user = get_user_by( 'id', $user_id );
1215 - } else {
1216 - $user = get_user_by( 'login', urldecode( $user_id ) );
1217 - }
1218 - } else {
1219 - return '';
1220 - }
1221 - if ( ! $deleted && ! $user ) {
1222 - LP_User_Factory::$_deleted_users[] = $user_id;
1223 - }
1224 924
1225 925 $user = learn_press_get_user( $user_id );
1226 926
1227 927 if ( ! $user ) {
@@ -1228,38 +928,33 @@
1228 928 return '';
1229 929 }
1230 930
1231 931 global $wp_query;
1232 - $args = array(
1233 - 'user' => $user->get_username(),
932 + $wp_user = get_userdata( $user_id );
933 + if ( ! $wp_user instanceof WP_User ) {
934 + return '';
935 + }
936 +
937 + $user_model = new UserModel( $wp_user->data );
938 + $args = array(
939 + 'user' => $user_model->get_slug_link(),
1234 940 );
1235 941
1236 - if ( isset( $args['user'] ) ) {
1237 - if ( '' === $tab ) {
1238 - $tab = learn_press_get_current_profile_tab();
1239 - }
1240 - if ( $tab ) {
1241 - $args['tab'] = $tab;
1242 - }
942 + if ( $tab ) {
943 + $args['tab'] = $tab;
944 + }
1243 945
1244 - /**
1245 - * If no tab is selected in profile and is current user
1246 - * then no need the username in profile link.
1247 - */
1248 - if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) {
1249 - unset( $args['user'] );
1250 - }
946 + /**
947 + * If no tab is selected in profile and is current user
948 + * then no need the username in profile link.
949 + */
950 + if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) {
951 + //unset( $args['user'] );
1251 952 }
1252 953
1253 - /*$args = array_map(
1254 - function ( $string ) {
1255 - return preg_replace( '/\s/', '+', $string );
1256 - },
1257 - $args
1258 - );*/
1259 954 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
1260 955 if ( $profile_link ) {
1261 - if ( get_option( 'permalink_structure' ) /*&& learn_press_get_page_id( 'profile' )*/ ) {
956 + if ( get_option( 'permalink_structure' ) ) {
1262 957 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
1263 958 } else {
1264 959 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
1265 960 }
@@ -1269,72 +964,9 @@
1269 964
1270 965 return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab );
1271 966 }
1272 967
1273 -/**********************************************/
1274 -/* Functions are used for hooks */
1275 -/**********************************************/
1276 -
1277 -function learn_press_hk_before_start_quiz( $true, $quiz_id, $course_id, $user_id ) {
1278 - if ( 'yes' !== get_post_meta( $quiz_id, '_lp_archive_history', true ) ) {
1279 - learn_press_remove_user_items( $user_id, $quiz_id, $course_id );
1280 - }
1281 -
1282 - return $true;
1283 -}
1284 -
1285 -add_filter( 'learn-press/before-start-quiz', 'learn_press_hk_before_start_quiz', 10, 4 );
1286 -
1287 -/*function learn_press_default_user_item_status( $item_id ) {
1288 - $status = '';
1289 - switch ( learn_press_get_post_type( $item_id ) ) {
1290 - case LP_LESSON_CPT:
1291 - $status = 'started';
1292 - break;
1293 - case LP_QUIZ_CPT:
1294 - $status = 'viewed';
1295 - break;
1296 - case LP_COURSE_CPT:
1297 - $status = 'enrolled';
1298 - }
1299 -
1300 - return apply_filters( 'learn-press/default-user-item-status', $status, $item_id );
1301 -}*/
1302 -
1303 -/**
1304 - * Get current state of distraction mode
1305 - *
1306 - * @return mixed
1307 - * @since 3.1.0
1308 - * @deprecated 4.2.0
1309 - */
1310 -function learn_press_get_user_distraction() {
1311 - _deprecated_function( __FUNCTION__, '4.2.0' );
1312 - if ( is_user_logged_in() ) {
1313 - return get_user_option( 'distraction_mode', get_current_user_id() );
1314 - } else {
1315 - return LearnPress::instance()->session->distraction_mode;
1316 - }
1317 -}
1318 -
1319 -/**
1320 - * @deprecated 4.2.0
1321 - */
1322 -function learn_press_get_user_role( $user_id ) {
1323 - _deprecated_function( __FUNCTION__, '4.2.0' );
1324 - if ( $user = learn_press_get_user( $user_id ) ) {
1325 - return $user->get_role();
1326 - }
1327 -
1328 - return false;
1329 -}
1330 -
1331 -/**
1332 - * @param array $args
1333 - * @param bool $wp_error
1334 - *
1335 - * @return bool|int|LP_User_Item|mixed|WP_Error
1336 - */
968 +/*
1337 969 function learn_press_create_user_item( $args = array(), $wp_error = false ) {
1338 970 global $wpdb;
1339 971
1340 972 $defaults = array(
@@ -1430,44 +1062,11 @@
1430 1062
1431 1063 do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta );
1432 1064
1433 1065 return $user_item;
1434 -}
1066 +}*/
1435 1067
1436 -/**
1437 - * @param array $args
1438 - * @param bool $wp_error - Optional. TRUE will return WP_Error on fail.
1439 - *
1440 - * @return bool|array|LP_User_Item|WP_Error
1441 - */
1442 -function learn_press_create_user_item_for_quiz( $args = array(), $wp_error = false ) {
1443 - $item_data = wp_parse_args(
1444 - $args,
1445 - array(
1446 - 'item_type' => LP_QUIZ_CPT,
1447 - 'status' => LP_ITEM_STARTED,
1448 - 'graduation' => LP_COURSE_GRADUATION_IN_PROGRESS,
1449 - 'user_id' => get_current_user_id(),
1450 - )
1451 - );
1452 -
1453 - $user_item = learn_press_create_user_item( $item_data, $wp_error );
1454 -
1455 - if ( $user_item && ! is_wp_error( $user_item ) ) {
1456 - $user_item = new LP_User_Item_Quiz( $user_item->get_data() );
1457 - $user_item->update();
1458 - }
1459 -
1460 - return $user_item;
1461 -}
1462 -
1463 -/**
1464 - * Get list user_item_id for Quiz in table learnpress_user_items
1465 - *
1466 - * @param int $quiz_id
1467 - * @param int $course_id
1468 - * @return array || false
1469 - */
1068 +/*
1470 1069 function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) {
1471 1070 global $wpdb;
1472 1071
1473 1072 $query = $wpdb->prepare( "SELECT user_item_id FROM $wpdb->learnpress_user_items WHERE ref_id=%d AND item_id=%d", $course_id, $quiz_id );
@@ -1477,161 +1076,11 @@
1477 1076 return $col;
1478 1077 } else {
1479 1078 return false;
1480 1079 }
1481 -}
1080 +}*/
1482 1081
1483 1082 /**
1484 - * Create new user item prepare for user starts a quiz
1485 - * Update error retry course not work - Nhamdv.
1486 - *
1487 - * @param int $quiz_id
1488 - * @param int $user_id
1489 - * @param int $course_id
1490 - * @param bool $wp_error
1491 - *
1492 - * @return array|bool|LP_User_Item|WP_Error
1493 - * @since 4.0.0
1494 - */
1495 -function learn_press_user_start_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) {
1496 - if ( ! $user_id ) {
1497 - $user_id = get_current_user_id();
1498 - }
1499 -
1500 - global $wpdb;
1501 -
1502 - $query = $wpdb->prepare(
1503 - "
1504 - SELECT user_item_id, item_id id, item_type type
1505 - FROM {$wpdb->learnpress_user_items}
1506 - WHERE user_item_id = (SELECT max(user_item_id)
1507 - FROM {$wpdb->learnpress_user_items}
1508 - WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress'))
1509 - ",
1510 - $user_id,
1511 - $course_id
1512 - );
1513 -
1514 - $parent = $wpdb->get_row( $query );
1515 -
1516 - do_action( 'learn-press/before-user-start-quiz', $quiz_id, $user_id, $course_id );
1517 -
1518 - $user = learn_press_get_user( $user_id );
1519 - $course_data = $user->get_course_data( $course_id );
1520 - $quiz_data = $course_data->get_item( $quiz_id );
1521 -
1522 - $quiz = LP_Quiz::get_quiz( $quiz_id );
1523 - $duration = $quiz->get_duration();
1524 - $user_quiz = learn_press_create_user_item_for_quiz(
1525 - array(
1526 - 'user_item_id' => $quiz_data ? $quiz_data->get_user_item_id() : 0,
1527 - 'item_id' => $quiz->get_id(),
1528 - 'duration' => $duration ? $duration->get() : 0,
1529 - 'user_id' => $user_id,
1530 - 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0,
1531 - 'ref_type' => $parent ? $parent->type : '',
1532 - 'ref_id' => $parent ? $parent->id : '',
1533 - ),
1534 - $wp_error
1535 - );
1536 -
1537 - if ( $user_quiz && ! is_wp_error( $user_quiz ) ) {
1538 - do_action( 'learn-press/user-started-quiz', $user_quiz, $quiz_id, $user_id, $course_id );
1539 - }
1540 -
1541 - // Reset first cache
1542 - //$user_quiz->get_status( 'status', true );
1543 -
1544 - return $user_quiz;
1545 -}
1546 -
1547 -/**
1548 - * Function retake quiz.
1549 - *
1550 - * @param [type] $quiz_id
1551 - * @param integer $user_id
1552 - * @param integer $course_id
1553 - * @param boolean $wp_error
1554 - *
1555 - * @return void
1556 - */
1557 -function learn_press_user_retake_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) {
1558 - if ( ! $user_id ) {
1559 - $user_id = get_current_user_id();
1560 - }
1561 -
1562 - if ( ! $course_id ) {
1563 - return new WP_Error( 'invalid_course_id', esc_html__( 'Invalid Course ID.', 'learnpress' ) );
1564 - }
1565 -
1566 - global $wpdb;
1567 -
1568 - $query = $wpdb->prepare(
1569 - "
1570 - SELECT user_item_id, item_id id, item_type type
1571 - FROM {$wpdb->learnpress_user_items}
1572 - WHERE user_item_id = (SELECT max(user_item_id)
1573 - FROM {$wpdb->learnpress_user_items}
1574 - WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress'))
1575 - ",
1576 - $user_id,
1577 - $course_id
1578 - );
1579 -
1580 - $parent = $wpdb->get_row( $query );
1581 -
1582 - if ( ! $parent ) {
1583 - return new WP_Error( 'invalid_user_item', esc_html__( 'Invalid Quiz', 'learnpress' ) );
1584 - }
1585 -
1586 - $data = learn_press_get_user_item(
1587 - array(
1588 - 'item_id' => $quiz_id,
1589 - 'user_id' => $user_id,
1590 - 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0,
1591 - 'ref_type' => $parent ? $parent->type : LP_COURSE_CPT,
1592 - 'ref_id' => $parent ? $parent->id : '',
1593 - )
1594 - );
1595 -
1596 - $user_item = new LP_User_Item_Quiz( $data );
1597 -
1598 - $ratake_count = get_post_meta( $quiz_id, '_lp_retake_count', true );
1599 -
1600 - if ( $ratake_count > 0 ) {
1601 - $user_item->update_retake_count();
1602 - }
1603 -
1604 - // Create new result in table learnpress_user_item_results.
1605 - LP_User_Items_Result_DB::instance()->insert( $data->user_item_id );
1606 -
1607 - // Remove user_item_meta.
1608 - learn_press_delete_user_item_meta( $data->user_item_id, '_lp_question_checked' );
1609 -
1610 - $user_item->set_status( LP_ITEM_STARTED )
1611 - ->set_start_time( time() ) // Error Retake when change timezone - Nhamdv
1612 - ->set_end_time()
1613 - ->set_graduation( LP_COURSE_GRADUATION_IN_PROGRESS )
1614 - ->update();
1615 -
1616 - // Reset first cache
1617 - //$user_item->get_status( 'status', true );
1618 -
1619 - // Error Retake when change timezone - Nhamdv
1620 - // learn_press_update_user_item_field(
1621 - // array(
1622 - // 'start_time' => current_time( 'mysql', true ),
1623 - // ),
1624 - // array(
1625 - // 'user_item_id' => $data->user_item_id,
1626 - // )
1627 - // );
1628 -
1629 - return $user_item;
1630 -}
1631 -
1632 -
1633 -/**
1634 1083 * Prepares list of questions for rest api.
1635 1084 *
1636 1085 * @param int[] $question_ids
1637 1086 * @param array $args
@@ -1638,9 +1087,9 @@
1638 1087 *
1639 1088 * @return array
1640 1089 * @since 3.3.0
1641 1090 */
1642 -function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ) : array {
1091 +function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ): array {
1643 1092 if ( is_numeric( $args ) ) {
1644 1093
1645 1094 } else {
1646 1095 $args = wp_parse_args(
@@ -1679,12 +1128,12 @@
1679 1128
1680 1129 if ( $instantCheck || $status == 'completed' ) {
1681 1130 $theExplanation = $question->get_explanation();
1682 1131 $checked = in_array( $id, $checkedQuestions );
1683 - $hasExplanation = ! ! $theExplanation;
1132 + $hasExplanation = (bool) $theExplanation;
1684 1133 }
1685 1134
1686 - $mark = $question->get_mark() ? $question->get_mark() : 1;
1135 + $mark = $question->get_mark() ? $question->get_mark() : 1;
1687 1136
1688 1137 $questionData = array(
1689 1138 'object' => $question,
1690 1139 'id' => absint( $id ),
@@ -1762,12 +1211,33 @@
1762 1211 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1763 1212 return;
1764 1213 }
1765 1214
1215 + if ( array_key_exists( 'lp_user_slug', $_POST ) ) {
1216 + $userModel = UserModel::find( $user_id, true );
1217 + if ( ! $userModel ) {
1218 + return;
1219 + }
1220 +
1221 + $slug_result = $userModel->update_user_nicename( (string) $_POST['lp_user_slug'] );
1222 + if ( is_wp_error( $slug_result ) ) {
1223 + wp_safe_redirect(
1224 + add_query_arg(
1225 + 'lp-message',
1226 + urlencode( $slug_result->get_error_message() ),
1227 + wp_get_referer()
1228 + )
1229 + );
1230 + die();
1231 + }
1232 + }
1233 +
1766 1234 if ( isset( $_POST['_lp_extra_info'] ) ) {
1767 - update_user_meta( $user_id, '_lp_extra_info', LP_Helper::sanitize_params_submitted( $_POST['_lp_extra_info'] ) );
1235 + $extra_info = LP_Request::get_param( '_lp_extra_info', array(), '', 'post' );
1236 + update_user_meta( $user_id, '_lp_extra_info', $extra_info );
1768 1237 }
1769 1238 }
1239 +
1770 1240 add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' );
1771 1241 add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' );
1772 1242
1773 1243 /**
@@ -1775,9 +1245,9 @@
1775 1245 *
1776 1246 * @param int $user_id
1777 1247 *
1778 1248 * @return array
1779 - * @since 4.0.0
1249 + * @since 4.0.1
1780 1250 */
1781 1251 function learn_press_get_user_extra_profile_info( $user_id = 0 ) {
1782 1252 if ( ! $user_id ) {
1783 1253 $user_id = get_current_user_id();
@@ -1783,30 +1253,61 @@
1783 1253 $user_id = get_current_user_id();
1784 1254 }
1785 1255
1786 1256 $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id );
1787 - $extra_fields = learn_press_get_user_extra_profile_fields();
1257 + $social_fields = learn_press_social_profiles();
1788 1258
1789 - $extra_profile_info = wp_parse_args(
1790 - $extra_profile_info,
1791 - array_fill_keys( array_keys( $extra_fields ), '' )
1792 - );
1259 + $user_socials = array();
1260 + foreach ( $social_fields as $key => $label ) {
1261 + $key = sanitize_key( $key );
1262 + $user_socials[ $key ] = '';
1263 + if ( is_array( $extra_profile_info ) && isset( $extra_profile_info[ $key ] ) ) {
1264 + $user_socials[ $key ] = esc_url_raw( $extra_profile_info[ $key ] );
1265 + }
1266 + }
1793 1267
1794 - return apply_filters( 'learn-press/user-extra-profile-info', $extra_profile_info, $user_id );
1268 + return apply_filters( 'learn-press/user-extra-profile-info', $user_socials, $user_id );
1795 1269 }
1796 1270
1271 +/**
1272 + * @return array
1273 + * @since 3.x.x
1274 + * @version 4.0.2
1275 + */
1797 1276 function learn_press_social_profiles() {
1798 1277 return apply_filters(
1799 1278 'learn-press/social-profiles',
1800 1279 array(
1801 - 'facebook',
1802 - 'twitter',
1803 - 'youtube',
1804 - 'linkedin',
1280 + 'facebook' => learn_press_social_profile_name( 'facebook' ),
1281 + 'twitter' => learn_press_social_profile_name( 'twitter' ),
1282 + 'youtube' => learn_press_social_profile_name( 'youtube' ),
1283 + 'linkedin' => learn_press_social_profile_name( 'linkedin' ),
1805 1284 )
1806 1285 );
1807 1286 }
1808 1287
1288 +function learn_press_social_profile_name( $key ) {
1289 + $name = '';
1290 + switch ( $key ) {
1291 + case 'facebook':
1292 + $name = esc_html__( 'Facebook Profile', 'learnpress' );
1293 + break;
1294 + case 'twitter':
1295 + $name = esc_html__( 'X Profile', 'learnpress' );
1296 + break;
1297 + case 'youtube':
1298 + $name = esc_html__( 'Youtube Channel', 'learnpress' );
1299 + break;
1300 + case 'linkedin':
1301 + $name = esc_html__( 'Linkedin Profile', 'learnpress' );
1302 + break;
1303 + default:
1304 + $name = ucfirst( $key );
1305 + }
1306 +
1307 + return apply_filters( 'learn-press/social-profile-name', $name, $key );
1308 +}
1309 +
1809 1310 function lp_add_default_fields( $fields ) {
1810 1311 $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' );
1811 1312
1812 1313 if ( $first_name === 'yes' ) {
@@ -1813,10 +1314,10 @@
1813 1314 ?>
1814 1315 <li class="form-field">
1815 1316 <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label>
1816 1317 <input id="reg_first_name" name="reg_first_name" type="text"
1817 - placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1818 - value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1318 + placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1319 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1819 1320 </li>
1820 1321 <?php
1821 1322 }
1822 1323
@@ -1826,10 +1327,10 @@
1826 1327 ?>
1827 1328 <li class="form-field">
1828 1329 <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label>
1829 1330 <input id="reg_last_name" name="reg_last_name" type="text"
1830 - placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1831 - value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1331 + placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1332 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1832 1333 </li>
1833 1334 <?php
1834 1335 }
1835 1336
@@ -1839,10 +1340,10 @@
1839 1340 ?>
1840 1341 <li class="form-field">
1841 1342 <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label>
1842 1343 <input id="reg_display_name" name="reg_display_name" type="text"
1843 - placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1844 - value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1344 + placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1345 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1845 1346 </li>
1846 1347 <?php
1847 1348 }
1848 1349 }
@@ -1850,9 +1351,9 @@
1850 1351 add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' );
1851 1352
1852 1353 function lp_custom_register_fields_display() {
1853 1354 ?>
1854 - <?php $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); ?>
1355 + <?php $custom_fields = LP_Profile::get_register_fields_custom(); ?>
1855 1356
1856 1357 <?php if ( $custom_fields ) : ?>
1857 1358 <?php foreach ( $custom_fields as $custom_field ) : ?>
1858 1359 <?php
@@ -1863,11 +1364,12 @@
1863 1364 <style>
1864 1365 .required label {
1865 1366 font-weight: bold;
1866 1367 }
1368 +
1867 1369 .required label:after {
1868 1370 content: ' *';
1869 - display:inline;
1371 + display: inline;
1870 1372 }
1871 1373 </style>
1872 1374 <?php
1873 1375 }
@@ -1886,10 +1388,10 @@
1886 1388 case 'tel':
1887 1389 ?>
1888 1390 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1889 1391 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1890 - type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1891 - value="" />
1392 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1393 + value=""/>
1892 1394 <?php
1893 1395 break;
1894 1396 case 'textarea':
1895 1397 ?>
@@ -1900,9 +1402,9 @@
1900 1402 case 'checkbox':
1901 1403 ?>
1902 1404 <label>
1903 1405 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1904 - type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1406 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1905 1407 <?php echo esc_html( $custom_field['name'] ); ?>
1906 1408 </label>
1907 1409 <?php
1908 1410 break;
@@ -1948,9 +1450,9 @@
1948 1450 return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id );
1949 1451 }
1950 1452
1951 1453 function lp_get_user_custom_fields() {
1952 - $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
1454 + $custom_fields = LP_Settings::get_option( 'register_profile_fields', array() );
1953 1455
1954 1456 $output = array();
1955 1457
1956 1458 if ( $custom_fields ) {
@@ -1968,50 +1470,37 @@
1968 1470 * @param $key
1969 1471 *
1970 1472 * @return bool
1971 1473 * @since 4.0.0
1474 + * @deprecated 4.3.2
1972 1475 */
1973 1476 function learn_press_is_social_profile( $key ) {
1477 + _deprecated_function( __FUNCTION__, '4.3.2' );
1478 +
1479 + return true;
1480 +
1974 1481 $is_socials = learn_press_social_profiles();
1975 1482
1976 1483 return in_array( $key, $is_socials );
1977 1484 }
1978 1485
1979 -function learn_press_social_profile_name( $key ) {
1980 - $name = '';
1981 - switch ( $key ) {
1982 - case 'facebook':
1983 - $name = esc_html__( 'Facebook Profile', 'learnpress' );
1984 - break;
1985 - case 'twitter':
1986 - $name = esc_html__( 'Twitter Profile', 'learnpress' );
1987 - break;
1988 - case 'googleplus':
1989 - $name = esc_html__( 'Google Profile', 'learnpress' );
1990 - break;
1991 - case 'youtube':
1992 - $name = esc_html__( 'Youtube Channel', 'learnpress' );
1993 - break;
1994 - case 'linkedin':
1995 - $name = esc_html__( 'Linkedin Profile', 'learnpress' );
1996 - break;
1997 - default:
1998 - $name = ucfirst( $key );
1999 - }
2000 -
2001 - return apply_filters( 'learn-press/social-profile-name', $name, $key );
2002 -}
2003 -
2004 1486 /**
2005 1487 * Get extra profile fields will be registered in backend profile.
2006 1488 *
2007 1489 * @return array
2008 1490 * @since 4.0.0
1491 + * @deprecated 4.3.2
2009 1492 */
2010 1493 function learn_press_get_user_extra_profile_fields() {
1494 + _deprecated_function( __FUNCTION__, '4.3.2' );
1495 + return array();
1496 +
2011 1497 $socials = learn_press_social_profiles();
2012 - $fields = array();
2013 1498
1499 + return $socials;
1500 +
1501 + $fields = array();
1502 +
2014 1503 foreach ( $socials as $social ) {
2015 1504 $fields[ $social ] = learn_press_social_profile_name( $social );
2016 1505 }
2017 1506
@@ -2032,10 +1521,13 @@
2032 1521
2033 1522 learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) );
2034 1523 learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) );
2035 1524 }
1525 +
2036 1526 add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 );
1527 +add_action( 'show_user_profile', 'learn_press_user_profile_data', 1000 );
2037 1528
1529 +/*
2038 1530 function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) {
2039 1531 if ( empty( $user_id ) ) {
2040 1532 return false;
2041 1533 }
@@ -2076,6 +1568,5 @@
2076 1568 'all' => count( $posts ),
2077 1569 'publish' => count( $public ),
2078 1570 'pending' => count( $pending ),
2079 1571 );
2080 -
2081 -}
1572 +}*/