Skip to content

Commit

Permalink
Adds support for array fields as in sub fields (#2187)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcasual authored Dec 19, 2024
2 parents 89fa414 + ddc6b3b commit 473efbb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 36 deletions.
80 changes: 61 additions & 19 deletions includes/class-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,46 @@ public static function check_entry_display( $entry, $view = null ) {
return $entry;
}

/**
* Formats date without applying site's timezone. This is a copy of {@see GFCommon::format_date()}.
*
* @since TBD
*
* @param string $gmt_datetime The UTC date/time value to be formatted.
* @param bool $is_human Indicates if a human-readable time difference such as "1 hour ago" should be returned when within 24hrs of the current time. Defaults to true.
* @param string $date_format The format the value should be returned in. Defaults to an empty string; the date format from the WordPress general settings, if configured, or Y-m-d.
* @param bool $include_time Indicates if the time should be included in the returned string. Defaults to true; the time format from the WordPress general settings, if configured, or H:i.
*
* @return string
*
*/
public static function format_date_without_timezone_offset( $gmt_datetime, $is_human = true, $date_format = '', $include_time = true ) {
if ( empty( $gmt_datetime ) ) {
return '';
}

$gmt_time = mysql2date( 'G', $gmt_datetime );

if ( $is_human ) {
$time_diff = time() - $gmt_time;

if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
return sprintf( esc_html__( '%s ago', 'gravityview' ), human_time_diff( $gmt_time ) );
}
}

if ( empty( $date_format ) ) {
$date_format = GFCommon::get_default_date_format();
}

if ( $include_time ) {
$time_format = GFCommon::get_default_time_format();

return sprintf( esc_html__( '%1$s at %2$s', 'gravityview' ), date_i18n( $date_format, $gmt_time, true ), date_i18n( $time_format, $gmt_time, true ) );
}

return date_i18n( $date_format, $gmt_time, true );
}

/**
* Allow formatting date and time based on GravityView standards
Expand All @@ -1179,34 +1219,34 @@ public static function check_entry_display( $entry, $view = null ) {
* @return int|null|string Formatted date based on the original date
*/
public static function format_date( $date_string = '', $args = array() ) {

$default_atts = array(
'raw' => false,
'timestamp' => false,
'diff' => false,
'human' => false,
'format' => '',
'time' => false,
);
$default_atts = [
'raw' => false,
'timestamp' => false,
'diff' => false,
'human' => false,
'format' => '',
'time' => false,
'no_tz_offset' => false,
];

$atts = wp_parse_args( $args, $default_atts );

/**
* Gravity Forms code to adjust date to locally-configured Time Zone
* Gravity Forms code to adjust date to locally-configured timezone.
*
* @see GFCommon::format_date() for original code
*/
$date_gmt_time = mysql2date( 'G', $date_string );
$date_local_timestamp = GFCommon::get_local_timestamp( $date_gmt_time );

$format = \GV\Utils::get( $atts, 'format' );
$is_human = ! empty( $atts['human'] );
$is_diff = ! empty( $atts['diff'] );
$is_raw = ! empty( $atts['raw'] );
$is_timestamp = ! empty( $atts['timestamp'] );
$include_time = ! empty( $atts['time'] );
$time_diff = strtotime( $date_string ) - current_time( 'timestamp' );

$format = \GV\Utils::get( $atts, 'format' );
$is_human = ! empty( $atts['human'] );
$is_diff = ! empty( $atts['diff'] );
$is_raw = ! empty( $atts['raw'] );
$is_timestamp = ! empty( $atts['timestamp'] );
$include_time = ! empty( $atts['time'] );
$no_tz_offset = ! empty( $atts['no_tz_offset'] );
$time_diff = strtotime( $date_string ) - current_time( 'timestamp' );

// If we're using time diff, we want to have a different default format
if ( empty( $format ) ) {
Expand All @@ -1223,11 +1263,13 @@ public static function format_date( $date_string = '', $args = array() ) {
$formatted_date = $date_local_timestamp;
} elseif ( $is_diff ) {
$formatted_date = sprintf( $format, human_time_diff( $date_gmt_time, current_time( 'timestamp' ) ) );
} elseif ( $no_tz_offset ) {
$formatted_date = self::format_date_without_timezone_offset( $date_string, $is_human, $format, $include_time );
} else {
$formatted_date = GFCommon::format_date( $date_string, $is_human, $format, $include_time );
}

unset( $format, $is_diff, $is_human, $is_timestamp, $is_raw, $date_gmt_time, $date_local_timestamp, $default_atts );
unset( $format, $is_diff, $is_human, $is_timestamp, $no_tz_offset, $is_raw, $date_gmt_time, $date_local_timestamp, $default_atts );

return $formatted_date;
}
Expand Down
32 changes: 16 additions & 16 deletions includes/class-gravityview-merge-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ public function cache_merge_tag_modifiers( $text ) {
* @return string If no modifiers passed, $raw_value is not a string, or {all_fields} Merge Tag is used, original value. Otherwise, output from modifier methods.
*/
public static function process_modifiers( $value, $merge_tag, $modifier, $field, $raw_value ) {

// Process array value for sub fields like name and address.
if ( is_array( $raw_value ) && isset( $raw_value[ $merge_tag ] ) ) {
// Process array value for sub-fields like name and address.
if ( $raw_value[ $merge_tag ] ?? null ) {
$raw_value = $raw_value[ $merge_tag ];
}

Expand Down Expand Up @@ -125,16 +124,13 @@ public static function process_modifiers( $value, $merge_tag, $modifier, $field,
$unserialized = maybe_unserialize( $raw_value );

if ( method_exists( $field, 'get_value_merge_tag' ) && is_array( $unserialized ) ) {

$non_gv_modifiers = array_diff( $modifiers, array_keys( $gv_modifiers ) );

$return = $field->get_value_merge_tag( $value, '', array( 'currency' => '' ), array(), implode( '', $non_gv_modifiers ), $raw_value, false, false, 'text', false );
}

foreach ( $modifiers as $passed_modifier ) {

foreach ( $gv_modifiers as $gv_modifier => $method ) {

// Uses ^ to only match the first modifier, to enforce same order as passed by GF
preg_match( '/^' . $gv_modifier . '/ism', $passed_modifier, $matches );

Expand All @@ -143,7 +139,8 @@ public static function process_modifiers( $value, $merge_tag, $modifier, $field,
}

// The called method is passed the raw value and the full matches array
$return = self::$method( $return, $matches, $value, $field, $passed_modifier );
$return = self::$method( $return, $matches, $value, $field, $passed_modifier, $merge_tag );

break;
}
}
Expand Down Expand Up @@ -206,16 +203,18 @@ public static function modifier_human( $raw_value, $matches, $value = '', $field
* Converts date and time values to the format modifier.
*
* @since 2.26
* @since TBD Added $merge_tag parameter.
*
* @param string $raw_value
* @param array $matches
* @param string $value
* @param array $field
* @param string $modifier
* @param string $merge_tag
*
* @return string
*/
private static function modifier_format( $raw_value, $matches, $value, $field, $modifier ) {
private static function modifier_format( $raw_value, $matches, $value, $field, $modifier, $merge_tag = '' ) {
$format = self::get_format_merge_tag_modifier_value( $modifier );

if ( ! $format ) {
Expand All @@ -230,7 +229,7 @@ private static function modifier_format( $raw_value, $matches, $value, $field, $
return self::format_date( $raw_value, $modifier );
}

return $raw_value;
return apply_filters( 'gravityview/merge_tags/modifiers/format', $raw_value, $format, $field, $modifier, $merge_tag );
}

/**
Expand Down Expand Up @@ -419,7 +418,7 @@ private static function modifier_strings( $raw_value, $matches, $value = '', $fi
* @since TBD
*
* @param string $raw_value The full name or string to convert.
*
*
* @return string The initials.
*/
public static function modifier_initials( $raw_value ) {
Expand Down Expand Up @@ -701,12 +700,13 @@ public static function format_date( $date_or_time_string = '', $modifier = '' )
$parsed_modifier = explode( ':', $modifier );

$atts = [
'format' => self::get_format_merge_tag_modifier_value( $modifier, false ),
'human' => in_array( 'human', $parsed_modifier ), // {date_created:human}
'diff' => in_array( 'diff', $parsed_modifier ), // {date_created:diff}
'raw' => in_array( 'raw', $parsed_modifier ), // {date_created:raw}
'timestamp' => in_array( 'timestamp', $parsed_modifier ), // {date_created:timestamp}
'time' => in_array( 'time', $parsed_modifier ), // {date_created:time}
'format' => self::get_format_merge_tag_modifier_value( $modifier, false ),
'human' => in_array( 'human', $parsed_modifier ), // {date_created:human}
'diff' => in_array( 'diff', $parsed_modifier ), // {date_created:diff}
'raw' => in_array( 'raw', $parsed_modifier ), // {date_created:raw}
'timestamp' => in_array( 'timestamp', $parsed_modifier ), // {date_created:timestamp}
'time' => in_array( 'time', $parsed_modifier ), // {date_created:time}
'no_tz_offset' => in_array( 'no_tz_offset', $parsed_modifier ), // {date_created:no_tz_offset}
];

return GVCommon::format_date( $date_or_time_string, $atts );
Expand Down
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h

#### 🚀 Added
* New Layout Builder View type for creating custom layouts with single or multi-column configurations and adjustable widths.
* `:initials` merge tag modifier for Name fields to display initials.
* Support for Source ID meta added in Gravity Forms 2.9
* `:initials` merge tag modifier for Name fields to display initials.
* `:format` merge tag modifier for field inputs (e.g., `{Event Field:1.1:format:Y-m-d}`).

#### 🐛 Fixed
* Merge tags in redirect URLs were not processed after editing or deleting an entry in the lightbox.
Expand Down

0 comments on commit 473efbb

Please sign in to comment.