@@ -95,15 +95,10 @@ public static function process_modifiers( $value, $merge_tag, $modifier, $field,
95
95
96
96
// matching regex => the value is the method to call to replace the value.
97
97
$ 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 */
107
102
'wpautop ' => 'modifier_strings ' ,
108
103
'esc_html ' => 'modifier_strings ' ,
109
104
'sanitize_html_class ' => 'modifier_strings ' ,
@@ -113,7 +108,7 @@ public static function process_modifiers( $value, $merge_tag, $modifier, $field,
113
108
'ucfirst ' => 'modifier_strings ' ,
114
109
'ucwords ' => 'modifier_strings ' ,
115
110
'wptexturize ' => 'modifier_strings ' ,
116
- 'format ' => 'modifier_format ' ,
111
+ 'format ' => 'modifier_format ' , /** @see modifier_format */
117
112
);
118
113
119
114
$ modifiers = explode ( ', ' , $ modifier );
@@ -181,7 +176,17 @@ public static function process_modifiers( $value, $merge_tag, $modifier, $field,
181
176
* @return string
182
177
*/
183
178
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 ) {
185
190
return self ::format_date ( $ raw_value , $ modifier );
186
191
}
187
192
@@ -578,63 +583,51 @@ public static function replace_entry_link( $original_text, $form = array(), $ent
578
583
}
579
584
580
585
/**
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.
582
589
*
583
- * @uses GVCommon::format_date()
590
+ * @since 1.16
584
591
*
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()
587
594
*
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`)
590
597
*
591
598
* @return int|string If timestamp requested, timestamp int. Otherwise, string output.
592
599
*/
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 );
610
613
}
611
614
612
615
/**
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".
617
618
*
618
619
* @since 1.16
620
+ * @since TODO Renamed and refactored to use regex and instead of working with an array.
619
621
*
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.
622
624
*
623
625
* @return string If format is found, the passed format. Otherwise, the backup.
624
626
*/
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 );
628
629
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 ;
638
631
}
639
632
640
633
/**
0 commit comments