PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
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 4.1.7.3.2 All 140 releases
← All changes | inc/user/lp-user-functions.php +358 -890 4.1.7.3.2 → 4.4.9 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,53 @@
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();
168 -
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' );
144 + foreach ( $item_types as $item_type ) {
145 + UserService::instance()->add_capabilities_for_roles( $item_type );
208 146 }
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 147 }
246 148
247 -add_action( 'init', 'learn_press_add_user_roles' );
248 -
249 149 /**
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 150 * Get queried user in profile link
359 151 *
360 152 * @return false|WP_User
361 153 * @since 3.0.0
154 + * @deprecated 4.2.9.1
362 155 */
363 156 function learn_press_get_profile_user() {
157 + return LP_Profile::instance()->get_user_current();
158 +
364 159 return LP_Profile::get_queried_user();
365 160 }
366 161
367 162
@@ -394,14 +189,14 @@
394 189 * ....
395 190 * field_name_n => value n
396 191 * )
397 192 * @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).
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).
400 195 *
401 196 * @return mixed
402 197 */
403 -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 ) {
404 199 global $wpdb;
405 200
406 201 // Table fields format.
407 202 $table_fields = array(
@@ -428,12 +223,10 @@
428 223 if ( LP_COURSE_CPT === $item_type ) {
429 224 if ( 'completed' === $fields['status'] ) {
430 225 $fields['status'] = 'finished';
431 226 }
432 - } else {
433 - if ( 'finished' === $fields['status'] ) {
227 + } elseif ( 'finished' === $fields['status'] ) {
434 228 $fields['status'] = 'completed';
435 - }
436 229 }
437 230 }
438 231
439 232 $data = array();
@@ -442,9 +235,9 @@
442 235 if ( ! empty( $table_fields[ $field ] ) ) {
443 236 $data[ $field ] = $value;
444 237
445 238 // Do not format the date-time field if it's value is NULL
446 - if ( in_array( $field, [ 'start_time', 'end_time' ] ) && empty( $value ) ) {
239 + if ( in_array( $field, array( 'start_time', 'end_time' ) ) && empty( $value ) ) {
447 240 $data[ $field ] = null;
448 241 $data_format[] = '';
449 242 } else {
450 243 if ( $value instanceof LP_Datetime ) {
@@ -518,14 +311,52 @@
518 311 } elseif ( $updated ) {
519 312 $updated_item = learn_press_get_user_item( $where );
520 313 }
521 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 +
522 351 /**
523 352 * If there is some fields does not contain in the main table
524 353 * then consider update them as metadata.
354 + *
525 355 * @comment by tungnx - 4.1.7.3
526 356 */
527 - /*if ( $updated_item && $update_extra_fields_as_meta ) {
357 + /*
358 + if ( $updated_item && $update_extra_fields_as_meta ) {
528 359 $extra_fields = array_diff_key( $fields, $table_fields );
529 360 if ( $extra_fields ) {
530 361 foreach ( $extra_fields as $meta_key => $meta_value ) {
531 362 if ( $meta_value == 'user_item_id' ) {
@@ -544,9 +375,9 @@
544 375 }
545 376 }
546 377 }*/
547 378
548 - do_action( 'learn-press/updated-user-item-meta', $updated_item );
379 + // do_action( 'learn-press/updated-user-item-meta', $updated_item );
549 380
550 381 return $updated_item;
551 382 }
552 383
@@ -553,9 +384,9 @@
553 384 /**
554 385 * Get user item row(s) from user items table by multiple WHERE conditional
555 386 *
556 387 * @param array|int $where
557 - * @param bool $single
388 + * @param bool $single
558 389 *
559 390 * @return array
560 391 */
561 392 function learn_press_get_user_item( $where, $single = true ) {
@@ -610,11 +441,11 @@
610 441
611 442 /**
612 443 * Get user item meta from user_itemmeta table
613 444 *
614 - * @param int $user_item_id .
445 + * @param int $user_item_id .
615 446 * @param string $meta_key .
616 - * @param bool $single .
447 + * @param bool $single .
617 448 *
618 449 * @return mixed
619 450 */
620 451 function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) {
@@ -628,11 +459,11 @@
628 459
629 460 /**
630 461 * Add user item meta into table user_itemmeta
631 462 *
632 - * @param int $user_item_id
463 + * @param int $user_item_id
633 464 * @param string $meta_key
634 - * @param mixed $meta_value
465 + * @param mixed $meta_value
635 466 * @param string $prev_value
636 467 *
637 468 * @return false|int
638 469 */
@@ -642,11 +473,11 @@
642 473
643 474 /**
644 475 * Update user item meta to table user_itemmeta
645 476 *
646 - * @param int $user_item_id
477 + * @param int $user_item_id
647 478 * @param string $meta_key
648 - * @param mixed $meta_value
479 + * @param mixed $meta_value
649 480 * @param string $prev_value
650 481 *
651 482 * @return bool|int
652 483 */
@@ -657,12 +488,12 @@
657 488
658 489 /**
659 490 * Update user item meta to table user_itemmeta
660 491 *
661 - * @param int $object_id
492 + * @param int $object_id
662 493 * @param string $meta_key
663 - * @param mixed $meta_value
664 - * @param bool $delete_all
494 + * @param mixed $meta_value
495 + * @param bool $delete_all
665 496 *
666 497 * @return bool|int
667 498 */
668 499 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
@@ -668,101 +499,8 @@
668 499 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
669 500 return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all );
670 501 }
671 502
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 503 if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) {
766 504 /**
767 505 * Filter the avatar
768 506 *
@@ -767,109 +505,105 @@
767 505 * Filter the avatar
768 506 *
769 507 * @param string $avatar
770 508 * @param string $id_or_email
771 - * @param array $size
509 + * @param array $args
772 510 *
773 511 * @return string
512 + * @since 1.0.0
513 + * @version 1.0.2
774 514 */
775 - 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 + }
776 520
777 - $profile = LP_Profile::instance();
521 + $user_id = 0;
778 522
779 - if ( ! $profile->is_enable_avatar() ) {
780 - return $avatar;
781 - }
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 + }
782 541
783 - if ( ( isset( $size['gravatar'] ) && $size['gravatar'] ) || ( $size['default'] && $size['force_default'] ) ) {
784 - return $avatar;
785 - }
542 + if ( ! $user_id ) {
543 + return $avatar;
544 + }
786 545
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;
546 + $user = UserModel::find( $user_id, true );
547 + if ( ! $user ) {
548 + return $avatar;
796 549 }
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 550
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'];
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'] );
825 555 } else {
826 - $height = 250;
827 - $width = 250;
556 + $class[] = $args['class'];
828 557 }
829 - } else {
830 - $img_size = $size['size'];
831 - $height = $size['height'];
832 - $width = $size['width'];
833 558 }
834 559
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 . '" />';
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;
836 575 }
837 -
838 - return $avatar;
839 576 }
840 577 }
841 -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 );
842 579
843 580
844 581 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;
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;
853 588
854 - if ( ! empty( $upload_dir['subdir'] ) ) {
855 - $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
856 - $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'] );
857 592
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 - }
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 + }
864 599
865 - $upload_dir['subdir'] = $subdir;
600 + $upload_dir['subdir'] = $subdir;
866 601
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 - }
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 ] );
872 606 }
873 607 }
874 608
875 609 return $upload_dir;
@@ -874,8 +608,9 @@
874 608
875 609 return $upload_dir;
876 610 }
877 611
612 +/*
878 613 add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 );
879 614 function _learn_press_before_purchase_course_handler( $course_id, $cart ) {
880 615 // Redirect to login page if user is not logged in
881 616 if ( ! is_user_logged_in() ) {
@@ -914,10 +649,11 @@
914 649 wp_redirect( get_the_permalink( $course_id ) );
915 650 exit();
916 651 }
917 652 }
918 -}
653 +}*/
919 654
655 +/*
920 656 function learn_press_user_is( $role, $user_id = 0 ) {
921 657 if ( ! $user_id ) {
922 658 $user = learn_press_get_current_user();
923 659 } else {
@@ -930,10 +666,11 @@
930 666 return $user->is_instructor();
931 667 }
932 668
933 669 return $role;
934 -}
670 +}*/
935 671
672 +/*
936 673 function learn_press_profile_tab_edit_content( $current, $tab, $user ) {
937 674 learn_press_get_template(
938 675 'profile/tabs/edit.php',
939 676 array(
@@ -941,25 +678,11 @@
941 678 'current' => $current,
942 679 'tab' => $tab,
943 680 )
944 681 );
945 -}
682 +}*/
946 683
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 -
684 +/*
962 685 function learn_press_update_user_option( $name, $value, $id = 0 ) {
963 686 if ( ! $id ) {
964 687 $id = get_current_user_id();
965 688 }
@@ -966,16 +689,11 @@
966 689 $key = 'learnpress_user_options';
967 690 $options = get_user_option( $key, $id );
968 691 $options[ $name ] = $value;
969 692 update_user_option( $id, $key, $options, true );
970 -}
693 +}*/
971 694
972 -/**
973 - * @param $name
974 - * @param int $id
975 - *
976 - * @return bool
977 - */
695 +/*
978 696 function learn_press_delete_user_option( $name, $id = 0 ) {
979 697 if ( ! $id ) {
980 698 $id = get_current_user_id();
981 699 }
@@ -988,16 +706,11 @@
988 706 return true;
989 707 }
990 708
991 709 return false;
992 -}
710 +}*/
993 711
994 -/**
995 - * @param $name
996 - * @param int $id
997 - *
998 - * @return bool
999 - */
712 +/*
1000 713 function learn_press_get_user_option( $name, $id = 0 ) {
1001 714 if ( ! $id ) {
1002 715 $id = get_current_user_id();
1003 716 }
@@ -1007,9 +720,9 @@
1007 720 return $options[ $name ];
1008 721 }
1009 722
1010 723 return false;
1011 -}
724 +}*/
1012 725
1013 726 /**
1014 727 * Update user basic information.
1015 728 *
@@ -1025,13 +738,13 @@
1025 738 }
1026 739
1027 740 $update_data = array(
1028 741 '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 ),
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' ),
1034 747 );
1035 748
1036 749 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
1037 750 $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' );
@@ -1038,9 +751,9 @@
1038 751
1039 752 $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta );
1040 753
1041 754 // Update for social.
1042 - $socials = LP_Request::get_array( 'user_profile_social' );
755 + $socials = LP_Request::get_param( 'user_profile_social' );
1043 756 $extra_data = get_user_meta( $user_id, '_lp_extra_info', true );
1044 757
1045 758 if ( ! empty( $extra_data ) ) {
1046 759 $socials = array_merge( $extra_data, $socials );
@@ -1056,55 +769,51 @@
1056 769 }
1057 770
1058 771 /**
1059 772 * Update new password.
773 + *
774 + * @version 1.0.1
1060 775 */
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 ) {
776 +function learn_press_update_user_profile_change_password() {
777 + try {
1072 778 $user = wp_get_current_user();
1073 - require_once ABSPATH . 'wp-includes/class-phpass.php';
1074 - $wp_hasher = new PasswordHash( 8, true );
779 + if ( ! $user ) {
780 + throw new Exception( __( 'The user is invalid', 'learnpress' ) );
781 + }
1075 782
1076 - if ( $wp_hasher->CheckPassword( $old_pass, $user->data->user_pass ) ) {
1077 - $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' ) );
1078 786 }
1079 - }
1080 787
1081 - try {
788 + $check_old_pass = wp_check_password( $old_pass, $user->data->user_pass, $user->ID );
1082 789 if ( ! $check_old_pass ) {
1083 790 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' );
791 + }
1087 792
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 );
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 + }
1096 799
1097 - if ( is_wp_error( $return ) ) {
1098 - return $wp_error ? $return : false;
1099 - }
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 + );
1100 806
1101 - return $return;
1102 - }
807 + $rs = wp_update_user( $update_data );
808 + if ( is_wp_error( $rs ) ) {
809 + throw new Exception( $rs->get_error_message() );
1103 810 }
1104 811 } catch ( Exception $ex ) {
1105 - return $wp_error ? new WP_Error( 'UPDATE_PROFILE_ERROR', $ex->getMessage() ) : false;
812 + return new WP_Error( 'error_lp_profile_update_pass', $ex->getMessage() );
1106 813 }
814 +
815 + return true;
1107 816 }
1108 817
1109 818 function learn_press_get_avatar_thumb_size() {
1110 819 $option = LP_Settings::get_option(
@@ -1121,8 +830,11 @@
1121 830 'height' => 250,
1122 831 );
1123 832 }
1124 833
834 + // For option get_avatar_url
835 + $option['size'] = $option['width'];
836 +
1125 837 return $option;
1126 838 }
1127 839
1128 840 function learn_press_get_course_thumbnail_dimensions() {
@@ -1143,34 +855,14 @@
1143 855
1144 856 return $option;
1145 857 }
1146 858
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 -
859 +/*
1168 860 function learn_press_get_user_avatar( $user_id = 0, $size = '' ) {
1169 861 $user = learn_press_get_user( $user_id );
1170 862
1171 863 return $user->get_profile_picture( '', $size );
1172 -}
864 +}*/
1173 865
1174 866 /**
1175 867 * Get profile instance for an user to view.
1176 868 *
@@ -1181,16 +873,9 @@
1181 873 function learn_press_get_profile( $for_user = 0 ) {
1182 874 return LP_Profile::instance( $for_user );
1183 875 }
1184 876
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 - */
877 +/*
1193 878 function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) {
1194 879 global $wpdb;
1195 880
1196 881 settype( $item_id, 'array' );
@@ -1213,44 +898,30 @@
1213 898 }
1214 899
1215 900 $query = $wpdb->prepare(
1216 901 "
1217 - DELETE
1218 - FROM {$wpdb->learnpress_user_items}
1219 - WHERE user_id = %d
1220 - AND ( item_id IN(" . join( ',', $format ) . ")
1221 - $where )
1222 - ",
902 + DELETE
903 + FROM {$wpdb->learnpress_user_items}
904 + WHERE user_id = %d
905 + AND ( item_id IN(" . join( ',', $format ) . ")
906 + $where )
907 + ",
1223 908 $args
1224 909 );
1225 -}
910 +}*/
1226 911
1227 912 /**
1228 913 * Get user profile link
1229 914 *
1230 - * @param int $user_id
915 + * @param int $user_id
1231 916 * @param null $tab
1232 917 *
1233 918 * @return mixed|string
1234 919 */
1235 -function learn_press_user_profile_link( $user_id = 0, $tab = null ) {
920 +function learn_press_user_profile_link( $user_id = 0, $tab = '' ) {
1236 921 if ( ! $user_id ) {
1237 922 $user_id = get_current_user_id();
1238 923 }
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 924
1254 925 $user = learn_press_get_user( $user_id );
1255 926
1256 927 if ( ! $user ) {
@@ -1257,38 +928,33 @@
1257 928 return '';
1258 929 }
1259 930
1260 931 global $wp_query;
1261 - $args = array(
1262 - '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(),
1263 940 );
1264 941
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 - }
942 + if ( $tab ) {
943 + $args['tab'] = $tab;
944 + }
1272 945
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 - }
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'] );
1280 952 }
1281 953
1282 - /*$args = array_map(
1283 - function ( $string ) {
1284 - return preg_replace( '/\s/', '+', $string );
1285 - },
1286 - $args
1287 - );*/
1288 954 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
1289 955 if ( $profile_link ) {
1290 - if ( get_option( 'permalink_structure' ) /*&& learn_press_get_page_id( 'profile' )*/ ) {
956 + if ( get_option( 'permalink_structure' ) ) {
1291 957 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
1292 958 } else {
1293 959 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
1294 960 }
@@ -1298,66 +964,9 @@
1298 964
1299 965 return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab );
1300 966 }
1301 967
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 - */
968 +/*
1360 969 function learn_press_create_user_item( $args = array(), $wp_error = false ) {
1361 970 global $wpdb;
1362 971
1363 972 $defaults = array(
@@ -1453,44 +1062,11 @@
1453 1062
1454 1063 do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta );
1455 1064
1456 1065 return $user_item;
1457 -}
1066 +}*/
1458 1067
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 - */
1068 +/*
1493 1069 function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) {
1494 1070 global $wpdb;
1495 1071
1496 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 );
@@ -1500,161 +1076,11 @@
1500 1076 return $col;
1501 1077 } else {
1502 1078 return false;
1503 1079 }
1504 -}
1080 +}*/
1505 1081
1506 1082 /**
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 1083 * Prepares list of questions for rest api.
1658 1084 *
1659 1085 * @param int[] $question_ids
1660 1086 * @param array $args
@@ -1661,9 +1087,9 @@
1661 1087 *
1662 1088 * @return array
1663 1089 * @since 3.3.0
1664 1090 */
1665 -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 {
1666 1092 if ( is_numeric( $args ) ) {
1667 1093
1668 1094 } else {
1669 1095 $args = wp_parse_args(
@@ -1702,12 +1128,12 @@
1702 1128
1703 1129 if ( $instantCheck || $status == 'completed' ) {
1704 1130 $theExplanation = $question->get_explanation();
1705 1131 $checked = in_array( $id, $checkedQuestions );
1706 - $hasExplanation = ! ! $theExplanation;
1132 + $hasExplanation = (bool) $theExplanation;
1707 1133 }
1708 1134
1709 - $mark = $question->get_mark() ? $question->get_mark() : 1;
1135 + $mark = $question->get_mark() ? $question->get_mark() : 1;
1710 1136
1711 1137 $questionData = array(
1712 1138 'object' => $question,
1713 1139 'id' => absint( $id ),
@@ -1785,12 +1211,33 @@
1785 1211 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1786 1212 return;
1787 1213 }
1788 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 +
1789 1234 if ( isset( $_POST['_lp_extra_info'] ) ) {
1790 - 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 );
1791 1237 }
1792 1238 }
1239 +
1793 1240 add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' );
1794 1241 add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' );
1795 1242
1796 1243 /**
@@ -1798,9 +1245,9 @@
1798 1245 *
1799 1246 * @param int $user_id
1800 1247 *
1801 1248 * @return array
1802 - * @since 4.0.0
1249 + * @since 4.0.1
1803 1250 */
1804 1251 function learn_press_get_user_extra_profile_info( $user_id = 0 ) {
1805 1252 if ( ! $user_id ) {
1806 1253 $user_id = get_current_user_id();
@@ -1806,30 +1253,61 @@
1806 1253 $user_id = get_current_user_id();
1807 1254 }
1808 1255
1809 1256 $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id );
1810 - $extra_fields = learn_press_get_user_extra_profile_fields();
1257 + $social_fields = learn_press_social_profiles();
1811 1258
1812 - $extra_profile_info = wp_parse_args(
1813 - $extra_profile_info,
1814 - array_fill_keys( array_keys( $extra_fields ), '' )
1815 - );
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 + }
1816 1267
1817 - 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 );
1818 1269 }
1819 1270
1271 +/**
1272 + * @return array
1273 + * @since 3.x.x
1274 + * @version 4.0.2
1275 + */
1820 1276 function learn_press_social_profiles() {
1821 1277 return apply_filters(
1822 1278 'learn-press/social-profiles',
1823 1279 array(
1824 - 'facebook',
1825 - 'twitter',
1826 - 'youtube',
1827 - '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' ),
1828 1284 )
1829 1285 );
1830 1286 }
1831 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 +
1832 1310 function lp_add_default_fields( $fields ) {
1833 1311 $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' );
1834 1312
1835 1313 if ( $first_name === 'yes' ) {
@@ -1836,10 +1314,10 @@
1836 1314 ?>
1837 1315 <li class="form-field">
1838 1316 <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label>
1839 1317 <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'] ?? '' ) ); ?>">
1318 + placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1319 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1842 1320 </li>
1843 1321 <?php
1844 1322 }
1845 1323
@@ -1849,10 +1327,10 @@
1849 1327 ?>
1850 1328 <li class="form-field">
1851 1329 <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label>
1852 1330 <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'] ?? '' ) ); ?>">
1331 + placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1332 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1855 1333 </li>
1856 1334 <?php
1857 1335 }
1858 1336
@@ -1862,10 +1340,10 @@
1862 1340 ?>
1863 1341 <li class="form-field">
1864 1342 <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label>
1865 1343 <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'] ?? '' ) ); ?>">
1344 + placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1345 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1868 1346 </li>
1869 1347 <?php
1870 1348 }
1871 1349 }
@@ -1873,9 +1351,9 @@
1873 1351 add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' );
1874 1352
1875 1353 function lp_custom_register_fields_display() {
1876 1354 ?>
1877 - <?php $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); ?>
1355 + <?php $custom_fields = LP_Profile::get_register_fields_custom(); ?>
1878 1356
1879 1357 <?php if ( $custom_fields ) : ?>
1880 1358 <?php foreach ( $custom_fields as $custom_field ) : ?>
1881 1359 <?php
@@ -1886,11 +1364,12 @@
1886 1364 <style>
1887 1365 .required label {
1888 1366 font-weight: bold;
1889 1367 }
1368 +
1890 1369 .required label:after {
1891 1370 content: ' *';
1892 - display:inline;
1371 + display: inline;
1893 1372 }
1894 1373 </style>
1895 1374 <?php
1896 1375 }
@@ -1909,10 +1388,10 @@
1909 1388 case 'tel':
1910 1389 ?>
1911 1390 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1912 1391 <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="" />
1392 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1393 + value=""/>
1915 1394 <?php
1916 1395 break;
1917 1396 case 'textarea':
1918 1397 ?>
@@ -1923,9 +1402,9 @@
1923 1402 case 'checkbox':
1924 1403 ?>
1925 1404 <label>
1926 1405 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1927 - type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1406 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1928 1407 <?php echo esc_html( $custom_field['name'] ); ?>
1929 1408 </label>
1930 1409 <?php
1931 1410 break;
@@ -1971,9 +1450,9 @@
1971 1450 return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id );
1972 1451 }
1973 1452
1974 1453 function lp_get_user_custom_fields() {
1975 - $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
1454 + $custom_fields = LP_Settings::get_option( 'register_profile_fields', array() );
1976 1455
1977 1456 $output = array();
1978 1457
1979 1458 if ( $custom_fields ) {
@@ -1991,50 +1470,37 @@
1991 1470 * @param $key
1992 1471 *
1993 1472 * @return bool
1994 1473 * @since 4.0.0
1474 + * @deprecated 4.3.2
1995 1475 */
1996 1476 function learn_press_is_social_profile( $key ) {
1477 + _deprecated_function( __FUNCTION__, '4.3.2' );
1478 +
1479 + return true;
1480 +
1997 1481 $is_socials = learn_press_social_profiles();
1998 1482
1999 1483 return in_array( $key, $is_socials );
2000 1484 }
2001 1485
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 1486 /**
2028 1487 * Get extra profile fields will be registered in backend profile.
2029 1488 *
2030 1489 * @return array
2031 1490 * @since 4.0.0
1491 + * @deprecated 4.3.2
2032 1492 */
2033 1493 function learn_press_get_user_extra_profile_fields() {
1494 + _deprecated_function( __FUNCTION__, '4.3.2' );
1495 + return array();
1496 +
2034 1497 $socials = learn_press_social_profiles();
2035 - $fields = array();
2036 1498
1499 + return $socials;
1500 +
1501 + $fields = array();
1502 +
2037 1503 foreach ( $socials as $social ) {
2038 1504 $fields[ $social ] = learn_press_social_profile_name( $social );
2039 1505 }
2040 1506
@@ -2055,10 +1521,13 @@
2055 1521
2056 1522 learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) );
2057 1523 learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) );
2058 1524 }
1525 +
2059 1526 add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 );
1527 +add_action( 'show_user_profile', 'learn_press_user_profile_data', 1000 );
2060 1528
1529 +/*
2061 1530 function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) {
2062 1531 if ( empty( $user_id ) ) {
2063 1532 return false;
2064 1533 }
@@ -2099,6 +1568,5 @@
2099 1568 'all' => count( $posts ),
2100 1569 'publish' => count( $public ),
2101 1570 'pending' => count( $pending ),
2102 1571 );
2103 -
2104 -}
1572 +}*/