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 +45 -250 4.1.7.3 → 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 71 /**
73 - * @var string
74 - * @deprecated 4.1.7.3
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,10 +117,10 @@
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 - $this->set_start_time( $item['start_time'] ?? time() );
120 - $this->set_end_time( $item['end_time'] ?? '' );
121 + $this->set_start_time( $item[ self::KEY_DATA_START_TIME ] ?? time() );
122 + $this->set_end_time( $item[ self::KEY_DATA_END_TIME ] ?? '' );
121 123 $this->set_user_id( absint( $item['user_id'] ?? get_current_user_id() ) );
122 124 $this->set_status( $item['status'] ?? '' );
123 125
124 126 if ( ! empty( $item['ref_id'] ) ) {
@@ -143,13 +145,9 @@
143 145 if ( ! empty( $item['access_level'] ) ) {
144 146 $this->set_data( 'access_level', $item['access_level'] );
145 147 }
146 148
147 - $this->set_data( 'graduation', $item['graduation'] ?? LP_COURSE_GRADUATION_IN_PROGRESS );
148 -
149 - // $new_data = $this->get_mysql_data();
150 - // ksort( $new_data );
151 - // $this->_data_key = md5( serialize( $new_data ) );
149 + $this->set_data( 'graduation', $item['graduation'] ?? '' );
152 150 }
153 151
154 152 public function set_user_id( $user_id ) {
155 153 $this->set_data( 'user_id', $user_id );
@@ -197,33 +195,14 @@
197 195 * @param mixed $time .
198 196 *
199 197 * @return $this
200 198 */
201 - public function set_start_time( $time ): LP_User_Item {
202 - $lp_time = new LP_Datetime( $time );
203 - $this->set_data( 'start_time', $lp_time );
199 + public function set_start_time( $time = '' ): LP_User_Item {
200 + $this->set_data_date( 'start_time', $time );
204 201
205 202 return $this;
206 203 }
207 204
208 - public function get_time( $field, $format = '', $human_diff_time = false ) {
209 - if ( ! $format ) {
210 - $format = get_option( 'date_format' );
211 - }
212 -
213 - $m_time = call_user_func( array( $this, 'get_' . $field ) );
214 - $time = mysql2date( 'G', call_user_func( array( $this, 'get_' . $field . '_gmt' ) ) );
215 - $time_diff = time() - $time;
216 -
217 - if ( $human_diff_time && $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
218 - $h_time = sprintf( __( '%s ago', 'learnpress' ), human_time_diff( $time ) );
219 - } else {
220 - $h_time = mysql2date( $format, $m_time );
221 - }
222 -
223 - return $h_time;
224 - }
225 -
226 205 /**
227 206 * Get start-time.
228 207 *
229 208 * @param string $format
@@ -228,35 +207,17 @@
228 207 *
229 208 * @param string $format
230 209 * @param bool $local
231 210 *
232 - * @return string|LP_Datetime
211 + * @return string|LP_Datetime|false
233 212 */
234 - public function get_start_time( $format = '', $local = false ) {
235 - $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 );
236 215
237 216 return $this->format_time( $date, $format, $local );
238 217 }
239 218
240 219 /**
241 - * @param string $format
242 - *
243 - * @return array|bool|LP_Datetime|mixed|string
244 - * @deprecated
245 - * Addon certificate v4.0.3 is using.
246 - */
247 - public function get_start_time_gmt( $format = '' ) {
248 - _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.0.0' );
249 -
250 - // $date = $this->get_data_date( 'start_time_gmt' );
251 - // if ( $format ) {
252 - // return $date->is_null() ? false : ( $format = 'i18n' ? learn_press_date_i18n( $date->getTimestamp() ) : $date->format( $format ) );
253 - // }
254 - //
255 - // return $date;
256 - }
257 -
258 - /**
259 220 * Set end-time for item.
260 221 *
261 222 * @param mixed $time
262 223 *
@@ -274,18 +235,14 @@
274 235 * @param string $format
275 236 *
276 237 * @return string|LP_Datetime
277 238 */
278 - public function get_end_time( $format = '' ) {
279 - $date = $this->get_data( 'end_time' );
239 + public function get_end_time( string $format = '' ) {
240 + $date = $this->get_data( self::KEY_DATA_END_TIME );
280 241
281 242 return $this->format_time( $date, $format );
282 243 }
283 244
284 - public function get_start_time_local() {
285 -
286 - }
287 -
288 245 /**
289 246 * @param string|int|LP_Datetime $date
290 247 * @param string $format
291 248 * @param bool $local
@@ -304,51 +261,31 @@
304 261 return $format ? $date->format( $format, $local ) : $date;
305 262 }
306 263
307 264 /**
308 - * Get end-time.
309 - *
310 - * @param mixed $time
311 - *
312 - * @deprecated 4.0.0
313 - */
314 - public function set_end_time_gmt( $time ) {
315 - _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.0.0' );
316 - }
317 -
318 - /**
319 - * Get end-time.
320 - *
321 - * @param string $format
322 - *
323 - * @return string|LP_Datetime
324 - * @deprecated
325 - */
326 - public function get_end_time_gmt( $format = '' ) {
327 - _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.0.0' );
328 - }
329 -
330 - /**
331 265 * Get expiration time.
332 266 *
333 - * @param string $format
334 - *
335 267 * @return string|LP_Datetime $time
336 268 * @since 3.3.0
337 - * @version 3.3.1
269 + * @version 3.3.2
338 270 */
339 - public function get_expiration_time( string $format = '' ) {
271 + public function get_expiration_time() {
340 272 $duration = get_post_meta( $this->get_item_id(), '_lp_duration', true );
341 - $start_time = $this->get_start_time( '', false );
273 + $start_time = $this->get_start_time()->getTimestamp();
342 274
343 275 if ( ! absint( $duration ) || ! $start_time ) {
344 276 $expire = null;
345 277 } else {
346 - $date = new LP_Datetime( $start_time->getPeriod( $duration, true ) );
347 - $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 );
348 285 }
349 286
350 - 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 );
351 288 }
352 289
353 290 /**
354 291 * Set item-status.
@@ -382,8 +319,16 @@
382 319 if ( (int) $this->get_user_id() === 0 ) {
383 320 return $got_status;
384 321 }
385 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 +
386 331 $lp_user_item = LP_User_Items_DB::getInstance();
387 332 $filter = new LP_User_Items_Filter();
388 333
389 334 $filter->user_id = $this->get_user_id();
@@ -398,9 +343,9 @@
398 343 }
399 344
400 345 $this->set_data( $field, $got_status );
401 346 } catch ( Throwable $e ) {
402 - LP_Debug::error_log( __FUNCTION__ . ':' . $e->getMessage() );
347 + LP_Debug::error_log( $e );
403 348 }
404 349
405 350 return $got_status;
406 351 }
@@ -636,14 +581,8 @@
636 581 */
637 582 $columns = array();
638 583
639 584 foreach ( $this->get_data() as $k => $v ) {
640 - switch ( $k ) {
641 - case 'start_time':
642 - case 'end_time':
643 - //$v = is_a( $v, 'LP_Datetime' ) ? $v->toSql( false ) : $v;
644 - break;
645 - }
646 585 $columns[ $k ] = $v;
647 586 }
648 587
649 588 return $columns;
@@ -742,18 +681,8 @@
742 681
743 682 return $rs;
744 683 }
745 684
746 - /**
747 - * @editor tungnx
748 - * @reason commnet - not use
749 - * @modify 4.1.2
750 - */
751 - /*
752 - public function is_course_item() {
753 - return learn_press_is_support_course_item_type( $this->get_data( 'item_type' ) );
754 - }*/
755 -
756 685 public function get_status_label( $status = '' ) {
757 686 $statuses = array(
758 687 LP_COURSE_ENROLLED => esc_html__( 'Enrolled', 'learnpress' ),
759 688 LP_COURSE_PURCHASED => esc_html__( 'Purchased', 'learnpress' ),
@@ -773,9 +702,9 @@
773 702 * Get time from user started to ended.
774 703 *
775 704 * @param string $context
776 705 *
777 - * @return bool|float|int
706 + * @return bool|int
778 707 */
779 708 public function get_time_interval( $context = '' ) {
780 709 $start = $this->get_start_time();
781 710 $end = $this->get_end_time();
@@ -786,83 +715,13 @@
786 715
787 716 if ( $start->is_null() || $end->is_null() ) {
788 717 return false;
789 718 }
790 - $interval = $end->getTimestamp() - $start->getTimestamp();
791 719
792 - return $interval;
720 + return $end->getTimestamp() - $start->getTimestamp();
793 721 }
794 722
795 723 /**
796 - * Return number of seconds has exceeded from the expiration time to now.
797 - * If less than or equals to 0 that means the time is exceeded.
798 - * Otherwise, the time is not exceeded.
799 - *
800 - * @return float|int
801 - * @since 3.3.0
802 - */
803 - public function get_exceeded() {
804 - $time = new LP_Datetime();
805 - $current = $time->getTimestamp( false );
806 - $exceeded = $this->get_expiration_time();
807 -
808 - return false !== $exceeded ? $exceeded->getTimestamp() - $current : false;
809 - }
810 -
811 - /**
812 - * Check if user was finished before the expiration time is exceeded.
813 - * If the expiration-time is NULL that mean the course is not set duration.
814 - *
815 - * @return bool|float|int
816 - * @since 3.3.0
817 - * Todo: check remove function
818 - */
819 - public function is_exceeded() {
820 - $expiration = $this->get_expiration_time();
821 - $end = $this->get_end_time();
822 -
823 - if ( ! $expiration ) {
824 - return false;
825 - }
826 -
827 - // If course is not finished then consider end time is current time
828 - if ( ! $end || 0 >= $end->getTimestamp() ) {
829 - $end = new LP_Datetime();
830 - $end = $end->getTimestamp( false );
831 - } else {
832 - $end = $end->getTimestamp();
833 - }
834 -
835 - return $expiration->getTimestamp() - $end;
836 - }
837 -
838 - /**
839 - * Get time remaining for user item.
840 - *
841 - * @param string $return - Optional. What kind of data to return.
842 - *
843 - * @return LP_Duration
844 - * @since 3.3.0
845 - */
846 - public function get_time_remaining( $return = 'object' ) {
847 - $is_exceeded = $this->is_exceeded();
848 - $time = false;
849 -
850 - if ( false !== $is_exceeded ) {
851 - $time = 0 < $is_exceeded ? absint( $is_exceeded ) : 0;
852 - }
853 -
854 - // return apply_filters( 'learn-press/quiz/time-remaining', $remaining, $this->get_item_id(), $this->get_course_id() );
855 - return apply_filters(
856 - 'learn-press/user-item-time-remaining',
857 - $return === 'object' ? new LP_Duration( $time ) : $time,
858 - $this->get_item_id(),
859 - $this->get_parent_id(),
860 - $this->get_user_id()
861 - );
862 - }
863 -
864 - /**
865 724 * Return true of item is completed/finished
866 725 *
867 726 * @param string $status
868 727 *
@@ -903,10 +762,10 @@
903 762
904 763 /**
905 764 * @return bool
906 765 */
907 - public function is_passed() {
908 - return 'passed' === $this->get_graduation();
766 + public function is_passed(): bool {
767 + return LP_COURSE_GRADUATION_PASSED === $this->get_graduation();
909 768 }
910 769
911 770 /**
912 771 * @param int $decimal
@@ -921,81 +780,15 @@
921 780 $this->get_item_id()
922 781 );
923 782 }
924 783
925 - /**
926 - * Calculate expiration time from the start time and duration.
927 - *
928 - * @param int|string|LP_Datetime $duration
929 - *
930 - * @return LP_Datetime
931 - * @since 3.3.0
932 - * @deprecated 4.1.7
933 - */
934 - /*public function set_duration( $duration ) {
935 - if ( $duration instanceof LP_Datetime ) {
936 - $period = $duration->toSql();
937 - } else {
938 - $period = $duration;
939 - }
940 -
941 - return $this->get_expiration_time();
942 - }*/
943 -
944 - /**
945 - * @deprecated 4.1.7
946 - */
947 - /*public function is_change() {
948 -
949 - $new_data = $this->get_mysql_data();
950 - ksort( $new_data );
951 -
952 - return $this->_data_key !== md5( serialize( $new_data ) );
953 - }*/
954 -
955 - /**
956 - * @deprecated 4.1.7.3
957 - */
958 - protected function _set_data_date( $key, $value, $extra = false ) {
959 - _deprecated_function( __FUNCTION__, '4.1.7.3', 'set_data_date' );
960 - if ( $value instanceof LP_Datetime ) {
961 - $value = $value->getTimestamp();
962 - } else {
963 - $value = is_numeric( $value ) ? $value : strtotime( $value );
964 - }
965 - $offset = (int) ( get_option( 'gmt_offset', 0 ) * HOUR_IN_SECONDS );
966 -
967 - parent::_set_data_date( $key, $value + $offset, $extra );
968 - }
969 -
970 - /**
971 - * @param $name
972 - * @param $arguments
973 - *
974 - * @return mixed
975 - * @deprecated 4.1.7
976 - */
977 - /*public function __call( $name, $arguments ) {
978 - if ( ! method_exists( $this, $name ) ) {
979 - $course = $this->get_course();
980 -
981 - if ( $course ) {
982 - $item = $course->get_item( $this->get_item_id() );
983 -
984 - if ( is_callable( array( $item, $name ) ) ) {
985 - return call_user_func_array( array( $item, $name ), $arguments );
986 - }
987 - }
988 - }
989 -
990 - return false;
991 - }*/
992 -
993 784 public function offsetSet( $offset, $value ) {
785 + _deprecated_function( __METHOD__, '4.2.3.5' );
994 786 // TODO: Implement offsetSet() method.
995 787 }
996 788
997 789 public function offsetGet( $offset ) {
790 + _deprecated_function( __METHOD__, '4.2.3.5' );
998 791 if ( is_callable( array( $this, 'get_' . $offset ) ) ) {
999 792 return call_user_func( array( $this, 'get_' . $offset ) );
1000 793 }
1001 794
@@ -1002,12 +795,14 @@
1002 795 return false;
1003 796 }
1004 797
1005 798 public function offsetUnset( $offset ) {
799 + _deprecated_function( __METHOD__, '4.2.3.5' );
1006 800 // TODO: Implement offsetUnset() method.
1007 801 }
1008 802
1009 803 public function offsetExists( $offset ) {
804 + _deprecated_function( __METHOD__, '4.2.3.5' );
1010 805 // TODO: Implement offsetExists() method.
1011 806 }
1012 807 }
1013 808