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
← All changes | inc/user/lp-user-functions.php +359 -889 4.1.7.3 → 4.4.9.1 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;
@@ -89,9 +92,9 @@
89 92 * If current user is not logged in, return a GUEST user
90 93 *
91 94 * @param bool $create_temp - Optional. Create temp user if user is not logged in.
92 95 *
93 - * @return bool|LP_User|LP_User_Guest
96 + * @return LP_User|LP_User_Guest
94 97 * @editor tungnx
95 98 * @modify 4.1.4
96 99 * @version 1.0.1
97 100 */
@@ -107,261 +110,55 @@
107 110 }
108 111
109 112 if ( ! function_exists( 'learn_press_get_user' ) ) {
110 113 /**
111 - * Get user by ID. Return false if the user does not exists.
114 + * Get user by ID. Return false if the user does not exist.
115 + * If user_id = 0, return a guest user.
112 116 *
113 - * @param int $user_id
117 + * @param int $user_id
114 118 * @param bool $current
115 119 *
116 - * @return LP_User|LP_User_Guest|mixed
117 - * Todo: check this function - tungnx
120 + * @return LP_User|LP_User_Guest|false
121 + * @since 3.0.0
122 + * @version 4.0.1
118 123 */
119 - function learn_press_get_user( $user_id, $current = false, $force_new = false ) {
120 - $is_guest = false;
121 - if ( ! is_null( LearnPress::instance()->session ) && $user_id != LearnPress::instance()->session->guest_user_id ) {
122 - if ( $current && ! get_user_by( 'id', $user_id ) ) {
123 - $user_id = get_current_user_id();
124 - }
125 - }
126 -
127 - if ( ! $user_id && isset( LearnPress::instance()->session ) ) {
128 - if ( ! LearnPress::instance()->session->guest_user_id ) {
129 - LearnPress::instance()->session->set_customer_session_cookie( 1 );
130 - LearnPress::instance()->session->guest_user_id = time();
131 - }
132 -
133 - $user_id = LearnPress::instance()->session->guest_user_id;
134 - $is_guest = true;
135 - }
136 -
137 - if ( ! $user_id ) {
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 ) {
138 127 return false;
139 128 }
140 129
141 - $user_id = '' . $user_id;
142 -
143 - if ( $force_new || ! array_key_exists( $user_id, LP_Global::$users ) ) {
144 - /**
145 - * LP Hook.
146 - *
147 - * Filter the default class name to get LP user.
148 - *
149 - * @since 3.3.0
150 - */
151 - $userClass = apply_filters( 'learn-press/user-class', $is_guest ? 'LP_User_Guest' : 'LP_User', $is_guest );
152 -
153 - LP_Global::$users[ $user_id ] = new $userClass( $user_id );
154 -
155 - do_action( 'learn-press/get-user', LP_Global::$users[ $user_id ], $user_id );
156 - }
157 -
158 - return LP_Global::$users[ $user_id ];
130 + return new $userClass( $user_id );
159 131 }
160 132 }
161 133
162 134 /**
163 - * Add more 2 user roles teacher and student
135 + * Add roles and add capabilities for roles
164 136 */
165 137 function learn_press_add_user_roles() {
138 + $item_types = [
139 + LP_COURSE_CPT,
140 + LP_LESSON_CPT,
141 + LP_ORDER_CPT,
142 + ];
166 143
167 - $settings = LP_Settings::instance();
144 + UserService::instance()->add_capabilities_for_roles();
168 145
169 - /* translators: user role */
170 - _x( 'LP Instructor', 'User role' );
171 -
172 - add_role(
173 - LP_TEACHER_ROLE,
174 - 'LP Instructor',
175 - array()
176 - );
177 -
178 - $course_cap = LP_COURSE_CPT . 's';
179 - $lesson_cap = LP_LESSON_CPT . 's';
180 - $order_cap = LP_ORDER_CPT . 's';
181 -
182 - $teacher = get_role( LP_TEACHER_ROLE );
183 - if ( $teacher ) {
184 - $teacher->add_cap( 'read_private_' . $course_cap );
185 - $teacher->add_cap( 'delete_published_' . $course_cap );
186 - $teacher->add_cap( 'edit_published_' . $course_cap );
187 - $teacher->add_cap( 'edit_' . $course_cap );
188 - $teacher->add_cap( 'delete_' . $course_cap );
189 - $teacher->add_cap( 'unfiltered_html' );
190 -
191 - $settings->get( 'required_review' );
192 -
193 - if ( $settings->get( 'required_review' ) == 'yes' ) {
194 - $teacher->remove_cap( 'publish_' . $course_cap );
195 - } else {
196 - $teacher->add_cap( 'publish_' . $course_cap );
197 - }
198 -
199 - $teacher->add_cap( 'read_private_' . $lesson_cap );
200 - $teacher->add_cap( 'delete_published_' . $lesson_cap );
201 - $teacher->add_cap( 'edit_published_' . $lesson_cap );
202 - $teacher->add_cap( 'edit_' . $lesson_cap );
203 - $teacher->add_cap( 'delete_' . $lesson_cap );
204 - $teacher->add_cap( 'publish_' . $lesson_cap );
205 - $teacher->add_cap( 'upload_files' );
206 - $teacher->add_cap( 'read' );
207 - $teacher->add_cap( 'edit_posts' );
146 + foreach ( $item_types as $item_type ) {
147 + UserService::instance()->add_capabilities_for_roles( $item_type );
208 148 }
209 -
210 - // administrator
211 - $admin = get_role( 'administrator' );
212 - if ( $admin ) {
213 - $admin->add_cap( 'read_private_' . $course_cap );
214 - $admin->add_cap( 'delete_' . $course_cap );
215 - $admin->add_cap( 'delete_published_' . $course_cap );
216 - $admin->add_cap( 'edit_' . $course_cap );
217 - $admin->add_cap( 'edit_published_' . $course_cap );
218 - $admin->add_cap( 'publish_' . $course_cap );
219 - $admin->add_cap( 'delete_private_' . $course_cap );
220 - $admin->add_cap( 'edit_private_' . $course_cap );
221 - $admin->add_cap( 'delete_others_' . $course_cap );
222 - $admin->add_cap( 'edit_others_' . $course_cap );
223 -
224 - $admin->add_cap( 'read_private_' . $lesson_cap );
225 - $admin->add_cap( 'delete_' . $lesson_cap );
226 - $admin->add_cap( 'delete_published_' . $lesson_cap );
227 - $admin->add_cap( 'edit_' . $lesson_cap );
228 - $admin->add_cap( 'edit_published_' . $lesson_cap );
229 - $admin->add_cap( 'publish_' . $lesson_cap );
230 - $admin->add_cap( 'delete_private_' . $lesson_cap );
231 - $admin->add_cap( 'edit_private_' . $lesson_cap );
232 - $admin->add_cap( 'delete_others_' . $lesson_cap );
233 - $admin->add_cap( 'edit_others_' . $lesson_cap );
234 -
235 - $admin->add_cap( 'delete_' . $order_cap );
236 - $admin->add_cap( 'delete_published_' . $order_cap );
237 - $admin->add_cap( 'edit_' . $order_cap );
238 - $admin->add_cap( 'edit_published_' . $order_cap );
239 - $admin->add_cap( 'publish_' . $order_cap );
240 - $admin->add_cap( 'delete_private_' . $order_cap );
241 - $admin->add_cap( 'edit_private_' . $order_cap );
242 - $admin->add_cap( 'delete_others_' . $order_cap );
243 - $admin->add_cap( 'edit_others_' . $order_cap );
244 - }
245 149 }
246 150
247 -add_action( 'init', 'learn_press_add_user_roles' );
248 -
249 151 /**
250 - * @param null $user_id
251 - * @param array $args
252 - *
253 - * @return mixed
254 - * @deprecated 4.1.7.3
255 - */
256 -/*function learn_press_get_user_questions( $user_id = null, $args = array() ) {
257 - if ( ! $user_id ) {
258 - $user_id = get_current_user_id();
259 - }
260 -
261 - return learn_press_get_user( $user_id )->get_questions( $args );
262 -}*/
263 -
264 -/**
265 - * Get the type of current user
266 - *
267 - * @param null $check_type
268 - *
269 - * @return bool|string
270 - */
271 -function learn_press_current_user_is( $check_type = null ) {
272 - global $current_user;
273 - $user_roles = $current_user->roles;
274 - $user_type = '';
275 -
276 - if ( in_array( 'lpr_teacher', $user_roles ) ) {
277 - $user_type = 'instructor';
278 - } elseif ( in_array( 'lp_teacher', $user_roles ) ) {
279 - $user_type = 'instructor';
280 - } elseif ( in_array( 'administrator', $user_roles ) ) {
281 - $user_type = 'administrator';
282 - }
283 -
284 - return $check_type ? $check_type == $user_type : $user_type;
285 -}
286 -
287 -function learn_press_user_has_roles( $roles, $user_id = null ) {
288 - $has_role = false;
289 - if ( ! $user_id ) {
290 - $user = wp_get_current_user();
291 - } else {
292 - $user = get_user_by( 'id', $user_id );
293 - }
294 - $available_roles = (array) $user->roles;
295 - if ( is_array( $roles ) ) {
296 - foreach ( $roles as $role ) {
297 - if ( in_array( $role, $available_roles ) ) {
298 - $has_role = true;
299 - break; // only need one of roles is in available
300 - }
301 - }
302 - } else {
303 - if ( in_array( $roles, $available_roles ) ) {
304 - $has_role = true;
305 - }
306 - }
307 -
308 - return $has_role;
309 -}
310 -
311 -function learn_press_current_user_can_view_profile_section( $section, $user ) {
312 - $current_user = wp_get_current_user();
313 - $view = true;
314 - if ( $user->get_data( 'user_login' ) != $current_user->user_login && $section == LP_Settings::instance()->get(
315 - 'profile_endpoints.profile-orders',
316 - 'profile-orders'
317 - ) ) {
318 - $view = false;
319 - }
320 -
321 - return apply_filters( 'learn_press_current_user_can_view_profile_section', $view, $section, $user );
322 -}
323 -
324 -/*function learn_press_profile_tab_courses_content( $current, $tab, $user ) {
325 - learn_press_get_template(
326 - 'profile/tabs/courses.php',
327 - array(
328 - 'user' => $user,
329 - 'current' => $current,
330 - 'tab' => $tab,
331 - )
332 - );
333 -}*/
334 -
335 -function learn_press_profile_tab_quizzes_content( $current, $tab, $user ) {
336 - learn_press_get_template(
337 - 'profile/tabs/quizzes.php',
338 - array(
339 - 'user' => $user,
340 - 'current' => $current,
341 - 'tab' => $tab,
342 - )
343 - );
344 -}
345 -
346 -function learn_press_profile_tab_orders_content( $current, $tab, $user ) {
347 - learn_press_get_template(
348 - 'profile/tabs/orders.php',
349 - array(
350 - 'user' => $user,
351 - 'current' => $current,
352 - 'tab' => $tab,
353 - )
354 - );
355 -}
356 -
357 -/**
358 152 * Get queried user in profile link
359 153 *
360 154 * @return false|WP_User
361 155 * @since 3.0.0
156 + * @deprecated 4.2.9.1
362 157 */
363 158 function learn_press_get_profile_user() {
159 + return LP_Profile::instance()->get_user_current();
160 +
364 161 return LP_Profile::get_queried_user();
365 162 }
366 163
367 164
@@ -394,14 +191,14 @@
394 191 * ....
395 192 * field_name_n => value n
396 193 * )
397 194 * @param mixed $where - Optional. Fields with values for conditional update with the same format of $fields.
398 - * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0).
399 - * @param bool $update_extra_fields_as_meta - Optional. Update extra fields as item meta (since 3.1.0).
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).
400 197 *
401 198 * @return mixed
402 199 */
403 -function learn_press_update_user_item_field( array $fields = [], $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) {
200 +function learn_press_update_user_item_field( array $fields = array(), $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) {
404 201 global $wpdb;
405 202
406 203 // Table fields format.
407 204 $table_fields = array(
@@ -428,12 +225,10 @@
428 225 if ( LP_COURSE_CPT === $item_type ) {
429 226 if ( 'completed' === $fields['status'] ) {
430 227 $fields['status'] = 'finished';
431 228 }
432 - } else {
433 - if ( 'finished' === $fields['status'] ) {
229 + } elseif ( 'finished' === $fields['status'] ) {
434 230 $fields['status'] = 'completed';
435 - }
436 231 }
437 232 }
438 233
439 234 $data = array();
@@ -442,9 +237,9 @@
442 237 if ( ! empty( $table_fields[ $field ] ) ) {
443 238 $data[ $field ] = $value;
444 239
445 240 // Do not format the date-time field if it's value is NULL
446 - if ( in_array( $field, [ 'start_time', 'end_time' ] ) && empty( $value ) ) {
241 + if ( in_array( $field, array( 'start_time', 'end_time' ) ) && empty( $value ) ) {
447 242 $data[ $field ] = null;
448 243 $data_format[] = '';
449 244 } else {
450 245 if ( $value instanceof LP_Datetime ) {
@@ -518,14 +313,52 @@
518 313 } elseif ( $updated ) {
519 314 $updated_item = learn_press_get_user_item( $where );
520 315 }
521 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 +
522 353 /**
523 354 * If there is some fields does not contain in the main table
524 355 * then consider update them as metadata.
356 + *
525 357 * @comment by tungnx - 4.1.7.3
526 358 */
527 - /*if ( $updated_item && $update_extra_fields_as_meta ) {
359 + /*
360 + if ( $updated_item && $update_extra_fields_as_meta ) {
528 361 $extra_fields = array_diff_key( $fields, $table_fields );
529 362 if ( $extra_fields ) {
530 363 foreach ( $extra_fields as $meta_key => $meta_value ) {
531 364 if ( $meta_value == 'user_item_id' ) {
@@ -544,9 +377,9 @@
544 377 }
545 378 }
546 379 }*/
547 380
548 - do_action( 'learn-press/updated-user-item-meta', $updated_item );
381 + // do_action( 'learn-press/updated-user-item-meta', $updated_item );
549 382
550 383 return $updated_item;
551 384 }
552 385
@@ -553,9 +386,9 @@
553 386 /**
554 387 * Get user item row(s) from user items table by multiple WHERE conditional
555 388 *
556 389 * @param array|int $where
557 - * @param bool $single
390 + * @param bool $single
558 391 *
559 392 * @return array
560 393 */
561 394 function learn_press_get_user_item( $where, $single = true ) {
@@ -610,11 +443,11 @@
610 443
611 444 /**
612 445 * Get user item meta from user_itemmeta table
613 446 *
614 - * @param int $user_item_id .
447 + * @param int $user_item_id .
615 448 * @param string $meta_key .
616 - * @param bool $single .
449 + * @param bool $single .
617 450 *
618 451 * @return mixed
619 452 */
620 453 function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) {
@@ -628,11 +461,11 @@
628 461
629 462 /**
630 463 * Add user item meta into table user_itemmeta
631 464 *
632 - * @param int $user_item_id
465 + * @param int $user_item_id
633 466 * @param string $meta_key
634 - * @param mixed $meta_value
467 + * @param mixed $meta_value
635 468 * @param string $prev_value
636 469 *
637 470 * @return false|int
638 471 */
@@ -642,11 +475,11 @@
642 475
643 476 /**
644 477 * Update user item meta to table user_itemmeta
645 478 *
646 - * @param int $user_item_id
479 + * @param int $user_item_id
647 480 * @param string $meta_key
648 - * @param mixed $meta_value
481 + * @param mixed $meta_value
649 482 * @param string $prev_value
650 483 *
651 484 * @return bool|int
652 485 */
@@ -657,12 +490,12 @@
657 490
658 491 /**
659 492 * Update user item meta to table user_itemmeta
660 493 *
661 - * @param int $object_id
494 + * @param int $object_id
662 495 * @param string $meta_key
663 - * @param mixed $meta_value
664 - * @param bool $delete_all
496 + * @param mixed $meta_value
497 + * @param bool $delete_all
665 498 *
666 499 * @return bool|int
667 500 */
668 501 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
@@ -668,101 +501,8 @@
668 501 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
669 502 return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all );
670 503 }
671 504
672 -/**
673 - * Get temp users.
674 - *
675 - * @return array
676 - * @deprecated 4.1.6.9
677 - */
678 -/*function learn_press_get_temp_users() {
679 - return false;
680 - if ( false === ( $temp_users = LP_Object_Cache::get( 'learn-press/temp-users' ) ) ) {
681 - global $wpdb;
682 - $query = $wpdb->prepare(
683 - "
684 - SELECT ID
685 - FROM {$wpdb->users} u
686 - INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id AND um.meta_key = %s AND um.meta_value = %s
687 - LEFT JOIN {$wpdb->usermeta} um2 ON u.ID = um2.user_id AND um2.meta_key = %s
688 - ",
689 - '_lp_temp_user',
690 - 'yes',
691 - '_lp_expiration'
692 - );
693 -
694 - $temp_users = $wpdb->get_col( $query );
695 -
696 - LP_Object_Cache::set( 'learn-press/temp-users', $temp_users );
697 - }
698 -
699 - return $temp_users;
700 -}*/
701 -
702 -/**
703 - * Update field created_time after added user item meta
704 - *
705 - * @use updated_{meta_type}_meta hook
706 - *
707 - * @param $meta_id
708 - * @param $object_id
709 - * @param $meta_key
710 - * @param $_meta_value
711 - * @deprecated 4.1.7.3
712 - */
713 -/*function _learn_press_update_created_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
714 - global $wpdb;
715 - $wpdb->update(
716 - $wpdb->learnpress_user_itemmeta,
717 - array( 'create_time' => current_time( 'mysql' ) ),
718 - array( 'meta_id' => $meta_id ),
719 - array( '%s' ),
720 - array( '%d' )
721 - );
722 -}*/
723 -
724 -// add_action( 'added_learnpress_user_item_meta', '_learn_press_update_created_time_user_item_meta', 10, 4 );
725 -
726 -/**
727 - * Update field updated_time after updated user item meta
728 - *
729 - * @use updated_{meta_type}_meta hook
730 - *
731 - * @param $meta_id
732 - * @param $object_id
733 - * @param $meta_key
734 - * @param $_meta_value
735 - * @deprecated
736 - */
737 -/*function _learn_press_update_updated_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
738 - global $wpdb;
739 - $wpdb->update(
740 - $wpdb->learnpress_user_itemmeta,
741 - array( 'update_time' => current_time( 'mysql' ) ),
742 - array( 'meta_id' => $meta_id ),
743 - array( '%s' ),
744 - array( '%d' )
745 - );
746 -}*/
747 -
748 -// add_action( 'updated_learnpress_user_item_meta', '_learn_press_update_updated_time_user_item_meta', 10, 4 );
749 -
750 -/**
751 - * @param $status
752 - * @param int $quiz_id
753 - * @param int $user_id
754 - * @param int $course_id
755 - *
756 - * @return bool|mixed
757 - * @deprecated 4.1.7.3
758 - */
759 -/*function learn_press_user_has_quiz_status( $status, $quiz_id = 0, $user_id = 0, $course_id = 0 ) {
760 - $user = learn_press_get_user( $user_id );
761 -
762 - return $user->has_quiz_status( $status, $quiz_id, $course_id );
763 -}*/
764 -
765 505 if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) {
766 506 /**
767 507 * Filter the avatar
768 508 *
@@ -767,109 +507,105 @@
767 507 * Filter the avatar
768 508 *
769 509 * @param string $avatar
770 510 * @param string $id_or_email
771 - * @param array $size
511 + * @param array $args
772 512 *
773 513 * @return string
514 + * @since 1.0.0
515 + * @version 1.0.2
774 516 */
775 - function learn_press_pre_get_avatar_callback( $avatar, $id_or_email = '', $size = array() ) {
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 + }
776 522
777 - $profile = LP_Profile::instance();
523 + $user_id = 0;
778 524
779 - if ( ! $profile->is_enable_avatar() ) {
780 - return $avatar;
781 - }
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 + }
782 543
783 - if ( ( isset( $size['gravatar'] ) && $size['gravatar'] ) || ( $size['default'] && $size['force_default'] ) ) {
784 - return $avatar;
785 - }
544 + if ( ! $user_id ) {
545 + return $avatar;
546 + }
786 547
787 - $user_id = 0;
788 -
789 - /**
790 - * Get the ID of user from $id_or_email
791 - */
792 - if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) {
793 - $user = get_user_by( 'email', $id_or_email );
794 - if ( $user ) {
795 - $user_id = $user->ID;
548 + $user = UserModel::find( $user_id, true );
549 + if ( ! $user ) {
550 + return $avatar;
796 551 }
797 - } elseif ( is_numeric( $id_or_email ) ) {
798 - $user_id = $id_or_email;
799 - } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) {
800 - $user_id = $id_or_email->user_id;
801 - } elseif ( $id_or_email instanceof WP_Comment ) {
802 - $user = get_user_by( 'email', $id_or_email->comment_author_email );
803 - if ( $user ) {
804 - $user_id = $user->ID;
805 - }
806 - }
807 552
808 - if ( ! $user_id ) {
809 - return $avatar;
810 - }
811 -
812 - $user = LP_User_Factory::get_user( $user_id );
813 -
814 - $profile_picture_src = $user->get_upload_profile_src();
815 - if ( $profile_picture_src ) {
816 - $setting_size = learn_press_get_avatar_thumb_size();
817 - $img_size = '';
818 -
819 - // Get avatar size
820 - if ( ! is_array( $size ) ) {
821 - if ( $size === 'thumbnail' ) {
822 - $img_size = '';
823 - $height = $setting_size['height'];
824 - $width = $setting_size['width'];
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'] );
825 557 } else {
826 - $height = 250;
827 - $width = 250;
558 + $class[] = $args['class'];
828 559 }
829 - } else {
830 - $img_size = $size['size'];
831 - $height = $size['height'];
832 - $width = $size['width'];
833 560 }
834 561
835 - $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 . '" />';
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;
836 577 }
837 -
838 - return $avatar;
839 578 }
840 579 }
841 -add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 1, 5 );
580 +add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 999, 3 );
842 581
843 582
844 583 function learn_press_user_profile_picture_upload_dir( $width_user = true ) {
845 - static $upload_dir;
846 - if ( ! $upload_dir ) {
847 - $upload_dir = wp_upload_dir();
848 - $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user );
849 - if ( $width_user ) {
850 - $subdir .= '/' . get_current_user_id();
851 - }
852 - $subdir = '/' . $subdir;
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;
853 590
854 - if ( ! empty( $upload_dir['subdir'] ) ) {
855 - $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
856 - $u_path = str_replace( '\\', '/', $upload_dir['path'] );
591 + if ( ! empty( $upload_dir['subdir'] ) ) {
592 + $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
593 + $u_path = str_replace( '\\', '/', $upload_dir['path'] );
857 594
858 - $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path );
859 - $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] );
860 - } else {
861 - $upload_dir['path'] = $upload_dir['path'] . $subdir;
862 - $upload_dir['url'] = $upload_dir['url'] . $subdir;
863 - }
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 + }
864 601
865 - $upload_dir['subdir'] = $subdir;
602 + $upload_dir['subdir'] = $subdir;
866 603
867 - // Point path/url to main site if we are in multisite
868 - if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
869 - foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) {
870 - $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] );
871 - }
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 ] );
872 608 }
873 609 }
874 610
875 611 return $upload_dir;
@@ -874,8 +610,9 @@
874 610
875 611 return $upload_dir;
876 612 }
877 613
614 +/*
878 615 add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 );
879 616 function _learn_press_before_purchase_course_handler( $course_id, $cart ) {
880 617 // Redirect to login page if user is not logged in
881 618 if ( ! is_user_logged_in() ) {
@@ -914,10 +651,11 @@
914 651 wp_redirect( get_the_permalink( $course_id ) );
915 652 exit();
916 653 }
917 654 }
918 -}
655 +}*/
919 656
657 +/*
920 658 function learn_press_user_is( $role, $user_id = 0 ) {
921 659 if ( ! $user_id ) {
922 660 $user = learn_press_get_current_user();
923 661 } else {
@@ -930,10 +668,11 @@
930 668 return $user->is_instructor();
931 669 }
932 670
933 671 return $role;
934 -}
672 +}*/
935 673
674 +/*
936 675 function learn_press_profile_tab_edit_content( $current, $tab, $user ) {
937 676 learn_press_get_template(
938 677 'profile/tabs/edit.php',
939 678 array(
@@ -941,25 +680,11 @@
941 680 'current' => $current,
942 681 'tab' => $tab,
943 682 )
944 683 );
945 -}
684 +}*/
946 685
947 -function learn_press_get_profile_endpoints() {
948 - $endpoints = (array) LP_Settings::instance()->get( 'profile_endpoints' );
949 - $tabs = LP_Profile::instance()->get_tabs();
950 - if ( $tabs ) {
951 - foreach ( $tabs as $slug => $info ) {
952 - if ( empty( $endpoints[ $slug ] ) ) {
953 - $endpoints[ $slug ] = $slug;
954 - }
955 - }
956 - }
957 -
958 - return apply_filters( 'learn_press_profile_tab_endpoints', $endpoints );
959 -}
960 -
961 -
686 +/*
962 687 function learn_press_update_user_option( $name, $value, $id = 0 ) {
963 688 if ( ! $id ) {
964 689 $id = get_current_user_id();
965 690 }
@@ -966,16 +691,11 @@
966 691 $key = 'learnpress_user_options';
967 692 $options = get_user_option( $key, $id );
968 693 $options[ $name ] = $value;
969 694 update_user_option( $id, $key, $options, true );
970 -}
695 +}*/
971 696
972 -/**
973 - * @param $name
974 - * @param int $id
975 - *
976 - * @return bool
977 - */
697 +/*
978 698 function learn_press_delete_user_option( $name, $id = 0 ) {
979 699 if ( ! $id ) {
980 700 $id = get_current_user_id();
981 701 }
@@ -988,16 +708,11 @@
988 708 return true;
989 709 }
990 710
991 711 return false;
992 -}
712 +}*/
993 713
994 -/**
995 - * @param $name
996 - * @param int $id
997 - *
998 - * @return bool
999 - */
714 +/*
1000 715 function learn_press_get_user_option( $name, $id = 0 ) {
1001 716 if ( ! $id ) {
1002 717 $id = get_current_user_id();
1003 718 }
@@ -1007,9 +722,9 @@
1007 722 return $options[ $name ];
1008 723 }
1009 724
1010 725 return false;
1011 -}
726 +}*/
1012 727
1013 728 /**
1014 729 * Update user basic information.
1015 730 *
@@ -1025,13 +740,13 @@
1025 740 }
1026 741
1027 742 $update_data = array(
1028 743 'ID' => $user_id,
1029 - 'first_name' => filter_input( INPUT_POST, 'first_name', FILTER_SANITIZE_STRING ),
1030 - 'last_name' => filter_input( INPUT_POST, 'last_name', FILTER_SANITIZE_STRING ),
1031 - 'description' => filter_input( INPUT_POST, 'description', FILTER_SANITIZE_STRING ),
1032 - 'display_name' => filter_input( INPUT_POST, 'account_display_name', FILTER_SANITIZE_STRING ),
1033 - 'user_email' => filter_input( INPUT_POST, 'account_email', FILTER_SANITIZE_EMAIL ),
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' ),
1034 749 );
1035 750
1036 751 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
1037 752 $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' );
@@ -1038,9 +753,9 @@
1038 753
1039 754 $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta );
1040 755
1041 756 // Update for social.
1042 - $socials = LP_Request::get_array( 'user_profile_social' );
757 + $socials = LP_Request::get_param( 'user_profile_social' );
1043 758 $extra_data = get_user_meta( $user_id, '_lp_extra_info', true );
1044 759
1045 760 if ( ! empty( $extra_data ) ) {
1046 761 $socials = array_merge( $extra_data, $socials );
@@ -1056,55 +771,51 @@
1056 771 }
1057 772
1058 773 /**
1059 774 * Update new password.
775 + *
776 + * @version 1.0.1
1060 777 */
1061 -function learn_press_update_user_profile_change_password( $wp_error = false ) {
1062 - $user_id = get_current_user_id();
1063 -
1064 - if ( ! $user_id ) {
1065 - return new WP_Error( 2, 'The user is invalid' );
1066 - }
1067 -
1068 - $old_pass = filter_input( INPUT_POST, 'pass0' );
1069 - $check_old_pass = false;
1070 -
1071 - if ( $old_pass ) {
778 +function learn_press_update_user_profile_change_password() {
779 + try {
1072 780 $user = wp_get_current_user();
1073 - require_once ABSPATH . 'wp-includes/class-phpass.php';
1074 - $wp_hasher = new PasswordHash( 8, true );
781 + if ( ! $user ) {
782 + throw new Exception( __( 'The user is invalid', 'learnpress' ) );
783 + }
1075 784
1076 - if ( $wp_hasher->CheckPassword( $old_pass, $user->data->user_pass ) ) {
1077 - $check_old_pass = true;
785 + $old_pass = LP_Request::get_param( 'pass0', '', 'text', 'post' );
786 + if ( empty( $old_pass ) ) {
787 + throw new Exception( __( 'Enter the old password!', 'learnpress' ) );
1078 788 }
1079 - }
1080 789
1081 - try {
790 + $check_old_pass = wp_check_password( $old_pass, $user->data->user_pass, $user->ID );
1082 791 if ( ! $check_old_pass ) {
1083 792 throw new Exception( __( 'The old password is incorrect!', 'learnpress' ) );
1084 - } else {
1085 - $new_pass = filter_input( INPUT_POST, 'pass1' );
1086 - $new_pass2 = filter_input( INPUT_POST, 'pass2' );
793 + }
1087 794
1088 - if ( ! $new_pass || ! $new_pass2 || ( $new_pass != $new_pass2 ) ) {
1089 - throw new Exception( __( 'Incorrect confirmation password!', 'learnpress' ) );
1090 - } else {
1091 - $update_data = array(
1092 - 'user_pass' => $new_pass,
1093 - 'ID' => get_current_user_id(),
1094 - );
1095 - $return = wp_update_user( $update_data );
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 + }
1096 801
1097 - if ( is_wp_error( $return ) ) {
1098 - return $wp_error ? $return : false;
1099 - }
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 + );
1100 808
1101 - return $return;
1102 - }
809 + $rs = wp_update_user( $update_data );
810 + if ( is_wp_error( $rs ) ) {
811 + throw new Exception( $rs->get_error_message() );
1103 812 }
1104 813 } catch ( Exception $ex ) {
1105 - return $wp_error ? new WP_Error( 'UPDATE_PROFILE_ERROR', $ex->getMessage() ) : false;
814 + return new WP_Error( 'error_lp_profile_update_pass', $ex->getMessage() );
1106 815 }
816 +
817 + return true;
1107 818 }
1108 819
1109 820 function learn_press_get_avatar_thumb_size() {
1110 821 $option = LP_Settings::get_option(
@@ -1121,8 +832,11 @@
1121 832 'height' => 250,
1122 833 );
1123 834 }
1124 835
836 + // For option get_avatar_url
837 + $option['size'] = $option['width'];
838 +
1125 839 return $option;
1126 840 }
1127 841
1128 842 function learn_press_get_course_thumbnail_dimensions() {
@@ -1143,34 +857,14 @@
1143 857
1144 858 return $option;
1145 859 }
1146 860
1147 -/**
1148 - * Set a fake cookie to
1149 - */
1150 -function learn_press_set_user_cookie_for_guest() {
1151 - if ( ! is_admin() && ! headers_sent() ) {
1152 - $guest_key = '_wordpress_lp_guest';
1153 -
1154 - if ( is_user_logged_in() ) {
1155 - if ( ! empty( $_COOKIE[ $guest_key ] ) ) {
1156 - learn_press_remove_cookie( $guest_key );
1157 - }
1158 - } else {
1159 - if ( empty( $_COOKIE[ $guest_key ] ) ) {
1160 - learn_press_setcookie( $guest_key, md5( time() ), time() + 3600 );
1161 - }
1162 - }
1163 - }
1164 -}
1165 -
1166 -add_action( 'wp', 'learn_press_set_user_cookie_for_guest' );
1167 -
861 +/*
1168 862 function learn_press_get_user_avatar( $user_id = 0, $size = '' ) {
1169 863 $user = learn_press_get_user( $user_id );
1170 864
1171 865 return $user->get_profile_picture( '', $size );
1172 -}
866 +}*/
1173 867
1174 868 /**
1175 869 * Get profile instance for an user to view.
1176 870 *
@@ -1181,16 +875,9 @@
1181 875 function learn_press_get_profile( $for_user = 0 ) {
1182 876 return LP_Profile::instance( $for_user );
1183 877 }
1184 878
1185 -/**
1186 - * Remove items from learnpress_user_items.
1187 - *
1188 - * @param int $user_id
1189 - * @param int $item_id
1190 - * @param int $course_id
1191 - * @param bool $include_course - Optional. If TRUE then remove course and it's items
1192 - */
879 +/*
1193 880 function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) {
1194 881 global $wpdb;
1195 882
1196 883 settype( $item_id, 'array' );
@@ -1213,44 +900,30 @@
1213 900 }
1214 901
1215 902 $query = $wpdb->prepare(
1216 903 "
1217 - DELETE
1218 - FROM {$wpdb->learnpress_user_items}
1219 - WHERE user_id = %d
1220 - AND ( item_id IN(" . join( ',', $format ) . ")
1221 - $where )
1222 - ",
904 + DELETE
905 + FROM {$wpdb->learnpress_user_items}
906 + WHERE user_id = %d
907 + AND ( item_id IN(" . join( ',', $format ) . ")
908 + $where )
909 + ",
1223 910 $args
1224 911 );
1225 -}
912 +}*/
1226 913
1227 914 /**
1228 915 * Get user profile link
1229 916 *
1230 - * @param int $user_id
917 + * @param int $user_id
1231 918 * @param null $tab
1232 919 *
1233 920 * @return mixed|string
1234 921 */
1235 -function learn_press_user_profile_link( $user_id = 0, $tab = null ) {
922 +function learn_press_user_profile_link( $user_id = 0, $tab = '' ) {
1236 923 if ( ! $user_id ) {
1237 924 $user_id = get_current_user_id();
1238 925 }
1239 - $user = false;
1240 - $deleted = in_array( $user_id, LP_User_Factory::$_deleted_users );
1241 - if ( ! $deleted ) {
1242 - if ( is_numeric( $user_id ) ) {
1243 - $user = get_user_by( 'id', $user_id );
1244 - } else {
1245 - $user = get_user_by( 'login', urldecode( $user_id ) );
1246 - }
1247 - } else {
1248 - return '';
1249 - }
1250 - if ( ! $deleted && ! $user ) {
1251 - LP_User_Factory::$_deleted_users[] = $user_id;
1252 - }
1253 926
1254 927 $user = learn_press_get_user( $user_id );
1255 928
1256 929 if ( ! $user ) {
@@ -1257,38 +930,33 @@
1257 930 return '';
1258 931 }
1259 932
1260 933 global $wp_query;
1261 - $args = array(
1262 - 'user' => $user->get_username(),
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(),
1263 942 );
1264 943
1265 - if ( isset( $args['user'] ) ) {
1266 - if ( '' === $tab ) {
1267 - $tab = learn_press_get_current_profile_tab();
1268 - }
1269 - if ( $tab ) {
1270 - $args['tab'] = $tab;
1271 - }
944 + if ( $tab ) {
945 + $args['tab'] = $tab;
946 + }
1272 947
1273 - /**
1274 - * If no tab is selected in profile and is current user
1275 - * then no need the username in profile link.
1276 - */
1277 - if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) {
1278 - unset( $args['user'] );
1279 - }
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'] );
1280 954 }
1281 955
1282 - /*$args = array_map(
1283 - function ( $string ) {
1284 - return preg_replace( '/\s/', '+', $string );
1285 - },
1286 - $args
1287 - );*/
1288 956 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
1289 957 if ( $profile_link ) {
1290 - if ( get_option( 'permalink_structure' ) /*&& learn_press_get_page_id( 'profile' )*/ ) {
958 + if ( get_option( 'permalink_structure' ) ) {
1291 959 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
1292 960 } else {
1293 961 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
1294 962 }
@@ -1298,66 +966,9 @@
1298 966
1299 967 return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab );
1300 968 }
1301 969
1302 -/**********************************************/
1303 -/* Functions are used for hooks */
1304 -/**********************************************/
1305 -
1306 -function learn_press_hk_before_start_quiz( $true, $quiz_id, $course_id, $user_id ) {
1307 - if ( 'yes' !== get_post_meta( $quiz_id, '_lp_archive_history', true ) ) {
1308 - learn_press_remove_user_items( $user_id, $quiz_id, $course_id );
1309 - }
1310 -
1311 - return $true;
1312 -}
1313 -
1314 -add_filter( 'learn-press/before-start-quiz', 'learn_press_hk_before_start_quiz', 10, 4 );
1315 -
1316 -/*function learn_press_default_user_item_status( $item_id ) {
1317 - $status = '';
1318 - switch ( learn_press_get_post_type( $item_id ) ) {
1319 - case LP_LESSON_CPT:
1320 - $status = 'started';
1321 - break;
1322 - case LP_QUIZ_CPT:
1323 - $status = 'viewed';
1324 - break;
1325 - case LP_COURSE_CPT:
1326 - $status = 'enrolled';
1327 - }
1328 -
1329 - return apply_filters( 'learn-press/default-user-item-status', $status, $item_id );
1330 -}*/
1331 -
1332 -/**
1333 - * Get current state of distraction mode
1334 - *
1335 - * @return mixed
1336 - * @since 3.1.0
1337 - */
1338 -function learn_press_get_user_distraction() {
1339 - if ( is_user_logged_in() ) {
1340 - return get_user_option( 'distraction_mode', get_current_user_id() );
1341 - } else {
1342 - return LearnPress::instance()->session->distraction_mode;
1343 - }
1344 -}
1345 -
1346 -function learn_press_get_user_role( $user_id ) {
1347 - if ( $user = learn_press_get_user( $user_id ) ) {
1348 - return $user->get_role();
1349 - }
1350 -
1351 - return false;
1352 -}
1353 -
1354 -/**
1355 - * @param array $args
1356 - * @param bool $wp_error
1357 - *
1358 - * @return bool|int|LP_User_Item|mixed|WP_Error
1359 - */
970 +/*
1360 971 function learn_press_create_user_item( $args = array(), $wp_error = false ) {
1361 972 global $wpdb;
1362 973
1363 974 $defaults = array(
@@ -1453,44 +1064,11 @@
1453 1064
1454 1065 do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta );
1455 1066
1456 1067 return $user_item;
1457 -}
1068 +}*/
1458 1069
1459 -/**
1460 - * @param array $args
1461 - * @param bool $wp_error - Optional. TRUE will return WP_Error on fail.
1462 - *
1463 - * @return bool|array|LP_User_Item|WP_Error
1464 - */
1465 -function learn_press_create_user_item_for_quiz( $args = array(), $wp_error = false ) {
1466 - $item_data = wp_parse_args(
1467 - $args,
1468 - array(
1469 - 'item_type' => LP_QUIZ_CPT,
1470 - 'status' => LP_ITEM_STARTED,
1471 - 'graduation' => LP_COURSE_GRADUATION_IN_PROGRESS,
1472 - 'user_id' => get_current_user_id(),
1473 - )
1474 - );
1475 -
1476 - $user_item = learn_press_create_user_item( $item_data, $wp_error );
1477 -
1478 - if ( $user_item && ! is_wp_error( $user_item ) ) {
1479 - $user_item = new LP_User_Item_Quiz( $user_item->get_data() );
1480 - $user_item->update();
1481 - }
1482 -
1483 - return $user_item;
1484 -}
1485 -
1486 -/**
1487 - * Get list user_item_id for Quiz in table learnpress_user_items
1488 - *
1489 - * @param int $quiz_id
1490 - * @param int $course_id
1491 - * @return array || false
1492 - */
1070 +/*
1493 1071 function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) {
1494 1072 global $wpdb;
1495 1073
1496 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 );
@@ -1500,161 +1078,11 @@
1500 1078 return $col;
1501 1079 } else {
1502 1080 return false;
1503 1081 }
1504 -}
1082 +}*/
1505 1083
1506 1084 /**
1507 - * Create new user item prepare for user starts a quiz
1508 - * Update error retry course not work - Nhamdv.
1509 - *
1510 - * @param int $quiz_id
1511 - * @param int $user_id
1512 - * @param int $course_id
1513 - * @param bool $wp_error
1514 - *
1515 - * @return array|bool|LP_User_Item|WP_Error
1516 - * @since 4.0.0
1517 - */
1518 -function learn_press_user_start_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) {
1519 - if ( ! $user_id ) {
1520 - $user_id = get_current_user_id();
1521 - }
1522 -
1523 - global $wpdb;
1524 -
1525 - $query = $wpdb->prepare(
1526 - "
1527 - SELECT user_item_id, item_id id, item_type type
1528 - FROM {$wpdb->learnpress_user_items}
1529 - WHERE user_item_id = (SELECT max(user_item_id)
1530 - FROM {$wpdb->learnpress_user_items}
1531 - WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress'))
1532 - ",
1533 - $user_id,
1534 - $course_id
1535 - );
1536 -
1537 - $parent = $wpdb->get_row( $query );
1538 -
1539 - do_action( 'learn-press/before-user-start-quiz', $quiz_id, $user_id, $course_id );
1540 -
1541 - $user = learn_press_get_user( $user_id );
1542 - $course_data = $user->get_course_data( $course_id );
1543 - $quiz_data = $course_data->get_item( $quiz_id );
1544 -
1545 - $quiz = LP_Quiz::get_quiz( $quiz_id );
1546 - $duration = $quiz->get_duration();
1547 - $user_quiz = learn_press_create_user_item_for_quiz(
1548 - array(
1549 - 'user_item_id' => $quiz_data ? $quiz_data->get_user_item_id() : 0,
1550 - 'item_id' => $quiz->get_id(),
1551 - 'duration' => $duration ? $duration->get() : 0,
1552 - 'user_id' => $user_id,
1553 - 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0,
1554 - 'ref_type' => $parent ? $parent->type : '',
1555 - 'ref_id' => $parent ? $parent->id : '',
1556 - ),
1557 - $wp_error
1558 - );
1559 -
1560 - if ( $user_quiz && ! is_wp_error( $user_quiz ) ) {
1561 - do_action( 'learn-press/user-started-quiz', $user_quiz, $quiz_id, $user_id, $course_id );
1562 - }
1563 -
1564 - // Reset first cache
1565 - //$user_quiz->get_status( 'status', true );
1566 -
1567 - return $user_quiz;
1568 -}
1569 -
1570 -/**
1571 - * Function retake quiz.
1572 - *
1573 - * @param [type] $quiz_id
1574 - * @param integer $user_id
1575 - * @param integer $course_id
1576 - * @param boolean $wp_error
1577 - *
1578 - * @return void
1579 - */
1580 -function learn_press_user_retake_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) {
1581 - if ( ! $user_id ) {
1582 - $user_id = get_current_user_id();
1583 - }
1584 -
1585 - if ( ! $course_id ) {
1586 - return new WP_Error( 'invalid_course_id', esc_html__( 'Invalid Course ID.', 'learnpress' ) );
1587 - }
1588 -
1589 - global $wpdb;
1590 -
1591 - $query = $wpdb->prepare(
1592 - "
1593 - SELECT user_item_id, item_id id, item_type type
1594 - FROM {$wpdb->learnpress_user_items}
1595 - WHERE user_item_id = (SELECT max(user_item_id)
1596 - FROM {$wpdb->learnpress_user_items}
1597 - WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress'))
1598 - ",
1599 - $user_id,
1600 - $course_id
1601 - );
1602 -
1603 - $parent = $wpdb->get_row( $query );
1604 -
1605 - if ( ! $parent ) {
1606 - return new WP_Error( 'invalid_user_item', esc_html__( 'Invalid Quiz', 'learnpress' ) );
1607 - }
1608 -
1609 - $data = learn_press_get_user_item(
1610 - array(
1611 - 'item_id' => $quiz_id,
1612 - 'user_id' => $user_id,
1613 - 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0,
1614 - 'ref_type' => $parent ? $parent->type : LP_COURSE_CPT,
1615 - 'ref_id' => $parent ? $parent->id : '',
1616 - )
1617 - );
1618 -
1619 - $user_item = new LP_User_Item_Quiz( $data );
1620 -
1621 - $ratake_count = get_post_meta( $quiz_id, '_lp_retake_count', true );
1622 -
1623 - if ( $ratake_count > 0 ) {
1624 - $user_item->update_retake_count();
1625 - }
1626 -
1627 - // Create new result in table learnpress_user_item_results.
1628 - LP_User_Items_Result_DB::instance()->insert( $data->user_item_id );
1629 -
1630 - // Remove user_item_meta.
1631 - learn_press_delete_user_item_meta( $data->user_item_id, '_lp_question_checked' );
1632 -
1633 - $user_item->set_status( LP_ITEM_STARTED )
1634 - ->set_start_time( time() ) // Error Retake when change timezone - Nhamdv
1635 - ->set_end_time()
1636 - ->set_graduation( LP_COURSE_GRADUATION_IN_PROGRESS )
1637 - ->update();
1638 -
1639 - // Reset first cache
1640 - //$user_item->get_status( 'status', true );
1641 -
1642 - // Error Retake when change timezone - Nhamdv
1643 - // learn_press_update_user_item_field(
1644 - // array(
1645 - // 'start_time' => current_time( 'mysql', true ),
1646 - // ),
1647 - // array(
1648 - // 'user_item_id' => $data->user_item_id,
1649 - // )
1650 - // );
1651 -
1652 - return $user_item;
1653 -}
1654 -
1655 -
1656 -/**
1657 1085 * Prepares list of questions for rest api.
1658 1086 *
1659 1087 * @param int[] $question_ids
1660 1088 * @param array $args
@@ -1661,9 +1089,9 @@
1661 1089 *
1662 1090 * @return array
1663 1091 * @since 3.3.0
1664 1092 */
1665 -function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ) : array {
1093 +function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ): array {
1666 1094 if ( is_numeric( $args ) ) {
1667 1095
1668 1096 } else {
1669 1097 $args = wp_parse_args(
@@ -1702,12 +1130,12 @@
1702 1130
1703 1131 if ( $instantCheck || $status == 'completed' ) {
1704 1132 $theExplanation = $question->get_explanation();
1705 1133 $checked = in_array( $id, $checkedQuestions );
1706 - $hasExplanation = ! ! $theExplanation;
1134 + $hasExplanation = (bool) $theExplanation;
1707 1135 }
1708 1136
1709 - $mark = $question->get_mark() ? $question->get_mark() : 1;
1137 + $mark = $question->get_mark() ? $question->get_mark() : 1;
1710 1138
1711 1139 $questionData = array(
1712 1140 'object' => $question,
1713 1141 'id' => absint( $id ),
@@ -1785,12 +1213,33 @@
1785 1213 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1786 1214 return;
1787 1215 }
1788 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 +
1789 1236 if ( isset( $_POST['_lp_extra_info'] ) ) {
1790 - update_user_meta( $user_id, '_lp_extra_info', LP_Helper::sanitize_params_submitted( $_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 );
1791 1239 }
1792 1240 }
1241 +
1793 1242 add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' );
1794 1243 add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' );
1795 1244
1796 1245 /**
@@ -1798,9 +1247,9 @@
1798 1247 *
1799 1248 * @param int $user_id
1800 1249 *
1801 1250 * @return array
1802 - * @since 4.0.0
1251 + * @since 4.0.1
1803 1252 */
1804 1253 function learn_press_get_user_extra_profile_info( $user_id = 0 ) {
1805 1254 if ( ! $user_id ) {
1806 1255 $user_id = get_current_user_id();
@@ -1806,30 +1255,61 @@
1806 1255 $user_id = get_current_user_id();
1807 1256 }
1808 1257
1809 1258 $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id );
1810 - $extra_fields = learn_press_get_user_extra_profile_fields();
1259 + $social_fields = learn_press_social_profiles();
1811 1260
1812 - $extra_profile_info = wp_parse_args(
1813 - $extra_profile_info,
1814 - array_fill_keys( array_keys( $extra_fields ), '' )
1815 - );
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 + }
1816 1269
1817 - return apply_filters( 'learn-press/user-extra-profile-info', $extra_profile_info, $user_id );
1270 + return apply_filters( 'learn-press/user-extra-profile-info', $user_socials, $user_id );
1818 1271 }
1819 1272
1273 +/**
1274 + * @return array
1275 + * @since 3.x.x
1276 + * @version 4.0.2
1277 + */
1820 1278 function learn_press_social_profiles() {
1821 1279 return apply_filters(
1822 1280 'learn-press/social-profiles',
1823 1281 array(
1824 - 'facebook',
1825 - 'twitter',
1826 - 'youtube',
1827 - 'linkedin',
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' ),
1828 1286 )
1829 1287 );
1830 1288 }
1831 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 +
1832 1312 function lp_add_default_fields( $fields ) {
1833 1313 $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' );
1834 1314
1835 1315 if ( $first_name === 'yes' ) {
@@ -1836,10 +1316,10 @@
1836 1316 ?>
1837 1317 <li class="form-field">
1838 1318 <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label>
1839 1319 <input id="reg_first_name" name="reg_first_name" type="text"
1840 - placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1841 - value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1320 + placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1321 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1842 1322 </li>
1843 1323 <?php
1844 1324 }
1845 1325
@@ -1849,10 +1329,10 @@
1849 1329 ?>
1850 1330 <li class="form-field">
1851 1331 <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label>
1852 1332 <input id="reg_last_name" name="reg_last_name" type="text"
1853 - placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1854 - value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1333 + placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1334 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1855 1335 </li>
1856 1336 <?php
1857 1337 }
1858 1338
@@ -1862,10 +1342,10 @@
1862 1342 ?>
1863 1343 <li class="form-field">
1864 1344 <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label>
1865 1345 <input id="reg_display_name" name="reg_display_name" type="text"
1866 - placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1867 - value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1346 + placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1347 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1868 1348 </li>
1869 1349 <?php
1870 1350 }
1871 1351 }
@@ -1873,9 +1353,9 @@
1873 1353 add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' );
1874 1354
1875 1355 function lp_custom_register_fields_display() {
1876 1356 ?>
1877 - <?php $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); ?>
1357 + <?php $custom_fields = LP_Profile::get_register_fields_custom(); ?>
1878 1358
1879 1359 <?php if ( $custom_fields ) : ?>
1880 1360 <?php foreach ( $custom_fields as $custom_field ) : ?>
1881 1361 <?php
@@ -1886,11 +1366,12 @@
1886 1366 <style>
1887 1367 .required label {
1888 1368 font-weight: bold;
1889 1369 }
1370 +
1890 1371 .required label:after {
1891 1372 content: ' *';
1892 - display:inline;
1373 + display: inline;
1893 1374 }
1894 1375 </style>
1895 1376 <?php
1896 1377 }
@@ -1909,10 +1390,10 @@
1909 1390 case 'tel':
1910 1391 ?>
1911 1392 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1912 1393 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1913 - type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1914 - value="" />
1394 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1395 + value=""/>
1915 1396 <?php
1916 1397 break;
1917 1398 case 'textarea':
1918 1399 ?>
@@ -1923,9 +1404,9 @@
1923 1404 case 'checkbox':
1924 1405 ?>
1925 1406 <label>
1926 1407 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1927 - type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1408 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1928 1409 <?php echo esc_html( $custom_field['name'] ); ?>
1929 1410 </label>
1930 1411 <?php
1931 1412 break;
@@ -1971,9 +1452,9 @@
1971 1452 return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id );
1972 1453 }
1973 1454
1974 1455 function lp_get_user_custom_fields() {
1975 - $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
1456 + $custom_fields = LP_Settings::get_option( 'register_profile_fields', array() );
1976 1457
1977 1458 $output = array();
1978 1459
1979 1460 if ( $custom_fields ) {
@@ -1991,50 +1472,37 @@
1991 1472 * @param $key
1992 1473 *
1993 1474 * @return bool
1994 1475 * @since 4.0.0
1476 + * @deprecated 4.3.2
1995 1477 */
1996 1478 function learn_press_is_social_profile( $key ) {
1479 + _deprecated_function( __FUNCTION__, '4.3.2' );
1480 +
1481 + return true;
1482 +
1997 1483 $is_socials = learn_press_social_profiles();
1998 1484
1999 1485 return in_array( $key, $is_socials );
2000 1486 }
2001 1487
2002 -function learn_press_social_profile_name( $key ) {
2003 - $name = '';
2004 - switch ( $key ) {
2005 - case 'facebook':
2006 - $name = esc_html__( 'Facebook Profile', 'learnpress' );
2007 - break;
2008 - case 'twitter':
2009 - $name = esc_html__( 'Twitter Profile', 'learnpress' );
2010 - break;
2011 - case 'googleplus':
2012 - $name = esc_html__( 'Google Profile', 'learnpress' );
2013 - break;
2014 - case 'youtube':
2015 - $name = esc_html__( 'Youtube Channel', 'learnpress' );
2016 - break;
2017 - case 'linkedin':
2018 - $name = esc_html__( 'Linkedin Profile', 'learnpress' );
2019 - break;
2020 - default:
2021 - $name = ucfirst( $key );
2022 - }
2023 -
2024 - return apply_filters( 'learn-press/social-profile-name', $name, $key );
2025 -}
2026 -
2027 1488 /**
2028 1489 * Get extra profile fields will be registered in backend profile.
2029 1490 *
2030 1491 * @return array
2031 1492 * @since 4.0.0
1493 + * @deprecated 4.3.2
2032 1494 */
2033 1495 function learn_press_get_user_extra_profile_fields() {
1496 + _deprecated_function( __FUNCTION__, '4.3.2' );
1497 + return array();
1498 +
2034 1499 $socials = learn_press_social_profiles();
2035 - $fields = array();
2036 1500
1501 + return $socials;
1502 +
1503 + $fields = array();
1504 +
2037 1505 foreach ( $socials as $social ) {
2038 1506 $fields[ $social ] = learn_press_social_profile_name( $social );
2039 1507 }
2040 1508
@@ -2055,10 +1523,13 @@
2055 1523
2056 1524 learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) );
2057 1525 learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) );
2058 1526 }
1527 +
2059 1528 add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 );
1529 +add_action( 'show_user_profile', 'learn_press_user_profile_data', 1000 );
2060 1530
1531 +/*
2061 1532 function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) {
2062 1533 if ( empty( $user_id ) ) {
2063 1534 return false;
2064 1535 }
@@ -2099,6 +1570,5 @@
2099 1570 'all' => count( $posts ),
2100 1571 'publish' => count( $public ),
2101 1572 'pending' => count( $pending ),
2102 1573 );
2103 -
2104 -}
1574 +}*/