Completed
Push — master ( c021d3...ffb3c6 )
by Stephanie
04:56
created

FrmField::update()   D

Complexity

Conditions 10
Paths 108

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 45
rs 4.7302
cc 10
eloc 26
nc 108
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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