Skip to content

Commit 9632d66

Browse files
authored
Use local timezone when formatting Time fields (#2107)
2 parents a6cfc83 + 07edb8b commit 9632d66

File tree

2 files changed

+46
-52
lines changed

2 files changed

+46
-52
lines changed

includes/class-gravityview-merge-tags.php

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,10 @@ public static function process_modifiers( $value, $merge_tag, $modifier, $field,
9595

9696
// matching regex => the value is the method to call to replace the value.
9797
$gv_modifiers = array(
98-
'maxwords:(\d+)' => 'modifier_maxwords',
99-
/** @see modifier_maxwords */
100-
'timestamp' => 'modifier_timestamp',
101-
/** @see modifier_timestamp */
102-
'explode' => 'modifier_explode',
103-
/** @see modifier_explode */
104-
105-
/** @see modifier_strings */
106-
'urlencode' => 'modifier_strings',
98+
'maxwords:(\d+)' => 'modifier_maxwords', /** @see modifier_maxwords */
99+
'timestamp' => 'modifier_timestamp', /** @see modifier_timestamp */
100+
'explode' => 'modifier_explode', /** @see modifier_explode */
101+
'urlencode' => 'modifier_strings', /** @see modifier_strings */
107102
'wpautop' => 'modifier_strings',
108103
'esc_html' => 'modifier_strings',
109104
'sanitize_html_class' => 'modifier_strings',
@@ -113,7 +108,7 @@ public static function process_modifiers( $value, $merge_tag, $modifier, $field,
113108
'ucfirst' => 'modifier_strings',
114109
'ucwords' => 'modifier_strings',
115110
'wptexturize' => 'modifier_strings',
116-
'format' => 'modifier_format',
111+
'format' => 'modifier_format', /** @see modifier_format */
117112
);
118113

119114
$modifiers = explode( ',', $modifier );
@@ -181,7 +176,17 @@ public static function process_modifiers( $value, $merge_tag, $modifier, $field,
181176
* @return string
182177
*/
183178
private static function modifier_format( $raw_value, $matches, $value, $field, $modifier ) {
184-
if ( ( $field instanceof GF_Field_Date || $field instanceof GF_Field_Time ) && $modifier ) {
179+
$format = self::get_format_merge_tag_modifier_value( $modifier );
180+
181+
if ( ! $format ) {
182+
return $raw_value;
183+
}
184+
185+
if ( $field instanceof GF_Field_Time ) {
186+
return ( new DateTime( $raw_value ) )->format( $format ); // GF's Time field always uses local time.
187+
}
188+
189+
if ( $field instanceof GF_Field_Date ) {
185190
return self::format_date( $raw_value, $modifier );
186191
}
187192

@@ -578,63 +583,51 @@ public static function replace_entry_link( $original_text, $form = array(), $ent
578583
}
579584

580585
/**
581-
* Format Merge Tags using GVCommon::format_date()
586+
* Formats merge tag value using Merge Tags using GVCommon::format_date()
587+
*
588+
* @todo This is no longer needed since Gravity Forms 2.5 as it supports modifiers, but should be reviewed before removal.
582589
*
583-
* @uses GVCommon::format_date()
590+
* @since 1.16
584591
*
585-
* @see https://docs.gravitykit.com/article/331-date-created-merge-tag for documentation
586-
* @todo Once Gravity Forms 2.5 becomes the minimum requirement, this is no longer needed.
592+
* @see https://docs.gravitykit.com/article/331-date-created-merge-tag for documentation
593+
* @uses GVCommon::format_date()
587594
*
588-
* @param string $date_created The Gravity Forms date created format
589-
* @param string $property Any modifiers for the merge tag (`human`, `format:m/d/Y`)
595+
* @param string $date_or_time_string The Gravity Forms date or time string.
596+
* @param string $modifier Merge tag modifier (`human`, `format:m/d/Y`)
590597
*
591598
* @return int|string If timestamp requested, timestamp int. Otherwise, string output.
592599
*/
593-
public static function format_date( $date_created = '', $property = '' ) {
594-
595-
// Expand all modifiers, skipping escaped colons. str_replace worked better than preg_split( "/(?<!\\):/" )
596-
$exploded = explode( ':', str_replace( '\:', '|COLON|', $property ) );
597-
598-
$atts = array(
599-
'format' => self::get_format_from_modifiers( $exploded, false ),
600-
'human' => in_array( 'human', $exploded ), // {date_created:human}
601-
'diff' => in_array( 'diff', $exploded ), // {date_created:diff}
602-
'raw' => in_array( 'raw', $exploded ), // {date_created:raw}
603-
'timestamp' => in_array( 'timestamp', $exploded ), // {date_created:timestamp}
604-
'time' => in_array( 'time', $exploded ), // {date_created:time}
605-
);
606-
607-
$formatted_date = GVCommon::format_date( $date_created, $atts );
608-
609-
return $formatted_date;
600+
public static function format_date( $date_or_time_string = '', $modifier = '' ) {
601+
$parsed_modifier = explode( ':', $modifier );
602+
603+
$atts = [
604+
'format' => self::get_format_merge_tag_modifier_value( $modifier, false ),
605+
'human' => in_array( 'human', $parsed_modifier ), // {date_created:human}
606+
'diff' => in_array( 'diff', $parsed_modifier ), // {date_created:diff}
607+
'raw' => in_array( 'raw', $parsed_modifier ), // {date_created:raw}
608+
'timestamp' => in_array( 'timestamp', $parsed_modifier ), // {date_created:timestamp}
609+
'time' => in_array( 'time', $parsed_modifier ), // {date_created:time}
610+
];
611+
612+
return GVCommon::format_date( $date_or_time_string, $atts );
610613
}
611614

612615
/**
613-
* If there is a `:format` modifier in a merge tag, grab the formatting
614-
*
615-
* The `:format` modifier should always have the format follow it; it's the next item in the array
616-
* In `foo:format:bar`, "bar" will be the returned format
616+
* Returns the `format:` merge tag modifier value.
617+
* This handles cases such as "foo:format:m/d/Y", "format:m/d/Y", "format:m/d/Y\ \a\t\ H\:i\:s".
617618
*
618619
* @since 1.16
620+
* @since TODO Renamed and refactored to use regex and instead of working with an array.
619621
*
620-
* @param array $exploded Array of modifiers with a possible `format` value
621-
* @param string $backup The backup value to use, if not found
622+
* @param string $modifier Merge tag modifier.
623+
* @param mixed $backup The backup value to use, if format not found.
622624
*
623625
* @return string If format is found, the passed format. Otherwise, the backup.
624626
*/
625-
private static function get_format_from_modifiers( $exploded, $backup = '' ) {
626-
627-
$return = $backup;
627+
private static function get_format_merge_tag_modifier_value( $modifier, $backup = '' ) {
628+
preg_match( '/(?:^|:)format:(.*)/', $modifier, $match );
628629

629-
$format_key_index = array_search( 'format', $exploded );
630-
631-
// If there's a "format:[php date format string]" date format, grab it
632-
if ( false !== $format_key_index && isset( $exploded[ $format_key_index + 1 ] ) ) {
633-
// Return escaped colons placeholder
634-
$return = str_replace( '|COLON|', ':', implode( ':', array_slice( $exploded, $format_key_index + 1 ) ) );
635-
}
636-
637-
return $return;
630+
return isset( $match[1] ) ? str_replace( '\:', ':', $match[1] ) : $backup;
638631
}
639632

640633
/**

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h
2626
* Fixed: When searching a View, the searched time zone would not appear as selected in the Search Bar.
2727
* Fixed: Fields added to the View could not be configured and would disappear after saving the View when Multiple Forms was enabled.
2828
* Fixed: Fatal error on the Edit Entry screen when Multiple Forms is enabled.
29+
* Fixed: The ``:format` merge tag modifier on the Time field returned a UTC-adjusted time value.
2930

3031
= 2.26 on August 8, 2024 =
3132

0 commit comments

Comments
 (0)