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