Skip to content

Commit 4502624

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

File tree

1 file changed

+80
-31
lines changed

1 file changed

+80
-31
lines changed

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

Lines changed: 80 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,99 @@
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;
38-
}
36+
public function replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
37+
$locale = $this->_args['locale'];
38+
39+
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER );
40+
41+
foreach ( $matches as $match ) {
42+
43+
$input_id = $match[1];
44+
$field = GFFormsModel::get_field( $form, $input_id );
45+
46+
if ( ! $field || $field->get_input_type() !== 'date' ) {
47+
continue;
48+
}
3949

40-
$format = $field->dateFormat ? $field->dateFormat : 'mdy';
41-
$parsed_date = GFCommon::parse_date( $value, $format );
50+
$i = $match[0][0] === '{' ? 4 : 5;
51+
$modifier = rgar( array_map( 'trim', explode( ',', rgar( $match, $i ) ) ), 0 );
52+
if ( ! $modifier ) {
53+
continue;
54+
}
4255

43-
// On the Notifications/Confirmation side, & gets encoded to &. Decode it back.
44-
$modifier = htmlspecialchars_decode( $modifier );
56+
$value = GFFormsModel::get_lead_field_value( $entry, $field );
57+
$value = $field->get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $value, $url_encode, $esc_html, $format, $nl2br );
58+
if ( ! $value ) {
59+
continue;
60+
}
4561

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 );
62+
$format = $field->dateFormat ? $field->dateFormat : 'mdy';
63+
$parsed_date = GFCommon::parse_date( $value, $format );
4964

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'] ) ) );
65+
// On the Notifications/Confirmation side, & gets encoded to &. Decode it back.
66+
$modifier = htmlspecialchars_decode( $modifier );
5267

53-
$text = str_replace( $match[0], $replace, $text );
68+
// For whatever reason, Populate Anything's LMTs works better with `&comma` than `,`. But... date() doesn't
69+
// like it so let's replace it before we pass it to date().
70+
$modifier = str_replace( ',', ',', $modifier );
5471

72+
$timestamp = strtotime( sprintf( '%d-%d-%d', $parsed_date['year'], $parsed_date['month'], $parsed_date['day'] ) );
73+
74+
if ( $locale ) {
75+
$formatter = datefmt_create(
76+
$locale,
77+
IntlDateFormatter::FULL,
78+
IntlDateFormatter::FULL,
79+
null,
80+
null,
81+
$modifier
82+
);
83+
$replace = $formatter->format( $timestamp );
84+
} else {
85+
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
86+
$replace = date( $modifier, $timestamp );
87+
}
88+
89+
$text = str_replace( $match[0], $replace, $text );
90+
}
91+
92+
return $text;
5593
}
5694

57-
return $text;
58-
}, 10, 7 );
95+
public function is_applicable_form( $form ) {
96+
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
97+
98+
return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id'];
99+
}
100+
}
101+
102+
# Configuration
103+
104+
new GW_Format_Date_Merge_Tag( array(
105+
'form_id' => 123,
106+
'locale' => 'fr_FR', // When this option is enabled, use the ISO date format codes instead. See https://framework.zend.com/manual/1.12/en/zend.date.constants.html#zend.date.constants.selfdefinedformats
107+
) );

0 commit comments

Comments
 (0)