Completed
Push — issues/1038 ( 7b5150...f8dacd )
by Ravinder
20:43
created

Give_Fields_API::render_field_label()   C

Complexity

Conditions 7
Paths 40

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 29
nc 40
nop 3
dl 0
loc 37
rs 6.7272
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
		'id'                   => '',
31
		'data_type'            => '',
32
		'value'                => null,
33
		'required'             => false,
34
		'options'              => array(),
35
36
		// Set default value to field.
37
		'default'              => null,
38
39
		// Set checkbox value.
40
		'cbvalue'              => 'on',
41
42
		// Field with wrapper.
43
		'wrapper'              => true,
44
		'wrapper_type'         => 'p',
45
46
		// Add label, before and after field.
47
		'label'                => '',
48
		'label_position'       => 'before',
49
		'label_tooltip'        => '',
50
51
		// Show multiple fields in same row with in sub section.
52
		'sub_section_start'    => false,
53
		'sub_section_end'      => false,
54
55
		// Sortable.
56
		'sortable'             => false,
57
		'sortable-icon'        => false,
58
59
		// Add custom attributes.
60
		'label_attributes'     => array(),
61
		'field_attributes'     => array(),
62
		'wrapper_attributes'   => array(),
63
		'ul_attributes'        => array(),
64
65
		// Show/Hide field in before/after modal view.
66
		'show_without_modal'   => false,
67
		'show_within_modal'    => true,
68
69
		// Params to edit field html.
70
		// They accept callback or string input.
71
		'before_field'         => '',
72
		'after_field'          => '',
73
		'before_field_wrapper' => '',
74
		'after_field_wrapper'  => '',
75
		'before_field_label'   => '',
76
		'after_field_label'    => '',
77
78
		// Manually render field.
79
		'callback'             => '',
80
81
	);
82
83
	/**
84
	 * The defaults for all sections.
85
	 *
86
	 * @since  1.9
87
	 * @access static
88
	 */
89
	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...
90
		'type'                 => 'section',
91
		'label'                => '',
92
		'id'                   => '',
93
		'section_attributes'   => array(),
94
95
		// Manually render section.
96
		'callback'             => '',
97
98
		// Params to edit field html.
99
		// They accept callback or string input.
100
		'before_field_wrapper' => '',
101
		'after_field_wrapper'  => '',
102
		'before_field_label'   => '',
103
		'after_field_label'    => '',
104
	);
105
106
	/**
107
	 * The defaults for all blocks.
108
	 *
109
	 * @since  1.9
110
	 * @access static
111
	 */
112
	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...
113
		'type'             => 'block',
114
		'label'            => '',
115
		'id'             => '',
116
		'block_attributes' => array(),
117
118
		// Manually render section.
119
		'callback'         => '',
120
	);
121
122
123
	private function __construct() {
124
	}
125
126
127
	/**
128
	 * Get instance.
129
	 *
130
	 * @return static
131
	 */
132
	public static function get_instance() {
133
		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...
134
			self::$instance = new static();
135
		}
136
137
		return self::$instance;
138
	}
139
140
	/**
141
	 * Initialize this module
142
	 *
143
	 * @since  1.9
144
	 * @access static
145
	 */
146
	public function init() {
147
		add_filter( 'give_form_api_render_form_tags', array( $this, 'render_tags' ), 10, 2 );
148
	}
149
150
151
	/**
152
	 * Render callback.
153
	 *
154
	 * @since 1.9
155
	 * @access public
156
	 * @param       $callback
157
	 * @param array $field
158
	 * @param null  $form
159
	 *
160
	 * @return string
161
	 */
162
	public static function render_callback( $callback, $field = array(), $form = null  ){
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
		$field_html = '';
164
165
		if( empty( $callback ) || ! self::is_callback( $callback ) ) {
166
			return $field_html;
167
		}
168
169
		// Process callback to get field html.
170
		if ( is_string( $callback ) && function_exists( "$callback" ) ) {
171
			$field_html = $callback( $field );
172
		} elseif ( is_array( $callback ) && method_exists( $callback[0], "$callback[1]" ) ) {
173
			$field_html = $callback[0]->$callback[1]( $field );
174
		}
175
176
		return $field_html;
177
	}
178
179
180
	/**
181
	 * Render custom field.
182
	 *
183
	 * @since  1.0
184
	 * @access private
185
	 *
186
	 * @param array $field
187
	 * @param array $form
188
	 *
189
	 * @return string
190
	 */
191
	private function render_custom_field( $field, $form = null ) {
192
		$field_html = '';
193
194
		if ( ! empty( $field['callback'] ) ) {
195
			$field = self::$instance->set_default_values( $field, $form );
196
			
197
			$callback = $field['callback'];
198
			$field_html = self::render_callback( $callback, $field, $form );
199
		}
200
201
		return $field_html;
202
	}
203
204
205
	/**
206
	 * Render `{{form_fields}}` tag.
207
	 *
208
	 * @since  1.9
209
	 * @access private
210
	 *
211
	 * @param  string $form_html
212
	 * @param  array  $form
213
	 *
214
	 * @return string
215
	 */
216
	public function render_tags( $form_html, $form ) {
217
		// Bailout: If form does not contain any field.
218
		if ( empty( $form['fields'] ) ) {
219
			str_replace( '{{form_fields}}', '', $form_html );
220
221
			return $form_html;
222
		}
223
224
		$fields_html = '';
225
226
		// Set responsive fields.
227
		self::$instance->set_responsive_field( $form );
228
229
		// Render fields.
230
		foreach ( $form['fields'] as $key => $field ) {
231
			// Set default value.
232
			$field['id'] = empty( $field['id'] ) ? $key : $field['id'];
233
234
			// Render custom form with callback.
235
			if ( $field_html = self::$instance->render_custom_field( $field, $form ) ) {
236
				$fields_html .= $field_html;
237
			}
238
239
			switch ( true ) {
240
				// Block.
241
				case ( 'block' === self::get_field_type( $field ) ):
242
					$fields_html .= self::$instance->render_block( $field, $form );
243
					break;
244
245
				// Section.
246
				case ( 'section' === self::get_field_type( $field ) ):
247
					$fields_html .= self::$instance->render_section( $field, $form );
248
					break;
249
250
				// Field
251
				default:
252
					$fields_html .= self::render_tag( $field, $form );
253
			}
254
		}
255
256
		$form_html = str_replace( '{{form_fields}}', $fields_html, $form_html );
257
258
		return $form_html;
259
	}
260
261
262
	/**
263
	 * Render section.
264
	 *
265
	 * @since  1.9
266
	 * @access public
267
	 *
268
	 * @param array $section
269
	 * @param array $form
270
	 * @param array $args Helper argument to render section.
271
	 *
272
	 * @return string
273
	 */
274
	public static function render_section( $section, $form = null, $args = array() ) {
275
		// Set default values if necessary.
276
		if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) {
277
			$section = self::$instance->set_default_values( $section, $form );
278
		}
279
280
		ob_start();
281
282
		// Set before section html.
283
		$section['before_field_wrapper'] = self::is_callback( $section['before_field_wrapper'] )
284
			? self::render_callback( $section['before_field_wrapper'] )
285
			: $section['before_field_wrapper'];
286
287
		// Set after section html.
288
		$section['after_field_wrapper'] = self::is_callback( $section['after_field_wrapper'] )
289
			? self::render_callback( $section['after_field_wrapper'] )
290
			: $section['after_field_wrapper'];
291
292
		// Set before section legend html.
293
		$section['before_field_label'] = self::is_callback( $section['before_field_label'] )
294
			? self::render_callback( $section['before_field_label'] )
295
			: $section['before_field_label'];
296
297
		// Set after section legend html.
298
		$section['after_field_label'] = self::is_callback( $section['after_field_label'] )
299
			? self::render_callback( $section['after_field_label'] )
300
			: $section['after_field_label'];
301
302
		echo $section['before_field_wrapper'];
303
		?>
304
		<fieldset <?php echo self::$instance->get_attributes( $section['section_attributes'] ); ?>>
305
			<?php
306
			// Legend.
307
			if ( ! empty( $section['label'] ) ) {
308
				echo $section['before_field_label'];
309
					echo "<legend ". self::get_attributes( $section['label_attributes'] ).">{$section['label']}</legend>";
310
				echo $section['after_field_label'];
311
			};
312
313
			// Fields.
314
			foreach ( $section['fields'] as $key => $field ) {
315
				echo self::render_tag( $field, $form, array( 'set_default' => false ) );
316
			}
317
			?>
318
		</fieldset>
319
		<?php
320
		echo $section['after_field_wrapper'];
321
322
		$section_html = ob_get_clean();
323
324
		/**
325
		 * Filter the section html.
326
		 *
327
		 * @since 1.9
328
		 *
329
		 * @param string $section_html
330
		 * @param array  $field
331
		 * @param array  $form
332
		 */
333
		$section_html = apply_filters(
334
			"give_field_api_render_section",
335
			$section_html,
336
			$section,
337
			$form,
338
			$args
339
		);
340
		return $section_html;
341
	}
342
343
344
	/**
345
	 * Render block.
346
	 *
347
	 * @since  1.9
348
	 * @access public
349
	 *
350
	 * @param array $block
351
	 * @param array $form
352
	 * @param array $args Helper argument to render section.
353
	 *
354
	 * @return string
355
	 */
356
	public static function render_block( $block, $form = null, $args = array() ) {
357
		// Set default values if necessary.
358
		if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) {
359
			$block = self::$instance->set_default_values( $block, $form );
360
		}
361
362
		ob_start();
363
		?>
364
		<div <?php echo self::$instance->get_attributes( $block['block_attributes'] ); ?>>
365
			<?php
366
			// Fields.
367
			foreach ( $block['fields'] as $key => $field ) {
368
				echo array_key_exists( 'fields', $field )
369
					? self::render_section( $field, $form, array( 'set_default' => false ) )
370
					: self::render_tag( $field, $form, array( 'set_default' => false ) );
371
			}
372
			?>
373
		</div>
374
		<?php
375
		$block_html = ob_get_clean();
376
377
		/**
378
		 * Filter the block html.
379
		 *
380
		 * @since 1.9
381
		 *
382
		 * @param string $block_html
383
		 * @param array  $field
384
		 * @param array  $form
385
		 */
386
		$block_html = apply_filters(
387
			"give_field_api_render_block",
388
			$block_html,
389
			$block,
390
			$form,
391
			$args
392
		);
393
394
		return $block_html;
395
	}
396
397
	/**
398
	 * Render tag
399
	 *
400
	 * @since   1.9
401
	 * @access  public
402
	 *
403
	 * @param array $field
404
	 * @param array $form
405
	 * @param array $args Helper argument to render section.
406
	 *
407
	 * @return string
408
	 */
409
	public static function render_tag( $field, $form = null, $args = array() ) {
410
		// Enqueue scripts.
411
		Give_Form_API::enqueue_scripts();
412
413
		if( ! empty( $field['sortable'] ) ) {
414
			wp_enqueue_script('jquery-ui-sortable');
415
		}
416
417
		// Set default values if necessary.
418
		if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) {
419
			$field = self::$instance->set_default_values( $field, $form );
420
		}
421
422
		$field_html     = '';
423
		$functions_name = "render_{$field['type']}_field";
424
425
		if ( 'section' === self::$instance->get_field_type( $field ) ) {
426
			$field_html = self::$instance->render_section( $field, $form, array( 'set_default' => false ) );
427
428
		} elseif ( method_exists( self::$instance, $functions_name ) ) {
429
			$field_html = self::$instance->{$functions_name}( $field, $form, $args );
430
431
		}
432
433
		/**
434
		 * Filter the custom field type html.
435
		 * Developer can use this hook to render custom field type.
436
		 *
437
		 * @since 1.9
438
		 *
439
		 * @param string $field_html
440
		 * @param array  $field
441
		 * @param array  $form
442
		 */
443
		$field_html = apply_filters(
444
			"give_field_api_render_{$field['type']}_field",
445
			$field_html,
446
			$field,
447
			$form,
448
			$args
449
		);
450
451
		/**
452
		 * Filter the all field type html.
453
		 *
454
		 * @since 1.9
455
		 *
456
		 * @param string $field_html
457
		 * @param array  $field
458
		 * @param array  $form
459
		 */
460
		$field_html = apply_filters(
461
			"give_field_api_render_field",
462
			$field_html,
463
			$field,
464
			$form,
465
			$args
466
		);
467
468
		return $field_html;
469
	}
470
471
472
	/**
473
	 * Render text field.
474
	 *
475
	 * @since  1.9
476
	 * @access public
477
	 *
478
	 * @param  array $field
479
	 * @param  array $form
480
	 * @param  array $args
481
	 *
482
	 * @return string
483
	 */
484
	public static function render_text_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
485
		$field_wrapper = self::$instance->render_field_wrapper( $field );
486
		$field['field_attributes']['name'] = self::get_field_name( $field );
487
		$field['field_attributes']['type'] = $field['type'];
488
489
		if( ! empty( $field['required'] ) ) {
490
			$field['field_attributes']['required'] = 'required';
491
			$field['field_attributes']['aria-required'] = 'true';
492
		}
493
494
		ob_start();
495
496
		echo '<input ' . self::$instance->get_attributes( $field['field_attributes'] ) . '>';
497
498
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
499
	}
500
501
	/**
502
	 * Render submit field.
503
	 *
504
	 * @since  1.9
505
	 * @access public
506
	 *
507
	 * @param  array $field
508
	 * @param  array $form
509
	 * @param  array $args
510
	 *
511
	 * @return string
512
	 */
513
	public static function render_submit_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
514
		return self::$instance->render_text_field( $field );
515
	}
516
517
	/**
518
	 * Render checkbox field.
519
	 *
520
	 * @since  1.9
521
	 * @access public
522
	 *
523
	 * @param  array $field
524
	 * @param  array $form
525
	 * @param  array $args
526
	 *
527
	 * @return string
528
	 */
529
	public static function render_checkbox_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
530
		return self::$instance->render_text_field( $field );
531
	}
532
533
	/**
534
	 * Render email field.
535
	 *
536
	 * @since  1.9
537
	 * @access public
538
	 *
539
	 * @param  array $field
540
	 * @param  array $form
541
	 * @param  array $args
542
	 *
543
	 * @return string
544
	 */
545
	public static function render_email_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
546
		return self::$instance->render_text_field( $field );
547
	}
548
549
	/**
550
	 * Render number field.
551
	 *
552
	 * @since  1.9
553
	 * @access public
554
	 *
555
	 * @param  array $field
556
	 * @param  array $form
557
	 * @param  array $args
558
	 *
559
	 * @return string
560
	 */
561
	public static function render_number_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
562
		return self::$instance->render_text_field( $field );
563
	}
564
565
	/**
566
	 * Render password field.
567
	 *
568
	 * @since  1.9
569
	 * @access public
570
	 *
571
	 * @param  array $field
572
	 * @param  array $form
573
	 * @param  array $args
574
	 *
575
	 * @return string
576
	 */
577
	public static function render_password_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
578
		return self::$instance->render_text_field( $field );
579
	}
580
581
	/**
582
	 * Render button field.
583
	 *
584
	 * @since  1.9
585
	 * @access public
586
	 *
587
	 * @param  array $field
588
	 * @param  array $form
589
	 * @param  array $args
590
	 *
591
	 * @return string
592
	 */
593
	public static function render_button_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
594
		return self::$instance->render_text_field( $field );
595
	}
596
597
	/**
598
	 * Render hidden field.
599
	 *
600
	 * @since  1.9
601
	 * @access public
602
	 *
603
	 * @param  array $field
604
	 * @param  array $form
605
	 * @param  array $args
606
	 *
607
	 * @return string
608
	 */
609
	public static function render_hidden_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
610
		$field['wrapper'] = false;
611
612
		return self::$instance->render_text_field( $field );
613
	}
614
615
	/**
616
	 * Render textarea field.
617
	 *
618
	 * @since  1.9
619
	 * @access public
620
	 *
621
	 * @param  array $field
622
	 * @param  array $form
623
	 * @param  array $args
624
	 *
625
	 * @return string
626
	 */
627
	public static function render_textarea_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
628
		$field_wrapper = self::$instance->render_field_wrapper( $field );
629
		ob_start();
630
		?>
631
		<textarea
632
				name="<?php echo self::get_field_name( $field ); ?>"
633
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
634
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
635
		><?php echo $field ['value']; ?></textarea>
636
637
638
		<?php
639
640
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
641
	}
642
643
	/**
644
	 * Render select field.
645
	 *
646
	 * @since  1.9
647
	 * @access public
648
	 *
649
	 * @param  array $field
650
	 * @param  array $form
651
	 * @param  array $args
652
	 *
653
	 * @return string
654
	 */
655
	public static function render_select_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
656
		$field_wrapper = self::$instance->render_field_wrapper( $field );
657
		ob_start();
658
659
		$options_html = '';
660
		foreach ( $field['options'] as $key => $option ) {
661
			$options_html .= '<option '. self::get_attributes( $option['field_attributes'] ). '>' . $option['label'] . '</option>';
662
		}
663
		?>
664
665
		<select
666
				name="<?php echo self::get_field_name( $field ); ?>"
667
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
668
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
669
		><?php echo $options_html; ?></select>
670
		<?php
671
672
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
673
	}
674
675
	/**
676
	 * Render multi select field.
677
	 *
678
	 * @since  1.9
679
	 * @access public
680
	 *
681
	 * @param  array $field
682
	 * @param  array $form
683
	 * @param  array $args
684
	 *
685
	 * @return string
686
	 */
687
	public static function render_multi_select_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
688
		$field['field_attributes'] = array_merge( $field['field_attributes'], array( 'multiple' => 'multiple' ) );
689
		$field['id']             = "{$field['id']}[]";
690
691
		return self::$instance->render_select_field( $field );
692
	}
693
694
	/**
695
	 * Render radio field.
696
	 *
697
	 * @since  1.9
698
	 * @access public
699
	 *
700
	 * @param  array $field
701
	 * @param  array $form
702
	 * @param  array $args
703
	 *
704
	 * @return string
705
	 */
706
	public static function render_radio_field( $field, $form = null, $args = array() ) {
707
		$field['wrapper_type'] = 'p' === $field['wrapper_type']
708
			? 'fieldset'
709
			: $field['wrapper_type'];
710
711
712
		$field_wrapper = self::$instance->render_field_wrapper( $field, $form, $args );
713
714
		ob_start();
715
716
		$id_base = $field['field_attributes']['id'];
717
		unset( $field['field_attributes']['id'] );
718
719
		$field['ul_attributes']['class'] = empty( $field['ul_attributes']['class'] )
720
			? 'give-radio-fields'
721
			: "give-radio-fields {$field['ul_attributes']['class']}";
722
723
		echo '<ul ' . self::get_attributes( $field['ul_attributes'] ) . '>';
724
		foreach ( $field['options'] as $key => $option ) :
725
			$option['label_position'] = ! empty( $option['label_position'] ) ? $option['label_position'] : $field['label_position'];
726
727
			$option = wp_parse_args( $option, self::$field_defaults );
728
729
			$option['field_attributes']['type'] = $field['type'];
730
			$option['field_attributes']['name'] = self::get_field_name( $field );
731
732
			$option['field_attributes']['id']   = ! empty( $option['field_attributes']['id'] )
733
				? $option['field_attributes']['id']
734
				: "{$id_base}-{$key}-" . uniqid();
735
736
			$option['label_attributes']['for']  = ! empty( $option['label_attributes']['for'] )
737
				? $option['label_attributes']['for']
738
				: $option['field_attributes']['id'];
739
740
741
			$option['label_attributes']['class'] = empty( $option['label_attributes']['class'] )
742
				? 'give-label'
743
				: "give-label {$option['label_attributes']['class']}";
744
745
			?>
746
			<li>
747
				<?php
748
				echo str_replace(
749
					'{{form_field}}',
750
					'<input ' . self::$instance->get_attributes( $option['field_attributes'] ) . '>',
751
					self::get_field_template( $option, $form, $args )
752
				);
753
				?>
754
			</li>
755
			<?php
756
		endforeach;
757
		echo '</ul>';
758
759
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
760
	}
761
762
	/**
763
	 * Render multi checkbox field.
764
	 *
765
	 * Note:
766
	 * You can reorder checklist if sortable with help of give_reorder_array function.
767
	 *
768
	 * @since  1.9
769
	 * @access public
770
	 *
771
	 * @param  array $field
772
	 * @param  array $form
773
	 * @param  array $args
774
	 *
775
	 * @return string
776
	 */
777
	public static function render_multi_checkbox_field( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
778
		// Field wrapper.
779
		$field['wrapper_type'] = 'p' === $field['wrapper_type']
780
			? 'fieldset'
781
			: $field['wrapper_type'];
782
783
		// Field value.
784
		$field['value'] = is_array( $field['value'] )
785
			? $field['value']
786
			: array();
787
788
		// Field type.
789
		$field['field_attributes']['type'] = 'checkbox';
790
791
		// Field name.
792
		$field['field_attributes']['name'] = self::get_field_name( $field ) . '[]';
793
794
		$field_wrapper = self::$instance->render_field_wrapper( $field );
795
		ob_start();
796
797
		$id_base = $field['field_attributes']['id'];
798
		unset( $field['field_attributes']['id'] );
799
800
		// Set ul attributes.
801
		$field['ul_attributes']['class'] = empty( $field['ul_attributes']['class'] )
802
			? 'give-checklist-fields'
803
			: "give-checklist-fields {$field['ul_attributes']['class']}";
804
		$field['ul_attributes']['data-give-sortable-list'] =absint( $field['sortable'] );
805
		$field['ul_attributes']['data-give-sortable-icon'] =absint( $field['sortable-icon'] );
806
807
		echo '<ul ' . self::get_attributes( $field['ul_attributes'] ) . '>';
808
809
		foreach ( $field['options'] as $key => $option ) :
810
			// Set basic values for field.
811
			$option = is_array( $option ) ? $option : array( 'label' => $option );
812
			$option['field_attributes']['id'] = "give-{$id_base}-{$key}";
813
			$option['field_attributes']['data-give-required'] = ( $field['required'] ? 1 : 0 );
814
			$option['field_attributes']['value']         = empty( $option['field_attributes']['value'] )
815
				? $key
816
				: $option['field_attributes']['value'];
817
818
			// Check if field checked or not.
819
			if (
820
				! empty( $field['value'] ) && in_array( $key, $field['value'] )
821
				|| (
822
					( ! empty( $field['repeater_default_template'] ) || ! empty( $field['repeater_template'] ) )
823
					&& is_array( $option )
824
					&& ! empty( $option['checked'] )
825
				)
826
			) {
827
				$option['field_attributes']['checked'] = 'checked';
828
			}
829
830
			// Add extra attribute per checkbox.
831
			$option['field_attributes'] = is_array( $option ) && ! empty( $option['field_attributes'] )
832
				? array_merge( $field['field_attributes'], $option['field_attributes'] )
833
				: $field['field_attributes'];
834
835
			// Add field specific class.
836
			$option['field_attributes']['class'] = trim( "{$option['field_attributes']['class']} give-{$key}" );
837
			?>
838
			<li>
839
				<label class="give-label" for="<?php echo $option['field_attributes']['id']; ?>">
840
					<input <?php echo self::$instance->get_attributes( $option['field_attributes'] ); ?>><?php echo( ! is_array( $option ) ? $option : ( isset( $option['label'] ) ? $option['label'] : '' ) ); ?>
841
				</label>
842
			</li>
843
			<?php
844
		endforeach;
845
846
		echo '</ul>';
847
848
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
849
	}
850
851
852
	/**
853
	 * Render group field
854
	 *
855
	 * @since 1.9
856
	 * @access public
857
	 *
858
	 * @param  array $fields
859
	 * @param  array $form
860
	 * @param  array $args
861
	 *
862
	 * @return string
863
	 */
864
	public static function render_group_field( $fields, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
865
		// Bailout.
866
		if ( ! isset( $fields['fields'] ) || empty( $fields['fields'] ) ) {
867
			return '';
868
		}
869
870
		$group_numbering       = isset( $fields['options']['group_numbering'] ) ? (int) $fields['options']['group_numbering'] : 0;
871
		$close_tabs            = isset( $fields['options']['close_tabs'] ) ? (int) $fields['options']['close_tabs'] : 0;
872
		$repeater_field_values = $fields['value'];
873
		$header_title          = isset( $fields['options']['header_title'] )
874
			? $fields['options']['header_title']
875
			: esc_attr__( 'Group', 'give' );
876
877
		$add_default_donation_field = false;
878
879
		// Check if level is not created or we have to add default level.
880
		if ( is_array( $repeater_field_values ) && ( $fields_count = count( $repeater_field_values ) ) ) {
881
			$repeater_field_values = array_values( $repeater_field_values );
882
		} else {
883
			$fields_count               = 1;
884
			$add_default_donation_field = true;
885
		}
886
887
		$field_wrapper = self::$instance->render_field_wrapper( $fields );
888
889
		ob_start();
890
		?>
891
		<div class="give-repeatable-field-section" id="<?php echo "{$fields['id']}_field"; ?>"
892
			 data-group-numbering="<?php echo $group_numbering; ?>" data-close-tabs="<?php echo $close_tabs; ?>">
893
			<?php if ( ! empty( $fields['label'] ) ) : ?>
894
				<p class="give-repeater-field-name"><?php echo $fields['label']; ?></p>
895
			<?php endif; ?>
896
897
			<?php if ( ! empty( $fields['description'] ) ) : ?>
898
				<p class="give-repeater-field-description"><?php echo $fields['description']; ?></p>
899
			<?php endif; ?>
900
901
			<table class="give-repeatable-fields-section-wrapper" cellspacing="0">
902
				<tbody class="container"<?php echo " data-rf-row-count=\"{$fields_count}\""; ?>>
903
					<!--Repeater field group template-->
904
					<tr class="give-template give-row">
905
						<td class="give-repeater-field-wrap give-column" colspan="2">
906
							<div class="give-row-head give-move">
907
								<button type="button" class="give-toggle-btn">
908
									<span class="give-toggle-indicator"></span>
909
								</button>
910
								<span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-</span>
911
								<h2>
912
									<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
913
								</h2>
914
							</div>
915
							<div class="give-row-body">
916
								<?php
917
								foreach ( $fields['fields'] as $field_id => $field ) :
918
									$field['id']     = ! empty( $field['id'] ) ? $field['id'] : $field_id;
919
									$field['repeat'] = true;
920
									$field['repeater_template'] = true;
921
922
									$field['repeater_field_name'] = self::get_repeater_field_name( $field, $fields );
923
									$field['field_attributes']['id'] = str_replace( array( '[', ']' ), array( '_', '', ), $field['repeater_field_name'] );
924
925
									echo self::render_tag( $field, $form );
926
								endforeach;
927
								?>
928
							</div>
929
						</td>
930
					</tr>
931
932
					<?php if ( ! empty( $repeater_field_values ) ) : ?>
933
						<!--Stored repeater field group-->
934
						<?php foreach ( $repeater_field_values as $index => $field_group ) : ?>
935
							<tr class="give-row">
936
								<td class="give-repeater-field-wrap give-column" colspan="2">
937
									<div class="give-row-head give-move">
938
										<button type="button" class="give-toggle-btn">
939
											<span class="give-toggle-indicator"></span>
940
										</button>
941
										<sapn class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
942
										</sapn>
943
										<h2>
944
											<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
945
										</h2>
946
									</div>
947
									<div class="give-row-body">
948
										<?php
949
										foreach ( $fields['fields'] as $field_id => $field ) :
950
											$field['id']     = ! empty( $field['id'] ) ? $field['id'] : $field_id;
951
											$field['repeat'] = true;
952
953
											$field['repeater_field_name'] = self::get_repeater_field_name( $field, $fields, $index );
954
											$field['value'] = self::get_repeater_field_value( $field, $field_group, $fields );
955
											$field['field_attributes']['id']  = str_replace( array( '[', ']' ), array( '_', '', ), $field['repeater_field_name'] );
956
957
											echo self::render_tag( $field, $form );
958
										endforeach;
959
										?>
960
									</div>
961
								</td>
962
							</tr>
963
						<?php endforeach; ?>
964
965
					<?php elseif ( $add_default_donation_field ) : ?>
966
						<!--Default repeater field group-->
967
						<tr class="give-row">
968
							<td class="give-repeater-field-wrap give-column" colspan="2">
969
								<div class="give-row-head give-move">
970
									<button type="button" class="give-toggle-btn">
971
										<span class="give-toggle-indicator"></span>
972
									</button>
973
									<sapn class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
974
									</sapn>
975
									<h2>
976
										<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
977
									</h2>
978
								</div>
979
								<div class="give-row-body">
980
									<?php
981
									foreach ( $fields['fields'] as $field_id => $field ) :
982
										$field['id']     = ! empty( $field['id'] ) ? $field['id'] : $field_id;
983
										$field['repeat'] = true;
984
										$field['repeater_default_template'] = true;
985
986
										$field['repeater_field_name'] = self::get_repeater_field_name( $field, $fields, 0 );
987
										$field['field_attributes']['id']  = str_replace( array( '[', ']' ), array( '_', '', ), $field['repeater_field_name'] );
988
989
										echo self::render_tag( $field, $form );
990
									endforeach;
991
									?>
992
								</div>
993
							</td>
994
						</tr>
995
					<?php endif; ?>
996
				</tbody>
997
				<tfoot>
998
					<tr>
999
						<?php
1000
						$add_row_btn_title = isset( $fields['options']['add_button'] )
1001
							? $add_row_btn_title = $fields['options']['add_button']
1002
							: esc_html__( 'Add Row', 'give' );
1003
						?>
1004
						<td colspan="2" class="give-add-repeater-field-section-row-wrap">
1005
							<button class="button button-primary give-add-repeater-field-section-row"><?php echo $add_row_btn_title; ?></button>
1006
						</td>
1007
					</tr>
1008
				</tfoot>
1009
			</table>
1010
		</div>
1011
		<?php
1012
1013
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
1014
	}
1015
1016
1017
	/**
1018
	 * Render field
1019
	 *
1020
	 * @since  1.9
1021
	 * @access private
1022
	 *
1023
	 * @param  array $field
1024
	 * @param  array $form
1025
	 * @param  array $args
1026
	 *
1027
	 * @return string
1028
	 */
1029
	public static function get_field_template( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1030
		/**
1031
		 * Field Html template.
1032
		 */
1033
		// Set before field html.
1034
		$field['before_field'] = self::is_callback( $field['before_field'] )
1035
			? self::render_callback( $field['before_field'] )
1036
			: $field['before_field'];
1037
1038
		// Set after field html.
1039
		$field['after_field'] = self::is_callback( $field['after_field'] )
1040
			? self::render_callback( $field['after_field'] )
1041
			: $field['after_field'];
1042
1043
		$field_html = "{$field['before_field']}{{form_field}}{$field['after_field']}";
1044
1045
1046
		/**
1047
		 * Field html with label.
1048
		 */
1049
		switch ( $field['label_position'] ) {
1050
			case 'after':
1051
				$field['before_field_label'] = empty( $field['before_field_label'] )
1052
					? $field_html
1053
					: $field_html . $field['before_field_label'];
1054
				break;
1055
1056
			case 'inside':
1057
				$field['label'] = empty( $field['label'] )
1058
					? $field_html
1059
					: $field_html . $field['label'];
1060
				break;
1061
1062
			//case 'before':
1063
			default:
1064
				$field['after_field_label'] = empty( $field['after_field_label'] )
1065
					? $field_html
1066
					: $field['after_field_label'] . $field_html;
1067
		}
1068
1069
1070
		$field_html = trim( self::$instance->render_field_label( $field ) );
1071
1072
		return $field_html;
1073
	}
1074
1075
1076
	/**
1077
	 * Render wrapper
1078
	 *
1079
	 * @since  1.9
1080
	 * @access private
1081
	 *
1082
	 * @param  array $field
1083
	 * @param  array $form
1084
	 * @param  array $args
1085
	 *
1086
	 * @return string
1087
	 */
1088
	public static function render_field_wrapper( $field, $form = null, $args = array() ) {
1089
		/**
1090
		 * Get field template.
1091
		 */
1092
		$field_html = self::get_field_template( $field, $form, $args );
1093
1094
		/**
1095
		 * Field with label html with wrapper.
1096
		 */
1097
		ob_start();
1098
1099
		// Set before wrapper html.
1100
		$field['before_field_wrapper'] = self::is_callback( $field['before_field_wrapper'] )
1101
			? self::render_callback( $field['before_field_wrapper'] )
1102
			: $field['before_field_wrapper'];
1103
1104
		echo $field['before_field_wrapper'];
1105
1106
		if ( $field['wrapper'] ) :
1107
1108
1109
			echo '<' . $field['wrapper_type'] . ' ' . self::$instance->get_attributes( $field['wrapper_attributes'] ) . '>';
1110
			echo $field_html;
1111
			echo "</{$field['wrapper_type']}>";
1112
1113
		else :
1114
			echo $field_html;
1115
		endif;
1116
1117
		// Set after wrapper html.
1118
		$field['after_field_wrapper'] = self::is_callback( $field['after_field_wrapper'] )
1119
			? self::render_callback( $field['after_field_wrapper'] )
1120
			: $field['after_field_wrapper'];
1121
1122
		echo $field['after_field_wrapper'];
1123
1124
		$field_html = ob_get_clean();
1125
1126
		return $field_html;
1127
	}
1128
1129
1130
	/**
1131
	 * Render label
1132
	 *
1133
	 * @since  1.9
1134
	 * @access private
1135
	 *
1136
	 * @param  array $field
1137
	 * @param  array $form
1138
	 * @param  array $args
1139
	 *
1140
	 * @return string
1141
	 */
1142
	public function render_field_label( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1143
		ob_start();
1144
		$label_type = ( 'fieldset' === $field['wrapper_type'] ? 'legend' : 'label' );
1145
1146
		$field['label_attributes']['for'] = $field['field_attributes']['id'];
1147
1148
		// Set before label html.
1149
		$field['before_field_label'] = self::is_callback( $field['before_field_label'] )
1150
			? self::render_callback( $field['before_field_label'] )
1151
			: $field['before_field_label'];
1152
		echo $field['before_field_label'];
1153
		?>
1154
		<?php if ( ! empty( $field['label'] ) ) : ?>
1155
1156
			<<?php echo $label_type; ?> <?php echo self::get_attributes( $field['label_attributes'] ); ?>>
1157
1158
			<?php echo $field['label']; ?>
1159
1160
			<?php if ( $field['required'] ) : ?>
1161
				<span class="give-required-indicator">*</span>
1162
			<?php endif; ?>
1163
1164
			<?php if ( $field['label_tooltip'] ) : ?>
1165
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php echo $field['label_tooltip'] ?>"></span>
1166
			<?php endif; ?>
1167
			</<?php echo $label_type; ?>>
1168
1169
		<?php endif; ?>
1170
		<?php
1171
		// Set after label html.
1172
		$field['after_field_label'] = self::is_callback( $field['after_field_label'] )
1173
			? self::render_callback( $field['after_field_label'] )
1174
			: $field['after_field_label'];
1175
		echo $field['after_field_label'];
1176
1177
		return ob_get_clean();
1178
	}
1179
1180
	/**
1181
	 * Get field attribute string from field arguments.
1182
	 *
1183
	 * @since  1.9
1184
	 * @access private
1185
	 *
1186
	 * @param array $attributes
1187
	 *
1188
	 * @return array|string
1189
	 */
1190
	private static function get_attributes( $attributes ) {
1191
		$field_attributes_val = '';
1192
1193
		if ( ! empty( $attributes ) ) {
1194
			foreach ( $attributes as $attribute_name => $attribute_val ) {
1195
				$field_attributes_val[] = "{$attribute_name}=\"{$attribute_val}\"";
1196
			}
1197
		}
1198
1199
		if ( ! empty( $field_attributes_val ) ) {
1200
			$field_attributes_val = implode( ' ', $field_attributes_val );
1201
		}
1202
1203
		return $field_attributes_val;
1204
	}
1205
1206
	/**
1207
	 * Set default values for fields
1208
	 *
1209
	 * @since  1.0
1210
	 * @access private
1211
	 *
1212
	 * @param array $field
1213
	 * @param array $form
1214
	 * @param array $args
1215
	 *
1216
	 * @return array
1217
	 */
1218
	public static function set_default_values( $field, $form = null, $args = array() ) {
1219
		/**
1220
		 * Filter the field before set default values.
1221
		 *
1222
		 * @since 1.9
1223
		 *
1224
		 * @param array $field
1225
		 * @param array $form
1226
		 */
1227
		$field = ! empty( $args['fire_filter'] )
1228
			? apply_filters( 'give_field_api_pre_set_default_values', $field, $form )
1229
			: $field;
1230
1231
		switch ( self::$instance->get_field_type( $field ) ) {
1232
			case 'block':
1233
				// Set default values for block.
1234
				$field = wp_parse_args( $field, self::$block_defaults );
1235
1236
				// Set block id.
1237
				$field['field_attributes']['id'] = ! isset( $field['field_attributes']['id'] )
1238
					? "give-{$field['id']}-field"
1239
					: $field['field_attributes']['id'];
1240
1241
				// Set wrapper class.
1242
				$field['block_attributes']['class'] = empty( $field['block_attributes']['class'] )
1243
					? "give-block-wrap js-give-block-wrap give-block-{$field['id']}"
1244
					: trim( "give-block-wrap js-give-block-wrap give-block-{$field['id']} {$field['block_attributes']['class']}" );
1245
1246
				foreach ( $field['fields'] as $key => $single_field ) {
1247
					$single_field['id']    = ! empty( $single_field['id'] )
1248
						? $single_field['id']
1249
						: $key;
1250
					$field['fields'][ $key ] = self::$instance->set_default_values( $single_field, $form, array( 'fire_filter' => false ) );
1251
				}
1252
1253
				break;
1254
1255
			case 'section':
1256
				// Set default values for block.
1257
				$field = wp_parse_args( $field, self::$section_defaults );
1258
1259
				// Set section id.
1260
				$field['field_attributes']['id'] = ! isset( $field['field_attributes']['id'] )
1261
					? "give-{$field['id']}-field"
1262
					: $field['field_attributes']['id'];
1263
1264
				// Set wrapper class.
1265
				$field['section_attributes']['class'] = empty( $field['section_attributes']['class'] )
1266
					? 'give-section-wrap'
1267
					: trim( "give-section-wrap {$field['section_attributes']['class']}" );
1268
1269
				foreach ( $field['fields'] as $key => $single_field ) {
1270
					$single_field['id']    = ! empty( $single_field['id'] )
1271
						? $single_field['id']
1272
						: $key;
1273
					$field['fields'][ $key ] = self::$instance->set_default_values( $single_field, $form, array( 'fire_filter' => false ) );
1274
				}
1275
1276
				break;
1277
1278
			default:
1279
				// Set default values for field or section.
1280
				$field = wp_parse_args( $field, self::$field_defaults );
1281
1282
				// Set default class for label.
1283
				$field['label_attributes']['class'] = empty( $field['label_attributes']['class'] )
1284
					? 'give-label'
1285
					: trim( "give-label {$field['label_attributes']['class']}" );
1286
1287
				// Set field id.
1288
				$field['field_attributes']['id'] = ! isset( $field['field_attributes']['id'] )
1289
					? "give-{$field['id']}-field"
1290
					: $field['field_attributes']['id'];
1291
1292
				// Set field class.
1293
				$field['field_attributes']['class'] = empty( $field['field_attributes']['class'] )
1294
					? "give-field js-give-field"
1295
					: trim( "give-field js-give-field {$field['field_attributes']['class']}" );
1296
1297
				// Set wrapper class.
1298
				$field['wrapper_attributes']['class'] = empty( $field['wrapper_attributes']['class'] )
1299
					? "give-field-wrap {$field['id']}_field"
1300
					: trim( "give-field-wrap {$field['id']}_field {$field['wrapper_attributes']['class']}"  );
1301
1302
				// if( 'group' === $field['type'] && ! empty( $field['fields'] ) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1303
				// 	foreach ( $field['fields'] as $key => $single_field ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1304
				// 		$single_field['id']    = ! empty( $single_field['id'] )
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1305
				// 			? $single_field['id']
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1306
				// 			: $key;
1307
				// 		$single_field['repeat'] = true;
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1308
				// 		$field['fields'][ $key ] = self::$instance->set_default_values( $single_field, $form, false );
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1309
				// 	}
1310
				// }
1311
1312
				/**
1313
				 * Filter the field values.
1314
				 *
1315
				 * @since 1.9
1316
				 *
1317
				 * @param array $field
1318
				 * @param array $form
1319
				 * @param array $args
1320
				 */
1321
				$field = apply_filters( 'give_field_api_set_values', $field, $form, $args );
1322
		}
1323
1324
		/**
1325
		 * Filter the field after set default values.
1326
		 *
1327
		 * @since 1.9
1328
		 *
1329
		 * @param array $field
1330
		 * @param array $form
1331
		 */
1332
		$field = ! empty( $args['fire_filter'] )
1333
			? apply_filters( 'give_field_api_post_set_default_values', $field, $form )
1334
			: $field;
1335
1336
		return $field;
1337
	}
1338
1339
1340
	/**
1341
	 * Set responsive fields.
1342
	 *
1343
	 * @since  1.9
1344
	 * @access private
1345
	 *
1346
	 * @param $form
1347
	 *
1348
	 * @return mixed
1349
	 */
1350
	private function set_responsive_field( &$form ) {
1351
1352
		foreach ( $form['fields'] as $key => $field ) {
1353
			switch ( true ) {
1354
				case array_key_exists( 'fields', $field ):
1355
					foreach ( $field['fields'] as $section_field_index => $section_field ) {
1356
						if ( ! self::$instance->is_sub_section( $section_field ) ) {
1357
							continue;
1358
						}
1359
1360
						$form['fields'][ $key ]['fields'][ $section_field_index ]['wrapper_attributes']['class'] = 'give-form-col';
1361
1362
						if ( array_key_exists( 'sub_section_end', $section_field ) ) {
1363
							$form['fields'][ $key ]['fields'][ $section_field_index ]['wrapper_attributes']['class'] = 'give-form-col give-form-col-end';
1364
1365
							// Clear float left for next field.
1366
							$fields_keys = array_keys( $form['fields'][ $key ]['fields'] );
1367
1368
							if ( $next_field_key = array_search( $section_field_index, $fields_keys ) ) {
1369
								if (
1370
									! isset( $fields_keys[ $next_field_key + 1 ] )
1371
									|| ! isset( $form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ] )
1372
								) {
1373
									continue;
1374
								}
1375
1376
								$next_field = $form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ];
1377
1378
								$next_field['wrapper_attributes']['class'] = isset( $next_field['wrapper_attributes']['class'] )
1379
									? $next_field['wrapper_attributes']['class'] . ' give-clearfix'
1380
									: 'give-clearfix';
1381
1382
								$form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ] = $next_field;
1383
							}
1384
						}
1385
					}
1386
1387
					break;
1388
1389
				default:
1390
					if ( ! self::$instance->is_sub_section( $field ) ) {
1391
						continue;
1392
					}
1393
1394
					$form['fields'][ $key ]['wrapper_attributes']['class'] = 'give-form-col';
1395
1396
					if ( array_key_exists( 'sub_section_end', $field ) ) {
1397
						$form['fields'][ $key ]['wrapper_attributes']['class'] = 'give-form-col give-form-col-end';
1398
1399
						// Clear float left for next field.
1400
						$fields_keys = array_keys( $form['fields'] );
1401
1402
						if ( $next_field_key = array_search( $key, $fields_keys ) ) {
1403
							$form['fields'][ $fields_keys[ $next_field_key + 1 ] ]['wrapper_attributes']['class'] = 'give-clearfix';
1404
						}
1405
					}
1406
			}
1407
		}
1408
	}
1409
1410
1411
	/**
1412
	 * Check if current field is part of sub section or not.
1413
	 *
1414
	 * @since  1.9
1415
	 * @access private
1416
	 *
1417
	 * @param $field
1418
	 *
1419
	 * @return bool
1420
	 */
1421
	private function is_sub_section( $field ) {
1422
		$is_sub_section = false;
1423
		if ( array_key_exists( 'sub_section_start', $field ) || array_key_exists( 'sub_section_end', $field ) ) {
1424
			$is_sub_section = true;
1425
		}
1426
1427
		return $is_sub_section;
1428
	}
1429
1430
1431
	/**
1432
	 * Get field type.
1433
	 *
1434
	 * @since  1.9
1435
	 * @access private
1436
	 *
1437
	 * @param  array $field
1438
	 * @param  array $form
1439
	 * @param  array $args
1440
	 *
1441
	 * @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...
1442
	 */
1443
	public static function get_field_type( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1444
		$field_type = 'field';
1445
1446
		if (
1447
			isset( $field['type'] )
1448
			&& 'block' === $field['type']
1449
		) {
1450
			$field_type = 'block';
1451
1452
		} elseif (
1453
			array_key_exists( 'fields', $field )
1454
			&& 'section' === $field['type']
1455
		) {
1456
			$field_type = 'section';
1457
1458
		}
1459
1460
		return $field_type;
1461
	}
1462
1463
	/**
1464
	 * Get field name.
1465
	 *
1466
	 * @since  1.9
1467
	 *
1468
	 * @param  array $field
1469
	 * @param  array $form
1470
	 * @param  array $args
1471
	 *
1472
	 * @return string
1473
	 */
1474
	public static function get_field_name( $field, $form = null, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1475
		$field_name = esc_attr( empty( $field['repeat'] ) ? $field['id'] : $field['repeater_field_name'] );
1476
1477
		/**
1478
		 * Filter the field name.
1479
		 *
1480
		 * @since 1.8
1481
		 *
1482
		 * @param string $field_name
1483
		 */
1484
		$field_name = apply_filters( 'give_get_field_name', $field_name, $field );
1485
1486
		return $field_name;
1487
	}
1488
1489
	/**
1490
	 * Get repeater field id.
1491
	 *
1492
	 * @since  1.9
1493
	 *
1494
	 * @param array    $field
1495
	 * @param array    $fields
1496
	 * @param int|bool $default
1497
	 *
1498
	 * @return string
1499
	 */
1500
	public static function get_repeater_field_name( $field, $fields , $default = false ) {
1501
		$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1502
1503
		// Get field id.
1504
		$field_id = "{$fields['id']}[{$row_placeholder}][{$field['id']}]";
1505
1506
		/**
1507
		 * Filter the specific repeater field id
1508
		 *
1509
		 * @since 1.8
1510
		 *
1511
		 * @param string $field_id
1512
		 */
1513
		$field_id = apply_filters( "give_get_repeater_field_{$field['id']}_name", $field_id, $field, $fields, $default );
1514
1515
		/**
1516
		 * Filter the repeater field id
1517
		 *
1518
		 * @since 1.8
1519
		 *
1520
		 * @param string $field_id
1521
		 */
1522
		$field_id = apply_filters( 'give_get_repeater_field_name', $field_id, $field, $fields, $default );
1523
1524
		return $field_id;
1525
	}
1526
1527
1528
	/**
1529
	 * Get repeater field value.
1530
	 *
1531
	 * @since 1.9
1532
	 * @access public
1533
	 *
1534
	 * @param array $field
1535
	 * @param array $field_value_group
1536
	 * @param array $fields
1537
	 *
1538
	 * @return mixed
1539
	 */
1540
	public static function get_repeater_field_value( $field, $field_value_group, $fields ) {
1541
		$field_value = ( isset( $field_value_group[ $field['id'] ] ) ? $field_value_group[ $field['id'] ] : '' );
1542
1543
		/**
1544
		 * Filter the specific repeater field value
1545
		 *
1546
		 * @since 1.8
1547
		 *
1548
		 * @param string $field_id
1549
		 */
1550
		$field_value = apply_filters( "give_get_repeater_field_{$field['id']}_value", $field_value, $field, $field_value_group, $fields );
1551
1552
		/**
1553
		 * Filter the repeater field value
1554
		 *
1555
		 * @since 1.8
1556
		 *
1557
		 * @param string $field_id
1558
		 */
1559
		$field_value = apply_filters( 'give_get_repeater_field_value', $field_value, $field, $field_value_group, $fields );
1560
1561
		return $field_value;
1562
	}
1563
1564
1565
	/**
1566
	 * Check if string or array is callback or not.
1567
	 *
1568
	 * @since  1.9
1569
	 * @access public
1570
	 *
1571
	 * @param $callback
1572
	 *
1573
	 * @return bool
1574
	 */
1575
	public static function is_callback( $callback ) {
1576
		$is_callback = false;
1577
1578
		// Process callback to get field html.
1579
		if ( is_string( $callback ) && function_exists( "$callback" ) ) {
1580
			$is_callback = true;
1581
		} elseif ( is_array( $callback ) && method_exists( $callback[0], "$callback[1]" ) ) {
1582
			$is_callback = true;
1583
		}
1584
1585
		return $is_callback;
1586
	}
1587
}
1588
1589
// @todo implement required and aria-required for all form fields. required should be custom tag because we want custom validation for form instead html5.
1590
// @todo develop field validation feature