PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
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 4.2.0 All 139 releases
← All changes | inc/class-lp-datetime.php +262 -237 4.1.7.3.14.4.8 View file →
@@ -2,43 +2,37 @@
2 2
3 3 /**
4 4 * Class LP_Datetime
5 5 */
6 -class LP_Datetime extends DateTime {
6 +class LP_Datetime {
7 7 /**
8 - * @var string $format.
8 + * @var string $format .
9 9 */
10 10 public static $format = 'Y-m-d H:i:s';
11 -
12 11 /**
13 - * @var object
12 + * Format date by config WP.
14 13 */
15 - protected static $gmt;
16 -
14 + const I18N_FORMAT = 'i18n';
17 15 /**
18 - * @var object
16 + * Format date time by config WP.
19 17 */
20 - protected static $stz;
21 -
18 + const I18N_FORMAT_HAS_TIME = 'i18n_has_time';
22 19 /**
23 - * @var DateTimeZone
20 + * Format date time Human.
24 21 */
25 - protected $tz;
26 -
22 + const HUMAN_FORMAT = 'human';
27 23 /**
28 24 * String date time.
29 25 *
30 - * @var string $raw_date.
26 + * @var string $raw_date .
31 27 */
32 28 protected $raw_date = null;
33 29
34 - protected static $def_timezone = null;
35 -
36 30 /**
37 31 * Constructor.
38 32 *
39 - * @param string $date
40 - * @param mixed $tz
33 + * @param string|int $date
34 + * @param mixed $tz
41 35 *
42 36 * @throws
43 37 */
44 38 public function __construct( $date = '', $tz = null ) {
@@ -50,45 +44,16 @@
50 44
51 45 if ( empty( $this->raw_date ) ) {
52 46 $this->raw_date = current_time( 'mysql', 1 );
53 47 }
54 -
55 - //date_default_timezone_set( 'UTC' );
56 - parent::__construct( $this->raw_date );
57 48 }
58 49
59 50 /**
60 - * Get default timezone from param and wp settings
51 + * Check date is null
61 52 *
62 - * @param mixed $tz
63 - *
64 - * @return DateTimeZone|null|string
65 - * @deprecated 4.1.7.3
53 + * @return bool
66 54 */
67 - public static function get_default_timezone( $tz ) {
68 - _deprecated_function( __METHOD__, '4.1.7.3' );
69 - if ( empty( self::$def_timezone ) ) {
70 - if ( $tz === null ) {
71 - $tz = wp_timezone();
72 - } elseif ( is_string( $tz ) && $tz ) {
73 - $tz = new DateTimeZone( $tz );
74 - }
75 - self::$def_timezone = $tz;
76 - }
77 -
78 - return self::$def_timezone;
79 - }
80 -
81 - /**
82 - * Check if time is exceeded with current time
83 - *
84 - * using by Addon Content Drip.
85 - */
86 - public function is_exceeded( $interval = 0 ) {
87 - return $this->getTimestamp() >= current_time( 'timestamp' ) + $interval; // phpcs:ignore
88 - }
89 -
90 - public function is_null() {
55 + public function is_null(): bool {
91 56 return ! $this->raw_date || $this->raw_date === '0000-00-00 00:00:00';
92 57 }
93 58
94 59 public function get_raw_date() {
@@ -95,277 +60,337 @@
95 60 return $this->raw_date;
96 61 }
97 62
98 63 /**
99 - * @param string $name The name of the property.
100 - *
101 - * @return mixed
102 - * @deprecated 4.1.7.3
103 - */
104 - public function __get( $name ) {
105 - _deprecated_function( __METHOD__, '4.1.7.3' );
106 - /*$value = null;
107 -
108 - switch ( $name ) {
109 - case 'daysinmonth':
110 - $value = $this->format( 't', true );
111 - break;
112 -
113 - case 'dayofweek':
114 - $value = $this->format( 'N', true );
115 - break;
116 -
117 - case 'dayofyear':
118 - $value = $this->format( 'z', true );
119 - break;
120 -
121 - case 'isleapyear':
122 - $value = (bool) $this->format( 'L', true );
123 - break;
124 -
125 - case 'day':
126 - $value = $this->format( 'd', true );
127 - break;
128 -
129 - case 'hour':
130 - $value = $this->format( 'H', true );
131 - break;
132 -
133 - case 'minute':
134 - $value = $this->format( 'i', true );
135 - break;
136 -
137 - case 'second':
138 - $value = $this->format( 's', true );
139 - break;
140 -
141 - case 'month':
142 - $value = $this->format( 'm', true );
143 - break;
144 -
145 - case 'ordinal':
146 - $value = $this->format( 'S', true );
147 - break;
148 -
149 - case 'week':
150 - $value = $this->format( 'W', true );
151 - break;
152 -
153 - case 'year':
154 - $value = $this->format( 'Y', true );
155 - break;
156 -
157 - default:
158 - }
159 -
160 - return $value;*/
161 - }
162 -
163 - /**
164 64 * @return string The date as a formatted string.
165 65 */
166 66 public function __toString() {
167 - return (string) $this->format( self::$format, true );
67 + return $this->format( self::$format );
168 68 }
169 69
170 70 /**
171 71 * Gets the date as a formatted string.
172 72 *
173 - * @param string $format The date format specification string (see {@link PHP_MANUAL#date})
174 - * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
73 + * @param string $format Set i18n to return date in local time.
175 74 *
176 - * @return string The date string in the specified format format.
75 + * @return string.
76 + * @version 4.0.2
77 + * @since 3.0.0
177 78 */
178 - public function format( $format = '', $local = true ) {
179 - if ( '0000-00-00 00:00:00' === $this->raw_date ) {
180 - return '';
79 + public function format( string $format = '' ): string {
80 + $date_str = '';
81 +
82 + if ( '0000-00-00 00:00:00' === $this->get_raw_date() ) {
83 + return $date_str;
181 84 }
182 85
183 86 if ( empty( $format ) ) {
184 - $format = 'mysql';
87 + $format = get_option( 'date_format', 'Y-m-d' );
185 88 }
186 89
187 - $return = false;
188 -
189 90 switch ( $format ) {
190 - case 'i18n':
191 - $return = learn_press_date_i18n( $this->getTimestamp( $local ) );
91 + case 'i18n': // Display format Date by Timezone of WP.
92 + $time_stamp = $this->getTimestamp(); // UTC+0 (GMT)
93 + $time_stamp_by_time_zone = $time_stamp + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
94 + $date_format = get_option( 'date_format' );
95 + $date_str = date_i18n( $date_format, $time_stamp_by_time_zone );
192 96 break;
193 - case 'timestamp':
194 - $return = $this->getTimestamp( $local );
97 + case 'i18n_has_time': // Display format Date Time by Timezone of WP.
98 + $date_time_format_wp = apply_filters(
99 + 'learn-press/datetime/format/i18n_has_time',
100 + get_option( 'date_format' ) . ' ' . get_option( 'time_format' )
101 + );
102 + $time_stamp = $this->getTimestamp(); // UTC+0 (GMT)
103 + $time_stamp_by_time_zone = $time_stamp + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
104 + $date_str = apply_filters(
105 + 'learn-press/datetime/date/i18n_has_time',
106 + sprintf(
107 + '%s',
108 + date_i18n( $date_time_format_wp, $time_stamp_by_time_zone )
109 + ),
110 + $time_stamp,
111 + $time_stamp_by_time_zone
112 + );
195 113 break;
196 114 case 'human':
197 - $time1 = $this->getTimestamp( false );// mysql2date( 'G', $date->format('Y-m-d H:i:s') );
198 - $time_diff = ( time() ) - $time1;
115 + $time = $this->getTimestamp();
116 + $time_diff = time() - $time;
199 117
200 118 if ( $time_diff > 0 ) {
201 - $return = sprintf( __( '%s ago', 'learnpress' ), human_time_diff( $time1, time() ) );
119 + $date_str = sprintf( __( '%s ago', 'learnpress' ), human_time_diff( $time, time() ) );
202 120 }
203 121 break;
204 122 case 'mysql':
205 - $return = $this->format( 'Y-m-d H:i:s', $local );
123 + $date_str = gmdate( 'Y-m-d H:i:s', $this->getTimestamp() );
206 124 break;
207 125 default:
208 - $return = parent::format( $format );
126 + $date_str = gmdate( $format, $this->getTimestamp() );
209 127 }
210 128
211 - return $return;
129 + if ( ! is_string( $date_str ) ) {
130 + $date_str = '';
131 + }
132 +
133 + return $date_str;
212 134 }
213 135
214 136 /**
215 - * Get format date time by settings of WP
137 + * Display date human time diff.
138 + * 1. Show number days, hours if >= 1 days
139 + * 2. Show number hours, seconds if >= 1 hours
140 + * 3. Show number seconds if < 1 hours
216 141 *
217 - * @since 4.1.7.3
218 - * @return void
142 + * @param DateTime $date_start
143 + * @param DateTime $date_end
144 + *
145 + * @return string
146 + * @since 4.0.3
147 + * @version 1.0.5
219 148 */
220 - public function get_local_date_time() {
149 + public static function format_human_time_diff( DateTime $date_start, DateTime $date_end ): string {
150 + $diff = $date_end->diff( $date_start );
151 + $week = floor( $diff->d / 7 );
221 152
222 - }
153 + $i18n_year = self::get_string_plural_duration( $diff->y, 'year' );
154 + $i18n_month = self::get_string_plural_duration( $diff->m, 'month' );
155 + $i18n_week = self::get_string_plural_duration( $week, 'week' );
156 + $i18n_day = self::get_string_plural_duration( $diff->d, 'day' );
157 + $i18n_hour = self::get_string_plural_duration( $diff->h, 'hour' );
158 + $i18n_minute = self::get_string_plural_duration( $diff->i, 'minute' );
159 + $i18n_second = self::get_string_plural_duration( $diff->s, 'second' );
223 160
224 - /**
225 - * @param boolean $hours True to return the value in hours.
226 - *
227 - * @return float
228 - */
229 - public function getOffset( $hours = false ) {
230 - return $this->tz ? (float) $hours ? ( $this->tz->getOffset( $this ) / 3600 ) : $this->tz->getOffset( $this ) : 0;
161 + $format_date = '';
162 + $string = array(
163 + 'y' => '%y years',
164 + 'm' => '%m months',
165 + 'w' => '', // object don't have week, only add to custom week format
166 + 'd' => '%d days, %h hours',
167 + 'h' => '%h hours, %i minutes',
168 + 'i' => '%i minutes, %s seconds',
169 + 's' => $i18n_second,
170 + );
171 +
172 + foreach ( $string as $k => $v ) {
173 + if ( isset( $diff->{$k} ) && $diff->{$k} > 0 ) {
174 + switch ( $k ) {
175 + case 'y':
176 + $format_date = sprintf(
177 + '%1$s%2$s',
178 + $i18n_year,
179 + $diff->m > 0 ? ', ' . $i18n_month : ''
180 + );
181 + break;
182 + case 'm':
183 + $format_date = sprintf(
184 + '%1$s%2$s',
185 + $i18n_month,
186 + $diff->d > 0 ? ', ' . $i18n_day : ''
187 + );
188 + break;
189 + case 'd':
190 + $format_date = sprintf(
191 + '%1$s%2$s',
192 + $i18n_day,
193 + $diff->h > 0 ? ', ' . $i18n_hour : ''
194 + );
195 + break;
196 + case 'h':
197 + $format_date = sprintf(
198 + '%1$s%2$s',
199 + $i18n_hour,
200 + $diff->i > 0 ? ', ' . $i18n_minute : ''
201 + );
202 + break;
203 + case 'i':
204 + $format_date = sprintf(
205 + '%1$s%2$s',
206 + $i18n_minute,
207 + $diff->s > 0 ? ', ' . $i18n_second : ''
208 + );
209 + break;
210 + default:
211 + $format_date = $v;
212 + break;
213 + }
214 + break;
215 + } elseif ( 'w' === $k && $week > 0 ) {
216 + $day_remain = $diff->d - $week * 7;
217 + $format_date = sprintf(
218 + '%1$s%2$s',
219 + $i18n_week,
220 + $day_remain > 0 ? ', ' . self::get_string_plural_duration( $day_remain, 'day' ) : ''
221 + );
222 + break;
223 + }
224 + }
225 +
226 + return apply_filters(
227 + 'learn-press/datetime/format_human_time_diff',
228 + $format_date,
229 + $diff,
230 + $date_start,
231 + $date_end
232 + );
231 233 }
232 234
233 235 /**
234 - * @param DateTimeZone $tz The new DateTimeZone object.
236 + * Get the date as an SQL datetime string.
235 237 *
236 - * @return void
237 - * @deprecated 4.1.7.3
238 - */
239 - /*public function setTimezone( $tz ) {
240 - _deprecated_function( __METHOD__, '4.1.7.3' );
241 - $this->tz = $tz;
242 -
243 - parent::setTimezone( $tz );
244 - }*/
245 -
246 - /**
247 238 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
248 239 *
249 240 * @return string
250 - * @deprecated 4.1.7.3
241 + * @version 4.0.1
242 + * @since 3.0.0
251 243 */
252 - public function toISO8601( $local = true ) {
253 - _deprecated_function( __METHOD__, '4.1.7.3' );
254 - //return $this->format( DateTime::RFC3339, $local );
244 + public function toSql( bool $local = false ): string {
245 + if ( $local ) {
246 + return $this->toSqlLocal();
247 + }
248 +
249 + return $this->format( 'mysql' );
255 250 }
256 251
257 252 /**
258 - * Gets the date as an SQL datetime string.
253 + * Convert to format sql local time.
259 254 *
260 - * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
261 - *
262 - * @return string
255 + * @return string
256 + * @since 4.2.1
263 257 */
264 - public function toSql( $local = true ) {
265 - return $this->format( 'Y-m-d H:i:s', $local );
258 + private function toSqlLocal(): string {
259 + $time = $this->getTimestamp() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
260 +
261 + return gmdate( LP_Datetime::$format, $time );
266 262 }
267 263
268 264 /**
269 - * Consider the date is in GMT and convert to local time with
270 - * gmt_offset option of WP Core.
265 + * Convert local time to gmt+0 time.
271 266 *
267 + * @param LP_Datetime $date_time_local
272 268 * @param string $format
273 269 *
274 - * @return int|string
275 - * @since 4.0.0
276 - * @deprecated 4.1.7.3
270 + * @return string
271 + * @since 4.3.6
272 + * @version 1.0.0
277 273 */
278 - public function toLocal( $format = 'Y-m-d H:i:s' ) {
279 - _deprecated_function( __METHOD__, '4.1.7.3' );
280 - $time = $this->getTimestamp() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
281 -
282 - if ( $format ) {
283 - return date( $format, $time ); // phpcs:ignore
284 - }
285 -
286 - return $time;
274 + public function to_gmt_string( LP_Datetime $date_time_local, string $format = 'Y-m-d H:i:s' ): string {
275 + $time_stamp = $date_time_local->getTimestamp() - $this->get_wp_option_gmt_offset() * HOUR_IN_SECONDS;
276 + $time = new LP_Datetime( $time_stamp );
277 + return $time->format( $format );
287 278 }
288 279
289 280 /**
290 - * Gets the date as an RFC 822 string. IETF RFC 2822 supercedes RFC 822 and its definition
291 - * can be found at the IETF Web site.
281 + * Get gmt offset from wp option.
292 282 *
293 - * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
294 - *
295 - * @return string
283 + * @return false|mixed|null
296 284 */
297 - public function toRFC822( $local = true ) {
298 - return $this->format( DateTime::RFC2822, $local );
285 + public function get_wp_option_gmt_offset() {
286 + return get_option( 'gmt_offset' );
299 287 }
300 288
301 289 /**
302 - * Gets the date as UNIX time stamp.
290 + * Get timestamp of Date.
303 291 *
304 - * @return integer The date as a UNIX timestamp.
305 - * @deprecated 4.1.7.3
292 + * @return int
306 293 */
307 - public function toUnix() {
308 - _deprecated_function( __METHOD__, '4.1.7.3' );
309 - return (int) parent::format( 'U' );
310 - }
311 -
312 - public function getTimestamp( $local = true ) {
313 - $timestamp = parent::getTimestamp();
314 -
315 - if ( $local ) {
316 - $timestamp += $this->getOffset();
294 + public function getTimestamp(): int {
295 + try {
296 + $date = new DateTime( $this->get_raw_date() );
297 + } catch ( Throwable $e ) {
298 + error_log( __METHOD__ . ': ' . $e->getMessage() );
299 + $date = new DateTime();
317 300 }
318 301
319 - return $timestamp;
302 + return $date->getTimestamp();
320 303 }
321 304
322 305 /**
323 - * @deprecated 4.1.7.3
306 + * Get timestamp of Date in local time.
307 + * Note: timestamp before handle must timezone is GMT+0
308 + *
309 + * @return int
310 + * @since 4.2.7.3
324 311 */
325 - protected function setGMT( $local = false, $gmt = true ) {
326 - _deprecated_function( __METHOD__, '4.1.7.3' );
327 - /*if ( $gmt ) {
328 - if ( $local == false && ! empty( self::$gmt ) ) {
329 - parent::setTimezone( self::$gmt );
330 - }
331 - } else {
332 - if ( $local == false && ! empty( $this->tz ) ) {
333 - parent::setTimezone( $this->tz );
334 - }
335 - }*/
312 + public function getTimestampLocal(): int {
313 + return $this->getTimestamp() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
336 314 }
337 315
338 316 /**
339 - * @deprecated 4.1.7.3
317 + * Get string plural duration.
318 + *
319 + * @param int $duration_number
320 + * @param string $duration_type
321 + *
322 + * @return string
323 + * @version 1.0.5
324 + * @since 4.2.3.5
340 325 */
341 - public static function getSqlNullDate() {
342 - _deprecated_function( __METHOD__, '4.1.7.3' );
343 - return '0000-00-00 00:00:00';
326 + public static function get_string_plural_duration( int $duration_number, string $duration_type = '' ): string {
327 + switch ( strtolower( $duration_type ) ) {
328 + case 'second':
329 + $duration_str = sprintf(
330 + _n( '%s Second', '%s Seconds', $duration_number, 'learnpress' ),
331 + $duration_number
332 + );
333 + break;
334 + case 'minute':
335 + $duration_str = sprintf(
336 + _n( '%s Minute', '%s Minutes', $duration_number, 'learnpress' ),
337 + $duration_number
338 + );
339 + break;
340 + case 'hour':
341 + $duration_str = sprintf(
342 + _n( '%s Hour', '%s Hours', $duration_number, 'learnpress' ),
343 + $duration_number
344 + );
345 + break;
346 + case 'day':
347 + $duration_str = sprintf(
348 + _n( '%s Day', '%s Days', $duration_number, 'learnpress' ),
349 + $duration_number
350 + );
351 + break;
352 + case 'week':
353 + $duration_str = sprintf(
354 + _n( '%s Week', '%s Weeks', $duration_number, 'learnpress' ),
355 + $duration_number
356 + );
357 + break;
358 + case 'month':
359 + $duration_str = sprintf(
360 + _n( '%s Month', '%s Months', $duration_number, 'learnpress' ),
361 + $duration_number
362 + );
363 + break;
364 + case 'year':
365 + $duration_str = sprintf(
366 + _n( '%s Year', '%s Years', $duration_number, 'learnpress' ),
367 + $duration_number
368 + );
369 + break;
370 + default:
371 + $duration_str = $duration_number . ' ' . $duration_type;
372 + }
373 +
374 + return apply_filters( 'learn-press/i18n/plural_duration', $duration_str, $duration_number, $duration_type );
344 375 }
345 376
346 377 /**
347 - * Add X seconds into datetime of this object.
378 + * Get timezone string
348 379 *
349 - * @param int $seconds
350 - *
351 - * @throws
352 - *
353 - * @since 3.3.0
354 - * @deprecated 4.1.7.3
380 + * @return string
381 + * @since 4.2.7.2
382 + * @version 1.0.0
355 383 */
356 - public function addDuration( $seconds ) {
357 - _deprecated_function( __METHOD__, '4.1.7.3' );
358 - $timestamp = $this->getTimestamp();
359 - parent::__construct( date( 'Y-m-d H:i:s', $timestamp + $seconds ), $this->tz ); // phpcs:ignore
360 - }
384 + public static function get_timezone_string(): string {
385 + $wp_timezone = wp_timezone_string();
386 + $is_utc = (int) $wp_timezone !== 0;
361 387
362 - public function getPeriod( $seconds, $local = true ) {
363 - $timestamp = $this->getTimestamp( $local );
364 -
365 - if ( ! is_numeric( $seconds ) ) {
366 - $seconds = strtotime( $seconds ) - time();
388 + if ( $is_utc ) {
389 + $wp_timezone = sprintf( '%s %s', __( 'Timezone: UTC', 'learnpress' ), $wp_timezone );
390 + } else {
391 + $wp_timezone = sprintf( '%s %s', __( 'Timezone:', 'learnpress' ), $wp_timezone );
367 392 }
368 393
369 - return date( 'Y-m-d H:i:s', $timestamp + $seconds ); // phpcs:ignore
394 + return $wp_timezone;
370 395 }
371 396 }