Skip to content

Commit

Permalink
Make case sensitive modifiers great again
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcasual committed Aug 7, 2024
1 parent 76819b8 commit 2930ecf
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion includes/class-gravityview-merge-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
* @since 1.8.4
*/
class GravityView_Merge_Tags {
/**
* Microcache for merge tag modifiers.
*
* @since TBD
*
* @var array[]
*/
private static $merge_tag_modifiers = [];

/**
* @since 1.8.4
Expand All @@ -20,12 +28,44 @@ public function __construct() {
* @since 1.8.4
*/
private function add_hooks() {

/** @see GFCommon::replace_variables_prepopulate */
add_filter( 'gform_replace_merge_tags', array( 'GravityView_Merge_Tags', 'replace_gv_merge_tags' ), 10, 7 );

// Process after 10 priority
add_filter( 'gform_merge_tag_filter', array( 'GravityView_Merge_Tags', 'process_modifiers' ), 20, 5 );

add_filter( 'gform_pre_replace_merge_tags', [ $this, 'cache_merge_tag_modifiers' ] );
}

/**
* Caches merge tag modifiers to preserve their case sensitivity.
* This is necessary because {@see GFCommon::replace_field_variable()) applies
* `strtolower()` to the modifier and causing issues where case is expexted,
* such as date formatting (e.g., `format:Y-m-d`).
*
* @since TBD
*
* @param string $text Text with merge tags.
*
* @return string
*/
public function cache_merge_tag_modifiers( $text ) {
// Regex pattern taken from GFCommon::replace_variables().
preg_match_all( '/{[^{]*?:(\d+(\.\w+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER );

if ( ! $matches ) {
return $text;
}

foreach ( $matches as $match ) {
$modifier = $match[4] ?? '';

if ( $modifier ) {
self::$merge_tag_modifiers[ strtolower( $modifier ) ] = $modifier;
}
}

return $text;
}

/**
Expand All @@ -50,6 +90,9 @@ public static function process_modifiers( $value, $merge_tag, $modifier, $field,
return $value;
}

// Retrieve the original case-sensitive modifier.
$modifier = self::$merge_tag_modifiers[ strtolower( $modifier ) ] ?? $modifier;

// matching regex => the value is the method to call to replace the value.
$gv_modifiers = array(
'maxwords:(\d+)' => 'modifier_maxwords',
Expand Down

0 comments on commit 2930ecf

Please sign in to comment.