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 +230 -226 4.2.04.4.8 View file →
@@ -4,41 +4,35 @@
4 4 * Class LP_Datetime
5 5 */
6 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 33 * @param string|int $date
40 - * @param mixed $tz
34 + * @param mixed $tz
41 35 *
42 36 * @throws
43 37 */
44 38 public function __construct( $date = '', $tz = null ) {
@@ -50,49 +44,11 @@
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 - //$m = $this->format( 'm' );
58 48 }
59 49
60 50 /**
61 - * Get default timezone from param and wp settings
62 - *
63 - * @param mixed $tz
64 - *
65 - * @return DateTimeZone|null|string
66 - * @deprecated 4.1.7.3
67 - */
68 - public static function get_default_timezone( $tz ) {
69 - _deprecated_function( __METHOD__, '4.1.7.3' );
70 - if ( empty( self::$def_timezone ) ) {
71 - if ( $tz === null ) {
72 - $tz = wp_timezone();
73 - } elseif ( is_string( $tz ) && $tz ) {
74 - $tz = new DateTimeZone( $tz );
75 - }
76 - self::$def_timezone = $tz;
77 - }
78 -
79 - return self::$def_timezone;
80 - }
81 -
82 - /**
83 - * Check if time is exceeded with current time
84 - *
85 - * using by Addon Content Drip.
86 - *
87 - * @since 3.0.1
88 - * @version 4.0.1
89 - */
90 - public function is_exceeded(): bool {
91 - return $this->getTimestamp() >= time();
92 - }
93 -
94 - /**
95 51 * Check date is null
96 52 *
97 53 * @return bool
98 54 */
@@ -104,73 +60,8 @@
104 60 return $this->raw_date;
105 61 }
106 62
107 63 /**
108 - * @param string $name The name of the property.
109 - *
110 - * @return mixed
111 - * @deprecated 4.1.7.3
112 - */
113 - public function __get( $name ) {
114 - _deprecated_function( __METHOD__, '4.1.7.3' );
115 - /*$value = null;
116 -
117 - switch ( $name ) {
118 - case 'daysinmonth':
119 - $value = $this->format( 't', true );
120 - break;
121 -
122 - case 'dayofweek':
123 - $value = $this->format( 'N', true );
124 - break;
125 -
126 - case 'dayofyear':
127 - $value = $this->format( 'z', true );
128 - break;
129 -
130 - case 'isleapyear':
131 - $value = (bool) $this->format( 'L', true );
132 - break;
133 -
134 - case 'day':
135 - $value = $this->format( 'd', true );
136 - break;
137 -
138 - case 'hour':
139 - $value = $this->format( 'H', true );
140 - break;
141 -
142 - case 'minute':
143 - $value = $this->format( 'i', true );
144 - break;
145 -
146 - case 'second':
147 - $value = $this->format( 's', true );
148 - break;
149 -
150 - case 'month':
151 - $value = $this->format( 'm', true );
152 - break;
153 -
154 - case 'ordinal':
155 - $value = $this->format( 'S', true );
156 - break;
157 -
158 - case 'week':
159 - $value = $this->format( 'W', true );
160 - break;
161 -
162 - case 'year':
163 - $value = $this->format( 'Y', true );
164 - break;
165 -
166 - default:
167 - }
168 -
169 - return $value;*/
170 - }
171 -
172 - /**
173 64 * @return string The date as a formatted string.
174 65 */
175 66 public function __toString() {
176 67 return $this->format( self::$format );
@@ -178,16 +69,15 @@
178 69
179 70 /**
180 71 * Gets the date as a formatted string.
181 72 *
182 - * @param string $format Set i18n to return date in local time.
183 - * @param boolean $local.
73 + * @param string $format Set i18n to return date in local time.
184 74 *
75 + * @return string.
76 + * @version 4.0.2
185 77 * @since 3.0.0
186 - * @version 4.0.1
187 - * @return string.
188 78 */
189 - public function format( string $format = '', bool $local = true ): string {
79 + public function format( string $format = '' ): string {
190 80 $date_str = '';
191 81
192 82 if ( '0000-00-00 00:00:00' === $this->get_raw_date() ) {
193 83 return $date_str;
@@ -197,14 +87,31 @@
197 87 $format = get_option( 'date_format', 'Y-m-d' );
198 88 }
199 89
200 90 switch ( $format ) {
201 - case 'i18n':
202 - $date_str = learn_press_date_i18n( $this->getTimestamp() );
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 );
203 96 break;
204 - /*case 'timestamp':
205 - $date_str = $this->getTimestamp();
206 - break;*/
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 + );
113 + break;
207 114 case 'human':
208 115 $time = $this->getTimestamp();
209 116 $time_diff = time() - $time;
210 117
@@ -226,99 +133,158 @@
226 133 return $date_str;
227 134 }
228 135
229 136 /**
230 - * @param boolean $hours True to return the value in hours.
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
231 141 *
232 - * @return float
233 - * @deprecated 4.1.7.3
142 + * @param DateTime $date_start
143 + * @param DateTime $date_end
144 + *
145 + * @return string
146 + * @since 4.0.3
147 + * @version 1.0.5
234 148 */
235 - public function getOffset( $hours = false ) {
236 - _deprecated_function( __METHOD__, '4.1.7.3' );
237 - return $this->tz ? (float) $hours ? ( $this->tz->getOffset( $this ) / 3600 ) : $this->tz->getOffset( $this ) : 0;
238 - }
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 );
239 152
240 - /**
241 - * @param DateTimeZone $tz The new DateTimeZone object.
242 - *
243 - * @return void
244 - * @deprecated 4.1.7.3
245 - */
246 - /*public function setTimezone( $tz ) {
247 - _deprecated_function( __METHOD__, '4.1.7.3' );
248 - $this->tz = $tz;
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' );
249 160
250 - parent::setTimezone( $tz );
251 - }*/
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 + );
252 171
253 - /**
254 - * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
255 - *
256 - * @return string
257 - * @deprecated 4.1.7.3
258 - */
259 - public function toISO8601( $local = true ) {
260 - _deprecated_function( __METHOD__, '4.1.7.3' );
261 - //return $this->format( DateTime::RFC3339, $local );
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 + );
262 233 }
263 234
264 235 /**
265 - * Gets the date as an SQL datetime string.
236 + * Get the date as an SQL datetime string.
266 237 *
267 238 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
268 239 *
240 + * @return string
241 + * @version 4.0.1
269 242 * @since 3.0.0
270 - * @version 4.0.1
271 - * @return string
272 243 */
273 - public function toSql( $local = true ): string {
244 + public function toSql( bool $local = false ): string {
245 + if ( $local ) {
246 + return $this->toSqlLocal();
247 + }
248 +
274 249 return $this->format( 'mysql' );
275 250 }
276 251
277 252 /**
278 - * Consider the date is in GMT and convert to local time with
279 - * gmt_offset option of WP Core.
253 + * Convert to format sql local time.
280 254 *
281 - * @param string $format
282 - *
283 - * @return int|string
284 - * @since 4.0.0
285 - * @deprecated 4.1.7.3
255 + * @return string
256 + * @since 4.2.1
286 257 */
287 - public function toLocal( $format = 'Y-m-d H:i:s' ) {
288 - _deprecated_function( __METHOD__, '4.1.7.3' );
258 + private function toSqlLocal(): string {
289 259 $time = $this->getTimestamp() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
290 260
291 - if ( $format ) {
292 - return date( $format, $time ); // phpcs:ignore
293 - }
294 -
295 - return $time;
261 + return gmdate( LP_Datetime::$format, $time );
296 262 }
297 263
298 264 /**
299 - * Gets the date as an RFC 822 string. IETF RFC 2822 supercedes RFC 822 and its definition
300 - * can be found at the IETF Web site.
265 + * Convert local time to gmt+0 time.
301 266 *
302 - * @param boolean $local True to return the date string in the local time zone, false to return it in GMT.
267 + * @param LP_Datetime $date_time_local
268 + * @param string $format
303 269 *
304 - * @return string
305 - * @deprecated 4.1.7.3
270 + * @return string
271 + * @since 4.3.6
272 + * @version 1.0.0
306 273 */
307 - public function toRFC822( $local = true ) {
308 - _deprecated_function( __METHOD__, '4.1.7.3' );
309 - return $this->format( DateTime::RFC2822, $local );
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 );
310 278 }
311 279
312 280 /**
313 - * Gets the date as UNIX time stamp.
281 + * Get gmt offset from wp option.
314 282 *
315 - * @return integer The date as a UNIX timestamp.
316 - * @deprecated 4.1.7.3
283 + * @return false|mixed|null
317 284 */
318 - public function toUnix() {
319 - _deprecated_function( __METHOD__, '4.1.7.3' );
320 - //return (int) parent::format( 'U' );
285 + public function get_wp_option_gmt_offset() {
286 + return get_option( 'gmt_offset' );
321 287 }
322 288
323 289 /**
324 290 * Get timestamp of Date.
@@ -324,13 +290,13 @@
324 290 * Get timestamp of Date.
325 291 *
326 292 * @return int
327 293 */
328 - public function getTimestamp( $local = true ): int {
294 + public function getTimestamp(): int {
329 295 try {
330 296 $date = new DateTime( $this->get_raw_date() );
331 297 } catch ( Throwable $e ) {
332 - error_log( $e->getMessage() );
298 + error_log( __METHOD__ . ': ' . $e->getMessage() );
333 299 $date = new DateTime();
334 300 }
335 301
336 302 return $date->getTimestamp();
@@ -336,57 +302,95 @@
336 302 return $date->getTimestamp();
337 303 }
338 304
339 305 /**
340 - * @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
341 311 */
342 - protected function setGMT( $local = false, $gmt = true ) {
343 - _deprecated_function( __METHOD__, '4.1.7.3' );
344 - /*if ( $gmt ) {
345 - if ( $local == false && ! empty( self::$gmt ) ) {
346 - parent::setTimezone( self::$gmt );
347 - }
348 - } else {
349 - if ( $local == false && ! empty( $this->tz ) ) {
350 - parent::setTimezone( $this->tz );
351 - }
352 - }*/
312 + public function getTimestampLocal(): int {
313 + return $this->getTimestamp() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
353 314 }
354 315
355 316 /**
356 - * @deprecated 4.1.7.3
357 - */
358 - public static function getSqlNullDate() {
359 - _deprecated_function( __METHOD__, '4.1.7.3' );
360 - return '0000-00-00 00:00:00';
361 - }
362 -
363 - /**
364 - * Add X seconds into datetime of this object.
317 + * Get string plural duration.
365 318 *
366 - * @param int $seconds
319 + * @param int $duration_number
320 + * @param string $duration_type
367 321 *
368 - * @throws
369 - *
370 - * @since 3.3.0
371 - * @deprecated 4.1.7.3
322 + * @return string
323 + * @version 1.0.5
324 + * @since 4.2.3.5
372 325 */
373 - public function addDuration( $seconds ) {
374 - _deprecated_function( __METHOD__, '4.1.7.3' );
375 - $timestamp = $this->getTimestamp();
376 - //parent::__construct( date( 'Y-m-d H:i:s', $timestamp + $seconds ), $this->tz ); // phpcs:ignore
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 );
377 375 }
378 376
379 377 /**
380 - * @deprecated 4.1.7.3
378 + * Get timezone string
379 + *
380 + * @return string
381 + * @since 4.2.7.2
382 + * @version 1.0.0
381 383 */
382 - public function getPeriod( $seconds, $local = true ) {
383 - _deprecated_function( __METHOD__, '4.1.7.3' );
384 - /*$timestamp = $this->getTimestamp( $local );
384 + public static function get_timezone_string(): string {
385 + $wp_timezone = wp_timezone_string();
386 + $is_utc = (int) $wp_timezone !== 0;
385 387
386 - if ( ! is_numeric( $seconds ) ) {
387 - $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 );
388 392 }
389 393
390 - return date( 'Y-m-d H:i:s', $timestamp + $seconds );*/
394 + return $wp_timezone;
391 395 }
392 396 }