Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for custom "date_format" and "time_format" arguments #498

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions includes/CMB2_Sanitize.php
Expand Up @@ -209,7 +209,19 @@ public function text_money() {
* @return string Timestring
*/
public function text_date_timestamp() {
return is_array( $this->value ) ? array_map( 'strtotime', $this->value ) : strtotime( $this->value );
if ( is_array( $this->value ) ) {
$returnValue = [ ];
foreach ( $this->value as $value ) {
$date_object = DateTime::createFromFormat( $this->field->args['date_format'], $value );
$returnValue[] = $date_object ? $date_object->setTime( 0, 0, 0 )->getTimeStamp() : '';

}
} else {
$date_object = DateTime::createFromFormat( $this->field->args['date_format'], $this->value );
$returnValue = $date_object ? $date_object->setTime( 0, 0, 0 )->getTimeStamp() : '';
}

return $returnValue;
}

/**
Expand All @@ -228,13 +240,13 @@ public function text_datetime_timestamp( $repeat = false ) {
return $repeat_value;
}

$this->value = strtotime( $this->value['date'] . ' ' . $this->value['time'] );
$this->value = DateTime::createFromFormat( $this->field->args['date_format'] .' ' .$this->field->args['time_format'], $this->value['date']. ' ' .$this->value['time'] );

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

return $this->value;
return $this->value->getTimeStamp();
}

/**
Expand Down
16 changes: 16 additions & 0 deletions includes/CMB2_Types.php
Expand Up @@ -500,10 +500,13 @@ public function wysiwyg( $args = array() ) {
}

public function text_date( $args = array() ) {
$dateFormat = cmb2_utils()->php_to_js_dateformat( $this->field->args( 'date_format' ) );

$args = wp_parse_args( $args, array(
'class' => 'cmb2-text-small cmb2-datepicker',
'value' => $this->field->get_timestamp_format(),
'desc' => $this->_desc(),
'data-datepicker' => '{ "dateFormat": "' . $dateFormat . '" }'
) );

CMB2_JS::add_dependencies( array( 'jquery-ui-core', 'jquery-ui-datepicker' ) );
Expand All @@ -513,6 +516,9 @@ public function text_date( $args = array() ) {

// Alias for text_date
public function text_date_timestamp( $args = array() ) {
$dateFormat = cmb2_utils()->php_to_js_dateformat( $this->field->args( 'date_format' ) );
$args['data-datepicker'] = '{ "dateFormat": "' . $dateFormat . '" }';

return $this->text_date( $args );
}

Expand Down Expand Up @@ -555,6 +561,11 @@ public function text_datetime_timestamp( $args = array() ) {
'desc' => '',
) );

if ( $this->field->args('date_format') ) {
$dateFormat = cmb2_utils()->php_to_js_dateformat($this->field->args('date_format'));
$date_args['data-datepicker'] = '{ "dateFormat": "' . $dateFormat . '" }';
}

$time_args = wp_parse_args( $args['timepicker'], array(
'class' => 'cmb2-timepicker text-time',
'name' => $this->_name( '[time]' ),
Expand All @@ -563,6 +574,11 @@ public function text_datetime_timestamp( $args = array() ) {
'desc' => $args['desc'],
) );

if ( $this->field->args( 'time_format' ) ) {
$timeFormat = cmb2_utils()->php_to_js_dateformat($this->field->args('time_format'));
$time_args['data-timepicker'] = '{ "timeFormat": "' . $timeFormat . '" }';
}

CMB2_JS::add_dependencies( array( 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-datetimepicker' ) );

return $this->input( $date_args ) . "\n" . $this->input( $time_args );
Expand Down
45 changes: 45 additions & 0 deletions includes/CMB2_Utils.php
Expand Up @@ -212,4 +212,49 @@ public function url( $path = '' ) {
return $this->url . $path;
}

/**
* Takes a php date() format string and returns a string formatted to suit for the date/time pickers
* It will work with only with the following subset ot date() options:
*
* d, j, z, m, n, y, Y, h, H and i.
*
* A slight effort is made to deal with escaped characters.
*
* Other options are ignored, because they would either bring compatibility problems between PHP and JS, or
* bring even more translation troubles.
*
* @since 2.0.6
*
* @param string $format php date format
*
* @return string reformatted string
*/
public function php_to_js_dateformat( $format ) {

// order is relevant here, since the replacement will be done sequentially.
$supported_options = [
'd' => 'dd', // Day, leading 0
'j' => 'd', // Day, no 0
'z' => 'o', // Day of the year, no leading zeroes,
'm' => 'mm', // Month of the year, leading 0
'n' => 'm', // Month of the year, no leading 0
'y' => 'y', // Year, two digit
'Y' => 'yy', // Year, full
'h' => 'H', // 12-hour format of an hour with leading zeros
'H' => 'HH', // 24-hour format of an hour with leading zeros
'i' => 'mm', // Minutes with leading zeros
];

foreach ( $supported_options as $php => $js ) {
// replaces every instance of a supported option, but skips escaped characters
$format = preg_replace( "~(?<!\\\\)$php~", $js, $format );
}

$format = preg_replace_callback( '~(?:\\\.)+~', function ( $m ) {
return "&#39;" . str_replace( '\\', '', $m[0] ) . "&#39;";
}, $format );

return $format;
}

}
18 changes: 14 additions & 4 deletions js/cmb2.js
Expand Up @@ -721,17 +721,27 @@ window.CMB2 = (function(window, document, $, undefined){
return;
}

$selector.timepicker( 'destroy' );
$selector.timepicker( cmb.defaults.time_picker );
$selector.each(function() {
var options = cmb.defaults.time_picker;
$(this).timepicker( 'destroy' );
var additionalOptions = $(this).data("timepicker");
$.extend( options, additionalOptions );
$(this).timepicker( options );
});
};

cmb.initDatePickers = function( $selector ) {
if ( ! $selector.length ) {
return;
}

$selector.datepicker( 'destroy' );
$selector.datepicker( cmb.defaults.date_picker );
$selector.each(function() {
var options = cmb.defaults.date_picker;
$(this).datepicker( 'destroy' );
var additionalOptions = $(this).data("datepicker");
$.extend( options, additionalOptions );
$(this).datepicker( options );
});
};

cmb.initColorPickers = function( $selector ) {
Expand Down