Completed
Push — issues/1038 ( 8f0d36...71fe2b )
by Ravinder
19:11
created

Give_Fields_API::render_email_field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 3
rs 10
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
99
	/**
100
	 * The defaults for all blocks.
101
	 *
102
	 * @since  1.9
103
	 * @access static
104
	 */
105
	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...
106
		'type'             => 'block',
107
		'label'            => '',
108
		'id'             => '',
109
		'block_attributes' => array(),
110
111
		// Manually render section.
112
		'callback'         => '',
113
	);
114
115
116
	private function __construct() {
117
	}
118
119
120
	/**
121
	 * Get instance.
122
	 *
123
	 * @return static
124
	 */
125
	public static function get_instance() {
126
		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...
127
			self::$instance = new static();
128
		}
129
130
		return self::$instance;
131
	}
132
133
	/**
134
	 * Initialize this module
135
	 *
136
	 * @since  1.9
137
	 * @access static
138
	 */
139
	public function init() {
140
		add_filter( 'give_form_api_render_form_tags', array( $this, 'render_tags' ), 10, 2 );
141
	}
142
143
144
	/**
145
	 * Render callback.
146
	 *
147
	 * @since 1.9
148
	 * @access public
149
	 * @param       $callback
150
	 * @param array $field
151
	 * @param null  $form
152
	 *
153
	 * @return string
154
	 */
155
	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...
156
		$field_html = '';
157
158
		if( empty( $callback ) || ! self::is_callback( $callback ) ) {
159
			return $field_html;
160
		}
161
162
		// Process callback to get field html.
163
		if ( is_string( $callback ) && function_exists( "$callback" ) ) {
164
			$field_html = $callback( $field );
165
		} elseif ( is_array( $callback ) && method_exists( $callback[0], "$callback[1]" ) ) {
166
			$field_html = $callback[0]->$callback[1]( $field );
167
		}
168
169
		return $field_html;
170
	}
171
172
173
	/**
174
	 * Render custom field.
175
	 *
176
	 * @since  1.0
177
	 * @access private
178
	 *
179
	 * @param array $field
180
	 * @param array $form
181
	 *
182
	 * @return string
183
	 */
184
	private function render_custom_field( $field, $form = null ) {
185
		$field_html = '';
186
187
		if ( ! empty( $field['callback'] ) ) {
188
			$field = self::$instance->set_default_values( $field, $form );
189
			
190
			$callback = $field['callback'];
191
			$field_html = self::render_callback( $callback, $field, $form );
192
		}
193
194
		return $field_html;
195
	}
196
197
198
	/**
199
	 * Render `{{form_fields}}` tag.
200
	 *
201
	 * @since  1.9
202
	 * @access private
203
	 *
204
	 * @param  string $form_html
205
	 * @param  array  $form
206
	 *
207
	 * @return string
208
	 */
209
	public function render_tags( $form_html, $form ) {
210
		// Bailout: If form does not contain any field.
211
		if ( empty( $form['fields'] ) ) {
212
			str_replace( '{{form_fields}}', '', $form_html );
213
214
			return $form_html;
215
		}
216
217
		$fields_html = '';
218
219
		// Set responsive fields.
220
		self::$instance->set_responsive_field( $form );
221
222
		// Render fields.
223
		foreach ( $form['fields'] as $key => $field ) {
224
			// Set default value.
225
			$field['id'] = empty( $field['id'] ) ? $key : $field['id'];
226
227
			// Render custom form with callback.
228
			if ( $field_html = self::$instance->render_custom_field( $field, $form ) ) {
229
				$fields_html .= $field_html;
230
			}
231
232
			switch ( true ) {
233
				// Block.
234
				case ( 'block' === self::get_field_type( $field ) ):
235
					$fields_html .= self::$instance->render_block( $field, $form );
236
					break;
237
238
				// Section.
239
				case ( 'section' === self::get_field_type( $field ) ):
240
					$fields_html .= self::$instance->render_section( $field, $form );
241
					break;
242
243
				// Field
244
				default:
245
					$fields_html .= self::render_tag( $field, $form );
246
			}
247
		}
248
249
		$form_html = str_replace( '{{form_fields}}', $fields_html, $form_html );
250
251
		return $form_html;
252
	}
253
254
255
	/**
256
	 * Render section.
257
	 *
258
	 * @since  1.9
259
	 * @access public
260
	 *
261
	 * @param array $section
262
	 * @param array $form
263
	 * @param array $args Helper argument to render section.
264
	 *
265
	 * @return string
266
	 */
267
	public static function render_section( $section, $form = null, $args = array() ) {
268
		// Set default values if necessary.
269
		if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) {
270
			$section = self::$instance->set_default_values( $section, $form );
271
		}
272
273
		ob_start();
274
275
		// Set before section html.
276
		$section['before_field_wrapper'] = self::is_callback( $section['before_field_wrapper'] )
277
			? self::render_callback( $section['before_field_wrapper'] )
278
			: $section['before_field_wrapper'];
279
280
		// Set after section html.
281
		$section['after_field_wrapper'] = self::is_callback( $section['after_field_wrapper'] )
282
			? self::render_callback( $section['after_field_wrapper'] )
283
			: $section['after_field_wrapper'];
284
285
		// Set before section legend html.
286
		$section['before_field_lebel'] = self::is_callback( $section['before_field_lebel'] )
287
			? self::render_callback( $section['before_field_lebel'] )
288
			: $section['before_field_lebel'];
289
290
		// Set after section legend html.
291
		$section['after_field_legend'] = self::is_callback( $section['after_field_legend'] )
292
			? self::render_callback( $section['after_field_legend'] )
293
			: $section['after_field_legend'];
294
295
		echo $section['before_field_wrapper'];
296
		?>
297
		<fieldset <?php echo self::$instance->get_attributes( $section['section_attributes'] ); ?>>
298
			<?php
299
			// Legend.
300
			if ( ! empty( $section['label'] ) ) {
301
				echo $section['before_field_label'];
302
					echo "<legend ". self::get_attributes( $section['label_attributes'] ).">{$section['label']}</legend>";
303
				echo $section['after_field_label'];
304
			};
305
306
			// Fields.
307
			foreach ( $section['fields'] as $key => $field ) {
308
				echo self::render_tag( $field, $form, array( 'set_default' => false ) );
309
			}
310
			?>
311
		</fieldset>
312
		<?php
313
		echo $section['after_field_wrapper'];
314
315
		$section_html = ob_get_clean();
316
317
		/**
318
		 * Filter the specific section html.
319
		 *
320
		 * @since 1.9
321
		 *
322
		 * @param string $section_html
323
		 * @param array  $field
324
		 * @param array  $form
325
		 */
326
		$section_html = apply_filters(
327
			"give_field_api_render_{$section['type']}_section",
328
			$section_html,
329
			$section,
330
			$form,
331
			$args
332
		);
333
334
		/**
335
		 * Filter the section html.
336
		 *
337
		 * @since 1.9
338
		 *
339
		 * @param string $section_html
340
		 * @param array  $field
341
		 * @param array  $form
342
		 */
343
		$section_html = apply_filters(
344
			"give_field_api_render_section",
345
			$section_html,
346
			$section,
347
			$form,
348
			$args
349
		);
350
		return $section_html;
351
	}
352
353
354
	/**
355
	 * Render block.
356
	 *
357
	 * @since  1.9
358
	 * @access public
359
	 *
360
	 * @param array $block
361
	 * @param array $form
362
	 * @param array $args Helper argument to render section.
363
	 *
364
	 * @return string
365
	 */
366
	public static function render_block( $block, $form = null, $args = array() ) {
367
		// Set default values if necessary.
368
		if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) {
369
			$block = self::$instance->set_default_values( $block, $form );
370
		}
371
372
		ob_start();
373
		?>
374
		<div <?php echo self::$instance->get_attributes( $block['block_attributes'] ); ?>>
375
			<?php
376
			// Fields.
377
			foreach ( $block['fields'] as $key => $field ) {
378
				echo array_key_exists( 'fields', $field )
379
					? self::render_section( $field, $form, array( 'set_default' => false ) )
380
					: self::render_tag( $field, $form, array( 'set_default' => false ) );
381
			}
382
			?>
383
		</div>
384
		<?php
385
		$block_html = ob_get_clean();
386
387
		/**
388
		 * Filter the specific block html.
389
		 *
390
		 * @since 1.9
391
		 *
392
		 * @param string $block_html
393
		 * @param array  $field
394
		 * @param array  $form
395
		 */
396
		$block_html = apply_filters(
397
			"give_field_api_render_{$block['type']}_block",
398
			$block_html,
399
			$block,
400
			$form,
401
			$args
402
		);
403
404
		/**
405
		 * Filter the block html.
406
		 *
407
		 * @since 1.9
408
		 *
409
		 * @param string $block_html
410
		 * @param array  $field
411
		 * @param array  $form
412
		 */
413
		$block_html = apply_filters(
414
			"give_field_api_render_block",
415
			$block_html,
416
			$block,
417
			$form,
418
			$args
419
		);
420
421
		return $block_html;
422
	}
423
424
	/**
425
	 * Render tag
426
	 *
427
	 * @since   1.9
428
	 * @access  public
429
	 *
430
	 * @param array $field
431
	 * @param array $form
432
	 * @param array $args Helper argument to render section.
433
	 *
434
	 * @return string
435
	 */
436
	public static function render_tag( $field, $form = null, $args = array() ) {
437
		// Enqueue scripts.
438
		Give_Form_API::enqueue_scripts();
439
440
		if( ! empty( $field['sortable'] ) ) {
441
			wp_enqueue_script('jquery-ui-sortable');
442
		}
443
444
		// Set default values if necessary.
445
		if ( ! isset( $args['set_default'] ) || (bool) $args['set_default'] ) {
446
			$field = self::$instance->set_default_values( $field, $form );
447
		}
448
449
		$field_html     = '';
450
		$functions_name = "render_{$field['type']}_field";
451
452
		if ( 'section' === self::$instance->get_field_type( $field ) ) {
453
			$field_html = self::$instance->render_section( $field, $form, array( 'set_default' => false ) );
454
455
		} elseif ( method_exists( self::$instance, $functions_name ) ) {
456
			$field_html = self::$instance->{$functions_name}( $field, $form, $args );
457
458
		}
459
460
		/**
461
		 * Filter the custom field type html.
462
		 * Developer can use this hook to render custom field type.
463
		 *
464
		 * @since 1.9
465
		 *
466
		 * @param string $field_html
467
		 * @param array  $field
468
		 * @param array  $form
469
		 */
470
		$field_html = apply_filters(
471
			"give_field_api_render_{$field['type']}_field",
472
			$field_html,
473
			$field,
474
			$form,
475
			$args
476
		);
477
478
		/**
479
		 * Filter the all field type html.
480
		 *
481
		 * @since 1.9
482
		 *
483
		 * @param string $field_html
484
		 * @param array  $field
485
		 * @param array  $form
486
		 */
487
		$field_html = apply_filters(
488
			"give_field_api_render_field",
489
			$field_html,
490
			$field,
491
			$form,
492
			$args
493
		);
494
495
		return $field_html;
496
	}
497
498
499
	/**
500
	 * Render text field.
501
	 *
502
	 * @since  1.9
503
	 * @access public
504
	 *
505
	 * @param  array $field
506
	 * @param  array $form
507
	 * @param  array $args
508
	 *
509
	 * @return string
510
	 */
511
	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...
512
		$field_wrapper = self::$instance->render_field_wrapper( $field );
513
		$field['field_attributes']['name'] = self::get_field_name( $field );
514
		$field['field_attributes']['type'] = $field['type'];
515
516
		if( ! empty( $field['required'] ) ) {
517
			$field['field_attributes']['required'] = 'required';
518
			$field['field_attributes']['aria-required'] = 'true';
519
		}
520
521
		ob_start();
522
523
		echo '<input ' . self::$instance->get_attributes( $field['field_attributes'] ) . '>';
524
525
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
526
	}
527
528
	/**
529
	 * Render submit field.
530
	 *
531
	 * @since  1.9
532
	 * @access public
533
	 *
534
	 * @param  array $field
535
	 * @param  array $form
536
	 * @param  array $args
537
	 *
538
	 * @return string
539
	 */
540
	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...
541
		return self::$instance->render_text_field( $field );
542
	}
543
544
	/**
545
	 * Render checkbox field.
546
	 *
547
	 * @since  1.9
548
	 * @access public
549
	 *
550
	 * @param  array $field
551
	 * @param  array $form
552
	 * @param  array $args
553
	 *
554
	 * @return string
555
	 */
556
	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...
557
		return self::$instance->render_text_field( $field );
558
	}
559
560
	/**
561
	 * Render email field.
562
	 *
563
	 * @since  1.9
564
	 * @access public
565
	 *
566
	 * @param  array $field
567
	 * @param  array $form
568
	 * @param  array $args
569
	 *
570
	 * @return string
571
	 */
572
	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...
573
		return self::$instance->render_text_field( $field );
574
	}
575
576
	/**
577
	 * Render number field.
578
	 *
579
	 * @since  1.9
580
	 * @access public
581
	 *
582
	 * @param  array $field
583
	 * @param  array $form
584
	 * @param  array $args
585
	 *
586
	 * @return string
587
	 */
588
	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...
589
		return self::$instance->render_text_field( $field );
590
	}
591
592
	/**
593
	 * Render password field.
594
	 *
595
	 * @since  1.9
596
	 * @access public
597
	 *
598
	 * @param  array $field
599
	 * @param  array $form
600
	 * @param  array $args
601
	 *
602
	 * @return string
603
	 */
604
	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...
605
		return self::$instance->render_text_field( $field );
606
	}
607
608
	/**
609
	 * Render button field.
610
	 *
611
	 * @since  1.9
612
	 * @access public
613
	 *
614
	 * @param  array $field
615
	 * @param  array $form
616
	 * @param  array $args
617
	 *
618
	 * @return string
619
	 */
620
	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...
621
		return self::$instance->render_text_field( $field );
622
	}
623
624
	/**
625
	 * Render hidden field.
626
	 *
627
	 * @since  1.9
628
	 * @access public
629
	 *
630
	 * @param  array $field
631
	 * @param  array $form
632
	 * @param  array $args
633
	 *
634
	 * @return string
635
	 */
636
	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...
637
		$field['wrapper'] = false;
638
639
		return self::$instance->render_text_field( $field );
640
	}
641
642
	/**
643
	 * Render textarea field.
644
	 *
645
	 * @since  1.9
646
	 * @access public
647
	 *
648
	 * @param  array $field
649
	 * @param  array $form
650
	 * @param  array $args
651
	 *
652
	 * @return string
653
	 */
654
	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...
655
		$field_wrapper = self::$instance->render_field_wrapper( $field );
656
		ob_start();
657
		?>
658
		<textarea
659
				name="<?php echo self::get_field_name( $field ); ?>"
660
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
661
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
662
		><?php echo $field ['value']; ?></textarea>
663
664
665
		<?php
666
667
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
668
	}
669
670
	/**
671
	 * Render select field.
672
	 *
673
	 * @since  1.9
674
	 * @access public
675
	 *
676
	 * @param  array $field
677
	 * @param  array $form
678
	 * @param  array $args
679
	 *
680
	 * @return string
681
	 */
682
	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...
683
		$field_wrapper = self::$instance->render_field_wrapper( $field );
684
		ob_start();
685
686
		$options_html = '';
687
		foreach ( $field['options'] as $key => $option ) {
688
			$options_html .= '<option '. self::get_attributes( $option['field_attributes'] ). '>' . $option['label'] . '</option>';
689
		}
690
		?>
691
692
		<select
693
				name="<?php echo self::get_field_name( $field ); ?>"
694
			<?php echo( $field['required'] ? 'required=""' : '' ); ?>
695
			<?php echo self::$instance->get_attributes( $field['field_attributes'] ); ?>
696
		><?php echo $options_html; ?></select>
697
		<?php
698
699
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
700
	}
701
702
	/**
703
	 * Render multi select field.
704
	 *
705
	 * @since  1.9
706
	 * @access public
707
	 *
708
	 * @param  array $field
709
	 * @param  array $form
710
	 * @param  array $args
711
	 *
712
	 * @return string
713
	 */
714
	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...
715
		$field['field_attributes'] = array_merge( $field['field_attributes'], array( 'multiple' => 'multiple' ) );
716
		$field['id']             = "{$field['id']}[]";
717
718
		return self::$instance->render_select_field( $field );
719
	}
720
721
	/**
722
	 * Render radio field.
723
	 *
724
	 * @since  1.9
725
	 * @access public
726
	 *
727
	 * @param  array $field
728
	 * @param  array $form
729
	 * @param  array $args
730
	 *
731
	 * @return string
732
	 */
733
	public static function render_radio_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...
734
		$field['wrapper_type'] = 'p' === $field['wrapper_type']
735
			? 'fieldset'
736
			: $field['wrapper_type'];
737
738
739
		$field_wrapper = self::$instance->render_field_wrapper( $field );
740
		ob_start();
741
742
		$id_base = $field['field_attributes']['id'];
743
		unset( $field['field_attributes']['id'] );
744
745
		$field['ul_attributes']['class'] = empty( $field['ul_attributes']['class'] )
746
			? 'give-radio-fields'
747
			: "give-radio-fields {$field['ul_attributes']['class']}";
748
749
		echo '<ul '. self::get_attributes( $field['ul_attributes'] ) .'>';
750
		foreach ( $field['options'] as $key => $option ) :
751
			$option['field_attributes']['type'] = $field['type'];
752
			$option['field_attributes']['name'] = self::get_field_name( $field );
753
			$option['field_attributes']['id'] = "{$id_base}-{$key}";
754
755
			$option['label_attributes']['class'] = empty( $option['label_attributes']['class'] )
756
				? 'give-label'
757
				: "give-label {$option['label_attributes']['class']}";
758
			$option['label_attributes']['for'] = $option['field_attributes']['id'];
759
			?>
760
			<li>
761
				<label <?php echo self::get_attributes( $option['label_attributes'] ); ?>>
762
					<input <?php echo self::$instance->get_attributes( $option['field_attributes'] ); ?>><?php echo $option['label']; ?>
763
				</label>
764
			</li>
765
			<?php
766
		endforeach;
767
		echo '</ul>';
768
769
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
770
	}
771
772
	/**
773
	 * Render multi checkbox field.
774
	 *
775
	 * Note:
776
	 * You can reorder checklist if sortable with help of give_reorder_array function.
777
	 *
778
	 * @since  1.9
779
	 * @access public
780
	 *
781
	 * @param  array $field
782
	 * @param  array $form
783
	 * @param  array $args
784
	 *
785
	 * @return string
786
	 */
787
	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...
788
		// Field wrapper.
789
		$field['wrapper_type'] = 'p' === $field['wrapper_type']
790
			? 'fieldset'
791
			: $field['wrapper_type'];
792
793
		// Field value.
794
		$field['value'] = is_array( $field['value'] )
795
			? $field['value']
796
			: array();
797
798
		// Field type.
799
		$field['field_attributes']['type'] = 'checkbox';
800
801
		// Field name.
802
		$field['field_attributes']['name'] = self::get_field_name( $field ) . '[]';
803
804
		$field_wrapper = self::$instance->render_field_wrapper( $field );
805
		ob_start();
806
807
		$id_base = $field['field_attributes']['id'];
808
		unset( $field['field_attributes']['id'] );
809
810
		// Set ul attributes.
811
		$field['ul_attributes']['class'] = empty( $field['ul_attributes']['class'] )
812
			? 'give-checklist-fields'
813
			: "give-checklist-fields {$field['ul_attributes']['class']}";
814
		$field['ul_attributes']['data-give-sortable-list'] =absint( $field['sortable'] );
815
		$field['ul_attributes']['data-give-sortable-icon'] =absint( $field['sortable-icon'] );
816
817
		echo '<ul ' . self::get_attributes( $field['ul_attributes'] ) . '>';
818
819
		foreach ( $field['options'] as $key => $option ) :
820
			// Set basic values for field.
821
			$option = is_array( $option ) ? $option : array( 'label' => $option );
822
			$option['field_attributes']['id'] = "give-{$id_base}-{$key}";
823
			$option['field_attributes']['data-give-required'] = ( $field['required'] ? 1 : 0 );
824
			$option['field_attributes']['value']         = empty( $option['field_attributes']['value'] )
825
				? $key
826
				: $option['field_attributes']['value'];
827
828
			// Check if field checked or not.
829
			if (
830
				! empty( $field['value'] ) && in_array( $key, $field['value'] )
831
				|| (
832
					( ! empty( $field['repeater_default_template'] ) || ! empty( $field['repeater_template'] ) )
833
					&& is_array( $option )
834
					&& ! empty( $option['checked'] )
835
				)
836
			) {
837
				$option['field_attributes']['checked'] = 'checked';
838
			}
839
840
			// Add extra attribute per checkbox.
841
			$option['field_attributes'] = is_array( $option ) && ! empty( $option['field_attributes'] )
842
				? array_merge( $field['field_attributes'], $option['field_attributes'] )
843
				: $field['field_attributes'];
844
845
			// Add field specific class.
846
			$option['field_attributes']['class'] = trim( "{$option['field_attributes']['class']} give-{$key}" );
847
			?>
848
			<li>
849
				<label class="give-label" for="<?php echo $option['field_attributes']['id']; ?>">
850
					<input <?php echo self::$instance->get_attributes( $option['field_attributes'] ); ?>><?php echo( ! is_array( $option ) ? $option : ( isset( $option['label'] ) ? $option['label'] : '' ) ); ?>
851
				</label>
852
			</li>
853
			<?php
854
		endforeach;
855
856
		echo '</ul>';
857
858
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
859
	}
860
861
862
	/**
863
	 * Render group field
864
	 *
865
	 * @since 1.9
866
	 * @access public
867
	 *
868
	 * @param  array $fields
869
	 * @param  array $form
870
	 * @param  array $args
871
	 *
872
	 * @return string
873
	 */
874
	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...
875
		// Bailout.
876
		if ( ! isset( $fields['fields'] ) || empty( $fields['fields'] ) ) {
877
			return '';
878
		}
879
880
		$group_numbering       = isset( $fields['options']['group_numbering'] ) ? (int) $fields['options']['group_numbering'] : 0;
881
		$close_tabs            = isset( $fields['options']['close_tabs'] ) ? (int) $fields['options']['close_tabs'] : 0;
882
		$repeater_field_values = $fields['value'];
883
		$header_title          = isset( $fields['options']['header_title'] )
884
			? $fields['options']['header_title']
885
			: esc_attr__( 'Group', 'give' );
886
887
		$add_default_donation_field = false;
888
889
		// Check if level is not created or we have to add default level.
890
		if ( is_array( $repeater_field_values ) && ( $fields_count = count( $repeater_field_values ) ) ) {
891
			$repeater_field_values = array_values( $repeater_field_values );
892
		} else {
893
			$fields_count               = 1;
894
			$add_default_donation_field = true;
895
		}
896
897
		$field_wrapper = self::$instance->render_field_wrapper( $fields );
898
899
		ob_start();
900
		?>
901
		<div class="give-repeatable-field-section" id="<?php echo "{$fields['id']}_field"; ?>"
902
			 data-group-numbering="<?php echo $group_numbering; ?>" data-close-tabs="<?php echo $close_tabs; ?>">
903
			<?php if ( ! empty( $fields['label'] ) ) : ?>
904
				<p class="give-repeater-field-name"><?php echo $fields['label']; ?></p>
905
			<?php endif; ?>
906
907
			<?php if ( ! empty( $fields['description'] ) ) : ?>
908
				<p class="give-repeater-field-description"><?php echo $fields['description']; ?></p>
909
			<?php endif; ?>
910
911
			<table class="give-repeatable-fields-section-wrapper" cellspacing="0">
912
				<tbody class="container"<?php echo " data-rf-row-count=\"{$fields_count}\""; ?>>
913
					<!--Repeater field group template-->
914
					<tr class="give-template give-row">
915
						<td class="give-repeater-field-wrap give-column" colspan="2">
916
							<div class="give-row-head give-move">
917
								<button type="button" class="give-toggle-btn">
918
									<span class="give-toggle-indicator"></span>
919
								</button>
920
								<span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-</span>
921
								<h2>
922
									<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
923
								</h2>
924
							</div>
925
							<div class="give-row-body">
926
								<?php
927
								foreach ( $fields['fields'] as $field_id => $field ) :
928
									$field['id']     = ! empty( $field['id'] ) ? $field['id'] : $field_id;
929
									$field['repeat'] = true;
930
									$field['repeater_template'] = true;
931
932
									$field['repeater_field_name'] = self::get_repeater_field_name( $field, $fields );
933
									$field['field_attributes']['id'] = str_replace( array( '[', ']' ), array( '_', '', ), $field['repeater_field_name'] );
934
935
									echo self::render_tag( $field, $form );
936
								endforeach;
937
								?>
938
							</div>
939
						</td>
940
					</tr>
941
942
					<?php if ( ! empty( $repeater_field_values ) ) : ?>
943
						<!--Stored repeater field group-->
944
						<?php foreach ( $repeater_field_values as $index => $field_group ) : ?>
945
							<tr class="give-row">
946
								<td class="give-repeater-field-wrap give-column" colspan="2">
947
									<div class="give-row-head give-move">
948
										<button type="button" class="give-toggle-btn">
949
											<span class="give-toggle-indicator"></span>
950
										</button>
951
										<sapn class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
952
										</sapn>
953
										<h2>
954
											<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
955
										</h2>
956
									</div>
957
									<div class="give-row-body">
958
										<?php
959
										foreach ( $fields['fields'] as $field_id => $field ) :
960
											$field['id']     = ! empty( $field['id'] ) ? $field['id'] : $field_id;
961
											$field['repeat'] = true;
962
963
											$field['repeater_field_name'] = self::get_repeater_field_name( $field, $fields, $index );
964
											$field['value'] = self::get_repeater_field_value( $field, $field_group, $fields );
965
											$field['field_attributes']['id']  = str_replace( array( '[', ']' ), array( '_', '', ), $field['repeater_field_name'] );
966
967
											echo self::render_tag( $field, $form );
968
										endforeach;
969
										?>
970
									</div>
971
								</td>
972
							</tr>
973
						<?php endforeach; ?>
974
975
					<?php elseif ( $add_default_donation_field ) : ?>
976
						<!--Default repeater field group-->
977
						<tr class="give-row">
978
							<td class="give-repeater-field-wrap give-column" colspan="2">
979
								<div class="give-row-head give-move">
980
									<button type="button" class="give-toggle-btn">
981
										<span class="give-toggle-indicator"></span>
982
									</button>
983
									<sapn class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
984
									</sapn>
985
									<h2>
986
										<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
987
									</h2>
988
								</div>
989
								<div class="give-row-body">
990
									<?php
991
									foreach ( $fields['fields'] as $field_id => $field ) :
992
										$field['id']     = ! empty( $field['id'] ) ? $field['id'] : $field_id;
993
										$field['repeat'] = true;
994
										$field['repeater_default_template'] = true;
995
996
										$field['repeater_field_name'] = self::get_repeater_field_name( $field, $fields, 0 );
997
										$field['field_attributes']['id']  = str_replace( array( '[', ']' ), array( '_', '', ), $field['repeater_field_name'] );
998
999
										echo self::render_tag( $field, $form );
1000
									endforeach;
1001
									?>
1002
								</div>
1003
							</td>
1004
						</tr>
1005
					<?php endif; ?>
1006
				</tbody>
1007
				<tfoot>
1008
					<tr>
1009
						<?php
1010
						$add_row_btn_title = isset( $fields['options']['add_button'] )
1011
							? $add_row_btn_title = $fields['options']['add_button']
1012
							: esc_html__( 'Add Row', 'give' );
1013
						?>
1014
						<td colspan="2" class="give-add-repeater-field-section-row-wrap">
1015
							<button class="button button-primary give-add-repeater-field-section-row"><?php echo $add_row_btn_title; ?></button>
1016
						</td>
1017
					</tr>
1018
				</tfoot>
1019
			</table>
1020
		</div>
1021
		<?php
1022
1023
		return str_replace( '{{form_field}}', ob_get_clean(), $field_wrapper );
1024
	}
1025
1026
1027
	/**
1028
	 * Render wrapper
1029
	 *
1030
	 * @since  1.9
1031
	 * @access private
1032
	 *
1033
	 * @param  array $field
1034
	 * @param  array $form
1035
	 * @param  array $args
1036
	 *
1037
	 * @return string
1038
	 */
1039
	public static function render_field_wrapper( $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...
1040
		ob_start();
1041
1042
		// Label: before field.
1043
		if ( 'before' === $field['label_position'] ) {
1044
			echo self::$instance->render_label( $field );
1045
		}
1046
1047
		// Set before field html.
1048
		$field['before_field'] = self::is_callback( $field['before_field'] )
1049
			? self::render_callback( $field['before_field'] )
1050
			: $field['before_field'];
1051
1052
		// Set after field html.
1053
		$field['after_field'] = self::is_callback( $field['after_field'] )
1054
			? self::render_callback( $field['after_field'] )
1055
			: $field['after_field'];
1056
1057
		echo "{$field['before_field']}{{form_field}}{$field['after_field']}";
1058
1059
		// Label: before field.
1060
		if ( 'after' === $field['label_position'] ) {
1061
			echo self::$instance->render_label( $field );
1062
		}
1063
1064
		$field_with_label = ob_get_clean();
1065
1066
		ob_start();
1067
1068
		if ( $field['wrapper'] ) :
1069
1070
			// Set before wrapper html.
1071
			$field['before_field_wrapper'] = self::is_callback( $field['before_field_wrapper'] )
1072
				? self::render_callback( $field['before_field_wrapper'] )
1073
				: $field['before_field_wrapper'];
1074
1075
			echo $field['before_field_wrapper'];
1076
1077
			echo '<' . $field['wrapper_type'] . ' ' . self::$instance->get_attributes( $field['wrapper_attributes'] ) . '>';
1078
				echo $field_with_label;
1079
			echo "</{$field['wrapper_type']}>";
1080
1081
			// Set after wrapper html.
1082
			$field['after_field_wrapper'] = self::is_callback( $field['after_field_wrapper'] )
1083
				? self::render_callback( $field['after_field_wrapper'] )
1084
				: $field['after_field_wrapper'];
1085
1086
			echo $field['after_field_wrapper'];
1087
		else :
1088
			echo $field_with_label;
1089
		endif;
1090
1091
		return ob_get_clean();
1092
	}
1093
1094
1095
	/**
1096
	 * Render label
1097
	 *
1098
	 * @since  1.9
1099
	 * @access private
1100
	 *
1101
	 * @param  array $field
1102
	 * @param  array $form
1103
	 * @param  array $args
1104
	 *
1105
	 * @return string
1106
	 */
1107
	private function render_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...
1108
		ob_start();
1109
		$label_type = ( 'fieldset' === $field['wrapper_type'] ? 'legend' : 'label' );
1110
		$field['label_attributes']['for'] = $field['field_attributes']['id'];
1111
		?>
1112
		<?php if ( ! empty( $field['label'] ) ) : ?>
1113
			<?php
1114
			// Set before label html.
1115
			$field['before_field_label'] = self::is_callback( $field['before_field_label'] )
1116
				? self::render_callback( $field['before_field_label'] )
1117
				: $field['before_field_label'];
1118
			echo $field['before_field_label'];
1119
			?>
1120
			<<?php echo $label_type; ?> <?php echo self::get_attributes( $field['label_attributes'] ); ?>>
1121
1122
				<?php echo $field['label']; ?>
1123
1124
				<?php if ( $field['required'] ) : ?>
1125
					<span class="give-required-indicator">*</span>
1126
				<?php endif; ?>
1127
1128
				<?php if ( $field['label_tooltip'] ) : ?>
1129
					<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php echo $field['label_tooltip'] ?>"></span>
1130
				<?php endif; ?>
1131
			</<?php echo $label_type; ?>>
1132
			<?php
1133
			// Set after label html.
1134
			$field['after_field_label'] = self::is_callback( $field['after_field_label'] )
1135
				? self::render_callback( $field['after_field_label'] )
1136
				: $field['after_field_label'];
1137
			echo $field['after_field_label'];
1138
			?>
1139
		<?php endif; ?>
1140
		<?php
1141
		return ob_get_clean();
1142
	}
1143
1144
	/**
1145
	 * Get field attribute string from field arguments.
1146
	 *
1147
	 * @since  1.9
1148
	 * @access private
1149
	 *
1150
	 * @param array $attributes
1151
	 *
1152
	 * @return array|string
1153
	 */
1154
	private function get_attributes( $attributes ) {
1155
		$field_attributes_val = '';
1156
1157
		if ( ! empty( $attributes ) ) {
1158
			foreach ( $attributes as $attribute_name => $attribute_val ) {
1159
				$field_attributes_val[] = "{$attribute_name}=\"{$attribute_val}\"";
1160
			}
1161
		}
1162
1163
		if ( ! empty( $field_attributes_val ) ) {
1164
			$field_attributes_val = implode( ' ', $field_attributes_val );
1165
		}
1166
1167
		return $field_attributes_val;
1168
	}
1169
1170
	/**
1171
	 * Set default values for fields
1172
	 *
1173
	 * @since  1.0
1174
	 * @access private
1175
	 *
1176
	 * @param array $field
1177
	 * @param array $form
1178
	 * @param array $args
1179
	 *
1180
	 * @return array
1181
	 */
1182
	public static function set_default_values( $field, $form = null, $args = array() ) {
1183
		/**
1184
		 * Filter the field before set default values.
1185
		 *
1186
		 * @since 1.9
1187
		 *
1188
		 * @param array $field
1189
		 * @param array $form
1190
		 */
1191
		$field = ! empty( $args['fire_filter'] )
1192
			? apply_filters( 'give_field_api_pre_set_default_values', $field, $form )
1193
			: $field;
1194
1195
		switch ( self::$instance->get_field_type( $field ) ) {
1196
			case 'block':
1197
				// Set default values for block.
1198
				$field = wp_parse_args( $field, self::$block_defaults );
1199
1200
				// Set block id.
1201
				$field['field_attributes']['id'] = ! isset( $field['field_attributes']['id'] )
1202
					? "give-{$field['id']}-field"
1203
					: $field['field_attributes']['id'];
1204
1205
				// Set wrapper class.
1206
				$field['block_attributes']['class'] = empty( $field['block_attributes']['class'] )
1207
					? "give-block-wrap js-give-block-wrap give-block-{$field['id']}"
1208
					: trim( "give-block-wrap js-give-block-wrap give-block-{$field['id']} {$field['block_attributes']['class']}" );
1209
1210
				foreach ( $field['fields'] as $key => $single_field ) {
1211
					$single_field['id']    = ! empty( $single_field['id'] )
1212
						? $single_field['id']
1213
						: $key;
1214
					$field['fields'][ $key ] = self::$instance->set_default_values( $single_field, $form, array( 'fire_filter' => false ) );
1215
				}
1216
1217
				break;
1218
1219
			case 'section':
1220
				// Set default values for block.
1221
				$field = wp_parse_args( $field, self::$section_defaults );
1222
1223
				// Set section id.
1224
				$field['field_attributes']['id'] = ! isset( $field['field_attributes']['id'] )
1225
					? "give-{$field['id']}-field"
1226
					: $field['field_attributes']['id'];
1227
1228
				// Set wrapper class.
1229
				$field['section_attributes']['class'] = empty( $field['section_attributes']['class'] )
1230
					? 'give-section-wrap'
1231
					: trim( "give-section-wrap {$field['section_attributes']['class']}" );
1232
1233
				foreach ( $field['fields'] as $key => $single_field ) {
1234
					$single_field['id']    = ! empty( $single_field['id'] )
1235
						? $single_field['id']
1236
						: $key;
1237
					$field['fields'][ $key ] = self::$instance->set_default_values( $single_field, $form, array( 'fire_filter' => false ) );
1238
				}
1239
1240
				break;
1241
1242
			default:
1243
				// Set default values for field or section.
1244
				$field = wp_parse_args( $field, self::$field_defaults );
1245
1246
				// Set default class for label.
1247
				$field['label_attributes']['class'] = empty( $field['label_attributes']['class'] )
1248
					? 'give-label'
1249
					: trim( "give-label {$field['label_attributes']['class']}" );
1250
1251
				// Set field id.
1252
				$field['field_attributes']['id'] = ! isset( $field['field_attributes']['id'] )
1253
					? "give-{$field['id']}-field"
1254
					: $field['field_attributes']['id'];
1255
1256
				// Set field class.
1257
				$field['field_attributes']['class'] = empty( $field['field_attributes']['class'] )
1258
					? "give-field js-give-field"
1259
					: trim( "give-field js-give-field {$field['field_attributes']['class']}" );
1260
1261
				// Set wrapper class.
1262
				$field['wrapper_attributes']['class'] = empty( $field['wrapper_attributes']['class'] )
1263
					? "give-field-wrap {$field['id']}_field"
1264
					: trim( "give-field-wrap {$field['id']}_field {$field['wrapper_attributes']['class']}"  );
1265
1266
				// 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...
1267
				// 	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...
1268
				// 		$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...
1269
				// 			? $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...
1270
				// 			: $key;
1271
				// 		$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...
1272
				// 		$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...
1273
				// 	}
1274
				// }
1275
1276
				/**
1277
				 * Filter the field values.
1278
				 *
1279
				 * @since 1.9
1280
				 *
1281
				 * @param array $field
1282
				 * @param array $form
1283
				 */
1284
				$field = apply_filters( 'give_field_api_set_values', $field, $form );
1285
		}
1286
1287
		/**
1288
		 * Filter the field after set default values.
1289
		 *
1290
		 * @since 1.9
1291
		 *
1292
		 * @param array $field
1293
		 * @param array $form
1294
		 */
1295
		$field = ! empty( $args['fire_filter'] )
1296
			? apply_filters( 'give_field_api_post_set_default_values', $field, $form )
1297
			: $field;
1298
1299
		return $field;
1300
	}
1301
1302
1303
	/**
1304
	 * Set responsive fields.
1305
	 *
1306
	 * @since  1.9
1307
	 * @access private
1308
	 *
1309
	 * @param $form
1310
	 *
1311
	 * @return mixed
1312
	 */
1313
	private function set_responsive_field( &$form ) {
1314
1315
		foreach ( $form['fields'] as $key => $field ) {
1316
			switch ( true ) {
1317
				case array_key_exists( 'fields', $field ):
1318
					foreach ( $field['fields'] as $section_field_index => $section_field ) {
1319
						if ( ! self::$instance->is_sub_section( $section_field ) ) {
1320
							continue;
1321
						}
1322
1323
						$form['fields'][ $key ]['fields'][ $section_field_index ]['wrapper_attributes']['class'] = 'give-form-col';
1324
1325
						if ( array_key_exists( 'sub_section_end', $section_field ) ) {
1326
							$form['fields'][ $key ]['fields'][ $section_field_index ]['wrapper_attributes']['class'] = 'give-form-col give-form-col-end';
1327
1328
							// Clear float left for next field.
1329
							$fields_keys = array_keys( $form['fields'][ $key ]['fields'] );
1330
1331
							if ( $next_field_key = array_search( $section_field_index, $fields_keys ) ) {
1332
								if (
1333
									! isset( $fields_keys[ $next_field_key + 1 ] )
1334
									|| ! isset( $form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ] )
1335
								) {
1336
									continue;
1337
								}
1338
1339
								$next_field = $form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ];
1340
1341
								$next_field['wrapper_attributes']['class'] = isset( $next_field['wrapper_attributes']['class'] )
1342
									? $next_field['wrapper_attributes']['class'] . ' give-clearfix'
1343
									: 'give-clearfix';
1344
1345
								$form['fields'][ $key ]['fields'][ $fields_keys[ $next_field_key + 1 ] ] = $next_field;
1346
							}
1347
						}
1348
					}
1349
1350
					break;
1351
1352
				default:
1353
					if ( ! self::$instance->is_sub_section( $field ) ) {
1354
						continue;
1355
					}
1356
1357
					$form['fields'][ $key ]['wrapper_attributes']['class'] = 'give-form-col';
1358
1359
					if ( array_key_exists( 'sub_section_end', $field ) ) {
1360
						$form['fields'][ $key ]['wrapper_attributes']['class'] = 'give-form-col give-form-col-end';
1361
1362
						// Clear float left for next field.
1363
						$fields_keys = array_keys( $form['fields'] );
1364
1365
						if ( $next_field_key = array_search( $key, $fields_keys ) ) {
1366
							$form['fields'][ $fields_keys[ $next_field_key + 1 ] ]['wrapper_attributes']['class'] = 'give-clearfix';
1367
						}
1368
					}
1369
			}
1370
		}
1371
	}
1372
1373
1374
	/**
1375
	 * Check if current field is part of sub section or not.
1376
	 *
1377
	 * @since  1.9
1378
	 * @access private
1379
	 *
1380
	 * @param $field
1381
	 *
1382
	 * @return bool
1383
	 */
1384
	private function is_sub_section( $field ) {
1385
		$is_sub_section = false;
1386
		if ( array_key_exists( 'sub_section_start', $field ) || array_key_exists( 'sub_section_end', $field ) ) {
1387
			$is_sub_section = true;
1388
		}
1389
1390
		return $is_sub_section;
1391
	}
1392
1393
1394
	/**
1395
	 * Get field type.
1396
	 *
1397
	 * @since  1.9
1398
	 * @access private
1399
	 *
1400
	 * @param  array $field
1401
	 * @param  array $form
1402
	 * @param  array $args
1403
	 *
1404
	 * @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...
1405
	 */
1406
	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...
1407
		$field_type = 'field';
1408
1409
		if (
1410
			isset( $field['type'] )
1411
			&& 'block' === $field['type']
1412
		) {
1413
			$field_type = 'block';
1414
1415
		} elseif (
1416
			array_key_exists( 'fields', $field )
1417
			&& 'section' === $field['type']
1418
		) {
1419
			$field_type = 'section';
1420
1421
		}
1422
1423
		return $field_type;
1424
	}
1425
1426
	/**
1427
	 * Get field name.
1428
	 *
1429
	 * @since  1.9
1430
	 *
1431
	 * @param  array $field
1432
	 * @param  array $form
1433
	 * @param  array $args
1434
	 *
1435
	 * @return string
1436
	 */
1437
	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...
1438
		$field_name = esc_attr( empty( $field['repeat'] ) ? $field['id'] : $field['repeater_field_name'] );
1439
1440
		/**
1441
		 * Filter the field name.
1442
		 *
1443
		 * @since 1.8
1444
		 *
1445
		 * @param string $field_name
1446
		 */
1447
		$field_name = apply_filters( 'give_get_field_name', $field_name, $field );
1448
1449
		return $field_name;
1450
	}
1451
1452
	/**
1453
	 * Get repeater field id.
1454
	 *
1455
	 * @since  1.9
1456
	 *
1457
	 * @param array    $field
1458
	 * @param array    $fields
1459
	 * @param int|bool $default
1460
	 *
1461
	 * @return string
1462
	 */
1463
	public static function get_repeater_field_name( $field, $fields , $default = false ) {
1464
		$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1465
1466
		// Get field id.
1467
		$field_id = "{$fields['id']}[{$row_placeholder}][{$field['id']}]";
1468
1469
		/**
1470
		 * Filter the specific repeater field id
1471
		 *
1472
		 * @since 1.8
1473
		 *
1474
		 * @param string $field_id
1475
		 */
1476
		$field_id = apply_filters( "give_get_repeater_field_{$field['id']}_name", $field_id, $field, $fields, $default );
1477
1478
		/**
1479
		 * Filter the repeater field id
1480
		 *
1481
		 * @since 1.8
1482
		 *
1483
		 * @param string $field_id
1484
		 */
1485
		$field_id = apply_filters( 'give_get_repeater_field_name', $field_id, $field, $fields, $default );
1486
1487
		return $field_id;
1488
	}
1489
1490
1491
	/**
1492
	 * Get repeater field value.
1493
	 *
1494
	 * @since 1.9
1495
	 * @access public
1496
	 *
1497
	 * @param array $field
1498
	 * @param array $field_value_group
1499
	 * @param array $fields
1500
	 *
1501
	 * @return mixed
1502
	 */
1503
	public static function get_repeater_field_value( $field, $field_value_group, $fields ) {
1504
		$field_value = ( isset( $field_value_group[ $field['id'] ] ) ? $field_value_group[ $field['id'] ] : '' );
1505
1506
		/**
1507
		 * Filter the specific repeater field value
1508
		 *
1509
		 * @since 1.8
1510
		 *
1511
		 * @param string $field_id
1512
		 */
1513
		$field_value = apply_filters( "give_get_repeater_field_{$field['id']}_value", $field_value, $field, $field_value_group, $fields );
1514
1515
		/**
1516
		 * Filter the repeater field value
1517
		 *
1518
		 * @since 1.8
1519
		 *
1520
		 * @param string $field_id
1521
		 */
1522
		$field_value = apply_filters( 'give_get_repeater_field_value', $field_value, $field, $field_value_group, $fields );
1523
1524
		return $field_value;
1525
	}
1526
1527
1528
	/**
1529
	 * Check if string or array is callback or not.
1530
	 *
1531
	 * @since  1.9
1532
	 * @access public
1533
	 *
1534
	 * @param $callback
1535
	 *
1536
	 * @return bool
1537
	 */
1538
	public static function is_callback( $callback ) {
1539
		$is_callback = false;
1540
1541
		// Process callback to get field html.
1542
		if ( is_string( $callback ) && function_exists( "$callback" ) ) {
1543
			$is_callback = true;
1544
		} elseif ( is_array( $callback ) && method_exists( $callback[0], "$callback[1]" ) ) {
1545
			$is_callback = true;
1546
		}
1547
1548
		return $is_callback;
1549
	}
1550
}
1551
1552
// @todo implement required and aria-required for all form fields. required should be custom tag because we want custom validation for form instead html5.
1553