| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| class GW_Format_Date_Merge_Tag {
|
|
|
| private $_args = array();
|
|
|
| public function __construct( $args = array() ) {
|
|
|
|
|
| $this->_args = wp_parse_args( $args, array(
|
| 'form_id' => false,
|
| 'field_id' => false,
|
| 'locale' => null,
|
| ) );
|
|
|
|
|
| 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 );
|
|
|
|
|
| $modifier = htmlspecialchars_decode( $modifier );
|
|
|
|
|
|
|
| $modifier = str_replace( ',', ',', $modifier );
|
|
|
| $timestamp = strtotime( sprintf( '%d-%d-%d', $parsed_date['year'], $parsed_date['month'], $parsed_date['day'] ) );
|
|
|
|
|
| $replace = wp_date( $modifier, $timestamp, new DateTimeZone( 'UTC' ) );
|
|
|
| $text = str_replace( $match[0], $replace, $text );
|
| }
|
|
|
|
|
| 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'];
|
| }
|
| }
|
|
|
|
|
|
|
| new GW_Format_Date_Merge_Tag();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
| |
Comments