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 +393 -1031 4.1.7.1 → 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,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,55 @@
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();
144 + UserService::instance()->add_capabilities_for_roles();
164 145
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' );
146 + foreach ( $item_types as $item_type ) {
147 + UserService::instance()->add_capabilities_for_roles( $item_type );
204 148 }
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 149 }
242 150
243 -add_action( 'init', 'learn_press_add_user_roles' );
244 -
245 151 /**
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 152 * Get queried user in profile link
354 153 *
355 154 * @return false|WP_User
356 155 * @since 3.0.0
156 + * @deprecated 4.2.9.1
357 157 */
358 158 function learn_press_get_profile_user() {
159 + return LP_Profile::instance()->get_user_current();
160 +
359 161 return LP_Profile::get_queried_user();
360 162 }
361 163
362 164
@@ -389,18 +191,19 @@
389 191 * ....
390 192 * field_name_n => value n
391 193 * )
392 194 * @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).
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).
395 197 *
396 198 * @return mixed
397 199 */
398 -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 ) {
399 201 global $wpdb;
400 202
401 - // Table fields
203 + // Table fields format.
402 204 $table_fields = array(
205 + 'user_item_id' => '%d',
403 206 'user_id' => '%d',
404 207 'item_id' => '%d',
405 208 'ref_id' => '%d',
406 209 'start_time' => '%s',
@@ -422,31 +225,27 @@
422 225 if ( LP_COURSE_CPT === $item_type ) {
423 226 if ( 'completed' === $fields['status'] ) {
424 227 $fields['status'] = 'finished';
425 228 }
426 - } else {
427 - if ( 'finished' === $fields['status'] ) {
229 + } elseif ( 'finished' === $fields['status'] ) {
428 230 $fields['status'] = 'completed';
429 - }
430 231 }
431 232 }
432 233
433 - $data = array();
434 - $data_format = array();
435 - $date_time_fields = array(
436 - 'start_time',
437 - 'end_time',
438 - );
439 -
234 + $data = array();
235 + $data_format = array();
440 236 foreach ( $fields as $field => $value ) {
441 237 if ( ! empty( $table_fields[ $field ] ) ) {
442 238 $data[ $field ] = $value;
443 239
444 240 // Do not format the date-time field if it's value is NULL
445 - if ( in_array( $field, $date_time_fields ) && ! $value ) {
241 + if ( in_array( $field, array( 'start_time', 'end_time' ) ) && empty( $value ) ) {
446 242 $data[ $field ] = null;
447 243 $data_format[] = '';
448 244 } else {
245 + if ( $value instanceof LP_Datetime ) {
246 + $data[ $field ] = $value->toSql();
247 + }
449 248 $data_format[] = $table_fields[ $field ];
450 249 }
451 250 }
452 251 }
@@ -457,18 +256,17 @@
457 256 array( 'user_item_id' => $fields['user_item_id'] )
458 257 );
459 258 }
460 259
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 -
260 + // Set where and where format
465 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 + }
466 266
467 - // Build where and where format
468 - if ( $where ) {
469 267 foreach ( $where as $field => $value ) {
470 - if ( ! empty( $table_fields[ $field ] ) ) {
268 + if ( isset( $table_fields[ $field ] ) ) {
471 269 $where_format[] = $table_fields[ $field ];
472 270 }
473 271 }
474 272 }
@@ -479,17 +277,9 @@
479 277
480 278 $inserted = false;
481 279 $updated = false;
482 280
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
281 + // Update to database.
492 282 if ( $where ) {
493 283 $updated = $wpdb->update(
494 284 $wpdb->learnpress_user_items,
495 285 $data,
@@ -497,16 +287,14 @@
497 287 $data_format,
498 288 $where_format
499 289 );
500 290 } else {
501 -
502 - // Otherwise, insert a new one
291 + // Insert to database.
503 292 if ( $wpdb->insert(
504 293 $wpdb->learnpress_user_items,
505 294 $data,
506 295 $data_format
507 - )
508 - ) {
296 + ) ) {
509 297 $inserted = $wpdb->insert_id;
510 298 }
511 299 }
512 300
@@ -525,12 +313,51 @@
525 313 } elseif ( $updated ) {
526 314 $updated_item = learn_press_get_user_item( $where );
527 315 }
528 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 +
529 353 /**
530 354 * If there is some fields does not contain in the main table
531 - * then consider update them as meta data.
355 + * then consider update them as metadata.
356 + *
357 + * @comment by tungnx - 4.1.7.3
532 358 */
359 + /*
533 360 if ( $updated_item && $update_extra_fields_as_meta ) {
534 361 $extra_fields = array_diff_key( $fields, $table_fields );
535 362 if ( $extra_fields ) {
536 363 foreach ( $extra_fields as $meta_key => $meta_value ) {
@@ -548,11 +375,11 @@
548 375 learn_press_update_user_item_meta( $updated_item->user_item_id, $meta_key, $meta_value );
549 376 }
550 377 }
551 378 }
552 - }
379 + }*/
553 380
554 - do_action( 'learn-press/updated-user-item-meta', $updated_item );
381 + // do_action( 'learn-press/updated-user-item-meta', $updated_item );
555 382
556 383 return $updated_item;
557 384 }
558 385
@@ -559,9 +386,9 @@
559 386 /**
560 387 * Get user item row(s) from user items table by multiple WHERE conditional
561 388 *
562 389 * @param array|int $where
563 - * @param bool $single
390 + * @param bool $single
564 391 *
565 392 * @return array
566 393 */
567 394 function learn_press_get_user_item( $where, $single = true ) {
@@ -600,9 +427,9 @@
600 427 SELECT *
601 428 FROM {$wpdb->prefix}learnpress_user_items
602 429 WHERE " . join( ' AND ', $where_str ) . '
603 430 ORDER BY user_item_id DESC
604 - ',
431 + ',
605 432 $where
606 433 );
607 434 if ( $single || ! empty( $where['user_item_id'] ) ) {
608 435 $item = $wpdb->get_row( $query );
@@ -616,11 +443,11 @@
616 443
617 444 /**
618 445 * Get user item meta from user_itemmeta table
619 446 *
620 - * @param int $user_item_id .
447 + * @param int $user_item_id .
621 448 * @param string $meta_key .
622 - * @param bool $single .
449 + * @param bool $single .
623 450 *
624 451 * @return mixed
625 452 */
626 453 function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) {
@@ -634,11 +461,11 @@
634 461
635 462 /**
636 463 * Add user item meta into table user_itemmeta
637 464 *
638 - * @param int $user_item_id
465 + * @param int $user_item_id
639 466 * @param string $meta_key
640 - * @param mixed $meta_value
467 + * @param mixed $meta_value
641 468 * @param string $prev_value
642 469 *
643 470 * @return false|int
644 471 */
@@ -648,11 +475,11 @@
648 475
649 476 /**
650 477 * Update user item meta to table user_itemmeta
651 478 *
652 - * @param int $user_item_id
479 + * @param int $user_item_id
653 480 * @param string $meta_key
654 - * @param mixed $meta_value
481 + * @param mixed $meta_value
655 482 * @param string $prev_value
656 483 *
657 484 * @return bool|int
658 485 */
@@ -663,12 +490,12 @@
663 490
664 491 /**
665 492 * Update user item meta to table user_itemmeta
666 493 *
667 - * @param int $object_id
494 + * @param int $object_id
668 495 * @param string $meta_key
669 - * @param mixed $meta_value
670 - * @param bool $delete_all
496 + * @param mixed $meta_value
497 + * @param bool $delete_all
671 498 *
672 499 * @return bool|int
673 500 */
674 501 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
@@ -674,98 +501,8 @@
674 501 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
675 502 return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all );
676 503 }
677 504
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 505 if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) {
769 506 /**
770 507 * Filter the avatar
771 508 *
@@ -770,109 +507,105 @@
770 507 * Filter the avatar
771 508 *
772 509 * @param string $avatar
773 510 * @param string $id_or_email
774 - * @param array $size
511 + * @param array $args
775 512 *
776 513 * @return string
514 + * @since 1.0.0
515 + * @version 1.0.2
777 516 */
778 - 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 + }
779 522
780 - $profile = LP_Profile::instance();
523 + $user_id = 0;
781 524
782 - if ( ! $profile->is_enable_avatar() ) {
783 - return $avatar;
784 - }
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 + }
785 543
786 - if ( ( isset( $size['gravatar'] ) && $size['gravatar'] ) || ( $size['default'] && $size['force_default'] ) ) {
787 - return $avatar;
788 - }
544 + if ( ! $user_id ) {
545 + return $avatar;
546 + }
789 547
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;
548 + $user = UserModel::find( $user_id, true );
549 + if ( ! $user ) {
550 + return $avatar;
799 551 }
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 552
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'];
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'] );
828 557 } else {
829 - $height = 250;
830 - $width = 250;
558 + $class[] = $args['class'];
831 559 }
832 - } else {
833 - $img_size = $size['size'];
834 - $height = $size['height'];
835 - $width = $size['width'];
836 560 }
837 561
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 . '" />';
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;
839 577 }
840 -
841 - return $avatar;
842 578 }
843 579 }
844 -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 );
845 581
846 582
847 583 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;
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;
856 590
857 - if ( ! empty( $upload_dir['subdir'] ) ) {
858 - $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
859 - $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'] );
860 594
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 - }
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 + }
867 601
868 - $upload_dir['subdir'] = $subdir;
602 + $upload_dir['subdir'] = $subdir;
869 603
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 - }
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 ] );
875 608 }
876 609 }
877 610
878 611 return $upload_dir;
@@ -877,8 +610,9 @@
877 610
878 611 return $upload_dir;
879 612 }
880 613
614 +/*
881 615 add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 );
882 616 function _learn_press_before_purchase_course_handler( $course_id, $cart ) {
883 617 // Redirect to login page if user is not logged in
884 618 if ( ! is_user_logged_in() ) {
@@ -888,9 +622,9 @@
888 622 'learn_press_purchase_course_login_redirect',
889 623 learn_press_get_login_url( $return_url )
890 624 );
891 625 if ( $redirect !== false ) {
892 - 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' ) );
893 627
894 628 if ( learn_press_is_ajax() ) {
895 629 learn_press_send_json(
896 630 array(
@@ -906,9 +640,9 @@
906 640 } else {
907 641 $user = learn_press_get_current_user();
908 642 $redirect = false;
909 643 if ( $user->has_finished_course( $course_id ) ) {
910 - learn_press_add_message( __( 'You have already finished course', 'learnpress' ) );
644 + learn_press_add_message( __( 'You have already finished the course', 'learnpress' ) );
911 645 $redirect = true;
912 646 } elseif ( $user->has_purchased_course( $course_id ) ) {
913 647 learn_press_add_message( __( 'You have already enrolled in this course', 'learnpress' ) );
914 648 $redirect = true;
@@ -917,10 +651,11 @@
917 651 wp_redirect( get_the_permalink( $course_id ) );
918 652 exit();
919 653 }
920 654 }
921 -}
655 +}*/
922 656
657 +/*
923 658 function learn_press_user_is( $role, $user_id = 0 ) {
924 659 if ( ! $user_id ) {
925 660 $user = learn_press_get_current_user();
926 661 } else {
@@ -933,10 +668,11 @@
933 668 return $user->is_instructor();
934 669 }
935 670
936 671 return $role;
937 -}
672 +}*/
938 673
674 +/*
939 675 function learn_press_profile_tab_edit_content( $current, $tab, $user ) {
940 676 learn_press_get_template(
941 677 'profile/tabs/edit.php',
942 678 array(
@@ -944,25 +680,11 @@
944 680 'current' => $current,
945 681 'tab' => $tab,
946 682 )
947 683 );
948 -}
684 +}*/
949 685
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 -
686 +/*
965 687 function learn_press_update_user_option( $name, $value, $id = 0 ) {
966 688 if ( ! $id ) {
967 689 $id = get_current_user_id();
968 690 }
@@ -969,16 +691,11 @@
969 691 $key = 'learnpress_user_options';
970 692 $options = get_user_option( $key, $id );
971 693 $options[ $name ] = $value;
972 694 update_user_option( $id, $key, $options, true );
973 -}
695 +}*/
974 696
975 -/**
976 - * @param $name
977 - * @param int $id
978 - *
979 - * @return bool
980 - */
697 +/*
981 698 function learn_press_delete_user_option( $name, $id = 0 ) {
982 699 if ( ! $id ) {
983 700 $id = get_current_user_id();
984 701 }
@@ -991,16 +708,11 @@
991 708 return true;
992 709 }
993 710
994 711 return false;
995 -}
712 +}*/
996 713
997 -/**
998 - * @param $name
999 - * @param int $id
1000 - *
1001 - * @return bool
1002 - */
714 +/*
1003 715 function learn_press_get_user_option( $name, $id = 0 ) {
1004 716 if ( ! $id ) {
1005 717 $id = get_current_user_id();
1006 718 }
@@ -1010,119 +722,11 @@
1010 722 return $options[ $name ];
1011 723 }
1012 724
1013 725 return false;
1014 -}
726 +}*/
1015 727
1016 728 /**
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 729 * Update user basic information.
1126 730 *
1127 731 * @param bool $wp_error - Optional. Return WP_Error object in case updating failed.
1128 732 *
@@ -1131,18 +735,18 @@
1131 735 function learn_press_update_user_profile_basic_information( $wp_error = false ) {
1132 736 $user_id = get_current_user_id();
1133 737
1134 738 if ( ! $user_id ) {
1135 - return new WP_Error( 2, 'User is invalid!' );
739 + return new WP_Error( 2, 'The user is invalid' );
1136 740 }
1137 741
1138 742 $update_data = array(
1139 743 '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 ),
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' ),
1145 749 );
1146 750
1147 751 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
1148 752 $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' );
@@ -1149,9 +753,9 @@
1149 753
1150 754 $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta );
1151 755
1152 756 // Update for social.
1153 - $socials = LP_Request::get_array( 'user_profile_social' );
757 + $socials = LP_Request::get_param( 'user_profile_social' );
1154 758 $extra_data = get_user_meta( $user_id, '_lp_extra_info', true );
1155 759
1156 760 if ( ! empty( $extra_data ) ) {
1157 761 $socials = array_merge( $extra_data, $socials );
@@ -1167,55 +771,51 @@
1167 771 }
1168 772
1169 773 /**
1170 774 * Update new password.
775 + *
776 + * @version 1.0.1
1171 777 */
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 ) {
778 +function learn_press_update_user_profile_change_password() {
779 + try {
1183 780 $user = wp_get_current_user();
1184 - require_once ABSPATH . 'wp-includes/class-phpass.php';
1185 - $wp_hasher = new PasswordHash( 8, true );
781 + if ( ! $user ) {
782 + throw new Exception( __( 'The user is invalid', 'learnpress' ) );
783 + }
1186 784
1187 - if ( $wp_hasher->CheckPassword( $old_pass, $user->data->user_pass ) ) {
1188 - $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' ) );
1189 788 }
1190 - }
1191 789
1192 - try {
790 + $check_old_pass = wp_check_password( $old_pass, $user->data->user_pass, $user->ID );
1193 791 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' );
792 + throw new Exception( __( 'The old password is incorrect!', 'learnpress' ) );
793 + }
1198 794
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 );
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 + }
1207 801
1208 - if ( is_wp_error( $return ) ) {
1209 - return $wp_error ? $return : false;
1210 - }
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 + );
1211 808
1212 - return $return;
1213 - }
809 + $rs = wp_update_user( $update_data );
810 + if ( is_wp_error( $rs ) ) {
811 + throw new Exception( $rs->get_error_message() );
1214 812 }
1215 813 } catch ( Exception $ex ) {
1216 - return $wp_error ? new WP_Error( 'UPDATE_PROFILE_ERROR', $ex->getMessage() ) : false;
814 + return new WP_Error( 'error_lp_profile_update_pass', $ex->getMessage() );
1217 815 }
816 +
817 + return true;
1218 818 }
1219 819
1220 820 function learn_press_get_avatar_thumb_size() {
1221 821 $option = LP_Settings::get_option(
@@ -1232,8 +832,11 @@
1232 832 'height' => 250,
1233 833 );
1234 834 }
1235 835
836 + // For option get_avatar_url
837 + $option['size'] = $option['width'];
838 +
1236 839 return $option;
1237 840 }
1238 841
1239 842 function learn_press_get_course_thumbnail_dimensions() {
@@ -1254,34 +857,14 @@
1254 857
1255 858 return $option;
1256 859 }
1257 860
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 -
861 +/*
1279 862 function learn_press_get_user_avatar( $user_id = 0, $size = '' ) {
1280 863 $user = learn_press_get_user( $user_id );
1281 864
1282 865 return $user->get_profile_picture( '', $size );
1283 -}
866 +}*/
1284 867
1285 868 /**
1286 869 * Get profile instance for an user to view.
1287 870 *
@@ -1292,16 +875,9 @@
1292 875 function learn_press_get_profile( $for_user = 0 ) {
1293 876 return LP_Profile::instance( $for_user );
1294 877 }
1295 878
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 - */
879 +/*
1304 880 function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) {
1305 881 global $wpdb;
1306 882
1307 883 settype( $item_id, 'array' );
@@ -1324,44 +900,30 @@
1324 900 }
1325 901
1326 902 $query = $wpdb->prepare(
1327 903 "
1328 - DELETE
1329 - FROM {$wpdb->learnpress_user_items}
1330 - WHERE user_id = %d
1331 - AND ( item_id IN(" . join( ',', $format ) . ")
1332 - $where )
1333 - ",
904 + DELETE
905 + FROM {$wpdb->learnpress_user_items}
906 + WHERE user_id = %d
907 + AND ( item_id IN(" . join( ',', $format ) . ")
908 + $where )
909 + ",
1334 910 $args
1335 911 );
1336 -}
912 +}*/
1337 913
1338 914 /**
1339 915 * Get user profile link
1340 916 *
1341 - * @param int $user_id
917 + * @param int $user_id
1342 918 * @param null $tab
1343 919 *
1344 920 * @return mixed|string
1345 921 */
1346 -function learn_press_user_profile_link( $user_id = 0, $tab = null ) {
922 +function learn_press_user_profile_link( $user_id = 0, $tab = '' ) {
1347 923 if ( ! $user_id ) {
1348 924 $user_id = get_current_user_id();
1349 925 }
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 926
1365 927 $user = learn_press_get_user( $user_id );
1366 928
1367 929 if ( ! $user ) {
@@ -1368,32 +930,33 @@
1368 930 return '';
1369 931 }
1370 932
1371 933 global $wp_query;
1372 - $args = array(
1373 - '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(),
1374 942 );
1375 943
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 - }
944 + if ( $tab ) {
945 + $args['tab'] = $tab;
946 + }
1383 947
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 - }
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'] );
1391 954 }
1392 - $args = array_map( '_learn_press_urlencode', $args );
955 +
1393 956 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
1394 957 if ( $profile_link ) {
1395 - if ( get_option( 'permalink_structure' ) /*&& learn_press_get_page_id( 'profile' )*/ ) {
958 + if ( get_option( 'permalink_structure' ) ) {
1396 959 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
1397 960 } else {
1398 961 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
1399 962 }
@@ -1403,66 +966,9 @@
1403 966
1404 967 return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab );
1405 968 }
1406 969
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 - */
970 +/*
1465 971 function learn_press_create_user_item( $args = array(), $wp_error = false ) {
1466 972 global $wpdb;
1467 973
1468 974 $defaults = array(
@@ -1467,15 +973,15 @@
1467 973
1468 974 $defaults = array(
1469 975 'user_id' => get_current_user_id(),
1470 976 'item_id' => '',
1471 - 'start_time' => current_time( 'mysql', true ),
977 + 'start_time' => time(),
1472 978 'end_time' => '',
1473 979 'graduation' => '',
1474 980 'item_type' => '',
1475 981 'status' => '',
1476 982 'ref_id' => 0,
1477 - 'ref_type' => 0,
983 + 'ref_type' => '',
1478 984 'parent_id' => 0,
1479 985 'create_meta' => array(),
1480 986 );
1481 987
@@ -1489,9 +995,10 @@
1489 995
1490 996 return 0;
1491 997 }
1492 998
1493 - 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 ) {
1494 1001 $item_data['item_type'] = $post_type;
1495 1002 }
1496 1003
1497 1004 // Get id and type of ref if they are null
@@ -1514,9 +1021,10 @@
1514 1021 }
1515 1022 }
1516 1023
1517 1024 // Filter
1518 - 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 ) {
1519 1027 if ( $wp_error ) {
1520 1028 return new WP_Error( 'invalid_item_data', __( 'Invalid item data.', 'learnpress' ) );
1521 1029 }
1522 1030
@@ -1531,13 +1039,10 @@
1531 1039 unset( $item_data['create_meta'] );
1532 1040 }
1533 1041
1534 1042 $user_item = new LP_User_Item( $item_data );
1535 -
1536 - $result = $user_item->update( true, false );
1537 -
1043 + $result = $user_item->update();
1538 1044 if ( ! $result || is_wp_error( $result ) ) {
1539 -
1540 1045 if ( $wp_error && is_wp_error( $result ) ) {
1541 1046 return $result;
1542 1047 }
1543 1048
@@ -1559,46 +1064,11 @@
1559 1064
1560 1065 do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta );
1561 1066
1562 1067 return $user_item;
1563 -}
1068 +}*/
1564 1069
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 - */
1070 +/*
1601 1071 function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) {
1602 1072 global $wpdb;
1603 1073
1604 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 );
@@ -1608,161 +1078,11 @@
1608 1078 return $col;
1609 1079 } else {
1610 1080 return false;
1611 1081 }
1612 -}
1082 +}*/
1613 1083
1614 1084 /**
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 1085 * Prepares list of questions for rest api.
1766 1086 *
1767 1087 * @param int[] $question_ids
1768 1088 * @param array $args
@@ -1769,9 +1089,9 @@
1769 1089 *
1770 1090 * @return array
1771 1091 * @since 3.3.0
1772 1092 */
1773 -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 {
1774 1094 if ( is_numeric( $args ) ) {
1775 1095
1776 1096 } else {
1777 1097 $args = wp_parse_args(
@@ -1810,12 +1130,12 @@
1810 1130
1811 1131 if ( $instantCheck || $status == 'completed' ) {
1812 1132 $theExplanation = $question->get_explanation();
1813 1133 $checked = in_array( $id, $checkedQuestions );
1814 - $hasExplanation = ! ! $theExplanation;
1134 + $hasExplanation = (bool) $theExplanation;
1815 1135 }
1816 1136
1817 - $mark = $question->get_mark() ? $question->get_mark() : 1;
1137 + $mark = $question->get_mark() ? $question->get_mark() : 1;
1818 1138
1819 1139 $questionData = array(
1820 1140 'object' => $question,
1821 1141 'id' => absint( $id ),
@@ -1893,12 +1213,33 @@
1893 1213 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1894 1214 return;
1895 1215 }
1896 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 +
1897 1236 if ( isset( $_POST['_lp_extra_info'] ) ) {
1898 - 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 );
1899 1239 }
1900 1240 }
1241 +
1901 1242 add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' );
1902 1243 add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' );
1903 1244
1904 1245 /**
@@ -1906,9 +1247,9 @@
1906 1247 *
1907 1248 * @param int $user_id
1908 1249 *
1909 1250 * @return array
1910 - * @since 4.0.0
1251 + * @since 4.0.1
1911 1252 */
1912 1253 function learn_press_get_user_extra_profile_info( $user_id = 0 ) {
1913 1254 if ( ! $user_id ) {
1914 1255 $user_id = get_current_user_id();
@@ -1914,30 +1255,61 @@
1914 1255 $user_id = get_current_user_id();
1915 1256 }
1916 1257
1917 1258 $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id );
1918 - $extra_fields = learn_press_get_user_extra_profile_fields();
1259 + $social_fields = learn_press_social_profiles();
1919 1260
1920 - $extra_profile_info = wp_parse_args(
1921 - $extra_profile_info,
1922 - array_fill_keys( array_keys( $extra_fields ), '' )
1923 - );
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 + }
1924 1269
1925 - 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 );
1926 1271 }
1927 1272
1273 +/**
1274 + * @return array
1275 + * @since 3.x.x
1276 + * @version 4.0.2
1277 + */
1928 1278 function learn_press_social_profiles() {
1929 1279 return apply_filters(
1930 1280 'learn-press/social-profiles',
1931 1281 array(
1932 - 'facebook',
1933 - 'twitter',
1934 - 'youtube',
1935 - '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' ),
1936 1286 )
1937 1287 );
1938 1288 }
1939 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 +
1940 1312 function lp_add_default_fields( $fields ) {
1941 1313 $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' );
1942 1314
1943 1315 if ( $first_name === 'yes' ) {
@@ -1944,10 +1316,10 @@
1944 1316 ?>
1945 1317 <li class="form-field">
1946 1318 <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label>
1947 1319 <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'] ?? '' ) ); ?>">
1320 + placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1321 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1950 1322 </li>
1951 1323 <?php
1952 1324 }
1953 1325
@@ -1957,10 +1329,10 @@
1957 1329 ?>
1958 1330 <li class="form-field">
1959 1331 <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label>
1960 1332 <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'] ?? '' ) ); ?>">
1333 + placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1334 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1963 1335 </li>
1964 1336 <?php
1965 1337 }
1966 1338
@@ -1970,10 +1342,10 @@
1970 1342 ?>
1971 1343 <li class="form-field">
1972 1344 <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label>
1973 1345 <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'] ?? '' ) ); ?>">
1346 + placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1347 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1976 1348 </li>
1977 1349 <?php
1978 1350 }
1979 1351 }
@@ -1981,9 +1353,9 @@
1981 1353 add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' );
1982 1354
1983 1355 function lp_custom_register_fields_display() {
1984 1356 ?>
1985 - <?php $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); ?>
1357 + <?php $custom_fields = LP_Profile::get_register_fields_custom(); ?>
1986 1358
1987 1359 <?php if ( $custom_fields ) : ?>
1988 1360 <?php foreach ( $custom_fields as $custom_field ) : ?>
1989 1361 <?php
@@ -1994,11 +1366,12 @@
1994 1366 <style>
1995 1367 .required label {
1996 1368 font-weight: bold;
1997 1369 }
1370 +
1998 1371 .required label:after {
1999 1372 content: ' *';
2000 - display:inline;
1373 + display: inline;
2001 1374 }
2002 1375 </style>
2003 1376 <?php
2004 1377 }
@@ -2017,10 +1390,10 @@
2017 1390 case 'tel':
2018 1391 ?>
2019 1392 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
2020 1393 <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="" />
1394 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1395 + value=""/>
2023 1396 <?php
2024 1397 break;
2025 1398 case 'textarea':
2026 1399 ?>
@@ -2031,9 +1404,9 @@
2031 1404 case 'checkbox':
2032 1405 ?>
2033 1406 <label>
2034 1407 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
2035 - type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1408 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
2036 1409 <?php echo esc_html( $custom_field['name'] ); ?>
2037 1410 </label>
2038 1411 <?php
2039 1412 break;
@@ -2079,9 +1452,9 @@
2079 1452 return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id );
2080 1453 }
2081 1454
2082 1455 function lp_get_user_custom_fields() {
2083 - $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
1456 + $custom_fields = LP_Settings::get_option( 'register_profile_fields', array() );
2084 1457
2085 1458 $output = array();
2086 1459
2087 1460 if ( $custom_fields ) {
@@ -2099,50 +1472,37 @@
2099 1472 * @param $key
2100 1473 *
2101 1474 * @return bool
2102 1475 * @since 4.0.0
1476 + * @deprecated 4.3.2
2103 1477 */
2104 1478 function learn_press_is_social_profile( $key ) {
1479 + _deprecated_function( __FUNCTION__, '4.3.2' );
1480 +
1481 + return true;
1482 +
2105 1483 $is_socials = learn_press_social_profiles();
2106 1484
2107 1485 return in_array( $key, $is_socials );
2108 1486 }
2109 1487
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 1488 /**
2136 1489 * Get extra profile fields will be registered in backend profile.
2137 1490 *
2138 1491 * @return array
2139 1492 * @since 4.0.0
1493 + * @deprecated 4.3.2
2140 1494 */
2141 1495 function learn_press_get_user_extra_profile_fields() {
1496 + _deprecated_function( __FUNCTION__, '4.3.2' );
1497 + return array();
1498 +
2142 1499 $socials = learn_press_social_profiles();
2143 - $fields = array();
2144 1500
1501 + return $socials;
1502 +
1503 + $fields = array();
1504 +
2145 1505 foreach ( $socials as $social ) {
2146 1506 $fields[ $social ] = learn_press_social_profile_name( $social );
2147 1507 }
2148 1508
@@ -2163,10 +1523,13 @@
2163 1523
2164 1524 learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) );
2165 1525 learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) );
2166 1526 }
1527 +
2167 1528 add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 );
1529 +add_action( 'show_user_profile', 'learn_press_user_profile_data', 1000 );
2168 1530
1531 +/*
2169 1532 function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) {
2170 1533 if ( empty( $user_id ) ) {
2171 1534 return false;
2172 1535 }
@@ -2207,6 +1570,5 @@
2207 1570 'all' => count( $posts ),
2208 1571 'publish' => count( $public ),
2209 1572 'pending' => count( $pending ),
2210 1573 );
2211 -
2212 -}
1574 +}*/