|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Gravity Wiz // Gravity Forms // Advanced Conditional Shortcodes |
| 3 | + * This snippet is now available as a free plugin with Spellbook. ✨ |
| 4 | + * Instructions for installing this plugin can be found here: |
4 | 5 | * https://gravitywiz.com/gravity-forms-advanced-conditional-shortcode/ |
5 | | - * |
6 | | - * Allows multiple conditions in a single Gravity Forms shortcode. |
7 | | - * |
8 | | - * @version 0.1 |
9 | | - * @author David Smith <[email protected]> |
10 | | - * @license GPL-2.0+ |
11 | | - * @link https://gravitywiz.com |
12 | | - * |
13 | | - * Plugin Name: Gravity Forms Advanced Conditional Shortcodes |
14 | | - * Plugin URI: http://gravitywiz.com |
15 | | - * Description: Allows multiple conditions in a single Gravity Forms shortcode. |
16 | | - * Author: Gravity Wiz |
17 | | - * Version: 0.1 |
18 | | - * Author URI: http://gravitywiz.com |
19 | | - * |
20 | 6 | */ |
21 | | -add_filter( 'gform_shortcode_conditional', function( $result, $atts, $content ) { |
22 | | - |
23 | | - if ( ! isset( $atts['value'] ) || isset( $atts['merge_tag'] ) ) { |
24 | | - return $result; |
25 | | - } |
26 | | - |
27 | | - $relation = strtolower( rgar( $atts, 'relation', 'all' ) ); // or 'any' |
28 | | - $conditions = array(); |
29 | | - |
30 | | - foreach ( $atts as $key => $prop ) { |
31 | | - |
32 | | - preg_match( '|value(\d*)$|', $key, $match ); |
33 | | - if ( ! empty( $match ) ) { |
34 | | - list( , $index ) = $match; |
35 | | - $conditions[] = array( |
36 | | - 'value' => rgar( $atts, sprintf( 'value%s', $index ) ), |
37 | | - 'operator' => rgar( $atts, sprintf( 'operator%s', $index ) ), |
38 | | - 'compare' => rgar( $atts, sprintf( 'compare%s', $index ) ), |
39 | | - ); |
40 | | - } |
41 | | - } |
42 | | - |
43 | | - $conditional_met = $relation == 'all'; |
44 | | - |
45 | | - foreach ( $conditions as $condition ) { |
46 | | - $is_match = GFFormsModel::matches_operation( $condition['value'], $condition['compare'], $condition['operator'] ); |
47 | | - if ( $relation == 'any' && $is_match ) { |
48 | | - $conditional_met = true; |
49 | | - break; |
50 | | - } elseif ( $relation == 'all' && ! $is_match ) { |
51 | | - $conditional_met = false; |
52 | | - break; |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - if ( ! $conditional_met ) { |
57 | | - return ''; |
58 | | - } |
59 | | - |
60 | | - // find and remove any starting/closing <br> tags |
61 | | - if ( rgar( $atts, 'format' ) != 'raw' ) { |
62 | | - $content = preg_replace( '/^<br(?: *\/)?>|<br(?: *\/)?>$/', '', $content ); |
63 | | - } |
64 | | - |
65 | | - return do_shortcode( $content ); |
66 | | -}, 10, 3 ); |
0 commit comments