Completed
Push — issues/1038 ( 47bf78...afe4b5 )
by Ravinder
20:14
created

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