Completed
Push — master ( 24c16b...4fad70 )
by Jamie
03:43
created

FrmField::pro_field_selection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 28
rs 8.8571
c 1
b 1
f 0
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
		$values = apply_filters( 'frm_clean_' . $values['type'] . '_field_options_before_update', $values );
165
166
		// serialize array values
167
		foreach ( array( 'default_value', 'field_options', 'options' ) as $opt ) {
168
			if ( isset( $values[ $opt ] ) && is_array( $values[ $opt ] ) ) {
169
				$values[ $opt ] = serialize( $values[ $opt ] );
170
			}
171
		}
172
173
		$query_results = $wpdb->update( $wpdb->prefix . 'frm_fields', $values, array( 'id' => $id ) );
174
175
        $form_id = 0;
176
		if ( isset( $values['form_id'] ) ) {
177
            $form_id = absint( $values['form_id'] );
178
		} else {
179
            $field = self::getOne($id);
180
            if ( $field ) {
181
                $form_id = $field->form_id;
182
            }
183
            unset($field);
184
        }
185
        unset($values);
186
187
		if ( $query_results ) {
188
            wp_cache_delete( $id, 'frm_field' );
189
            if ( $form_id ) {
190
                self::delete_form_transient($form_id);
191
            }
192
        }
193
194
        return $query_results;
195
    }
196
197
	/**
198
	* Keep backslashes in the phone format option
199
	*
200
	* @since 2.0.8
201
	* @param $values array - pass by reference
202
	*/
203
	private static function preserve_phone_format_backslashes( &$values ) {
204
		if ( isset( $values['field_options']['format'] ) ) {
205
			$values['field_options']['format'] = FrmAppHelper::preserve_backslashes( $values['field_options']['format'] );
206
		}
207
	}
208
209
    public static function destroy( $id ) {
210
		global $wpdb;
211
212
		do_action( 'frm_before_destroy_field', $id );
213
214
		wp_cache_delete( $id, 'frm_field' );
215
		$field = self::getOne( $id );
216
		if ( ! $field ) {
217
			return false;
218
		}
219
220
		self::delete_form_transient( $field->form_id );
221
222
		$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas WHERE field_id=%d', $id ) );
223
		return $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_fields WHERE id=%d', $id ) );
224
    }
225
226
	public static function delete_form_transient( $form_id ) {
227
		$form_id = absint( $form_id );
228
		delete_transient( 'frm_form_fields_' . $form_id . 'excludeinclude' );
229
		delete_transient( 'frm_form_fields_' . $form_id . 'includeinclude' );
230
		delete_transient( 'frm_form_fields_' . $form_id . 'includeexclude' );
231
		delete_transient( 'frm_form_fields_' . $form_id . 'excludeexclude' );
232
233
		global $wpdb;
234
		$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%' ) );
235
236
		$cache_key = serialize( array( 'fi.form_id' => $form_id ) ) . 'field_orderlb';
237
        wp_cache_delete($cache_key, 'frm_field');
238
239
		// this cache key is autogenerated in FrmDb::get_var
240
		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' );
241
242
        $form = FrmForm::getOne($form_id);
243
        if ( $form && $form->parent_form_id ) {
244
            self::delete_form_transient( $form->parent_form_id );
245
        }
246
    }
247
248
	/**
249
	 * If $field is numeric, get the field object
250
	 */
251
	public static function maybe_get_field( &$field ) {
252
		if ( ! is_object( $field ) ) {
253
			$field = self::getOne( $field );
254
		}
255
	}
256
257
	public static function getOne( $id ) {
0 ignored issues
show
Coding Style introduced by
The function name getOne is in camel caps, but expected get_one instead as per the coding standard.
Loading history...
258
		if ( empty( $id ) ) {
259
			return;
260
		}
261
262
        global $wpdb;
263
264
        $where = is_numeric($id) ? 'id=%d' : 'field_key=%s';
265
		$query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'frm_fields WHERE ' . $where, $id );
266
267
        $results = FrmAppHelper::check_cache( $id, 'frm_field', $query, 'get_row', 0 );
268
269
        if ( empty($results) ) {
270
            return $results;
271
        }
272
273 View Code Duplication
        if ( is_numeric($id) ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
274
            wp_cache_set( $results->field_key, $results, 'frm_field' );
275
        } else if ( $results ) {
276
            wp_cache_set( $results->id, $results, 'frm_field' );
277
        }
278
279
		self::prepare_options( $results );
280
281
        return stripslashes_deep($results);
282
    }
283
284
    /**
285
     * Get the field type by key or id
286
     * @param int|string The field id or key
287
	 * @param mixed $col The name of the column in the fields database table
288
     */
289
    public static function &get_type( $id, $col = 'type' ) {
290
        $field = FrmAppHelper::check_cache( $id, 'frm_field' );
291
        if ( $field ) {
292
            $type = $field->{$col};
293
        } else {
294
            $type = FrmDb::get_var( 'frm_fields', array( 'or' => 1, 'id' => $id, 'field_key' => $id ), $col );
295
        }
296
297
        return $type;
298
    }
299
300
	public static function get_all_types_in_form( $form_id, $type, $limit = '', $inc_sub = 'exclude' ) {
301
        if ( ! $form_id ) {
302
            return array();
303
        }
304
305
		$results = self::get_fields_from_transients( $form_id, array( 'inc_embed' => $inc_sub, 'inc_repeat' => $inc_sub ) );
306
		if ( ! empty( $results ) ) {
307
            $fields = array();
308
            $count = 0;
309
            foreach ( $results as $result ) {
310
                if ( $type != $result->type ) {
311
                    continue;
312
                }
313
314
				$fields[ $result->id ] = $result;
315
                $count++;
316
                if ( $limit == 1 ) {
317
                    $fields = $result;
318
                    break;
319
                }
320
321
                if ( ! empty($limit) && $count >= $limit ) {
322
                    break;
323
                }
324
325
                unset($result);
326
            }
327
            return stripslashes_deep($fields);
328
        }
329
330
        self::$use_cache = false;
331
332
		$where = array( 'fi.form_id' => (int) $form_id, 'fi.type' => $type );
333
		self::maybe_include_repeating_fields( $inc_sub, $where );
334
		$results = self::getAll( $where, 'field_order', $limit );
335
        self::$use_cache = true;
336
        self::include_sub_fields($results, $inc_sub, $type);
337
338
        return $results;
339
    }
340
341
	public static function get_all_for_form( $form_id, $limit = '', $inc_embed = 'exclude', $inc_repeat = 'include' ) {
342
        if ( ! (int) $form_id ) {
343
            return array();
344
        }
345
346
		$results = self::get_fields_from_transients( $form_id, array( 'inc_embed' => $inc_embed, 'inc_repeat' => $inc_repeat ) );
347
		if ( ! empty( $results ) ) {
348
            if ( empty($limit) ) {
349
				return $results;
350
            }
351
352
            $fields = array();
353
            $count = 0;
354
            foreach ( $results as $result ) {
355
				$fields[ $result->id ] = $result;
356
                if ( ! empty($limit) && $count >= $limit ) {
357
                    break;
358
                }
359
            }
360
361
			return $fields;
362
        }
363
364
        self::$use_cache = false;
365
366
		$where = array( 'fi.form_id' => absint( $form_id ) );
367
		self::maybe_include_repeating_fields( $inc_repeat, $where );
368
		$results = self::getAll( $where, 'field_order', $limit );
369
370
        self::$use_cache = true;
371
372
		self::include_sub_fields( $results, $inc_embed, 'all' );
373
374
        if ( empty($limit) ) {
375
			self::set_field_transient( $results, $form_id, 0, array( 'inc_embed' => $inc_embed, 'inc_repeat' => $inc_repeat ) );
376
        }
377
378
		return $results;
379
    }
380
381
	/**
382
	* If repeating fields should be included, adjust $where accordingly
383
	*
384
	* @param string $inc_repeat
385
	* @param array $where - pass by reference
386
	*/
387
	private static function maybe_include_repeating_fields( $inc_repeat, &$where ) {
388
		if ( $inc_repeat == 'include' ) {
389
			$form_id = $where['fi.form_id'];
390
			$where[] = array( 'or' => 1, 'fi.form_id' => $form_id, 'fr.parent_form_id' => $form_id );
391
			unset( $where['fi.form_id'] );
392
		}
393
	}
394
395
	public static function include_sub_fields( &$results, $inc_embed, $type = 'all' ) {
396
		if ( 'include' != $inc_embed ) {
397
            return;
398
        }
399
400
        $form_fields = $results;
401
		$index_offset = 1;
402
        foreach ( $form_fields as $k => $field ) {
403
            if ( 'form' != $field->type || ! isset($field->field_options['form_select']) ) {
404
                continue;
405
            }
406
407
            if ( $type == 'all' ) {
408
                $sub_fields = self::get_all_for_form( $field->field_options['form_select'] );
409
            } else {
410
                $sub_fields = self::get_all_types_in_form($field->form_id, $type);
411
            }
412
413
            if ( ! empty($sub_fields) ) {
414
				$index = $k + $index_offset;
415
				$index_offset += count( $sub_fields );
416
				array_splice($results, $index, 0, $sub_fields);
417
            }
418
            unset($field, $sub_fields);
419
        }
420
    }
421
422
	public static function getAll( $where = array(), $order_by = '', $limit = '', $blog_id = false ) {
0 ignored issues
show
Coding Style introduced by
The function name getAll is in camel caps, but expected get_all instead as per the coding standard.
Loading history...
423
		$cache_key = maybe_serialize( $where ) . $order_by . 'l' . $limit . 'b' . $blog_id;
424
        if ( self::$use_cache ) {
425
            // make sure old cache doesn't get saved as a transient
426
            $results = wp_cache_get($cache_key, 'frm_field');
427
            if ( false !== $results ) {
428
                return stripslashes_deep($results);
429
            }
430
        }
431
432
        global $wpdb;
433
434
        if ( $blog_id && is_multisite() ) {
435
            global $wpmuBaseTablePrefix;
436
            if ( $wpmuBaseTablePrefix ) {
437
				$prefix = $wpmuBaseTablePrefix . $blog_id . '_';
438
            } else {
439
                $prefix = $wpdb->get_blog_prefix( $blog_id );
440
            }
441
442
			$table_name = $prefix . 'frm_fields';
443
			$form_table_name = $prefix . 'frm_forms';
444
		} else {
445
			$table_name = $wpdb->prefix . 'frm_fields';
446
			$form_table_name = $wpdb->prefix . 'frm_forms';
447
        }
448
449 View Code Duplication
		if ( ! empty( $order_by ) && strpos( $order_by, 'ORDER BY' ) === false ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
450
			$order_by = ' ORDER BY ' . $order_by;
451
		}
452
453
        $limit = FrmAppHelper::esc_limit($limit);
454
455
        $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";
456
        $query_type = ( $limit == ' LIMIT 1' || $limit == 1 ) ? 'row' : 'results';
457
458
        if ( is_array($where) ) {
459
            $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 );
460
		} else {
461
			// if the query is not an array, then it has already been prepared
462
            $query .= FrmAppHelper::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
463
464
			$function_name = ( $query_type == 'row' ) ? 'get_row' : 'get_results';
465
			$results = $wpdb->$function_name( $query );
466
        }
467
        unset( $where );
468
469
		self::format_field_results( $results );
470
471
		wp_cache_set( $cache_key, $results, 'frm_field', 300 );
472
473
		return stripslashes_deep( $results );
474
	}
475
476
	/**
477
	 * @since 2.0.8
478
	 */
479
	private static function format_field_results( &$results ) {
480
		if ( is_array( $results ) ) {
481
			foreach ( $results as $r_key => $result ) {
482
				wp_cache_set( $result->id, $result, 'frm_field' );
483
				wp_cache_set( $result->field_key, $result, 'frm_field' );
484
485
				$results[ $r_key ]->field_options = maybe_unserialize( $result->field_options );
486
				$results[ $r_key ]->options = maybe_unserialize( $result->options );
487
				$results[ $r_key ]->default_value = maybe_unserialize( $result->default_value );
488
489
				unset( $r_key, $result );
490
			}
491 View Code Duplication
		} else if ( $results ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
492
			wp_cache_set( $results->id, $results, 'frm_field' );
493
			wp_cache_set( $results->field_key, $results, 'frm_field' );
494
495
			self::prepare_options( $results );
496
		}
497
	}
498
499
	/**
500
	 * Unserialize all the serialized field data
501
	 * @since 2.0
502
	 */
503
	private static function prepare_options( &$results ) {
504
		$results->field_options = maybe_unserialize( $results->field_options );
505
506
		$results->options = maybe_unserialize($results->options);
507
		$results->default_value = maybe_unserialize($results->default_value);
508
	}
509
510
	/**
511
	 * If a form has too many fields, thay won't all save into a single transient.
512
	 * We'll break them into groups of 200
513
	 * @since 2.0.1
514
	 */
515
	private static function get_fields_from_transients( $form_id, $args ) {
516
		$fields = array();
517
		self::get_next_transient( $fields, 'frm_form_fields_' . $form_id . $args['inc_embed'] . $args['inc_repeat'] );
518
		return $fields;
519
	}
520
521
	/**
522
	 * Called by get_fields_from_transients
523
	 * @since 2.0.1
524
	 */
525
	private static function get_next_transient( &$fields, $base_name, $next = 0 ) {
526
		$name = $next ? $base_name . $next : $base_name;
527
		$next_fields = get_transient( $name );
528
529
		if ( $next_fields ) {
530
			$fields = array_merge( $fields, $next_fields );
531
532
			if ( count( $next_fields ) >= self::$transient_size ) {
533
				// if this transient is full, check for another
534
				$next++;
535
				self::get_next_transient( $fields, $base_name, $next );
536
			}
537
		}
538
	}
539
540
	/**
541
	 * Save the transients in chunks for large forms
542
	 * @since 2.0.1
543
	 */
544
	private static function set_field_transient( &$fields, $form_id, $next = 0, $args = array() ) {
545
		$base_name = 'frm_form_fields_' . $form_id . $args['inc_embed'] . $args['inc_repeat'];
546
		$field_chunks = array_chunk( $fields, self::$transient_size );
547
548
		foreach ( $field_chunks as $field ) {
549
			$name = $next ? $base_name . $next : $base_name;
550
			$set = set_transient( $name, $field, 60 * 60 * 6 );
551
			if ( ! $set ) {
552
				// the transient didn't save
553
				if ( $name != $base_name ) {
554
					// if the first saved an others fail, this will show an incmoplete form
555
					self::delete_form_transient( $form_id );
556
				}
557
				return;
558
			}
559
560
			$next++;
561
		}
562
	}
563
564
	public static function getIds( $where = '', $order_by = '', $limit = '' ) {
0 ignored issues
show
Coding Style introduced by
The function name getIds is in camel caps, but expected get_ids instead as per the coding standard.
Loading history...
565
		_deprecated_function( __FUNCTION__, '2.0' );
566
        global $wpdb;
567 View Code Duplication
        if ( ! empty($order_by) && ! strpos($order_by, 'ORDER BY') !== false ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
568
			$order_by = ' ORDER BY ' . $order_by;
569
        }
570
571
		$query = 'SELECT fi.id  FROM ' . $wpdb->prefix . 'frm_fields fi ' .
572
			'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON fi.form_id=fr.id' .
573
			FrmAppHelper::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
574
575
        $method = ( $limit == ' LIMIT 1' || $limit == 1 ) ? 'get_var' : 'get_col';
576
		$cache_key = 'getIds_' . maybe_serialize( $where ) . $order_by . $limit;
577
        $results = FrmAppHelper::check_cache($cache_key, 'frm_field', $query, $method);
578
579
        return $results;
580
    }
581
582
	public static function is_no_save_field( $type ) {
583
		return in_array( $type, self::no_save_fields() );
584
	}
585
586
	public static function no_save_fields() {
587
		return array( 'divider', 'end_divider', 'captcha', 'break', 'html', 'form' );
588
	}
589
590
	/**
591
	 * Check if this field can hold an array of values
592
	 *
593
	 * @since 2.0.9
594
	 *
595
	 * @param array|object $field
596
	 * @return boolean
597
	 */
598
	public static function is_field_with_multiple_values( $field ) {
599
		if ( ! $field ) {
600
			return false;
601
		}
602
603
		if ( is_array( $field ) ) {
604
605
			$is_multi_value_field = (
606
				$field['type'] == 'checkbox' ||
607
				$field['type'] == 'address' ||
608
				( $field['type'] == 'data' && isset($field['data_type']) && $field['data_type'] == 'checkbox' ) ||
609
				self::is_multiple_select( $field )
610
			);
611
612
		} else {
613
			$is_multi_value_field = (
614
				$field->type == 'checkbox' ||
615
				$field->type == 'address' ||
616
				( $field->type == 'data' && isset( $field->field_options['data_type'] ) && $field->field_options['data_type'] == 'checkbox' ) ||
617
				self::is_multiple_select( $field )
618
			);
619
		}
620
621
		return $is_multi_value_field;
622
	}
623
624
	/**
625
	 * Check if this is a multiselect dropdown field
626
	 *
627
	 * @since 2.0.9
628
	 * @return boolean
629
	 */
630
	public static function is_multiple_select( $field ) {
631
		if ( is_array( $field ) ) {
632
			return self::is_option_true( $field, 'multiple' ) && ( ( $field['type'] == 'select' || ( $field['type'] == 'data' && isset( $field['data_type'] ) && $field['data_type'] == 'select') ) );
633
		} else {
634
			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') ) );
635
		}
636
	}
637
638
	/**
639
	 * Check if a field is read only. Read only can be set in the field options,
640
	 * but disabled with the shortcode options
641
	 *
642
	 * @since 2.0.9
643
	 */
644
	public static function is_read_only( $field ) {
645
		global $frm_vars;
646
		return ( self::is_option_true( $field, 'read_only' ) && ( ! isset( $frm_vars['readonly'] ) || $frm_vars['readonly'] != 'disabled' ) );
647
	}
648
649
	/**
650
	 * @since 2.0.9
651
	 */
652
	public static function is_required( $field ) {
653
		$required = ( $field['required'] != '0' );
654
		$required = apply_filters( 'frm_is_field_required', $required, $field );
655
		return $required;
656
	}
657
658
	/**
659
	 * @since 2.0.9
660
	 */
661
	public static function is_option_true( $field, $option ) {
662
		if ( is_array( $field ) ) {
663
			return self::is_option_true_in_array( $field, $option );
664
		} else {
665
			return self::is_option_true_in_object( $field, $option );
666
		}
667
	}
668
669
	/**
670
	 * @since 2.0.9
671
	 */
672
	public static function is_option_empty( $field, $option ) {
673
		if ( is_array( $field ) ) {
674
			return self::is_option_empty_in_array( $field, $option );
675
		} else {
676
			return self::is_option_empty_in_object( $field, $option );
677
		}
678
	}
679
680
	public static function is_option_true_in_array( $field, $option ) {
681
		return isset( $field[ $option ] ) && $field[ $option ];
682
	}
683
684
	public static function is_option_true_in_object( $field, $option ) {
685
		return isset( $field->field_options[ $option ] ) && $field->field_options[ $option ];
686
	}
687
688
	public static function is_option_empty_in_array( $field, $option ) {
689
		return ! isset( $field[ $option ] ) || empty( $field[ $option ] );
690
	}
691
692
	public static function is_option_empty_in_object( $field, $option ) {
693
		return ! isset( $field->field_options[ $option ] ) || empty( $field->field_options[ $option ] );
694
	}
695
696
	public static function is_option_value_in_object( $field, $option ) {
697
		return isset( $field->field_options[ $option ] ) && $field->field_options[ $option ] != '';
698
	}
699
700
	/**
701
	 * @since 2.0.18
702
	 */
703
	public static function get_option( $field, $option ) {
704
		if ( is_array( $field ) ) {
705
			$option = self::get_option_in_array( $field, $option );
706
		} else {
707
			$option = self::get_option_in_object( $field, $option );
708
		}
709
		return $option;
710
	}
711
712
	public static function get_option_in_array( $field, $option ) {
713
		return $field[ $option ];
714
	}
715
716
	public static function get_option_in_object( $field, $option ) {
717
		return isset( $field->field_options[ $option ] ) ? $field->field_options[ $option ] : '';
718
	}
719
720
	/**
721
	* @since 2.0.09
722
	*/
723
	public static function is_repeating_field( $field ) {
724
		if ( is_array( $field ) ) {
725
			$is_repeating_field = ( 'divider' == $field['type'] );
726
		} else {
727
			$is_repeating_field = ( 'divider' == $field->type );
728
		}
729
		return ( $is_repeating_field && self::is_option_true( $field, 'repeat' ) );
730
	}
731
732
    /**
733
     * @param string $key
734
     * @return int field id
735
     */
736
	public static function get_id_by_key( $key ) {
737
        $id = FrmDb::get_var( 'frm_fields', array( 'field_key' => sanitize_title( $key ) ) );
738
        return $id;
739
    }
740
741
	/**
742
	 * @param string $id
743
	 * @return string
744
	 */
745
	public static function get_key_by_id( $id ) {
746
		return FrmDb::get_var( 'frm_fields', array( 'id' => $id ), 'field_key' );
747
	}
748
}
749