Last active
October 23, 2023 15:29
-
-
Save matty0501/3fae3f5a609a92855d28b350cf572f69 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Gravity Perks // Populate Anything // Range Modifier for Live Merge Tags | |
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/ | |
* | |
* Use the :range modifier with checkbox merge tags to get the range of checked options | |
* Works well with the :value modifier | |
* | |
* Will return `x - y` where the min and max are different, or `x` if they are the same | |
* | |
* # Return the range of checked values in field 4 | |
* @{Checkboxes:4:value,range} | |
* | |
*/ | |
add_filter( 'gppa_live_merge_tag_value', function( $value, $merge_tag ) { | |
$bits = explode( ':', $merge_tag ); | |
$modifiers = gw_parse_modifiers( array_pop( $bits ) ); | |
if ( ! isset( $modifiers['range'] ) ) { | |
return $value; | |
} | |
$bits = explode( ',', $value ); | |
$min = trim( min( $bits ) ); | |
$max = trim( max( $bits ) ); | |
if( $min == $max ){ | |
return $min; | |
} | |
return $min . ' - ' . $max; | |
}, 10, 2 ); | |
if ( ! function_exists( 'gw_parse_modifiers' ) ) { | |
function gw_parse_modifiers( $modifiers_str ) { | |
preg_match_all( '/([a-z]+)(?:(?:\[(.+?)\])|,?)/i', $modifiers_str, $modifiers, PREG_SET_ORDER ); | |
$parsed = array(); | |
foreach ( $modifiers as $modifier ) { | |
list( $match, $modifier, $value ) = array_pad( $modifier, 3, null ); | |
if ( $value === null ) { | |
$value = $modifier; | |
} | |
// Split '1,2,3' into array( 1, 2, 3 ). | |
if ( strpos( $value, ',' ) !== false ) { | |
$value = array_map( 'trim', explode( ',', $value ) ); | |
} | |
$parsed[ strtolower( $modifier ) ] = $value; | |
} | |
return $parsed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment