|
13 | 13 | * Description: Adds {query_param:PARAM} merge tags for pulling values from query/GET params. |
14 | 14 | * Author: Gravity Wiz |
15 | 15 | * Version: 0.1 |
16 | | - * Author URI: http://gravitywiz.com |
| 16 | + * Author URI: https://gravitywiz.com |
17 | 17 | */ |
18 | 18 | add_filter( 'gform_replace_merge_tags', function( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) { |
19 | 19 | preg_match_all( '/\{query_param:(.*?)\}/', $text, $dyn_pop_matches, PREG_SET_ORDER ); |
|
23 | 23 | $full_tag = $match[0]; |
24 | 24 | $modifier = $match[1]; |
25 | 25 |
|
26 | | - $value = sanitize_text_field( esc_sql( rgget( $modifier ) ) ); |
| 26 | + $value = sanitize_text_field( esc_sql( gw_get_get_query_param( $modifier ) ) ); |
27 | 27 |
|
28 | 28 | $text = str_replace( $full_tag, $url_encode ? urlencode( $value ) : $value, $text ); |
29 | 29 | } |
30 | 30 | } |
31 | 31 |
|
32 | 32 | return $text; |
33 | 33 | }, 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