Skip to content

Commit d6a1900

Browse files
committed
Added support for filtering by multiple field values (via field_filters and field_filters_mode attributes).
1 parent c06034e commit d6a1900

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

gravity-forms/gw-entry-count-shortcode-advanced.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,33 @@
22
/**
33
* Gravity Wiz // Shortcode: Entry Count // Display a Filterable Entry Count
44
* 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"]
523
*/
624
add_filter( 'gform_shortcode_entry_count', function( $output, $atts ) {
725

826
$atts = shortcode_atts( array(
927
'id' => false,
1028
'field_id' => false,
1129
'value' => false,
30+
'field_filters' => array(), // Format: field_id:operator:value,field_id:operator:value
31+
'field_filters_mode' => 'all', // Accepts: all|any
1232
'format' => false,
1333
'start_date' => false,
1434
'end_date' => false,
@@ -33,7 +53,37 @@
3353
'status' => 'active',
3454
);
3555

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'] ) {
3787
$args['field_filters'] = array(
3888
array(
3989
'key' => $atts['field_id'],

0 commit comments

Comments
 (0)