Completed
Push — master ( b679d7...d53c6c )
by Jamie
03:32
created
classes/models/FrmField.php 1 patch
Spacing   +76 added lines, -76 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-if ( ! defined('ABSPATH') ) {
2
+if ( ! defined( 'ABSPATH' ) ) {
3 3
 	die( 'You are not allowed to call this page directly.' );
4 4
 }
5 5
 
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
 	static $transient_size = 200;
9 9
 
10 10
 	public static function field_selection() {
11
-		$fields = apply_filters('frm_available_fields', array(
11
+		$fields = apply_filters( 'frm_available_fields', array(
12 12
 			'text'      => __( 'Single Line Text', 'formidable' ),
13 13
 			'textarea'  => __( 'Paragraph Text', 'formidable' ),
14 14
 			'checkbox'  => __( 'Checkboxes', 'formidable' ),
@@ -17,13 +17,13 @@  discard block
 block discarded – undo
17 17
 			'email'     => __( 'Email Address', 'formidable' ),
18 18
 			'url'       => __( 'Website/URL', 'formidable' ),
19 19
 			'captcha'   => __( 'reCAPTCHA', 'formidable' ),
20
-		));
20
+		) );
21 21
 
22 22
 		return $fields;
23 23
 	}
24 24
 
25 25
 	public static function pro_field_selection() {
26
-		return apply_filters('frm_pro_available_fields', array(
26
+		return apply_filters( 'frm_pro_available_fields', array(
27 27
 			'end_divider' => array(
28 28
 				'name'  => __( 'End Section', 'formidable' ),
29 29
 				'switch_from' => 'divider',
@@ -48,36 +48,36 @@  discard block
 block discarded – undo
48 48
 			'tag'       => __( 'Tags', 'formidable' ),
49 49
 			'credit_card' => __( 'Credit Card', 'formidable' ),
50 50
 			'address'   => __( 'Address', 'formidable' ),
51
-		));
51
+		) );
52 52
 	}
53 53
 
54 54
     public static function create( $values, $return = true ) {
55 55
         global $wpdb, $frm_duplicate_ids;
56 56
 
57 57
         $new_values = array();
58
-        $key = isset($values['field_key']) ? $values['field_key'] : $values['name'];
58
+        $key = isset( $values['field_key'] ) ? $values['field_key'] : $values['name'];
59 59
 		$new_values['field_key'] = FrmAppHelper::get_unique_key( $key, $wpdb->prefix . 'frm_fields', 'field_key' );
60 60
 
61 61
 		foreach ( array( 'name', 'description', 'type', 'default_value' ) as $col ) {
62
-			$new_values[ $col ] = $values[ $col ];
62
+			$new_values[$col] = $values[$col];
63 63
         }
64 64
 
65 65
         $new_values['options'] = $values['options'];
66 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;
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 70
         $new_values['field_options'] = $values['field_options'];
71
-        $new_values['created_at'] = current_time('mysql', 1);
71
+        $new_values['created_at'] = current_time( 'mysql', 1 );
72 72
 
73 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);
74
+			$frm_duplicate_ids[$values['field_key']] = $new_values['field_key'];
75
+            $new_values = apply_filters( 'frm_duplicated_field', $new_values );
76 76
         }
77 77
 
78 78
 		foreach ( $new_values as $k => $v ) {
79 79
             if ( is_array( $v ) ) {
80
-				$new_values[ $k ] = serialize( $v );
80
+				$new_values[$k] = serialize( $v );
81 81
 			}
82 82
             unset( $k, $v );
83 83
         }
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
 
99 99
 		if ( $query_results ) {
100 100
 			if ( isset( $values['id'] ) ) {
101
-				$frm_duplicate_ids[ $values['id'] ] = $new_id;
101
+				$frm_duplicate_ids[$values['id']] = $new_id;
102 102
 			}
103 103
 			return $new_id;
104 104
 		} else {
@@ -113,9 +113,9 @@  discard block
 block discarded – undo
113 113
 		$fields = self::getAll( $where, 'field_order', '', $blog_id );
114 114
 
115 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);
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 119
             }
120 120
 
121 121
             $values = array();
@@ -138,11 +138,11 @@  discard block
 block discarded – undo
138 138
 				$values['form_id'] = $new_repeat_form_id;
139 139
 			}
140 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);
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 146
         }
147 147
     }
148 148
 
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
 			$values['field_key'] = FrmAppHelper::get_unique_key( $values['field_key'], $wpdb->prefix . 'frm_fields', 'field_key', $id );
156 156
 		}
157 157
 
158
-        if ( isset($values['required']) ) {
158
+        if ( isset( $values['required'] ) ) {
159 159
             $values['required'] = (int) $values['required'];
160 160
         }
161 161
 
@@ -167,8 +167,8 @@  discard block
 block discarded – undo
167 167
 
168 168
 		// serialize array values
169 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 ] );
170
+			if ( isset( $values[$opt] ) && is_array( $values[$opt] ) ) {
171
+				$values[$opt] = serialize( $values[$opt] );
172 172
 			}
173 173
 		}
174 174
 
@@ -178,18 +178,18 @@  discard block
 block discarded – undo
178 178
 		if ( isset( $values['form_id'] ) ) {
179 179
             $form_id = absint( $values['form_id'] );
180 180
 		} else {
181
-            $field = self::getOne($id);
181
+            $field = self::getOne( $id );
182 182
             if ( $field ) {
183 183
                 $form_id = $field->form_id;
184 184
             }
185
-            unset($field);
185
+            unset( $field );
186 186
         }
187
-        unset($values);
187
+        unset( $values );
188 188
 
189 189
 		if ( $query_results ) {
190 190
             wp_cache_delete( $id, 'frm_field' );
191 191
             if ( $form_id ) {
192
-                self::delete_form_transient($form_id);
192
+                self::delete_form_transient( $form_id );
193 193
             }
194 194
         }
195 195
 
@@ -236,12 +236,12 @@  discard block
 block discarded – undo
236 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 237
 
238 238
 		$cache_key = serialize( array( 'fi.form_id' => $form_id ) ) . 'field_orderlb';
239
-        wp_cache_delete($cache_key, 'frm_field');
239
+        wp_cache_delete( $cache_key, 'frm_field' );
240 240
 
241 241
 		// this cache key is autogenerated in FrmDb::get_var
242 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 243
 
244
-        $form = FrmForm::getOne($form_id);
244
+        $form = FrmForm::getOne( $form_id );
245 245
         if ( $form && $form->parent_form_id ) {
246 246
             self::delete_form_transient( $form->parent_form_id );
247 247
         }
@@ -263,16 +263,16 @@  discard block
 block discarded – undo
263 263
 
264 264
         global $wpdb;
265 265
 
266
-        $where = is_numeric($id) ? 'id=%d' : 'field_key=%s';
266
+        $where = is_numeric( $id ) ? 'id=%d' : 'field_key=%s';
267 267
 		$query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'frm_fields WHERE ' . $where, $id );
268 268
 
269 269
         $results = FrmAppHelper::check_cache( $id, 'frm_field', $query, 'get_row', 0 );
270 270
 
271
-        if ( empty($results) ) {
271
+        if ( empty( $results ) ) {
272 272
             return $results;
273 273
         }
274 274
 
275
-        if ( is_numeric($id) ) {
275
+        if ( is_numeric( $id ) ) {
276 276
             wp_cache_set( $results->field_key, $results, 'frm_field' );
277 277
         } else if ( $results ) {
278 278
             wp_cache_set( $results->id, $results, 'frm_field' );
@@ -280,7 +280,7 @@  discard block
 block discarded – undo
280 280
 
281 281
 		self::prepare_options( $results );
282 282
 
283
-        return stripslashes_deep($results);
283
+        return stripslashes_deep( $results );
284 284
     }
285 285
 
286 286
     /**
@@ -313,20 +313,20 @@  discard block
 block discarded – undo
313 313
                     continue;
314 314
                 }
315 315
 
316
-				$fields[ $result->id ] = $result;
317
-                $count++;
316
+				$fields[$result->id] = $result;
317
+                $count ++;
318 318
                 if ( $limit == 1 ) {
319 319
                     $fields = $result;
320 320
                     break;
321 321
                 }
322 322
 
323
-                if ( ! empty($limit) && $count >= $limit ) {
323
+                if ( ! empty( $limit ) && $count >= $limit ) {
324 324
                     break;
325 325
                 }
326 326
 
327
-                unset($result);
327
+                unset( $result );
328 328
             }
329
-            return stripslashes_deep($fields);
329
+            return stripslashes_deep( $fields );
330 330
         }
331 331
 
332 332
         self::$use_cache = false;
@@ -335,7 +335,7 @@  discard block
 block discarded – undo
335 335
 		self::maybe_include_repeating_fields( $inc_sub, $where );
336 336
 		$results = self::getAll( $where, 'field_order', $limit );
337 337
         self::$use_cache = true;
338
-        self::include_sub_fields($results, $inc_sub, $type);
338
+        self::include_sub_fields( $results, $inc_sub, $type );
339 339
 
340 340
         return $results;
341 341
     }
@@ -347,15 +347,15 @@  discard block
 block discarded – undo
347 347
 
348 348
 		$results = self::get_fields_from_transients( $form_id, array( 'inc_embed' => $inc_embed, 'inc_repeat' => $inc_repeat ) );
349 349
 		if ( ! empty( $results ) ) {
350
-            if ( empty($limit) ) {
350
+            if ( empty( $limit ) ) {
351 351
 				return $results;
352 352
             }
353 353
 
354 354
             $fields = array();
355 355
             $count = 0;
356 356
             foreach ( $results as $result ) {
357
-				$fields[ $result->id ] = $result;
358
-                if ( ! empty($limit) && $count >= $limit ) {
357
+				$fields[$result->id] = $result;
358
+                if ( ! empty( $limit ) && $count >= $limit ) {
359 359
                     break;
360 360
                 }
361 361
             }
@@ -373,7 +373,7 @@  discard block
 block discarded – undo
373 373
 
374 374
 		self::include_sub_fields( $results, $inc_embed, 'all' );
375 375
 
376
-        if ( empty($limit) ) {
376
+        if ( empty( $limit ) ) {
377 377
 			self::set_field_transient( $results, $form_id, 0, array( 'inc_embed' => $inc_embed, 'inc_repeat' => $inc_repeat ) );
378 378
         }
379 379
 
@@ -402,22 +402,22 @@  discard block
 block discarded – undo
402 402
         $form_fields = $results;
403 403
 		$index_offset = 1;
404 404
         foreach ( $form_fields as $k => $field ) {
405
-            if ( 'form' != $field->type || ! isset($field->field_options['form_select']) ) {
405
+            if ( 'form' != $field->type || ! isset( $field->field_options['form_select'] ) ) {
406 406
                 continue;
407 407
             }
408 408
 
409 409
             if ( $type == 'all' ) {
410 410
                 $sub_fields = self::get_all_for_form( $field->field_options['form_select'] );
411 411
             } else {
412
-                $sub_fields = self::get_all_types_in_form($field->form_id, $type);
412
+                $sub_fields = self::get_all_types_in_form( $field->form_id, $type );
413 413
             }
414 414
 
415
-            if ( ! empty($sub_fields) ) {
415
+            if ( ! empty( $sub_fields ) ) {
416 416
 				$index = $k + $index_offset;
417 417
 				$index_offset += count( $sub_fields );
418
-				array_splice($results, $index, 0, $sub_fields);
418
+				array_splice( $results, $index, 0, $sub_fields );
419 419
             }
420
-            unset($field, $sub_fields);
420
+            unset( $field, $sub_fields );
421 421
         }
422 422
     }
423 423
 
@@ -425,9 +425,9 @@  discard block
 block discarded – undo
425 425
 		$cache_key = maybe_serialize( $where ) . $order_by . 'l' . $limit . 'b' . $blog_id;
426 426
         if ( self::$use_cache ) {
427 427
             // make sure old cache doesn't get saved as a transient
428
-            $results = wp_cache_get($cache_key, 'frm_field');
428
+            $results = wp_cache_get( $cache_key, 'frm_field' );
429 429
             if ( false !== $results ) {
430
-                return stripslashes_deep($results);
430
+                return stripslashes_deep( $results );
431 431
             }
432 432
         }
433 433
 
@@ -452,16 +452,16 @@  discard block
 block discarded – undo
452 452
 			$order_by = ' ORDER BY ' . $order_by;
453 453
 		}
454 454
 
455
-        $limit = FrmAppHelper::esc_limit($limit);
455
+        $limit = FrmAppHelper::esc_limit( $limit );
456 456
 
457 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 458
         $query_type = ( $limit == ' LIMIT 1' || $limit == 1 ) ? 'row' : 'results';
459 459
 
460
-        if ( is_array($where) ) {
460
+        if ( is_array( $where ) ) {
461 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 462
 		} else {
463 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;
464
+            $query .= FrmAppHelper::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
465 465
 
466 466
 			$function_name = ( $query_type == 'row' ) ? 'get_row' : 'get_results';
467 467
 			$results = $wpdb->$function_name( $query );
@@ -484,9 +484,9 @@  discard block
 block discarded – undo
484 484
 				wp_cache_set( $result->id, $result, 'frm_field' );
485 485
 				wp_cache_set( $result->field_key, $result, 'frm_field' );
486 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 );
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 490
 
491 491
 				unset( $r_key, $result );
492 492
 			}
@@ -505,8 +505,8 @@  discard block
 block discarded – undo
505 505
 	private static function prepare_options( &$results ) {
506 506
 		$results->field_options = maybe_unserialize( $results->field_options );
507 507
 
508
-		$results->options = maybe_unserialize($results->options);
509
-		$results->default_value = maybe_unserialize($results->default_value);
508
+		$results->options = maybe_unserialize( $results->options );
509
+		$results->default_value = maybe_unserialize( $results->default_value );
510 510
 	}
511 511
 
512 512
 	/**
@@ -533,7 +533,7 @@  discard block
 block discarded – undo
533 533
 
534 534
 			if ( count( $next_fields ) >= self::$transient_size ) {
535 535
 				// if this transient is full, check for another
536
-				$next++;
536
+				$next ++;
537 537
 				self::get_next_transient( $fields, $base_name, $next );
538 538
 			}
539 539
 		}
@@ -559,14 +559,14 @@  discard block
 block discarded – undo
559 559
 				return;
560 560
 			}
561 561
 
562
-			$next++;
562
+			$next ++;
563 563
 		}
564 564
 	}
565 565
 
566 566
 	public static function getIds( $where = '', $order_by = '', $limit = '' ) {
567 567
 		_deprecated_function( __FUNCTION__, '2.0' );
568 568
         global $wpdb;
569
-        if ( ! empty($order_by) && ! strpos($order_by, 'ORDER BY') !== false ) {
569
+        if ( ! empty( $order_by ) && ! strpos( $order_by, 'ORDER BY' ) !== false ) {
570 570
 			$order_by = ' ORDER BY ' . $order_by;
571 571
         }
572 572
 
@@ -576,7 +576,7 @@  discard block
 block discarded – undo
576 576
 
577 577
         $method = ( $limit == ' LIMIT 1' || $limit == 1 ) ? 'get_var' : 'get_col';
578 578
 		$cache_key = 'getIds_' . maybe_serialize( $where ) . $order_by . $limit;
579
-        $results = FrmAppHelper::check_cache($cache_key, 'frm_field', $query, $method);
579
+        $results = FrmAppHelper::check_cache( $cache_key, 'frm_field', $query, $method );
580 580
 
581 581
         return $results;
582 582
     }
@@ -607,8 +607,8 @@  discard block
 block discarded – undo
607 607
 			$is_multi_value_field = (
608 608
 				$field['type'] == 'checkbox' ||
609 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' ) ||
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 612
 				self::is_multiple_select( $field )
613 613
 			);
614 614
 
@@ -633,9 +633,9 @@  discard block
 block discarded – undo
633 633
 	 */
634 634
 	public static function is_multiple_select( $field ) {
635 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') ) );
636
+			return self::is_option_true( $field, 'multiple' ) && ( ( $field['type'] == 'select' || ( $field['type'] == 'data' && isset( $field['data_type'] ) && $field['data_type'] == 'select' ) ) );
637 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') ) );
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 639
 		}
640 640
 	}
641 641
 
@@ -682,23 +682,23 @@  discard block
 block discarded – undo
682 682
 	}
683 683
 
684 684
 	public static function is_option_true_in_array( $field, $option ) {
685
-		return isset( $field[ $option ] ) && $field[ $option ];
685
+		return isset( $field[$option] ) && $field[$option];
686 686
 	}
687 687
 
688 688
 	public static function is_option_true_in_object( $field, $option ) {
689
-		return isset( $field->field_options[ $option ] ) && $field->field_options[ $option ];
689
+		return isset( $field->field_options[$option] ) && $field->field_options[$option];
690 690
 	}
691 691
 
692 692
 	public static function is_option_empty_in_array( $field, $option ) {
693
-		return ! isset( $field[ $option ] ) || empty( $field[ $option ] );
693
+		return ! isset( $field[$option] ) || empty( $field[$option] );
694 694
 	}
695 695
 
696 696
 	public static function is_option_empty_in_object( $field, $option ) {
697
-		return ! isset( $field->field_options[ $option ] ) || empty( $field->field_options[ $option ] );
697
+		return ! isset( $field->field_options[$option] ) || empty( $field->field_options[$option] );
698 698
 	}
699 699
 
700 700
 	public static function is_option_value_in_object( $field, $option ) {
701
-		return isset( $field->field_options[ $option ] ) && $field->field_options[ $option ] != '';
701
+		return isset( $field->field_options[$option] ) && $field->field_options[$option] != '';
702 702
 	}
703 703
 
704 704
 	/**
@@ -714,11 +714,11 @@  discard block
 block discarded – undo
714 714
 	}
715 715
 
716 716
 	public static function get_option_in_array( $field, $option ) {
717
-		return $field[ $option ];
717
+		return $field[$option];
718 718
 	}
719 719
 
720 720
 	public static function get_option_in_object( $field, $option ) {
721
-		return isset( $field->field_options[ $option ] ) ? $field->field_options[ $option ] : '';
721
+		return isset( $field->field_options[$option] ) ? $field->field_options[$option] : '';
722 722
 	}
723 723
 
724 724
 	/**
Please login to merge, or discard this patch.