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-item/class-lp-user-item.php +63 -411 4.1.7 → 4.4.9 View file →
@@ -4,9 +4,9 @@
4 4 * Class LP_User_Item
5 5 *
6 6 * @since 3.0.0
7 7 */
8 -class LP_User_Item extends LP_Abstract_Object_Data implements ArrayAccess {
8 +class LP_User_Item extends LP_Abstract_Object_Data {
9 9 /**
10 10 * Auto increment
11 11 *
12 12 * @var int
@@ -67,14 +67,16 @@
67 67 *
68 68 * @var string
69 69 */
70 70 public $_parent_id = '';
71 -
72 -
73 71 /**
74 - * @var string
72 + * Key get data start time form DB.
75 73 */
76 - protected $_data_key = '';
74 + const KEY_DATA_START_TIME = 'start_time';
75 + /**
76 + * Key get data end time form DB.
77 + */
78 + const KEY_DATA_END_TIME = 'end_time';
77 79
78 80 /**
79 81 * LP_User_Item constructor.
80 82 *
@@ -115,34 +117,13 @@
115 117 $this->set_data( 'item_type', learn_press_get_post_type( $item['item_id'] ) );
116 118 $item_id = $item['item_id'];
117 119 }
118 120
119 - if ( ! empty( $item['start_time'] ) ) {
120 - $this->set_start_time( $item['start_time'] );
121 - } else {
122 - $this->set_start_time( current_time( 'mysql', 1 ) );
123 - }
121 + $this->set_start_time( $item[ self::KEY_DATA_START_TIME ] ?? time() );
122 + $this->set_end_time( $item[ self::KEY_DATA_END_TIME ] ?? '' );
123 + $this->set_user_id( absint( $item['user_id'] ?? get_current_user_id() ) );
124 + $this->set_status( $item['status'] ?? '' );
124 125
125 - if ( ! empty( $item['end_time'] ) ) {
126 - $this->set_end_time( $item['end_time'] );
127 - } else {
128 - $this->set_end_time( null );
129 - }
130 -
131 - if ( ! empty( $item['user_id'] ) ) {
132 - $item['user_id'] = absint( $item['user_id'] );
133 -
134 - $this->set_user_id( $item['user_id'] );
135 - } else {
136 - $this->set_user_id( get_current_user_id() );
137 - }
138 -
139 - if ( ! empty( $item['status'] ) ) {
140 - $this->set_status( $item['status'] );
141 - } else {
142 - $this->set_status( '' );
143 - }
144 -
145 126 if ( ! empty( $item['ref_id'] ) ) {
146 127 $item['ref_id'] = absint( $item['ref_id'] );
147 128
148 129 $this->set_ref_id( $item['ref_id'] );
@@ -164,15 +145,9 @@
164 145 if ( ! empty( $item['access_level'] ) ) {
165 146 $this->set_data( 'access_level', $item['access_level'] );
166 147 }
167 148
168 - if ( ! empty( $item['graduation'] ) ) {
169 - $this->set_data( 'graduation', $item['graduation'] );
170 - }
171 -
172 - $new_data = $this->get_mysql_data();
173 - ksort( $new_data );
174 - $this->_data_key = md5( serialize( $new_data ) );
149 + $this->set_data( 'graduation', $item['graduation'] ?? '' );
175 150 }
176 151
177 152 public function set_user_id( $user_id ) {
178 153 $this->set_data( 'user_id', $user_id );
@@ -220,32 +195,14 @@
220 195 * @param mixed $time .
221 196 *
222 197 * @return $this
223 198 */
224 - public function set_start_time( $time ) {
225 - $this->_set_data_date( 'start_time', $time );
199 + public function set_start_time( $time = '' ): LP_User_Item {
200 + $this->set_data_date( 'start_time', $time );
226 201
227 202 return $this;
228 203 }
229 204
230 - public function get_time( $field, $format = '', $human_diff_time = false ) {
231 - if ( ! $format ) {
232 - $format = get_option( 'date_format' );
233 - }
234 -
235 - $m_time = call_user_func( array( $this, 'get_' . $field ) );
236 - $time = mysql2date( 'G', call_user_func( array( $this, 'get_' . $field . '_gmt' ) ) );
237 - $time_diff = time() - $time;
238 -
239 - if ( $human_diff_time && $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
240 - $h_time = sprintf( __( '%s ago', 'learnpress' ), human_time_diff( $time ) );
241 - } else {
242 - $h_time = mysql2date( $format, $m_time );
243 - }
244 -
245 - return $h_time;
246 - }
247 -
248 205 /**
249 206 * Get start-time.
250 207 *
251 208 * @param string $format
@@ -250,43 +207,17 @@
250 207 *
251 208 * @param string $format
252 209 * @param bool $local
253 210 *
254 - * @return string|LP_Datetime
211 + * @return string|LP_Datetime|false
255 212 */
256 - public function get_start_time( $format = '', $local = false ) {
257 - $date = $this->get_data( 'start_time' );
213 + public function get_start_time( string $format = '', $local = false ) {
214 + $date = $this->get_data( self::KEY_DATA_START_TIME );
258 215
259 216 return $this->format_time( $date, $format, $local );
260 217 }
261 218
262 219 /**
263 - * @param $time
264 - *
265 - * @deprecated 4.0.0
266 - */
267 - public function set_start_time_gmt( $time ) {
268 - _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.0.0' );
269 - }
270 -
271 - /**
272 - * @param string $format
273 - *
274 - * @return array|bool|LP_Datetime|mixed|string
275 - * @deprecated
276 - */
277 - public function get_start_time_gmt( $format = '' ) {
278 - _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.0.0' );
279 -
280 - // $date = $this->get_data_date( 'start_time_gmt' );
281 - // if ( $format ) {
282 - // return $date->is_null() ? false : ( $format = 'i18n' ? learn_press_date_i18n( $date->getTimestamp() ) : $date->format( $format ) );
283 - // }
284 - //
285 - // return $date;
286 - }
287 -
288 - /**
289 220 * Set end-time for item.
290 221 *
291 222 * @param mixed $time
292 223 *
@@ -291,14 +222,10 @@
291 222 * @param mixed $time
292 223 *
293 224 * @return $this
294 225 */
295 - public function set_end_time( $time ) {
296 - if ( $time && '0000-00-00 00:00:00' !== $time ) {
297 - $this->_set_data_date( 'end_time', $time );
298 - } else {
299 - $this->_set_data( 'end_time', '' );
300 - }
226 + public function set_end_time( $time = '' ): LP_User_Item {
227 + $this->set_data_date( 'end_time', $time );
301 228
302 229 return $this;
303 230 }
304 231
@@ -308,18 +235,14 @@
308 235 * @param string $format
309 236 *
310 237 * @return string|LP_Datetime
311 238 */
312 - public function get_end_time( $format = '' ) {
313 - $date = $this->get_data( 'end_time' );
239 + public function get_end_time( string $format = '' ) {
240 + $date = $this->get_data( self::KEY_DATA_END_TIME );
314 241
315 242 return $this->format_time( $date, $format );
316 243 }
317 244
318 - public function get_start_time_local() {
319 -
320 - }
321 -
322 245 /**
323 246 * @param string|int|LP_Datetime $date
324 247 * @param string $format
325 248 * @param bool $local
@@ -338,51 +261,31 @@
338 261 return $format ? $date->format( $format, $local ) : $date;
339 262 }
340 263
341 264 /**
342 - * Get end-time.
343 - *
344 - * @param mixed $time
345 - *
346 - * @deprecated 4.0.0
347 - */
348 - public function set_end_time_gmt( $time ) {
349 - _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.0.0' );
350 - }
351 -
352 - /**
353 - * Get end-time.
354 - *
355 - * @param string $format
356 - *
357 - * @return string|LP_Datetime
358 - * @deprecated
359 - */
360 - public function get_end_time_gmt( $format = '' ) {
361 - _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.0.0' );
362 - }
363 -
364 - /**
365 265 * Get expiration time.
366 266 *
367 - * @param string $format
368 - *
369 267 * @return string|LP_Datetime $time
370 268 * @since 3.3.0
371 - * @version 3.3.1
269 + * @version 3.3.2
372 270 */
373 - public function get_expiration_time( string $format = '' ) {
271 + public function get_expiration_time() {
374 272 $duration = get_post_meta( $this->get_item_id(), '_lp_duration', true );
375 - $start_time = $this->get_start_time( '', false );
273 + $start_time = $this->get_start_time()->getTimestamp();
376 274
377 275 if ( ! absint( $duration ) || ! $start_time ) {
378 276 $expire = null;
379 277 } else {
380 - $date = new LP_Datetime( $start_time->getPeriod( $duration, true ) );
381 - $expire = $this->format_time( $date, $format );
278 + // Convert duration from string to seconds.
279 + if ( ! is_numeric( $duration ) ) {
280 + $duration = strtotime( $duration ) - time();
281 + }
282 +
283 + $expire_time = $start_time + $duration;
284 + $expire = new LP_Datetime( $expire_time );
382 285 }
383 286
384 - return apply_filters( 'learn-press/user-item/expiration-time', $expire, $format, $this );
287 + return apply_filters( 'learn-press/user-item/expiration-time', $expire, $duration, $this );
385 288 }
386 289
387 290 /**
388 291 * Set item-status.
@@ -416,8 +319,16 @@
416 319 if ( (int) $this->get_user_id() === 0 ) {
417 320 return $got_status;
418 321 }
419 322
323 + if ( LP_COURSE_CPT === $this->get_type() ) {
324 + return $this->get_data( $field );
325 + }
326 +
327 + /*error_log('status user item id: ' . $this->get_id());
328 + error_log('status user item type: ' . $this->get_type());
329 + error_log('status user course status: ' . $this->get_data( $field ));*/
330 +
420 331 $lp_user_item = LP_User_Items_DB::getInstance();
421 332 $filter = new LP_User_Items_Filter();
422 333
423 334 $filter->user_id = $this->get_user_id();
@@ -432,9 +343,9 @@
432 343 }
433 344
434 345 $this->set_data( $field, $got_status );
435 346 } catch ( Throwable $e ) {
436 - LP_Debug::error_log( __FUNCTION__ . ':' . $e->getMessage() );
347 + LP_Debug::error_log( $e );
437 348 }
438 349
439 350 return $got_status;
440 351 }
@@ -670,14 +581,8 @@
670 581 */
671 582 $columns = array();
672 583
673 584 foreach ( $this->get_data() as $k => $v ) {
674 - switch ( $k ) {
675 - case 'start_time':
676 - case 'end_time':
677 - $v = is_a( $v, 'LP_Datetime' ) ? $v->toSql( false ) : $v;
678 - break;
679 - }
680 585 $columns[ $k ] = $v;
681 586 }
682 587
683 588 return $columns;
@@ -751,18 +656,11 @@
751 656 *
752 657 * @updated 3.1.0
753 658 *
754 659 * @return bool|mixed
755 - * @throws Exception
756 660 */
757 661 public function update( $force = false, $wp_error = false ) {
758 -
759 - $data = $this->get_mysql_data();
760 -
761 - /**
762 - * @since 3.3.0
763 - * Allow filter to modify data
764 - */
662 + $data = $this->get_mysql_data();
765 663 $data = apply_filters( 'learn-press/update-user-item-data', $data, $this->get_user_item_id() );
766 664 $where = array();
767 665
768 666 if ( $this->get_user_item_id() ) {
@@ -768,38 +666,23 @@
768 666 if ( $this->get_user_item_id() ) {
769 667 $where = array( 'user_item_id' => $this->get_user_item_id() );
770 668 }
771 669
772 - $return = learn_press_update_user_item_field( $data, $where );
670 + $rs = learn_press_update_user_item_field( $data, $where );
773 671
774 672 // Clear cache first status
775 673 $this->get_status( 'status', true );
776 674
777 - if ( $return ) {
778 - foreach ( (array) $return as $k => $v ) {
675 + if ( $rs ) {
676 + foreach ( (array) $rs as $k => $v ) {
779 677 $this->_set_data( $k, $v );
780 678 }
781 679 $this->_changes = array();
782 680 }
783 681
784 - /*$data_course = $this->get_parent();
785 - if ( $data_course ) {
786 - $data_course->calculate_course_results();
787 - }*/
788 -
789 - return $return;
682 + return $rs;
790 683 }
791 684
792 - /**
793 - * @editor tungnx
794 - * @reason commnet - not use
795 - * @modify 4.1.2
796 - */
797 - /*
798 - public function is_course_item() {
799 - return learn_press_is_support_course_item_type( $this->get_data( 'item_type' ) );
800 - }*/
801 -
802 685 public function get_status_label( $status = '' ) {
803 686 $statuses = array(
804 687 LP_COURSE_ENROLLED => esc_html__( 'Enrolled', 'learnpress' ),
805 688 LP_COURSE_PURCHASED => esc_html__( 'Purchased', 'learnpress' ),
@@ -819,9 +702,9 @@
819 702 * Get time from user started to ended.
820 703 *
821 704 * @param string $context
822 705 *
823 - * @return bool|float|int
706 + * @return bool|int
824 707 */
825 708 public function get_time_interval( $context = '' ) {
826 709 $start = $this->get_start_time();
827 710 $end = $this->get_end_time();
@@ -832,124 +715,13 @@
832 715
833 716 if ( $start->is_null() || $end->is_null() ) {
834 717 return false;
835 718 }
836 - $interval = $end->getTimestamp() - $start->getTimestamp();
837 719
838 - return $interval;
720 + return $end->getTimestamp() - $start->getTimestamp();
839 721 }
840 722
841 - /*public function get_history() {
842 - return LP_Object_Cache::get(
843 - sprintf(
844 - 'course-item-%s-%s-%s',
845 - $this->get_user_id(),
846 - $this->get_course( 'id' ),
847 - $this->get_id()
848 - ),
849 - 'learn-press/user-course-items'
850 - );
851 - }*/
852 -
853 723 /**
854 - * @editor tungnx
855 - * @modify 4.1.2
856 - * @reason comment - not use
857 - */
858 - /*
859 - public function count_history() {
860 - if ( $items = $this->get_history() ) {
861 - return sizeof( $items );
862 - }
863 -
864 - return 0;
865 - }*/
866 -
867 - /**
868 - * @editor tungnx
869 - * @modify 4.1.2
870 - * @reason comment - not use
871 - */
872 - /*
873 - public function remove_user_items_history( $keep = 10 ) {
874 - learn_press_remove_user_items_history(
875 - $this->get_item_id(),
876 - $this->get_course( 'id' ),
877 - $this->get_user_id(),
878 - $keep
879 - );
880 - }*/
881 -
882 - /**
883 - * Return number of seconds has exceeded from the expiration time to now.
884 - * If less than or equals to 0 that means the time is exceeded.
885 - * Otherwise, the time is not exceeded.
886 - *
887 - * @return float|int
888 - * @since 3.3.0
889 - */
890 - public function get_exceeded() {
891 - $time = new LP_Datetime();
892 - $current = $time->getTimestamp( false );
893 - $exceeded = $this->get_expiration_time();
894 -
895 - return false !== $exceeded ? $exceeded->getTimestamp() - $current : false;
896 - }
897 -
898 - /**
899 - * Check if user was finished before the expiration time is exceeded.
900 - * If the expiration-time is NULL that mean the course is not set duration.
901 - *
902 - * @return bool|float|int
903 - * @since 3.3.0
904 - * Todo: check remove function
905 - */
906 - public function is_exceeded() {
907 - $expiration = $this->get_expiration_time();
908 - $end = $this->get_end_time();
909 -
910 - if ( ! $expiration ) {
911 - return false;
912 - }
913 -
914 - // If course is not finished then consider end time is current time
915 - if ( ! $end || 0 >= $end->getTimestamp() ) {
916 - $end = new LP_Datetime();
917 - $end = $end->getTimestamp( false );
918 - } else {
919 - $end = $end->getTimestamp();
920 - }
921 -
922 - return $expiration->getTimestamp() - $end;
923 - }
924 -
925 - /**
926 - * Get time remaining for user item.
927 - *
928 - * @param string $return - Optional. What kind of data to return.
929 - *
930 - * @return LP_Duration
931 - * @since 3.3.0
932 - */
933 - public function get_time_remaining( $return = 'object' ) {
934 - $is_exceeded = $this->is_exceeded();
935 - $time = false;
936 -
937 - if ( false !== $is_exceeded ) {
938 - $time = 0 < $is_exceeded ? absint( $is_exceeded ) : 0;
939 - }
940 -
941 - // return apply_filters( 'learn-press/quiz/time-remaining', $remaining, $this->get_item_id(), $this->get_course_id() );
942 - return apply_filters(
943 - 'learn-press/user-item-time-remaining',
944 - $return === 'object' ? new LP_Duration( $time ) : $time,
945 - $this->get_item_id(),
946 - $this->get_parent_id(),
947 - $this->get_user_id()
948 - );
949 - }
950 -
951 - /**
952 724 * Return true of item is completed/finished
953 725 *
954 726 * @param string $status
955 727 *
@@ -966,37 +738,12 @@
966 738 * @modify 4.1.4.1 - should review to improve
967 739 * @version 4.0.1
968 740 */
969 741 public function complete( $status = 'completed' ) {
970 - global $wpdb;
971 -
972 - // $end_time = new LP_Datetime();
973 - // $null_time = null;
974 -
975 742 try {
976 - // if ( ! $this->get_end_time() ) {
977 - $this->set_end_time( current_time( 'mysql', 1 ) );
978 - // }
979 -
743 + $this->set_end_time( time() );
980 744 $this->set_status( $status );
981 745 $this->update();
982 -
983 - /*
984 - $query = $wpdb->prepare(
985 - "SELECT user_item_id
986 - FROM {$wpdb->prefix}learnpress_user_items
987 - WHERE user_id = %d
988 - AND item_id = %d
989 - AND status = %s
990 - GROUP BY user_item_id DESC
991 - LIMIT 1
992 - ",
993 - $this->get_user_id(),
994 - $this->get_item_id(),
995 - $status
996 - );
997 -
998 - return $wpdb->get_var( $query );*/
999 746 } catch ( Throwable $e ) {
1000 747 error_log( __FUNCTION__ . ':' . $e->getMessage() );
1001 748 return false;
1002 749 }
@@ -1004,45 +751,8 @@
1004 751 return true;
1005 752 }
1006 753
1007 754 /**
1008 - * @editor tungnx
1009 - * @modify 4.1.2
1010 - * @reason comment - not use
1011 - */
1012 - /*
1013 - public function delete_meta_data( $include = '', $exclude = '' ) {
1014 - global $wpdb;
1015 -
1016 - $where = '';
1017 - if ( $include ) {
1018 - settype( $include, 'array' );
1019 - $format = array_fill( 0, sizeof( $include ), '%s' );
1020 - $where .= $wpdb->prepare( ' AND meta_key IN(' . join( ',', $format ) . ')', $include );
1021 - }
1022 -
1023 - if ( $exclude ) {
1024 - settype( $exclude, 'array' );
1025 - $format = array_fill( 0, sizeof( $exclude ), '%s' );
1026 - $where .= $wpdb->prepare( ' AND meta_key IN(' . join( ',', $format ) . ')', $exclude );
1027 - }
1028 -
1029 - $query = $wpdb->prepare(
1030 - "
1031 - DELETE FROM {$wpdb->learnpress_user_itemmeta}
1032 - WHERE learnpress_user_item_id = %d
1033 - {$where}
1034 - ",
1035 - $this->get_user_item_id()
1036 - );
1037 -
1038 - $wpdb->query( $query );
1039 -
1040 - $this->_meta_data = array();
1041 - update_meta_cache( 'learnpress_user_item', $this->get_user_item_id() );
1042 - }*/
1043 -
1044 - /**
1045 755 * Get post type of item.
1046 756 *
1047 757 * @return string
1048 758 */
@@ -1052,93 +762,33 @@
1052 762
1053 763 /**
1054 764 * @return bool
1055 765 */
1056 - public function is_passed() {
1057 - return 'passed' === $this->get_graduation();
766 + public function is_passed(): bool {
767 + return LP_COURSE_GRADUATION_PASSED === $this->get_graduation();
1058 768 }
1059 769
770 + /**
771 + * @param int $decimal
772 + *
773 + * @return mixed|null
774 + */
1060 775 public function get_percent_result( $decimal = 1 ) {
1061 776 return apply_filters(
1062 777 'learn-press/user/item-percent-result',
1063 - sprintf(
1064 - '%s%%',
1065 - round( $this->get_result( 'result' ), $decimal ),
1066 - $this->get_user_id(),
1067 - $this->get_item_id()
1068 - )
778 + sprintf( '%s%', round( $this->get_result( 'result' ), $decimal ) ),
779 + $this->get_user_id(),
780 + $this->get_item_id()
1069 781 );
1070 782 }
1071 783
1072 - /**
1073 - * Calculate expiration time from the start time and duration.
1074 - *
1075 - * @param int|string|LP_Datetime $duration
1076 - *
1077 - * @return LP_Datetime
1078 - * @since 3.3.0
1079 - * @depecated 4.1.7
1080 - */
1081 - /*public function set_duration( $duration ) {
1082 - if ( $duration instanceof LP_Datetime ) {
1083 - $period = $duration->toSql();
1084 - } else {
1085 - $period = $duration;
1086 - }
1087 -
1088 - return $this->get_expiration_time();
1089 - }*/
1090 -
1091 - /**
1092 - * @depecated 4.1.7
1093 - */
1094 - /*public function is_change() {
1095 -
1096 - $new_data = $this->get_mysql_data();
1097 - ksort( $new_data );
1098 -
1099 - return $this->_data_key !== md5( serialize( $new_data ) );
1100 - }*/
1101 -
1102 - protected function _set_data_date( $key, $value, $extra = false ) {
1103 - if ( $value instanceof LP_Datetime ) {
1104 - $value = $value->getTimestamp();
1105 - } else {
1106 - $value = is_numeric( $value ) ? $value : strtotime( $value );
1107 - }
1108 - $offset = (int) ( get_option( 'gmt_offset', 0 ) * HOUR_IN_SECONDS );
1109 -
1110 - parent::_set_data_date( $key, $value + $offset, $extra );
1111 - }
1112 -
1113 - /**
1114 - * @param $name
1115 - * @param $arguments
1116 - *
1117 - * @return mixed
1118 - * @depecated 4.1.7
1119 - */
1120 - /*public function __call( $name, $arguments ) {
1121 - if ( ! method_exists( $this, $name ) ) {
1122 - $course = $this->get_course();
1123 -
1124 - if ( $course ) {
1125 - $item = $course->get_item( $this->get_item_id() );
1126 -
1127 - if ( is_callable( array( $item, $name ) ) ) {
1128 - return call_user_func_array( array( $item, $name ), $arguments );
1129 - }
1130 - }
1131 - }
1132 -
1133 - return false;
1134 - }*/
1135 -
1136 784 public function offsetSet( $offset, $value ) {
785 + _deprecated_function( __METHOD__, '4.2.3.5' );
1137 786 // TODO: Implement offsetSet() method.
1138 787 }
1139 788
1140 789 public function offsetGet( $offset ) {
790 + _deprecated_function( __METHOD__, '4.2.3.5' );
1141 791 if ( is_callable( array( $this, 'get_' . $offset ) ) ) {
1142 792 return call_user_func( array( $this, 'get_' . $offset ) );
1143 793 }
1144 794
@@ -1145,12 +795,14 @@
1145 795 return false;
1146 796 }
1147 797
1148 798 public function offsetUnset( $offset ) {
799 + _deprecated_function( __METHOD__, '4.2.3.5' );
1149 800 // TODO: Implement offsetUnset() method.
1150 801 }
1151 802
1152 803 public function offsetExists( $offset ) {
804 + _deprecated_function( __METHOD__, '4.2.3.5' );
1153 805 // TODO: Implement offsetExists() method.
1154 806 }
1155 807 }
1156 808