Skip to content

Commit 4957c1b

Browse files
authored
gw-query-param-merge-tag.php: Added supported for targeting complex query params like filters[1]['foo'].
1 parent df43d15 commit 4957c1b

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

gravity-forms/gw-query-param-merge-tag.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Description: Adds {query_param:PARAM} merge tags for pulling values from query/GET params.
1414
* Author: Gravity Wiz
1515
* Version: 0.1
16-
* Author URI: http://gravitywiz.com
16+
* Author URI: https://gravitywiz.com
1717
*/
1818
add_filter( 'gform_replace_merge_tags', function( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
1919
preg_match_all( '/\{query_param:(.*?)\}/', $text, $dyn_pop_matches, PREG_SET_ORDER );
@@ -23,11 +23,52 @@
2323
$full_tag = $match[0];
2424
$modifier = $match[1];
2525

26-
$value = sanitize_text_field( esc_sql( rgget( $modifier ) ) );
26+
$value = sanitize_text_field( esc_sql( gw_get_get_query_param( $modifier ) ) );
2727

2828
$text = str_replace( $full_tag, $url_encode ? urlencode( $value ) : $value, $text );
2929
}
3030
}
3131

3232
return $text;
3333
}, 10, 7 );
34+
35+
if ( ! function_exists( 'gw_get_query_param' ) ) :
36+
/**
37+
* Get a query parameter value from $_GET when the key may use
38+
* array syntax like filters[53][foo], possibly URL-encoded.
39+
*
40+
* Examples:
41+
* gw_get_get_query_param( 'filters[53]' );
42+
* gw_get_get_query_param( 'filters%5B53%5D' );
43+
* gw_get_get_query_param( 'filters[53][foo]' );
44+
*/
45+
function gw_get_get_query_param( $key ) {
46+
47+
// Normalize the incoming key using WP helpers.
48+
$key = wp_unslash( $key ); // In case it came from a request.
49+
$key = urldecode( $key ); // Decode URL-encoded names like filters%5B53%5D.
50+
$key = trim( $key, "\"'" ); // Strip surrounding quotes.
51+
52+
// Work with an unslashed copy of $_GET.
53+
$get = wp_unslash( $_GET );
54+
55+
// No array syntax – return directly.
56+
if ( strpos( $key, '[' ) === false ) {
57+
return isset( $get[ $key ] ) ? $get[ $key ] : null;
58+
}
59+
60+
// Convert "filters[53][foo]" → [ 'filters', '53', 'foo' ].
61+
$parts = preg_split( '/\[|\]/', $key, -1, PREG_SPLIT_NO_EMPTY );
62+
63+
$value = $get;
64+
65+
foreach ( $parts as $part ) {
66+
if ( ! is_array( $value ) || ! array_key_exists( $part, $value ) ) {
67+
return null;
68+
}
69+
$value = $value[ $part ];
70+
}
71+
72+
return $value;
73+
}
74+
endif;

0 commit comments

Comments
 (0)