PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 All 140 releases
← All changes | inc/user/abstract-lp-user.php +136 -219 4.1.7.3.1 → 4.4.9 View file →
@@ -10,8 +10,11 @@
10 10
11 11 /**
12 12 * Prevent loading this file directly
13 13 */
14 +
15 +use LearnPress\Models\UserItems\UserCourseModel;
16 +
14 17 defined( 'ABSPATH' ) || exit();
15 18
16 19 if ( ! class_exists( 'LP_Abstract_User' ) ) {
17 20
@@ -79,9 +82,8 @@
79 82
80 83 if ( $this->get_id() > 0 ) {
81 84 $this->load();
82 85 }
83 -
84 86 }
85 87
86 88 /**
87 89 * Load user data from curd
@@ -95,14 +97,14 @@
95 97 *
96 98 * @param int $course_id .
97 99 *
98 100 * @return LP_User_Item_Course|bool
99 - * @version 3.1.3
100 - * @editor tungnx
101 + * @version 3.1.4
101 102 * @modify 4.1.3
102 103 */
103 104 public function get_course_data( int $course_id = 0 ) {
104 - $lp_user_items_db = LP_User_Items_DB::getInstance();
105 + $lp_user_items_db = LP_User_Items_DB::getInstance();
106 + $object_course_data = false;
105 107
106 108 try {
107 109 if ( ! $course_id ) {
108 110 $course_id = get_the_ID();
@@ -107,21 +109,14 @@
107 109 if ( ! $course_id ) {
108 110 $course_id = get_the_ID();
109 111 }
110 112
111 - /*if ( $this instanceof LP_User_Guest ) {
112 - throw new Exception( 'User is Guest' );
113 - }*/
114 -
115 - $filter = new LP_User_Items_Filter();
116 - $filter->item_id = $course_id;
117 - $filter->user_id = $this->get_id();
118 - $last_user_course = $lp_user_items_db->get_last_user_course( $filter );
119 -
120 - if ( $last_user_course ) {
121 - $object_course_data = new LP_User_Item_Course( $last_user_course );
113 + $userCourseModel = UserCourseModel::find( $this->get_id(), $course_id, true );
114 + if ( $userCourseModel ) {
115 + $userCourseArr = (array) $userCourseModel;
116 + $userCourseArr['user_item_id'] = $userCourseModel->get_user_item_id();
117 + $object_course_data = new LP_User_Item_Course( $userCourseArr );
122 118 } else {
123 - $object_course_data = false;
124 119 /**
125 120 * Todo: some themes still not check false, so still use below code.\
126 121 * @editor tungnx 4.1.6.9
127 122 */
@@ -134,8 +129,28 @@
134 129 return $object_course_data;
135 130 }
136 131
137 132 /**
133 + * Get course attend of user not Guest.
134 + * Replace get_course_data method.
135 + *
136 + * @param int $course_id
137 + * @return false|UserCourseModel
138 + * @since 4.2.5
139 + * @version 1.0.1
140 + */
141 + public function get_course_attend( int $course_id = 0 ) {
142 + if ( $this instanceof LP_User_Guest ) {
143 + return false;
144 + }
145 +
146 + $filter = new LP_User_Items_Filter();
147 + $filter->item_id = $course_id;
148 + $filter->user_id = $this->get_id();
149 + return UserCourseModel::get_user_item_model_from_db( $filter );
150 + }
151 +
152 + /**
138 153 * Get user course item.
139 154 *
140 155 * @param int $item_id
141 156 * @param int $course_id
@@ -523,58 +538,8 @@
523 538 return $result;
524 539 }
525 540
526 541 /**
527 - * Mark question that user has checked.
528 - *
529 - * @param int $question_id
530 - * @param int $quiz_id
531 - * @param int $course_id
532 - *
533 - * @return WP_Error|mixed
534 - * @since 3.0.0
535 - */
536 - public function hint( $question_id, $quiz_id, $course_id ) {
537 - $course = learn_press_get_course( $course_id );
538 - if ( ! $course ) {
539 - return false;
540 - }
541 -
542 - if ( ! $course->has_item( $quiz_id ) ) {
543 - return false;
544 - }
545 -
546 - /**
547 - * @var $quiz LP_Quiz
548 - */
549 - $quiz = $course->get_item( $quiz_id );
550 - if ( ! $quiz instanceof LP_Course_Item_Quiz ) {
551 - return false;
552 - }
553 -
554 - if ( ! $quiz->has_question( $question_id ) ) {
555 - return false;
556 - }
557 -
558 - $quiz_data = $this->get_item_data( $quiz_id, $course_id );
559 - $remain = $quiz_data->hint( $question_id );
560 - if ( false === $remain ) {
561 - return new WP_Error( 1001, __( 'You can not hint at the question.', 'learnpress' ) );
562 - }
563 -
564 - return $remain;
565 - }
566 -
567 - public function get_quiz_last_results( $quiz_id ) {
568 - $results = $this->get_course_info( $quiz_id );
569 - if ( $results ) {
570 - $results = reset( $results );
571 - }
572 -
573 - return apply_filters( 'learn_press_user_quiz_last_results', $results, $quiz_id, $this );
574 - }
575 -
576 - /**
577 542 * Check if user has at least one role.
578 543 *
579 544 * @param array|string $roles
580 545 *
@@ -606,30 +571,8 @@
606 571
607 572 return $is;
608 573 }
609 574
610 - /**
611 - *
612 - * Check what the user can do
613 - *
614 - * @param $role
615 - *
616 - * @return mixed
617 - * @throws Exception
618 - */
619 - public function can( $role ) {
620 - _deprecated_function( __FUNCTION__, '3.0.8' );
621 - $args = func_get_args();
622 - unset( $args[0] );
623 - $method = 'can_' . preg_replace( '!-!', '_', $role );
624 - $callback = array( $this, $method );
625 - if ( is_callable( $callback ) ) {
626 - return call_user_func_array( $callback, $args );
627 - } else {
628 - throw new Exception( sprintf( __( 'The role %s for the user doesn\'t exist', 'learnpress' ), $role ) );
629 - }
630 - }
631 -
632 575 public function can_edit_item( $item_id, $course_id = 0 ) {
633 576 $return = $this->is_admin();
634 577
635 578 if ( ! $return ) {
@@ -644,24 +587,8 @@
644 587 return apply_filters( 'learn_press_user_can_edit_item', $return, $item_id, $course_id, $this->get_id() );
645 588 }
646 589
647 590 /**
648 - * Check if user can finish course by getting current progress
649 - * and compares with course passing condition.
650 - *
651 - * @param int $course_id
652 - *
653 - * @return bool
654 - * @editor tungnx
655 - * @modify 4.1.3
656 - */
657 - public function can_finish_course( int $course_id ) {
658 - _deprecated_function( __FUNCTION__, '4.1.3', 'is_course_finished' );
659 -
660 - return true;
661 - }
662 -
663 - /**
664 591 * Check if course has any passed status for an user.
665 592 * Statuses: depending on value of column `status` in user_items.
666 593 * - purchased: bought and order is completed, `start_date` and `end_date` is null
667 594 * - enrolled: value of column `status` in user_items is enrolled
@@ -718,8 +645,20 @@
718 645 //$user_course->save();
719 646 $return = $user_course->complete( LP_COURSE_FINISHED );
720 647
721 648 if ( $return ) {
649 + /**
650 + * LP_User_Item::complete() writes straight to the table, so it never runs
651 + * UserCourseModel::clean_caches() the way UserItemModel::save() does. The
652 + * cached model still reports the enrollment as in progress, and that cache
653 + * outlives the request wherever a persistent object cache is installed - the
654 + * course then reads back unfinished and keeps offering "Finish course".
655 + */
656 + $userCourseModel = UserCourseModel::find( $this->get_id(), $course_id );
657 + if ( $userCourseModel instanceof UserCourseModel ) {
658 + $userCourseModel->clean_caches();
659 + }
660 +
722 661 do_action( 'learn-press/user-course-finished', $course_id, $this->get_id(), $return );
723 662 }
724 663 }
725 664
@@ -765,22 +704,8 @@
765 704 public function is_author_of( $post_id ) {
766 705 return absint( get_post_field( 'post_author', $post_id ) ) === $this->get_id();
767 706 }
768 707
769 - public function has( $role ) {
770 - _deprecated_function( __FUNCTION__, '3.0.8' );
771 -
772 - $args = func_get_args();
773 - unset( $args[0] );
774 - $method = 'has_' . preg_replace( '!-!', '_', $role );
775 - $callback = array( $this, $method );
776 - if ( is_callable( $callback ) ) {
777 - return call_user_func_array( $callback, $args );
778 - } else {
779 - throw new Exception( sprintf( __( 'The role %s for the user doesn\'t exist', 'learnpress' ), $role ) );
780 - }
781 - }
782 -
783 708 public function get( $role ) {
784 709 $args = func_get_args();
785 710 unset( $args[0] );
786 711 $method = 'get_' . preg_replace( '!-!', '_', $role );
@@ -1012,29 +937,8 @@
1012 937 return apply_filters( 'learn_press_user_has_completed_item', $return, $item );
1013 938 }
1014 939
1015 940 /**
1016 - * Get the remaining time of a course for the user.
1017 - *
1018 - * @param int $course_id
1019 - *
1020 - * @return bool|int|string
1021 - */
1022 - public function get_course_remaining_time( $course_id ) {
1023 - $course = learn_press_get_course( $course_id );
1024 - $remain = false;
1025 -
1026 - if ( $course && $course->get_id() ) {
1027 - $course_data = $this->get_course_data( $course_id, true );
1028 - if ( $course_data ) {
1029 - $remain = $course_data->is_exceeded();
1030 - }
1031 - }
1032 -
1033 - return $remain > 0 ? learn_press_seconds_to_weeks( $remain ) : false;
1034 - }
1035 -
1036 - /**
1037 941 * Get the order that contains the course.
1038 942 *
1039 943 * @param int $course_id
1040 944 *
@@ -1076,60 +980,8 @@
1076 980 return $wpdb->get_var( $query );
1077 981 }
1078 982
1079 983 /**
1080 - * @param $question_id
1081 - * @param null $quiz_id
1082 - *
1083 - * @return bool
1084 - */
1085 - public function get_answer_results( $question_id, $quiz_id = null ) {
1086 -
1087 - _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '3.0.0' );
1088 -
1089 - $data = false;
1090 - if ( ! $quiz_id ) {
1091 - $quiz_id = $this->get_quiz_by_question( $question_id );
1092 - }
1093 - if ( $quiz_id ) {
1094 - $question = LP_Question::get_question( $question_id );
1095 -
1096 - if ( $question ) {
1097 - $quiz_results = $this->get_quiz_results( $quiz_id );
1098 - if ( ! empty( $quiz_results->question_answers ) ) {
1099 - $question_answer = array_key_exists(
1100 - $question_id,
1101 - $quiz_results->question_answers
1102 - ) ? $quiz_results->question_answers[ $question_id ] : null;
1103 - $data = $question->check( $question_answer );
1104 - }
1105 - }
1106 - }
1107 -
1108 - return $data;
1109 - }
1110 -
1111 - /**
1112 - * @param $question_id
1113 - * @param null $quiz_id
1114 - *
1115 - * @return bool
1116 - */
1117 - public function is_answered_question( $question_id, $quiz_id = null ) {
1118 - if ( empty( $this->answered_questions ) ) {
1119 - $this->answered_questions = array();
1120 - }
1121 - if ( ! $quiz_id ) {
1122 - $quiz_id = $this->get_quiz_by_question( $question_id );
1123 - }
1124 -
1125 - $results = $this->get_quiz_results( $quiz_id );
1126 - $answered = ! empty( $results->question_answers ) ? $results->question_answers : array();
1127 -
1128 - return $answered ? array_key_exists( $question_id, $answered ) : false;
1129 - }
1130 -
1131 - /**
1132 984 * @return array
1133 985 */
1134 986 public function get_roles() {
1135 987 return (array) $this->get_data( 'roles' );
@@ -1152,25 +1004,8 @@
1152 1004 return $quiz_data ? $quiz_data->has_checked_question( $question_id ) : false;
1153 1005 }
1154 1006
1155 1007 /**
1156 - * @param $question_id
1157 - * @param $quiz_id
1158 - * @param int $course_id
1159 - *
1160 - * @return bool
1161 - */
1162 - public function has_hinted_answer( $question_id, $quiz_id, $course_id = 0 ) {
1163 - if ( ! $course_id ) {
1164 - $course_id = get_the_ID();
1165 - }
1166 -
1167 - $quiz_data = $this->get_item_data( $quiz_id, $course_id );
1168 -
1169 - return $quiz_data ? $quiz_data->has_hinted_question( $question_id ) : false;
1170 - }
1171 -
1172 - /**
1173 1008 * Return TRUE if user is already exists.
1174 1009 *
1175 1010 * @return bool
1176 1011 */
@@ -1194,13 +1029,33 @@
1194 1029 * @param mixed $size
1195 1030 *
1196 1031 * @return string
1197 1032 */
1198 - public function get_upload_profile_src( $size = '' ) {
1199 - return LP_Profile::instance( $this->get_id() )->get_upload_profile_src( $size );
1033 + public function get_upload_profile_src() {
1034 + return LP_Profile::instance( $this->get_id() )->get_upload_profile_src();
1200 1035 }
1201 1036
1202 1037 /**
1038 + * Get profile avatar url
1039 + *
1040 + * @return string
1041 + * @since 4.2.7.2
1042 + * @version 1.0.0
1043 + */
1044 + public function get_profile_avatar_url(): string {
1045 + $avatar_url = $this->get_upload_profile_src();
1046 + if ( empty( $avatar_url ) ) {
1047 + $args = learn_press_get_avatar_thumb_size();
1048 + $avatar_url = get_avatar_url( $this->get_id(), $args );
1049 + if ( empty( $avatar_url ) ) {
1050 + $avatar_url = LP_PLUGIN_URL . 'assets/images/avatar-default.png';
1051 + }
1052 + }
1053 +
1054 + return $avatar_url;
1055 + }
1056 +
1057 + /**
1203 1058 * @param string $type
1204 1059 * @param int $size
1205 1060 * @param bool $src_only
1206 1061 *
@@ -1211,11 +1066,13 @@
1211 1066 }
1212 1067
1213 1068 /**
1214 1069 * Get links socials of use on Profile page
1070 + * Icon is font awesome
1215 1071 *
1216 1072 * @param int $user_id
1217 1073 * @return array
1074 + * @deprecated 4.2.3
1218 1075 */
1219 1076 public function get_profile_socials( int $user_id = 0 ): array {
1220 1077 $socials = array();
1221 1078 $extra_info = learn_press_get_user_extra_profile_info( $user_id );
@@ -1227,21 +1084,24 @@
1227 1084 }
1228 1085
1229 1086 switch ( $k ) {
1230 1087 case 'facebook':
1231 - $i = '<i class="fab fa-facebook-f"></i>';
1088 + $i = '<i class="lp-icon-facebook-f"></i>';
1232 1089 break;
1233 1090 case 'twitter':
1234 - $i = '<i class="fab fa-twitter"></i>';
1091 + $i = '<i class="lp-icon-twitter"></i>';
1235 1092 break;
1236 1093 case 'googleplus':
1237 - $i = '<i class="fab fa-google-plus-g"></i>';
1094 + $i = '<i class="lp-icon-google-plus"></i>';
1238 1095 break;
1239 1096 case 'youtube':
1240 - $i = '<i class="fab fa-youtube"></i>';
1097 + $i = '<i class="lp-icon-youtube-play"></i>';
1241 1098 break;
1099 + case 'linkedin':
1100 + $i = '<i class="lp-icon-linkedin"></i>';
1101 + break;
1242 1102 default:
1243 - $i = sprintf( '<i class="fab fa-%s"></i>', $k );
1103 + $i = sprintf( '<i class="lp-icon-%s"></i>', $k );
1244 1104 }
1245 1105
1246 1106 $icon = apply_filters(
1247 1107 'learn-press/user-profile-social-icon',
@@ -1257,8 +1117,58 @@
1257 1117 return apply_filters( 'learn-press/user-profile-socials', $socials, $this->get_id(), $this );
1258 1118 }
1259 1119
1260 1120 /**
1121 + * Get links socials of use on Profile page
1122 + * Icon is svg
1123 + *
1124 + * @param int $user_id
1125 + * @return array
1126 + * @since 4.2.3
1127 + * @version 1.0.0
1128 + */
1129 + public function get_profile_social( int $user_id = 0 ): array {
1130 + $socials = array();
1131 + $extra_info = learn_press_get_user_extra_profile_info( $user_id );
1132 +
1133 + if ( $extra_info ) {
1134 + foreach ( $extra_info as $k => $v ) {
1135 + if ( empty( $v ) ) {
1136 + continue;
1137 + }
1138 +
1139 + switch ( $k ) {
1140 + case 'facebook':
1141 + $i = '<i class="lp-user-ico lp-icon-facebook"></i>';
1142 + break;
1143 + case 'twitter':
1144 + $i = '<i class="lp-user-ico lp-icon-twitter"></i>';
1145 + break;
1146 + case 'linkedin':
1147 + $i = '<i class="lp-user-ico lp-icon-linkedin"></i>';
1148 + break;
1149 + case 'youtube':
1150 + $i = '<i class="lp-user-ico lp-icon-youtube-play"></i>';
1151 + break;
1152 + default:
1153 + $i = sprintf( '<i class="lp-user-ico lp-icon-%s"></i>', esc_attr( $k ) );
1154 + }
1155 +
1156 + $icon = apply_filters(
1157 + 'learn-press/user-profile-social-icon',
1158 + $i,
1159 + $k,
1160 + $this->get_id(),
1161 + $this
1162 + );
1163 + $socials[ $k ] = sprintf( '<a href="%s">%s</a>', esc_url_raw( $v ), $icon );
1164 + }
1165 + }
1166 +
1167 + return apply_filters( 'learn-press/user-profile-socials', $socials, $this->get_id(), $this );
1168 + }
1169 +
1170 + /**
1261 1171 * Check course of user has graduation is in-progress
1262 1172 *
1263 1173 * @param $course_id
1264 1174 * @return bool
@@ -1267,9 +1177,9 @@
1267 1177 public function is_course_in_progress( $course_id ): bool {
1268 1178 $user = learn_press_get_user( $this->get_id() );
1269 1179 $user_course = $user->get_course_data( $course_id );
1270 1180
1271 - return $user_course && LP_COURSE_GRADUATION_IN_PROGRESS === $user_course->get_graduation();
1181 + return $user_course && LP_COURSE_GRADUATION_IN_PROGRESS === $user_course->get_graduation();
1272 1182 }
1273 1183
1274 1184 /**
1275 1185 * Return TRUE if user can do a quiz
@@ -1311,10 +1221,17 @@
1311 1221
1312 1222 return $course_results['completed_items'] >= $course->count_items();
1313 1223 }
1314 1224
1225 + /**
1226 + * Get role of user.
1227 + *
1228 + * @return string
1229 + * @since 3.0.0
1230 + * @version 1.0.1
1231 + */
1315 1232 public function get_role() {
1316 - return $this->is_admin() ? 'admin' : ( $this->is_instructor() ? 'instructor' : 'user' );
1233 + return $this->get_data( 'role' );
1317 1234 }
1318 1235
1319 1236 /**
1320 1237 * Get user course's grade.
@@ -1343,9 +1260,9 @@
1343 1260 *
1344 1261 * @return bool
1345 1262 */
1346 1263 public function is_guest(): bool {
1347 - return ! $this->get_id() || ! get_user_by( 'id', $this->get_id() );
1264 + return $this instanceof LP_User_Guest;
1348 1265 }
1349 1266
1350 1267 /**
1351 1268 * Check if user can edit a post.
@@ -1385,9 +1302,9 @@
1385 1302 *
1386 1303 * @return string
1387 1304 */
1388 1305 public function get_description(): string {
1389 - return $this->get_data( 'description', '' );
1306 + return get_the_author_meta( 'description', $this->get_id() );
1390 1307 }
1391 1308
1392 1309 /**
1393 1310 * Return user first name.