Completed
Push — issues/1038 ( 4273dc...e8ae90 )
by Ravinder
18:47
created

Give_Fields_API::get_attributes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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