Skip to content

Commit 37c8763

Browse files
committed
Account for various types of timestamp values passed through REST API.
1 parent c8b29a0 commit 37c8763

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

includes/CMB2_Sanitize.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,24 @@ public function text_datetime_timestamp( $repeat = false ) {
297297
// date_create_from_format if there is a slash in the value.
298298
$this->value = wp_unslash( $this->value );
299299

300-
$test = is_array( $this->value ) ? array_filter( $this->value ) : '';
301-
if ( empty( $test ) ) {
302-
return '';
300+
if ( is_array( $this->value ) ) {
301+
$test = array_filter( $this->value );
302+
if ( empty( $test ) ) {
303+
return '';
304+
}
303305
}
304306

305307
$repeat_value = $this->_check_repeat( __FUNCTION__, $repeat );
306308
if ( false !== $repeat_value ) {
307309
return $repeat_value;
308310
}
309311

310-
if ( isset( $this->value['date'], $this->value['time'] ) ) {
312+
// Account for timestamp values passed through REST API.
313+
if ( is_scalar( $this->value ) && CMB2_Utils::is_valid_date( $this->value ) ) {
314+
315+
$this->value = CMB2_Utils::make_valid_time_stamp( $this->value );
316+
317+
} elseif ( isset( $this->value['date'], $this->value['time'] ) ) {
311318
$this->value = $this->field->get_timestamp_from_value( $this->value['date'] . ' ' . $this->value['time'] );
312319
}
313320

includes/CMB2_Utils.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public static function timezone_string() {
247247
}
248248

249249
/**
250-
* Returns a timestamp, first checking if value already is a timestamp.
250+
* Returns a unix timestamp, first checking if value already is a timestamp.
251251
*
252252
* @since 2.0.0
253253
* @param string|int $string Possible timestamp string.
@@ -258,9 +258,41 @@ public static function make_valid_time_stamp( $string ) {
258258
return 0;
259259
}
260260

261-
return self::is_valid_time_stamp( $string )
262-
? (int) $string :
263-
strtotime( (string) $string );
261+
$timestamp = @strtotime( (string) $string );
262+
if ( ! empty( $timestamp ) ) {
263+
264+
// We got a timestamp, first try!
265+
return $timestamp;
266+
}
267+
268+
$valid = self::is_valid_time_stamp( $string );
269+
if ( $valid ) {
270+
$timestamp = (int) $string;
271+
$length = strlen( (string) $timestamp );
272+
$unixlength = strlen( (string) time() );
273+
$diff = $length - $unixlength;
274+
275+
// If value is larger than a unix timestamp, we need to round to the
276+
// nearest unix timestamp (in seconds).
277+
if ( $diff > 0 ) {
278+
$divider = (int) '1' . str_repeat( '0', $diff );
279+
$timestamp = round( $timestamp / $divider );
280+
}
281+
}
282+
283+
return $timestamp;
284+
}
285+
286+
/**
287+
* Determine if a value is a valid date.
288+
*
289+
* @since 2.9.1
290+
* @param mixed $date Value to check.
291+
* @return boolean Whether value is a valid date
292+
*/
293+
public static function is_valid_date( $date ) {
294+
return ( is_string( $date ) && @strtotime( $date ) )
295+
|| self::is_valid_time_stamp( $date );
264296
}
265297

266298
/**

0 commit comments

Comments
 (0)