Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Bump to 2.7.0.2
  Fix timestamp offsets when using date timestamp fields
  Fix `$object_id` doc block types in helper-functions.php. Fixes CMB2#1365
  Add props for CMB2#1347
  Fix tests to account for CMB2#1347
  • Loading branch information
lipemat committed Nov 23, 2020
2 parents eec751e + 4939326 commit b857a08
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
21 changes: 15 additions & 6 deletions includes/CMB2_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ public function field_timezone_offset() {
return CMB2_Utils::timezone_offset( $this->field_timezone() );
}


/**
* Return timezone string
*
Expand All @@ -805,23 +806,30 @@ public function field_timezone() {
// Is there another meta key with a timezone stored as its value we should use?
elseif ( $this->args( 'timezone_meta_key' ) ) {
$value = $this->get_data( $this->args( 'timezone_meta_key' ) );
} elseif ( ! empty( get_option( 'timezone_string' ) ) ) {
$value = get_option( 'timezone_string' );
}

return $value;
}


/**
* Format the timestamp field value based on the field date/time format arg
*
* @param int $meta_value Timestamp.
* @param string $format Either date_format or time_format.
*
* @since 2.0.0
* @param int $meta_value Timestamp.
* @param string $format Either date_format or time_format.
* @return string Formatted date
*/
public function format_timestamp( $meta_value, $format = 'date_format' ) {
if ( $tz_offset = $this->field_timezone_offset() ) {
$meta_value += (int) $tz_offset;
}

return date( stripslashes( $this->args( $format ) ), $meta_value );
}

/**
* Return a formatted timestamp for a field
*
Expand All @@ -845,16 +853,17 @@ public function get_timestamp_format( $format = 'date_format', $meta_value = 0 )
? array_map( array( $this, 'format_timestamp' ), $meta_value, $format )
: $this->format_timestamp( $meta_value, $format );
}

/**
* Get timestamp from text date
*
* @param string $value Date value.
*
* @since 2.2.0
* @param string $value Date value.
* @return mixed Unix timestamp representing the date.
*/
public function get_timestamp_from_value( $value ) {
return CMB2_Utils::get_timestamp_from_value( $value, $this->args( 'date_format' ) );
return CMB2_Utils::get_timestamp_from_value( $value, $this->args( 'date_format' ), $this->field_timezone() );
}

/**
Expand Down
4 changes: 0 additions & 4 deletions includes/CMB2_Sanitize.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,6 @@ public function text_datetime_timestamp( $repeat = false ) {
$this->value = $this->field->get_timestamp_from_value( $this->value['date'] . ' ' . $this->value['time'] );
}

if ( $tz_offset = $this->field->field_timezone_offset() ) {
$this->value += (int) $tz_offset;
}

return $this->value;
}

Expand Down
15 changes: 10 additions & 5 deletions includes/CMB2_Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,17 +437,22 @@ protected static function normalize_path( $path ) {
return $path;
}


/**
* Get timestamp from text date
*
* @param string $value Date value.
* @param string $deprecated No longer used date_format.
*
* @since 2.2.0
* @param string $value Date value.
* @param string $date_format Expected date format.
* @return mixed Unix timestamp representing the date.
*/
public static function get_timestamp_from_value( $value, $date_format ) {
$date_object = date_create_from_format( $date_format, $value );
return $date_object ? $date_object->setTime( 0, 0, 0 )->getTimeStamp() : strtotime( $value );
public static function get_timestamp_from_value( $value, $deprecated, $timezone = null ) {
if ( empty( $timezone ) ) {
return strtotime( $value );
}

return ( new DateTime( $value, new DateTimeZone( $timezone ) ) )->getTimestamp();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion init.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Bill Erickson (@billerickson / billerickson.net)
* Andrew Norcross (@norcross / andrewnorcross.com)
*
* Version: 2.7.0.1
* Version: 2.7.0.2
*
* Text Domain: cmb2
* Domain Path: languages
Expand Down
4 changes: 2 additions & 2 deletions tests/test-cmb-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ public function test_taxonomy_multicheck_hierarchical_field_after_value_update()

public function test_file_list_field() {
$this->assertHTMLstringsAreEqual(
'<input type="hidden" class="cmb2-upload-file cmb2-upload-list" name="field_test_field" id="field_test_field" value="" size="45" data-previewsize=\'[120,120]\' data-sizename=\'thumbnail\' data-queryargs=\'\' data-hash=\'4lavrjdps2t0\'/><input type="button" class="cmb2-upload-button button-secondary cmb2-upload-list" name="" id="" value="' . esc_attr__( 'Add or Upload Files', 'cmb2' ) . '" data-hash=\'4lavrjdps2t0\'/><p class="cmb2-metabox-description">This is a description</p><ul id="field_test_field-status" class="cmb2-media-status cmb-attach-list"></ul>',
'<input type="hidden" class="cmb2-upload-file cmb2-upload-list" name="field_test_field" id="field_test_field" value="" size="45" data-previewsize=\'[120,120]\' data-sizename=\'thumbnail\' data-queryargs=\'\' data-hash=\'4lavrjdps2t0\'/><input type="button" class="cmb2-upload-button button-secondary cmb2-upload-list" value="' . esc_attr__( 'Add or Upload Files', 'cmb2' ) . '" data-hash=\'4lavrjdps2t0\'/><p class="cmb2-metabox-description">This is a description</p><ul id="field_test_field-status" class="cmb2-media-status cmb-attach-list"></ul>',
$this->capture_render( array(
$this->get_field_type_object( array(
'type' => 'file_list',
Expand Down Expand Up @@ -912,7 +912,7 @@ public function test_file_list_field_after_value_update() {
: 'thumbnail';

$this->assertHTMLstringsAreEqual(
sprintf( '<input type="hidden" class="cmb2-upload-file cmb2-upload-list" name="field_test_field" id="field_test_field" value="" size="45" data-previewsize=\'[50,50]\' data-sizename=\'' . $sizename . '\' data-queryargs=\'\' data-hash=\'4lavrjdps2t0\'/><input type="button" class="cmb2-upload-button button-secondary cmb2-upload-list" name="" id="" value="' . esc_attr__( 'Add or Upload Files', 'cmb2' ) . '" data-hash=\'4lavrjdps2t0\'/><p class="cmb2-metabox-description">This is a description</p><ul id="field_test_field-status" class="cmb2-media-status cmb-attach-list">%1$s%2$s</ul>',
sprintf( '<input type="hidden" class="cmb2-upload-file cmb2-upload-list" name="field_test_field" id="field_test_field" value="" size="45" data-previewsize=\'[50,50]\' data-sizename=\'' . $sizename . '\' data-queryargs=\'\' data-hash=\'4lavrjdps2t0\'/><input type="button" class="cmb2-upload-button button-secondary cmb2-upload-list" value="' . esc_attr__( 'Add or Upload Files', 'cmb2' ) . '" data-hash=\'4lavrjdps2t0\'/><p class="cmb2-metabox-description">This is a description</p><ul id="field_test_field-status" class="cmb2-media-status cmb-attach-list">%1$s%2$s</ul>',
$this->file_sprintf( array(
'file_name' => $field_type->get_file_name_from_path( $attach_1_url ),
'attachment_id' => $this->attachment_id,
Expand Down

0 comments on commit b857a08

Please sign in to comment.