1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Set of common functions to separate main plugin from Gravity Forms API and other cross-plugin methods |
4
|
|
|
* |
5
|
|
|
* @package GravityView |
6
|
|
|
* @license GPL2+ |
7
|
|
|
* @author Katz Web Services, Inc. |
8
|
|
|
* @link http://gravityview.co |
9
|
|
|
* @copyright Copyright 2014, Katz Web Services, Inc. |
10
|
|
|
* |
11
|
|
|
* @since 1.5.2 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** If this file is called directly, abort. */ |
15
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
16
|
|
|
die; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
class GVCommon { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns the form object for a given Form ID. |
23
|
|
|
* |
24
|
|
|
* @access public |
25
|
|
|
* @param mixed $form_id |
26
|
|
|
* @return array|false Array: Form object returned from Gravity Forms; False: no form ID specified or Gravity Forms isn't active. |
27
|
|
|
*/ |
28
|
27 |
|
public static function get_form( $form_id ) { |
29
|
27 |
|
if ( empty( $form_id ) ) { |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Only get_form_meta is cached. ::facepalm:: |
34
|
27 |
|
if ( class_exists( 'GFFormsModel' ) ) { |
35
|
27 |
|
return GFFormsModel::get_form_meta( $form_id ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ( class_exists( 'GFAPI' ) ) { |
39
|
|
|
return GFAPI::get_form( $form_id ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Alias of GravityView_Roles_Capabilities::has_cap() |
47
|
|
|
* |
48
|
|
|
* @since 1.15 |
49
|
|
|
* |
50
|
|
|
* @see GravityView_Roles_Capabilities::has_cap() |
51
|
|
|
* |
52
|
|
|
* @param string|array $caps Single capability or array of capabilities |
53
|
|
|
* @param int $object_id (optional) Parameter can be used to check for capabilities against a specific object, such as a post or user |
54
|
|
|
* @param int|null $user_id (optional) Check the capabilities for a user who is not necessarily the currently logged-in user |
55
|
|
|
* |
56
|
|
|
* @return bool True: user has at least one passed capability; False: user does not have any defined capabilities |
57
|
|
|
*/ |
58
|
21 |
|
public static function has_cap( $caps = '', $object_id = null, $user_id = null ) { |
59
|
21 |
|
return GravityView_Roles_Capabilities::has_cap( $caps, $object_id, $user_id ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return a Gravity Forms field array, whether using GF 1.9 or not |
64
|
|
|
* |
65
|
|
|
* @since 1.7 |
66
|
|
|
* |
67
|
|
|
* @param array|GF_Fields $field Gravity Forms field or array |
68
|
|
|
* @return array Array version of $field |
69
|
|
|
*/ |
70
|
|
|
public static function get_field_array( $field ) { |
71
|
|
|
|
72
|
|
|
if ( class_exists( 'GF_Fields' ) ) { |
73
|
|
|
|
74
|
|
|
$field_object = GF_Fields::create( $field ); |
75
|
|
|
|
76
|
|
|
// Convert the field object in 1.9 to an array for backward compatibility |
77
|
|
|
$field_array = get_object_vars( $field_object ); |
78
|
|
|
|
79
|
|
|
} else { |
80
|
|
|
$field_array = $field; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $field_array; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get all existing Views |
88
|
|
|
* |
89
|
|
|
* @since 1.5.4 Added $args array |
90
|
|
|
* |
91
|
|
|
* @param array $args Pass custom array of args, formatted as if for `get_posts()` |
92
|
|
|
* |
93
|
|
|
* @return WP_Post[] Array of Views as `WP_Post`. Empty array if none found. |
94
|
|
|
*/ |
95
|
3 |
|
public static function get_all_views( $args = array() ) { |
96
|
|
|
|
97
|
|
|
$default_params = array( |
98
|
3 |
|
'post_type' => 'gravityview', |
99
|
|
|
'posts_per_page' => -1, |
|
|
|
|
100
|
|
|
'post_status' => 'publish', |
101
|
|
|
); |
102
|
|
|
|
103
|
3 |
|
$params = wp_parse_args( $args, $default_params ); |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @filter `gravityview/get_all_views/params` Modify the parameters sent to get all views. |
107
|
|
|
* @param[in,out] array $params Array of parameters to pass to `get_posts()` |
108
|
|
|
*/ |
109
|
3 |
|
$views_params = apply_filters( 'gravityview/get_all_views/params', $params ); |
110
|
|
|
|
111
|
3 |
|
$views = get_posts( $views_params ); |
112
|
|
|
|
113
|
3 |
|
return $views; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the form array for an entry based only on the entry ID |
119
|
|
|
* @param int|string $entry_slug Entry slug |
120
|
|
|
* @return array|false Array: Form object returned from Gravity Forms; False: form doesn't exist, or $entry didn't exist or $entry didn't specify form ID |
121
|
|
|
*/ |
122
|
|
|
public static function get_form_from_entry_id( $entry_slug ) { |
123
|
|
|
|
124
|
|
|
$entry = self::get_entry( $entry_slug, true, false ); |
125
|
|
|
|
126
|
|
|
$form = false; |
127
|
|
|
|
128
|
|
|
if( $entry ) { |
129
|
|
|
$form = GFAPI::get_form( $entry['form_id'] ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $form; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Check whether a form has product fields |
137
|
|
|
* |
138
|
|
|
* @since 1.16 |
139
|
|
|
* @since 1.20 Refactored the field types to get_product_field_types() method |
140
|
|
|
* |
141
|
|
|
* @param array $form Gravity Forms form array |
142
|
|
|
* |
143
|
|
|
* @return bool|GF_Field[] |
144
|
|
|
*/ |
145
|
3 |
|
public static function has_product_field( $form = array() ) { |
146
|
|
|
|
147
|
3 |
|
$product_fields = self::get_product_field_types(); |
148
|
|
|
|
149
|
3 |
|
$fields = GFAPI::get_fields_by_type( $form, $product_fields ); |
150
|
|
|
|
151
|
3 |
|
return empty( $fields ) ? false : $fields; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Return array of product field types |
156
|
|
|
* |
157
|
|
|
* Modify the value using the `gform_product_field_types` filter |
158
|
|
|
* |
159
|
|
|
* @since 1.20 |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
4 |
|
public static function get_product_field_types() { |
164
|
|
|
|
165
|
4 |
|
$product_fields = apply_filters( 'gform_product_field_types', array( 'option', 'quantity', 'product', 'total', 'shipping', 'calculation', 'price', 'hiddenproduct', 'singleproduct', 'singleshipping' ) ); |
166
|
|
|
|
167
|
4 |
|
return $product_fields; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Check if an entry has transaction data |
172
|
|
|
* |
173
|
|
|
* Checks the following keys to see if they are set: 'payment_status', 'payment_date', 'transaction_id', 'payment_amount', 'payment_method' |
174
|
|
|
* |
175
|
|
|
* @since 1.20 |
176
|
|
|
* |
177
|
|
|
* @param array $entry Gravity Forms entry array |
178
|
|
|
* |
179
|
|
|
* @return bool True: Entry has metadata suggesting it has communicated with a payment gateway; False: it does not have that data. |
180
|
|
|
*/ |
181
|
4 |
|
public static function entry_has_transaction_data( $entry = array() ) { |
182
|
|
|
|
183
|
4 |
|
if ( ! is_array( $entry ) ) { |
184
|
1 |
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
4 |
|
$has_transaction_data = false; |
188
|
|
|
|
189
|
4 |
|
$payment_meta = array( 'payment_status', 'payment_date', 'transaction_id', 'payment_amount', 'payment_method' ); |
190
|
|
|
|
191
|
4 |
|
foreach ( $payment_meta as $meta ) { |
192
|
|
|
|
193
|
4 |
|
$has_transaction_data = \GV\Utils::get( $entry, $meta, false ); |
194
|
|
|
|
195
|
4 |
|
if( ! empty( $has_transaction_data ) ) { |
196
|
4 |
|
break; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
4 |
|
return (bool) $has_transaction_data; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the entry ID from the entry slug, which may or may not be the entry ID |
205
|
|
|
* |
206
|
|
|
* @since 1.5.2 |
207
|
|
|
* @param string $slug The entry slug, as returned by GravityView_API::get_entry_slug() |
208
|
|
|
* @return int|null The entry ID, if exists; `NULL` if not |
209
|
|
|
*/ |
210
|
2 |
|
public static function get_entry_id_from_slug( $slug ) { |
211
|
2 |
|
global $wpdb; |
|
|
|
|
212
|
|
|
|
213
|
|
|
$search_criteria = array( |
214
|
|
|
'field_filters' => array( |
215
|
|
|
array( |
216
|
2 |
|
'key' => 'gravityview_unique_id', // Search the meta values |
217
|
2 |
|
'value' => $slug, |
218
|
2 |
|
'operator' => 'is', |
219
|
2 |
|
'type' => 'meta', |
220
|
|
|
), |
221
|
|
|
) |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
// Limit to one for speed |
225
|
|
|
$paging = array( |
226
|
2 |
|
'page_size' => 1, |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @filter `gravityview/common/get_entry_id_from_slug/form_id` The form ID used to get the custom entry ID. Change this to avoid collisions with data from other forms with the same values and the same field ID. |
231
|
|
|
* @since 1.17.2 |
232
|
|
|
* @param int $form_id ID of the form to search. Default: `0` (searches all forms) |
233
|
|
|
*/ |
234
|
2 |
|
$form_id = apply_filters( 'gravityview/common/get_entry_id_from_slug/form_id', 0 ); |
235
|
|
|
|
236
|
2 |
|
$results = GFAPI::get_entries( intval( $form_id ), $search_criteria, null, $paging ); |
237
|
|
|
|
238
|
2 |
|
$result = ( ! empty( $results ) && ! empty( $results[0]['id'] ) ) ? $results[0]['id'] : null; |
239
|
|
|
|
240
|
2 |
|
return $result; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Alias of GFAPI::get_forms() |
245
|
|
|
* |
246
|
|
|
* @see GFAPI::get_forms() |
247
|
|
|
* |
248
|
|
|
* @since 1.19 Allow "any" $active status option |
249
|
|
|
* |
250
|
|
|
* @param bool|string $active Status of forms. Use `any` to get array of forms with any status. Default: `true` |
251
|
|
|
* @param bool $trash Include forms in trash? Default: `false` |
252
|
|
|
* |
253
|
|
|
* @return array Empty array if GFAPI class isn't available or no forms. Otherwise, the array of Forms |
254
|
|
|
*/ |
255
|
1 |
|
public static function get_forms( $active = true, $trash = false ) { |
256
|
1 |
|
$forms = array(); |
257
|
1 |
|
if ( class_exists( 'GFAPI' ) ) { |
258
|
1 |
|
if( 'any' === $active ) { |
259
|
|
|
$active_forms = GFAPI::get_forms( true, $trash ); |
260
|
|
|
$inactive_forms = GFAPI::get_forms( false, $trash ); |
261
|
|
|
$forms = array_merge( array_filter( $active_forms ), array_filter( $inactive_forms ) ); |
262
|
|
|
} else { |
263
|
1 |
|
$forms = GFAPI::get_forms( $active, $trash ); |
264
|
|
|
} |
265
|
|
|
} |
266
|
1 |
|
return $forms; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Return array of fields' id and label, for a given Form ID |
271
|
|
|
* |
272
|
|
|
* @access public |
273
|
|
|
* @param string|array $form_id (default: '') or $form object |
|
|
|
|
274
|
|
|
* @param bool $add_default_properties |
275
|
|
|
* @param bool $include_parent_field |
276
|
|
|
* @return array |
277
|
|
|
*/ |
278
|
|
|
public static function get_form_fields( $form = '', $add_default_properties = false, $include_parent_field = true ) { |
279
|
|
|
|
280
|
|
|
if ( ! is_array( $form ) ) { |
281
|
|
|
$form = self::get_form( $form ); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
$fields = array(); |
285
|
|
|
$has_product_fields = false; |
286
|
|
|
$has_post_fields = false; |
287
|
|
|
|
288
|
|
|
if ( $form ) { |
289
|
|
|
foreach ( $form['fields'] as $field ) { |
290
|
|
|
if ( $include_parent_field || empty( $field['inputs'] ) ) { |
291
|
|
|
$fields["{$field['id']}"] = array( |
|
|
|
|
292
|
|
|
'label' => \GV\Utils::get( $field, 'label' ), |
293
|
|
|
'parent' => null, |
294
|
|
|
'type' => \GV\Utils::get( $field, 'type' ), |
295
|
|
|
'adminLabel' => \GV\Utils::get( $field, 'adminLabel' ), |
296
|
|
|
'adminOnly' => \GV\Utils::get( $field, 'adminOnly' ), |
297
|
|
|
); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if ( $add_default_properties && ! empty( $field['inputs'] ) ) { |
301
|
|
|
foreach ( $field['inputs'] as $input ) { |
302
|
|
|
|
303
|
|
|
if( ! empty( $input['isHidden'] ) ) { |
304
|
|
|
continue; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @hack |
309
|
|
|
* In case of email/email confirmation, the input for email has the same id as the parent field |
310
|
|
|
*/ |
311
|
|
|
if( 'email' === $field['type'] && false === strpos( $input['id'], '.' ) ) { |
312
|
|
|
continue; |
313
|
|
|
} |
314
|
|
|
$fields["{$input['id']}"] = array( |
|
|
|
|
315
|
|
|
'label' => \GV\Utils::get( $input, 'label' ), |
316
|
|
|
'customLabel' => \GV\Utils::get( $input, 'customLabel' ), |
317
|
|
|
'parent' => $field, |
318
|
|
|
'type' => \GV\Utils::get( $field, 'type' ), |
319
|
|
|
'adminLabel' => \GV\Utils::get( $field, 'adminLabel' ), |
320
|
|
|
'adminOnly' => \GV\Utils::get( $field, 'adminOnly' ), |
321
|
|
|
); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
326
|
|
|
if( GFCommon::is_product_field( $field['type'] ) ){ |
327
|
|
|
$has_product_fields = true; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
if ( GFCommon::is_post_field( $field ) ) { |
331
|
|
|
$has_post_fields = true; |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @since 1.7 |
338
|
|
|
*/ |
339
|
|
|
if ( $has_post_fields ) { |
340
|
|
|
$fields['post_id'] = array( |
341
|
|
|
'label' => __( 'Post ID', 'gravityview' ), |
342
|
|
|
'type' => 'post_id', |
343
|
|
|
); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
if ( $has_product_fields ) { |
347
|
|
|
|
348
|
|
|
$payment_fields = GravityView_Fields::get_all( 'pricing' ); |
349
|
|
|
|
350
|
|
|
foreach ( $payment_fields as $payment_field ) { |
351
|
|
|
|
352
|
|
|
// Either the field exists ($fields['shipping']) or the form explicitly contains a `shipping` field with numeric key |
353
|
|
|
if( isset( $fields["{$payment_field->name}"] ) || GFCommon::get_fields_by_type( $form, $payment_field->name ) ) { |
|
|
|
|
354
|
|
|
continue; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
$fields["{$payment_field->name}"] = array( |
|
|
|
|
358
|
|
|
'label' => $payment_field->label, |
359
|
|
|
'desc' => $payment_field->description, |
360
|
|
|
'type' => $payment_field->name, |
361
|
|
|
); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @filter `gravityview/common/get_form_fields` Modify the form fields shown in the Add Field field picker. |
367
|
|
|
* @since 1.17 |
368
|
|
|
* @param array $fields Associative array of fields, with keys as field type, values an array with the following keys: (string) `label` (required), (string) `type` (required), `desc`, (string) `customLabel`, (GF_Field) `parent`, (string) `adminLabel`, (bool)`adminOnly` |
369
|
|
|
* @param array $form GF Form array |
370
|
|
|
* @param bool $include_parent_field Whether to include the parent field when getting a field with inputs |
371
|
|
|
*/ |
372
|
|
|
$fields = apply_filters( 'gravityview/common/get_form_fields', $fields, $form, $include_parent_field ); |
373
|
|
|
|
374
|
|
|
return $fields; |
375
|
|
|
|
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* get extra fields from entry meta |
380
|
|
|
* @param string $form_id (default: '') |
381
|
|
|
* @return array |
382
|
|
|
*/ |
383
|
|
|
public static function get_entry_meta( $form_id, $only_default_column = true ) { |
384
|
|
|
|
385
|
|
|
$extra_fields = GFFormsModel::get_entry_meta( $form_id ); |
386
|
|
|
|
387
|
|
|
$fields = array(); |
388
|
|
|
|
389
|
|
|
foreach ( $extra_fields as $key => $field ){ |
390
|
|
|
if ( ! empty( $only_default_column ) && ! empty( $field['is_default_column'] ) ) { |
391
|
|
|
$fields[ $key ] = array( 'label' => $field['label'], 'type' => 'entry_meta' ); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
return $fields; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Wrapper for the Gravity Forms GFFormsModel::search_lead_ids() method |
401
|
|
|
* |
402
|
|
|
* @see GFEntryList::leads_page() |
403
|
|
|
* @param int $form_id ID of the Gravity Forms form |
404
|
|
|
* @since 1.1.6 |
405
|
|
|
* @return array|void Array of entry IDs. Void if Gravity Forms isn't active. |
406
|
|
|
*/ |
407
|
|
|
public static function get_entry_ids( $form_id, $search_criteria = array() ) { |
408
|
|
|
|
409
|
|
|
if ( ! class_exists( 'GFFormsModel' ) ) { |
410
|
|
|
return; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return GFFormsModel::search_lead_ids( $form_id, $search_criteria ); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Calculates the Search Criteria used on the self::get_entries / self::get_entry methods |
418
|
|
|
* |
419
|
|
|
* @since 1.7.4 |
420
|
|
|
* |
421
|
|
|
* @param array $passed_criteria array Input Criteria (search_criteria, sorting, paging) |
422
|
|
|
* @param array $form_ids array Gravity Forms form IDs |
423
|
|
|
* @return array |
424
|
|
|
*/ |
425
|
32 |
|
public static function calculate_get_entries_criteria( $passed_criteria = array(), $form_ids = array() ) { |
426
|
|
|
|
427
|
|
|
$search_criteria_defaults = array( |
428
|
32 |
|
'search_criteria' => null, |
429
|
|
|
'sorting' => null, |
430
|
|
|
'paging' => null, |
431
|
32 |
|
'cache' => (isset( $passed_criteria['cache'] ) ? (bool) $passed_criteria['cache'] : true), |
432
|
|
|
'context_view_id' => null, |
433
|
|
|
); |
434
|
|
|
|
435
|
32 |
|
$criteria = wp_parse_args( $passed_criteria, $search_criteria_defaults ); |
436
|
|
|
|
437
|
32 |
|
if ( ! empty( $criteria['search_criteria']['field_filters'] ) ) { |
438
|
4 |
|
foreach ( $criteria['search_criteria']['field_filters'] as &$filter ) { |
439
|
|
|
|
440
|
4 |
|
if ( ! is_array( $filter ) ) { |
441
|
4 |
|
continue; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
// By default, we want searches to be wildcard for each field. |
445
|
4 |
|
$filter['operator'] = empty( $filter['operator'] ) ? 'contains' : $filter['operator']; |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @filter `gravityview_search_operator` Modify the search operator for the field (contains, is, isnot, etc) |
449
|
|
|
* @param string $operator Existing search operator |
450
|
|
|
* @param array $filter array with `key`, `value`, `operator`, `type` keys |
451
|
|
|
*/ |
452
|
4 |
|
$filter['operator'] = apply_filters( 'gravityview_search_operator', $filter['operator'], $filter ); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
// don't send just the [mode] without any field filter. |
456
|
4 |
|
if( count( $criteria['search_criteria']['field_filters'] ) === 1 && array_key_exists( 'mode' , $criteria['search_criteria']['field_filters'] ) ) { |
|
|
|
|
457
|
1 |
|
unset( $criteria['search_criteria']['field_filters']['mode'] ); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
} |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Prepare date formats to be in Gravity Forms DB format; |
466
|
|
|
* $passed_criteria may include date formats incompatible with Gravity Forms. |
467
|
|
|
*/ |
468
|
32 |
|
foreach ( array('start_date', 'end_date' ) as $key ) { |
|
|
|
|
469
|
|
|
|
470
|
32 |
|
if ( ! empty( $criteria['search_criteria'][ $key ] ) ) { |
471
|
|
|
|
472
|
|
|
// Use date_create instead of new DateTime so it returns false if invalid date format. |
473
|
1 |
|
$date = date_create( $criteria['search_criteria'][ $key ] ); |
474
|
|
|
|
475
|
1 |
|
if ( $date ) { |
476
|
|
|
// Gravity Forms wants dates in the `Y-m-d H:i:s` format. |
477
|
1 |
|
$criteria['search_criteria'][ $key ] = $date->format( 'Y-m-d H:i:s' ); |
478
|
|
|
} else { |
479
|
1 |
|
gravityview()->log->error( '{key} Date format not valid:', array( 'key' => $key, $criteria['search_criteria'][ $key ] ) ); |
480
|
|
|
|
481
|
|
|
// If it's an invalid date, unset it. Gravity Forms freaks out otherwise. |
482
|
32 |
|
unset( $criteria['search_criteria'][ $key ] ); |
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
} |
486
|
|
|
|
487
|
32 |
|
if ( empty( $criteria['context_view_id'] ) ) { |
488
|
|
|
// Calculate the context view id and send it to the advanced filter |
489
|
20 |
|
if ( GravityView_frontend::getInstance()->getSingleEntry() ) { |
490
|
19 |
|
$criteria['context_view_id'] = GravityView_frontend::getInstance()->get_context_view_id(); |
491
|
2 |
|
} else if ( class_exists( 'GravityView_View_Data' ) && GravityView_View_Data::getInstance() && GravityView_View_Data::getInstance()->has_multiple_views() ) { |
|
|
|
|
492
|
1 |
|
$criteria['context_view_id'] = GravityView_frontend::getInstance()->get_context_view_id(); |
493
|
2 |
|
} else if ( 'delete' === GFForms::get( 'action' ) ) { |
494
|
1 |
|
$criteria['context_view_id'] = isset( $_GET['view_id'] ) ? intval( $_GET['view_id'] ) : null; |
|
|
|
|
495
|
|
|
} |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* @filter `gravityview_search_criteria` Apply final criteria filter (Used by the Advanced Filter extension) |
500
|
|
|
* @param array $criteria Search criteria used by GravityView |
501
|
|
|
* @param array $form_ids Forms to search |
502
|
|
|
* @param int $view_id ID of the view being used to search |
503
|
|
|
*/ |
504
|
32 |
|
$criteria = apply_filters( 'gravityview_search_criteria', $criteria, $form_ids, $criteria['context_view_id'] ); |
505
|
|
|
|
506
|
32 |
|
return (array)$criteria; |
|
|
|
|
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Retrieve entries given search, sort, paging criteria |
512
|
|
|
* |
513
|
|
|
* @see GFAPI::get_entries() |
514
|
|
|
* @see GFFormsModel::get_field_filters_where() |
515
|
|
|
* @access public |
516
|
|
|
* @param int|array $form_ids The ID of the form or an array IDs of the Forms. Zero for all forms. |
517
|
|
|
* @param mixed $passed_criteria (default: null) |
518
|
|
|
* @param mixed &$total Optional. An output parameter containing the total number of entries. Pass a non-null value to generate the total count. (default: null) |
519
|
|
|
* @return mixed False: Error fetching entries. Array: Multi-dimensional array of Gravity Forms entry arrays |
520
|
|
|
*/ |
521
|
|
|
public static function get_entries( $form_ids = null, $passed_criteria = null, &$total = null ) { |
522
|
|
|
|
523
|
|
|
// Filter the criteria before query (includes Adv Filter) |
524
|
|
|
$criteria = self::calculate_get_entries_criteria( $passed_criteria, $form_ids ); |
525
|
|
|
|
526
|
|
|
gravityview()->log->debug( '[gravityview_get_entries] Final Parameters', array( 'data' => $criteria ) ); |
527
|
|
|
|
528
|
|
|
// Return value |
529
|
|
|
$return = null; |
530
|
|
|
|
531
|
|
|
/** Reduce # of database calls */ |
532
|
|
|
add_filter( 'gform_is_encrypted_field', '__return_false' ); |
533
|
|
|
|
534
|
|
|
if ( ! empty( $criteria['cache'] ) ) { |
535
|
|
|
|
536
|
|
|
$Cache = new GravityView_Cache( $form_ids, $criteria ); |
537
|
|
|
|
538
|
|
|
if ( $entries = $Cache->get() ) { |
539
|
|
|
|
540
|
|
|
// Still update the total count when using cached results |
541
|
|
|
if ( ! is_null( $total ) ) { |
542
|
|
|
$total = GFAPI::count_entries( $form_ids, $criteria['search_criteria'] ); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
$return = $entries; |
546
|
|
|
} |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
if ( is_null( $return ) && class_exists( 'GFAPI' ) && ( is_numeric( $form_ids ) || is_array( $form_ids ) ) ) { |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* @filter `gravityview_pre_get_entries` Define entries to be used before GFAPI::get_entries() is called |
553
|
|
|
* @since 1.14 |
554
|
|
|
* @param null $return If you want to override GFAPI::get_entries() and define entries yourself, tap in here. |
555
|
|
|
* @param array $criteria The final search criteria used to generate the request to `GFAPI::get_entries()` |
556
|
|
|
* @param array $passed_criteria The original search criteria passed to `GVCommon::get_entries()` |
557
|
|
|
* @param int|null $total Optional. An output parameter containing the total number of entries. Pass a non-null value to generate |
558
|
|
|
* @deprecated |
559
|
|
|
*/ |
560
|
|
|
$entries = apply_filters( 'gravityview_before_get_entries', null, $criteria, $passed_criteria, $total ); |
561
|
|
|
|
562
|
|
|
// No entries returned from gravityview_before_get_entries |
563
|
|
|
if( is_null( $entries ) ) { |
564
|
|
|
|
565
|
|
|
$entries = GFAPI::get_entries( $form_ids, $criteria['search_criteria'], $criteria['sorting'], $criteria['paging'], $total ); |
566
|
|
|
|
567
|
|
|
if ( is_wp_error( $entries ) ) { |
568
|
|
|
gravityview()->log->error( '{error}', array( 'error' => $entries->get_error_message(), 'data' => $entries ) ); |
569
|
|
|
|
570
|
|
|
/** Remove filter added above */ |
571
|
|
|
remove_filter( 'gform_is_encrypted_field', '__return_false' ); |
572
|
|
|
return false; |
573
|
|
|
} |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
if ( ! empty( $criteria['cache'] ) && isset( $Cache ) ) { |
577
|
|
|
|
578
|
|
|
// Cache results |
579
|
|
|
$Cache->set( $entries, 'entries' ); |
580
|
|
|
|
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
$return = $entries; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** Remove filter added above */ |
587
|
|
|
remove_filter( 'gform_is_encrypted_field', '__return_false' ); |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* @filter `gravityview_entries` Modify the array of entries returned to GravityView after it has been fetched from the cache or from `GFAPI::get_entries()`. |
591
|
|
|
* @param array|null $entries Array of entries as returned by the cache or by `GFAPI::get_entries()` |
592
|
|
|
* @param array $criteria The final search criteria used to generate the request to `GFAPI::get_entries()` |
593
|
|
|
* @param array $passed_criteria The original search criteria passed to `GVCommon::get_entries()` |
594
|
|
|
* @param int|null $total Optional. An output parameter containing the total number of entries. Pass a non-null value to generate |
595
|
|
|
*/ |
596
|
|
|
$return = apply_filters( 'gravityview_entries', $return, $criteria, $passed_criteria, $total ); |
597
|
|
|
|
598
|
|
|
return $return; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Get the entry ID from a string that may be the Entry ID or the Entry Slug |
604
|
|
|
* |
605
|
|
|
* @since 1.18 |
606
|
|
|
* |
607
|
|
|
* @param string $entry_id_or_slug The ID or slug of an entry. |
608
|
|
|
* @param bool $force_allow_ids Whether to force allowing getting the ID of an entry, even if custom slugs are enabled |
609
|
|
|
* |
610
|
|
|
* @return false|int|null Returns the ID of the entry found, if custom slugs is enabled. Returns original value if custom slugs is disabled. Returns false if not allowed to convert slug to ID. Returns NULL if entry not found for the passed slug. |
611
|
|
|
*/ |
612
|
19 |
|
public static function get_entry_id( $entry_id_or_slug = '', $force_allow_ids = false ) { |
613
|
|
|
|
614
|
19 |
|
$entry_id = false; |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* @filter `gravityview_custom_entry_slug` Whether to enable and use custom entry slugs. |
618
|
|
|
* @param boolean True: Allow for slugs based on entry values. False: always use entry IDs (default) |
619
|
|
|
*/ |
620
|
19 |
|
$custom_slug = apply_filters( 'gravityview_custom_entry_slug', false ); |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* @filter `gravityview_custom_entry_slug_allow_id` When using a custom slug, allow access to the entry using the original slug (the Entry ID). |
624
|
|
|
* - If disabled (default), only allow access to an entry using the custom slug value. (example: `/entry/custom-slug/` NOT `/entry/123/`) |
625
|
|
|
* - If enabled, you could access using the custom slug OR the entry id (example: `/entry/custom-slug/` OR `/entry/123/`) |
626
|
|
|
* @param boolean $custom_slug_id_access True: allow accessing the slug by ID; False: only use the slug passed to the method. |
627
|
|
|
*/ |
628
|
19 |
|
$custom_slug_id_access = $force_allow_ids || apply_filters( 'gravityview_custom_entry_slug_allow_id', false ); |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* If we're using custom entry slugs, we do a meta value search |
632
|
|
|
* instead of doing a straightup ID search. |
633
|
|
|
*/ |
634
|
19 |
|
if ( $custom_slug ) { |
635
|
|
|
// Search for IDs matching $entry_id_or_slug |
636
|
2 |
|
$entry_id = self::get_entry_id_from_slug( $entry_id_or_slug ); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
// If custom slug is off, search using the entry ID |
640
|
|
|
// ID allow ID access is on, also use entry ID as a backup |
641
|
19 |
|
if ( false === $custom_slug || true === $custom_slug_id_access ) { |
642
|
|
|
// Search for IDs matching $entry_slug |
643
|
19 |
|
$entry_id = $entry_id_or_slug; |
644
|
|
|
} |
645
|
|
|
|
646
|
19 |
|
return $entry_id; |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* Return a single entry object |
651
|
|
|
* |
652
|
|
|
* Since 1.4, supports custom entry slugs. The way that GravityView fetches an entry based on the custom slug is by searching `gravityview_unique_id` meta. The `$entry_slug` is fetched by getting the current query var set by `is_single_entry()` |
653
|
|
|
* |
654
|
|
|
* @access public |
655
|
|
|
* @param string|int $entry_slug Either entry ID or entry slug string |
656
|
|
|
* @param boolean $force_allow_ids Force the get_entry() method to allow passed entry IDs, even if the `gravityview_custom_entry_slug_allow_id` filter returns false. |
657
|
|
|
* @param boolean $check_entry_display Check whether the entry is visible for the current View configuration. Default: true. {@since 1.14} |
658
|
|
|
* @return array|boolean |
659
|
|
|
*/ |
660
|
25 |
|
public static function get_entry( $entry_slug, $force_allow_ids = false, $check_entry_display = true ) { |
661
|
|
|
|
662
|
25 |
|
if ( class_exists( 'GFAPI' ) && ! empty( $entry_slug ) ) { |
663
|
|
|
|
664
|
19 |
|
$entry_id = self::get_entry_id( $entry_slug, $force_allow_ids ); |
665
|
|
|
|
666
|
19 |
|
if ( empty( $entry_id ) ) { |
667
|
2 |
|
return false; |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
// fetch the entry |
671
|
19 |
|
$entry = GFAPI::get_entry( $entry_id ); |
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* @filter `gravityview/common/get_entry/check_entry_display` Override whether to check entry display rules against filters |
675
|
|
|
* @since 1.16.2 |
676
|
|
|
* @param bool $check_entry_display Check whether the entry is visible for the current View configuration. Default: true. |
677
|
|
|
* @param array $entry Gravity Forms entry array |
678
|
|
|
*/ |
679
|
19 |
|
$check_entry_display = apply_filters( 'gravityview/common/get_entry/check_entry_display', $check_entry_display, $entry ); |
680
|
|
|
|
681
|
19 |
|
if( $check_entry_display ) { |
682
|
|
|
// Is the entry allowed |
683
|
19 |
|
$entry = self::check_entry_display( $entry ); |
684
|
|
|
} |
685
|
|
|
|
686
|
19 |
|
if( is_wp_error( $entry ) ) { |
687
|
|
|
gravityview()->log->error( '{error}', array( 'error' => $entry->get_error_message() ) ); |
688
|
|
|
return false; |
689
|
|
|
} |
690
|
|
|
|
691
|
19 |
|
return $entry; |
692
|
|
|
} |
693
|
|
|
|
694
|
18 |
|
return false; |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
/** |
698
|
|
|
* Wrapper for the GFFormsModel::matches_operation() method that adds additional comparisons, including: |
699
|
|
|
* 'equals', 'greater_than_or_is', 'greater_than_or_equals', 'less_than_or_is', 'less_than_or_equals', |
700
|
|
|
* 'not_contains', 'in', and 'not_in' |
701
|
|
|
* |
702
|
|
|
* @since 1.13 You can define context, which displays/hides based on what's being displayed (single, multiple, edit) |
703
|
|
|
* @since 1.22.1 Added 'in' and 'not_in' for JSON-encoded array values, serialized non-strings |
704
|
|
|
* |
705
|
|
|
* @see https://docs.gravityview.co/article/252-gvlogic-shortcode |
706
|
|
|
* @uses GFFormsModel::matches_operation |
707
|
|
|
* @since 1.7.5 |
708
|
|
|
* |
709
|
|
|
* @param string $val1 Left side of comparison |
710
|
|
|
* @param string $val2 Right side of comparison |
711
|
|
|
* @param string $operation Type of comparison |
712
|
|
|
* |
713
|
|
|
* @return bool True: matches, false: not matches |
714
|
|
|
*/ |
715
|
4 |
|
public static function matches_operation( $val1, $val2, $operation ) { |
716
|
|
|
|
717
|
4 |
|
$json_function = function_exists('wp_json_encode') ? 'wp_json_encode' : 'json_encode'; |
|
|
|
|
718
|
|
|
|
719
|
|
|
// Only process strings |
720
|
4 |
|
$val1 = ! is_string( $val1 ) ? $json_function( $val1 ) : $val1; |
721
|
4 |
|
$val2 = ! is_string( $val2 ) ? $json_function( $val2 ) : $val2; |
722
|
|
|
|
723
|
4 |
|
$value = false; |
724
|
|
|
|
725
|
4 |
|
if( 'context' === $val1 ) { |
726
|
|
|
|
727
|
|
|
$matching_contexts = array( $val2 ); |
728
|
|
|
|
729
|
|
|
// We allow for non-standard contexts. |
730
|
|
|
switch( $val2 ) { |
731
|
|
|
// Check for either single or edit |
732
|
|
|
case 'singular': |
733
|
|
|
$matching_contexts = array( 'single', 'edit' ); |
734
|
|
|
break; |
735
|
|
|
// Use multiple as alias for directory for consistency |
736
|
|
|
case 'multiple': |
737
|
|
|
$matching_contexts = array( 'directory' ); |
738
|
|
|
break; |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
$val1 = in_array( gravityview_get_context(), $matching_contexts ) ? $val2 : false; |
742
|
|
|
} |
743
|
|
|
|
744
|
4 |
|
switch ( $operation ) { |
745
|
4 |
|
case 'equals': |
746
|
2 |
|
$value = self::matches_operation( $val1, $val2, 'is' ); |
747
|
2 |
|
break; |
748
|
4 |
|
case 'greater_than_or_is': |
749
|
4 |
|
case 'greater_than_or_equals': |
750
|
2 |
|
$is = self::matches_operation( $val1, $val2, 'is' ); |
751
|
2 |
|
$gt = self::matches_operation( $val1, $val2, 'greater_than' ); |
752
|
2 |
|
$value = ( $is || $gt ); |
753
|
2 |
|
break; |
754
|
4 |
|
case 'less_than_or_is': |
755
|
4 |
|
case 'less_than_or_equals': |
756
|
1 |
|
$is = self::matches_operation( $val1, $val2, 'is' ); |
757
|
1 |
|
$gt = self::matches_operation( $val1, $val2, 'less_than' ); |
758
|
1 |
|
$value = ( $is || $gt ); |
759
|
1 |
|
break; |
760
|
4 |
|
case 'not_contains': |
761
|
1 |
|
$contains = self::matches_operation( $val1, $val2, 'contains' ); |
762
|
1 |
|
$value = ! $contains; |
763
|
1 |
|
break; |
764
|
|
|
/** |
765
|
|
|
* @since 1.22.1 Handle JSON-encoded comparisons |
766
|
|
|
*/ |
767
|
4 |
|
case 'in': |
768
|
4 |
|
case 'not_in': |
769
|
|
|
|
770
|
1 |
|
$json_val_1 = json_decode( $val1, true ); |
771
|
1 |
|
$json_val_2 = json_decode( $val2, true ); |
772
|
|
|
|
773
|
1 |
|
if( ! empty( $json_val_1 ) || ! empty( $json_val_2 ) ) { |
774
|
|
|
|
775
|
1 |
|
$json_in = false; |
776
|
1 |
|
$json_val_1 = $json_val_1 ? $json_val_1 : array( $val1 ); |
777
|
1 |
|
$json_val_2 = $json_val_2 ? $json_val_2 : array( $val2 ); |
778
|
|
|
|
779
|
|
|
// For JSON, we want to compare as "in" or "not in" rather than "contains" |
780
|
1 |
|
foreach ( $json_val_1 as $item_1 ) { |
781
|
1 |
|
foreach ( $json_val_2 as $item_2 ) { |
782
|
1 |
|
$json_in = self::matches_operation( $item_1, $item_2, 'is' ); |
783
|
|
|
|
784
|
1 |
|
if( $json_in ) { |
785
|
1 |
|
break 2; |
786
|
|
|
} |
787
|
|
|
} |
788
|
|
|
} |
789
|
|
|
|
790
|
1 |
|
$value = ( $operation === 'in' ) ? $json_in : ! $json_in; |
791
|
|
|
} |
792
|
1 |
|
break; |
793
|
|
|
|
794
|
4 |
|
case 'less_than': |
795
|
4 |
|
case '<' : |
796
|
1 |
|
if ( is_string( $val1 ) && is_string( $val2 ) ) { |
797
|
1 |
|
$value = $val1 < $val2; |
798
|
|
|
} else { |
799
|
|
|
$value = GFFormsModel::matches_operation( $val1, $val2, $operation ); |
800
|
|
|
} |
801
|
1 |
|
break; |
802
|
4 |
|
case 'greater_than': |
803
|
4 |
|
case '>' : |
804
|
2 |
|
if ( is_string( $val1 ) && is_string( $val2 ) ) { |
805
|
2 |
|
$value = $val1 > $val2; |
806
|
|
|
} else { |
807
|
|
|
$value = GFFormsModel::matches_operation( $val1, $val2, $operation ); |
808
|
|
|
} |
809
|
2 |
|
break; |
810
|
|
|
default: |
811
|
4 |
|
$value = GFFormsModel::matches_operation( $val1, $val2, $operation ); |
812
|
|
|
} |
813
|
|
|
|
814
|
4 |
|
return $value; |
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
/** |
818
|
|
|
* |
819
|
|
|
* Checks if a certain entry is valid according to the View search filters (specially the Adv Filters) |
820
|
|
|
* |
821
|
|
|
* @uses GVCommon::calculate_get_entries_criteria(); |
822
|
|
|
* @see GFFormsModel::is_value_match() |
823
|
|
|
* |
824
|
|
|
* @since 1.7.4 |
825
|
|
|
* |
826
|
|
|
* @param array $entry Gravity Forms Entry object |
827
|
|
|
* @return WP_Error|array Returns WP_Error if entry is not valid according to the view search filters (Adv Filter). Returns original $entry value if passes. |
828
|
|
|
*/ |
829
|
20 |
|
public static function check_entry_display( $entry ) { |
830
|
|
|
|
831
|
20 |
|
if ( ! $entry || is_wp_error( $entry ) ) { |
|
|
|
|
832
|
1 |
|
return new WP_Error('entry_not_found', 'Entry was not found.', $entry ); |
|
|
|
|
833
|
|
|
} |
834
|
|
|
|
835
|
20 |
|
if ( empty( $entry['form_id'] ) ) { |
836
|
1 |
|
return new WP_Error( 'form_id_not_set', '[apply_filters_to_entry] Entry is empty!', $entry ); |
837
|
|
|
} |
838
|
|
|
|
839
|
20 |
|
$criteria = self::calculate_get_entries_criteria(); |
840
|
|
|
|
841
|
20 |
|
if ( empty( $criteria['search_criteria'] ) || ! is_array( $criteria['search_criteria'] ) ) { |
842
|
20 |
|
gravityview()->log->debug( '[apply_filters_to_entry] Entry approved! No search criteria found:', array( 'data' => $criteria ) ); |
843
|
20 |
|
return $entry; |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
// Make sure the current View is connected to the same form as the Entry |
847
|
1 |
|
if( ! empty( $criteria['context_view_id'] ) ) { |
848
|
1 |
|
$context_view_id = intval( $criteria['context_view_id'] ); |
849
|
1 |
|
$context_form_id = gravityview_get_form_id( $context_view_id ); |
850
|
1 |
|
if( intval( $context_form_id ) !== intval( $entry['form_id'] ) ) { |
851
|
1 |
|
return new WP_Error( 'view_id_not_match', sprintf( '[apply_filters_to_entry] Entry form ID does not match current View connected form ID:', $entry['form_id'] ), $criteria['context_view_id'] ); |
852
|
|
|
} |
853
|
|
|
} |
854
|
|
|
|
855
|
1 |
|
$search_criteria = $criteria['search_criteria']; |
856
|
|
|
|
857
|
|
|
// check entry status |
858
|
1 |
|
if ( array_key_exists( 'status', $search_criteria ) && $search_criteria['status'] != $entry['status'] ) { |
859
|
1 |
|
return new WP_Error( 'status_not_valid', sprintf( '[apply_filters_to_entry] Entry status - %s - is not valid according to filter:', $entry['status'] ), $search_criteria ); |
860
|
|
|
} |
861
|
|
|
|
862
|
|
|
// check entry date |
863
|
|
|
// @todo: Does it make sense to apply the Date create filters to the single entry? |
864
|
|
|
|
865
|
|
|
// field_filters |
866
|
1 |
|
if ( empty( $search_criteria['field_filters'] ) || ! is_array( $search_criteria['field_filters'] ) ) { |
867
|
1 |
|
gravityview()->log->debug( '[apply_filters_to_entry] Entry approved! No field filters criteria found:', array( 'data' => $search_criteria ) ); |
868
|
1 |
|
return $entry; |
869
|
|
|
} |
870
|
|
|
|
871
|
1 |
|
$filters = $search_criteria['field_filters']; |
872
|
|
|
|
873
|
1 |
|
$mode = array_key_exists( 'mode', $filters ) ? strtolower( $filters['mode'] ) : 'all'; |
874
|
|
|
|
875
|
|
|
// Prevent the mode from being processed below |
876
|
1 |
|
unset( $filters['mode'] ); |
877
|
|
|
|
878
|
1 |
|
$form = self::get_form( $entry['form_id'] ); |
879
|
|
|
|
880
|
1 |
|
foreach ( $filters as $filter ) { |
881
|
|
|
|
882
|
1 |
|
if ( ! isset( $filter['key'] ) ) { |
883
|
|
|
gravityview()->log->debug( '[apply_filters_to_entry] Filter key not set: {filter}', array( 'filter' => $filter ) ); |
884
|
|
|
continue; |
885
|
|
|
} |
886
|
|
|
|
887
|
1 |
|
$k = $filter['key']; |
888
|
|
|
|
889
|
1 |
|
$field = self::get_field( $form, $k ); |
890
|
|
|
|
891
|
1 |
|
if ( is_null( $field ) ) { |
892
|
1 |
|
$field_value = isset( $entry[ $k ] ) ? $entry[ $k ] : null; |
893
|
1 |
|
$field = $k; |
894
|
|
|
} else { |
895
|
|
|
$field_value = GFFormsModel::get_lead_field_value( $entry, $field ); |
896
|
|
|
// If it's a complex field, then fetch the input's value, if exists at the current key. Otherwise, let GF handle it |
897
|
|
|
$field_value = ( is_array( $field_value ) && isset( $field_value[ $k ] ) ) ? \GV\Utils::get( $field_value, $k ) : $field_value; |
898
|
|
|
} |
899
|
|
|
|
900
|
1 |
|
$operator = isset( $filter['operator'] ) ? strtolower( $filter['operator'] ) : 'is'; |
901
|
|
|
|
902
|
1 |
|
$is_value_match = GravityView_GFFormsModel::is_value_match( $field_value, $filter['value'], $operator, $field ); |
903
|
|
|
|
904
|
|
|
// Any match is all we need to know |
905
|
1 |
|
if ( $is_value_match && 'any' === $mode ) { |
906
|
1 |
|
return $entry; |
907
|
|
|
} |
908
|
|
|
|
909
|
|
|
// Any failed match is a total fail |
910
|
1 |
|
if ( ! $is_value_match && 'all' === $mode ) { |
911
|
1 |
|
return new WP_Error('failed_criteria', '[apply_filters_to_entry] Entry cannot be displayed. Failed a criterium for ALL mode', $filter ); |
|
|
|
|
912
|
|
|
} |
913
|
|
|
} |
914
|
|
|
|
915
|
|
|
// at this point, if in ALL mode, then entry is approved - all conditions were met. |
916
|
|
|
// Or, for ANY mode, means none of the conditions were satisfied, so entry is not approved |
917
|
1 |
|
if ( 'all' === $mode ) { |
918
|
1 |
|
gravityview()->log->debug( '[apply_filters_to_entry] Entry approved: all conditions were met' ); |
919
|
1 |
|
return $entry; |
920
|
|
|
} else { |
921
|
|
|
return new WP_Error('failed_any_criteria', '[apply_filters_to_entry] Entry cannot be displayed. Failed all the criteria for ANY mode', $filters ); |
|
|
|
|
922
|
|
|
} |
923
|
|
|
|
924
|
|
|
} |
925
|
|
|
|
926
|
|
|
|
927
|
|
|
/** |
928
|
|
|
* Allow formatting date and time based on GravityView standards |
929
|
|
|
* |
930
|
|
|
* @since 1.16 |
931
|
|
|
* |
932
|
|
|
* @see GVCommon_Test::test_format_date for examples |
933
|
|
|
* |
934
|
|
|
* @param string $date_string The date as stored by Gravity Forms (`Y-m-d h:i:s` GMT) |
935
|
|
|
* @param string|array $args Array or string of settings for output parsed by `wp_parse_args()`; Can use `raw=1` or `array('raw' => true)` \n |
936
|
|
|
* - `raw` Un-formatted date string in original `Y-m-d h:i:s` format |
937
|
|
|
* - `timestamp` Integer timestamp returned by GFCommon::get_local_timestamp() |
938
|
|
|
* - `diff` "%s ago" format, unless other `format` is defined |
939
|
|
|
* - `human` Set $is_human parameter to true for `GFCommon::format_date()`. Shows `diff` within 24 hours or date after. Format based on blog setting, unless `format` is defined. |
940
|
|
|
* - `time` Include time in the `GFCommon::format_date()` output |
941
|
|
|
* - `format` Define your own date format, or `diff` format |
942
|
|
|
* |
943
|
|
|
* @return int|null|string Formatted date based on the original date |
944
|
|
|
*/ |
945
|
3 |
|
public static function format_date( $date_string = '', $args = array() ) { |
946
|
|
|
|
947
|
|
|
$default_atts = array( |
948
|
3 |
|
'raw' => false, |
949
|
|
|
'timestamp' => false, |
950
|
|
|
'diff' => false, |
951
|
|
|
'human' => false, |
952
|
|
|
'format' => '', |
953
|
|
|
'time' => false, |
954
|
|
|
); |
955
|
|
|
|
956
|
3 |
|
$atts = wp_parse_args( $args, $default_atts ); |
957
|
|
|
|
958
|
|
|
/** |
959
|
|
|
* Gravity Forms code to adjust date to locally-configured Time Zone |
960
|
|
|
* @see GFCommon::format_date() for original code |
961
|
|
|
*/ |
962
|
3 |
|
$date_gmt_time = mysql2date( 'G', $date_string ); |
963
|
3 |
|
$date_local_timestamp = GFCommon::get_local_timestamp( $date_gmt_time ); |
964
|
|
|
|
965
|
3 |
|
$format = \GV\Utils::get( $atts, 'format' ); |
966
|
3 |
|
$is_human = ! empty( $atts['human'] ); |
967
|
3 |
|
$is_diff = ! empty( $atts['diff'] ); |
968
|
3 |
|
$is_raw = ! empty( $atts['raw'] ); |
969
|
3 |
|
$is_timestamp = ! empty( $atts['timestamp'] ); |
970
|
3 |
|
$include_time = ! empty( $atts['time'] ); |
971
|
|
|
|
972
|
|
|
// If we're using time diff, we want to have a different default format |
973
|
3 |
|
if( empty( $format ) ) { |
974
|
|
|
/* translators: %s: relative time from now, used for generic date comparisons. "1 day ago", or "20 seconds ago" */ |
975
|
3 |
|
$format = $is_diff ? esc_html__( '%s ago', 'gravityview' ) : get_option( 'date_format' ); |
976
|
|
|
} |
977
|
|
|
|
978
|
|
|
// If raw was specified, don't modify the stored value |
979
|
3 |
|
if ( $is_raw ) { |
980
|
1 |
|
$formatted_date = $date_string; |
981
|
3 |
|
} elseif( $is_timestamp ) { |
982
|
1 |
|
$formatted_date = $date_local_timestamp; |
983
|
3 |
|
} elseif ( $is_diff ) { |
984
|
1 |
|
$formatted_date = sprintf( $format, human_time_diff( $date_gmt_time ) ); |
985
|
|
|
} else { |
986
|
3 |
|
$formatted_date = GFCommon::format_date( $date_string, $is_human, $format, $include_time ); |
987
|
|
|
} |
988
|
|
|
|
989
|
3 |
|
unset( $format, $is_diff, $is_human, $is_timestamp, $is_raw, $date_gmt_time, $date_local_timestamp, $default_atts ); |
990
|
|
|
|
991
|
3 |
|
return $formatted_date; |
992
|
|
|
} |
993
|
|
|
|
994
|
|
|
/** |
995
|
|
|
* Retrieve the label of a given field id (for a specific form) |
996
|
|
|
* |
997
|
|
|
* @access public |
998
|
|
|
* @since 1.17 Added $field_value parameter |
999
|
|
|
* |
1000
|
|
|
* @param array $form Gravity Forms form array |
1001
|
|
|
* @param string $field_id ID of the field. If an input, full input ID (like `1.3`) |
1002
|
|
|
* @param string|array $field_value Raw value of the field. |
1003
|
|
|
* @return string |
1004
|
|
|
*/ |
1005
|
1 |
|
public static function get_field_label( $form = array(), $field_id = '', $field_value = '' ) { |
1006
|
|
|
|
1007
|
1 |
|
if ( empty( $form ) || empty( $field_id ) ) { |
1008
|
|
|
return ''; |
1009
|
|
|
} |
1010
|
|
|
|
1011
|
1 |
|
$field = self::get_field( $form, $field_id ); |
1012
|
|
|
|
1013
|
1 |
|
$label = \GV\Utils::get( $field, 'label' ); |
1014
|
|
|
|
1015
|
1 |
|
if( floor( $field_id ) !== floatval( $field_id ) ) { |
1016
|
1 |
|
$label = GFFormsModel::get_choice_text( $field, $field_value, $field_id ); |
1017
|
|
|
} |
1018
|
|
|
|
1019
|
1 |
|
return $label; |
1020
|
|
|
} |
1021
|
|
|
|
1022
|
|
|
|
1023
|
|
|
/** |
1024
|
|
|
* Returns the field details array of a specific form given the field id |
1025
|
|
|
* |
1026
|
|
|
* Alias of GFFormsModel::get_field |
1027
|
|
|
* |
1028
|
|
|
* @since 1.19 Allow passing form ID as well as form array |
1029
|
|
|
* |
1030
|
|
|
* @uses GFFormsModel::get_field |
1031
|
|
|
* @see GFFormsModel::get_field |
1032
|
|
|
* @access public |
1033
|
|
|
* @param array|int $form Form array or ID |
1034
|
|
|
* @param string|int $field_id |
1035
|
|
|
* @return GF_Field|null Gravity Forms field object, or NULL: Gravity Forms GFFormsModel does not exist or field at $field_id doesn't exist. |
1036
|
|
|
*/ |
1037
|
3 |
|
public static function get_field( $form, $field_id ) { |
1038
|
|
|
|
1039
|
3 |
|
if ( is_numeric( $form ) ) { |
1040
|
|
|
$form = GFAPI::get_form( $form ); |
1041
|
|
|
} |
1042
|
|
|
|
1043
|
3 |
|
if ( class_exists( 'GFFormsModel' ) ){ |
1044
|
3 |
|
return GFFormsModel::get_field( $form, $field_id ); |
1045
|
|
|
} else { |
1046
|
|
|
return null; |
1047
|
|
|
} |
1048
|
|
|
} |
1049
|
|
|
|
1050
|
|
|
|
1051
|
|
|
/** |
1052
|
|
|
* Check whether the post is GravityView |
1053
|
|
|
* |
1054
|
|
|
* - Check post type. Is it `gravityview`? |
1055
|
|
|
* - Check shortcode |
1056
|
|
|
* |
1057
|
|
|
* @param WP_Post $post WordPress post object |
1058
|
|
|
* @return boolean True: yep, GravityView; No: not! |
1059
|
|
|
*/ |
1060
|
2 |
|
public static function has_gravityview_shortcode( $post = null ) { |
1061
|
2 |
|
if ( ! is_a( $post, 'WP_Post' ) ) { |
1062
|
1 |
|
return false; |
1063
|
|
|
} |
1064
|
|
|
|
1065
|
2 |
|
if ( 'gravityview' === get_post_type( $post ) ) { |
1066
|
2 |
|
return true; |
1067
|
|
|
} |
1068
|
|
|
|
1069
|
2 |
|
return self::has_shortcode_r( $post->post_content, 'gravityview' ) ? true : false; |
1070
|
|
|
|
1071
|
|
|
} |
1072
|
|
|
|
1073
|
|
|
|
1074
|
|
|
/** |
1075
|
|
|
* Placeholder until the recursive has_shortcode() patch is merged |
1076
|
|
|
* @see https://core.trac.wordpress.org/ticket/26343#comment:10 |
1077
|
|
|
* @param string $content Content to check whether there's a shortcode |
1078
|
|
|
* @param string $tag Current shortcode tag |
1079
|
|
|
*/ |
1080
|
2 |
|
public static function has_shortcode_r( $content, $tag = 'gravityview' ) { |
1081
|
2 |
|
if ( false === strpos( $content, '[' ) ) { |
1082
|
1 |
|
return false; |
1083
|
|
|
} |
1084
|
|
|
|
1085
|
2 |
|
if ( shortcode_exists( $tag ) ) { |
1086
|
|
|
|
1087
|
2 |
|
$shortcodes = array(); |
1088
|
|
|
|
1089
|
2 |
|
preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ); |
1090
|
2 |
|
if ( empty( $matches ) ){ |
1091
|
1 |
|
return false; |
1092
|
|
|
} |
1093
|
|
|
|
1094
|
2 |
|
foreach ( $matches as $shortcode ) { |
1095
|
2 |
|
if ( $tag === $shortcode[2] ) { |
1096
|
|
|
|
1097
|
|
|
// Changed this to $shortcode instead of true so we get the parsed atts. |
1098
|
2 |
|
$shortcodes[] = $shortcode; |
1099
|
|
|
|
1100
|
1 |
|
} else if ( isset( $shortcode[5] ) && $results = self::has_shortcode_r( $shortcode[5], $tag ) ) { |
1101
|
1 |
|
foreach( $results as $result ) { |
1102
|
2 |
|
$shortcodes[] = $result; |
1103
|
|
|
} |
1104
|
|
|
} |
1105
|
|
|
} |
1106
|
|
|
|
1107
|
2 |
|
return $shortcodes; |
1108
|
|
|
} |
1109
|
|
|
return false; |
1110
|
|
|
} |
1111
|
|
|
|
1112
|
|
|
|
1113
|
|
|
|
1114
|
|
|
/** |
1115
|
|
|
* Get the views for a particular form |
1116
|
|
|
* |
1117
|
|
|
* @since 1.15.2 Add $args array and limit posts_per_page to 500 |
1118
|
|
|
* |
1119
|
|
|
* @uses get_posts() |
1120
|
|
|
* |
1121
|
|
|
* @param int $form_id Gravity Forms form ID |
1122
|
|
|
* @param array $args Pass args sent to get_posts() |
1123
|
|
|
* |
1124
|
|
|
* @return array Array with view details, as returned by get_posts() |
1125
|
|
|
*/ |
1126
|
1 |
|
public static function get_connected_views( $form_id, $args = array() ) { |
1127
|
|
|
|
1128
|
|
|
$defaults = array( |
1129
|
1 |
|
'post_type' => 'gravityview', |
1130
|
1 |
|
'posts_per_page' => 100, |
|
|
|
|
1131
|
1 |
|
'meta_key' => '_gravityview_form_id', |
|
|
|
|
1132
|
1 |
|
'meta_value' => (int)$form_id, |
|
|
|
|
1133
|
|
|
); |
1134
|
|
|
|
1135
|
1 |
|
$args = wp_parse_args( $args, $defaults ); |
1136
|
|
|
|
1137
|
1 |
|
$views = get_posts( $args ); |
1138
|
|
|
|
1139
|
1 |
|
return $views; |
1140
|
|
|
} |
1141
|
|
|
|
1142
|
|
|
/** |
1143
|
|
|
* Get the Gravity Forms form ID connected to a View |
1144
|
|
|
* |
1145
|
|
|
* @param int $view_id The ID of the View to get the connected form of |
1146
|
|
|
* |
1147
|
|
|
* @return false|string ID of the connected Form, if exists. Empty string if not. False if not the View ID isn't valid. |
1148
|
|
|
*/ |
1149
|
1 |
|
public static function get_meta_form_id( $view_id ) { |
1150
|
1 |
|
return get_post_meta( $view_id, '_gravityview_form_id', true ); |
1151
|
|
|
} |
1152
|
|
|
|
1153
|
|
|
/** |
1154
|
|
|
* Get the template ID (`list`, `table`, `datatables`, `map`) for a View |
1155
|
|
|
* |
1156
|
|
|
* @see GravityView_Template::template_id |
1157
|
|
|
* |
1158
|
|
|
* @param int $view_id The ID of the View to get the layout of |
1159
|
|
|
* |
1160
|
|
|
* @return string GravityView_Template::template_id value. Empty string if not. |
1161
|
|
|
*/ |
1162
|
70 |
|
public static function get_meta_template_id( $view_id ) { |
1163
|
70 |
|
return get_post_meta( $view_id, '_gravityview_directory_template', true ); |
1164
|
|
|
} |
1165
|
|
|
|
1166
|
|
|
|
1167
|
|
|
/** |
1168
|
|
|
* Get all the settings for a View |
1169
|
|
|
* |
1170
|
|
|
* @uses \GV\View_Settings::defaults() Parses the settings with the plugin defaults as backups. |
1171
|
|
|
* @param int $post_id View ID |
1172
|
|
|
* @return array Associative array of settings with plugin defaults used if not set by the View |
1173
|
|
|
*/ |
1174
|
69 |
|
public static function get_template_settings( $post_id ) { |
1175
|
|
|
|
1176
|
69 |
|
$settings = get_post_meta( $post_id, '_gravityview_template_settings', true ); |
1177
|
|
|
|
1178
|
69 |
|
if ( class_exists( '\GV\View_Settings' ) ) { |
1179
|
|
|
|
1180
|
69 |
|
return wp_parse_args( (array)$settings, \GV\View_Settings::defaults() ); |
|
|
|
|
1181
|
|
|
|
1182
|
|
|
} |
1183
|
|
|
|
1184
|
|
|
// Backup, in case GravityView_View_Data isn't loaded yet. |
1185
|
|
|
return $settings; |
1186
|
|
|
} |
1187
|
|
|
|
1188
|
|
|
/** |
1189
|
|
|
* Get the setting for a View |
1190
|
|
|
* |
1191
|
|
|
* If the setting isn't set by the View, it returns the plugin default. |
1192
|
|
|
* |
1193
|
|
|
* @param int $post_id View ID |
1194
|
|
|
* @param string $key Key for the setting |
1195
|
|
|
* @return mixed|null Setting value, or NULL if not set. |
1196
|
|
|
*/ |
1197
|
1 |
|
public static function get_template_setting( $post_id, $key ) { |
1198
|
|
|
|
1199
|
1 |
|
$settings = self::get_template_settings( $post_id ); |
1200
|
|
|
|
1201
|
1 |
|
if ( isset( $settings[ $key ] ) ) { |
1202
|
1 |
|
return $settings[ $key ]; |
1203
|
|
|
} |
1204
|
|
|
|
1205
|
|
|
return null; |
1206
|
|
|
} |
1207
|
|
|
|
1208
|
|
|
/** |
1209
|
|
|
* Get the field configuration for the View |
1210
|
|
|
* |
1211
|
|
|
* array( |
1212
|
|
|
* |
1213
|
|
|
* [other zones] |
1214
|
|
|
* |
1215
|
|
|
* 'directory_list-title' => array( |
1216
|
|
|
* |
1217
|
|
|
* [other fields] |
1218
|
|
|
* |
1219
|
|
|
* '5372653f25d44' => array( |
1220
|
|
|
* 'id' => string '9' (length=1) |
1221
|
|
|
* 'label' => string 'Screenshots' (length=11) |
1222
|
|
|
* 'show_label' => string '1' (length=1) |
1223
|
|
|
* 'custom_label' => string '' (length=0) |
1224
|
|
|
* 'custom_class' => string 'gv-gallery' (length=10) |
1225
|
|
|
* 'only_loggedin' => string '0' (length=1) |
1226
|
|
|
* 'only_loggedin_cap' => string 'read' (length=4) |
1227
|
|
|
* ) |
1228
|
|
|
* |
1229
|
|
|
* [other fields] |
1230
|
|
|
* ) |
1231
|
|
|
* |
1232
|
|
|
* [other zones] |
1233
|
|
|
* ) |
1234
|
|
|
* |
1235
|
|
|
* @since 1.17.4 Added $apply_filter parameter |
1236
|
|
|
* |
1237
|
|
|
* @param int $post_id View ID |
1238
|
|
|
* @param bool $apply_filter Whether to apply the `gravityview/configuration/fields` filter [Default: true] |
1239
|
|
|
* @return array Multi-array of fields with first level being the field zones. See code comment. |
1240
|
|
|
*/ |
1241
|
1 |
|
public static function get_directory_fields( $post_id, $apply_filter = true ) { |
1242
|
1 |
|
$fields = get_post_meta( $post_id, '_gravityview_directory_fields', true ); |
1243
|
|
|
|
1244
|
1 |
|
if ( $apply_filter ) { |
1245
|
|
|
/** |
1246
|
|
|
* @filter `gravityview/configuration/fields` Filter the View fields' configuration array |
1247
|
|
|
* @since 1.6.5 |
1248
|
|
|
* |
1249
|
|
|
* @param $fields array Multi-array of fields with first level being the field zones |
1250
|
|
|
* @param $post_id int Post ID |
1251
|
|
|
*/ |
1252
|
1 |
|
$fields = apply_filters( 'gravityview/configuration/fields', $fields, $post_id ); |
1253
|
|
|
|
1254
|
|
|
/** |
1255
|
|
|
* @filter `gravityview/view/configuration/fields` Filter the View fields' configuration array. |
1256
|
|
|
* @since 2.0 |
1257
|
|
|
* |
1258
|
|
|
* @param array $fields Multi-array of fields with first level being the field zones. |
1259
|
|
|
* @param \GV\View $view The View the fields are being pulled for. |
1260
|
|
|
*/ |
1261
|
1 |
|
$fields = apply_filters( 'gravityview/view/configuration/fields', $fields, \GV\View::by_id( $post_id ) ); |
1262
|
|
|
} |
1263
|
|
|
|
1264
|
1 |
|
return $fields; |
1265
|
|
|
} |
1266
|
|
|
|
1267
|
|
|
/** |
1268
|
|
|
* Get the widget configuration for a View |
1269
|
|
|
* |
1270
|
|
|
* @param int $view_id View ID |
1271
|
|
|
* @param bool $json_decode Whether to JSON-decode the widget values. Default: `false` |
1272
|
|
|
* |
1273
|
|
|
* @return array Multi-array of widgets, with the slug of each widget "zone" being the key ("header_top"), and each widget having their own "id" |
1274
|
|
|
*/ |
1275
|
|
|
public static function get_directory_widgets( $view_id, $json_decode = false ) { |
1276
|
|
|
|
1277
|
|
|
$view_widgets = get_post_meta( $view_id, '_gravityview_directory_widgets', true ); |
1278
|
|
|
|
1279
|
|
|
$defaults = array( |
1280
|
|
|
'header_top' => array(), |
1281
|
|
|
'header_left' => array(), |
1282
|
|
|
'header_right' => array(), |
1283
|
|
|
'footer_left' => array(), |
1284
|
|
|
'footer_right' => array(), |
1285
|
|
|
); |
1286
|
|
|
|
1287
|
|
|
$directory_widgets = wp_parse_args( $view_widgets, $defaults ); |
1288
|
|
|
|
1289
|
|
|
if( $json_decode ) { |
1290
|
|
|
$directory_widgets = gv_map_deep( $directory_widgets, 'gv_maybe_json_decode' ); |
1291
|
|
|
} |
1292
|
|
|
|
1293
|
|
|
return $directory_widgets; |
1294
|
|
|
} |
1295
|
|
|
|
1296
|
|
|
|
1297
|
|
|
/** |
1298
|
|
|
* Render dropdown (select) with the list of sortable fields from a form ID |
1299
|
|
|
* |
1300
|
|
|
* @access public |
1301
|
|
|
* @param int $formid Form ID |
1302
|
|
|
* @return string html |
1303
|
|
|
*/ |
1304
|
|
|
public static function get_sortable_fields( $formid, $current = '' ) { |
1305
|
|
|
$output = '<option value="" ' . selected( '', $current, false ).'>' . esc_html__( 'Default', 'gravityview' ) .'</option>'; |
1306
|
|
|
|
1307
|
|
|
if ( empty( $formid ) ) { |
1308
|
|
|
return $output; |
1309
|
|
|
} |
1310
|
|
|
|
1311
|
|
|
$fields = self::get_sortable_fields_array( $formid ); |
1312
|
|
|
|
1313
|
|
|
if ( ! empty( $fields ) ) { |
1314
|
|
|
|
1315
|
|
|
$blacklist_field_types = apply_filters( 'gravityview_blacklist_field_types', array( 'list', 'textarea' ), null ); |
1316
|
|
|
|
1317
|
|
|
foreach ( $fields as $id => $field ) { |
1318
|
|
|
if ( in_array( $field['type'], $blacklist_field_types ) ) { |
1319
|
|
|
continue; |
1320
|
|
|
} |
1321
|
|
|
|
1322
|
|
|
$output .= '<option value="'. $id .'" '. selected( $id, $current, false ).'>'. esc_attr( $field['label'] ) .'</option>'; |
1323
|
|
|
} |
1324
|
|
|
} |
1325
|
|
|
|
1326
|
|
|
return $output; |
1327
|
|
|
} |
1328
|
|
|
|
1329
|
|
|
/** |
1330
|
|
|
* |
1331
|
|
|
* @param int $formid Gravity Forms form ID |
1332
|
|
|
* @param array $blacklist Field types to exclude |
1333
|
|
|
* |
1334
|
|
|
* @since 1.8 |
1335
|
|
|
* |
1336
|
|
|
* @todo Get all fields, check if sortable dynamically |
1337
|
|
|
* |
1338
|
|
|
* @return array |
1339
|
|
|
*/ |
1340
|
|
|
public static function get_sortable_fields_array( $formid, $blacklist = array( 'list', 'textarea' ) ) { |
1341
|
|
|
|
1342
|
|
|
// Get fields with sub-inputs and no parent |
1343
|
|
|
$fields = self::get_form_fields( $formid, true, false ); |
1344
|
|
|
|
1345
|
|
|
$date_created = array( |
1346
|
|
|
'date_created' => array( |
1347
|
|
|
'type' => 'date_created', |
1348
|
|
|
'label' => __( 'Date Created', 'gravityview' ), |
1349
|
|
|
), |
1350
|
|
|
); |
1351
|
|
|
|
1352
|
|
|
$fields = $date_created + $fields; |
1353
|
|
|
|
1354
|
|
|
$blacklist_field_types = apply_filters( 'gravityview_blacklist_field_types', $blacklist, NULL ); |
|
|
|
|
1355
|
|
|
|
1356
|
|
|
// TODO: Convert to using array_filter |
1357
|
|
|
foreach( $fields as $id => $field ) { |
1358
|
|
|
|
1359
|
|
|
if( in_array( $field['type'], $blacklist_field_types ) ) { |
1360
|
|
|
unset( $fields[ $id ] ); |
1361
|
|
|
} |
1362
|
|
|
} |
1363
|
|
|
|
1364
|
|
|
/** |
1365
|
|
|
* @filter `gravityview/common/sortable_fields` Filter the sortable fields |
1366
|
|
|
* @since 1.12 |
1367
|
|
|
* @param array $fields Sub-set of GF form fields that are sortable |
1368
|
|
|
* @param int $formid The Gravity Forms form ID that the fields are from |
1369
|
|
|
*/ |
1370
|
|
|
$fields = apply_filters( 'gravityview/common/sortable_fields', $fields, $formid ); |
1371
|
|
|
|
1372
|
|
|
return $fields; |
1373
|
|
|
} |
1374
|
|
|
|
1375
|
|
|
/** |
1376
|
|
|
* Returns the GF Form field type for a certain field(id) of a form |
1377
|
|
|
* @param object $form Gravity Forms form |
1378
|
|
|
* @param mixed $field_id Field ID or Field array |
1379
|
|
|
* @return string field type |
1380
|
|
|
*/ |
1381
|
|
|
public static function get_field_type( $form = null, $field_id = '' ) { |
1382
|
|
|
|
1383
|
|
|
if ( ! empty( $field_id ) && ! is_array( $field_id ) ) { |
1384
|
|
|
$field = self::get_field( $form, $field_id ); |
1385
|
|
|
} else { |
1386
|
|
|
$field = $field_id; |
1387
|
|
|
} |
1388
|
|
|
|
1389
|
|
|
return class_exists( 'RGFormsModel' ) ? RGFormsModel::get_input_type( $field ) : ''; |
1390
|
|
|
|
1391
|
|
|
} |
1392
|
|
|
|
1393
|
|
|
|
1394
|
|
|
/** |
1395
|
|
|
* Checks if the field type is a 'numeric' field type (e.g. to be used when sorting) |
1396
|
|
|
* @param int|array $form form ID or form array |
1397
|
|
|
* @param int|array $field field key or field array |
1398
|
|
|
* @return boolean |
1399
|
|
|
*/ |
1400
|
|
|
public static function is_field_numeric( $form = null, $field = '' ) { |
1401
|
|
|
|
1402
|
|
|
if ( ! is_array( $form ) && ! is_array( $field ) ) { |
1403
|
|
|
$form = self::get_form( $form ); |
1404
|
|
|
} |
1405
|
|
|
|
1406
|
|
|
// If entry meta, it's a string. Otherwise, numeric |
1407
|
|
|
if( ! is_numeric( $field ) && is_string( $field ) ) { |
1408
|
|
|
$type = $field; |
1409
|
|
|
} else { |
1410
|
|
|
$type = self::get_field_type( $form, $field ); |
1411
|
|
|
} |
1412
|
|
|
|
1413
|
|
|
/** |
1414
|
|
|
* @filter `gravityview/common/numeric_types` What types of fields are numeric? |
1415
|
|
|
* @since 1.5.2 |
1416
|
|
|
* @param array $numeric_types Fields that are numeric. Default: `[ number, time ]` |
1417
|
|
|
*/ |
1418
|
|
|
$numeric_types = apply_filters( 'gravityview/common/numeric_types', array( 'number', 'time' ) ); |
1419
|
|
|
|
1420
|
|
|
// Defer to GravityView_Field setting, if the field type is registered and `is_numeric` is true |
1421
|
|
|
if( $gv_field = GravityView_Fields::get( $type ) ) { |
1422
|
|
|
if( true === $gv_field->is_numeric ) { |
1423
|
|
|
$numeric_types[] = $gv_field->is_numeric; |
1424
|
|
|
} |
1425
|
|
|
} |
1426
|
|
|
|
1427
|
|
|
$return = in_array( $type, $numeric_types ); |
1428
|
|
|
|
1429
|
|
|
return $return; |
1430
|
|
|
} |
1431
|
|
|
|
1432
|
|
|
/** |
1433
|
|
|
* Encrypt content using Javascript so that it's hidden when JS is disabled. |
1434
|
|
|
* |
1435
|
|
|
* This is mostly used to hide email addresses from scraper bots. |
1436
|
|
|
* |
1437
|
|
|
* @param string $content Content to encrypt |
1438
|
|
|
* @param string $message Message shown if Javascript is disabled |
1439
|
|
|
* |
1440
|
|
|
* @see https://github.com/katzwebservices/standalone-phpenkoder StandalonePHPEnkoder on Github |
1441
|
|
|
* |
1442
|
|
|
* @since 1.7 |
1443
|
|
|
* |
1444
|
|
|
* @return string Content, encrypted |
1445
|
|
|
*/ |
1446
|
|
|
public static function js_encrypt( $content, $message = '' ) { |
1447
|
|
|
|
1448
|
|
|
$output = $content; |
1449
|
|
|
|
1450
|
|
|
if ( ! class_exists( 'StandalonePHPEnkoder' ) ) { |
1451
|
|
|
include_once( GRAVITYVIEW_DIR . 'includes/lib/StandalonePHPEnkoder.php' ); |
1452
|
|
|
} |
1453
|
|
|
|
1454
|
|
|
if ( class_exists( 'StandalonePHPEnkoder' ) ) { |
1455
|
|
|
|
1456
|
|
|
$enkoder = new StandalonePHPEnkoder; |
1457
|
|
|
|
1458
|
|
|
$message = empty( $message ) ? __( 'Email hidden; Javascript is required.', 'gravityview' ) : $message; |
1459
|
|
|
|
1460
|
|
|
/** |
1461
|
|
|
* @filter `gravityview/phpenkoder/msg` Modify the message shown when Javascript is disabled and an encrypted email field is displayed |
1462
|
|
|
* @since 1.7 |
1463
|
|
|
* @param string $message Existing message |
1464
|
|
|
* @param string $content Content to encrypt |
1465
|
|
|
*/ |
1466
|
|
|
$enkoder->enkode_msg = apply_filters( 'gravityview/phpenkoder/msg', $message, $content ); |
1467
|
|
|
|
1468
|
|
|
$output = $enkoder->enkode( $content ); |
1469
|
|
|
} |
1470
|
|
|
|
1471
|
|
|
return $output; |
1472
|
|
|
} |
1473
|
|
|
|
1474
|
|
|
/** |
1475
|
|
|
* |
1476
|
|
|
* Do the same than parse_str without max_input_vars limitation: |
1477
|
|
|
* Parses $string as if it were the query string passed via a URL and sets variables in the current scope. |
1478
|
|
|
* @param $string array string to parse (not altered like in the original parse_str(), use the second parameter!) |
1479
|
|
|
* @param $result array If the second parameter is present, variables are stored in this variable as array elements |
1480
|
|
|
* @return bool true or false if $string is an empty string |
1481
|
|
|
* @since 1.5.3 |
1482
|
|
|
* |
1483
|
|
|
* @author rubo77 at https://gist.github.com/rubo77/6821632 |
1484
|
|
|
**/ |
1485
|
|
|
public static function gv_parse_str( $string, &$result ) { |
1486
|
|
|
if ( empty( $string ) ) { |
1487
|
|
|
return false; |
1488
|
|
|
} |
1489
|
|
|
|
1490
|
|
|
$result = array(); |
1491
|
|
|
|
1492
|
|
|
// find the pairs "name=value" |
1493
|
|
|
$pairs = explode( '&', $string ); |
1494
|
|
|
|
1495
|
|
|
foreach ( $pairs as $pair ) { |
1496
|
|
|
// use the original parse_str() on each element |
1497
|
|
|
parse_str( $pair, $params ); |
1498
|
|
|
|
1499
|
|
|
$k = key( $params ); |
1500
|
|
|
if ( ! isset( $result[ $k ] ) ) { |
1501
|
|
|
$result += $params; |
1502
|
|
|
} elseif ( array_key_exists( $k, $params ) && is_array( $params[ $k ] ) ) { |
1503
|
|
|
$result[ $k ] = self::array_merge_recursive_distinct( $result[ $k ], $params[ $k ] ); |
1504
|
|
|
} |
1505
|
|
|
} |
1506
|
|
|
return true; |
1507
|
|
|
} |
1508
|
|
|
|
1509
|
|
|
|
1510
|
|
|
/** |
1511
|
|
|
* Generate an HTML anchor tag with a list of supported attributes |
1512
|
|
|
* |
1513
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a Supported attributes defined here |
1514
|
|
|
* @uses esc_url_raw() to sanitize $href |
1515
|
|
|
* @uses esc_attr() to sanitize $atts |
1516
|
|
|
* |
1517
|
|
|
* @since 1.6 |
1518
|
|
|
* |
1519
|
|
|
* @param string $href URL of the link. Sanitized using `esc_url_raw()` |
1520
|
|
|
* @param string $anchor_text The text or HTML inside the anchor. This is not sanitized in the function. |
1521
|
|
|
* @param array|string $atts Attributes to be added to the anchor tag. Parsed by `wp_parse_args()`, sanitized using `esc_attr()` |
1522
|
|
|
* |
1523
|
|
|
* @return string HTML output of anchor link. If empty $href, returns NULL |
1524
|
|
|
*/ |
1525
|
25 |
|
public static function get_link_html( $href = '', $anchor_text = '', $atts = array() ) { |
1526
|
|
|
|
1527
|
|
|
// Supported attributes for anchor tags. HREF left out intentionally. |
1528
|
|
|
$allowed_atts = array( |
1529
|
25 |
|
'href' => null, // Will override the $href argument if set |
1530
|
|
|
'title' => null, |
1531
|
|
|
'rel' => null, |
1532
|
|
|
'id' => null, |
1533
|
|
|
'class' => null, |
1534
|
|
|
'target' => null, |
1535
|
|
|
'style' => null, |
1536
|
|
|
|
1537
|
|
|
// Used by GravityView |
1538
|
|
|
'data-viewid' => null, |
1539
|
|
|
|
1540
|
|
|
// Not standard |
1541
|
|
|
'hreflang' => null, |
1542
|
|
|
'type' => null, |
1543
|
|
|
'tabindex' => null, |
1544
|
|
|
|
1545
|
|
|
// Deprecated HTML4 but still used |
1546
|
|
|
'name' => null, |
1547
|
|
|
'onclick' => null, |
1548
|
|
|
'onchange' => null, |
1549
|
|
|
'onkeyup' => null, |
1550
|
|
|
|
1551
|
|
|
// HTML5 only |
1552
|
|
|
'download' => null, |
1553
|
|
|
'media' => null, |
1554
|
|
|
'ping' => null, |
1555
|
|
|
); |
1556
|
|
|
|
1557
|
|
|
/** |
1558
|
|
|
* @filter `gravityview/get_link/allowed_atts` Modify the attributes that are allowed to be used in generating links |
1559
|
|
|
* @param array $allowed_atts Array of attributes allowed |
1560
|
|
|
*/ |
1561
|
25 |
|
$allowed_atts = apply_filters( 'gravityview/get_link/allowed_atts', $allowed_atts ); |
1562
|
|
|
|
1563
|
|
|
// Make sure the attributes are formatted as array |
1564
|
25 |
|
$passed_atts = wp_parse_args( $atts ); |
1565
|
|
|
|
1566
|
|
|
// Make sure the allowed attributes are only the ones in the $allowed_atts list |
1567
|
25 |
|
$final_atts = shortcode_atts( $allowed_atts, $passed_atts ); |
1568
|
|
|
|
1569
|
|
|
// Remove attributes with empty values |
1570
|
25 |
|
$final_atts = array_filter( $final_atts ); |
1571
|
|
|
|
1572
|
|
|
// If the href wasn't passed as an attribute, use the value passed to the function |
1573
|
25 |
|
if ( empty( $final_atts['href'] ) && ! empty( $href ) ) { |
1574
|
25 |
|
$final_atts['href'] = $href; |
1575
|
|
|
} |
1576
|
|
|
|
1577
|
25 |
|
$final_atts['href'] = esc_url_raw( $href ); |
1578
|
|
|
|
1579
|
|
|
/** |
1580
|
|
|
* Fix potential security issue with target=_blank |
1581
|
|
|
* @see https://dev.to/ben/the-targetblank-vulnerability-by-example |
1582
|
|
|
*/ |
1583
|
25 |
|
if( '_blank' === \GV\Utils::get( $final_atts, 'target' ) ) { |
1584
|
4 |
|
$final_atts['rel'] = trim( \GV\Utils::get( $final_atts, 'rel', '' ) . ' noopener noreferrer' ); |
1585
|
|
|
} |
1586
|
|
|
|
1587
|
|
|
// Sort the attributes alphabetically, to help testing |
1588
|
25 |
|
ksort( $final_atts ); |
1589
|
|
|
|
1590
|
|
|
// For each attribute, generate the code |
1591
|
25 |
|
$output = ''; |
1592
|
25 |
|
foreach ( $final_atts as $attr => $value ) { |
1593
|
25 |
|
$output .= sprintf( ' %s="%s"', $attr, esc_attr( $value ) ); |
1594
|
|
|
} |
1595
|
|
|
|
1596
|
25 |
|
if( '' !== $output ) { |
1597
|
25 |
|
$output = '<a' . $output . '>' . $anchor_text . '</a>'; |
1598
|
|
|
} |
1599
|
|
|
|
1600
|
25 |
|
return $output; |
1601
|
|
|
} |
1602
|
|
|
|
1603
|
|
|
/** |
1604
|
|
|
* array_merge_recursive does indeed merge arrays, but it converts values with duplicate |
1605
|
|
|
* keys to arrays rather than overwriting the value in the first array with the duplicate |
1606
|
|
|
* value in the second array, as array_merge does. |
1607
|
|
|
* |
1608
|
|
|
* @see http://php.net/manual/en/function.array-merge-recursive.php |
1609
|
|
|
* |
1610
|
|
|
* @since 1.5.3 |
1611
|
|
|
* @param array $array1 |
1612
|
|
|
* @param array $array2 |
1613
|
|
|
* @return array |
1614
|
|
|
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk> |
1615
|
|
|
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com> |
1616
|
|
|
*/ |
1617
|
|
|
public static function array_merge_recursive_distinct( array &$array1, array &$array2 ) { |
1618
|
|
|
$merged = $array1; |
1619
|
|
|
foreach ( $array2 as $key => $value ) { |
1620
|
|
|
if ( is_array( $value ) && isset( $merged[ $key ] ) && is_array( $merged[ $key ] ) ) { |
1621
|
|
|
$merged[ $key ] = self::array_merge_recursive_distinct( $merged[ $key ], $value ); |
1622
|
|
|
} else if ( is_numeric( $key ) && isset( $merged[ $key ] ) ) { |
1623
|
|
|
$merged[] = $value; |
1624
|
|
|
} else { |
1625
|
|
|
$merged[ $key ] = $value; |
1626
|
|
|
} |
1627
|
|
|
} |
1628
|
|
|
|
1629
|
|
|
return $merged; |
1630
|
|
|
} |
1631
|
|
|
|
1632
|
|
|
/** |
1633
|
|
|
* Get WordPress users with reasonable limits set |
1634
|
|
|
* |
1635
|
|
|
* @param string $context Where are we using this information (e.g. change_entry_creator, search_widget ..) |
1636
|
|
|
* @param array $args Arguments to modify the user query. See get_users() {@since 1.14} |
1637
|
|
|
* @return array Array of WP_User objects. |
1638
|
|
|
*/ |
1639
|
|
|
public static function get_users( $context = 'change_entry_creator', $args = array() ) { |
1640
|
|
|
|
1641
|
|
|
$default_args = array( |
1642
|
|
|
'number' => 2000, |
1643
|
|
|
'orderby' => 'display_name', |
1644
|
|
|
'order' => 'ASC', |
1645
|
|
|
'fields' => array( 'ID', 'display_name', 'user_login', 'user_nicename' ) |
1646
|
|
|
); |
1647
|
|
|
|
1648
|
|
|
// Merge in the passed arg |
1649
|
|
|
$get_users_settings = wp_parse_args( $args, $default_args ); |
1650
|
|
|
|
1651
|
|
|
/** |
1652
|
|
|
* @filter `gravityview/get_users/{$context}` There are issues with too many users using [get_users()](http://codex.wordpress.org/Function_Reference/get_users) where it breaks the select. We try to keep it at a reasonable number. \n |
1653
|
|
|
* `$context` is where are we using this information (e.g. change_entry_creator, search_widget ..) |
1654
|
|
|
* @param array $settings Settings array, with `number` key defining the # of users to display |
1655
|
|
|
*/ |
1656
|
|
|
$get_users_settings = apply_filters( 'gravityview/get_users/'. $context, apply_filters( 'gravityview_change_entry_creator_user_parameters', $get_users_settings ) ); |
1657
|
|
|
|
1658
|
|
|
return get_users( $get_users_settings ); |
1659
|
|
|
} |
1660
|
|
|
|
1661
|
|
|
|
1662
|
|
|
/** |
1663
|
|
|
* Display updated/error notice |
1664
|
|
|
* |
1665
|
|
|
* @since 1.19.2 Added $cap and $object_id parameters |
1666
|
|
|
* |
1667
|
|
|
* @param string $notice text/HTML of notice |
1668
|
|
|
* @param string $class CSS class for notice (`updated` or `error`) |
1669
|
|
|
* @param string $cap [Optional] Define a capability required to show a notice. If not set, displays to all caps. |
1670
|
|
|
* |
1671
|
|
|
* @return string |
1672
|
|
|
*/ |
1673
|
4 |
|
public static function generate_notice( $notice, $class = '', $cap = '', $object_id = null ) { |
1674
|
|
|
|
1675
|
|
|
// If $cap is defined, only show notice if user has capability |
1676
|
4 |
|
if( $cap && ! GVCommon::has_cap( $cap, $object_id ) ) { |
1677
|
4 |
|
return ''; |
1678
|
|
|
} |
1679
|
|
|
|
1680
|
|
|
return '<div class="gv-notice '.gravityview_sanitize_html_class( $class ) .'">'. $notice .'</div>'; |
1681
|
|
|
} |
1682
|
|
|
|
1683
|
|
|
/** |
1684
|
|
|
* Inspired on \GFCommon::encode_shortcodes, reverse the encoding by replacing the ascii characters by the shortcode brackets |
1685
|
|
|
* @since 1.16.5 |
1686
|
|
|
* @param string $string Input string to decode |
1687
|
|
|
* @return string $string Output string |
1688
|
|
|
*/ |
1689
|
|
|
public static function decode_shortcodes( $string ) { |
1690
|
|
|
$replace = array( '[', ']', '"' ); |
1691
|
|
|
$find = array( '[', ']', '"' ); |
1692
|
|
|
$string = str_replace( $find, $replace, $string ); |
1693
|
|
|
|
1694
|
|
|
return $string; |
1695
|
|
|
} |
1696
|
|
|
|
1697
|
|
|
|
1698
|
|
|
/** |
1699
|
|
|
* Send email using GFCommon::send_email() |
1700
|
|
|
* |
1701
|
|
|
* @since 1.17 |
1702
|
|
|
* |
1703
|
|
|
* @see GFCommon::send_email This just makes the method public |
1704
|
|
|
* |
1705
|
|
|
* @param string $from Sender address (required) |
1706
|
|
|
* @param string $to Recipient address (required) |
1707
|
|
|
* @param string $bcc BCC recipients (required) |
1708
|
|
|
* @param string $reply_to Reply-to address (required) |
1709
|
|
|
* @param string $subject Subject line (required) |
1710
|
|
|
* @param string $message Message body (required) |
1711
|
|
|
* @param string $from_name Displayed name of the sender |
1712
|
|
|
* @param string $message_format If "html", sent text as `text/html`. Otherwise, `text/plain`. Default: "html". |
1713
|
|
|
* @param string|array $attachments Optional. Files to attach. {@see wp_mail()} for usage. Default: "". |
1714
|
|
|
* @param array|false $entry Gravity Forms entry array, related to the email. Default: false. |
1715
|
|
|
* @param array|false $notification Gravity Forms notification that triggered the email. {@see GFCommon::send_notification}. Default:false. |
1716
|
|
|
*/ |
1717
|
|
|
public static function send_email( $from, $to, $bcc, $reply_to, $subject, $message, $from_name = '', $message_format = 'html', $attachments = '', $entry = false, $notification = false ) { |
1718
|
|
|
|
1719
|
|
|
$SendEmail = new ReflectionMethod( 'GFCommon', 'send_email' ); |
1720
|
|
|
|
1721
|
|
|
// It was private; let's make it public |
1722
|
|
|
$SendEmail->setAccessible( true ); |
1723
|
|
|
|
1724
|
|
|
// Required: $from, $to, $bcc, $replyTo, $subject, $message |
|
|
|
|
1725
|
|
|
// Optional: $from_name, $message_format, $attachments, $lead, $notification |
|
|
|
|
1726
|
|
|
$SendEmail->invoke( new GFCommon, $from, $to, $bcc, $reply_to, $subject, $message, $from_name, $message_format, $attachments, $entry, $notification ); |
1727
|
|
|
} |
1728
|
|
|
|
1729
|
|
|
|
1730
|
|
|
} //end class |
1731
|
|
|
|
1732
|
|
|
|
1733
|
|
|
/** |
1734
|
|
|
* Generate an HTML anchor tag with a list of supported attributes |
1735
|
|
|
* |
1736
|
|
|
* @see GVCommon::get_link_html() |
1737
|
|
|
* |
1738
|
|
|
* @since 1.6 |
1739
|
|
|
* |
1740
|
|
|
* @param string $href URL of the link. |
1741
|
|
|
* @param string $anchor_text The text or HTML inside the anchor. This is not sanitized in the function. |
1742
|
|
|
* @param array|string $atts Attributes to be added to the anchor tag |
1743
|
|
|
* |
1744
|
|
|
* @return string HTML output of anchor link. If empty $href, returns NULL |
1745
|
|
|
*/ |
1746
|
|
|
function gravityview_get_link( $href = '', $anchor_text = '', $atts = array() ) { |
1747
|
24 |
|
return GVCommon::get_link_html( $href, $anchor_text, $atts ); |
1748
|
|
|
} |
1749
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.