PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.1
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / user / lp-user-functions.php

lp-user-functions.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.1, at inc/user/lp-user-functions.php

2,082 lines 54.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Common functions to process actions about user
4 *
5 * @author ThimPress
6 * @package LearnPress/Functions/User
7 * @version 1.0
8 */
9
10 function learn_press_get_user_profile_tabs() {
11 return LP_Profile::instance()->get_tabs();
12 }
13
14 /**
15 * Delete user data by user ID
16 *
17 * @param int $user_id
18 * @param int $course_id
19 */
20 function learn_press_delete_user_data( $user_id, $course_id = 0 ) {
21 global $wpdb;
22 // TODO: Should be deleted user's order and order data???
23
24 $query_args = array( $user_id );
25
26 if ( $course_id ) {
27 $query_args[] = $course_id;
28 }
29
30 $query = $wpdb->prepare(
31 "
32 SELECT user_item_id
33 FROM {$wpdb->prefix}learnpress_user_items
34 WHERE user_id = %d
35 " . ( $course_id ? ' AND item_id = %d' : '' ) . '
36 ',
37 $query_args
38 );
39
40 // delete all courses user has enrolled
41 $query = $wpdb->prepare(
42 "
43 DELETE FROM {$wpdb->prefix}learnpress_user_items
44 WHERE user_id = %d
45 " . ( $course_id ? ' AND item_id = %d' : '' ) . '
46 ',
47 $query_args
48 );
49
50 @$wpdb->query( $query );
51 }
52
53 /**
54 * Get user_item_id field in table learnpress_user_items
55 * with the user_id, item_id. If $course_id is not passed
56 * then item_id is ID of a course. Otherwise, item_id is
57 * ID of an item (like quiz/lesson).
58 *
59 * @param int $user_id
60 * @param int $item_id
61 * @param int $course_id
62 *
63 * @return bool
64 * @editor tungnx
65 * @reason this function only get cache, not handle get user_item_id
66 * @deprecated 4.1.6.9
67 */
68 function learn_press_get_user_item_id( $user_id, $item_id, $course_id = 0 /* added 3.0.0 */ ) {
69 return false;
70 }
71
72 /**
73 * Get current user ID
74 *
75 * @return int
76 */
77 function learn_press_get_current_user_id(): int {
78 $user = learn_press_get_current_user();
79
80 if ( ! $user ) {
81 return 0;
82 }
83
84 return $user->get_id();
85 }
86
87 /**
88 * Get the user by $user_id passed. If $user_id is NULL, get current user.
89 * If current user is not logged in, return a GUEST user
90 *
91 * @param bool $create_temp - Optional. Create temp user if user is not logged in.
92 *
93 * @return LP_User|LP_User_Guest
94 * @editor tungnx
95 * @modify 4.1.4
96 * @version 1.0.1
97 */
98 function learn_press_get_current_user( $create_temp = true ) {
99 $user_id = get_current_user_id();
100
101 if ( $user_id ) {
102 return learn_press_get_user( $user_id );
103 }
104
105 // Return LP_User_Guest
106 return learn_press_get_user( 0 );
107 }
108
109 if ( ! function_exists( 'learn_press_get_user' ) ) {
110 /**
111 * Get user by ID. Return false if the user does not exist.
112 * If user_id = 0, return a guest user.
113 *
114 * @param int $user_id
115 * @param bool $current
116 *
117 * @return LP_User|LP_User_Guest|false
118 * @since 3.0.0
119 * @version 4.0.1
120 */
121 function learn_press_get_user( $user_id = 0, $current = false, $force_new = false ) {
122 $userClass = $user_id && get_user_by( 'ID', $user_id ) ? 'LP_User' : ( $user_id == 0 ? 'LP_User_Guest' : false );
123 if ( false === $userClass ) {
124 return false;
125 }
126
127 return new $userClass( $user_id );
128 }
129 }
130
131 /**
132 * Add more 2 user roles teacher and student
133 */
134 function learn_press_add_user_roles() {
135
136 $settings = LP_Settings::instance();
137
138 /* translators: user role */
139 _x( 'LP Instructor', 'User role' );
140
141 add_role(
142 LP_TEACHER_ROLE,
143 'LP Instructor',
144 array()
145 );
146
147 $course_cap = LP_COURSE_CPT . 's';
148 $lesson_cap = LP_LESSON_CPT . 's';
149 $order_cap = LP_ORDER_CPT . 's';
150
151 $teacher = get_role( LP_TEACHER_ROLE );
152 if ( $teacher ) {
153 $teacher->add_cap( 'read_private_' . $course_cap );
154 $teacher->add_cap( 'delete_published_' . $course_cap );
155 $teacher->add_cap( 'edit_published_' . $course_cap );
156 $teacher->add_cap( 'edit_' . $course_cap );
157 $teacher->add_cap( 'delete_' . $course_cap );
158 $teacher->add_cap( 'unfiltered_html' );
159
160 $settings->get( 'required_review' );
161
162 if ( $settings->get( 'required_review' ) == 'yes' ) {
163 $teacher->remove_cap( 'publish_' . $course_cap );
164 } else {
165 $teacher->add_cap( 'publish_' . $course_cap );
166 }
167
168 $teacher->add_cap( 'read_private_' . $lesson_cap );
169 $teacher->add_cap( 'delete_published_' . $lesson_cap );
170 $teacher->add_cap( 'edit_published_' . $lesson_cap );
171 $teacher->add_cap( 'edit_' . $lesson_cap );
172 $teacher->add_cap( 'delete_' . $lesson_cap );
173 $teacher->add_cap( 'publish_' . $lesson_cap );
174 $teacher->add_cap( 'upload_files' );
175 $teacher->add_cap( 'read' );
176 $teacher->add_cap( 'edit_posts' );
177 }
178
179 // administrator
180 $admin = get_role( 'administrator' );
181 if ( $admin ) {
182 $admin->add_cap( 'read_private_' . $course_cap );
183 $admin->add_cap( 'delete_' . $course_cap );
184 $admin->add_cap( 'delete_published_' . $course_cap );
185 $admin->add_cap( 'edit_' . $course_cap );
186 $admin->add_cap( 'edit_published_' . $course_cap );
187 $admin->add_cap( 'publish_' . $course_cap );
188 $admin->add_cap( 'delete_private_' . $course_cap );
189 $admin->add_cap( 'edit_private_' . $course_cap );
190 $admin->add_cap( 'delete_others_' . $course_cap );
191 $admin->add_cap( 'edit_others_' . $course_cap );
192
193 $admin->add_cap( 'read_private_' . $lesson_cap );
194 $admin->add_cap( 'delete_' . $lesson_cap );
195 $admin->add_cap( 'delete_published_' . $lesson_cap );
196 $admin->add_cap( 'edit_' . $lesson_cap );
197 $admin->add_cap( 'edit_published_' . $lesson_cap );
198 $admin->add_cap( 'publish_' . $lesson_cap );
199 $admin->add_cap( 'delete_private_' . $lesson_cap );
200 $admin->add_cap( 'edit_private_' . $lesson_cap );
201 $admin->add_cap( 'delete_others_' . $lesson_cap );
202 $admin->add_cap( 'edit_others_' . $lesson_cap );
203
204 $admin->add_cap( 'delete_' . $order_cap );
205 $admin->add_cap( 'delete_published_' . $order_cap );
206 $admin->add_cap( 'edit_' . $order_cap );
207 $admin->add_cap( 'edit_published_' . $order_cap );
208 $admin->add_cap( 'publish_' . $order_cap );
209 $admin->add_cap( 'delete_private_' . $order_cap );
210 $admin->add_cap( 'edit_private_' . $order_cap );
211 $admin->add_cap( 'delete_others_' . $order_cap );
212 $admin->add_cap( 'edit_others_' . $order_cap );
213 }
214 }
215
216 add_action( 'init', 'learn_press_add_user_roles' );
217
218 /**
219 * @param null $user_id
220 * @param array $args
221 *
222 * @return mixed
223 * @deprecated 4.1.7.3
224 */
225 /*function learn_press_get_user_questions( $user_id = null, $args = array() ) {
226 if ( ! $user_id ) {
227 $user_id = get_current_user_id();
228 }
229
230 return learn_press_get_user( $user_id )->get_questions( $args );
231 }*/
232
233 /**
234 * Get the type of current user
235 *
236 * @param null $check_type
237 *
238 * @return bool|string
239 */
240 function learn_press_current_user_is( $check_type = null ) {
241 global $current_user;
242 $user_roles = $current_user->roles;
243 $user_type = '';
244
245 if ( in_array( 'lpr_teacher', $user_roles ) ) {
246 $user_type = 'instructor';
247 } elseif ( in_array( 'lp_teacher', $user_roles ) ) {
248 $user_type = 'instructor';
249 } elseif ( in_array( 'administrator', $user_roles ) ) {
250 $user_type = 'administrator';
251 }
252
253 return $check_type ? $check_type == $user_type : $user_type;
254 }
255
256 function learn_press_user_has_roles( $roles, $user_id = null ) {
257 $has_role = false;
258 if ( ! $user_id ) {
259 $user = wp_get_current_user();
260 } else {
261 $user = get_user_by( 'id', $user_id );
262 }
263 $available_roles = (array) $user->roles;
264 if ( is_array( $roles ) ) {
265 foreach ( $roles as $role ) {
266 if ( in_array( $role, $available_roles ) ) {
267 $has_role = true;
268 break; // only need one of roles is in available
269 }
270 }
271 } else {
272 if ( in_array( $roles, $available_roles ) ) {
273 $has_role = true;
274 }
275 }
276
277 return $has_role;
278 }
279
280 function learn_press_current_user_can_view_profile_section( $section, $user ) {
281 $current_user = wp_get_current_user();
282 $view = true;
283 if ( $user->get_data( 'user_login' ) != $current_user->user_login && $section == LP_Settings::instance()->get(
284 'profile_endpoints.profile-orders',
285 'profile-orders'
286 ) ) {
287 $view = false;
288 }
289
290 return apply_filters( 'learn_press_current_user_can_view_profile_section', $view, $section, $user );
291 }
292
293 /*function learn_press_profile_tab_courses_content( $current, $tab, $user ) {
294 learn_press_get_template(
295 'profile/tabs/courses.php',
296 array(
297 'user' => $user,
298 'current' => $current,
299 'tab' => $tab,
300 )
301 );
302 }*/
303
304 function learn_press_profile_tab_quizzes_content( $current, $tab, $user ) {
305 learn_press_get_template(
306 'profile/tabs/quizzes.php',
307 array(
308 'user' => $user,
309 'current' => $current,
310 'tab' => $tab,
311 )
312 );
313 }
314
315 function learn_press_profile_tab_orders_content( $current, $tab, $user ) {
316 learn_press_get_template(
317 'profile/tabs/orders.php',
318 array(
319 'user' => $user,
320 'current' => $current,
321 'tab' => $tab,
322 )
323 );
324 }
325
326 /**
327 * Get queried user in profile link
328 *
329 * @return false|WP_User
330 * @since 3.0.0
331 */
332 function learn_press_get_profile_user() {
333 return LP_Profile::get_queried_user();
334 }
335
336
337 /**
338 * Add instructor registration button to register page and admin bar
339 */
340 function learn_press_user_become_teacher_registration_form() {
341 if ( LP_Settings::instance()->get( 'instructor_registration' ) != 'yes' ) {
342 return;
343 }
344 ?>
345 <p>
346 <label for="become_teacher">
347 <input type="checkbox" name="become_teacher" id="become_teacher">
348 <?php esc_html_e( 'Want to become an instructor?', 'learnpress' ); ?>
349 </label>
350 </p>
351 <?php
352 }
353
354 add_action( 'register_form', 'learn_press_user_become_teacher_registration_form' );
355
356 /**
357 * Update data into table learnpress_user_items.
358 *
359 * @param array $fields - Fields and values to be updated.
360 * Format: array(
361 * field_name_1 => value 1,
362 * field_name_2 => value 2,
363 * ....
364 * field_name_n => value n
365 * )
366 * @param mixed $where - Optional. Fields with values for conditional update with the same format of $fields.
367 * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0).
368 * @param bool $update_extra_fields_as_meta - Optional. Update extra fields as item meta (since 3.1.0).
369 *
370 * @return mixed
371 */
372 function learn_press_update_user_item_field( array $fields = [], $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) {
373 global $wpdb;
374
375 // Table fields format.
376 $table_fields = array(
377 'user_item_id' => '%d',
378 'user_id' => '%d',
379 'item_id' => '%d',
380 'ref_id' => '%d',
381 'start_time' => '%s',
382 'end_time' => '%s',
383 'access_level' => '%d',
384 'graduation' => '%s',
385 'item_type' => '%s',
386 'status' => '%s',
387 'ref_type' => '%s',
388 'parent_id' => '%d',
389 );
390
391 /**
392 * Validate item status
393 */
394 if ( ! empty( $fields['item_id'] ) && ! empty( $fields['status'] ) ) {
395 $item_type = learn_press_get_post_type( $fields['item_id'] );
396
397 if ( LP_COURSE_CPT === $item_type ) {
398 if ( 'completed' === $fields['status'] ) {
399 $fields['status'] = 'finished';
400 }
401 } else {
402 if ( 'finished' === $fields['status'] ) {
403 $fields['status'] = 'completed';
404 }
405 }
406 }
407
408 $data = array();
409 $data_format = array();
410 foreach ( $fields as $field => $value ) {
411 if ( ! empty( $table_fields[ $field ] ) ) {
412 $data[ $field ] = $value;
413
414 // Do not format the date-time field if it's value is NULL
415 if ( in_array( $field, [ 'start_time', 'end_time' ] ) && empty( $value ) ) {
416 $data[ $field ] = null;
417 $data_format[] = '';
418 } else {
419 if ( $value instanceof LP_Datetime ) {
420 $data[ $field ] = $value->toSql();
421 }
422 $data_format[] = $table_fields[ $field ];
423 }
424 }
425 }
426
427 if ( ! empty( $fields['user_item_id'] ) ) {
428 $where = wp_parse_args(
429 $where,
430 array( 'user_item_id' => $fields['user_item_id'] )
431 );
432 }
433
434 // Set where and where format
435 $where_format = array();
436 if ( is_array( $where ) && ! empty( $where ) ) {
437 if ( empty( $where['user_id'] ) ) {
438 $where['user_id'] = ! empty( $fields['user_id'] ) ? $fields['user_id'] : learn_press_get_current_user_id();
439 }
440
441 foreach ( $where as $field => $value ) {
442 if ( isset( $table_fields[ $field ] ) ) {
443 $where_format[] = $table_fields[ $field ];
444 }
445 }
446 }
447
448 if ( ! $data ) {
449 return false;
450 }
451
452 $inserted = false;
453 $updated = false;
454
455 // Update to database.
456 if ( $where ) {
457 $updated = $wpdb->update(
458 $wpdb->learnpress_user_items,
459 $data,
460 $where,
461 $data_format,
462 $where_format
463 );
464 } else {
465 // Insert to database.
466 if ( $wpdb->insert(
467 $wpdb->learnpress_user_items,
468 $data,
469 $data_format
470 ) ) {
471 $inserted = $wpdb->insert_id;
472 }
473 }
474
475 if ( $updated && ! empty( $where['user_item_id'] ) ) {
476 $inserted = $where['user_item_id'];
477 }
478
479 /**
480 * @var object|bool $updated_item
481 */
482 $updated_item = false;
483
484 // Get the item we just have updated or inserted.
485 if ( $inserted ) {
486 $updated_item = learn_press_get_user_item( $inserted );
487 } elseif ( $updated ) {
488 $updated_item = learn_press_get_user_item( $where );
489 }
490
491 /**
492 * If there is some fields does not contain in the main table
493 * then consider update them as metadata.
494 * @comment by tungnx - 4.1.7.3
495 */
496 /*if ( $updated_item && $update_extra_fields_as_meta ) {
497 $extra_fields = array_diff_key( $fields, $table_fields );
498 if ( $extra_fields ) {
499 foreach ( $extra_fields as $meta_key => $meta_value ) {
500 if ( $meta_value == 'user_item_id' ) {
501 continue;
502 }
503
504 if ( $meta_value === false ) {
505 learn_press_delete_user_item_meta( $updated_item->user_item_id, $meta_key );
506 } else {
507
508 if ( empty( $meta_value ) ) {
509 $meta_value = '';
510 }
511 learn_press_update_user_item_meta( $updated_item->user_item_id, $meta_key, $meta_value );
512 }
513 }
514 }
515 }*/
516
517 do_action( 'learn-press/updated-user-item-meta', $updated_item );
518
519 return $updated_item;
520 }
521
522 /**
523 * Get user item row(s) from user items table by multiple WHERE conditional
524 *
525 * @param array|int $where
526 * @param bool $single
527 *
528 * @return array
529 */
530 function learn_press_get_user_item( $where, $single = true ) {
531 global $wpdb;
532
533 // Table fields
534 $table_fields = array(
535 'user_item_id' => '%d',
536 'user_id' => '%d',
537 'item_id' => '%d',
538 'ref_id' => '%d',
539 'start_time' => '%s',
540 'end_time' => '%s',
541 'item_type' => '%s',
542 'status' => '%s',
543 'ref_type' => '%s',
544 'parent_id' => '%d',
545 );
546
547 // If $where is a number consider we are searching the record with unique user_item_id
548 if ( is_numeric( $where ) ) {
549 $where = array( 'user_item_id' => $where );
550 }
551
552 $where_str = array();
553 foreach ( $where as $field => $value ) {
554 if ( ! empty( $table_fields[ $field ] ) ) {
555 $where_str[] = "{$field} = " . $table_fields[ $field ];
556 }
557 }
558 $item = false;
559
560 if ( $where_str ) {
561 $query = $wpdb->prepare(
562 "
563 SELECT *
564 FROM {$wpdb->prefix}learnpress_user_items
565 WHERE " . join( ' AND ', $where_str ) . '
566 ORDER BY user_item_id DESC
567 ',
568 $where
569 );
570 if ( $single || ! empty( $where['user_item_id'] ) ) {
571 $item = $wpdb->get_row( $query );
572 } else {
573 $item = $wpdb->get_results( $query );
574 }
575 }
576
577 return $item;
578 }
579
580 /**
581 * Get user item meta from user_itemmeta table
582 *
583 * @param int $user_item_id .
584 * @param string $meta_key .
585 * @param bool $single .
586 *
587 * @return mixed
588 */
589 function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) {
590 $meta = false;
591 if ( metadata_exists( 'learnpress_user_item', $user_item_id, $meta_key ) ) {
592 $meta = get_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $single );
593 }
594
595 return $meta;
596 }
597
598 /**
599 * Add user item meta into table user_itemmeta
600 *
601 * @param int $user_item_id
602 * @param string $meta_key
603 * @param mixed $meta_value
604 * @param string $prev_value
605 *
606 * @return false|int
607 */
608 function learn_press_add_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) {
609 return add_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value );
610 }
611
612 /**
613 * Update user item meta to table user_itemmeta
614 *
615 * @param int $user_item_id
616 * @param string $meta_key
617 * @param mixed $meta_value
618 * @param string $prev_value
619 *
620 * @return bool|int
621 */
622 function learn_press_update_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) {
623 return update_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value );
624 }
625
626
627 /**
628 * Update user item meta to table user_itemmeta
629 *
630 * @param int $object_id
631 * @param string $meta_key
632 * @param mixed $meta_value
633 * @param bool $delete_all
634 *
635 * @return bool|int
636 */
637 function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
638 return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all );
639 }
640
641 /**
642 * Get temp users.
643 *
644 * @return array
645 * @deprecated 4.1.6.9
646 */
647 /*function learn_press_get_temp_users() {
648 return false;
649 if ( false === ( $temp_users = LP_Object_Cache::get( 'learn-press/temp-users' ) ) ) {
650 global $wpdb;
651 $query = $wpdb->prepare(
652 "
653 SELECT ID
654 FROM {$wpdb->users} u
655 INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id AND um.meta_key = %s AND um.meta_value = %s
656 LEFT JOIN {$wpdb->usermeta} um2 ON u.ID = um2.user_id AND um2.meta_key = %s
657 ",
658 '_lp_temp_user',
659 'yes',
660 '_lp_expiration'
661 );
662
663 $temp_users = $wpdb->get_col( $query );
664
665 LP_Object_Cache::set( 'learn-press/temp-users', $temp_users );
666 }
667
668 return $temp_users;
669 }*/
670
671 /**
672 * Update field created_time after added user item meta
673 *
674 * @use updated_{meta_type}_meta hook
675 *
676 * @param $meta_id
677 * @param $object_id
678 * @param $meta_key
679 * @param $_meta_value
680 * @deprecated 4.1.7.3
681 */
682 /*function _learn_press_update_created_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
683 global $wpdb;
684 $wpdb->update(
685 $wpdb->learnpress_user_itemmeta,
686 array( 'create_time' => current_time( 'mysql' ) ),
687 array( 'meta_id' => $meta_id ),
688 array( '%s' ),
689 array( '%d' )
690 );
691 }*/
692
693 // add_action( 'added_learnpress_user_item_meta', '_learn_press_update_created_time_user_item_meta', 10, 4 );
694
695 /**
696 * Update field updated_time after updated user item meta
697 *
698 * @use updated_{meta_type}_meta hook
699 *
700 * @param $meta_id
701 * @param $object_id
702 * @param $meta_key
703 * @param $_meta_value
704 * @deprecated
705 */
706 /*function _learn_press_update_updated_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
707 global $wpdb;
708 $wpdb->update(
709 $wpdb->learnpress_user_itemmeta,
710 array( 'update_time' => current_time( 'mysql' ) ),
711 array( 'meta_id' => $meta_id ),
712 array( '%s' ),
713 array( '%d' )
714 );
715 }*/
716
717 // add_action( 'updated_learnpress_user_item_meta', '_learn_press_update_updated_time_user_item_meta', 10, 4 );
718
719 /**
720 * @param $status
721 * @param int $quiz_id
722 * @param int $user_id
723 * @param int $course_id
724 *
725 * @return bool|mixed
726 * @deprecated 4.1.7.3
727 */
728 /*function learn_press_user_has_quiz_status( $status, $quiz_id = 0, $user_id = 0, $course_id = 0 ) {
729 $user = learn_press_get_user( $user_id );
730
731 return $user->has_quiz_status( $status, $quiz_id, $course_id );
732 }*/
733
734 if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) {
735 /**
736 * Filter the avatar
737 *
738 * @param string $avatar
739 * @param string $id_or_email
740 * @param array $size
741 *
742 * @return string
743 */
744 function learn_press_pre_get_avatar_callback( $avatar, $id_or_email = '', $size = array() ) {
745
746 $profile = LP_Profile::instance();
747
748 if ( ! $profile->is_enable_avatar() ) {
749 return $avatar;
750 }
751
752 if ( ( isset( $size['gravatar'] ) && $size['gravatar'] ) || ( $size['default'] && $size['force_default'] ) ) {
753 return $avatar;
754 }
755
756 $user_id = 0;
757
758 /**
759 * Get the ID of user from $id_or_email
760 */
761 if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) {
762 $user = get_user_by( 'email', $id_or_email );
763 if ( $user ) {
764 $user_id = $user->ID;
765 }
766 } elseif ( is_numeric( $id_or_email ) ) {
767 $user_id = $id_or_email;
768 } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) {
769 $user_id = $id_or_email->user_id;
770 } elseif ( $id_or_email instanceof WP_Comment ) {
771 $user = get_user_by( 'email', $id_or_email->comment_author_email );
772 if ( $user ) {
773 $user_id = $user->ID;
774 }
775 }
776
777 if ( ! $user_id ) {
778 return $avatar;
779 }
780
781 $user = LP_User_Factory::get_user( $user_id );
782
783 $profile_picture_src = $user->get_upload_profile_src();
784 if ( $profile_picture_src ) {
785 $setting_size = learn_press_get_avatar_thumb_size();
786 $img_size = '';
787
788 // Get avatar size
789 if ( ! is_array( $size ) ) {
790 if ( $size === 'thumbnail' ) {
791 $img_size = '';
792 $height = $setting_size['height'];
793 $width = $setting_size['width'];
794 } else {
795 $height = 250;
796 $width = 250;
797 }
798 } else {
799 $img_size = $size['size'];
800 $height = $size['height'];
801 $width = $size['width'];
802 }
803
804 $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 . '" />';
805 }
806
807 return $avatar;
808 }
809 }
810 add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 1, 5 );
811
812
813 function learn_press_user_profile_picture_upload_dir( $width_user = true ) {
814 static $upload_dir;
815 if ( ! $upload_dir ) {
816 $upload_dir = wp_upload_dir();
817 $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user );
818 if ( $width_user ) {
819 $subdir .= '/' . get_current_user_id();
820 }
821 $subdir = '/' . $subdir;
822
823 if ( ! empty( $upload_dir['subdir'] ) ) {
824 $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
825 $u_path = str_replace( '\\', '/', $upload_dir['path'] );
826
827 $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path );
828 $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] );
829 } else {
830 $upload_dir['path'] = $upload_dir['path'] . $subdir;
831 $upload_dir['url'] = $upload_dir['url'] . $subdir;
832 }
833
834 $upload_dir['subdir'] = $subdir;
835
836 // Point path/url to main site if we are in multisite
837 if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
838 foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) {
839 $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] );
840 }
841 }
842 }
843
844 return $upload_dir;
845 }
846
847 add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 );
848 function _learn_press_before_purchase_course_handler( $course_id, $cart ) {
849 // Redirect to login page if user is not logged in
850 if ( ! is_user_logged_in() ) {
851 $return_url = esc_url_raw( add_query_arg( $_POST, get_the_permalink( $course_id ) ) );
852 $return_url = apply_filters( 'learn_press_purchase_course_login_redirect_return_url', $return_url );
853 $redirect = apply_filters(
854 'learn_press_purchase_course_login_redirect',
855 learn_press_get_login_url( $return_url )
856 );
857 if ( $redirect !== false ) {
858 learn_press_add_message( __( 'Please log in to enroll in this course', 'learnpress' ) );
859
860 if ( learn_press_is_ajax() ) {
861 learn_press_send_json(
862 array(
863 'redirect' => $redirect,
864 'result' => 'success',
865 )
866 );
867 } else {
868 wp_redirect( $redirect );
869 exit();
870 }
871 }
872 } else {
873 $user = learn_press_get_current_user();
874 $redirect = false;
875 if ( $user->has_finished_course( $course_id ) ) {
876 learn_press_add_message( __( 'You have already finished the course', 'learnpress' ) );
877 $redirect = true;
878 } elseif ( $user->has_purchased_course( $course_id ) ) {
879 learn_press_add_message( __( 'You have already enrolled in this course', 'learnpress' ) );
880 $redirect = true;
881 }
882 if ( $redirect ) {
883 wp_redirect( get_the_permalink( $course_id ) );
884 exit();
885 }
886 }
887 }
888
889 function learn_press_user_is( $role, $user_id = 0 ) {
890 if ( ! $user_id ) {
891 $user = learn_press_get_current_user();
892 } else {
893 $user = learn_press_get_user( $user_id );
894 }
895 if ( $role == 'admin' ) {
896 return $user->is_admin();
897 }
898 if ( $role == 'instructor' ) {
899 return $user->is_instructor();
900 }
901
902 return $role;
903 }
904
905 function learn_press_profile_tab_edit_content( $current, $tab, $user ) {
906 learn_press_get_template(
907 'profile/tabs/edit.php',
908 array(
909 'user' => $user,
910 'current' => $current,
911 'tab' => $tab,
912 )
913 );
914 }
915
916 function learn_press_get_profile_endpoints() {
917 $endpoints = (array) LP_Settings::instance()->get( 'profile_endpoints' );
918 $tabs = LP_Profile::instance()->get_tabs();
919 if ( $tabs ) {
920 foreach ( $tabs as $slug => $info ) {
921 if ( empty( $endpoints[ $slug ] ) ) {
922 $endpoints[ $slug ] = $slug;
923 }
924 }
925 }
926
927 return apply_filters( 'learn_press_profile_tab_endpoints', $endpoints );
928 }
929
930
931 function learn_press_update_user_option( $name, $value, $id = 0 ) {
932 if ( ! $id ) {
933 $id = get_current_user_id();
934 }
935 $key = 'learnpress_user_options';
936 $options = get_user_option( $key, $id );
937 $options[ $name ] = $value;
938 update_user_option( $id, $key, $options, true );
939 }
940
941 /**
942 * @param $name
943 * @param int $id
944 *
945 * @return bool
946 */
947 function learn_press_delete_user_option( $name, $id = 0 ) {
948 if ( ! $id ) {
949 $id = get_current_user_id();
950 }
951 $key = 'learnpress_user_options';
952 $options = get_user_option( $key, $id );
953 if ( is_array( $options ) && array_key_exists( $name, $options ) ) {
954 unset( $options[ $name ] );
955 update_user_option( $id, $key, $options, true );
956
957 return true;
958 }
959
960 return false;
961 }
962
963 /**
964 * @param $name
965 * @param int $id
966 *
967 * @return bool
968 */
969 function learn_press_get_user_option( $name, $id = 0 ) {
970 if ( ! $id ) {
971 $id = get_current_user_id();
972 }
973 $key = 'learnpress_user_options';
974 $options = get_user_option( $key, $id );
975 if ( is_array( $options ) && array_key_exists( $name, $options ) ) {
976 return $options[ $name ];
977 }
978
979 return false;
980 }
981
982 /**
983 * Update user basic information.
984 *
985 * @param bool $wp_error - Optional. Return WP_Error object in case updating failed.
986 *
987 * @return bool|mixed|WP_Error
988 */
989 function learn_press_update_user_profile_basic_information( $wp_error = false ) {
990 $user_id = get_current_user_id();
991
992 if ( ! $user_id ) {
993 return new WP_Error( 2, 'The user is invalid' );
994 }
995
996 $update_data = array(
997 'ID' => $user_id,
998 'first_name' => filter_input( INPUT_POST, 'first_name', FILTER_SANITIZE_STRING ),
999 'last_name' => filter_input( INPUT_POST, 'last_name', FILTER_SANITIZE_STRING ),
1000 'description' => filter_input( INPUT_POST, 'description', FILTER_SANITIZE_STRING ),
1001 'display_name' => filter_input( INPUT_POST, 'account_display_name', FILTER_SANITIZE_STRING ),
1002 'user_email' => filter_input( INPUT_POST, 'account_email', FILTER_SANITIZE_EMAIL ),
1003 );
1004
1005 $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
1006 $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' );
1007
1008 $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta );
1009
1010 // Update for social.
1011 $socials = LP_Request::get_array( 'user_profile_social' );
1012 $extra_data = get_user_meta( $user_id, '_lp_extra_info', true );
1013
1014 if ( ! empty( $extra_data ) ) {
1015 $socials = array_merge( $extra_data, $socials );
1016 }
1017
1018 update_user_meta( $user_id, '_lp_extra_info', $socials );
1019
1020 if ( is_wp_error( $return ) ) {
1021 return $wp_error ? $return : false;
1022 }
1023
1024 return $return;
1025 }
1026
1027 /**
1028 * Update new password.
1029 */
1030 function learn_press_update_user_profile_change_password( $wp_error = false ) {
1031 $user_id = get_current_user_id();
1032
1033 if ( ! $user_id ) {
1034 return new WP_Error( 2, 'The user is invalid' );
1035 }
1036
1037 $old_pass = filter_input( INPUT_POST, 'pass0' );
1038 $check_old_pass = false;
1039
1040 if ( $old_pass ) {
1041 $user = wp_get_current_user();
1042 require_once ABSPATH . 'wp-includes/class-phpass.php';
1043 $wp_hasher = new PasswordHash( 8, true );
1044
1045 if ( $wp_hasher->CheckPassword( $old_pass, $user->data->user_pass ) ) {
1046 $check_old_pass = true;
1047 }
1048 }
1049
1050 try {
1051 if ( ! $check_old_pass ) {
1052 throw new Exception( __( 'The old password is incorrect!', 'learnpress' ) );
1053 } else {
1054 $new_pass = filter_input( INPUT_POST, 'pass1' );
1055 $new_pass2 = filter_input( INPUT_POST, 'pass2' );
1056
1057 if ( ! $new_pass || ! $new_pass2 || ( $new_pass != $new_pass2 ) ) {
1058 throw new Exception( __( 'Incorrect confirmation password!', 'learnpress' ) );
1059 } else {
1060 $update_data = array(
1061 'user_pass' => $new_pass,
1062 'ID' => get_current_user_id(),
1063 );
1064 $return = wp_update_user( $update_data );
1065
1066 if ( is_wp_error( $return ) ) {
1067 return $wp_error ? $return : false;
1068 }
1069
1070 return $return;
1071 }
1072 }
1073 } catch ( Exception $ex ) {
1074 return $wp_error ? new WP_Error( 'UPDATE_PROFILE_ERROR', $ex->getMessage() ) : false;
1075 }
1076 }
1077
1078 function learn_press_get_avatar_thumb_size() {
1079 $option = LP_Settings::get_option(
1080 'avatar_dimensions',
1081 array(
1082 'width' => 250,
1083 'height' => 250,
1084 )
1085 );
1086
1087 if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) {
1088 $option = array(
1089 'width' => 250,
1090 'height' => 250,
1091 );
1092 }
1093
1094 return $option;
1095 }
1096
1097 function learn_press_get_course_thumbnail_dimensions() {
1098 $option = LP_Settings::get_option(
1099 'course_thumbnail_dimensions',
1100 array(
1101 'width' => 500,
1102 'height' => 300,
1103 )
1104 );
1105
1106 if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) {
1107 $option = array(
1108 'width' => 500,
1109 'height' => 300,
1110 );
1111 }
1112
1113 return $option;
1114 }
1115
1116 /**
1117 * Set a fake cookie to
1118 * @deprecated 4.2.0
1119 */
1120 function learn_press_set_user_cookie_for_guest() {
1121 _deprecated_function( __METHOD__, '4.2.0' );
1122 if ( ! is_admin() && ! headers_sent() ) {
1123 $guest_key = '_wordpress_lp_guest';
1124
1125 if ( is_user_logged_in() ) {
1126 if ( ! empty( $_COOKIE[ $guest_key ] ) ) {
1127 learn_press_remove_cookie( $guest_key );
1128 }
1129 } else {
1130 if ( empty( $_COOKIE[ $guest_key ] ) ) {
1131 learn_press_setcookie( $guest_key, md5( time() ), time() + 3600 );
1132 }
1133 }
1134 }
1135 }
1136
1137 //add_action( 'wp', 'learn_press_set_user_cookie_for_guest' );
1138
1139 function learn_press_get_user_avatar( $user_id = 0, $size = '' ) {
1140 $user = learn_press_get_user( $user_id );
1141
1142 return $user->get_profile_picture( '', $size );
1143 }
1144
1145 /**
1146 * Get profile instance for an user to view.
1147 *
1148 * @param int $for_user
1149 *
1150 * @return LP_Profile|WP_Error
1151 */
1152 function learn_press_get_profile( $for_user = 0 ) {
1153 return LP_Profile::instance( $for_user );
1154 }
1155
1156 /**
1157 * Remove items from learnpress_user_items.
1158 *
1159 * @param int $user_id
1160 * @param int $item_id
1161 * @param int $course_id
1162 * @param bool $include_course - Optional. If TRUE then remove course and it's items
1163 */
1164 function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) {
1165 global $wpdb;
1166
1167 settype( $item_id, 'array' );
1168
1169 $format = array_fill( 0, sizeof( $item_id ), '%d' );
1170 $where = '';
1171
1172 $args = array( $user_id );
1173 $args = array_merge( $args, $item_id );
1174
1175 if ( $course_id ) {
1176 $args[] = $course_id;
1177 $where = 'AND ref_id = %d';
1178 }
1179
1180 if ( $include_course ) {
1181 $where .= ' OR ( item_id = %d AND item_type = %s )';
1182 $args[] = $course_id;
1183 $args[] = LP_COURSE_CPT;
1184 }
1185
1186 $query = $wpdb->prepare(
1187 "
1188 DELETE
1189 FROM {$wpdb->learnpress_user_items}
1190 WHERE user_id = %d
1191 AND ( item_id IN(" . join( ',', $format ) . ")
1192 $where )
1193 ",
1194 $args
1195 );
1196 }
1197
1198 /**
1199 * Get user profile link
1200 *
1201 * @param int $user_id
1202 * @param null $tab
1203 *
1204 * @return mixed|string
1205 */
1206 function learn_press_user_profile_link( $user_id = 0, $tab = null ) {
1207 if ( ! $user_id ) {
1208 $user_id = get_current_user_id();
1209 }
1210 $user = false;
1211 $deleted = in_array( $user_id, LP_User_Factory::$_deleted_users );
1212 if ( ! $deleted ) {
1213 if ( is_numeric( $user_id ) ) {
1214 $user = get_user_by( 'id', $user_id );
1215 } else {
1216 $user = get_user_by( 'login', urldecode( $user_id ) );
1217 }
1218 } else {
1219 return '';
1220 }
1221 if ( ! $deleted && ! $user ) {
1222 LP_User_Factory::$_deleted_users[] = $user_id;
1223 }
1224
1225 $user = learn_press_get_user( $user_id );
1226
1227 if ( ! $user ) {
1228 return '';
1229 }
1230
1231 global $wp_query;
1232 $args = array(
1233 'user' => $user->get_username(),
1234 );
1235
1236 if ( isset( $args['user'] ) ) {
1237 if ( '' === $tab ) {
1238 $tab = learn_press_get_current_profile_tab();
1239 }
1240 if ( $tab ) {
1241 $args['tab'] = $tab;
1242 }
1243
1244 /**
1245 * If no tab is selected in profile and is current user
1246 * then no need the username in profile link.
1247 */
1248 if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) {
1249 unset( $args['user'] );
1250 }
1251 }
1252
1253 /*$args = array_map(
1254 function ( $string ) {
1255 return preg_replace( '/\s/', '+', $string );
1256 },
1257 $args
1258 );*/
1259 $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
1260 if ( $profile_link ) {
1261 if ( get_option( 'permalink_structure' ) /*&& learn_press_get_page_id( 'profile' )*/ ) {
1262 $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
1263 } else {
1264 $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
1265 }
1266 } else {
1267 $url = get_author_posts_url( $user_id );
1268 }
1269
1270 return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab );
1271 }
1272
1273 /**********************************************/
1274 /* Functions are used for hooks */
1275 /**********************************************/
1276
1277 function learn_press_hk_before_start_quiz( $true, $quiz_id, $course_id, $user_id ) {
1278 if ( 'yes' !== get_post_meta( $quiz_id, '_lp_archive_history', true ) ) {
1279 learn_press_remove_user_items( $user_id, $quiz_id, $course_id );
1280 }
1281
1282 return $true;
1283 }
1284
1285 add_filter( 'learn-press/before-start-quiz', 'learn_press_hk_before_start_quiz', 10, 4 );
1286
1287 /*function learn_press_default_user_item_status( $item_id ) {
1288 $status = '';
1289 switch ( learn_press_get_post_type( $item_id ) ) {
1290 case LP_LESSON_CPT:
1291 $status = 'started';
1292 break;
1293 case LP_QUIZ_CPT:
1294 $status = 'viewed';
1295 break;
1296 case LP_COURSE_CPT:
1297 $status = 'enrolled';
1298 }
1299
1300 return apply_filters( 'learn-press/default-user-item-status', $status, $item_id );
1301 }*/
1302
1303 /**
1304 * Get current state of distraction mode
1305 *
1306 * @return mixed
1307 * @since 3.1.0
1308 * @deprecated 4.2.0
1309 */
1310 function learn_press_get_user_distraction() {
1311 _deprecated_function( __FUNCTION__, '4.2.0' );
1312 if ( is_user_logged_in() ) {
1313 return get_user_option( 'distraction_mode', get_current_user_id() );
1314 } else {
1315 return LearnPress::instance()->session->distraction_mode;
1316 }
1317 }
1318
1319 /**
1320 * @deprecated 4.2.0
1321 */
1322 function learn_press_get_user_role( $user_id ) {
1323 _deprecated_function( __FUNCTION__, '4.2.0' );
1324 if ( $user = learn_press_get_user( $user_id ) ) {
1325 return $user->get_role();
1326 }
1327
1328 return false;
1329 }
1330
1331 /**
1332 * @param array $args
1333 * @param bool $wp_error
1334 *
1335 * @return bool|int|LP_User_Item|mixed|WP_Error
1336 */
1337 function learn_press_create_user_item( $args = array(), $wp_error = false ) {
1338 global $wpdb;
1339
1340 $defaults = array(
1341 'user_id' => get_current_user_id(),
1342 'item_id' => '',
1343 'start_time' => time(),
1344 'end_time' => '',
1345 'graduation' => '',
1346 'item_type' => '',
1347 'status' => '',
1348 'ref_id' => 0,
1349 'ref_type' => '',
1350 'parent_id' => 0,
1351 'create_meta' => array(),
1352 );
1353
1354 $item_data = wp_parse_args( $args, $defaults );
1355
1356 // Validate item_id and post type
1357 if ( empty( $item_data['item_id'] ) ) {
1358 if ( $wp_error ) {
1359 return new WP_Error( 'invalid_item_id', __( 'Invalid item id.', 'learnpress' ) );
1360 }
1361
1362 return 0;
1363 }
1364
1365 $post_type = learn_press_get_post_type( $item_data['item_id'] );
1366 if ( empty( $item_data['item_type'] ) && $post_type ) {
1367 $item_data['item_type'] = $post_type;
1368 }
1369
1370 // Get id and type of ref if they are null
1371 if ( ! empty( $item_data['parent_id'] ) && ( empty( $item_data['ref_id'] ) || ( empty( $item_data['ref_type'] ) ) ) ) {
1372 $parent = $wpdb->get_row(
1373 $wpdb->prepare(
1374 "SELECT * FROM {$wpdb->learnpress_user_items} WHERE %d",
1375 $item_data['parent_id']
1376 )
1377 );
1378
1379 if ( $parent ) {
1380 if ( empty( $item_data['ref_id'] ) ) {
1381 $item_data['ref_id'] = $parent->item_id;
1382 }
1383
1384 if ( empty( $item_data['ref_type'] ) ) {
1385 $item_data['ref_type'] = $parent->item_type;
1386 }
1387 }
1388 }
1389
1390 // Filter
1391 $item_data = apply_filters( 'learn-press/create-user-item-data', $item_data );
1392 if ( ! $item_data ) {
1393 if ( $wp_error ) {
1394 return new WP_Error( 'invalid_item_data', __( 'Invalid item data.', 'learnpress' ) );
1395 }
1396
1397 return 0;
1398 }
1399
1400 do_action( 'learn-press/before-create-user-item', $item_data );
1401
1402 $create_meta = ! empty( $item_data['create_meta'] ) ? $item_data['create_meta'] : false;
1403
1404 if ( $create_meta ) {
1405 unset( $item_data['create_meta'] );
1406 }
1407
1408 $user_item = new LP_User_Item( $item_data );
1409 $result = $user_item->update();
1410 if ( ! $result || is_wp_error( $result ) ) {
1411 if ( $wp_error && is_wp_error( $result ) ) {
1412 return $result;
1413 }
1414
1415 return 0;
1416 }
1417
1418 do_action( 'learn-press/created-user-item', $user_item, $item_data );
1419
1420 $create_meta = apply_filters( 'learn-press/create-user-item-meta', $create_meta, $item_data );
1421 if ( ! $create_meta ) {
1422 return $user_item;
1423 }
1424
1425 do_action( 'learn-press/before-create-user-item-meta', $create_meta );
1426
1427 foreach ( $create_meta as $key => $value ) {
1428 learn_press_update_user_item_meta( $user_item->get_user_item_id(), $key, $value );
1429 }
1430
1431 do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta );
1432
1433 return $user_item;
1434 }
1435
1436 /**
1437 * @param array $args
1438 * @param bool $wp_error - Optional. TRUE will return WP_Error on fail.
1439 *
1440 * @return bool|array|LP_User_Item|WP_Error
1441 */
1442 function learn_press_create_user_item_for_quiz( $args = array(), $wp_error = false ) {
1443 $item_data = wp_parse_args(
1444 $args,
1445 array(
1446 'item_type' => LP_QUIZ_CPT,
1447 'status' => LP_ITEM_STARTED,
1448 'graduation' => LP_COURSE_GRADUATION_IN_PROGRESS,
1449 'user_id' => get_current_user_id(),
1450 )
1451 );
1452
1453 $user_item = learn_press_create_user_item( $item_data, $wp_error );
1454
1455 if ( $user_item && ! is_wp_error( $user_item ) ) {
1456 $user_item = new LP_User_Item_Quiz( $user_item->get_data() );
1457 $user_item->update();
1458 }
1459
1460 return $user_item;
1461 }
1462
1463 /**
1464 * Get list user_item_id for Quiz in table learnpress_user_items
1465 *
1466 * @param int $quiz_id
1467 * @param int $course_id
1468 * @return array || false
1469 */
1470 function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) {
1471 global $wpdb;
1472
1473 $query = $wpdb->prepare( "SELECT user_item_id FROM $wpdb->learnpress_user_items WHERE ref_id=%d AND item_id=%d", $course_id, $quiz_id );
1474 $col = $wpdb->get_col( $query );
1475
1476 if ( ! empty( $col ) ) {
1477 return $col;
1478 } else {
1479 return false;
1480 }
1481 }
1482
1483 /**
1484 * Create new user item prepare for user starts a quiz
1485 * Update error retry course not work - Nhamdv.
1486 *
1487 * @param int $quiz_id
1488 * @param int $user_id
1489 * @param int $course_id
1490 * @param bool $wp_error
1491 *
1492 * @return array|bool|LP_User_Item|WP_Error
1493 * @since 4.0.0
1494 */
1495 function learn_press_user_start_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) {
1496 if ( ! $user_id ) {
1497 $user_id = get_current_user_id();
1498 }
1499
1500 global $wpdb;
1501
1502 $query = $wpdb->prepare(
1503 "
1504 SELECT user_item_id, item_id id, item_type type
1505 FROM {$wpdb->learnpress_user_items}
1506 WHERE user_item_id = (SELECT max(user_item_id)
1507 FROM {$wpdb->learnpress_user_items}
1508 WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress'))
1509 ",
1510 $user_id,
1511 $course_id
1512 );
1513
1514 $parent = $wpdb->get_row( $query );
1515
1516 do_action( 'learn-press/before-user-start-quiz', $quiz_id, $user_id, $course_id );
1517
1518 $user = learn_press_get_user( $user_id );
1519 $course_data = $user->get_course_data( $course_id );
1520 $quiz_data = $course_data->get_item( $quiz_id );
1521
1522 $quiz = LP_Quiz::get_quiz( $quiz_id );
1523 $duration = $quiz->get_duration();
1524 $user_quiz = learn_press_create_user_item_for_quiz(
1525 array(
1526 'user_item_id' => $quiz_data ? $quiz_data->get_user_item_id() : 0,
1527 'item_id' => $quiz->get_id(),
1528 'duration' => $duration ? $duration->get() : 0,
1529 'user_id' => $user_id,
1530 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0,
1531 'ref_type' => $parent ? $parent->type : '',
1532 'ref_id' => $parent ? $parent->id : '',
1533 ),
1534 $wp_error
1535 );
1536
1537 if ( $user_quiz && ! is_wp_error( $user_quiz ) ) {
1538 do_action( 'learn-press/user-started-quiz', $user_quiz, $quiz_id, $user_id, $course_id );
1539 }
1540
1541 // Reset first cache
1542 //$user_quiz->get_status( 'status', true );
1543
1544 return $user_quiz;
1545 }
1546
1547 /**
1548 * Function retake quiz.
1549 *
1550 * @param [type] $quiz_id
1551 * @param integer $user_id
1552 * @param integer $course_id
1553 * @param boolean $wp_error
1554 *
1555 * @return void
1556 */
1557 function learn_press_user_retake_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) {
1558 if ( ! $user_id ) {
1559 $user_id = get_current_user_id();
1560 }
1561
1562 if ( ! $course_id ) {
1563 return new WP_Error( 'invalid_course_id', esc_html__( 'Invalid Course ID.', 'learnpress' ) );
1564 }
1565
1566 global $wpdb;
1567
1568 $query = $wpdb->prepare(
1569 "
1570 SELECT user_item_id, item_id id, item_type type
1571 FROM {$wpdb->learnpress_user_items}
1572 WHERE user_item_id = (SELECT max(user_item_id)
1573 FROM {$wpdb->learnpress_user_items}
1574 WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress'))
1575 ",
1576 $user_id,
1577 $course_id
1578 );
1579
1580 $parent = $wpdb->get_row( $query );
1581
1582 if ( ! $parent ) {
1583 return new WP_Error( 'invalid_user_item', esc_html__( 'Invalid Quiz', 'learnpress' ) );
1584 }
1585
1586 $data = learn_press_get_user_item(
1587 array(
1588 'item_id' => $quiz_id,
1589 'user_id' => $user_id,
1590 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0,
1591 'ref_type' => $parent ? $parent->type : LP_COURSE_CPT,
1592 'ref_id' => $parent ? $parent->id : '',
1593 )
1594 );
1595
1596 $user_item = new LP_User_Item_Quiz( $data );
1597
1598 $ratake_count = get_post_meta( $quiz_id, '_lp_retake_count', true );
1599
1600 if ( $ratake_count > 0 ) {
1601 $user_item->update_retake_count();
1602 }
1603
1604 // Create new result in table learnpress_user_item_results.
1605 LP_User_Items_Result_DB::instance()->insert( $data->user_item_id );
1606
1607 // Remove user_item_meta.
1608 learn_press_delete_user_item_meta( $data->user_item_id, '_lp_question_checked' );
1609
1610 $user_item->set_status( LP_ITEM_STARTED )
1611 ->set_start_time( time() ) // Error Retake when change timezone - Nhamdv
1612 ->set_end_time()
1613 ->set_graduation( LP_COURSE_GRADUATION_IN_PROGRESS )
1614 ->update();
1615
1616 // Reset first cache
1617 //$user_item->get_status( 'status', true );
1618
1619 // Error Retake when change timezone - Nhamdv
1620 // learn_press_update_user_item_field(
1621 // array(
1622 // 'start_time' => current_time( 'mysql', true ),
1623 // ),
1624 // array(
1625 // 'user_item_id' => $data->user_item_id,
1626 // )
1627 // );
1628
1629 return $user_item;
1630 }
1631
1632
1633 /**
1634 * Prepares list of questions for rest api.
1635 *
1636 * @param int[] $question_ids
1637 * @param array $args
1638 *
1639 * @return array
1640 * @since 3.3.0
1641 */
1642 function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ) : array {
1643 if ( is_numeric( $args ) ) {
1644
1645 } else {
1646 $args = wp_parse_args(
1647 $args,
1648 array(
1649 'instant_hint' => true,
1650 'instant_check' => true,
1651 'quiz_status' => '',
1652 'checked_questions' => array(),
1653 'hinted_questions' => array(),
1654 'answered' => array(),
1655 'show_correct_review' => true,
1656 )
1657 );
1658 }
1659
1660 $checkedQuestions = $args['checked_questions'];
1661 $hintedQuestions = $args['hinted_questions'];
1662 $instantHint = $args['instant_hint'];
1663 $instantCheck = $args['instant_check'];
1664 $quizStatus = $args['quiz_status'];
1665 $answered = $args['answered'];
1666 $status = $args['status'] ?? '';
1667 $questions = array();
1668
1669 if ( $question_ids ) {
1670 foreach ( $question_ids as $id ) {
1671 $question = learn_press_get_question( $id );
1672 $hasHint = false;
1673 $hasExplanation = false;
1674 $canCheck = false;
1675 $hinted = false;
1676 $checked = false;
1677 $theHint = $question->get_hint();
1678 $theExplanation = '';
1679
1680 if ( $instantCheck || $status == 'completed' ) {
1681 $theExplanation = $question->get_explanation();
1682 $checked = in_array( $id, $checkedQuestions );
1683 $hasExplanation = ! ! $theExplanation;
1684 }
1685
1686 $mark = $question->get_mark() ? $question->get_mark() : 1;
1687
1688 $questionData = array(
1689 'object' => $question,
1690 'id' => absint( $id ),
1691 'title' => $question->get_title(),
1692 'type' => $question->get_type(),
1693 'point' => $mark,
1694 );
1695
1696 $content = $question->get_content();
1697 if ( $content ) {
1698 $questionData['content'] = $content;
1699 }
1700
1701 if ( $theHint ) {
1702 $questionData['hint'] = $theHint;
1703 }
1704
1705 if ( $status == 'completed' || ( $checked && $theExplanation ) ) {
1706 $questionData['explanation'] = $theExplanation;
1707 }
1708
1709 if ( $hasExplanation ) {
1710 $questionData['has_explanation'] = $hasExplanation;
1711
1712 if ( $checked ) {
1713 $questionData['explanation'] = $theExplanation;
1714 }
1715 }
1716
1717 $with_true_or_false = ( $checked || ( $quizStatus == 'completed' && $args['show_correct_review'] ) );
1718
1719 // if ( $question->is_support( 'answer-options' ) ) {
1720 $questionData['options'] = learn_press_get_question_options_for_js(
1721 $question,
1722 array(
1723 'include_is_true' => $with_true_or_false,
1724 'answer' => $answered[ $id ]['answered'] ?? '',
1725 )
1726 );
1727 // }
1728
1729 $questions[] = $questionData;
1730 }
1731
1732 /**
1733 * Remove answered
1734 */
1735 if ( $quizStatus !== 'completed' ) {
1736 if ( $checkedQuestions && $quizStatus ) {
1737
1738 $omitIds = array_diff( $question_ids, $checkedQuestions );
1739
1740 if ( $omitIds ) {
1741 foreach ( $omitIds as $omitId ) {
1742 if ( ! empty( $answered[ $omitId ] ) ) {
1743 unset( $answered[ $omitId ] );
1744 }
1745 }
1746 }
1747 }
1748 }
1749 }
1750
1751 return apply_filters( 'learn-press/list-questions-data', $questions );
1752 }
1753
1754 /**
1755 * Update extra profile data upon update user.
1756 *
1757 * @param int $user_id
1758 *
1759 * @since 4.0.0
1760 */
1761 function learn_press_update_extra_user_profile_fields( $user_id ) {
1762 if ( ! current_user_can( 'edit_user', $user_id ) ) {
1763 return;
1764 }
1765
1766 if ( isset( $_POST['_lp_extra_info'] ) ) {
1767 update_user_meta( $user_id, '_lp_extra_info', LP_Helper::sanitize_params_submitted( $_POST['_lp_extra_info'] ) );
1768 }
1769 }
1770 add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' );
1771 add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' );
1772
1773 /**
1774 * Get extra profile info data
1775 *
1776 * @param int $user_id
1777 *
1778 * @return array
1779 * @since 4.0.0
1780 */
1781 function learn_press_get_user_extra_profile_info( $user_id = 0 ) {
1782 if ( ! $user_id ) {
1783 $user_id = get_current_user_id();
1784 }
1785
1786 $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id );
1787 $extra_fields = learn_press_get_user_extra_profile_fields();
1788
1789 $extra_profile_info = wp_parse_args(
1790 $extra_profile_info,
1791 array_fill_keys( array_keys( $extra_fields ), '' )
1792 );
1793
1794 return apply_filters( 'learn-press/user-extra-profile-info', $extra_profile_info, $user_id );
1795 }
1796
1797 function learn_press_social_profiles() {
1798 return apply_filters(
1799 'learn-press/social-profiles',
1800 array(
1801 'facebook',
1802 'twitter',
1803 'youtube',
1804 'linkedin',
1805 )
1806 );
1807 }
1808
1809 function lp_add_default_fields( $fields ) {
1810 $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' );
1811
1812 if ( $first_name === 'yes' ) {
1813 ?>
1814 <li class="form-field">
1815 <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label>
1816 <input id="reg_first_name" name="reg_first_name" type="text"
1817 placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1818 value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1819 </li>
1820 <?php
1821 }
1822
1823 $last_name = LP_Settings::instance()->get( 'enable_register_last_name' );
1824
1825 if ( $last_name === 'yes' ) {
1826 ?>
1827 <li class="form-field">
1828 <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label>
1829 <input id="reg_last_name" name="reg_last_name" type="text"
1830 placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1831 value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1832 </li>
1833 <?php
1834 }
1835
1836 $display_name = LP_Settings::instance()->get( 'enable_register_display_name' );
1837
1838 if ( $display_name === 'yes' ) {
1839 ?>
1840 <li class="form-field">
1841 <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label>
1842 <input id="reg_display_name" name="reg_display_name" type="text"
1843 placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1844 value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1845 </li>
1846 <?php
1847 }
1848 }
1849
1850 add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' );
1851
1852 function lp_custom_register_fields_display() {
1853 ?>
1854 <?php $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); ?>
1855
1856 <?php if ( $custom_fields ) : ?>
1857 <?php foreach ( $custom_fields as $custom_field ) : ?>
1858 <?php
1859 $cf_class = '';
1860 if ( $custom_field['required'] == 'yes' ) {
1861 $cf_class = ' required';
1862 ?>
1863 <style>
1864 .required label {
1865 font-weight: bold;
1866 }
1867 .required label:after {
1868 content: ' *';
1869 display:inline;
1870 }
1871 </style>
1872 <?php
1873 }
1874
1875 if ( isset( $custom_field['id'] ) ) {
1876 ?>
1877 <?php $value = $custom_field['id']; ?>
1878
1879 <li class="form-field<?php echo esc_attr( $cf_class ); ?>">
1880 <?php
1881 switch ( $custom_field['type'] ) {
1882 case 'text':
1883 case 'number':
1884 case 'email':
1885 case 'url':
1886 case 'tel':
1887 ?>
1888 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1889 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1890 type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1891 value="" />
1892 <?php
1893 break;
1894 case 'textarea':
1895 ?>
1896 <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1897 <textarea name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"></textarea>
1898 <?php
1899 break;
1900 case 'checkbox':
1901 ?>
1902 <label>
1903 <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1904 type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1905 <?php echo esc_html( $custom_field['name'] ); ?>
1906 </label>
1907 <?php
1908 break;
1909 }
1910 ?>
1911 </li>
1912 <?php } ?>
1913 <?php endforeach; ?>
1914 <?php endif; ?>
1915 <?php
1916 }
1917
1918 add_action( 'learn-press/after-form-register-fields', 'lp_custom_register_fields_display' );
1919
1920 /**
1921 * Custom register fields
1922 *
1923 * @param [type] $user_id
1924 *
1925 * @return void
1926 */
1927 function lp_user_custom_register_fields( $user_id, $fields = array() ) {
1928 if ( ! empty( $fields ) ) {
1929 update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $fields ) );
1930 } elseif ( isset( $_POST['_lp_custom_register'] ) ) {
1931 update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ) );
1932 }
1933 }
1934
1935 add_action( 'personal_options_update', 'lp_user_custom_register_fields' );
1936 add_action( 'edit_user_profile_update', 'lp_user_custom_register_fields' );
1937
1938 function lp_get_user_custom_register_fields( $user_id = 0 ) {
1939 if ( ! $user_id ) {
1940 $user_id = get_current_user_id();
1941 }
1942
1943 $register_fields = get_the_author_meta( '_lp_custom_register', $user_id );
1944 $defaults = lp_get_user_custom_fields();
1945
1946 $extra_profile_info = wp_parse_args( $register_fields, $defaults );
1947
1948 return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id );
1949 }
1950
1951 function lp_get_user_custom_fields() {
1952 $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' );
1953
1954 $output = array();
1955
1956 if ( $custom_fields ) {
1957 foreach ( $custom_fields as $field ) {
1958 $output[ $field['id'] ] = '';
1959 }
1960 }
1961
1962 return $output;
1963 }
1964
1965 /**
1966 * Check extra user data is a social profile.
1967 *
1968 * @param $key
1969 *
1970 * @return bool
1971 * @since 4.0.0
1972 */
1973 function learn_press_is_social_profile( $key ) {
1974 $is_socials = learn_press_social_profiles();
1975
1976 return in_array( $key, $is_socials );
1977 }
1978
1979 function learn_press_social_profile_name( $key ) {
1980 $name = '';
1981 switch ( $key ) {
1982 case 'facebook':
1983 $name = esc_html__( 'Facebook Profile', 'learnpress' );
1984 break;
1985 case 'twitter':
1986 $name = esc_html__( 'Twitter Profile', 'learnpress' );
1987 break;
1988 case 'googleplus':
1989 $name = esc_html__( 'Google Profile', 'learnpress' );
1990 break;
1991 case 'youtube':
1992 $name = esc_html__( 'Youtube Channel', 'learnpress' );
1993 break;
1994 case 'linkedin':
1995 $name = esc_html__( 'Linkedin Profile', 'learnpress' );
1996 break;
1997 default:
1998 $name = ucfirst( $key );
1999 }
2000
2001 return apply_filters( 'learn-press/social-profile-name', $name, $key );
2002 }
2003
2004 /**
2005 * Get extra profile fields will be registered in backend profile.
2006 *
2007 * @return array
2008 * @since 4.0.0
2009 */
2010 function learn_press_get_user_extra_profile_fields() {
2011 $socials = learn_press_social_profiles();
2012 $fields = array();
2013
2014 foreach ( $socials as $social ) {
2015 $fields[ $social ] = learn_press_social_profile_name( $social );
2016 }
2017
2018 return apply_filters( 'learn-press/user-extra-profile-fields', $fields );
2019 }
2020
2021 /**
2022 * Show courses user enrolled on backend
2023 *
2024 * @param $user
2025 *
2026 * @return void
2027 */
2028 function learn_press_user_profile_data( $user ) {
2029 if ( ! is_admin() ) {
2030 return;
2031 }
2032
2033 learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) );
2034 learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) );
2035 }
2036 add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 );
2037
2038 function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) {
2039 if ( empty( $user_id ) ) {
2040 return false;
2041 }
2042
2043 $args = array(
2044 'author' => $user_id,
2045 'posts_per_page' => - 1,
2046 'post_type' => $post_type,
2047 'post_status' => 'any',
2048 );
2049
2050 $posts = get_posts( $args );
2051
2052 $output = array(
2053 'all' => count( $posts ),
2054 'publish' => array(),
2055 'pending' => array(),
2056 );
2057
2058 $pending = $public = array();
2059
2060 if ( ! empty( $posts ) ) {
2061 foreach ( $posts as $post ) {
2062 switch ( $post->post_status ) {
2063 case 'pending':
2064 $pending[] = $post;
2065 break;
2066 case 'publish':
2067 $public[] = $post;
2068 break;
2069 default:
2070 break;
2071 }
2072 }
2073 }
2074
2075 return array(
2076 'all' => count( $posts ),
2077 'publish' => count( $public ),
2078 'pending' => count( $pending ),
2079 );
2080
2081 }
2082