Created
September 4, 2025 16:10
-
-
Save saifsultanc/3c8327e985054fdb49f21618c51eb8b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action( 'gform_after_update_entry', function ( $form, $entry_id ) { | |
| $gppa_lmt = GP_Populate_Anything_Live_Merge_Tags::get_instance(); | |
| $entry = GFAPI::get_entry( $entry_id ); | |
| foreach ( $form['fields'] as $field ) { | |
| // For any field having Live Merge Tags. | |
| if ( $gppa_lmt->has_live_merge_tag( $field->defaultValue ) ) { | |
| $gppa_lmt->populate_lmt_whitelist( $form ); | |
| remove_all_filters('gform_pre_replace_merge_tags'); | |
| // Process the Live Merge Tags. | |
| $merge_tag = preg_replace( '/@(?=\{)/', '', $field->defaultValue ); | |
| $value = GFCommon::replace_variables( $merge_tag, $form, $entry ); | |
| $instance = new GW_Format_Date_Merge_Tag(); | |
| $value = $instance->replace_merge_tags( $merge_tag, $form, $entry, false, false, false, 'text' ); | |
| GFFormsModel::update_entry_field_value( $form, $entry, $field, '', $field->id, $value ); | |
| } | |
| } | |
| }, 15, 2 ); | |
| class GW_Format_Date_Merge_Tag { | |
| private $_args = array(); | |
| public function __construct( $args = array() ) { | |
| // set our default arguments, parse against the provided arguments, and store for use throughout the class | |
| $this->_args = wp_parse_args( $args, array( | |
| 'form_id' => false, | |
| 'field_id' => false, | |
| 'locale' => null, | |
| ) ); | |
| // do version check in the init to make sure if GF is going to be loaded, it is already loaded | |
| add_action( 'init', array( $this, 'init' ) ); | |
| } | |
| public function init() { | |
| add_filter( 'gform_pre_replace_merge_tags', array( $this, 'replace_merge_tags' ), 10, 7 ); | |
| } | |
| public function replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) { | |
| if ( ! $this->is_applicable_form( $form ) ) { | |
| return $text; | |
| } | |
| $current_locale = determine_locale(); | |
| $locale = $this->_args['locale']; | |
| if ( $locale ) { | |
| switch_to_locale( $locale ); | |
| } | |
| preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER ); | |
| foreach ( $matches as $match ) { | |
| $input_id = $match[1]; | |
| $field = GFFormsModel::get_field( $form, $input_id ); | |
| if ( ! $field || $field->get_input_type() !== 'date' ) { | |
| continue; | |
| } | |
| $i = $match[0][0] === '{' ? 4 : 5; | |
| $modifier = rgar( array_map( 'trim', explode( ',', rgar( $match, $i ) ) ), 0 ); | |
| if ( ! $modifier ) { | |
| continue; | |
| } | |
| $value = GFFormsModel::get_lead_field_value( $entry, $field ); | |
| $value = $field->get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $value, $url_encode, $esc_html, $format, $nl2br ); | |
| if ( ! $value ) { | |
| continue; | |
| } | |
| $format = $field->dateFormat ? $field->dateFormat : 'mdy'; | |
| $parsed_date = GFCommon::parse_date( $value, $format ); | |
| // On the Notifications/Confirmation side, & gets encoded to &. Decode it back. | |
| $modifier = htmlspecialchars_decode( $modifier ); | |
| // For whatever reason, Populate Anything's LMTs works better with `&comma` than `,`. But... date() doesn't | |
| // like it so let's replace it before we pass it to date(). | |
| $modifier = str_replace( ',', ',', $modifier ); | |
| $timestamp = strtotime( sprintf( '%d-%d-%d', $parsed_date['year'], $parsed_date['month'], $parsed_date['day'] ) ); | |
| // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date | |
| $replace = wp_date( $modifier, $timestamp, new DateTimeZone( 'UTC' ) ); | |
| $text = str_replace( $match[0], $replace, $text ); | |
| } | |
| // Switch back to default locale. | |
| if ( $locale ) { | |
| switch_to_locale( $current_locale ); | |
| } | |
| return $text; | |
| } | |
| public function is_applicable_form( $form ) { | |
| $form_id = isset( $form['id'] ) ? $form['id'] : $form; | |
| return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id']; | |
| } | |
| } | |
| # Configuration | |
| new GW_Format_Date_Merge_Tag(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment