|
9 | 9 | * Plugin URI: https://gravitywiz.com/gravity-forms-date-merge-tags/ |
10 | 10 | * Description: Adds merge tag modifiers for formatting date merge tags using PHP Date Formats. |
11 | 11 | * Author: Gravity Wiz |
12 | | - * Version: 0.4 |
| 12 | + * Version: 0.5 |
13 | 13 | * Author URI: https://gravitywiz.com |
14 | 14 | */ |
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 { |
16 | 16 |
|
17 | | - preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER ); |
| 17 | + private $_args = array(); |
18 | 18 |
|
19 | | - foreach ( $matches as $match ) { |
| 19 | + public function __construct( $args = array() ) { |
20 | 20 |
|
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 | + ) ); |
23 | 27 |
|
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 | + } |
27 | 31 |
|
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 | + } |
33 | 35 |
|
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 | + } |
39 | 49 |
|
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 | + } |
42 | 55 |
|
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 | + } |
45 | 61 |
|
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 ); |
49 | 64 |
|
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 ); |
52 | 67 |
|
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 ); |
54 | 71 |
|
| 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; |
55 | 93 | } |
56 | 94 |
|
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