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 -328 4.1.7.2 → 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,33 +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 - return $return;
682 + return $rs;
785 683 }
786 684
787 - /**
788 - * @editor tungnx
789 - * @reason commnet - not use
790 - * @modify 4.1.2
791 - */
792 - /*
793 - public function is_course_item() {
794 - return learn_press_is_support_course_item_type( $this->get_data( 'item_type' ) );
795 - }*/
796 -
797 685 public function get_status_label( $status = '' ) {
798 686 $statuses = array(
799 687 LP_COURSE_ENROLLED => esc_html__( 'Enrolled', 'learnpress' ),
800 688 LP_COURSE_PURCHASED => esc_html__( 'Purchased', 'learnpress' ),
@@ -814,9 +702,9 @@
814 702 * Get time from user started to ended.
815 703 *
816 704 * @param string $context
817 705 *
818 - * @return bool|float|int
706 + * @return bool|int
819 707 */
820 708 public function get_time_interval( $context = '' ) {
821 709 $start = $this->get_start_time();
822 710 $end = $this->get_end_time();
@@ -827,83 +715,13 @@
827 715
828 716 if ( $start->is_null() || $end->is_null() ) {
829 717 return false;
830 718 }
831 - $interval = $end->getTimestamp() - $start->getTimestamp();
832 719
833 - return $interval;
720 + return $end->getTimestamp() - $start->getTimestamp();
834 721 }
835 722
836 723 /**
837 - * Return number of seconds has exceeded from the expiration time to now.
838 - * If less than or equals to 0 that means the time is exceeded.
839 - * Otherwise, the time is not exceeded.
840 - *
841 - * @return float|int
842 - * @since 3.3.0
843 - */
844 - public function get_exceeded() {
845 - $time = new LP_Datetime();
846 - $current = $time->getTimestamp( false );
847 - $exceeded = $this->get_expiration_time();
848 -
849 - return false !== $exceeded ? $exceeded->getTimestamp() - $current : false;
850 - }
851 -
852 - /**
853 - * Check if user was finished before the expiration time is exceeded.
854 - * If the expiration-time is NULL that mean the course is not set duration.
855 - *
856 - * @return bool|float|int
857 - * @since 3.3.0
858 - * Todo: check remove function
859 - */
860 - public function is_exceeded() {
861 - $expiration = $this->get_expiration_time();
862 - $end = $this->get_end_time();
863 -
864 - if ( ! $expiration ) {
865 - return false;
866 - }
867 -
868 - // If course is not finished then consider end time is current time
869 - if ( ! $end || 0 >= $end->getTimestamp() ) {
870 - $end = new LP_Datetime();
871 - $end = $end->getTimestamp( false );
872 - } else {
873 - $end = $end->getTimestamp();
874 - }
875 -
876 - return $expiration->getTimestamp() - $end;
877 - }
878 -
879 - /**
880 - * Get time remaining for user item.
881 - *
882 - * @param string $return - Optional. What kind of data to return.
883 - *
884 - * @return LP_Duration
885 - * @since 3.3.0
886 - */
887 - public function get_time_remaining( $return = 'object' ) {
888 - $is_exceeded = $this->is_exceeded();
889 - $time = false;
890 -
891 - if ( false !== $is_exceeded ) {
892 - $time = 0 < $is_exceeded ? absint( $is_exceeded ) : 0;
893 - }
894 -
895 - // return apply_filters( 'learn-press/quiz/time-remaining', $remaining, $this->get_item_id(), $this->get_course_id() );
896 - return apply_filters(
897 - 'learn-press/user-item-time-remaining',
898 - $return === 'object' ? new LP_Duration( $time ) : $time,
899 - $this->get_item_id(),
900 - $this->get_parent_id(),
901 - $this->get_user_id()
902 - );
903 - }
904 -
905 - /**
906 724 * Return true of item is completed/finished
907 725 *
908 726 * @param string $status
909 727 *
@@ -920,37 +738,12 @@
920 738 * @modify 4.1.4.1 - should review to improve
921 739 * @version 4.0.1
922 740 */
923 741 public function complete( $status = 'completed' ) {
924 - global $wpdb;
925 -
926 - // $end_time = new LP_Datetime();
927 - // $null_time = null;
928 -
929 742 try {
930 - // if ( ! $this->get_end_time() ) {
931 - $this->set_end_time( current_time( 'mysql', 1 ) );
932 - // }
933 -
743 + $this->set_end_time( time() );
934 744 $this->set_status( $status );
935 745 $this->update();
936 -
937 - /*
938 - $query = $wpdb->prepare(
939 - "SELECT user_item_id
940 - FROM {$wpdb->prefix}learnpress_user_items
941 - WHERE user_id = %d
942 - AND item_id = %d
943 - AND status = %s
944 - GROUP BY user_item_id DESC
945 - LIMIT 1
946 - ",
947 - $this->get_user_id(),
948 - $this->get_item_id(),
949 - $status
950 - );
951 -
952 - return $wpdb->get_var( $query );*/
953 746 } catch ( Throwable $e ) {
954 747 error_log( __FUNCTION__ . ':' . $e->getMessage() );
955 748 return false;
956 749 }
@@ -969,93 +762,33 @@
969 762
970 763 /**
971 764 * @return bool
972 765 */
973 - public function is_passed() {
974 - return 'passed' === $this->get_graduation();
766 + public function is_passed(): bool {
767 + return LP_COURSE_GRADUATION_PASSED === $this->get_graduation();
975 768 }
976 769
770 + /**
771 + * @param int $decimal
772 + *
773 + * @return mixed|null
774 + */
977 775 public function get_percent_result( $decimal = 1 ) {
978 776 return apply_filters(
979 777 'learn-press/user/item-percent-result',
980 - sprintf(
981 - '%s%%',
982 - round( $this->get_result( 'result' ), $decimal ),
983 - $this->get_user_id(),
984 - $this->get_item_id()
985 - )
778 + sprintf( '%s%', round( $this->get_result( 'result' ), $decimal ) ),
779 + $this->get_user_id(),
780 + $this->get_item_id()
986 781 );
987 782 }
988 783
989 - /**
990 - * Calculate expiration time from the start time and duration.
991 - *
992 - * @param int|string|LP_Datetime $duration
993 - *
994 - * @return LP_Datetime
995 - * @since 3.3.0
996 - * @depecated 4.1.7
997 - */
998 - /*public function set_duration( $duration ) {
999 - if ( $duration instanceof LP_Datetime ) {
1000 - $period = $duration->toSql();
1001 - } else {
1002 - $period = $duration;
1003 - }
1004 -
1005 - return $this->get_expiration_time();
1006 - }*/
1007 -
1008 - /**
1009 - * @depecated 4.1.7
1010 - */
1011 - /*public function is_change() {
1012 -
1013 - $new_data = $this->get_mysql_data();
1014 - ksort( $new_data );
1015 -
1016 - return $this->_data_key !== md5( serialize( $new_data ) );
1017 - }*/
1018 -
1019 - protected function _set_data_date( $key, $value, $extra = false ) {
1020 - if ( $value instanceof LP_Datetime ) {
1021 - $value = $value->getTimestamp();
1022 - } else {
1023 - $value = is_numeric( $value ) ? $value : strtotime( $value );
1024 - }
1025 - $offset = (int) ( get_option( 'gmt_offset', 0 ) * HOUR_IN_SECONDS );
1026 -
1027 - parent::_set_data_date( $key, $value + $offset, $extra );
1028 - }
1029 -
1030 - /**
1031 - * @param $name
1032 - * @param $arguments
1033 - *
1034 - * @return mixed
1035 - * @depecated 4.1.7
1036 - */
1037 - /*public function __call( $name, $arguments ) {
1038 - if ( ! method_exists( $this, $name ) ) {
1039 - $course = $this->get_course();
1040 -
1041 - if ( $course ) {
1042 - $item = $course->get_item( $this->get_item_id() );
1043 -
1044 - if ( is_callable( array( $item, $name ) ) ) {
1045 - return call_user_func_array( array( $item, $name ), $arguments );
1046 - }
1047 - }
1048 - }
1049 -
1050 - return false;
1051 - }*/
1052 -
1053 784 public function offsetSet( $offset, $value ) {
785 + _deprecated_function( __METHOD__, '4.2.3.5' );
1054 786 // TODO: Implement offsetSet() method.
1055 787 }
1056 788
1057 789 public function offsetGet( $offset ) {
790 + _deprecated_function( __METHOD__, '4.2.3.5' );
1058 791 if ( is_callable( array( $this, 'get_' . $offset ) ) ) {
1059 792 return call_user_func( array( $this, 'get_' . $offset ) );
1060 793 }
1061 794
@@ -1062,12 +795,14 @@
1062 795 return false;
1063 796 }
1064 797
1065 798 public function offsetUnset( $offset ) {
799 + _deprecated_function( __METHOD__, '4.2.3.5' );
1066 800 // TODO: Implement offsetUnset() method.
1067 801 }
1068 802
1069 803 public function offsetExists( $offset ) {
804 + _deprecated_function( __METHOD__, '4.2.3.5' );
1070 805 // TODO: Implement offsetExists() method.
1071 806 }
1072 807 }
1073 808