Completed
Push — issues/1038 ( 463413...403c0a )
by Ravinder
18:41
created

Give_Fields_API::get_field_name()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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