PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
← All changes | inc/user/lp-user-functions.php +392 -1032 4.1.74.4.8 View file →
@@ -1,5 +1,10 @@
1 1 <?php
2 +
3 +use LearnPress\Models\UserItems\UserItemModel;
4 +use LearnPress\Models\UserModel;
5 +use LearnPress\Services\UserService;
6 +
2 7 /**
3 8 * Common functions to process actions about user
4 9 *
5 10 * @author ThimPress
@@ -5,14 +10,31 @@
5 10 * @author ThimPress
6 11 * @package LearnPress/Functions/User
7 12 * @version 1.0
8 13 */
9 -
10 14 function learn_press_get_user_profile_tabs() {
11 15 return LP_Profile::instance()->get_tabs();
12 16 }
13 17
14 18 /**
19 + * Ensure new users receive a public slug.
20 + *
21 + * @param int $user_id
22 + *
23 + * @return void
24 + */
25 +function learn_press_maybe_generate_user_pretty_slug_on_register( int $user_id ) {
26 + $userModel = UserModel::find( $user_id, true );
27 + if ( ! $userModel ) {
28 + return;
29 + }
30 +
31 + $userModel->generate_pretty_slug();
32 +}
33 +
34 +//add_action( 'user_register', 'learn_press_maybe_generate_user_pretty_slug_on_register' );
35 +
36 +/**
15 37 * Delete user data by user ID
16 38 *
17 39 * @param int $user_id
18 40 * @param int $course_id
@@ -50,34 +72,19 @@
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 - * @depecated 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 80 function learn_press_get_current_user_id() {
78 81 $user = learn_press_get_current_user();
79 82
83 + if ( ! $user ) {
84 + return 0;
85 + }
86 +
80 87 return $user->get_id();
81 88 }
82 89
83 90 /**
@@ -85,9 +92,9 @@
85 92 * If current user is not logged in, return a GUEST user
86 93 *
87 94 * @param bool $create_temp - Optional. Create temp user if user is not logged in.
88 95 *
89 - * @return bool|LP_User|LP_User_Guest
96 + * @return LP_User|LP_User_Guest
90 97 * @editor tungnx
91 98 * @modify 4.1.4
92 99 * @version 1.0.1
93 100 */
@@ -103,260 +110,53 @@
103 110 }
104 111
105 112 if ( ! function_exists( 'learn_press_get_user' ) ) {
106 113 /**
107 - * 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.
108 116 *
109 - * @param int $user_id
117 + * @param int $user_id
110 118 * @param bool $current
111 119 *
112 - * @return LP_User|LP_User_Guest|mixed
113 - * Todo: check this function - tungnx
120 + * @return LP_User|LP_User_Guest|false
121 + * @since 3.0.0
122 + * @version 4.0.1
114 123 */
115 - function learn_press_get_user( $user_id, $current = false, $force_new = false ) {
116 - $is_guest = false;
117 - if ( ! is_null( LP()->session ) && $user_id != LP()->session->guest_user_id ) {
118 - if ( $current && ! get_user_by( 'id', $user_id ) ) {
119 - $user_id = get_current_user_id();
120 - }
121 - }
122 -
123 - if ( ! $user_id && isset( LP()->session ) ) {
124 - if ( ! LP()->session->guest_user_id ) {
125 - LP()->session->set_customer_session_cookie( 1 );
126 - LP()->session->guest_user_id = time();
127 - }
128 -
129 - $user_id = LP()->session->guest_user_id;
130 - $is_guest = true;
131 - }
132 -
133 - 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 ) {
134 127 return false;
135 128 }
136 129
137 - $user_id = '' . $user_id;
138 -
139 - if ( $force_new || ! array_key_exists( $user_id, LP_Global::$users ) ) {
140 - /**
141 - * LP Hook.
142 - *
143 - * Filter the default class name to get LP user.
144 - *
145 - * @since 3.3.0
146 - */
147 - $userClass = apply_filters( 'learn-press/user-class', $is_guest ? 'LP_User_Guest' : 'LP_User', $is_guest );
148 -
149 - LP_Global::$users[ $user_id ] = new $userClass( $user_id );
150 -
151 - do_action( 'learn-press/get-user', LP_Global::$users[ $user_id ], $user_id );
152 - }
153 -
154 - return LP_Global::$users[ $user_id ];
130 + return new $userClass( $user_id );
155 131 }
156 132 }
157 133
158 134 /**
159 - * Add more 2 user roles teacher and student
135 + * Add roles and add capabilities for roles
160 136 */
161 137 function learn_press_add_user_roles() {
138 + $item_types = [
139 + LP_COURSE_CPT,
140 + LP_LESSON_CPT,
141 + LP_ORDER_CPT,
142 + ];
162 143
163 - $settings = LP_Settings::instance();
164 -
165 - /* translators: user role */
166 - _x( 'LP Instructor', 'User role' );
167 -
168 - add_role(
169 - LP_TEACHER_ROLE,
170 - 'LP Instructor',
171 - array()
172 - );
173 -
174 - $course_cap = LP_COURSE_CPT . 's';
175 - $lesson_cap = LP_LESSON_CPT . 's';
176 - $order_cap = LP_ORDER_CPT . 's';
177 -
178 - $teacher = get_role( LP_TEACHER_ROLE );
179 - if ( $teacher ) {
180 - $teacher->add_cap( 'read_private_' . $course_cap );
181 - $teacher->add_cap( 'delete_published_' . $course_cap );
182 - $teacher->add_cap( 'edit_published_' . $course_cap );
183 - $teacher->add_cap( 'edit_' . $course_cap );
184 - $teacher->add_cap( 'delete_' . $course_cap );
185 - $teacher->add_cap( 'unfiltered_html' );
186 -
187 - $settings->get( 'required_review' );
188 -
189 - if ( $settings->get( 'required_review' ) == 'yes' ) {
190 - $teacher->remove_cap( 'publish_' . $course_cap );
191 - } else {
192 - $teacher->add_cap( 'publish_' . $course_cap );
193 - }
194 -
195 - $teacher->add_cap( 'read_private_' . $lesson_cap );
196 - $teacher->add_cap( 'delete_published_' . $lesson_cap );
197 - $teacher->add_cap( 'edit_published_' . $lesson_cap );
198 - $teacher->add_cap( 'edit_' . $lesson_cap );
199 - $teacher->add_cap( 'delete_' . $lesson_cap );
200 - $teacher->add_cap( 'publish_' . $lesson_cap );
201 - $teacher->add_cap( 'upload_files' );
202 - $teacher->add_cap( 'read' );
203 - $teacher->add_cap( 'edit_posts' );
144 + foreach ( $item_types as $item_type ) {
145 + UserService::instance()->add_capabilities_for_roles( $item_type );
204 146 }
205 -
206 - // administrator
207 - $admin = get_role( 'administrator' );
208 - if ( $admin ) {
209 - $admin->add_cap( 'read_private_' . $course_cap );
210 - $admin->add_cap( 'delete_' . $course_cap );
211 - $admin->add_cap( 'delete_published_' . $course_cap );
212 - $admin->add_cap( 'edit_' . $course_cap );
213 - $admin->add_cap( 'edit_published_' . $course_cap );
214 - $admin->add_cap( 'publish_' . $course_cap );
215 - $admin->add_cap( 'delete_private_' . $course_cap );
216 - $admin->add_cap( 'edit_private_' . $course_cap );
217 - $admin->add_cap( 'delete_others_' . $course_cap );
218 - $admin->add_cap( 'edit_others_' . $course_cap );
219 -
220 - $admin->add_cap( 'read_private_' . $lesson_cap );
221 - $admin->add_cap( 'delete_' . $lesson_cap );
222 - $admin->add_cap( 'delete_published_' . $lesson_cap );
223 - $admin->add_cap( 'edit_' . $lesson_cap );
224 - $admin->add_cap( 'edit_published_' . $lesson_cap );
225 - $admin->add_cap( 'publish_' . $lesson_cap );
226 - $admin->add_cap( 'delete_private_' . $lesson_cap );
227 - $admin->add_cap( 'edit_private_' . $lesson_cap );
228 - $admin->add_cap( 'delete_others_' . $lesson_cap );
229 - $admin->add_cap( 'edit_others_' . $lesson_cap );
230 -
231 - $admin->add_cap( 'delete_' . $order_cap );
232 - $admin->add_cap( 'delete_published_' . $order_cap );
233 - $admin->add_cap( 'edit_' . $order_cap );
234 - $admin->add_cap( 'edit_published_' . $order_cap );
235 - $admin->add_cap( 'publish_' . $order_cap );
236 - $admin->add_cap( 'delete_private_' . $order_cap );
237 - $admin->add_cap( 'edit_private_' . $order_cap );
238 - $admin->add_cap( 'delete_others_' . $order_cap );
239 - $admin->add_cap( 'edit_others_' . $order_cap );
240 - }
241 147 }
242 148
243 -add_action( 'init', 'learn_press_add_user_roles' );
244 -
245 149 /**
246 - * @param null $user_id
247 - * @param array $args
248 - *
249 - * @return mixed
250 - */
251 -function learn_press_get_user_questions( $user_id = null, $args = array() ) {
252 - if ( ! $user_id ) {
253 - $user_id = get_current_user_id();
254 - }
255 -
256 - return learn_press_get_user( $user_id )->get_questions( $args );
257 -}
258 -
259 -/**
260 - * Get the type of current user
261 - *
262 - * @param null $check_type
263 - *
264 - * @return bool|string
265 - */
266 -function learn_press_current_user_is( $check_type = null ) {
267 - global $current_user;
268 - $user_roles = $current_user->roles;
269 - $user_type = '';
270 -
271 - if ( in_array( 'lpr_teacher', $user_roles ) ) {
272 - $user_type = 'instructor';
273 - } elseif ( in_array( 'lp_teacher', $user_roles ) ) {
274 - $user_type = 'instructor';
275 - } elseif ( in_array( 'administrator', $user_roles ) ) {
276 - $user_type = 'administrator';
277 - }
278 -
279 - return $check_type ? $check_type == $user_type : $user_type;
280 -}
281 -
282 -function learn_press_user_has_roles( $roles, $user_id = null ) {
283 - $has_role = false;
284 - if ( ! $user_id ) {
285 - $user = wp_get_current_user();
286 - } else {
287 - $user = get_user_by( 'id', $user_id );
288 - }
289 - $available_roles = (array) $user->roles;
290 - if ( is_array( $roles ) ) {
291 - foreach ( $roles as $role ) {
292 - if ( in_array( $role, $available_roles ) ) {
293 - $has_role = true;
294 - break; // only need one of roles is in available
295 - }
296 - }
297 - } else {
298 - if ( in_array( $roles, $available_roles ) ) {
299 - $has_role = true;
300 - }
301 - }
302 -
303 - return $has_role;
304 -}
305 -
306 -function learn_press_current_user_can_view_profile_section( $section, $user ) {
307 - $current_user = wp_get_current_user();
308 - $view = true;
309 - if ( $user->get_data( 'user_login' ) != $current_user->user_login && $section == LP_Settings::instance()->get(
310 - 'profile_endpoints.profile-orders',
311 - 'profile-orders'
312 - ) ) {
313 - $view = false;
314 - }
315 -
316 - return apply_filters( 'learn_press_current_user_can_view_profile_section', $view, $section, $user );
317 -}
318 -
319 -/*function learn_press_profile_tab_courses_content( $current, $tab, $user ) {
320 - learn_press_get_template(
321 - 'profile/tabs/courses.php',
322 - array(
323 - 'user' => $user,
324 - 'current' => $current,
325 - 'tab' => $tab,
326 - )
327 - );
328 -}*/
329 -
330 -function learn_press_profile_tab_quizzes_content( $current, $tab, $user ) {
331 - learn_press_get_template(
332 - 'profile/tabs/quizzes.php',
333 - array(
334 - 'user' => $user,
335 - 'current' => $current,
336 - 'tab' => $tab,
337 - )
338 - );
339 -}
340 -
341 -function learn_press_profile_tab_orders_content( $current, $tab, $user ) {
342 - learn_press_get_template(
343 - 'profile/tabs/orders.php',
344 - array(
345 - 'user' => $user,
346 - 'current' => $current,
347 - 'tab' => $tab,
348 - )
349 - );
350 -}
351 -
352 -/**
353 150 * Get queried user in profile link
354 151 *
355 152 * @return false|WP_User
356 153 * @since 3.0.0
154 + * @deprecated 4.2.9.1
357 155 */
358 156 function learn_press_get_profile_user() {
157 + return LP_Profile::instance()->get_user_current();
158 +
359 159 return LP_Profile::get_queried_user();
360 160 }
361 161
362 162
@@ -389,18 +189,19 @@
389 189 * ....
390 190 * field_name_n => value n
391 191 * )
392 192 * @param mixed $where - Optional. Fields with values for conditional update with the same format of $fields.
393 - * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0).
394 - * @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).
395 195 *
396 196 * @return mixed
397 197 */
398 -function learn_press_update_user_item_field( $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 ) {
399 199 global $wpdb;
400 200
401 - // Table fields
201 + // Table fields format.
402 202 $table_fields = array(
203 + 'user_item_id' => '%d',
403 204 'user_id' => '%d',
404 205 'item_id' => '%d',
405 206 'ref_id' => '%d',
406 207 'start_time' => '%s',
@@ -422,31 +223,27 @@
422 223 if ( LP_COURSE_CPT === $item_type ) {
423 224 if ( 'completed' === $fields['status'] ) {
424 225 $fields['status'] = 'finished';
425 226 }
426 - } else {
427 - if ( 'finished' === $fields['status'] ) {
227 + } elseif ( 'finished' === $fields['status'] ) {
428 228 $fields['status'] = 'completed';
429 - }
430 229 }
431 230 }
432 231
433 - $data = array();
434 - $data_format = array();
435 - $date_time_fields = array(
436 - 'start_time',
437 - 'end_time',
438 - );
439 -
232 + $data = array();
233 + $data_format = array();
440 234 foreach ( $fields as $field => $value ) {
441 235 if ( ! empty( $table_fields[ $field ] ) ) {
442 236 $data[ $field ] = $value;
443 237
444 238 // Do not format the date-time field if it's value is NULL
445 - if ( in_array( $field, $date_time_fields ) && ! $value ) {
239 + if ( in_array( $field, array( 'start_time', 'end_time' ) ) && empty( $value ) ) {
446 240 $data[ $field ] = null;
447 241 $data_format[] = '';
448 242 } else {
243 + if ( $value instanceof LP_Datetime ) {
244 + $data[ $field ] = $value->toSql();
245 + }
449 246 $data_format[] = $table_fields[ $field ];
450 247 }
451 248 }
452 249 }
@@ -457,18 +254,17 @@
457 254 array( 'user_item_id' => $fields['user_item_id'] )
458 255 );
459 256 }
460 257
461 - if ( $where && empty( $where['user_id'] ) ) {
462 - $where['user_id'] = ! empty( $fields['user_id'] ) ? $fields['user_id'] : learn_press_get_current_user_id();
463 - }
464 -
258 + // Set where and where format
465 259 $where_format = array();
260 + if ( is_array( $where ) && ! empty( $where ) ) {
261 + if ( empty( $where['user_id'] ) ) {
262 + $where['user_id'] = ! empty( $fields['user_id'] ) ? $fields['user_id'] : learn_press_get_current_user_id();
263 + }
466 264
467 - // Build where and where format
468 - if ( $where ) {
469 265 foreach ( $where as $field => $value ) {
470 - if ( ! empty( $table_fields[ $field ] ) ) {
266 + if ( isset( $table_fields[ $field ] ) ) {
471 267 $where_format[] = $table_fields[ $field ];
472 268 }
473 269 }
474 270 }
@@ -479,17 +275,9 @@
479 275
480 276 $inserted = false;
481 277 $updated = false;
482 278
483 - // Ensure all fields are instance of LP_Datetime have to
484 - // convert to string of datetime.
485 - foreach ( $data as $k => $v ) {
486 - if ( $v instanceof LP_Datetime ) {
487 - $data[ $k ] = $v->toSql();
488 - }
489 - }
490 -
491 - // If $where is not empty consider we are updating
279 + // Update to database.
492 280 if ( $where ) {
493 281 $updated = $wpdb->update(
494 282 $wpdb->learnpress_user_items,
495 283 $data,
@@ -497,16 +285,14 @@
497 285 $data_format,
498 286 $where_format
499 287 );
500 288 } else {
501 -
502 - // Otherwise, insert a new one
289 + // Insert to database.
503 290 if ( $wpdb->insert(
504 291 $wpdb->learnpress_user_items,
505 292 $data,
506 293 $data_format
507 - )
508 - ) {
294 + ) ) {
509 295 $inserted = $wpdb->insert_id;
510 296 }
511 297 }
512 298
@@ -525,12 +311,51 @@
525 311 } elseif ( $updated ) {
526 312 $updated_item = learn_press_get_user_item( $where );
527 313 }
528 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 +
529 351 /**
530 352 * If there is some fields does not contain in the main table
531 - * then consider update them as meta data.
353 + * then consider update them as metadata.
354 + *
355 + * @comment by tungnx - 4.1.7.3
532 356 */
357 + /*
533 358 if ( $updated_item && $update_extra_fields_as_meta ) {
534 359 $extra_fields = array_diff_key( $fields, $table_fields );
535 360 if ( $extra_fields ) {
536 361 foreach ( $extra_fields as $meta_key => $meta_value ) {
@@ -548,11 +373,11 @@
548 373 learn_press_update_user_item_meta( $updated_item->user_item_id, $meta_key, $meta_value );
549 374 }
550 375 }
551 376 }
552 - }
377 + }*/
553 378
554 - do_action( 'learn-press/updated-user-item-meta', $updated_item );
379 + // do_action( 'learn-press/updated-user-item-meta', $updated_item );
555 380
556 381 return $updated_item;
557 382 }
558 383
@@ -559,9 +384,9 @@
559 384 /**
560 385 * Get user item row(s) from user items table by multiple WHERE conditional
561 386 *
562 387 * @param array|int $where
563 - * @param bool $single
388 + * @param bool $single
564 389 *
565 390 * @return array
566 391 */
567 392 function learn_press_get_user_item( $where, $single = true ) {
@@ -600,9 +425,9 @@
600 425 SELECT *
601 426 FROM {$wpdb->prefix}learnpress_user_items
602 427 WHERE " . join( ' AND ', $where_str ) . '
603 428 ORDER BY user_item_id DESC
604 - ',
429 + ',
605 430 $where
606 431 );
607 432 if ( $single || ! empty( $where['user_item_id'] ) ) {
608 433 $item = $wpdb->get_row( $query );
@@ -616,11 +441,11 @@
616 441
617 442 /**
618 443 * Get user item meta from user_itemmeta table
619 444 *
620 - * @param int $user_item_id .
445 + * @param int $user_item_id .
621 446 * @param string $meta_key .
622 - * @param bool $single .
447 + * @param bool $single .
623 448 *
624 449 * @return mixed
625 450 */
626 451 function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) {
@@ -634,11 +459,11 @@
634 459
635 460 /**
636 461 * Add user item meta into table user_itemmeta
637 462 *
638 - * @param int $user_item_id
463 + * @param int $user_item_id
639 464 * @param string $meta_key
640 - * @param mixed $meta_value
465 + * @param mixed $meta_value
641 466 * @param string $prev_value
642 467 *
643 468 * @return false|int
644 469 */
@@ -648,11 +473,11 @@
648 473
649 474 /**
650 475 * Update user item meta to table user_itemmeta
651 476 *
652 - * @param int $user_item_id
477 + * @param int $user_item_id
653 478 * @param string $meta_key
654 - * @param mixed $meta_value
479 + * @param mixed $meta_value
655 480 * @param string $prev_value
656 481 *
657 482 * @return bool|int
658 483 */
@@ -663,12 +488,12 @@
663 488
664 489 /**
665 490 * Update user item meta to table user_itemmeta
666 491 *
667 - * @param int $object_id
492 + * @param int $object_id
668 493 * @param string $meta_key
669 - * @param mixed $meta_value
670 - * @param bool $delete_all
494 + * @param mixed $meta_value
495 + * @param bool $delete_all
671 496 *
672 497 * @return bool|int
673 498 */
674 499 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
@@ -674,98 +499,8 @@
674 499 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
675 500 return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all );
676 501 }
677 502
678 -/**
679 - * Get temp users.
680 - *
681 - * @return array
682 - * @depecated 4.1.6.9
683 - */
684 -/*function learn_press_get_temp_users() {
685 - return false;
686 - if ( false === ( $temp_users = LP_Object_Cache::get( 'learn-press/temp-users' ) ) ) {
687 - global $wpdb;
688 - $query = $wpdb->prepare(
689 - "
690 - SELECT ID
691 - FROM {$wpdb->users} u
692 - INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id AND um.meta_key = %s AND um.meta_value = %s
693 - LEFT JOIN {$wpdb->usermeta} um2 ON u.ID = um2.user_id AND um2.meta_key = %s
694 - ",
695 - '_lp_temp_user',
696 - 'yes',
697 - '_lp_expiration'
698 - );
699 -
700 - $temp_users = $wpdb->get_col( $query );
701 -
702 - LP_Object_Cache::set( 'learn-press/temp-users', $temp_users );
703 - }
704 -
705 - return $temp_users;
706 -}*/
707 -
708 -/**
709 - * Update field created_time after added user item meta
710 - *
711 - * @use updated_{meta_type}_meta hook
712 - *
713 - * @param $meta_id
714 - * @param $object_id
715 - * @param $meta_key
716 - * @param $_meta_value
717 - */
718 -function _learn_press_update_created_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
719 - global $wpdb;
720 - $wpdb->update(
721 - $wpdb->learnpress_user_itemmeta,
722 - array( 'create_time' => current_time( 'mysql' ) ),
723 - array( 'meta_id' => $meta_id ),
724 - array( '%s' ),
725 - array( '%d' )
726 - );
727 -}
728 -
729 -// add_action( 'added_learnpress_user_item_meta', '_learn_press_update_created_time_user_item_meta', 10, 4 );
730 -
731 -/**
732 - * Update field updated_time after updated user item meta
733 - *
734 - * @use updated_{meta_type}_meta hook
735 - *
736 - * @param $meta_id
737 - * @param $object_id
738 - * @param $meta_key
739 - * @param $_meta_value
740 - */
741 -function _learn_press_update_updated_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
742 - global $wpdb;
743 - $wpdb->update(
744 - $wpdb->learnpress_user_itemmeta,
745 - array( 'update_time' => current_time( 'mysql' ) ),
746 - array( 'meta_id' => $meta_id ),
747 - array( '%s' ),
748 - array( '%d' )
749 - );
750 -}
751 -
752 -// add_action( 'updated_learnpress_user_item_meta', '_learn_press_update_updated_time_user_item_meta', 10, 4 );
753 -
754 -/**
755 - * @param $status
756 - * @param int $quiz_id
757 - * @param int $user_id
758 - * @param int $course_id
759 - *
760 - * @return bool|mixed
761 - */
762 -function learn_press_user_has_quiz_status( $status, $quiz_id = 0, $user_id = 0, $course_id = 0 ) {
763 - $user = learn_press_get_user( $user_id );
764 -
765 - return $user->has_quiz_status( $status, $quiz_id, $course_id );
766 -}
767 -
768 503 if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) {
769 504 /**
770 505 * Filter the avatar
771 506 *
@@ -770,109 +505,105 @@
770 505 * Filter the avatar
771 506 *
772 507 * @param string $avatar
773 508 * @param string $id_or_email
774 - * @param array $size
509 + * @param array $args
775 510 *
776 511 * @return string
512 + * @since 1.0.0
513 + * @version 1.0.2
777 514 */
778 - 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 + }
779 520
780 - $profile = LP_Profile::instance();
521 + $user_id = 0;
781 522
782 - if ( ! $profile->is_enable_avatar() ) {
783 - return $avatar;
784 - }
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 + }
785 541
786 - if ( ( isset( $size['gravatar'] ) && $size['gravatar'] ) || ( $size['default'] && $size['force_default'] ) ) {
787 - return $avatar;
788 - }
542 + if ( ! $user_id ) {
543 + return $avatar;
544 + }
789 545
790 - $user_id = 0;
791 -
792 - /**
793 - * Get the ID of user from $id_or_email
794 - */
795 - if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) {
796 - $user = get_user_by( 'email', $id_or_email );
797 - if ( $user ) {
798 - $user_id = $user->ID;
546 + $user = UserModel::find( $user_id, true );
547 + if ( ! $user ) {
548 + return $avatar;
799 549 }
800 - } elseif ( is_numeric( $id_or_email ) ) {
801 - $user_id = $id_or_email;
802 - } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) {
803 - $user_id = $id_or_email->user_id;
804 - } elseif ( $id_or_email instanceof WP_Comment ) {
805 - $user = get_user_by( 'email', $id_or_email->comment_author_email );
806 - if ( $user ) {
807 - $user_id = $user->ID;
808 - }
809 - }
810 550
811 - if ( ! $user_id ) {
812 - return $avatar;
813 - }
814 -
815 - $user = LP_User_Factory::get_user( $user_id );
816 -
817 - $profile_picture_src = $user->get_upload_profile_src();
818 - if ( $profile_picture_src ) {
819 - $setting_size = learn_press_get_avatar_thumb_size();
820 - $img_size = '';
821 -
822 - // Get avatar size
823 - if ( ! is_array( $size ) ) {
824 - if ( $size === 'thumbnail' ) {
825 - $img_size = '';
826 - $height = $setting_size['height'];
827 - $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'] );
828 555 } else {
829 - $height = 250;
830 - $width = 250;
556 + $class[] = $args['class'];
831 557 }
832 - } else {
833 - $img_size = $size['size'];
834 - $height = $size['height'];
835 - $width = $size['width'];
836 558 }
837 559
838 - $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;
839 575 }
840 -
841 - return $avatar;
842 576 }
843 577 }
844 -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 );
845 579
846 580
847 581 function learn_press_user_profile_picture_upload_dir( $width_user = true ) {
848 - static $upload_dir;
849 - if ( ! $upload_dir ) {
850 - $upload_dir = wp_upload_dir();
851 - $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user );
852 - if ( $width_user ) {
853 - $subdir .= '/' . get_current_user_id();
854 - }
855 - $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;
856 588
857 - if ( ! empty( $upload_dir['subdir'] ) ) {
858 - $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
859 - $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'] );
860 592
861 - $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path );
862 - $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] );
863 - } else {
864 - $upload_dir['path'] = $upload_dir['path'] . $subdir;
865 - $upload_dir['url'] = $upload_dir['url'] . $subdir;
866 - }
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 + }
867 599
868 - $upload_dir['subdir'] = $subdir;
600 + $upload_dir['subdir'] = $subdir;
869 601
870 - // Point path/url to main site if we are in multisite
871 - if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
872 - foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) {
873 - $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] );
874 - }
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 ] );
875 606 }
876 607 }
877 608
878 609 return $upload_dir;
@@ -877,8 +608,9 @@
877 608
878 609 return $upload_dir;
879 610 }
880 611
612 +/*
881 613 add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 );
882 614 function _learn_press_before_purchase_course_handler( $course_id, $cart ) {
883 615 // Redirect to login page if user is not logged in
884 616 if ( ! is_user_logged_in() ) {
@@ -888,9 +620,9 @@
888 620 'learn_press_purchase_course_login_redirect',
889 621 learn_press_get_login_url( $return_url )
890 622 );
891 623 if ( $redirect !== false ) {
892 - learn_press_add_message( __( 'Please login to enroll this course', 'learnpress' ) );
624 + learn_press_add_message( __( 'Please log in to enroll in this course', 'learnpress' ) );
893 625
894 626 if ( learn_press_is_ajax() ) {
895 627 learn_press_send_json(
896 628 array(
@@ -906,9 +638,9 @@
906 638 } else {
907 639 $user = learn_press_get_current_user();
908 640 $redirect = false;
909 641 if ( $user->has_finished_course( $course_id ) ) {
910 - learn_press_add_message( __( 'You have already finished course', 'learnpress' ) );
642 + learn_press_add_message( __( 'You have already finished the course', 'learnpress' ) );
911 643 $redirect = true;
912 644 } elseif ( $user->has_purchased_course( $course_id ) ) {
913 645 learn_press_add_message( __( 'You have already enrolled in this course', 'learnpress' ) );
914 646 $redirect = true;
@@ -917,10 +649,11 @@
917 649 wp_redirect( get_the_permalink( $course_id ) );
918 650 exit();
919 651 }
920 652 }
921 -}
653 +}*/
922 654
655 +/*
923 656 function learn_press_user_is( $role, $user_id = 0 ) {
924 657 if ( ! $user_id ) {
925 658 $user = learn_press_get_current_user();
926 659 } else {
@@ -933,10 +666,11 @@
933 666 return $user->is_instructor();
934 667 }
935 668
936 669 return $role;
937 -}
670 +}*/
938 671
672 +/*
939 673 function learn_press_profile_tab_edit_content( $current, $tab, $user ) {
940 674 learn_press_get_template(
941 675 'profile/tabs/edit.php',
942 676 array(
@@ -944,25 +678,11 @@
944 678 'current' => $current,
945 679 'tab' => $tab,
946 680 )
947 681 );
948 -}
682 +}*/
949 683
950 -function learn_press_get_profile_endpoints() {
951 - $endpoints = (array) LP_Settings::instance()->get( 'profile_endpoints' );
952 - $tabs = LP_Profile::instance()->get_tabs();
953 - if ( $tabs ) {
954 - foreach ( $tabs as $slug => $info ) {
955 - if ( empty( $endpoints[ $slug ] ) ) {
956 - $endpoints[ $slug ] = $slug;
957 - }
958 - }
959 - }
960 -
961 - return apply_filters( 'learn_press_profile_tab_endpoints', $endpoints );
962 -}
963 -
964 -
684 +/*
965 685 function learn_press_update_user_option( $name, $value, $id = 0 ) {
966 686 if ( ! $id ) {
967 687 $id = get_current_user_id();
968 688 }
@@ -969,16 +689,11 @@
969 689 $key = 'learnpress_user_options';
970 690 $options = get_user_option( $key, $id );
971 691 $options[ $name ] = $value;
972 692 update_user_option( $id, $key, $options, true );
973 -}
693 +}*/
974 694
975 -/**
976 - * @param $name
977 - * @param int $id
978 - *
979 - * @return bool
980 - */
695 +/*
981 696 function learn_press_delete_user_option( $name, $id = 0 ) {
982 697 if ( ! $id ) {
983 698 $id = get_current_user_id();
984 699 }
@@ -991,16 +706,11 @@
991 706 return true;
992 707 }
993 708
994 709 return false;
995 -}
710 +}*/
996 711
997 -/**
998 - * @param $name
999 - * @param int $id
1000 - *
1001 - * @return bool
1002 - */
712 +/*
1003 713 function learn_press_get_user_option( $name, $id = 0 ) {
1004 714 if ( ! $id ) {
1005 715 $id = get_current_user_id();
1006 716 }
@@ -1010,119 +720,11 @@
1010 720 return $options[ $name ];
1011 721 }
1012 722
1013 723 return false;
1014 -}
724 +}*/
1015 725
1016 726 /**
1017 - * Check and update user information from request in user profile page
1018 - */
1019 -function learn_press_update_user_profile() {
1020 -
1021 - if ( ! LP()->is_request( 'post' ) ) {
1022 - return;
1023 - }
1024 - $nonce = learn_press_get_request( 'profile-nonce' );
1025 -
1026 - if ( ! wp_verify_nonce( $nonce, 'learn-press-update-user-profile-' . get_current_user_id() ) ) {
1027 - return;
1028 - }
1029 - $section = learn_press_get_request( 'lp-profile-section' );
1030 -
1031 - do_action( 'learn_press_update_user_profile_' . $section );
1032 - do_action( 'learn_press_update_user_profile', $section );
1033 -}
1034 -
1035 -// add_action( 'init', 'learn_press_update_user_profile' );
1036 -
1037 -// /**
1038 -// * Update user avatar
1039 -// */
1040 -// function learn_press_update_user_profile_avatar() {
1041 -// $user_id = get_current_user_id();
1042 -// $data = learn_press_get_request( 'lp-user-avatar-crop' );
1043 -
1044 -// if ( ! $user_id ) {
1045 -// return new WP_Error( 2, 'User is invalid!' );
1046 -// }
1047 -
1048 -// $upload_dir = learn_press_user_profile_picture_upload_dir();
1049 -
1050 -// if ( learn_press_get_request( 'lp-user-avatar-custom' ) != 'yes' ) {
1051 -// delete_user_meta( get_current_user_id(), '_lp_profile_picture' );
1052 -
1053 -// return false;
1054 -// }
1055 -
1056 -// $path_img = get_user_meta( $user_id, '_lp_profile_picture', true );
1057 -
1058 -// $path = $upload_dir['basedir'] . $path_img;
1059 -
1060 -// if ( ! file_exists( $path ) ) {
1061 -// return false;
1062 -// }
1063 -
1064 -// $filetype = wp_check_filetype( $path );
1065 -
1066 -// if ( 'jpeg' == $filetype['ext'] ) {
1067 -// $im = imagecreatefromjpeg( $path );
1068 -// } elseif ( 'png' == $filetype['ext'] ) {
1069 -// $im = imagecreatefrompng( $path );
1070 -// }
1071 -
1072 -// if ( ! isset( $im ) ) {
1073 -// return false;
1074 -// }
1075 -
1076 -// $points = explode( ',', $data['points'] );
1077 -// $im_crop = imagecreatetruecolor( $data['width'], $data['height'] );
1078 -
1079 -// if ( ! $im ) {
1080 -// return false;
1081 -// }
1082 -
1083 -// $dst_x = 0;
1084 -// $dst_y = 0;
1085 -// $dst_w = $data['width'];
1086 -// $dst_h = $data['height'];
1087 -// $src_x = $points[0];
1088 -// $src_y = $points[1];
1089 -// $src_w = $points[2] - $points[0];
1090 -// $src_h = $points[3] - $points[1];
1091 -
1092 -// imagecopyresampled( $im_crop, $im, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
1093 -
1094 -// $newname = md5( $user_id . microtime( true ) );
1095 -// $output = dirname( $path );
1096 -
1097 -// if ( 'jpeg' == $filetype['ext'] ) {
1098 -// $newname .= '.jpeg';
1099 -// $output .= '/' . $newname;
1100 -// imagejpeg( $im_crop, $output );
1101 -// } elseif ( 'png' == $filetype['ext'] ) {
1102 -// $newname .= '.png';
1103 -// $output .= '/' . $newname;
1104 -// imagepng( $im_crop, $output );
1105 -// }
1106 -
1107 -// $new_avatar = false;
1108 -
1109 -// if ( file_exists( $output ) ) {
1110 -// $new_avatar = preg_replace( '!^/!', '', $upload_dir['subdir'] ) . '/' . $newname;
1111 -// update_user_meta( $user_id, '_lp_profile_picture', '/' . $new_avatar );
1112 -// update_user_meta( $user_id, '_lp_profile_picture_changed', 'yes' );
1113 -
1114 -// $new_avatar = $upload_dir['baseurl'] . '/' . $new_avatar;
1115 -// }
1116 -
1117 -// @unlink( $path );
1118 -
1119 -// return $new_avatar;
1120 -// }
1121 -
1122 -// add_action( 'learn_press_update_user_profile_avatar', 'learn_press_update_user_profile_avatar' );
1123 -
1124 -/**
1125 727 * Update user basic information.
1126 728 *
1127 729 * @param bool $wp_error - Optional. Return WP_Error object in case updating failed.
1128 730 *
@@ -1131,18 +733,18 @@
1131 733 function learn_press_update_user_profile_basic_information( $wp_error = false ) {
1132 734 $user_id = get_current_user_id();
1133 735
1134 736 if ( ! $user_id ) {
1135 - return new WP_Error( 2, 'User is invalid!' );
737 + return new WP_Error( 2, 'The user is invalid' );
1136 738 }
1137 739
1138 740 $update_data = array(
1139 741 'ID' => $user_id,
1140 - 'first_name' => filter_input( INPUT_POST, 'first_name', FILTER_SANITIZE_STRING ),
1141 - 'last_name' => filter_input( INPUT_POST, 'last_name', FILTER_SANITIZE_STRING ),
1142 - 'description' => filter_input( INPUT_POST, 'description', FILTER_SANITIZE_STRING ),
1143 - 'display_name' => filter_input( INPUT_POST, 'account_display_name', FILTER_SANITIZE_STRING ),
1144 - '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' ),
1145 747 );
1146 748
1147 749 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
1148 750 $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' );
@@ -1149,9 +751,9 @@
1149 751
1150 752 $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta );
1151 753
1152 754 // Update for social.
1153 - $socials = LP_Request::get_array( 'user_profile_social' );
755 + $socials = LP_Request::get_param( 'user_profile_social' );
1154 756 $extra_data = get_user_meta( $user_id, '_lp_extra_info', true );
1155 757
1156 758 if ( ! empty( $extra_data ) ) {
1157 759 $socials = array_merge( $extra_data, $socials );
@@ -1167,55 +769,51 @@
1167 769 }
1168 770
1169 771 /**
1170 772 * Update new password.
773 + *
774 + * @version 1.0.1
1171 775 */
1172 -function learn_press_update_user_profile_change_password( $wp_error = false ) {
1173 - $user_id = get_current_user_id();
1174 -
1175 - if ( ! $user_id ) {
1176 - return new WP_Error( 2, 'User is invalid!' );
1177 - }
1178 -
1179 - $old_pass = filter_input( INPUT_POST, 'pass0' );
1180 - $check_old_pass = false;
1181 -
1182 - if ( $old_pass ) {
776 +function learn_press_update_user_profile_change_password() {
777 + try {
1183 778 $user = wp_get_current_user();
1184 - require_once ABSPATH . 'wp-includes/class-phpass.php';
1185 - $wp_hasher = new PasswordHash( 8, true );
779 + if ( ! $user ) {
780 + throw new Exception( __( 'The user is invalid', 'learnpress' ) );
781 + }
1186 782
1187 - if ( $wp_hasher->CheckPassword( $old_pass, $user->data->user_pass ) ) {
1188 - $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' ) );
1189 786 }
1190 - }
1191 787
1192 - try {
788 + $check_old_pass = wp_check_password( $old_pass, $user->data->user_pass, $user->ID );
1193 789 if ( ! $check_old_pass ) {
1194 - throw new Exception( __( 'Old password incorrect!', 'learnpress' ) );
1195 - } else {
1196 - $new_pass = filter_input( INPUT_POST, 'pass1' );
1197 - $new_pass2 = filter_input( INPUT_POST, 'pass2' );
790 + throw new Exception( __( 'The old password is incorrect!', 'learnpress' ) );
791 + }
1198 792
1199 - if ( ! $new_pass || ! $new_pass2 || ( $new_pass != $new_pass2 ) ) {
1200 - throw new Exception( __( 'Confirmation password incorrect!', 'learnpress' ) );
1201 - } else {
1202 - $update_data = array(
1203 - 'user_pass' => $new_pass,
1204 - 'ID' => get_current_user_id(),
1205 - );
1206 - $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 + }
1207 799
1208 - if ( is_wp_error( $return ) ) {
1209 - return $wp_error ? $return : false;
1210 - }
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 + );
1211 806
1212 - return $return;
1213 - }
807 + $rs = wp_update_user( $update_data );
808 + if ( is_wp_error( $rs ) ) {
809 + throw new Exception( $rs->get_error_message() );
1214 810 }
1215 811 } catch ( Exception $ex ) {
1216 - return $wp_error ? new WP_Error( 'UPDATE_PROFILE_ERROR', $ex->getMessage() ) : false;
812 + return new WP_Error( 'error_lp_profile_update_pass', $ex->getMessage() );
1217 813 }
814 +
815 + return true;
1218 816 }
1219 817
1220 818 function learn_press_get_avatar_thumb_size() {
1221 819 $option = LP_Settings::get_option(
@@ -1232,8 +830,11 @@
1232 830 'height' => 250,
1233 831 );
1234 832 }
1235 833
834 + // For option get_avatar_url
835 + $option['size'] = $option['width'];
836 +
1236 837 return $option;
1237 838 }
1238 839
1239 840 function learn_press_get_course_thumbnail_dimensions() {
@@ -1254,34 +855,14 @@
1254 855
1255 856 return $option;
1256 857 }
1257 858
1258 -/**
1259 - * Set a fake cookie to
1260 - */
1261 -function learn_press_set_user_cookie_for_guest() {
1262 - if ( ! is_admin() && ! headers_sent() ) {
1263 - $guest_key = '_wordpress_lp_guest';
1264 -
1265 - if ( is_user_logged_in() ) {
1266 - if ( ! empty( $_COOKIE[ $guest_key ] ) ) {
1267 - learn_press_remove_cookie( $guest_key );
1268 - }
1269 - } else {
1270 - if ( empty( $_COOKIE[ $guest_key ] ) ) {
1271 - learn_press_setcookie( $guest_key, md5( time() ), time() + 3600 );
1272 - }
1273 - }
1274 - }
1275 -}
1276 -
1277 -add_action( 'wp', 'learn_press_set_user_cookie_for_guest' );
1278 -
859 +/*
1279 860 function learn_press_get_user_avatar( $user_id = 0, $size = '' ) {
1280 861 $user = learn_press_get_user( $user_id );
1281 862
1282 863 return $user->get_profile_picture( '', $size );
1283 -}
864 +}*/
1284 865
1285 866 /**
1286 867 * Get profile instance for an user to view.
1287 868 *
@@ -1292,16 +873,9 @@
1292 873 function learn_press_get_profile( $for_user = 0 ) {
1293 874 return LP_Profile::instance( $for_user );
1294 875 }
1295 876
1296 -/**
1297 - * Remove items from learnpress_user_items.
1298 - *
1299 - * @param int $user_id
1300 - * @param int $item_id
1301 - * @param int $course_id
1302 - * @param bool $include_course - Optional. If TRUE then remove course and it's items
1303 - */
877 +/*
1304 878 function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) {
1305 879 global $wpdb;
1306 880
1307 881 settype( $item_id, 'array' );
@@ -1324,44 +898,30 @@
1324 898 }
1325 899
1326 900 $query = $wpdb->prepare(
1327 901 "
1328 - DELETE
1329 - FROM {$wpdb->learnpress_user_items}
1330 - WHERE user_id = %d
1331 - AND ( item_id IN(" . join( ',', $format ) . ")
1332 - $where )
1333 - ",
902 + DELETE
903 + FROM {$wpdb->learnpress_user_items}
904 + WHERE user_id = %d
905 + AND ( item_id IN(" . join( ',', $format ) . ")
906 + $where )
907 + ",
1334 908 $args
1335 909 );
1336 -}
910 +}*/
1337 911
1338 912 /**
1339 913 * Get user profile link
1340 914 *
1341 - * @param int $user_id
915 + * @param int $user_id
1342 916 * @param null $tab
1343 917 *
1344 918 * @return mixed|string
1345 919 */
1346 -function learn_press_user_profile_link( $user_id = 0, $tab = null ) {
920 +function learn_press_user_profile_link( $user_id = 0, $tab = '' ) {
1347 921 if ( ! $user_id ) {
1348 922 $user_id = get_current_user_id();
1349 923 }
1350 - $user = false;
1351 - $deleted = in_array( $user_id, LP_User_Factory::$_deleted_users );
1352 - if ( ! $deleted ) {
1353 - if ( is_numeric( $user_id ) ) {
1354 - $user = get_user_by( 'id', $user_id );
1355 - } else {
1356 - $user = get_user_by( 'login', urldecode( $user_id ) );
1357 - }
1358 - } else {
1359 - return '';
1360 - }
1361 - if ( ! $deleted && ! $user ) {
1362 - LP_User_Factory::$_deleted_users[] = $user_id;
1363 - }
1364 924
1365 925 $user = learn_press_get_user( $user_id );
1366 926
1367 927 if ( ! $user ) {
@@ -1368,32 +928,33 @@
1368 928 return '';
1369 929 }
1370 930
1371 931 global $wp_query;
1372 - $args = array(
1373 - '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(),
1374 940 );
1375 941
1376 - if ( isset( $args['user'] ) ) {
1377 - if ( '' === $tab ) {
1378 - $tab = learn_press_get_current_profile_tab();
1379 - }
1380 - if ( $tab ) {
1381 - $args['tab'] = $tab;
1382 - }
942 + if ( $tab ) {
943 + $args['tab'] = $tab;
944 + }
1383 945
1384 - /**
1385 - * If no tab is selected in profile and is current user
1386 - * then no need the username in profile link.
1387 - */
1388 - if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) {
1389 - unset( $args['user'] );
1390 - }
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'] );
1391 952 }
1392 - $args = array_map( '_learn_press_urlencode', $args );
953 +
1393 954 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
1394 955 if ( $profile_link ) {
1395 - if ( get_option( 'permalink_structure' ) /*&& learn_press_get_page_id( 'profile' )*/ ) {
956 + if ( get_option( 'permalink_structure' ) ) {
1396 957 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
1397 958 } else {
1398 959 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
1399 960 }
@@ -1403,66 +964,9 @@
1403 964
1404 965 return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab );
1405 966 }
1406 967
1407 -/**********************************************/
1408 -/* Functions are used for hooks */
1409 -/**********************************************/
1410 -
1411 -function learn_press_hk_before_start_quiz( $true, $quiz_id, $course_id, $user_id ) {
1412 - if ( 'yes' !== get_post_meta( $quiz_id, '_lp_archive_history', true ) ) {
1413 - learn_press_remove_user_items( $user_id, $quiz_id, $course_id );
1414 - }
1415 -
1416 - return $true;
1417 -}
1418 -
1419 -add_filter( 'learn-press/before-start-quiz', 'learn_press_hk_before_start_quiz', 10, 4 );
1420 -
1421 -/*function learn_press_default_user_item_status( $item_id ) {
1422 - $status = '';
1423 - switch ( learn_press_get_post_type( $item_id ) ) {
1424 - case LP_LESSON_CPT:
1425 - $status = 'started';
1426 - break;
1427 - case LP_QUIZ_CPT:
1428 - $status = 'viewed';
1429 - break;
1430 - case LP_COURSE_CPT:
1431 - $status = 'enrolled';
1432 - }
1433 -
1434 - return apply_filters( 'learn-press/default-user-item-status', $status, $item_id );
1435 -}*/
1436 -
1437 -/**
1438 - * Get current state of distraction mode
1439 - *
1440 - * @return mixed
1441 - * @since 3.1.0
1442 - */
1443 -function learn_press_get_user_distraction() {
1444 - if ( is_user_logged_in() ) {
1445 - return get_user_option( 'distraction_mode', get_current_user_id() );
1446 - } else {
1447 - return LP()->session->distraction_mode;
1448 - }
1449 -}
1450 -
1451 -function learn_press_get_user_role( $user_id ) {
1452 - if ( $user = learn_press_get_user( $user_id ) ) {
1453 - return $user->get_role();
1454 - }
1455 -
1456 - return false;
1457 -}
1458 -
1459 -/**
1460 - * @param array $args
1461 - * @param bool $wp_error
1462 - *
1463 - * @return bool|int|LP_User_Item|mixed|WP_Error
1464 - */
968 +/*
1465 969 function learn_press_create_user_item( $args = array(), $wp_error = false ) {
1466 970 global $wpdb;
1467 971
1468 972 $defaults = array(
@@ -1467,15 +971,15 @@
1467 971
1468 972 $defaults = array(
1469 973 'user_id' => get_current_user_id(),
1470 974 'item_id' => '',
1471 - 'start_time' => current_time( 'mysql', true ),
975 + 'start_time' => time(),
1472 976 'end_time' => '',
1473 977 'graduation' => '',
1474 978 'item_type' => '',
1475 979 'status' => '',
1476 980 'ref_id' => 0,
1477 - 'ref_type' => 0,
981 + 'ref_type' => '',
1478 982 'parent_id' => 0,
1479 983 'create_meta' => array(),
1480 984 );
1481 985
@@ -1489,9 +993,10 @@
1489 993
1490 994 return 0;
1491 995 }
1492 996
1493 - if ( empty( $item_data['item_type'] ) && $post_type = learn_press_get_post_type( $item_data['item_id'] ) ) {
997 + $post_type = learn_press_get_post_type( $item_data['item_id'] );
998 + if ( empty( $item_data['item_type'] ) && $post_type ) {
1494 999 $item_data['item_type'] = $post_type;
1495 1000 }
1496 1001
1497 1002 // Get id and type of ref if they are null
@@ -1514,9 +1019,10 @@
1514 1019 }
1515 1020 }
1516 1021
1517 1022 // Filter
1518 - if ( ! $item_data = apply_filters( 'learn-press/create-user-item-data', $item_data ) ) {
1023 + $item_data = apply_filters( 'learn-press/create-user-item-data', $item_data );
1024 + if ( ! $item_data ) {
1519 1025 if ( $wp_error ) {
1520 1026 return new WP_Error( 'invalid_item_data', __( 'Invalid item data.', 'learnpress' ) );
1521 1027 }
1522 1028
@@ -1531,13 +1037,10 @@
1531 1037 unset( $item_data['create_meta'] );
1532 1038 }
1533 1039
1534 1040 $user_item = new LP_User_Item( $item_data );
1535 -
1536 - $result = $user_item->update( true, false );
1537 -
1041 + $result = $user_item->update();
1538 1042 if ( ! $result || is_wp_error( $result ) ) {
1539 -
1540 1043 if ( $wp_error && is_wp_error( $result ) ) {
1541 1044 return $result;
1542 1045 }
1543 1046
@@ -1559,46 +1062,11 @@
1559 1062
1560 1063 do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta );
1561 1064
1562 1065 return $user_item;
1563 -}
1066 +}*/
1564 1067
1565 -/**
1566 - * @param array $args
1567 - * @param bool $wp_error - Optional. TRUE will return WP_Error on fail.
1568 - *
1569 - * @return bool|array|LP_User_Item|WP_Error
1570 - */
1571 -function learn_press_create_user_item_for_quiz( $args = array(), $wp_error = false ) {
1572 - global $wpdb;
1573 -
1574 - $item_data = wp_parse_args(
1575 - $args,
1576 - array(
1577 - 'item_type' => LP_QUIZ_CPT,
1578 - 'status' => LP_ITEM_STARTED,
1579 - 'graduation' => LP_COURSE_GRADUATION_IN_PROGRESS,
1580 - 'user_id' => get_current_user_id(),
1581 - )
1582 - );
1583 -
1584 - $user_item = learn_press_create_user_item( $item_data, $wp_error );
1585 -
1586 - if ( $user_item && ! is_wp_error( $user_item ) ) {
1587 - $user_item = new LP_User_Item_Quiz( $user_item->get_data() );
1588 - $user_item->update( true );
1589 - }
1590 -
1591 - return $user_item;
1592 -}
1593 -
1594 -/**
1595 - * Get list user_item_id for Quiz in table learnpress_user_items
1596 - *
1597 - * @param int $quiz_id
1598 - * @param int $course_id
1599 - * @return array || false
1600 - */
1068 +/*
1601 1069 function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) {
1602 1070 global $wpdb;
1603 1071
1604 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 );
@@ -1608,161 +1076,11 @@
1608 1076 return $col;
1609 1077 } else {
1610 1078 return false;
1611 1079 }
1612 -}
1080 +}*/
1613 1081
1614 1082 /**
1615 - * Create new user item prepare for user starts a quiz
1616 - * Update error retry course not work - Nhamdv.
1617 - *
1618 - * @param int $quiz_id
1619 - * @param int $user_id
1620 - * @param int $course_id
1621 - * @param bool $wp_error
1622 - *
1623 - * @return array|bool|LP_User_Item|WP_Error
1624 - * @since 4.0.0
1625 - */
1626 -function learn_press_user_start_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) {
1627 - if ( ! $user_id ) {
1628 - $user_id = get_current_user_id();
1629 - }
1630 -
1631 - global $wpdb;
1632 -
1633 - $query = $wpdb->prepare(
1634 - "
1635 - SELECT user_item_id, item_id id, item_type type
1636 - FROM {$wpdb->learnpress_user_items}
1637 - WHERE user_item_id = (SELECT max(user_item_id)
1638 - FROM {$wpdb->learnpress_user_items}
1639 - WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress'))
1640 - ",
1641 - $user_id,
1642 - $course_id
1643 - );
1644 -
1645 - $parent = $wpdb->get_row( $query );
1646 -
1647 - do_action( 'learn-press/before-user-start-quiz', $quiz_id, $user_id, $course_id );
1648 -
1649 - $user = learn_press_get_user( $user_id );
1650 - $course_data = $user->get_course_data( $course_id );
1651 - $quiz_data = $course_data->get_item( $quiz_id );
1652 -
1653 - $quiz = LP_Quiz::get_quiz( $quiz_id );
1654 - $duration = $quiz->get_duration();
1655 - $user_quiz = learn_press_create_user_item_for_quiz(
1656 - array(
1657 - 'user_item_id' => $quiz_data ? $quiz_data->get_user_item_id() : 0,
1658 - 'item_id' => $quiz->get_id(),
1659 - 'duration' => $duration ? $duration->get() : 0,
1660 - 'user_id' => $user_id,
1661 - 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0,
1662 - 'ref_type' => $parent ? $parent->type : '',
1663 - 'ref_id' => $parent ? $parent->id : '',
1664 - ),
1665 - $wp_error
1666 - );
1667 -
1668 - if ( $user_quiz && ! is_wp_error( $user_quiz ) ) {
1669 - do_action( 'learn-press/user-started-quiz', $user_quiz, $quiz_id, $user_id, $course_id );
1670 - }
1671 -
1672 - // Reset first cache
1673 - $user_quiz->get_status( 'status', true );
1674 -
1675 - return $user_quiz;
1676 -}
1677 -
1678 -/**
1679 - * Function retake quiz.
1680 - *
1681 - * @param [type] $quiz_id
1682 - * @param integer $user_id
1683 - * @param integer $course_id
1684 - * @param boolean $wp_error
1685 - *
1686 - * @return void
1687 - */
1688 -function learn_press_user_retake_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) {
1689 - if ( ! $user_id ) {
1690 - $user_id = get_current_user_id();
1691 - }
1692 -
1693 - if ( ! $course_id ) {
1694 - return new WP_Error( 'invalid_course_id', esc_html__( 'Invalid Course ID.', 'learnpress' ) );
1695 - }
1696 -
1697 - global $wpdb;
1698 -
1699 - $query = $wpdb->prepare(
1700 - "
1701 - SELECT user_item_id, item_id id, item_type type
1702 - FROM {$wpdb->learnpress_user_items}
1703 - WHERE user_item_id = (SELECT max(user_item_id)
1704 - FROM {$wpdb->learnpress_user_items}
1705 - WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress'))
1706 - ",
1707 - $user_id,
1708 - $course_id
1709 - );
1710 -
1711 - $parent = $wpdb->get_row( $query );
1712 -
1713 - if ( ! $parent ) {
1714 - return new WP_Error( 'invalid_user_item', esc_html__( 'Invalid Quiz', 'learnpress' ) );
1715 - }
1716 -
1717 - $data = learn_press_get_user_item(
1718 - array(
1719 - 'item_id' => $quiz_id,
1720 - 'user_id' => $user_id,
1721 - 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0,
1722 - 'ref_type' => $parent ? $parent->type : LP_COURSE_CPT,
1723 - 'ref_id' => $parent ? $parent->id : '',
1724 - )
1725 - );
1726 -
1727 - $user_item = new LP_User_Item_Quiz( $data );
1728 -
1729 - $ratake_count = get_post_meta( $quiz_id, '_lp_retake_count', true );
1730 -
1731 - if ( $ratake_count > 0 ) {
1732 - $user_item->update_retake_count();
1733 - }
1734 -
1735 - // Create new result in table learnpress_user_item_results.
1736 - LP_User_Items_Result_DB::instance()->insert( $data->user_item_id );
1737 -
1738 - // Remove user_item_meta.
1739 - learn_press_delete_user_item_meta( $data->user_item_id, '_lp_question_checked' );
1740 -
1741 - $user_item->set_status( LP_ITEM_STARTED )
1742 - ->set_start_time( current_time( 'mysql', 1 ) ) // Error Retake when change timezone - Nhamdv
1743 - ->set_end_time( '' )
1744 - ->set_graduation( LP_COURSE_GRADUATION_IN_PROGRESS )
1745 - ->update();
1746 -
1747 - // Reset first cache
1748 - $user_item->get_status( 'status', true );
1749 -
1750 - // Error Retake when change timezone - Nhamdv
1751 - // learn_press_update_user_item_field(
1752 - // array(
1753 - // 'start_time' => current_time( 'mysql', true ),
1754 - // ),
1755 - // array(
1756 - // 'user_item_id' => $data->user_item_id,
1757 - // )
1758 - // );
1759 -
1760 - return $user_item;
1761 -}
1762 -
1763 -
1764 -/**
1765 1083 * Prepares list of questions for rest api.
1766 1084 *
1767 1085 * @param int[] $question_ids
1768 1086 * @param array $args
@@ -1769,9 +1087,9 @@
1769 1087 *
1770 1088 * @return array
1771 1089 * @since 3.3.0
1772 1090 */
1773 -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 {
1774 1092 if ( is_numeric( $args ) ) {
1775 1093
1776 1094 } else {
1777 1095 $args = wp_parse_args(
@@ -1810,12 +1128,12 @@
1810 1128
1811 1129 if ( $instantCheck || $status == 'completed' ) {
1812 1130 $theExplanation = $question->get_explanation();
1813 1131 $checked = in_array( $id, $checkedQuestions );
1814 - $hasExplanation = ! ! $theExplanation;
1132 + $hasExplanation = (bool) $theExplanation;
1815 1133 }
1816 1134
1817 - $mark = $question->get_mark() ? $question->get_mark() : 1;
1135 + $mark = $question->get_mark() ? $question->get_mark() : 1;
1818 1136
1819 1137 $questionData = array(
1820 1138 'object' => $question,
1821 1139 'id' => absint( $id ),
@@ -1893,12 +1211,33 @@
1893 1211 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1894 1212 return;
1895 1213 }
1896 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 +
1897 1234 if ( isset( $_POST['_lp_extra_info'] ) ) {
1898 - 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 );
1899 1237 }
1900 1238 }
1239 +
1901 1240 add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' );
1902 1241 add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' );
1903 1242
1904 1243 /**
@@ -1906,9 +1245,9 @@
1906 1245 *
1907 1246 * @param int $user_id
1908 1247 *
1909 1248 * @return array
1910 - * @since 4.0.0
1249 + * @since 4.0.1
1911 1250 */
1912 1251 function learn_press_get_user_extra_profile_info( $user_id = 0 ) {
1913 1252 if ( ! $user_id ) {
1914 1253 $user_id = get_current_user_id();
@@ -1914,30 +1253,61 @@
1914 1253 $user_id = get_current_user_id();
1915 1254 }
1916 1255
1917 1256 $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id );
1918 - $extra_fields = learn_press_get_user_extra_profile_fields();
1257 + $social_fields = learn_press_social_profiles();
1919 1258
1920 - $extra_profile_info = wp_parse_args(
1921 - $extra_profile_info,
1922 - array_fill_keys( array_keys( $extra_fields ), '' )
1923 - );
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 + }
1924 1267
1925 - 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 );
1926 1269 }
1927 1270
1271 +/**
1272 + * @return array
1273 + * @since 3.x.x
1274 + * @version 4.0.2
1275 + */
1928 1276 function learn_press_social_profiles() {
1929 1277 return apply_filters(
1930 1278 'learn-press/social-profiles',
1931 1279 array(
1932 - 'facebook',
1933 - 'twitter',
1934 - 'youtube',
1935 - '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' ),
1936 1284 )
1937 1285 );
1938 1286 }
1939 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 +
1940 1310 function lp_add_default_fields( $fields ) {
1941 1311 $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' );
1942 1312
1943 1313 if ( $first_name === 'yes' ) {
@@ -1944,10 +1314,10 @@
1944 1314 ?>
1945 1315 <li class="form-field">
1946 1316 <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label>
1947 1317 <input id="reg_first_name" name="reg_first_name" type="text"
1948 - placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1949 - 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'] ?? '' ) ); ?>">
1950 1320 </li>
1951 1321 <?php
1952 1322 }
1953 1323
@@ -1957,10 +1327,10 @@
1957 1327 ?>
1958 1328 <li class="form-field">
1959 1329 <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label>
1960 1330 <input id="reg_last_name" name="reg_last_name" type="text"
1961 - placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1962 - 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'] ?? '' ) ); ?>">
1963 1333 </li>
1964 1334 <?php
1965 1335 }
1966 1336
@@ -1970,10 +1340,10 @@
1970 1340 ?>
1971 1341 <li class="form-field">
1972 1342 <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label>
1973 1343 <input id="reg_display_name" name="reg_display_name" type="text"
1974 - placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1975 - 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'] ?? '' ) ); ?>">
1976 1346 </li>
1977 1347 <?php
1978 1348 }
1979 1349 }
@@ -1981,9 +1351,9 @@
1981 1351 add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' );
1982 1352
1983 1353 function lp_custom_register_fields_display() {
1984 1354 ?>
1985 - <?php $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); ?>
1355 + <?php $custom_fields = LP_Profile::get_register_fields_custom(); ?>
1986 1356
1987 1357 <?php if ( $custom_fields ) : ?>
1988 1358 <?php foreach ( $custom_fields as $custom_field ) : ?>
1989 1359 <?php
@@ -1994,11 +1364,12 @@
1994 1364 <style>
1995 1365 .required label {
1996 1366 font-weight: bold;
1997 1367 }
1368 +
1998 1369 .required label:after {
1999 1370 content: ' *';
2000 - display:inline;
1371 + display: inline;
2001 1372 }
2002 1373 </style>
2003 1374 <?php
2004 1375 }
@@ -2017,10 +1388,10 @@
2017 1388 case 'tel':
2018 1389 ?>
2019 1390 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
2020 1391 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
2021 - type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
2022 - value="" />
1392 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1393 + value=""/>
2023 1394 <?php
2024 1395 break;
2025 1396 case 'textarea':
2026 1397 ?>
@@ -2031,9 +1402,9 @@
2031 1402 case 'checkbox':
2032 1403 ?>
2033 1404 <label>
2034 1405 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
2035 - type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1406 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
2036 1407 <?php echo esc_html( $custom_field['name'] ); ?>
2037 1408 </label>
2038 1409 <?php
2039 1410 break;
@@ -2079,9 +1450,9 @@
2079 1450 return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id );
2080 1451 }
2081 1452
2082 1453 function lp_get_user_custom_fields() {
2083 - $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
1454 + $custom_fields = LP_Settings::get_option( 'register_profile_fields', array() );
2084 1455
2085 1456 $output = array();
2086 1457
2087 1458 if ( $custom_fields ) {
@@ -2099,50 +1470,37 @@
2099 1470 * @param $key
2100 1471 *
2101 1472 * @return bool
2102 1473 * @since 4.0.0
1474 + * @deprecated 4.3.2
2103 1475 */
2104 1476 function learn_press_is_social_profile( $key ) {
1477 + _deprecated_function( __FUNCTION__, '4.3.2' );
1478 +
1479 + return true;
1480 +
2105 1481 $is_socials = learn_press_social_profiles();
2106 1482
2107 1483 return in_array( $key, $is_socials );
2108 1484 }
2109 1485
2110 -function learn_press_social_profile_name( $key ) {
2111 - $name = '';
2112 - switch ( $key ) {
2113 - case 'facebook':
2114 - $name = esc_html__( 'Facebook Profile', 'learnpress' );
2115 - break;
2116 - case 'twitter':
2117 - $name = esc_html__( 'Twitter Profile', 'learnpress' );
2118 - break;
2119 - case 'googleplus':
2120 - $name = esc_html__( 'Google Profile', 'learnpress' );
2121 - break;
2122 - case 'youtube':
2123 - $name = esc_html__( 'Youtube Channel', 'learnpress' );
2124 - break;
2125 - case 'linkedin':
2126 - $name = esc_html__( 'Linkedin Profile', 'learnpress' );
2127 - break;
2128 - default:
2129 - $name = ucfirst( $key );
2130 - }
2131 -
2132 - return apply_filters( 'learn-press/social-profile-name', $name, $key );
2133 -}
2134 -
2135 1486 /**
2136 1487 * Get extra profile fields will be registered in backend profile.
2137 1488 *
2138 1489 * @return array
2139 1490 * @since 4.0.0
1491 + * @deprecated 4.3.2
2140 1492 */
2141 1493 function learn_press_get_user_extra_profile_fields() {
1494 + _deprecated_function( __FUNCTION__, '4.3.2' );
1495 + return array();
1496 +
2142 1497 $socials = learn_press_social_profiles();
2143 - $fields = array();
2144 1498
1499 + return $socials;
1500 +
1501 + $fields = array();
1502 +
2145 1503 foreach ( $socials as $social ) {
2146 1504 $fields[ $social ] = learn_press_social_profile_name( $social );
2147 1505 }
2148 1506
@@ -2163,10 +1521,13 @@
2163 1521
2164 1522 learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) );
2165 1523 learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) );
2166 1524 }
1525 +
2167 1526 add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 );
1527 +add_action( 'show_user_profile', 'learn_press_user_profile_data', 1000 );
2168 1528
1529 +/*
2169 1530 function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) {
2170 1531 if ( empty( $user_id ) ) {
2171 1532 return false;
2172 1533 }
@@ -2207,6 +1568,5 @@
2207 1568 'all' => count( $posts ),
2208 1569 'publish' => count( $public ),
2209 1570 'pending' => count( $pending ),
2210 1571 );
2211 -
2212 -}
1572 +}*/