Skip to content

Commit e4d5747

Browse files
committed
gw-format-date-merge-tags.php: Added i18n date format support.
1 parent 4425281 commit e4d5747

File tree

1 file changed

+81
-30
lines changed

1 file changed

+81
-30
lines changed

gravity-forms/gw-format-date-merge-tags.php

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,101 @@
99
* Plugin URI: https://gravitywiz.com/gravity-forms-date-merge-tags/
1010
* Description: Adds merge tag modifiers for formatting date merge tags using PHP Date Formats.
1111
* Author: Gravity Wiz
12-
* Version: 0.4
12+
* Version: 0.5
1313
* Author URI: https://gravitywiz.com
1414
*/
15-
add_filter( 'gform_pre_replace_merge_tags', function( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
15+
class GW_Format_Date_Merge_Tag {
1616

17-
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER );
17+
private $_args = array();
1818

19-
foreach ( $matches as $match ) {
19+
public function __construct( $args = array() ) {
2020

21-
$input_id = $match[1];
22-
$field = GFFormsModel::get_field( $form, $input_id );
21+
// set our default arguments, parse against the provided arguments, and store for use throughout the class
22+
$this->_args = wp_parse_args( $args, array(
23+
'form_id' => false,
24+
'field_id' => false,
25+
'locale' => null,
26+
) );
2327

24-
if ( ! $field || $field->get_input_type() !== 'date' ) {
25-
continue;
26-
}
28+
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
29+
add_action( 'init', array( $this, 'init' ) );
30+
}
2731

28-
$i = $match[0][0] === '{' ? 4 : 5;
29-
$modifier = rgar( array_map( 'trim', explode( ',', rgar( $match, $i ) ) ), 0 );
30-
if ( ! $modifier ) {
31-
continue;
32-
}
32+
public function init() {
33+
add_filter( 'gform_pre_replace_merge_tags', array( $this, 'replace_merge_tags' ), 10, 7 );
34+
}
3335

34-
$value = GFFormsModel::get_lead_field_value( $entry, $field );
35-
$value = $field->get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $value, $url_encode, $esc_html, $format, $nl2br );
36-
if ( ! $value ) {
37-
continue;
36+
public function replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
37+
$current_locale = determine_locale();
38+
$locale = $this->_args['locale'];
39+
if ( $locale ) {
40+
switch_to_locale( $locale );
3841
}
3942

40-
$format = $field->dateFormat ? $field->dateFormat : 'mdy';
41-
$parsed_date = GFCommon::parse_date( $value, $format );
43+
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER );
44+
45+
foreach ( $matches as $match ) {
46+
47+
$input_id = $match[1];
48+
$field = GFFormsModel::get_field( $form, $input_id );
49+
50+
if ( ! $field || $field->get_input_type() !== 'date' ) {
51+
continue;
52+
}
53+
54+
$i = $match[0][0] === '{' ? 4 : 5;
55+
$modifier = rgar( array_map( 'trim', explode( ',', rgar( $match, $i ) ) ), 0 );
56+
if ( ! $modifier ) {
57+
continue;
58+
}
59+
60+
$value = GFFormsModel::get_lead_field_value( $entry, $field );
61+
$value = $field->get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $value, $url_encode, $esc_html, $format, $nl2br );
62+
if ( ! $value ) {
63+
continue;
64+
}
65+
66+
$format = $field->dateFormat ? $field->dateFormat : 'mdy';
67+
$parsed_date = GFCommon::parse_date( $value, $format );
4268

43-
// On the Notifications/Confirmation side, & gets encoded to &. Decode it back.
44-
$modifier = htmlspecialchars_decode( $modifier );
69+
// On the Notifications/Confirmation side, & gets encoded to &. Decode it back.
70+
$modifier = htmlspecialchars_decode( $modifier );
4571

46-
// For whatever reason, Populate Anything's LMTs works better with `&comma` than `,`. But... date() doesn't
47-
// like it so let's replace it before we pass it to date().
48-
$modifier = str_replace( ',', ',', $modifier );
72+
// For whatever reason, Populate Anything's LMTs works better with `&comma` than `,`. But... date() doesn't
73+
// like it so let's replace it before we pass it to date().
74+
$modifier = str_replace( ',', ',', $modifier );
4975

50-
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
51-
$replace = date( $modifier, strtotime( sprintf( '%d-%d-%d', $parsed_date['year'], $parsed_date['month'], $parsed_date['day'] ) ) );
76+
$timestamp = strtotime( sprintf( '%d-%d-%d', $parsed_date['year'], $parsed_date['month'], $parsed_date['day'] ) );
5277

53-
$text = str_replace( $match[0], $replace, $text );
78+
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
79+
$replace = wp_date( $modifier, $timestamp );
5480

81+
$text = str_replace( $match[0], $replace, $text );
82+
}
83+
84+
// Switch back to default locale.
85+
if ( $locale ) {
86+
switch_to_locale( $current_locale );
87+
}
88+
89+
return $text;
5590
}
5691

57-
return $text;
58-
}, 10, 7 );
92+
public function is_applicable_form( $form ) {
93+
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
94+
95+
return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id'];
96+
}
97+
}
98+
99+
# Configuration
100+
101+
new GW_Format_Date_Merge_Tag( array(
102+
'form_id' => 123,
103+
) );
104+
105+
# Configuration with locale enabled.
106+
// new GW_Format_Date_Merge_Tag( array(
107+
// 'form_id' => 123,
108+
// 'locale' => 'fr_FR',
109+
// ) );

0 commit comments

Comments
 (0)