-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgppa-aggregate-functions.php
192 lines (166 loc) · 6.74 KB
/
gppa-aggregate-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Gravity Perks // GP Populate Anything // Add Support for Aggregate Functions
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Perform calculations on the values in a field/column and return a single value.
* To use the snippet you'd use a custom template type and enter the merge tag manually adding the specific property
* (e.g. column, field) you want to target, like this: {sum:your_property}
*
* Merge Tags
* {sum:ID} - The SUM merge tag adds up all values in a specific column/field.
* {avg:ID} - The AVG merge tag adds up all values and then calculates the average.
* {min:ID} - The MIN merge tag finds the minimum value in a specific column/field.
* {max:ID} - The MAX merge tag finds the maximum value in a specific column/field.
*
* Plugin Name: GP Populate Anything – Aggregate Functions
* Plugin URI: https://gravitywiz.com/documentation/gravity-forms-populate-anything/
* Description: Perform calculations on the values in a field/column and return a single value.
* Author: Gravity Wiz
* Version: 0.2
* Author URI: https://gravitywiz.com/
*/
class GPPA_Aggregate_Functions {
public static $sum_regex = '/{sum:(.+)}/';
public static $avg_regex = '/{avg:(.+)}/';
public static $min_regex = '/{min:(.+)}/';
public static $max_regex = '/{max:(.+)}/';
private static $instance;
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// Required by GPPA 2.0+ to ensure that all results are returned.
add_filter( 'gppa_query_all_value_objects', array( $this, 'enable_query_all_value_objects' ), 10, 7 );
add_filter( 'gppa_process_template', array( $this, 'replace_template_sum_merge_tags' ), 2, 7 );
add_filter( 'gppa_process_template', array( $this, 'replace_template_avg_merge_tags' ), 2, 7 );
add_filter( 'gppa_process_template', array( $this, 'replace_template_min_merge_tags' ), 2, 7 );
add_filter( 'gppa_process_template', array( $this, 'replace_template_max_merge_tags' ), 2, 7 );
}
public function enable_query_all_value_objects( $query_all_value_objects, $field, $field_values, $object_type_instance, $filter_groups, $primary_property, $templates ) {
return $this->matches_any_merge_tag( rgar( $templates, 'value' ) );
}
public function matches_any_merge_tag( $template_value ) {
return preg_match( self::$sum_regex, $template_value ) || preg_match( self::$avg_regex, $template_value ) || preg_match( self::$min_regex, $template_value ) || preg_match( self::$max_regex, $template_value );
}
public function replace_template_sum_merge_tags( $template_value, $field, $template, $populate, $object, $object_type, $objects ) {
if ( ! is_string( $template_value ) ) {
return $template_value;
}
preg_match_all( self::$sum_regex, $template_value, $matches, PREG_SET_ORDER );
if ( $matches ) {
foreach ( $matches as $match ) {
$full_match = $match[0];
$merge_tag = str_replace( $object_type->id . ':', '', $match[1] );
$sum = 0;
foreach ( $objects as $object ) {
$value = $this->get_object_value( $merge_tag, $object, $object_type );
if ( is_numeric( $value ) ) {
$sum += floatval( $value );
}
}
$template_value = str_replace( $full_match, $sum, $template_value );
}
}
return $template_value;
}
public function replace_template_avg_merge_tags( $template_value, $field, $template, $populate, $object, $object_type, $objects ) {
if ( ! is_string( $template_value ) ) {
return $template_value;
}
preg_match_all( self::$avg_regex, $template_value, $matches, PREG_SET_ORDER );
if ( $matches ) {
foreach ( $matches as $match ) {
$full_match = $match[0];
$merge_tag = str_replace( $object_type->id . ':', '', $match[1] );
$sum = 0;
$count = count( $objects );
foreach ( $objects as $object ) {
$value = $this->get_object_value( $merge_tag, $object, $object_type );
if ( is_numeric( $value ) ) {
$sum += floatval( $value );
}
}
$avg = ( $count > 0 ) ? ( $sum / $count ) : 0;
$template_value = str_replace( $full_match, $avg, $template_value );
}
}
return $template_value;
}
public function replace_template_min_merge_tags( $template_value, $field, $template, $populate, $object, $object_type, $objects ) {
if ( ! is_string( $template_value ) ) {
return $template_value;
}
preg_match_all( self::$min_regex, $template_value, $matches, PREG_SET_ORDER );
if ( $matches ) {
foreach ( $matches as $match ) {
$full_match = $match[0];
$merge_tag = str_replace( $object_type->id . ':', '', $match[1] );
$min = null;
foreach ( $objects as $object ) {
$value = $this->get_object_value( $merge_tag, $object, $object_type );
if ( is_numeric( $value ) ) {
$value = floatval( $value );
if ( is_null( $min ) || ( $value < $min ) ) {
$min = $value;
}
}
}
if ( is_null( $min ) ) {
$min = ' - ';
}
$template_value = str_replace( $full_match, $min, $template_value );
}
}
return $template_value;
}
public function replace_template_max_merge_tags( $template_value, $field, $template, $populate, $object, $object_type, $objects ) {
if ( ! is_string( $template_value ) ) {
return $template_value;
}
preg_match_all( self::$max_regex, $template_value, $matches, PREG_SET_ORDER );
if ( $matches ) {
foreach ( $matches as $match ) {
$full_match = $match[0];
$merge_tag = str_replace( $object_type->id . ':', '', $match[1] );
$max = null;
foreach ( $objects as $object ) {
$value = $this->get_object_value( $merge_tag, $object, $object_type );
if ( is_numeric( $value ) ) {
$value = floatval( $value );
if ( is_null( $max ) || ( $value > $max ) ) {
$max = $value;
}
}
}
if ( is_null( $max ) ) {
$max = ' - ';
}
$template_value = str_replace( $full_match, $max, $template_value );
}
}
return $template_value;
}
public function get_object_value( $merge_tag, $object, $object_type ) {
$value = $object_type->get_object_prop_value( $object, $merge_tag );
// Convert currency values to numbers for Gravity Forms product fields.
if ( $object_type->id === 'gf_entry' && strpos( $merge_tag, 'gf_field_' ) !== false ) {
$input_id = str_replace( 'gf_field_', '', $merge_tag );
$source_field = GFAPI::get_field( $object->form_id, $input_id );
if ( GFCommon::is_product_field( $source_field->type ) ) {
$value = GFCommon::to_number( $value, $object->currency );
}
}
return $value;
}
}
function gppa_aggregate_functions() {
return GPPA_Aggregate_Functions::get_instance();
}
gppa_aggregate_functions();