|
2 | 2 | /** |
3 | 3 | * Gravity Wiz // Shortcode: Entry Count // Display a Filterable Entry Count |
4 | 4 | * https://gravitywiz.com/ |
| 5 | + * |
| 6 | + * This shortcode displays the count of entries for a specified Gravity Form, with various filtering options. |
| 7 | + * |
| 8 | + * Shortcode Attributes: |
| 9 | + * - id (int): The ID of the Gravity Form. |
| 10 | + * - field_id (int|string): The ID of the field to filter by. |
| 11 | + * - value (mixed): The value to filter the field by. Supports boolean values ('true', 'false') and merge tags. |
| 12 | + * - field_filters (string): A comma-separated list of field filters in the format field_id:operator:value. |
| 13 | + * - field_filters_mode (string): The mode for field filters. Accepts 'all' or 'any'. Default is 'all'. |
| 14 | + * - format (string): The format for the output number. Accepts 'decimal' or 'comma'. |
| 15 | + * - start_date (string): The start date for filtering entries. |
| 16 | + * - end_date (string): The end date for filtering entries. |
| 17 | + * - current_user (bool): Whether to filter entries by the current user. |
| 18 | + * - display_min (int): The minimum number of entries to display. |
| 19 | + * - display_min_alt_text (string): The text to display if the entry count is less than display_min. |
| 20 | + * |
| 21 | + * Example Usage: |
| 22 | + * [gform_shortcode_entry_count id="1" field_id="2" value="example" field_filters="3:is:example,4:>5" field_filters_mode="any" format="comma" start_date="2023-01-01" end_date="2023-12-31" current_user="true" display_min="10" display_min_alt_text="Less than 10 entries"] |
5 | 23 | */ |
6 | 24 | add_filter( 'gform_shortcode_entry_count', function( $output, $atts ) { |
7 | 25 |
|
8 | 26 | $atts = shortcode_atts( array( |
9 | 27 | 'id' => false, |
10 | 28 | 'field_id' => false, |
11 | 29 | 'value' => false, |
| 30 | + 'field_filters' => array(), // Format: field_id:operator:value,field_id:operator:value |
| 31 | + 'field_filters_mode' => 'all', // Accepts: all|any |
12 | 32 | 'format' => false, |
13 | 33 | 'start_date' => false, |
14 | 34 | 'end_date' => false, |
|
33 | 53 | 'status' => 'active', |
34 | 54 | ); |
35 | 55 |
|
36 | | - if ( $atts['field_id'] ) { |
| 56 | + if ( $atts['field_filters'] ) { |
| 57 | + |
| 58 | + $args['field_filters'] = array(); |
| 59 | + |
| 60 | + $filter_pairs = explode(',', $atts['field_filters']); // Split by comma |
| 61 | + foreach ($filter_pairs as $pair) { |
| 62 | + $parts = explode(':', $pair); |
| 63 | + |
| 64 | + if (count($parts) === 3) { |
| 65 | + list($field_id, $operator, $value) = $parts; |
| 66 | + } elseif (count($parts) === 2) { |
| 67 | + // Default to "is" if operator is missing |
| 68 | + list($field_id, $value) = $parts; |
| 69 | + $operator = 'is'; |
| 70 | + } else { |
| 71 | + // Invalid format, skip |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + // Ensure values are trimmed |
| 76 | + $args['field_filters'][] = [ |
| 77 | + 'key' => trim($field_id), |
| 78 | + 'operator' => trim($operator), |
| 79 | + 'value' => trim($value) |
| 80 | + ]; |
| 81 | + |
| 82 | + $args['field_filters']['mode'] = rgar( $atts, 'field_filters_mode' ); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + } else if ( $atts['field_id'] ) { |
37 | 87 | $args['field_filters'] = array( |
38 | 88 | array( |
39 | 89 | 'key' => $atts['field_id'], |
|
0 commit comments