Completed
Push — issues/1038 ( fa741b...cbf4f0 )
by Ravinder
18:18
created

Give_Fields_API::render_multi_checkbox_field()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 3
nop 1
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
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
		'wrapper_type'         => 'p',
42
43
		// Add label, before and after field.
44
		'label'                => '',
45
		'label_position'       => 'before',
46
47
		// Add description to field as tooltip.
48
		'tooltip'              => '',
49
50
		// Show multiple fields in same row with in sub section.
51
		'sub_section_start'    => false,
52
		'sub_section_end'      => false,
53
54
		// Add custom attributes.
55
		'field_attributes'     => array(),
56
		'wrapper_attributes'   => array(),
57
58
		// Show/Hide field in before/after modal view.
59
		'show_without_modal'   => false,
60
		'show_within_modal'    => true,
61
62
		// Params to edit field html.
63
		'before_field'         => '',
64
		'after_field'          => '',
65
		'before_field_wrapper' => '',
66
		'after_field_wrapper'  => '',
67
		'before_label'         => '',
68
		'after_label'          => '',
69
70
		// Manually render field.
71
		'callback'             => '',
72
73
	);
74
75
	/**
76
	 * The defaults for all sections.
77
	 *
78
	 * @since  1.9
79
	 * @access static
80
	 */
81
	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...
82
		'type'               => 'section',
83
		'label'              => '',
84
		'name'               => '',
85
		'section_attributes' => array(),
86
87
		// Manually render section.
88
		'callback'           => '',
89
	);
90
91
	/**
92
	 * The defaults for all blocks.
93
	 *
94
	 * @since  1.9
95
	 * @access static
96
	 */
97
	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...
98
		'type'             => 'block',
99
		'label'            => '',
100
		'name'             => '',
101
		'block_attributes' => array(),
102
103
		// Manually render section.
104
		'callback'         => '',
105
	);
106
107
108
	private function __construct() {
109
	}
110
111
112
	/**
113
	 * Get instance.
114
	 *
115
	 * @return static
116
	 */
117
	public static function get_instance() {
118
		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...
119
			self::$instance = new static();
120
		}
121
122
		return self::$instance;
123
	}
124
125
	/**
126
	 * Initialize this module
127
	 *
128
	 * @since  1.9
129
	 * @access static
130
	 */
131
	public function init() {
132
		add_filter( 'give_form_api_render_form_tags', array( $this, 'render_tags' ), 10, 2 );
133
	}
134
135
136
	/**
137
	 * Render custom field.
138
	 *
139
	 * @since  1.0
140
	 * @access private
141
	 *
142
	 * @param array $field
143
	 * @param array $form
144
	 *
145
	 * @return bool
146
	 */
147
	private function render_custom_field( $field, $form = null ) {
148
		$field = self::$instance->set_default_values( $field, $form );
149
150
		$field_html = '';
151
152
		if ( empty( $field['callback'] ) ) {
153
			$callback = $field['callback'];
154
155
			// Process callback to get field html.
156
			if ( is_string( $callback ) && function_exists( "$callback" ) ) {
157
				$field_html = $callback( $field );
158
			} elseif ( is_array( $callback ) && method_exists( $callback[0], "$callback[1]" ) ) {
159
				$field_html = $callback[0]->$callback[1]( $field );
160
			}
161
		}
162
163
		return $field_html;
164
	}
165
166
167
	/**
168
	 * Render `{{form_fields}}` tag.
169
	 *
170
	 * @since  1.9
171
	 * @access private
172
	 *
173
	 * @param  string $form_html
174
	 * @param  array  $form
175
	 *
176
	 * @return string
177
	 */
178
	public function render_tags( $form_html, $form ) {
179
		// Bailout: If form does not contain any field.
180
		if ( empty( $form['fields'] ) ) {
181
			str_replace( '{{form_fields}}', '', $form_html );
182
183
			return $form_html;
184
		}
185
186
		$fields_html = '';
187
188
		// Set responsive fields.
189
		self::$instance->set_responsive_field( $form );
190
191
		// Render fields.
192
		foreach ( $form['fields'] as $key => $field ) {
193
			// Set default value.
194
			$field['name'] = empty( $field['name'] ) ? $key : $field['name'];
195
196
			// Render custom form with callback.
197
			if ( $field_html = self::$instance->render_custom_field( $field, $form ) ) {
198
				$fields_html .= $field_html;
199
			}
200
201
			switch ( true ) {
202
				// Block.
203
				case ( array_key_exists( 'type', $field ) && 'block' === $field['type'] ):
204
					$fields_html .= self::$instance->render_block( $field, $form );
205
					break;
206
207
				// Section.
208
				case array_key_exists( 'fields', $field ):
209
					$fields_html .= self::$instance->render_section( $field, $form );
210
					break;
211
212
				// Field
213
				default:
214
					$fields_html .= self::render_tag( $field, $form );
215
			}
216
		}
217
218
		$form_html = str_replace( '{{form_fields}}', $fields_html, $form_html );
219
220
		return $form_html;
221
	}
222
223
224
	/**
225
	 * Render section.
226
	 *
227
	 * @since  1.9
228
	 * @access public
229
	 *
230
	 * @param array $section
231
	 * @param array $form
232
	 * @param array $args Helper argument to render section.
233
	 *
234
	 * @return string
235
	 */
236
	public static function render_section( $section, $form = null, $args = array() ) {
237
		// Set default values if necessary.
238
		if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) {
239
			$section = self::$instance->set_default_values( $section, $form );
240
		}
241
242
		ob_start();
243
		?>
244
		<fieldset <?php echo self::$instance->get_attributes( $section['section_attributes'] ); ?>>
245
			<?php
246
			// Legend.
247
			if ( ! empty( $section['label'] ) ) {
248
				echo "<legend>{$section['label']}</legend>";
249
			};
250
251
			// Fields.
252
			foreach ( $section['fields'] as $key => $field ) {
253
				echo self::render_tag( $field, $form, array( 'set_default' => false ) );
254
			}
255
			?>
256
		</fieldset>
257
		<?php
258
		return ob_get_clean();
259
	}
260
261
262
	/**
263
	 * Render block.
264
	 *
265
	 * @since  1.9
266
	 * @access public
267
	 *
268
	 * @param array $block
269
	 * @param array $form
270
	 * @param array $args Helper argument to render section.
271
	 *
272
	 * @return string
273
	 */
274
	public static function render_block( $block, $form = null, $args = array() ) {
275
		// Set default values if necessary.
276
		if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) {
277
			$block = self::$instance->set_default_values( $block, $form );
278
		}
279
280
		ob_start();
281
		?>
282
		<div <?php echo self::$instance->get_attributes( $block['block_attributes'] ); ?>>
283
			<?php
284
			// Fields.
285
			foreach ( $block['fields'] as $key => $field ) {
286
				echo array_key_exists( 'fields', $field )
287
					? self::render_section( $field, $form, array( 'set_default' => false ) )
288
					: self::render_tag( $field, $form, array( 'set_default' => false ) );
289
			}
290
			?>
291
		</div>
292
		<?php
293
		return ob_get_clean();
294
	}
295
296
	/**
297
	 * Render tag
298
	 *
299
	 * @since   1.9
300
	 * @access  public
301
	 *
302
	 * @param array $field
303
	 * @param array $form
304
	 * @param array $args Helper argument to render section.
305
	 *
306
	 * @return string
307
	 */
308
	public static function render_tag( $field, $form = null, $args = array() ) {
309
		// Set default values if necessary.
310
		if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) {
311
			$field = self::$instance->set_default_values( $field, $form );
312
		}
313
314
		$field_html     = '';
315
		$functions_name = "render_{$field['type']}_field";
316
317
		if ( 'section' === self::$instance->get_field_type( $field ) ) {
318
			echo self::$instance->render_section( $field, $form, array( 'set_default' => false ) );
319
320
		} elseif ( method_exists( self::$instance, $functions_name ) ) {
321
			$field_html .= self::$instance->{$functions_name}( $field );
322
323
		} else {
324
			/**
325
			 * Filter the custom field type html.
326
			 * Developer can use this hook to render custom field type.
327
			 *
328
			 * @since 1.9
329
			 *
330
			 * @param string $field_html
331
			 * @param array  $field
332
			 * @param array  $form
333
			 */
334
			$field_html .= apply_filters(
335
				"give_field_api_render_{$field['type']}_field",
336
				$field_html,
337
				$field,
338
				$form
339
			);
340
		}
341
342
		return $field_html;
343
	}
344
345
346
	/**
347
	 * Render text field.
348
	 *
349
	 * @since  1.9
350
	 * @access private
351
	 *
352
	 * @param  array $field
353
	 *
354
	 * @return string
355
	 */
356
	public static function render_text_field( $field ) {
357
		$field_wrapper = self::$instance->render_field_wrapper( $field );
358
		ob_start();
359
		?>
360
		<input
361
				type="<?php echo $field['type']; ?>"
362
				name="<?php echo $field['name']; ?>"
363
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
364
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
365
		>
366
		<?php
367
368
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
369
	}
370
371
	/**
372
	 * Render submit field.
373
	 *
374
	 * @since  1.9
375
	 * @access private
376
	 *
377
	 * @param  array $field
378
	 *
379
	 * @return string
380
	 */
381
	public static function render_submit_field( $field ) {
382
		return self::$instance->render_text_field( $field );
383
	}
384
385
	/**
386
	 * Render checkbox field.
387
	 *
388
	 * @since  1.9
389
	 * @access private
390
	 *
391
	 * @param  array $field
392
	 *
393
	 * @return string
394
	 */
395
	public static function render_checkbox_field( $field ) {
396
		$field_wrapper = self::$instance->render_field_wrapper( $field );
397
		ob_start();
398
		?>
399
		<input
400
				type="checkbox"
401
				name="<?php echo $field['name']; ?>"
402
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
403
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
404
		>
405
		<?php
406
407
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
408
	}
409
410
	/**
411
	 * Render email field.
412
	 *
413
	 * @since  1.9
414
	 * @access private
415
	 *
416
	 * @param  array $field
417
	 *
418
	 * @return string
419
	 */
420
	public static function render_email_field( $field ) {
421
		return self::$instance->render_text_field( $field );
422
	}
423
424
	/**
425
	 * Render number field.
426
	 *
427
	 * @since  1.9
428
	 * @access private
429
	 *
430
	 * @param  array $field
431
	 *
432
	 * @return string
433
	 */
434
	public static function render_number_field( $field ) {
435
		return self::$instance->render_text_field( $field );
436
	}
437
438
	/**
439
	 * Render password field.
440
	 *
441
	 * @since  1.9
442
	 * @access private
443
	 *
444
	 * @param  array $field
445
	 *
446
	 * @return string
447
	 */
448
	public static function render_password_field( $field ) {
449
		return self::$instance->render_text_field( $field );
450
	}
451
452
	/**
453
	 * Render button field.
454
	 *
455
	 * @since  1.9
456
	 * @access private
457
	 *
458
	 * @param  array $field
459
	 *
460
	 * @return string
461
	 */
462
	public static function render_button_field( $field ) {
463
		return self::$instance->render_text_field( $field );
464
	}
465
466
	/**
467
	 * Render textarea field.
468
	 *
469
	 * @since  1.9
470
	 * @access private
471
	 *
472
	 * @param  array $field
473
	 *
474
	 * @return string
475
	 */
476
	public static function render_textarea_field( $field ) {
477
		$field_wrapper = self::$instance->render_field_wrapper( $field );
478
		ob_start();
479
		?>
480
		<textarea
481
				name="<?php echo $field['name']; ?>"
482
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
483
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
484
		><?php echo $field ['value']; ?></textarea>
485
486
487
		<?php
488
489
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
490
	}
491
492
	/**
493
	 * Render select field.
494
	 *
495
	 * @since  1.9
496
	 * @access private
497
	 *
498
	 * @param  array $field
499
	 *
500
	 * @return string
501
	 */
502
	public static function render_select_field( $field ) {
503
		$field_wrapper = self::$instance->render_field_wrapper( $field );
504
		ob_start();
505
506
		$options_html = '';
507
		foreach ( $field['options'] as $key => $option ) {
508
			$selected = '';
509
510
			if ( is_array( $field['value'] ) ) {
511
				$selected = in_array( $key, $field['value'] )
512
					? 'selected="selected"'
513
					: '';
514
515
			} else {
516
				$selected = selected( $key, $field['value'], false );
517
			}
518
519
			$options_html .= '<option value="' . $key . '" ' . $selected . '>' . $option . '</option>';
520
		}
521
		?>
522
523
		<select
524
				name="<?php echo $field['name']; ?>"
525
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
526
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
527
		><?php echo $options_html; ?></select>
528
		<?php
529
530
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
531
	}
532
533
	/**
534
	 * Render multi select field.
535
	 *
536
	 * @since  1.9
537
	 * @access private
538
	 *
539
	 * @param  array $field
540
	 *
541
	 * @return string
542
	 */
543
	public static function render_multi_select_field( $field ) {
544
		$field['field_attributes'] = array_merge( $field['field_attributes'], array( 'multiple' => 'multiple' ) );
545
		$field['name']             = "{$field['name']}[]";
546
547
		return self::$instance->render_select_field( $field );
548
	}
549
550
	/**
551
	 * Render text field.
552
	 *
553
	 * @since  1.9
554
	 * @access private
555
	 *
556
	 * @param  array $field
557
	 *
558
	 * @return string
559
	 */
560
	public static function render_radio_field( $field ) {
561
		$field_wrapper = self::$instance->render_field_wrapper( $field );
562
		ob_start();
563
564
		$id_base = $field['field_attributes']['id'];
565
		unset( $field['field_attributes']['id'] );
566
567
		foreach ( $field['options'] as $key => $option ) :
568
			?>
569
			<label class="give-label" for="<?php echo "{$id_base}-{$key}" ?>">
570
				<input
571
						type="<?php echo $field['type']; ?>"
572
						name="<?php echo $field['name']; ?>"
573
						value="<?php echo $key; ?>"
574
						id="<?php echo "{$id_base}-{$key}"; ?>"
575
					<?php checked( $key, $field['value'] ) ?>
576
					<?php echo( $field['required'] ? 'required=""' : '' ); ?>
577
					<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
578
				><?php echo $option; ?>
579
			</label>
580
			<?php
581
		endforeach;
582
583
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
584
	}
585
586
	/**
587
	 * Render multi checkbox field.
588
	 *
589
	 * @since  1.9
590
	 * @access private
591
	 *
592
	 * @param  array $field
593
	 *
594
	 * @return string
595
	 */
596
	public static function render_multi_checkbox_field( $field ) {
597
		$field_wrapper = self::$instance->render_field_wrapper( $field );
598
		ob_start();
599
600
		$id_base = $field['field_attributes']['id'];
601
		unset( $field['field_attributes']['id'] );
602
603
		foreach ( $field['options'] as $key => $option ) :
604
			$checked = in_array( $key, $field['value'] )
605
				? 'checked="checked"'
606
				: '';
607
			?>
608
			<label class="give-label" for="<?php echo "{$id_base}-{$key}" ?>">
609
				<input
610
						type="checkbox"
611
						name="<?php echo $field['name']; ?>[]"
612
						value="<?php echo $key; ?>"
613
						id="<?php echo "{$id_base}-{$key}"; ?>"
614
					<?php echo $checked ?>
615
					<?php echo( $field['required'] ? 'required=""' : '' ); ?>
616
					<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
617
				><?php echo $option; ?>
618
			</label>
619
			<?php
620
		endforeach;
621
622
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
623
	}
624
625
626
	/**
627
	 * Render wrapper
628
	 *
629
	 * @since  1.9
630
	 * @access private
631
	 *
632
	 * @param $field
633
	 *
634
	 * @return string
635
	 */
636
	private function render_field_wrapper( $field ) {
637
		ob_start();
638
639
		if ( $field['wrapper'] ) :
640
641
			echo $field['before_field_wrapper'];
642
			?>
643
			<<?php echo $field['wrapper_type']; ?> <?php echo self::$instance->get_attributes( $field['wrapper_attributes'] ); ?>>
644
			<?php
645
			// Label: before field.
646
			if ( 'before' === $field['label_position'] ) {
647
				echo self::$instance->render_label( $field );
648
			}
649
650
			echo "{$field['before_field']}{{form_field}}{$field['after_field']}";
651
652
			// Label: before field.
653
			if ( 'after' === $field['label_position'] ) {
654
				echo self::$instance->render_label( $field );
655
			}
656
			?>
657
			</<?php echo $field['wrapper_type']; ?>>
658
			<?php
659
			echo $field['after_field_wrapper'];
660
		else :
661
			echo "{$field['before_field']}{{form_field}}{$field['after_field']}";
662
		endif;
663
664
		return ob_get_clean();
665
	}
666
667
668
	/**
669
	 * Render label
670
	 *
671
	 * @since  1.9
672
	 * @access private
673
	 *
674
	 * @param $field
675
	 *
676
	 * @return string
677
	 */
678
	private function render_label( $field ) {
679
		ob_start();
680
		?>
681
		<?php if ( ! empty( $field['label'] ) ) : ?>
682
			<?php echo $field['before_label']; ?>
683
			<label class="give-label" for="<?php echo $field['field_attributes']['id']; ?>">
684
685
				<?php echo $field['label']; ?>
686
687
				<?php if ( $field['required'] ) : ?>
688
					<span class="give-required-indicator">*</span>
689
				<?php endif; ?>
690
691
				<?php if ( $field['tooltip'] ) : ?>
692
					<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php echo $field['tooltip'] ?>"></span>
693
				<?php endif; ?>
694
			</label>
695
			<?php echo $field['after_label']; ?>
696
		<?php endif; ?>
697
		<?php
698
		return ob_get_clean();
699
	}
700
701
	/**
702
	 * Get field attribute string from field arguments.
703
	 *
704
	 * @since  1.9
705
	 * @access private
706
	 *
707
	 * @param array $attributes
708
	 *
709
	 * @return array|string
710
	 */
711
	private function get_attributes( $attributes ) {
712
		$field_attributes_val = '';
713
714
		if ( ! empty( $attributes ) ) {
715
			foreach ( $attributes as $attribute_name => $attribute_val ) {
716
				$field_attributes_val[] = "{$attribute_name}=\"{$attribute_val}\"";
717
			}
718
		}
719
720
		if ( ! empty( $field_attributes_val ) ) {
721
			$field_attributes_val = implode( ' ', $field_attributes_val );
722
		}
723
724
		return $field_attributes_val;
725
	}
726
727
	/**
728
	 * Set default values for fields
729
	 *
730
	 * @since  1.0
731
	 * @access private
732
	 *
733
	 * @param array $field
734
	 * @param array $form
735
	 * @param bool  $fire_filter
736
	 *
737
	 * @return array
738
	 */
739
	private function set_default_values( $field, $form = null, $fire_filter = true ) {
740
		/**
741
		 * Filter the field before set default values.
742
		 *
743
		 * @since 1.9
744
		 *
745
		 * @param array $field
746
		 * @param array $form
747
		 */
748
		$field = $fire_filter
749
			? apply_filters( 'give_field_api_pre_set_default_values', $field, $form )
750
			: $field;
751
752
		switch ( self::$instance->get_field_type( $field ) ) {
753
			case 'block':
754
				// Set default values for block.
755
				$field = wp_parse_args( $field, self::$block_defaults );
756
757
				// Set wrapper class.
758
				$field['block_attributes']['class'] = empty( $field['block_attributes']['class'] )
759
					? "give-block-wrapper js-give-block-wrapper give-block-{$field['name']}"
760
					: "give-block-wrapper js-give-block-wrapper give-block-{$field['name']} {$field['block_attributes']['class']}";
761
762
				foreach ( $field['fields'] as $key => $single_field ) {
763
					$single_field['name']    = ! empty( $single_field['name'] )
764
						? $single_field['name']
765
						: $key;
766
					$field['fields'][ $key ] = self::$instance->set_default_values( $single_field, $form, false );
767
				}
768
769
				break;
770
771
			case 'section':
772
				// Set default values for block.
773
				$field = wp_parse_args( $field, self::$section_defaults );
774
775
				// Set wrapper class.
776
				$field['section_attributes']['class'] = empty( $field['section_attributes']['class'] )
777
					? 'give-section-wrapper'
778
					: "give-section-wrapper {$field['section_attributes']['class']}";
779
780
				foreach ( $field['fields'] as $key => $single_field ) {
781
					$single_field['name']    = ! empty( $single_field['name'] )
782
						? $single_field['name']
783
						: $key;
784
					$field['fields'][ $key ] = self::$instance->set_default_values( $single_field, $form, false );
785
				}
786
787
				break;
788
789
			default:
790
				// Set default values for field or section.
791
				$field = wp_parse_args( $field, self::$field_defaults );
792
793
				// Set ID.
794
				$field['field_attributes']['id'] = empty( $field['field_attributes']['id'] )
795
					? "give-{$field['name']}-field"
796
					: $field['field_attributes']['id'];
797
798
				// Set class.
799
				$field['field_attributes']['class'] = empty( $field['field_attributes']['class'] )
800
					? "give-field js-give-field give-field-type-{$field['type']}"
801
					: "give-field js-give-field give-field-type-{$field['type']} {$field['field_attributes']['class']}";
802
803
				// Set wrapper class.
804
				$field['wrapper_attributes']['class'] = empty( $field['wrapper_attributes']['class'] )
805
					? 'give-field-wrapper'
806
					: "give-field-wrapper {$field['wrapper_attributes']['class']}";
807
808
				/**
809
				 * Filter the field values.
810
				 *
811
				 * @since 1.9
812
				 *
813
				 * @param array $field
814
				 * @param array $form
815
				 */
816
				$field = apply_filters( 'give_field_api_set_values', $field, $form );
817
		}
818
819
		/**
820
		 * Filter the field after set default values.
821
		 *
822
		 * @since 1.9
823
		 *
824
		 * @param array $field
825
		 * @param array $form
826
		 */
827
		$field = $fire_filter
828
			? apply_filters( 'give_field_api_post_set_default_values', $field, $form )
829
			: $field;
830
831
		return $field;
832
	}
833
834
835
	/**
836
	 * Set responsive fields.
837
	 *
838
	 * @since  1.9
839
	 * @access private
840
	 *
841
	 * @param $form
842
	 *
843
	 * @return mixed
844
	 */
845
	private function set_responsive_field( &$form ) {
846
847
		foreach ( $form['fields'] as $key => $field ) {
848
			switch ( true ) {
849
				case array_key_exists( 'fields', $field ):
850
					foreach ( $field['fields'] as $section_field_index => $section_field ) {
851
						if ( ! self::$instance->is_sub_section( $section_field ) ) {
852
							continue;
853
						}
854
855
						$form['fields'][ $key ]['fields'][ $section_field_index ]['wrapper_attributes']['class'] = 'give-form-col';
856
857
						if ( array_key_exists( 'sub_section_end', $section_field ) ) {
858
							$form['fields'][ $key ]['fields'][ $section_field_index ]['wrapper_attributes']['class'] = 'give-form-col give-form-col-end';
859
860
							// Clear float left for next field.
861
							$fields_keys = array_keys( $form['fields'][ $key ]['fields'] );
862
863
							if ( $next_field_key = array_search( $section_field_index, $fields_keys ) ) {
864
								if (
865
									! isset( $fields_keys[ $next_field_key + 1 ] )
866
									|| ! isset( $form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ] )
867
								) {
868
									continue;
869
								}
870
871
								$next_field = $form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ];
872
873
								$next_field['wrapper_attributes']['class'] = isset( $next_field['wrapper_attributes']['class'] )
874
									? $next_field['wrapper_attributes']['class'] . ' give-clearfix'
875
									: 'give-clearfix';
876
877
								$form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ] = $next_field;
878
							}
879
						}
880
					}
881
882
					break;
883
884
				default:
885
					if ( ! self::$instance->is_sub_section( $field ) ) {
886
						continue;
887
					}
888
889
					$form['fields'][ $key ]['wrapper_attributes']['class'] = 'give-form-col';
890
891
					if ( array_key_exists( 'sub_section_end', $field ) ) {
892
						$form['fields'][ $key ]['wrapper_attributes']['class'] = 'give-form-col give-form-col-end';
893
894
						// Clear float left for next field.
895
						$fields_keys = array_keys( $form['fields'] );
896
897
						if ( $next_field_key = array_search( $key, $fields_keys ) ) {
898
							$form['fields'][ $fields_keys[ $next_field_key + 1 ] ]['wrapper_attributes']['class'] = 'give-clearfix';
899
						}
900
					}
901
			}
902
		}
903
	}
904
905
906
	/**
907
	 * Check if current field is part of sub section or not.
908
	 *
909
	 * @since  1.9
910
	 * @access private
911
	 *
912
	 * @param $field
913
	 *
914
	 * @return bool
915
	 */
916
	private function is_sub_section( $field ) {
917
		$is_sub_section = false;
918
		if ( array_key_exists( 'sub_section_start', $field ) || array_key_exists( 'sub_section_end', $field ) ) {
919
			$is_sub_section = true;
920
		}
921
922
		return $is_sub_section;
923
	}
924
925
926
	/**
927
	 * Get field type.
928
	 *
929
	 * @since  1.9
930
	 * @access private
931
	 *
932
	 * @param $field
933
	 *
934
	 * @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...
935
	 */
936
	public static function get_field_type( $field ) {
937
		$field_type = 'field';
938
939
		if (
940
			isset( $field['type'] )
941
			&& 'block' === $field['type']
942
		) {
943
			$field_type = 'block';
944
945
		} elseif ( array_key_exists( 'fields', $field ) ) {
946
			$field_type = 'section';
947
948
		}
949
950
		return $field_type;
951
	}
952
953
	/**
954
	 * Is the element a button?
955
	 *
956
	 * @since  1.9
957
	 * @access static
958
	 *
959
	 * @param array $element
960
	 *
961
	 * @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...
962
	 */
963
	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...
964
		return preg_match( '/^button|submit$/', $element['#type'] );
965
	}
966
}