1
|
|
|
<?php |
2
|
|
|
if ( ! defined('ABSPATH') ) { |
3
|
|
|
die( 'You are not allowed to call this page directly.' ); |
4
|
|
|
} |
5
|
|
|
|
6
|
|
|
class FrmField { |
7
|
|
|
static $use_cache = true; |
8
|
|
|
static $transient_size = 200; |
9
|
|
|
|
10
|
|
|
public static function field_selection() { |
11
|
|
|
$fields = apply_filters('frm_available_fields', array( |
12
|
|
|
'text' => __( 'Single Line Text', 'formidable' ), |
13
|
|
|
'textarea' => __( 'Paragraph Text', 'formidable' ), |
14
|
|
|
'checkbox' => __( 'Checkboxes', 'formidable' ), |
15
|
|
|
'radio' => __( 'Radio Buttons', 'formidable' ), |
16
|
|
|
'select' => __( 'Dropdown', 'formidable' ), |
17
|
|
|
'email' => __( 'Email Address', 'formidable' ), |
18
|
|
|
'url' => __( 'Website/URL', 'formidable' ), |
19
|
|
|
'captcha' => __( 'reCAPTCHA', 'formidable' ), |
20
|
|
|
)); |
21
|
|
|
|
22
|
|
|
return $fields; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function pro_field_selection() { |
26
|
|
|
return apply_filters('frm_pro_available_fields', array( |
27
|
|
|
'end_divider' => array( |
28
|
|
|
'name' => __( 'End Section', 'formidable' ), |
29
|
|
|
'switch_from' => 'divider', |
30
|
|
|
), |
31
|
|
|
'divider' => __( 'Section', 'formidable' ), |
32
|
|
|
'break' => __( 'Page Break', 'formidable' ), |
33
|
|
|
'file' => __( 'File Upload', 'formidable' ), |
34
|
|
|
'rte' => __( 'Rich Text', 'formidable' ), |
35
|
|
|
'number' => __( 'Number', 'formidable' ), |
36
|
|
|
'phone' => __( 'Phone Number', 'formidable' ), |
37
|
|
|
'date' => __( 'Date', 'formidable' ), |
38
|
|
|
'time' => __( 'Time', 'formidable' ), |
39
|
|
|
'image' => __( 'Image URL', 'formidable' ), |
40
|
|
|
'scale' => __( 'Scale', 'formidable' ), |
41
|
|
|
'data' => __( 'Dynamic Field', 'formidable' ), |
42
|
|
|
'lookup' => __( 'Lookup', 'formidable' ), |
43
|
|
|
'form' => __( 'Embed Form', 'formidable' ), |
44
|
|
|
'hidden' => __( 'Hidden Field', 'formidable' ), |
45
|
|
|
'user_id' => __( 'User ID (hidden)', 'formidable' ), |
46
|
|
|
'password' => __( 'Password', 'formidable' ), |
47
|
|
|
'html' => __( 'HTML', 'formidable' ), |
48
|
|
|
'tag' => __( 'Tags', 'formidable' ), |
49
|
|
|
'credit_card' => __( 'Credit Card', 'formidable' ), |
50
|
|
|
'address' => __( 'Address', 'formidable' ), |
51
|
|
|
)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function create( $values, $return = true ) { |
55
|
|
|
global $wpdb, $frm_duplicate_ids; |
56
|
|
|
|
57
|
|
|
$new_values = array(); |
58
|
|
|
$key = isset($values['field_key']) ? $values['field_key'] : $values['name']; |
59
|
|
|
$new_values['field_key'] = FrmAppHelper::get_unique_key( $key, $wpdb->prefix . 'frm_fields', 'field_key' ); |
60
|
|
|
|
61
|
|
|
foreach ( array( 'name', 'description', 'type', 'default_value' ) as $col ) { |
62
|
|
|
$new_values[ $col ] = $values[ $col ]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$new_values['options'] = $values['options']; |
66
|
|
|
|
67
|
|
|
$new_values['field_order'] = isset($values['field_order']) ? (int) $values['field_order'] : null; |
68
|
|
|
$new_values['required'] = isset($values['required']) ? (int) $values['required'] : 0; |
69
|
|
|
$new_values['form_id'] = isset($values['form_id']) ? (int) $values['form_id'] : null; |
70
|
|
|
$new_values['field_options'] = $values['field_options']; |
71
|
|
|
$new_values['created_at'] = current_time('mysql', 1); |
72
|
|
|
|
73
|
|
|
if ( isset( $values['id'] ) ) { |
74
|
|
|
$frm_duplicate_ids[ $values['field_key'] ] = $new_values['field_key']; |
75
|
|
|
$new_values = apply_filters('frm_duplicated_field', $new_values); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ( $new_values as $k => $v ) { |
79
|
|
|
if ( is_array( $v ) ) { |
80
|
|
|
$new_values[ $k ] = serialize( $v ); |
81
|
|
|
} |
82
|
|
|
unset( $k, $v ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
//if(isset($values['id']) and is_numeric($values['id'])) |
86
|
|
|
// $new_values['id'] = $values['id']; |
87
|
|
|
|
88
|
|
|
$query_results = $wpdb->insert( $wpdb->prefix . 'frm_fields', $new_values ); |
89
|
|
|
$new_id = 0; |
90
|
|
|
if ( $query_results ) { |
91
|
|
|
self::delete_form_transient( $new_values['form_id'] ); |
92
|
|
|
$new_id = $wpdb->insert_id; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ( ! $return ) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ( $query_results ) { |
100
|
|
|
if ( isset( $values['id'] ) ) { |
101
|
|
|
$frm_duplicate_ids[ $values['id'] ] = $new_id; |
102
|
|
|
} |
103
|
|
|
return $new_id; |
104
|
|
|
} else { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public static function duplicate( $old_form_id, $form_id, $copy_keys = false, $blog_id = false ) { |
110
|
|
|
global $frm_duplicate_ids; |
111
|
|
|
|
112
|
|
|
$where = array( array( 'or' => 1, 'fi.form_id' => $old_form_id, 'fr.parent_form_id' => $old_form_id ) ); |
113
|
|
|
$fields = self::getAll( $where, 'field_order', '', $blog_id ); |
114
|
|
|
|
115
|
|
|
foreach ( (array) $fields as $field ) { |
116
|
|
|
$new_key = ($copy_keys) ? $field->field_key : ''; |
117
|
|
|
if ( $copy_keys && substr($field->field_key, -1) == 2 ) { |
118
|
|
|
$new_key = rtrim($new_key, 2); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$values = array(); |
122
|
|
|
FrmFieldsHelper::fill_field( $values, $field, $form_id, $new_key ); |
123
|
|
|
|
124
|
|
|
// If this is a repeating section, create new form |
125
|
|
|
if ( self::is_repeating_field( $field ) ) { |
126
|
|
|
// create the repeatable form |
127
|
|
|
$new_repeat_form_id = apply_filters( 'frm_create_repeat_form', 0, array( 'parent_form_id' => $form_id, 'field_name' => $field->name ) ); |
128
|
|
|
|
129
|
|
|
// Save old form_select |
130
|
|
|
$old_repeat_form_id = $field->field_options['form_select']; |
131
|
|
|
|
132
|
|
|
// Update form_select for repeating field |
133
|
|
|
$values['field_options']['form_select'] = $new_repeat_form_id; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// If this is a field inside of a repeating section, associate it with the correct form |
137
|
|
|
if ( $field->form_id != $old_form_id && isset( $old_repeat_form_id ) && isset( $new_repeat_form_id ) && $field->form_id == $old_repeat_form_id ) { |
138
|
|
|
$values['form_id'] = $new_repeat_form_id; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$values = apply_filters('frm_duplicated_field', $values); |
142
|
|
|
$new_id = self::create($values); |
143
|
|
|
$frm_duplicate_ids[ $field->id ] = $new_id; |
144
|
|
|
$frm_duplicate_ids[ $field->field_key ] = $new_id; |
145
|
|
|
unset($field); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public static function update( $id, $values ) { |
150
|
|
|
global $wpdb; |
151
|
|
|
|
152
|
|
|
$id = absint( $id ); |
153
|
|
|
|
154
|
|
|
if ( isset( $values['field_key'] ) ) { |
155
|
|
|
$values['field_key'] = FrmAppHelper::get_unique_key( $values['field_key'], $wpdb->prefix . 'frm_fields', 'field_key', $id ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ( isset($values['required']) ) { |
159
|
|
|
$values['required'] = (int) $values['required']; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
self::preserve_phone_format_backslashes( $values ); |
163
|
|
|
|
164
|
|
|
if ( isset( $values['type'] ) ) { |
165
|
|
|
$values = apply_filters( 'frm_clean_' . $values['type'] . '_field_options_before_update', $values ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// serialize array values |
169
|
|
|
foreach ( array( 'default_value', 'field_options', 'options' ) as $opt ) { |
170
|
|
|
if ( isset( $values[ $opt ] ) && is_array( $values[ $opt ] ) ) { |
171
|
|
|
$values[ $opt ] = serialize( $values[ $opt ] ); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$query_results = $wpdb->update( $wpdb->prefix . 'frm_fields', $values, array( 'id' => $id ) ); |
176
|
|
|
|
177
|
|
|
$form_id = 0; |
178
|
|
|
if ( isset( $values['form_id'] ) ) { |
179
|
|
|
$form_id = absint( $values['form_id'] ); |
180
|
|
|
} else { |
181
|
|
|
$field = self::getOne($id); |
182
|
|
|
if ( $field ) { |
183
|
|
|
$form_id = $field->form_id; |
184
|
|
|
} |
185
|
|
|
unset($field); |
186
|
|
|
} |
187
|
|
|
unset($values); |
188
|
|
|
|
189
|
|
|
if ( $query_results ) { |
190
|
|
|
wp_cache_delete( $id, 'frm_field' ); |
191
|
|
|
if ( $form_id ) { |
192
|
|
|
self::delete_form_transient($form_id); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $query_results; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Keep backslashes in the phone format option |
201
|
|
|
* |
202
|
|
|
* @since 2.0.8 |
203
|
|
|
* @param $values array - pass by reference |
204
|
|
|
*/ |
205
|
|
|
private static function preserve_phone_format_backslashes( &$values ) { |
206
|
|
|
if ( isset( $values['field_options']['format'] ) ) { |
207
|
|
|
$values['field_options']['format'] = FrmAppHelper::preserve_backslashes( $values['field_options']['format'] ); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public static function destroy( $id ) { |
212
|
|
|
global $wpdb; |
213
|
|
|
|
214
|
|
|
do_action( 'frm_before_destroy_field', $id ); |
215
|
|
|
|
216
|
|
|
wp_cache_delete( $id, 'frm_field' ); |
217
|
|
|
$field = self::getOne( $id ); |
218
|
|
|
if ( ! $field ) { |
219
|
|
|
return false; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
self::delete_form_transient( $field->form_id ); |
223
|
|
|
|
224
|
|
|
$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas WHERE field_id=%d', $id ) ); |
225
|
|
|
return $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_fields WHERE id=%d', $id ) ); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public static function delete_form_transient( $form_id ) { |
229
|
|
|
$form_id = absint( $form_id ); |
230
|
|
|
delete_transient( 'frm_form_fields_' . $form_id . 'excludeinclude' ); |
231
|
|
|
delete_transient( 'frm_form_fields_' . $form_id . 'includeinclude' ); |
232
|
|
|
delete_transient( 'frm_form_fields_' . $form_id . 'includeexclude' ); |
233
|
|
|
delete_transient( 'frm_form_fields_' . $form_id . 'excludeexclude' ); |
234
|
|
|
|
235
|
|
|
global $wpdb; |
236
|
|
|
$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s', '_transient_timeout_frm_form_fields_' . $form_id . 'ex%', '_transient_frm_form_fields_' . $form_id . 'ex%', '_transient_timeout_frm_form_fields_' . $form_id . 'in%', '_transient_frm_form_fields_' . $form_id . 'in%' ) ); |
237
|
|
|
|
238
|
|
|
$cache_key = serialize( array( 'fi.form_id' => $form_id ) ) . 'field_orderlb'; |
239
|
|
|
wp_cache_delete($cache_key, 'frm_field'); |
240
|
|
|
|
241
|
|
|
// this cache key is autogenerated in FrmDb::get_var |
242
|
|
|
wp_cache_delete( '(__fi.form_id=%d_OR_fr.parent_form_id=%d_)__' . $form_id . '_' . $form_id . '_ORDER_BY_field_orderfi.*__fr.name_as_form_name_results', 'frm_field' ); |
243
|
|
|
|
244
|
|
|
$form = FrmForm::getOne($form_id); |
245
|
|
|
if ( $form && $form->parent_form_id ) { |
246
|
|
|
self::delete_form_transient( $form->parent_form_id ); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* If $field is numeric, get the field object |
252
|
|
|
*/ |
253
|
|
|
public static function maybe_get_field( &$field ) { |
254
|
|
|
if ( ! is_object( $field ) ) { |
255
|
|
|
$field = self::getOne( $field ); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public static function getOne( $id ) { |
|
|
|
|
260
|
|
|
if ( empty( $id ) ) { |
261
|
|
|
return; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
global $wpdb; |
265
|
|
|
|
266
|
|
|
$where = is_numeric($id) ? 'id=%d' : 'field_key=%s'; |
267
|
|
|
$query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'frm_fields WHERE ' . $where, $id ); |
268
|
|
|
|
269
|
|
|
$results = FrmAppHelper::check_cache( $id, 'frm_field', $query, 'get_row', 0 ); |
270
|
|
|
|
271
|
|
|
if ( empty($results) ) { |
272
|
|
|
return $results; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
View Code Duplication |
if ( is_numeric($id) ) { |
|
|
|
|
276
|
|
|
wp_cache_set( $results->field_key, $results, 'frm_field' ); |
277
|
|
|
} else if ( $results ) { |
278
|
|
|
wp_cache_set( $results->id, $results, 'frm_field' ); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
self::prepare_options( $results ); |
282
|
|
|
|
283
|
|
|
return stripslashes_deep($results); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get the field type by key or id |
288
|
|
|
* @param int|string The field id or key |
289
|
|
|
* @param mixed $col The name of the column in the fields database table |
290
|
|
|
*/ |
291
|
|
|
public static function &get_type( $id, $col = 'type' ) { |
292
|
|
|
$field = FrmAppHelper::check_cache( $id, 'frm_field' ); |
293
|
|
|
if ( $field ) { |
294
|
|
|
$type = $field->{$col}; |
295
|
|
|
} else { |
296
|
|
|
$type = FrmDb::get_var( 'frm_fields', array( 'or' => 1, 'id' => $id, 'field_key' => $id ), $col ); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $type; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public static function get_all_types_in_form( $form_id, $type, $limit = '', $inc_sub = 'exclude' ) { |
303
|
|
|
if ( ! $form_id ) { |
304
|
|
|
return array(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$results = self::get_fields_from_transients( $form_id, array( 'inc_embed' => $inc_sub, 'inc_repeat' => $inc_sub ) ); |
308
|
|
|
if ( ! empty( $results ) ) { |
309
|
|
|
$fields = array(); |
310
|
|
|
$count = 0; |
311
|
|
|
foreach ( $results as $result ) { |
312
|
|
|
if ( $type != $result->type ) { |
313
|
|
|
continue; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
$fields[ $result->id ] = $result; |
317
|
|
|
$count++; |
318
|
|
|
if ( $limit == 1 ) { |
319
|
|
|
$fields = $result; |
320
|
|
|
break; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
if ( ! empty($limit) && $count >= $limit ) { |
324
|
|
|
break; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
unset($result); |
328
|
|
|
} |
329
|
|
|
return stripslashes_deep($fields); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
self::$use_cache = false; |
333
|
|
|
|
334
|
|
|
$where = array( 'fi.form_id' => (int) $form_id, 'fi.type' => $type ); |
335
|
|
|
self::maybe_include_repeating_fields( $inc_sub, $where ); |
336
|
|
|
$results = self::getAll( $where, 'field_order', $limit ); |
337
|
|
|
self::$use_cache = true; |
338
|
|
|
self::include_sub_fields($results, $inc_sub, $type); |
339
|
|
|
|
340
|
|
|
return $results; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public static function get_all_for_form( $form_id, $limit = '', $inc_embed = 'exclude', $inc_repeat = 'include' ) { |
344
|
|
|
if ( ! (int) $form_id ) { |
345
|
|
|
return array(); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$results = self::get_fields_from_transients( $form_id, array( 'inc_embed' => $inc_embed, 'inc_repeat' => $inc_repeat ) ); |
349
|
|
|
if ( ! empty( $results ) ) { |
350
|
|
|
if ( empty($limit) ) { |
351
|
|
|
return $results; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$fields = array(); |
355
|
|
|
$count = 0; |
356
|
|
|
foreach ( $results as $result ) { |
357
|
|
|
$fields[ $result->id ] = $result; |
358
|
|
|
if ( ! empty($limit) && $count >= $limit ) { |
359
|
|
|
break; |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return $fields; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
self::$use_cache = false; |
367
|
|
|
|
368
|
|
|
$where = array( 'fi.form_id' => absint( $form_id ) ); |
369
|
|
|
self::maybe_include_repeating_fields( $inc_repeat, $where ); |
370
|
|
|
$results = self::getAll( $where, 'field_order', $limit ); |
371
|
|
|
|
372
|
|
|
self::$use_cache = true; |
373
|
|
|
|
374
|
|
|
self::include_sub_fields( $results, $inc_embed, 'all' ); |
375
|
|
|
|
376
|
|
|
if ( empty($limit) ) { |
377
|
|
|
self::set_field_transient( $results, $form_id, 0, array( 'inc_embed' => $inc_embed, 'inc_repeat' => $inc_repeat ) ); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
return $results; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* If repeating fields should be included, adjust $where accordingly |
385
|
|
|
* |
386
|
|
|
* @param string $inc_repeat |
387
|
|
|
* @param array $where - pass by reference |
388
|
|
|
*/ |
389
|
|
|
private static function maybe_include_repeating_fields( $inc_repeat, &$where ) { |
390
|
|
|
if ( $inc_repeat == 'include' ) { |
391
|
|
|
$form_id = $where['fi.form_id']; |
392
|
|
|
$where[] = array( 'or' => 1, 'fi.form_id' => $form_id, 'fr.parent_form_id' => $form_id ); |
393
|
|
|
unset( $where['fi.form_id'] ); |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
public static function include_sub_fields( &$results, $inc_embed, $type = 'all' ) { |
398
|
|
|
if ( 'include' != $inc_embed ) { |
399
|
|
|
return; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
$form_fields = $results; |
403
|
|
|
$index_offset = 1; |
404
|
|
|
foreach ( $form_fields as $k => $field ) { |
405
|
|
|
if ( 'form' != $field->type || ! isset($field->field_options['form_select']) ) { |
406
|
|
|
continue; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
if ( $type == 'all' ) { |
410
|
|
|
$sub_fields = self::get_all_for_form( $field->field_options['form_select'] ); |
411
|
|
|
} else { |
412
|
|
|
$sub_fields = self::get_all_types_in_form($field->form_id, $type); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
if ( ! empty($sub_fields) ) { |
416
|
|
|
$index = $k + $index_offset; |
417
|
|
|
$index_offset += count( $sub_fields ); |
418
|
|
|
array_splice($results, $index, 0, $sub_fields); |
419
|
|
|
} |
420
|
|
|
unset($field, $sub_fields); |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
public static function getAll( $where = array(), $order_by = '', $limit = '', $blog_id = false ) { |
|
|
|
|
425
|
|
|
$cache_key = maybe_serialize( $where ) . $order_by . 'l' . $limit . 'b' . $blog_id; |
426
|
|
|
if ( self::$use_cache ) { |
427
|
|
|
// make sure old cache doesn't get saved as a transient |
428
|
|
|
$results = wp_cache_get($cache_key, 'frm_field'); |
429
|
|
|
if ( false !== $results ) { |
430
|
|
|
return stripslashes_deep($results); |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
global $wpdb; |
435
|
|
|
|
436
|
|
|
if ( $blog_id && is_multisite() ) { |
437
|
|
|
global $wpmuBaseTablePrefix; |
438
|
|
|
if ( $wpmuBaseTablePrefix ) { |
439
|
|
|
$prefix = $wpmuBaseTablePrefix . $blog_id . '_'; |
440
|
|
|
} else { |
441
|
|
|
$prefix = $wpdb->get_blog_prefix( $blog_id ); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
$table_name = $prefix . 'frm_fields'; |
445
|
|
|
$form_table_name = $prefix . 'frm_forms'; |
446
|
|
|
} else { |
447
|
|
|
$table_name = $wpdb->prefix . 'frm_fields'; |
448
|
|
|
$form_table_name = $wpdb->prefix . 'frm_forms'; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
View Code Duplication |
if ( ! empty( $order_by ) && strpos( $order_by, 'ORDER BY' ) === false ) { |
|
|
|
|
452
|
|
|
$order_by = ' ORDER BY ' . $order_by; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
$limit = FrmAppHelper::esc_limit($limit); |
456
|
|
|
|
457
|
|
|
$query = "SELECT fi.*, fr.name as form_name FROM {$table_name} fi LEFT OUTER JOIN {$form_table_name} fr ON fi.form_id=fr.id"; |
458
|
|
|
$query_type = ( $limit == ' LIMIT 1' || $limit == 1 ) ? 'row' : 'results'; |
459
|
|
|
|
460
|
|
|
if ( is_array($where) ) { |
461
|
|
|
$results = FrmDb::get_var( $table_name . ' fi LEFT OUTER JOIN ' . $form_table_name . ' fr ON fi.form_id=fr.id', $where, 'fi.*, fr.name as form_name', array( 'order_by' => $order_by, 'limit' => $limit ), '', $query_type ); |
462
|
|
|
} else { |
463
|
|
|
// if the query is not an array, then it has already been prepared |
464
|
|
|
$query .= FrmAppHelper::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit; |
465
|
|
|
|
466
|
|
|
$function_name = ( $query_type == 'row' ) ? 'get_row' : 'get_results'; |
467
|
|
|
$results = $wpdb->$function_name( $query ); |
468
|
|
|
} |
469
|
|
|
unset( $where ); |
470
|
|
|
|
471
|
|
|
self::format_field_results( $results ); |
472
|
|
|
|
473
|
|
|
wp_cache_set( $cache_key, $results, 'frm_field', 300 ); |
474
|
|
|
|
475
|
|
|
return stripslashes_deep( $results ); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* @since 2.0.8 |
480
|
|
|
*/ |
481
|
|
|
private static function format_field_results( &$results ) { |
482
|
|
|
if ( is_array( $results ) ) { |
483
|
|
|
foreach ( $results as $r_key => $result ) { |
484
|
|
|
wp_cache_set( $result->id, $result, 'frm_field' ); |
485
|
|
|
wp_cache_set( $result->field_key, $result, 'frm_field' ); |
486
|
|
|
|
487
|
|
|
$results[ $r_key ]->field_options = maybe_unserialize( $result->field_options ); |
488
|
|
|
$results[ $r_key ]->options = maybe_unserialize( $result->options ); |
489
|
|
|
$results[ $r_key ]->default_value = maybe_unserialize( $result->default_value ); |
490
|
|
|
|
491
|
|
|
unset( $r_key, $result ); |
492
|
|
|
} |
493
|
|
View Code Duplication |
} else if ( $results ) { |
|
|
|
|
494
|
|
|
wp_cache_set( $results->id, $results, 'frm_field' ); |
495
|
|
|
wp_cache_set( $results->field_key, $results, 'frm_field' ); |
496
|
|
|
|
497
|
|
|
self::prepare_options( $results ); |
498
|
|
|
} |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Unserialize all the serialized field data |
503
|
|
|
* @since 2.0 |
504
|
|
|
*/ |
505
|
|
|
private static function prepare_options( &$results ) { |
506
|
|
|
$results->field_options = maybe_unserialize( $results->field_options ); |
507
|
|
|
|
508
|
|
|
$results->options = maybe_unserialize($results->options); |
509
|
|
|
$results->default_value = maybe_unserialize($results->default_value); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* If a form has too many fields, thay won't all save into a single transient. |
514
|
|
|
* We'll break them into groups of 200 |
515
|
|
|
* @since 2.0.1 |
516
|
|
|
*/ |
517
|
|
|
private static function get_fields_from_transients( $form_id, $args ) { |
518
|
|
|
$fields = array(); |
519
|
|
|
self::get_next_transient( $fields, 'frm_form_fields_' . $form_id . $args['inc_embed'] . $args['inc_repeat'] ); |
520
|
|
|
return $fields; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* Called by get_fields_from_transients |
525
|
|
|
* @since 2.0.1 |
526
|
|
|
*/ |
527
|
|
|
private static function get_next_transient( &$fields, $base_name, $next = 0 ) { |
528
|
|
|
$name = $next ? $base_name . $next : $base_name; |
529
|
|
|
$next_fields = get_transient( $name ); |
530
|
|
|
|
531
|
|
|
if ( $next_fields ) { |
532
|
|
|
$fields = array_merge( $fields, $next_fields ); |
533
|
|
|
|
534
|
|
|
if ( count( $next_fields ) >= self::$transient_size ) { |
535
|
|
|
// if this transient is full, check for another |
536
|
|
|
$next++; |
537
|
|
|
self::get_next_transient( $fields, $base_name, $next ); |
538
|
|
|
} |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* Save the transients in chunks for large forms |
544
|
|
|
* @since 2.0.1 |
545
|
|
|
*/ |
546
|
|
|
private static function set_field_transient( &$fields, $form_id, $next = 0, $args = array() ) { |
547
|
|
|
$base_name = 'frm_form_fields_' . $form_id . $args['inc_embed'] . $args['inc_repeat']; |
548
|
|
|
$field_chunks = array_chunk( $fields, self::$transient_size ); |
549
|
|
|
|
550
|
|
|
foreach ( $field_chunks as $field ) { |
551
|
|
|
$name = $next ? $base_name . $next : $base_name; |
552
|
|
|
$set = set_transient( $name, $field, 60 * 60 * 6 ); |
553
|
|
|
if ( ! $set ) { |
554
|
|
|
// the transient didn't save |
555
|
|
|
if ( $name != $base_name ) { |
556
|
|
|
// if the first saved an others fail, this will show an incmoplete form |
557
|
|
|
self::delete_form_transient( $form_id ); |
558
|
|
|
} |
559
|
|
|
return; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
$next++; |
563
|
|
|
} |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
public static function getIds( $where = '', $order_by = '', $limit = '' ) { |
|
|
|
|
567
|
|
|
_deprecated_function( __FUNCTION__, '2.0' ); |
568
|
|
|
global $wpdb; |
569
|
|
View Code Duplication |
if ( ! empty($order_by) && ! strpos($order_by, 'ORDER BY') !== false ) { |
|
|
|
|
570
|
|
|
$order_by = ' ORDER BY ' . $order_by; |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
$query = 'SELECT fi.id FROM ' . $wpdb->prefix . 'frm_fields fi ' . |
574
|
|
|
'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON fi.form_id=fr.id' . |
575
|
|
|
FrmAppHelper::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit; |
576
|
|
|
|
577
|
|
|
$method = ( $limit == ' LIMIT 1' || $limit == 1 ) ? 'get_var' : 'get_col'; |
578
|
|
|
$cache_key = 'getIds_' . maybe_serialize( $where ) . $order_by . $limit; |
579
|
|
|
$results = FrmAppHelper::check_cache($cache_key, 'frm_field', $query, $method); |
580
|
|
|
|
581
|
|
|
return $results; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
public static function is_no_save_field( $type ) { |
585
|
|
|
return in_array( $type, self::no_save_fields() ); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
public static function no_save_fields() { |
589
|
|
|
return array( 'divider', 'end_divider', 'captcha', 'break', 'html', 'form' ); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* Check if this field can hold an array of values |
594
|
|
|
* |
595
|
|
|
* @since 2.0.9 |
596
|
|
|
* |
597
|
|
|
* @param array|object $field |
598
|
|
|
* @return boolean |
599
|
|
|
*/ |
600
|
|
|
public static function is_field_with_multiple_values( $field ) { |
601
|
|
|
if ( ! $field ) { |
602
|
|
|
return false; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
if ( is_array( $field ) ) { |
606
|
|
|
|
607
|
|
|
$is_multi_value_field = ( |
608
|
|
|
$field['type'] == 'checkbox' || |
609
|
|
|
$field['type'] == 'address' || |
610
|
|
|
( $field['type'] == 'data' && isset($field['data_type']) && $field['data_type'] == 'checkbox' ) || |
611
|
|
|
( $field['type'] == 'lookup' && isset($field['data_type']) && $field['data_type'] == 'checkbox' ) || |
612
|
|
|
self::is_multiple_select( $field ) |
613
|
|
|
); |
614
|
|
|
|
615
|
|
|
} else { |
616
|
|
|
$is_multi_value_field = ( |
617
|
|
|
$field->type == 'checkbox' || |
618
|
|
|
$field->type == 'address' || |
619
|
|
|
( $field->type == 'data' && isset( $field->field_options['data_type'] ) && $field->field_options['data_type'] == 'checkbox' ) || |
620
|
|
|
( $field->type == 'lookup' && isset( $field->field_options['data_type'] ) && $field->field_options['data_type'] == 'checkbox' ) || |
621
|
|
|
self::is_multiple_select( $field ) |
622
|
|
|
); |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
return $is_multi_value_field; |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Check if this is a multiselect dropdown field |
630
|
|
|
* |
631
|
|
|
* @since 2.0.9 |
632
|
|
|
* @return boolean |
633
|
|
|
*/ |
634
|
|
|
public static function is_multiple_select( $field ) { |
635
|
|
|
if ( is_array( $field ) ) { |
636
|
|
|
return self::is_option_true( $field, 'multiple' ) && ( ( $field['type'] == 'select' || ( $field['type'] == 'data' && isset( $field['data_type'] ) && $field['data_type'] == 'select') ) ); |
637
|
|
|
} else { |
638
|
|
|
return self::is_option_true( $field, 'multiple' ) && ( ( $field->type == 'select' || ( $field->type == 'data' && isset($field->field_options['data_type'] ) && $field->field_options['data_type'] == 'select') ) ); |
639
|
|
|
} |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Check if a field is read only. Read only can be set in the field options, |
644
|
|
|
* but disabled with the shortcode options |
645
|
|
|
* |
646
|
|
|
* @since 2.0.9 |
647
|
|
|
*/ |
648
|
|
|
public static function is_read_only( $field ) { |
649
|
|
|
global $frm_vars; |
650
|
|
|
return ( self::is_option_true( $field, 'read_only' ) && ( ! isset( $frm_vars['readonly'] ) || $frm_vars['readonly'] != 'disabled' ) ); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* @since 2.0.9 |
655
|
|
|
*/ |
656
|
|
|
public static function is_required( $field ) { |
657
|
|
|
$required = ( $field['required'] != '0' ); |
658
|
|
|
$required = apply_filters( 'frm_is_field_required', $required, $field ); |
659
|
|
|
return $required; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* @since 2.0.9 |
664
|
|
|
*/ |
665
|
|
|
public static function is_option_true( $field, $option ) { |
666
|
|
|
if ( is_array( $field ) ) { |
667
|
|
|
return self::is_option_true_in_array( $field, $option ); |
668
|
|
|
} else { |
669
|
|
|
return self::is_option_true_in_object( $field, $option ); |
670
|
|
|
} |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* @since 2.0.9 |
675
|
|
|
*/ |
676
|
|
|
public static function is_option_empty( $field, $option ) { |
677
|
|
|
if ( is_array( $field ) ) { |
678
|
|
|
return self::is_option_empty_in_array( $field, $option ); |
679
|
|
|
} else { |
680
|
|
|
return self::is_option_empty_in_object( $field, $option ); |
681
|
|
|
} |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
public static function is_option_true_in_array( $field, $option ) { |
685
|
|
|
return isset( $field[ $option ] ) && $field[ $option ]; |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
public static function is_option_true_in_object( $field, $option ) { |
689
|
|
|
return isset( $field->field_options[ $option ] ) && $field->field_options[ $option ]; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
public static function is_option_empty_in_array( $field, $option ) { |
693
|
|
|
return ! isset( $field[ $option ] ) || empty( $field[ $option ] ); |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
public static function is_option_empty_in_object( $field, $option ) { |
697
|
|
|
return ! isset( $field->field_options[ $option ] ) || empty( $field->field_options[ $option ] ); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
public static function is_option_value_in_object( $field, $option ) { |
701
|
|
|
return isset( $field->field_options[ $option ] ) && $field->field_options[ $option ] != ''; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
/** |
705
|
|
|
* @since 2.0.18 |
706
|
|
|
*/ |
707
|
|
|
public static function get_option( $field, $option ) { |
708
|
|
|
if ( is_array( $field ) ) { |
709
|
|
|
$option = self::get_option_in_array( $field, $option ); |
710
|
|
|
} else { |
711
|
|
|
$option = self::get_option_in_object( $field, $option ); |
712
|
|
|
} |
713
|
|
|
return $option; |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
public static function get_option_in_array( $field, $option ) { |
717
|
|
|
return $field[ $option ]; |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
public static function get_option_in_object( $field, $option ) { |
721
|
|
|
return isset( $field->field_options[ $option ] ) ? $field->field_options[ $option ] : ''; |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
/** |
725
|
|
|
* @since 2.0.09 |
726
|
|
|
*/ |
727
|
|
|
public static function is_repeating_field( $field ) { |
728
|
|
|
if ( is_array( $field ) ) { |
729
|
|
|
$is_repeating_field = ( 'divider' == $field['type'] ); |
730
|
|
|
} else { |
731
|
|
|
$is_repeating_field = ( 'divider' == $field->type ); |
732
|
|
|
} |
733
|
|
|
return ( $is_repeating_field && self::is_option_true( $field, 'repeat' ) ); |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
/** |
737
|
|
|
* @param string $key |
738
|
|
|
* @return int field id |
739
|
|
|
*/ |
740
|
|
|
public static function get_id_by_key( $key ) { |
741
|
|
|
$id = FrmDb::get_var( 'frm_fields', array( 'field_key' => sanitize_title( $key ) ) ); |
742
|
|
|
return $id; |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
/** |
746
|
|
|
* @param string $id |
747
|
|
|
* @return string |
748
|
|
|
*/ |
749
|
|
|
public static function get_key_by_id( $id ) { |
750
|
|
|
return FrmDb::get_var( 'frm_fields', array( 'id' => $id ), 'field_key' ); |
751
|
|
|
} |
752
|
|
|
} |
753
|
|
|
|