Completed
Push — issues/1038 ( 67d5d4...f77bb0 )
by Ravinder
20:00
created

Give_Fields_API::set_default_values()   D

Complexity

Conditions 14
Paths 80

Size

Total Lines 84
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 42
nc 80
nop 3
dl 0
loc 84
rs 4.9516
c 0
b 0
f 0

How to fix   Long Method    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
3
/**
4
 * Fields API
5
 *
6
 * @package     Give
7
 * @subpackage  Classes/Give_Fields_API
8
 * @copyright   Copyright (c) 2016, WordImpress
9
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
10
 * @since       1.9
11
 */
12
class Give_Fields_API {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
13
	/**
14
	 * Instance.
15
	 *
16
	 * @since  1.9
17
	 * @access private
18
	 * @var Give_Fields_API
19
	 */
20
	static private $instance;
21
22
	/**
23
	 * The defaults for all elements
24
	 *
25
	 * @since  1.9
26
	 * @access static
27
	 */
28
	static $field_defaults = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $field_defaults.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
29
		'type'                 => '',
30
		'name'                 => '',
31
		'data_type'            => '',
32
		'value'                => '',
33
		'required'             => false,
34
		'options'              => array(),
35
36
		// Set default value to field.
37
		'default'              => '',
38
39
		// Field with wrapper.
40
		'wrapper'              => true,
41
42
		// Add label, before and after field.
43
		'label'                => '',
44
		'label_position'       => 'before',
45
46
		// Add description to field as tooltip.
47
		'tooltip'              => '',
48
49
		// Show multiple fields in same row with in sub section.
50
		'sub_section_start'    => false,
51
		'sub_section_end'      => false,
52
53
		// Add custom attributes.
54
		'field_attributes'     => array(),
55
		'wrapper_attributes'   => array(),
56
57
		// Show/Hide field in before/after modal view.
58
		'show_without_modal'   => false,
59
		'show_within_modal'    => true,
60
61
		// Params to edit field html.
62
		'before_field'         => '',
63
		'after_field'          => '',
64
		'before_field_wrapper' => '',
65
		'after_field_wrapper'  => '',
66
		'before_label'         => '',
67
		'after_label'          => '',
68
69
		// Manually render field.
70
		'callback'             => '',
71
72
	);
73
74
	/**
75
	 * The defaults for all sections.
76
	 *
77
	 * @since  1.9
78
	 * @access static
79
	 */
80
	static $section_defaults = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $section_defaults.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
81
		'type'               => 'section',
82
		'label'              => '',
83
		'name'               => '',
84
		'section_attributes' => array(),
85
86
		// Manually render section.
87
		'callback'           => '',
88
	);
89
90
	/**
91
	 * The defaults for all blocks.
92
	 *
93
	 * @since  1.9
94
	 * @access static
95
	 */
96
	static $block_defaults = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $block_defaults.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
97
		'type'             => 'block',
98
		'label'            => '',
99
		'name'             => '',
100
		'block_attributes' => array(),
101
102
		// Manually render section.
103
		'callback'         => '',
104
	);
105
106
107
	private function __construct() {
108
	}
109
110
111
	/**
112
	 * Get instance.
113
	 *
114
	 * @return static
115
	 */
116
	public static function get_instance() {
117
		if ( is_null( static::$instance ) ) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
118
			self::$instance = new static();
119
		}
120
121
		return self::$instance;
122
	}
123
124
	/**
125
	 * Initialize this module
126
	 *
127
	 * @since  1.9
128
	 * @access static
129
	 */
130
	public function init() {
131
		add_filter( 'give_form_api_render_form_tags', array( $this, 'render_tags' ), 10, 2 );
132
	}
133
134
135
	/**
136
	 * Render custom field.
137
	 *
138
	 * @since  1.0
139
	 * @access private
140
	 *
141
	 * @param array $field
142
	 *
143
	 * @return bool
144
	 */
145
	private function render_custom_field( $field ) {
146
		$field_html = '';
147
148
		if ( empty( $field['callback'] ) ) {
149
			$callback = $field['callback'];
150
151
			// Process callback to get field html.
152
			if ( is_string( $callback ) && function_exists( "$callback" ) ) {
153
				$field_html = $callback( $field );
154
			} elseif ( is_array( $callback ) && method_exists( $callback[0], "$callback[1]" ) ) {
155
				$field_html = $callback[0]->$callback[1]( $field );
156
			}
157
		}
158
159
		return $field_html;
160
	}
161
162
163
	/**
164
	 * Render `{{form_fields}}` tag.
165
	 *
166
	 * @since  1.9
167
	 * @access private
168
	 *
169
	 * @param  string $form_html
170
	 * @param  array  $form
171
	 *
172
	 * @return string
173
	 */
174
	public function render_tags( $form_html, $form ) {
175
		// Bailout: If form does not contain any field.
176
		if ( empty( $form['fields'] ) ) {
177
			str_replace( '{{form_fields}}', '', $form_html );
178
179
			return $form_html;
180
		}
181
182
		$fields_html = '';
183
184
		// Set responsive fields.
185
		self::$instance->set_responsive_field( $form );
186
187
		// Render fields.
188
		foreach ( $form['fields'] as $key => $field ) {
189
			// Set default value.
190
			$field['name'] = empty( $field['name'] ) ? $key : $field['name'];
191
			$field         = self::$instance->set_default_values( $field, $form );
192
193
194
			// Render custom form with callback.
195
			if ( $field_html = self::$instance->render_custom_field( $field ) ) {
196
				$fields_html .= $field_html;
197
			}
198
199
			switch ( true ) {
200
				// Block.
201
				case ( array_key_exists( 'type', $field ) && 'block' === $field['type'] ):
202
					$fields_html .= self::$instance->render_block( $field, $form );
203
					break;
204
205
				// Section.
206
				case array_key_exists( 'fields', $field ):
207
					$fields_html .= self::$instance->render_section( $field, $form );
208
					break;
209
210
				// Field
211
				default:
212
					$fields_html .= self::render_tag( $field, $form );
213
			}
214
		}
215
216
		$form_html = str_replace( '{{form_fields}}', $fields_html, $form_html );
217
218
		return $form_html;
219
	}
220
221
222
	/**
223
	 * Render section.
224
	 *
225
	 * @since  1.9
226
	 * @access public
227
	 *
228
	 * @param array $section
229
	 * @param array $form
230
	 *
231
	 * @return string
232
	 */
233
	public static function render_section( $section, $form = null ) {
234
		ob_start();
235
		?>
236
		<fieldset <?php echo self::$instance->get_attributes( $section['section_attributes'] ); ?>>
237
			<?php
238
			// Legend.
239
			if ( ! empty( $section['label'] ) ) {
240
				echo "<legend>{$section['label']}</legend>";
241
			};
242
243
			// Fields.
244
			foreach ( $section['fields'] as $key => $field ) {
245
				echo self::render_tag( $field, $form );
246
			}
247
			?>
248
		</fieldset>
249
		<?php
250
		return ob_get_clean();
251
	}
252
253
254
	/**
255
	 * Render block.
256
	 *
257
	 * @since  1.9
258
	 * @access public
259
	 *
260
	 * @param array $section
261
	 * @param array $form
262
	 *
263
	 * @return string
264
	 */
265
	public static function render_block( $section, $form = null ) {
266
		ob_start();
267
		?>
268
		<div <?php echo self::$instance->get_attributes( $section['block_attributes'] ); ?>>
269
			<?php
270
			// Fields.
271
			foreach ( $section['fields'] as $key => $field ) {
272
				echo array_key_exists( 'fields', $field )
273
					? self::render_section( $field, $form )
274
					: self::render_tag( $field, $form );
275
			}
276
			?>
277
		</div>
278
		<?php
279
		return ob_get_clean();
280
	}
281
282
	/**
283
	 * Render tag
284
	 *
285
	 * @since   1.9
286
	 * @access  public
287
	 *
288
	 * @param $field
289
	 * @param $form
290
	 *
291
	 * @return string
292
	 */
293
	public static function render_tag( $field, $form = null ) {
294
		$field_html     = '';
295
		$functions_name = "render_{$field['type']}_field";
296
297
		if ( method_exists( self::$instance, $functions_name ) ) {
298
			$field_html .= self::$instance->{$functions_name}( $field );
299
		} else {
300
			$field_html .= apply_filters( "give_field_api_render_{$field['type']}_field", '', $field, $form );
301
		}
302
303
		return $field_html;
304
	}
305
306
307
	/**
308
	 * Render text field.
309
	 *
310
	 * @since  1.9
311
	 * @access private
312
	 *
313
	 * @param  array $field
314
	 *
315
	 * @return string
316
	 */
317
	public static function render_text_field( $field ) {
318
		$field_wrapper = self::$instance->render_field_wrapper( $field );
319
		ob_start();
320
		?>
321
		<input
322
				type="<?php echo $field['type']; ?>"
323
				name="<?php echo $field['name']; ?>"
324
				value="<?php echo $field ['value']; ?>"
325
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
326
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
327
		>
328
		<?php
329
330
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
331
	}
332
333
	/**
334
	 * Render submit field.
335
	 *
336
	 * @since  1.9
337
	 * @access private
338
	 *
339
	 * @param  array $field
340
	 *
341
	 * @return string
342
	 */
343
	public static function render_submit_field( $field ) {
344
		return self::$instance->render_text_field( $field );
345
	}
346
347
	/**
348
	 * Render checkbox field.
349
	 *
350
	 * @since  1.9
351
	 * @access private
352
	 *
353
	 * @param  array $field
354
	 *
355
	 * @return string
356
	 */
357
	public static function render_checkbox_field( $field ) {
358
		return self::$instance->render_text_field( $field );
359
	}
360
361
	/**
362
	 * Render email field.
363
	 *
364
	 * @since  1.9
365
	 * @access private
366
	 *
367
	 * @param  array $field
368
	 *
369
	 * @return string
370
	 */
371
	public static function render_email_field( $field ) {
372
		return self::$instance->render_text_field( $field );
373
	}
374
375
	/**
376
	 * Render number field.
377
	 *
378
	 * @since  1.9
379
	 * @access private
380
	 *
381
	 * @param  array $field
382
	 *
383
	 * @return string
384
	 */
385
	public static function render_number_field( $field ) {
386
		return self::$instance->render_text_field( $field );
387
	}
388
389
	/**
390
	 * Render password field.
391
	 *
392
	 * @since  1.9
393
	 * @access private
394
	 *
395
	 * @param  array $field
396
	 *
397
	 * @return string
398
	 */
399
	public static function render_password_field( $field ) {
400
		return self::$instance->render_text_field( $field );
401
	}
402
403
	/**
404
	 * Render button field.
405
	 *
406
	 * @since  1.9
407
	 * @access private
408
	 *
409
	 * @param  array $field
410
	 *
411
	 * @return string
412
	 */
413
	public static function render_button_field( $field ) {
414
		return self::$instance->render_text_field( $field );
415
	}
416
417
	/**
418
	 * Render textarea field.
419
	 *
420
	 * @since  1.9
421
	 * @access private
422
	 *
423
	 * @param  array $field
424
	 *
425
	 * @return string
426
	 */
427
	public static function render_textarea_field( $field ) {
428
		$field_wrapper = self::$instance->render_field_wrapper( $field );
429
		ob_start();
430
		?>
431
		<textarea
432
				name="<?php echo $field['name']; ?>"
433
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
434
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
435
		><?php echo $field ['value']; ?></textarea>
436
437
438
		<?php
439
440
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
441
	}
442
443
	/**
444
	 * Render select field.
445
	 *
446
	 * @since  1.9
447
	 * @access private
448
	 *
449
	 * @param  array $field
450
	 *
451
	 * @return string
452
	 */
453
	public static function render_select_field( $field ) {
454
		$field_wrapper = self::$instance->render_field_wrapper( $field );
455
		ob_start();
456
457
		$options_html = '';
458
		foreach ( $field['options'] as $key => $option ) {
459
			$options_html .= "<option value=\"{$key}\">{$option}</option>";
460
		}
461
		?>
462
463
		<select
464
				name="<?php echo $field['name']; ?>"
465
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
466
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
467
		><?php echo $options_html; ?></select>
468
		<?php
469
470
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
471
	}
472
473
	/**
474
	 * Render multi select field.
475
	 *
476
	 * @since  1.9
477
	 * @access private
478
	 *
479
	 * @param  array $field
480
	 *
481
	 * @return string
482
	 */
483
	public static function render_multi_select_field( $field ) {
484
		$field['field_attributes'] = array_merge( $field['field_attributes'], array( 'multiple' => 'multiple' ) );
485
		$field['name']             = "{$field['name']}[]";
486
487
		return self::$instance->render_select_field( $field );
488
	}
489
490
	/**
491
	 * Render text field.
492
	 *
493
	 * @since  1.9
494
	 * @access private
495
	 *
496
	 * @param  array $field
497
	 *
498
	 * @return string
499
	 */
500
	public static function render_radio_field( $field ) {
501
		$field_wrapper = self::$instance->render_field_wrapper( $field );
502
		ob_start();
503
		foreach ( $field['options'] as $key => $option ) :
504
			// @todo id issue.
505
			?>
506
			<input
507
			type="<?php echo $field['type']; ?>"
508
			name="<?php echo $field['name']; ?>"
509
			value="<?php echo $key; ?>"
510
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
511
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
512
			><?php echo $option; ?>
513
			<?php
514
		endforeach;
515
516
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
517
	}
518
519
520
	/**
521
	 * Render wrapper
522
	 *
523
	 * @since  1.9
524
	 * @access private
525
	 *
526
	 * @param $field
527
	 *
528
	 * @return string
529
	 */
530
	private function render_field_wrapper( $field ) {
531
		ob_start();
532
533
		if( $field['wrapper'] ) :
534
535
			echo $field['before_field_wrapper'];
536
			?>
537
			<p <?php echo self::$instance->get_attributes( $field['wrapper_attributes'] ); ?>>
538
				<?php
539
				// Label: before field.
540
				if ( 'before' === $field['label_position'] ) {
541
					echo self::$instance->render_label( $field );
542
				}
543
544
				echo "{$field['before_field']}{{form_field}}{$field['before_field']}";
545
546
				// Label: before field.
547
				if ( 'after' === $field['label_position'] ) {
548
					echo self::$instance->render_label( $field );
549
				}
550
				?>
551
			</p>
552
			<?php
553
			echo $field['after_field_wrapper'];
554
		else :
555
			echo "{$field['before_field']}{{form_field}}{$field['after_field']}";
556
		endif;
557
558
		return ob_get_clean();
559
	}
560
561
562
	/**
563
	 * Render label
564
	 *
565
	 * @since  1.9
566
	 * @access private
567
	 *
568
	 * @param $field
569
	 *
570
	 * @return string
571
	 */
572
	private function render_label( $field ) {
573
		ob_start();
574
		?>
575
		<?php if ( ! empty( $field['label'] ) ) : ?>
576
			<?php echo $field['before_label']; ?>
577
			<label class="give-label" for="<?php echo $field['field_attributes']['id']; ?>">
578
579
				<?php echo $field['label']; ?>
580
581
				<?php if ( $field['required'] ) : ?>
582
					<span class="give-required-indicator">*</span>
583
				<?php endif; ?>
584
585
				<?php if ( $field['tooltip'] ) : ?>
586
					<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php echo $field['tooltip'] ?>"></span>
587
				<?php endif; ?>
588
			</label>
589
			<?php echo $field['after_label']; ?>
590
		<?php endif; ?>
591
		<?php
592
		return ob_get_clean();
593
	}
594
595
	/**
596
	 * Get field attribute string from field arguments.
597
	 *
598
	 * @since  1.9
599
	 * @access private
600
	 *
601
	 * @param array $attributes
602
	 *
603
	 * @return array|string
604
	 */
605
	private function get_attributes( $attributes ) {
606
		$field_attributes_val = '';
607
608
		if ( ! empty( $attributes ) ) {
609
			foreach ( $attributes as $attribute_name => $attribute_val ) {
610
				$field_attributes_val[] = "{$attribute_name}=\"{$attribute_val}\"";
611
			}
612
		}
613
614
		if ( ! empty( $field_attributes_val ) ) {
615
			$field_attributes_val = implode( ' ', $field_attributes_val );
616
		}
617
618
		return $field_attributes_val;
619
	}
620
621
	/**
622
	 * Set default values for fields
623
	 *
624
	 * @since  1.0
625
	 * @access private
626
	 *
627
	 * @param array $field
628
	 * @param array $form
629
	 * @param bool  $fire_filter
630
	 *
631
	 * @return array
632
	 */
633
	private function set_default_values( $field, $form = null, $fire_filter = true ) {
634
		/**
635
		 * Filter the field before set default values.
636
		 *
637
		 * @since 1.9
638
		 *
639
		 * @param array $field
640
		 * @param array $form
641
		 */
642
		$field = $fire_filter
643
			? apply_filters( 'give_field_api_pre_set_default_values', $field, $form )
644
			: $field;
645
646
		switch ( self::$instance->get_field_type( $field ) ) {
647
			case 'block':
648
				// Set default values for block.
649
				$field = wp_parse_args( $field, self::$block_defaults );
650
651
				// Set wrapper class.
652
				$field['block_attributes']['class'] = empty( $field['block_attributes']['class'] )
653
					? "give-block-wrapper js-give-block-wrapper give-block-{$field['name']}"
654
					: "give-block-wrapper js-give-block-wrapper give-block-{$field['name']} {$field['block_attributes']['class']}";
655
656
				foreach ( $field['fields'] as $key => $single_field ) {
657
					$single_field['name']    = ! empty( $single_field['name'] )
658
						? $single_field['name']
659
						: $key;
660
					$field['fields'][ $key ] = self::$instance->set_default_values( $single_field, $form, false );
661
				}
662
663
				break;
664
665
			case 'section':
666
				// Set default values for block.
667
				$field = wp_parse_args( $field, self::$section_defaults );
668
669
				// Set wrapper class.
670
				$field['section_attributes']['class'] = empty( $field['section_attributes']['class'] )
671
					? 'give-section-wrapper'
672
					: "give-section-wrapper {$field['section_attributes']['class']}";
673
674
				foreach ( $field['fields'] as $key => $single_field ) {
675
					$single_field['name']    = ! empty( $single_field['name'] )
676
						? $single_field['name']
677
						: $key;
678
					$field['fields'][ $key ] = self::$instance->set_default_values( $single_field, $form, false );
679
				}
680
681
				break;
682
683
			default:
684
				// Set default values for field or section.
685
				$field = wp_parse_args( $field, self::$field_defaults );
686
687
				// Set ID.
688
				$field['field_attributes']['id'] = empty( $field['field_attributes']['id'] )
689
					? "give-{$field['name']}-field"
690
					: $field['field_attributes']['id'];
691
692
				// Set class.
693
				$field['field_attributes']['class'] = empty( $field['field_attributes']['class'] )
694
					? "give-field js-give-field give-field-type-{$field['type']}"
695
					: "give-field js-give-field give-field-type-{$field['type']} {$field['field_attributes']['class']}";
696
697
				// Set wrapper class.
698
				$field['wrapper_attributes']['class'] = empty( $field['wrapper_attributes']['class'] )
699
					? 'give-field-wrapper'
700
					: "give-field-wrapper {$field['wrapper_attributes']['class']}";
701
		}
702
703
		/**
704
		 * Filter the field after set default values.
705
		 *
706
		 * @since 1.9
707
		 *
708
		 * @param array $field
709
		 * @param array $form
710
		 */
711
		$field = $fire_filter
712
			? apply_filters( 'give_field_api_post_set_default_values', $field, $form )
713
			: $field;
714
715
		return $field;
716
	}
717
718
719
	/**
720
	 * Set responsive fields.
721
	 *
722
	 * @since  1.9
723
	 * @access private
724
	 *
725
	 * @param $form
726
	 *
727
	 * @return mixed
728
	 */
729
	private function set_responsive_field( &$form ) {
730
731
		foreach ( $form['fields'] as $key => $field ) {
732
			switch ( true ) {
733
				case array_key_exists( 'fields', $field ):
734
					foreach ( $field['fields'] as $section_field_index => $section_field ) {
735
						if ( ! self::$instance->is_sub_section( $section_field ) ) {
736
							continue;
737
						}
738
739
						$form['fields'][ $key ]['fields'][ $section_field_index ]['wrapper_attributes']['class'] = 'give-form-col';
740
741
						if ( array_key_exists( 'sub_section_end', $section_field ) ) {
742
							$form['fields'][ $key ]['fields'][ $section_field_index ]['wrapper_attributes']['class'] = 'give-form-col give-form-col-end';
743
744
							// Clear float left for next field.
745
							$fields_keys = array_keys( $form['fields'][ $key ]['fields'] );
746
747
							if ( $next_field_key = array_search( $section_field_index, $fields_keys ) ) {
748
								if (
749
									! isset( $fields_keys[ $next_field_key + 1 ] )
750
									|| ! isset( $form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ] )
751
								) {
752
									continue;
753
								}
754
755
								$next_field = $form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ];
756
757
								$next_field['wrapper_attributes']['class'] = isset( $next_field['wrapper_attributes']['class'] )
758
									? $next_field['wrapper_attributes']['class'] . ' give-clearfix'
759
									: 'give-clearfix';
760
761
								$form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ] = $next_field;
762
							}
763
						}
764
					}
765
766
					break;
767
768
				default:
769
					if ( ! self::$instance->is_sub_section( $field ) ) {
770
						continue;
771
					}
772
773
					$form['fields'][ $key ]['wrapper_attributes']['class'] = 'give-form-col';
774
775
					if ( array_key_exists( 'sub_section_end', $field ) ) {
776
						$form['fields'][ $key ]['wrapper_attributes']['class'] = 'give-form-col give-form-col-end';
777
778
						// Clear float left for next field.
779
						$fields_keys = array_keys( $form['fields'] );
780
781
						if ( $next_field_key = array_search( $key, $fields_keys ) ) {
782
							$form['fields'][ $fields_keys[ $next_field_key + 1 ] ]['wrapper_attributes']['class'] = 'give-clearfix';
783
						}
784
					}
785
			}
786
		}
787
	}
788
789
790
	/**
791
	 * Check if current field is part of sub section or not.
792
	 *
793
	 * @since  1.9
794
	 * @access private
795
	 *
796
	 * @param $field
797
	 *
798
	 * @return bool
799
	 */
800
	private function is_sub_section( $field ) {
801
		$is_sub_section = false;
802
		if ( array_key_exists( 'sub_section_start', $field ) || array_key_exists( 'sub_section_end', $field ) ) {
803
			$is_sub_section = true;
804
		}
805
806
		return $is_sub_section;
807
	}
808
809
810
	/**
811
	 * Get field type.
812
	 *
813
	 * @since  1.9
814
	 * @access private
815
	 *
816
	 * @param $field
817
	 *
818
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
819
	 */
820
	private function get_field_type( $field ) {
821
		$field_type = 'field';
822
823
		if (
824
			isset( $field['type'] )
825
			&& 'block' === $field['type']
826
		) {
827
			$field_type = 'block';
828
829
		} else if ( array_key_exists( 'fields', $field ) ) {
830
			$field_type = 'section';
831
832
		}
833
834
		return $field_type;
835
	}
836
837
	/**
838
	 * Is the element a button?
839
	 *
840
	 * @since  1.9
841
	 * @access static
842
	 *
843
	 * @param array $element
844
	 *
845
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
846
	 */
847
	static function is_button( $element ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
848
		return preg_match( '/^button|submit$/', $element['#type'] );
849
	}
850
}
851
// @todo auto fill field values.
852
// @todo set clearfix class for section also