Skip to content

Commit

Permalink
Account for various types of timestamp values passed through REST API.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsternberg committed Apr 22, 2021
1 parent c8b29a0 commit 37c8763
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
15 changes: 11 additions & 4 deletions includes/CMB2_Sanitize.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,24 @@ public function text_datetime_timestamp( $repeat = false ) {
// date_create_from_format if there is a slash in the value.
$this->value = wp_unslash( $this->value );

$test = is_array( $this->value ) ? array_filter( $this->value ) : '';
if ( empty( $test ) ) {
return '';
if ( is_array( $this->value ) ) {
$test = array_filter( $this->value );
if ( empty( $test ) ) {
return '';
}
}

$repeat_value = $this->_check_repeat( __FUNCTION__, $repeat );
if ( false !== $repeat_value ) {
return $repeat_value;
}

if ( isset( $this->value['date'], $this->value['time'] ) ) {
// Account for timestamp values passed through REST API.
if ( is_scalar( $this->value ) && CMB2_Utils::is_valid_date( $this->value ) ) {

$this->value = CMB2_Utils::make_valid_time_stamp( $this->value );

} elseif ( isset( $this->value['date'], $this->value['time'] ) ) {
$this->value = $this->field->get_timestamp_from_value( $this->value['date'] . ' ' . $this->value['time'] );
}

Expand Down
40 changes: 36 additions & 4 deletions includes/CMB2_Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public static function timezone_string() {
}

/**
* Returns a timestamp, first checking if value already is a timestamp.
* Returns a unix timestamp, first checking if value already is a timestamp.
*
* @since 2.0.0
* @param string|int $string Possible timestamp string.
Expand All @@ -258,9 +258,41 @@ public static function make_valid_time_stamp( $string ) {
return 0;
}

return self::is_valid_time_stamp( $string )
? (int) $string :
strtotime( (string) $string );
$timestamp = @strtotime( (string) $string );
if ( ! empty( $timestamp ) ) {

// We got a timestamp, first try!
return $timestamp;
}

$valid = self::is_valid_time_stamp( $string );
if ( $valid ) {
$timestamp = (int) $string;
$length = strlen( (string) $timestamp );
$unixlength = strlen( (string) time() );
$diff = $length - $unixlength;

// If value is larger than a unix timestamp, we need to round to the
// nearest unix timestamp (in seconds).
if ( $diff > 0 ) {
$divider = (int) '1' . str_repeat( '0', $diff );
$timestamp = round( $timestamp / $divider );
}
}

return $timestamp;
}

/**
* Determine if a value is a valid date.
*
* @since 2.9.1
* @param mixed $date Value to check.
* @return boolean Whether value is a valid date
*/
public static function is_valid_date( $date ) {
return ( is_string( $date ) && @strtotime( $date ) )
|| self::is_valid_time_stamp( $date );
}

/**
Expand Down

0 comments on commit 37c8763

Please sign in to comment.