Completed
Push — issues/3312 ( 6b1a83 )
by Ravinder
1313:55 queued 1307:48
created

give-metabox-functions.php ➔ give_chosen_input()   C

Complexity

Conditions 9
Paths 192

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 192
nop 1
dl 0
loc 51
rs 6.9002
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Give Meta Box Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Functions
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.8
10
 */
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit; // Exit if accessed directly
13
}
14
15
16
/**
17
 * Check if field callback exist or not.
18
 *
19
 * @since  1.8
20
 *
21
 * @param  $field
22
 *
23
 * @return bool|string
24
 */
25
function give_is_field_callback_exist( $field ) {
26
	return ( give_get_field_callback( $field ) ? true : false );
27
}
28
29
/**
30
 * Get field callback.
31
 *
32
 * @since  1.8
33
 *
34
 * @param  $field
35
 *
36
 * @return bool|string
37
 */
38
function give_get_field_callback( $field ) {
39
	$func_name_prefix = 'give';
40
	$func_name        = '';
0 ignored issues
show
Unused Code introduced by
$func_name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
41
42
	// Set callback function on basis of cmb2 field name.
43
	switch ( $field['type'] ) {
44
		case 'radio_inline':
45
			$func_name = "{$func_name_prefix}_radio";
46
			break;
47
48
		case 'text':
49
		case 'text-medium':
50
		case 'text_medium':
51
		case 'text-small' :
52
		case 'text_small' :
53
		case 'number' :
54
		case 'email' :
55
			$func_name = "{$func_name_prefix}_text_input";
56
			break;
57
58
		case 'textarea' :
59
			$func_name = "{$func_name_prefix}_textarea_input";
60
			break;
61
62
		case 'colorpicker' :
63
			$func_name = "{$func_name_prefix}_{$field['type']}";
64
			break;
65
66
		case 'levels_id':
67
			$func_name = "{$func_name_prefix}_hidden_input";
68
			break;
69
70
		case 'group' :
71
			$func_name = "_{$func_name_prefix}_metabox_form_data_repeater_fields";
72
			break;
73
74
		case 'give_default_radio_inline':
75
			$func_name = "{$func_name_prefix}_radio";
76
			break;
77
78
		case 'donation_limit':
79
			$func_name = "{$func_name_prefix}_donation_limit";
80
			break;
81
82
		case 'chosen':
83
			$func_name = "{$func_name_prefix}_chosen_input";
84
			break;
85
86
		default:
87
88
			if (
89
				array_key_exists( 'callback', $field )
90
				&& ! empty( $field['callback'] )
91
			) {
92
				$func_name = $field['callback'];
93
			} else {
94
				$func_name = "{$func_name_prefix}_{$field['type']}";
95
			}
96
	}
97
98
	/**
99
	 * Filter the metabox setting render function
100
	 *
101
	 * @since 1.8
102
	 */
103
	$func_name = apply_filters( 'give_get_field_callback', $func_name, $field );
104
105
	// Exit if not any function exist.
106
	// Check if render callback exist or not.
107
	if ( empty( $func_name ) ) {
108
		return false;
109
	} elseif ( is_string( $func_name ) && ! function_exists( "$func_name" ) ) {
110
		return false;
111
	} elseif ( is_array( $func_name ) && ! method_exists( $func_name[0], "$func_name[1]" ) ) {
112
		return false;
113
	}
114
115
	return $func_name;
116
}
117
118
/**
119
 * This function adds backward compatibility to render cmb2 type field type.
120
 *
121
 * @since  1.8
122
 *
123
 * @param  array $field Field argument array.
124
 *
125
 * @return bool
126
 */
127
function give_render_field( $field ) {
128
129
	// Check if render callback exist or not.
130
	if ( ! ( $func_name = give_get_field_callback( $field ) ) ) {
131
		return false;
132
	}
133
134
	// CMB2 compatibility: Push all classes to attributes's class key
135
	if ( empty( $field['class'] ) ) {
136
		$field['class'] = '';
137
	}
138
139
	if ( empty( $field['attributes']['class'] ) ) {
140
		$field['attributes']['class'] = '';
141
	}
142
143
	$field['attributes']['class'] = trim( "give-field {$field['attributes']['class']} give-{$field['type']} {$field['class']}" );
144
	unset( $field['class'] );
145
146
	// CMB2 compatibility: Set wrapper class if any.
147 View Code Duplication
	if ( ! empty( $field['row_classes'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
		$field['wrapper_class'] = $field['row_classes'];
149
		unset( $field['row_classes'] );
150
	}
151
152
	// Set field params on basis of cmb2 field name.
153
	switch ( $field['type'] ) {
154
		case 'radio_inline':
155
			if ( empty( $field['wrapper_class'] ) ) {
156
				$field['wrapper_class'] = '';
157
			}
158
			$field['wrapper_class'] .= ' give-inline-radio-fields';
159
160
			break;
161
162
		case 'text':
163
		case 'text-medium':
164
		case 'text_medium':
165
		case 'text-small' :
166
		case 'text_small' :
167
			// CMB2 compatibility: Set field type to text.
168
			$field['type'] = isset( $field['attributes']['type'] ) ? $field['attributes']['type'] : 'text';
169
170
			// CMB2 compatibility: Set data type to price.
171
			if (
172
				empty( $field['data_type'] )
173
				&& ! empty( $field['attributes']['class'] )
174
				&& (
175
					false !== strpos( $field['attributes']['class'], 'money' )
176
					|| false !== strpos( $field['attributes']['class'], 'amount' )
177
				)
178
			) {
179
				$field['data_type'] = 'decimal';
180
			}
181
			break;
182
183
		case 'levels_id':
184
			$field['type'] = 'hidden';
185
			break;
186
187
		case 'colorpicker' :
188
			$field['type']  = 'text';
189
			$field['class'] = 'give-colorpicker';
190
			break;
191
192
		case 'give_default_radio_inline':
193
			$field['type']    = 'radio';
194
			$field['options'] = array(
195
				'default' => __( 'Default' ),
196
			);
197
			break;
198
199
		case 'donation_limit':
200
			$field['type']  = 'donation_limit';
201
			break;
202
	} // End switch().
203
204
	// CMB2 compatibility: Add support to define field description by desc & description param.
205
	// We encourage you to use description param.
206
	$field['description'] = ( ! empty( $field['description'] )
207
		? $field['description']
208
		: ( ! empty( $field['desc'] ) ? $field['desc'] : '' ) );
209
210
	// Call render function.
211
	if ( is_array( $func_name ) ) {
212
		$func_name[0]->{$func_name[1]}( $field );
213
	} else {
214
		$func_name( $field );
215
	}
216
217
	return true;
218
}
219
220
/**
221
 * Output a text input box.
222
 *
223
 * @since  1.8
224
 *
225
 * @param  array $field         {
226
 *                              Optional. Array of text input field arguments.
227
 *
228
 * @type string  $id            Field ID. Default ''.
229
 * @type string  $style         CSS style for input field. Default ''.
230
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
231
 * @type string  $value         Value of input field. Default ''.
232
 * @type string  $name          Name of input field. Default ''.
233
 * @type string  $type          Type of input field. Default 'text'.
234
 * @type string  $before_field  Text/HTML to add before input field. Default ''.
235
 * @type string  $after_field   Text/HTML to add after input field. Default ''.
236
 * @type string  $data_type     Define data type for value of input to filter it properly. Default ''.
237
 * @type string  $description   Description of input field. Default ''.
238
 * @type array   $attributes    List of attributes of input field. Default array().
239
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
240
 *                                               => '****' )
241
 * }
242
 * @return void
243
 */
244
function give_text_input( $field ) {
245
	global $thepostid, $post;
246
247
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
248
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
249
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
250
	$field['value']         = give_get_field_value( $field, $thepostid );
251
	$field['type']          = isset( $field['type'] ) ? $field['type'] : 'text';
252
	$field['before_field']  = '';
253
	$field['after_field']   = '';
254
	$data_type              = empty( $field['data_type'] ) ? '' : $field['data_type'];
255
256
	switch ( $data_type ) {
257
		case 'price' :
258
			$field['value'] = ( ! empty( $field['value'] ) ? give_format_decimal( give_maybe_sanitize_amount( $field['value'] ), false, false ) : $field['value'] );
259
260
			$field['before_field'] = ! empty( $field['before_field'] ) ? $field['before_field'] : ( give_get_option( 'currency_position', 'before' ) == 'before' ? '<span class="give-money-symbol give-money-symbol-before">' . give_currency_symbol() . '</span>' : '' );
261
			$field['after_field']  = ! empty( $field['after_field'] ) ? $field['after_field'] : ( give_get_option( 'currency_position', 'before' ) == 'after' ? '<span class="give-money-symbol give-money-symbol-after">' . give_currency_symbol() . '</span>' : '' );
262
			break;
263
264
		case 'decimal' :
265
			$field['attributes']['class'] .= ' give_input_decimal';
266
			$field['value']               = ( ! empty( $field['value'] ) ? give_format_decimal( give_maybe_sanitize_amount( $field['value'] ), false, false ) : $field['value'] );
267
			break;
268
269
		default :
270
			break;
271
	}
272
273
	?>
274
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
275
	<label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
276
	<?php echo $field['before_field']; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$field'
Loading history...
277
	<input
278
			type="<?php echo esc_attr( $field['type'] ); ?>"
279
			style="<?php echo esc_attr( $field['style'] ); ?>"
280
			name="<?php echo give_get_field_name( $field ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
281
			id="<?php echo esc_attr( $field['id'] ); ?>"
282
			value="<?php echo esc_attr( $field['value'] ); ?>"
283
		<?php echo give_get_custom_attributes( $field ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_custom_attributes'
Loading history...
284
	/>
285
	<?php echo $field['after_field']; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$field'
Loading history...
286
	<?php
287
	echo give_get_field_description( $field );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
288
	echo '</p>';
289
}
290
291
/**
292
 * Output a chosen input box.
293
 *
294
 * @param array $field         {
295
 *                              Optional. Array of text input field arguments.
296
 *
297
 * @type string $id            Field ID. Default ''.
298
 * @type string $style         CSS style for input field. Default ''.
299
 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
300
 * @type string $value         Value of input field. Default ''.
301
 * @type string $name          Name of input field. Default ''.
302
 * @type string $type          Type of input field. Default 'text'.
303
 * @type string $before_field  Text/HTML to add before input field. Default ''.
304
 * @type string $after_field   Text/HTML to add after input field. Default ''.
305
 * @type string $data_type     Define data type for value of input to filter it properly. Default ''.
306
 * @type string $description   Description of input field. Default ''.
307
 * @type array  $attributes    List of attributes of input field. Default array().
308
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
309
 *                                               => '****' )
310
 * }
311
 *
312
 * @since 2.1
313
 *
314
 * @return void
315
 */
316
function give_chosen_input( $field ) {
317
	global $thepostid, $post;
318
319
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
320
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
321
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
322
	$field['value']         = give_get_field_value( $field, $thepostid );
323
	$field['before_field']  = '';
324
	$field['after_field']   = '';
325
	$placeholder            = isset( $field['placeholder'] ) ? 'data-placeholder="' . $field['placeholder'] . '"' : '';
326
	$data_type              = ! empty( $field['data_type'] ) ? $field['data_type'] : '';
327
	$type                   = '';
328
	$allow_new_values       = '';
329
330
	// Set attributes based on multiselect datatype.
331
	if ( 'multiselect' === $data_type ) {
332
		$type = 'multiple';
333
		$allow_new_values = 'data-allows-new-values="true"';
334
	}
335
	?>
336
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
337
		<label for="<?php echo esc_attr( give_get_field_name( $field ) ); ?>">
338
			<?php echo wp_kses_post( $field['name'] ); ?>
339
		</label>
340
		<?php echo esc_attr( $field['before_field'] ); ?>
341
		<select
342
				class="give-select-chosen give-chosen-settings"
343
				style="<?php echo esc_attr( $field['style'] ); ?>"
344
				name="<?php echo esc_attr( give_get_field_name( $field ) ); ?>[]"
345
				id="<?php echo esc_attr( $field['id'] ); ?>"
346
			<?php echo esc_attr( $type ) . ' ' . esc_attr( $allow_new_values ) . ' ' . esc_attr( $placeholder ); ?>
347
		>
348
			<?php foreach ( $field['options'] as $key => $value ) { ?>
349
				<option value="<?php echo esc_attr( $key ); ?>"
350
					<?php
351
					if ( is_array( $field['value'] ) ) {
352
						selected( in_array( $key, $field['value'], true ) );
353
					} else {
354
						selected( $field['value'], $key, true );
355
					}
356
					?>
357
				>
358
					<?php echo esc_html( $value ); ?>
359
				</option>
360
			<?php } ?>
361
		</select>
362
		<?php echo esc_attr( $field['after_field'] ); ?>
363
		<?php echo give_get_field_description( $field ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
364
	</p>
365
	<?php
366
}
367
368
/**
369
 * Give range slider field.
370
 * Note: only for internal logic
371
 *
372
 * @since 2.1
373
 *
374
 * @param  array $field         {
375
 *                              Optional. Array of text input field arguments.
376
 *
377
 * @type string  $id            Field ID. Default ''.
378
 * @type string  $style         CSS style for input field. Default ''.
379
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
380
 * @type string  $value         Value of input field. Default ''.
381
 * @type string  $name          Name of input field. Default ''.
382
 * @type string  $type          Type of input field. Default 'text'.
383
 * @type string  $before_field  Text/HTML to add before input field. Default ''.
384
 * @type string  $after_field   Text/HTML to add after input field. Default ''.
385
 * @type string  $data_type     Define data type for value of input to filter it properly. Default ''.
386
 * @type string  $description   Description of input field. Default ''.
387
 * @type array   $attributes    List of attributes of input field. Default array().
388
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
389
 *                                               => '****' )
390
 * }
391
 *
392
 * @return void
393
 */
394
function give_donation_limit( $field ) {
395
	global $thepostid, $post;
396
397
	// Get Give donation form ID.
398
	$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
399
400
	// Default arguments.
401
	$default_options = array(
402
		'style'         => '',
403
		'wrapper_class' => '',
404
		'value'         => give_get_field_value( $field, $thepostid ),
405
		'data_type'     => 'decimal',
406
		'before_field'  => '',
407
		'after_field'   => '',
408
	);
409
410
	// Field options.
411
	$field['options'] = ! empty( $field['options'] ) ? $field['options'] : array();
412
413
	// Default field option arguments.
414
	$field['options'] = wp_parse_args( $field['options'], array(
415
			'display_label' => '',
416
			'minimum'       => give_format_decimal( '1.00', false, false ),
417
			'maximum'       => give_format_decimal( '999999.99', false, false ),
418
		)
419
	);
420
421
	// Set default field options.
422
	$field_options = wp_parse_args( $field, $default_options );
423
424
	// Get default minimum value, if empty.
425
	$field_options['value']['minimum'] = ! empty( $field_options['value']['minimum'] )
426
		? $field_options['value']['minimum']
427
		: $field_options['options']['minimum'];
428
429
	// Get default maximum value, if empty.
430
	$field_options['value']['maximum'] = ! empty( $field_options['value']['maximum'] )
431
		? $field_options['value']['maximum']
432
		: $field_options['options']['maximum'];
433
	?>
434
	<p class="give-field-wrap <?php echo esc_attr( $field_options['id'] ); ?>_field <?php echo esc_attr( $field_options['wrapper_class'] ); ?>">
435
	<label for="<?php echo give_get_field_name( $field_options ); ?>"><?php echo wp_kses_post( $field_options['name'] ); ?></label>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
436
	<span class="give_donation_limit_display">
437
		<?php
438
		foreach ( $field_options['value'] as $amount_range => $amount_value ) {
439
440
			switch ( $field_options['data_type'] ) {
441
				case 'price' :
442
					$currency_position = give_get_option( 'currency_position', 'before' );
443
					$price_field_labels     = 'minimum' === $amount_range ? __( 'Minimum amount', 'give' ) : __( 'Maximum amount', 'give' );
444
445
					$tooltip_html = array(
446
						'before' => Give()->tooltips->render_span( array(
447
							'label'       => $price_field_labels,
448
							'tag_content' => sprintf( '<span class="give-money-symbol give-money-symbol-before">%s</span>', give_currency_symbol() ),
449
						) ),
450
						'after'  => Give()->tooltips->render_span( array(
451
							'label'       => $price_field_labels,
452
							'tag_content' => sprintf( '<span class="give-money-symbol give-money-symbol-after">%s</span>', give_currency_symbol() ),
453
						) ),
454
					);
455
456
					$before_html = ! empty( $field_options['before_field'] )
457
						? $field_options['before_field']
458
						: ( 'before' === $currency_position ? $tooltip_html['before'] : '' );
459
460
					$after_html = ! empty( $field_options['after_field'] )
461
						? $field_options['after_field']
462
						: ( 'after' === $currency_position ? $tooltip_html['after'] : '' );
463
464
					$field_options['attributes']['class']    .= ' give-text_small';
465
					$field_options['value'][ $amount_range ] = $amount_value;
466
					break;
467
468
				case 'decimal' :
469
					$field_options['attributes']['class']    .= ' give_input_decimal give-text_small';
470
					$field_options['value'][ $amount_range ] = $amount_value;
471
					break;
472
			}
473
474
			echo '<span class=give-minmax-wrap>';
475
			printf( '<label for="%1$s_give_donation_limit_%2$s">%3$s</label>', esc_attr( $field_options['id'] ), esc_attr( $amount_range ), esc_html( $price_field_labels ) );
476
477
			echo isset( $before_html ) ? $before_html : '';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'isset'
Loading history...
478
			?>
479
			<input
480
					name="<?php echo give_get_field_name( $field_options ); ?>[<?php echo esc_attr( $amount_range ); ?>]"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
481
					type="text"
482
					id="<?php echo $field_options['id']; ?>_give_donation_limit_<?php echo $amount_range; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$field_options'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$amount_range'
Loading history...
483
					data-range_type="<?php echo esc_attr( $amount_range ); ?>"
484
					value="<?php echo give_format_decimal( esc_attr( $field_options['value'][ $amount_range ] ) ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_format_decimal'
Loading history...
485
					placeholder="<?php echo give_format_decimal( $field_options['options'][ $amount_range ] ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_format_decimal'
Loading history...
486
				<?php echo give_get_custom_attributes( $field_options ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_custom_attributes'
Loading history...
487
			/>
488
			<?php
489
			echo isset( $after_html ) ? $after_html : '';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'isset'
Loading history...
490
			echo '</span>';
491
		}
492
		?>
493
	</span>
494
		<?php echo give_get_field_description( $field_options ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
495
	</p>
496
	<?php
497
}
498
499
/**
500
 * Output a hidden input box.
501
 *
502
 * @since  1.8
503
 *
504
 * @param  array $field      {
505
 *                           Optional. Array of hidden text input field arguments.
506
 *
507
 * @type string  $id         Field ID. Default ''.
508
 * @type string  $value      Value of input field. Default ''.
509
 * @type string  $name       Name of input field. Default ''.
510
 * @type string  $type       Type of input field. Default 'text'.
511
 * @type array   $attributes List of attributes of input field. Default array().
512
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
513
 *                                               => '****' )
514
 * }
515
 * @return void
516
 */
517
function give_hidden_input( $field ) {
518
	global $thepostid, $post;
519
520
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
521
	$field['value'] = give_get_field_value( $field, $thepostid );
522
523
	// Custom attribute handling
524
	$custom_attributes = array();
525
526 View Code Duplication
	if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
527
528
		foreach ( $field['attributes'] as $attribute => $value ) {
529
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
530
		}
531
	}
532
	?>
533
534
	<input
535
			type="hidden"
536
			name="<?php echo give_get_field_name( $field ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
537
			id="<?php echo esc_attr( $field['id'] ); ?>"
538
			value="<?php echo esc_attr( $field['value'] ); ?>"
539
		<?php echo give_get_custom_attributes( $field ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_custom_attributes'
Loading history...
540
	/>
541
	<?php
542
}
543
544
/**
545
 * Output a textarea input box.
546
 *
547
 * @since  1.8
548
 * @since  1.8
549
 *
550
 * @param  array $field         {
551
 *                              Optional. Array of textarea input field arguments.
552
 *
553
 * @type string  $id            Field ID. Default ''.
554
 * @type string  $style         CSS style for input field. Default ''.
555
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
556
 * @type string  $value         Value of input field. Default ''.
557
 * @type string  $name          Name of input field. Default ''.
558
 * @type string  $description   Description of input field. Default ''.
559
 * @type array   $attributes    List of attributes of input field. Default array().
560
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
561
 *                                               => '****' )
562
 * }
563
 * @return void
564
 */
565
function give_textarea_input( $field ) {
566
	global $thepostid, $post;
567
568
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
569
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
570
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
571
	$field['value']         = give_get_field_value( $field, $thepostid );
572
573
	?>
574
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
575
	<label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
576
	<textarea
577
			style="<?php echo esc_attr( $field['style'] ); ?>"
578
			name="<?php echo give_get_field_name( $field ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
579
			id="<?php echo esc_attr( $field['id'] ); ?>"
580
			rows="10"
581
			cols="20"
582
		<?php echo give_get_custom_attributes( $field ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_custom_attributes'
Loading history...
583
	><?php echo esc_textarea( $field['value'] ); ?></textarea>
584
	<?php
585
	echo give_get_field_description( $field );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
586
	echo '</p>';
587
}
588
589
/**
590
 * Output a wysiwyg.
591
 *
592
 * @since  1.8
593
 *
594
 * @param  array $field         {
595
 *                              Optional. Array of WordPress editor field arguments.
596
 *
597
 * @type string  $id            Field ID. Default ''.
598
 * @type string  $style         CSS style for input field. Default ''.
599
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
600
 * @type string  $value         Value of input field. Default ''.
601
 * @type string  $name          Name of input field. Default ''.
602
 * @type string  $description   Description of input field. Default ''.
603
 * @type array   $attributes    List of attributes of input field. Default array().
604
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
605
 *                                               => '****' )
606
 * }
607
 * @return void
608
 */
609
function give_wysiwyg( $field ) {
610
	global $thepostid, $post;
611
612
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
613
	$field['value']         = give_get_field_value( $field, $thepostid );
614
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
615
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
616
617
	$field['unique_field_id'] = give_get_field_name( $field );
618
	$editor_attributes        = array(
619
		'textarea_name' => isset( $field['repeatable_field_id'] ) ? $field['repeatable_field_id'] : $field['id'],
620
		'textarea_rows' => '10',
621
		'editor_css'    => esc_attr( $field['style'] ),
622
		'editor_class'  => $field['attributes']['class'],
623
	);
624
	$data_wp_editor           = ' data-wp-editor="' . base64_encode( json_encode( array(
625
			$field['value'],
626
			$field['unique_field_id'],
627
			$editor_attributes,
628
		) ) ) . '"';
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 4 spaces, but found 8.
Loading history...
629
	$data_wp_editor           = isset( $field['repeatable_field_id'] ) ? $data_wp_editor : '';
630
631
	echo '<div class="give-field-wrap ' . $field['unique_field_id'] . '_field ' . esc_attr( $field['wrapper_class'] ) . '"' . $data_wp_editor . '><label for="' . $field['unique_field_id'] . '">' . wp_kses_post( $field['name'] ) . '</label>';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$field'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$data_wp_editor'
Loading history...
632
633
	wp_editor(
634
		$field['value'],
635
		$field['unique_field_id'],
636
		$editor_attributes
637
	);
638
639
	echo give_get_field_description( $field );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
640
	echo '</div>';
641
}
642
643
/**
644
 * Output a checkbox input box.
645
 *
646
 * @since  1.8
647
 *
648
 * @param  array $field         {
649
 *                              Optional. Array of checkbox field arguments.
650
 *
651
 * @type string  $id            Field ID. Default ''.
652
 * @type string  $style         CSS style for input field. Default ''.
653
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
654
 * @type string  $value         Value of input field. Default ''.
655
 * @type string  $cbvalue       Checkbox value. Default 'on'.
656
 * @type string  $name          Name of input field. Default ''.
657
 * @type string  $description   Description of input field. Default ''.
658
 * @type array   $attributes    List of attributes of input field. Default array().
659
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
660
 *                                               => '****' )
661
 * }
662
 * @return void
663
 */
664
function give_checkbox( $field ) {
665
	global $thepostid, $post;
666
667
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
668
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
669
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
670
	$field['value']         = give_get_field_value( $field, $thepostid );
671
	$field['cbvalue']       = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'on';
672
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
673
	?>
674
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
675
	<label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
676
	<input
677
			type="checkbox"
678
			style="<?php echo esc_attr( $field['style'] ); ?>"
679
			name="<?php echo give_get_field_name( $field ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
680
			id="<?php echo esc_attr( $field['id'] ); ?>"
681
			value="<?php echo esc_attr( $field['cbvalue'] ); ?>"
682
		<?php echo checked( $field['value'], $field['cbvalue'], false ); ?>
683
		<?php echo give_get_custom_attributes( $field ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_custom_attributes'
Loading history...
684
	/>
685
	<?php
686
	echo give_get_field_description( $field );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
687
	echo '</p>';
688
}
689
690
/**
691
 * Output a select input box.
692
 *
693
 * @since  1.8
694
 *
695
 * @param  array $field         {
696
 *                              Optional. Array of select field arguments.
697
 *
698
 * @type string  $id            Field ID. Default ''.
699
 * @type string  $style         CSS style for input field. Default ''.
700
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
701
 * @type string  $value         Value of input field. Default ''.
702
 * @type string  $name          Name of input field. Default ''.
703
 * @type string  $description   Description of input field. Default ''.
704
 * @type array   $attributes    List of attributes of input field. Default array().
705
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
706
 *                                               => '****' )
707
 * @type array   $options       List of options. Default array().
708
 *                                               for example: 'options' => array( '' => 'None', 'yes' => 'Yes' )
709
 * }
710
 * @return void
711
 */
712
function give_select( $field ) {
713
	global $thepostid, $post;
714
715
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
716
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
717
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
718
	$field['value']         = give_get_field_value( $field, $thepostid );
719
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
720
	?>
721
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
722
	<label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
723
	<select
724
	id="<?php echo esc_attr( $field['id'] ); ?>"
725
	name="<?php echo give_get_field_name( $field ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
726
	style="<?php echo esc_attr( $field['style'] ) ?>"
727
	<?php echo give_get_custom_attributes( $field ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_custom_attributes'
Loading history...
728
	>
729
	<?php
730
	foreach ( $field['options'] as $key => $value ) {
731
		echo '<option value="' . esc_attr( $key ) . '" ' . selected( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
732
	}
733
	echo '</select>';
734
	echo give_get_field_description( $field );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
735
	echo '</p>';
736
}
737
738
/**
739
 * Output a radio input box.
740
 *
741
 * @since  1.8
742
 *
743
 * @param  array $field         {
744
 *                              Optional. Array of radio field arguments.
745
 *
746
 * @type string  $id            Field ID. Default ''.
747
 * @type string  $style         CSS style for input field. Default ''.
748
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
749
 * @type string  $value         Value of input field. Default ''.
750
 * @type string  $name          Name of input field. Default ''.
751
 * @type string  $description   Description of input field. Default ''.
752
 * @type array   $attributes    List of attributes of input field. Default array().
753
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
754
 *                                               => '****' )
755
 * @type array   $options       List of options. Default array().
756
 *                                               for example: 'options' => array( 'enable' => 'Enable', 'disable' =>
757
 *                                               'Disable' )
758
 * }
759
 * @return void
760
 */
761
function give_radio( $field ) {
762
	global $thepostid, $post;
763
764
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
765
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
766
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
767
	$field['value']         = give_get_field_value( $field, $thepostid );
768
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
769
770
	echo '<fieldset class="give-field-wrap ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><span class="give-field-label">' . wp_kses_post( $field['name'] ) . '</span><legend class="screen-reader-text">' . wp_kses_post( $field['name'] ) . '</legend><ul class="give-radios">';
771
772
	foreach ( $field['options'] as $key => $value ) {
773
774
		echo '<li><label><input
775
				name="' . give_get_field_name( $field ) . '"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
776
				value="' . esc_attr( $key ) . '"
777
				type="radio"
778
				style="' . esc_attr( $field['style'] ) . '"
779
				' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . ' '
780
		     . give_get_custom_attributes( $field ) . '
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_custom_attributes'
Loading history...
781
				/> ' . esc_html( $value ) . '</label>
782
		</li>';
783
	}
784
	echo '</ul>';
785
786
	echo give_get_field_description( $field );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
787
	echo '</fieldset>';
788
}
789
790
/**
791
 * Output a colorpicker.
792
 *
793
 * @since  1.8
794
 *
795
 * @param  array $field         {
796
 *                              Optional. Array of colorpicker field arguments.
797
 *
798
 * @type string  $id            Field ID. Default ''.
799
 * @type string  $style         CSS style for input field. Default ''.
800
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
801
 * @type string  $value         Value of input field. Default ''.
802
 * @type string  $name          Name of input field. Default ''.
803
 * @type string  $description   Description of input field. Default ''.
804
 * @type array   $attributes    List of attributes of input field. Default array().
805
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
806
 *                                               => '****' )
807
 * }
808
 * @return void
809
 */
810
function give_colorpicker( $field ) {
811
	global $thepostid, $post;
812
813
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
814
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
815
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
816
	$field['value']         = give_get_field_value( $field, $thepostid );
817
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
818
	$field['type']          = 'text';
819
	?>
820
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
821
	<label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
822
	<input
823
			type="<?php echo esc_attr( $field['type'] ); ?>"
824
			style="<?php echo esc_attr( $field['style'] ); ?>"
825
			name="<?php echo give_get_field_name( $field ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
826
			id="' . esc_attr( $field['id'] ) . '" value="<?php echo esc_attr( $field['value'] ); ?>"
827
		<?php echo give_get_custom_attributes( $field ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_custom_attributes'
Loading history...
828
	/>
829
	<?php
830
	echo give_get_field_description( $field );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
831
	echo '</p>';
832
}
833
834
/**
835
 * Output a file upload field.
836
 *
837
 * @since  1.8.9
838
 *
839
 * @param array $field
840
 */
841
function give_file( $field ) {
842
	give_media( $field );
843
}
844
845
846
/**
847
 * Output a media upload field.
848
 *
849
 * @since  1.8
850
 *
851
 * @param array $field
852
 */
853
function give_media( $field ) {
854
	global $thepostid, $post;
855
856
	$thepostid    = empty( $thepostid ) ? $post->ID : $thepostid;
857
	$button_label = sprintf( __( 'Add or Upload %s', 'give' ), ( 'file' === $field['type'] ? __( 'File', 'give' ) : __( 'Image', 'give' ) ) );
858
859
	$field['style']               = isset( $field['style'] ) ? $field['style'] : '';
860
	$field['wrapper_class']       = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
861
	$field['value']               = give_get_field_value( $field, $thepostid );
862
	$field['name']                = isset( $field['name'] ) ? $field['name'] : $field['id'];
863
	$field['attributes']['class'] = "{$field['attributes']['class']} give-text-medium";
864
865
	// Allow developer to save attachment ID or attachment url as metadata.
866
	$field['fvalue'] = isset( $field['fvalue'] ) ? $field['fvalue'] : 'url';
867
868
	$allow_media_preview_tags = array( 'jpg', 'jpeg', 'png', 'gif', 'ico' );
869
	$preview_image_src        = $field['value'] ? ( 'id' === $field['fvalue'] ? wp_get_attachment_url( $field['value'] ) : $field['value'] ) : '#';
870
	$preview_image_extension  = $preview_image_src ? pathinfo( $preview_image_src, PATHINFO_EXTENSION ) : '';
871
	$is_show_preview          = in_array( $preview_image_extension, $allow_media_preview_tags );
872
	?>
873
	<fieldset class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
874
		<label for="<?php echo give_get_field_name( $field ) ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
875
		<input
876
				name="<?php echo give_get_field_name( $field ); ?>"
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
877
				id="<?php echo esc_attr( $field['id'] ); ?>"
878
				type="text"
879
				value="<?php echo $field['value']; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$field'
Loading history...
880
				style="<?php echo esc_attr( $field['style'] ); ?>"
881
			<?php echo give_get_custom_attributes( $field ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_custom_attributes'
Loading history...
882
		/>&nbsp;&nbsp;&nbsp;&nbsp;<input class="give-upload-button button" type="button" value="<?php echo $button_label; ?>" data-fvalue="<?php echo $field['fvalue']; ?>" data-field-type="<?php echo $field['type']; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$button_label'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$field'
Loading history...
883
		<?php echo give_get_field_description( $field ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
884
		<div class="give-image-thumb<?php echo ! $field['value'] || ! $is_show_preview ? ' give-hidden' : ''; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
885
			<span class="give-delete-image-thumb dashicons dashicons-no-alt"></span>
886
			<img src="<?php echo $preview_image_src; ?>" alt="">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$preview_image_src'
Loading history...
887
		</div>
888
	</fieldset>
889
	<?php
890
}
891
892
/**
893
 * Output a select field with payment options list.
894
 *
895
 * @since  1.8
896
 *
897
 * @param  array $field
898
 *
899
 * @return void
900
 */
901
function give_default_gateway( $field ) {
902
	global $thepostid, $post;
903
904
	// get all active payment gateways.
905
	$gateways         = give_get_enabled_payment_gateways( $thepostid );
906
	$field['options'] = array();
907
908
	// Set field option value.
909
	if ( ! empty( $gateways ) ) {
910
		foreach ( $gateways as $key => $option ) {
911
			$field['options'][ $key ] = $option['admin_label'];
912
		}
913
	}
914
915
	// Add a field to the Give Form admin single post view of this field
916
	if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
917
		$field['options'] = array_merge( array( 'global' => esc_html__( 'Global Default', 'give' ) ), $field['options'] );
918
	}
919
920
	// Render select field.
921
	give_select( $field );
922
}
923
924
/**
925
 * Output the documentation link.
926
 *
927
 * @since  1.8
928
 *
929
 * @param  array $field      {
930
 *                           Optional. Array of customizable link attributes.
931
 *
932
 * @type string  $name       Name of input field. Default ''.
933
 * @type string  $type       Type of input field. Default 'text'.
934
 * @type string  $url        Value to be passed as a link. Default 'https://givewp.com/documentation'.
935
 * @type string  $title      Value to be passed as text of link. Default 'Documentation'.
936
 * @type array   $attributes List of attributes of input field. Default array().
937
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
938
 *                                               => '****' )
939
 * }
940
 * @return void
941
 */
942
943
function give_docs_link( $field ) {
944
	$field['url']   = isset( $field['url'] ) ? $field['url'] : 'https://givewp.com/documentation';
945
	$field['title'] = isset( $field['title'] ) ? $field['title'] : 'Documentation';
946
947
	echo '<p class="give-docs-link"><a href="' . esc_url( $field['url'] )
948
	     . '" target="_blank">'
949
	     . sprintf( esc_html__( 'Need Help? See docs on "%s"', 'give' ), $field['title'] )
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
950
	     . '<span class="dashicons dashicons-editor-help"></span></a></p>';
951
}
952
953
954
/**
955
 * Output preview buttons.
956
 *
957
 * @since 2.0
958
 *
959
 * @param $field
960
 */
961
function give_email_preview_buttons( $field ) {
962
	/* @var WP_Post $post */
963
	global $post;
964
965
	$field_id = str_replace( array( '_give_', '_preview_buttons' ), '', $field['id'] );
966
967
	ob_start();
968
969
	echo '<p class="give-field-wrap ' . esc_attr( $field['id'] ) . '_field"><label for="' . give_get_field_name( $field ) . '">' . wp_kses_post( $field['name'] ) . '</label>';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_name'
Loading history...
970
971
	echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
972
		'<a href="%1$s" class="button-secondary" target="_blank">%2$s</a>',
973
		wp_nonce_url(
974
			add_query_arg(
975
				array(
976
					'give_action' => 'preview_email',
977
					'email_type'  => $field_id,
978
					'form_id'     => $post->ID,
979
				),
980
				home_url()
981
			), 'give-preview-email'
982
		),
983
		$field['name']
984
	);
985
986
	echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
987
		' <a href="%1$s" aria-label="%2$s" class="button-secondary">%3$s</a>',
988
		wp_nonce_url(
989
			add_query_arg(
990
				array(
991
					'give_action'  => 'send_preview_email',
992
					'email_type'   => $field_id,
993
					'give-messages[]' => 'sent-test-email',
994
					'form_id'      => $post->ID,
995
				)
996
			), 'give-send-preview-email' ),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
997
		esc_attr__( 'Send Test Email.', 'give' ),
998
		esc_html__( 'Send Test Email', 'give' )
999
	);
1000
1001
	if ( ! empty( $field['description'] ) ) {
1002
		echo '<span class="give-field-description">' . wp_kses_post( $field['desc'] ) . '</span>';
1003
	}
1004
1005
	echo '</p>';
1006
1007
	echo ob_get_clean();
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'ob_get_clean'
Loading history...
1008
}
1009
1010
/**
1011
 * Get setting field value.
1012
 *
1013
 * Note: Use only for single post, page or custom post type.
1014
 *
1015
 * @since  1.8
1016
 * @since  2.1 Added support for donation_limit.
1017
 *
1018
 * @param  array $field
1019
 * @param  int   $postid
1020
 *
1021
 * @return mixed
1022
 */
1023
function give_get_field_value( $field, $postid ) {
1024
	if ( isset( $field['attributes']['value'] ) ) {
1025
		return $field['attributes']['value'];
1026
	}
1027
1028
	// If field is range slider.
1029
	if ( 'donation_limit' === $field['type'] ) {
1030
1031
		// Get minimum value.
1032
		$minimum = give_get_meta( $postid, $field['id'] . '_minimum', true );
1033
1034
		// Give < 2.1
1035
		if ( '_give_custom_amount_range' === $field['id'] && empty( $minimum ) ) {
1036
			$minimum = give_get_meta( $postid, '_give_custom_amount_minimum', true );
1037
		}
1038
1039
		$field_value = array(
1040
			'minimum' => $minimum,
1041
			'maximum' => give_get_meta( $postid, $field['id'] . '_maximum', true ),
1042
		);
1043
	} else {
1044
		// Get value from db.
1045
		$field_value = give_get_meta( $postid, $field['id'], true );
1046
	}
1047
1048
	/**
1049
	 * Filter the field value before apply default value.
1050
	 *
1051
	 * @since 1.8
1052
	 *
1053
	 * @param mixed $field_value Field value.
1054
	 */
1055
	$field_value = apply_filters( "{$field['id']}_field_value", $field_value, $field, $postid );
1056
1057
	// Set default value if no any data saved to db.
1058
	if ( ! $field_value && isset( $field['default'] ) ) {
1059
		$field_value = $field['default'];
1060
	}
1061
1062
	return $field_value;
1063
}
1064
1065
1066
/**
1067
 * Get field description html.
1068
 *
1069
 * @since 1.8
1070
 *
1071
 * @param $field
1072
 *
1073
 * @return string
1074
 */
1075
function give_get_field_description( $field ) {
1076
	$field_desc_html = '';
1077
	$description     = '';
1078
1079
	// Check for both `description` and `desc`.
1080
	if ( isset( $field['description'] ) ) {
1081
		$description = $field['description'];
1082
	} elseif ( isset( $field['desc'] ) ) {
1083
		$description = $field['desc'];
1084
	}
1085
1086
	// Set if there is a description.
1087
	if ( ! empty( $description ) ) {
1088
		$field_desc_html = '<span class="give-field-description">' . wp_kses_post( $description ) . '</span>';
1089
	}
1090
1091
	return $field_desc_html;
1092
}
1093
1094
1095
/**
1096
 * Get repeater field value.
1097
 *
1098
 * Note: Use only for single post, page or custom post type.
1099
 *
1100
 * @since  1.8
1101
 *
1102
 * @param array $field
1103
 * @param array $field_group
1104
 * @param array $fields
1105
 *
1106
 * @return string
1107
 */
1108
function give_get_repeater_field_value( $field, $field_group, $fields ) {
1109
	$field_value = ( isset( $field_group[ $field['id'] ] ) ? $field_group[ $field['id'] ] : '' );
1110
1111
	/**
1112
	 * Filter the specific repeater field value
1113
	 *
1114
	 * @since 1.8
1115
	 *
1116
	 * @param string $field_id
1117
	 */
1118
	$field_value = apply_filters( "give_get_repeater_field_{$field['id']}_value", $field_value, $field, $field_group, $fields );
1119
1120
	/**
1121
	 * Filter the repeater field value
1122
	 *
1123
	 * @since 1.8
1124
	 *
1125
	 * @param string $field_id
1126
	 */
1127
	$field_value = apply_filters( 'give_get_repeater_field_value', $field_value, $field, $field_group, $fields );
1128
1129
	return $field_value;
1130
}
1131
1132
/**
1133
 * Get repeater field id.
1134
 *
1135
 * Note: Use only for single post, page or custom post type.
1136
 *
1137
 * @since  1.8
1138
 *
1139
 * @param array    $field
1140
 * @param array    $fields
1141
 * @param int|bool $default
1142
 *
1143
 * @return string
1144
 */
1145
function give_get_repeater_field_id( $field, $fields, $default = false ) {
1146
	$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1147
1148
	// Get field id.
1149
	$field_id = "{$fields['id']}[{$row_placeholder}][{$field['id']}]";
1150
1151
	/**
1152
	 * Filter the specific repeater field id
1153
	 *
1154
	 * @since 1.8
1155
	 *
1156
	 * @param string $field_id
1157
	 */
1158
	$field_id = apply_filters( "give_get_repeater_field_{$field['id']}_id", $field_id, $field, $fields, $default );
1159
1160
	/**
1161
	 * Filter the repeater field id
1162
	 *
1163
	 * @since 1.8
1164
	 *
1165
	 * @param string $field_id
1166
	 */
1167
	$field_id = apply_filters( 'give_get_repeater_field_id', $field_id, $field, $fields, $default );
1168
1169
	return $field_id;
1170
}
1171
1172
1173
/**
1174
 * Get field name.
1175
 *
1176
 * @since  1.8
1177
 *
1178
 * @param  array $field
1179
 *
1180
 * @return string
1181
 */
1182
function give_get_field_name( $field ) {
1183
	$field_name = esc_attr( empty( $field['repeat'] ) ? $field['id'] : $field['repeatable_field_id'] );
1184
1185
	/**
1186
	 * Filter the field name.
1187
	 *
1188
	 * @since 1.8
1189
	 *
1190
	 * @param string $field_name
1191
	 */
1192
	$field_name = apply_filters( 'give_get_field_name', $field_name, $field );
1193
1194
	return $field_name;
1195
}
1196
1197
/**
1198
 * Output repeater field or multi donation type form on donation from edit screen.
1199
 * Note: internal use only.
1200
 *
1201
 * @TODO   : Add support for wysiwyg type field.
1202
 *
1203
 * @since  1.8
1204
 *
1205
 * @param  array $fields
1206
 *
1207
 * @return void
1208
 */
1209
function _give_metabox_form_data_repeater_fields( $fields ) {
1210
	global $thepostid, $post;
1211
1212
	// Bailout.
1213
	if ( ! isset( $fields['fields'] ) || empty( $fields['fields'] ) ) {
1214
		return;
1215
	}
1216
1217
	$group_numbering = isset( $fields['options']['group_numbering'] ) ? (int) $fields['options']['group_numbering'] : 0;
1218
	$close_tabs      = isset( $fields['options']['close_tabs'] ) ? (int) $fields['options']['close_tabs'] : 0;
1219
	$wrapper_class   = isset( $fields['wrapper_class'] ) ? $fields['wrapper_class'] : '';
1220
	?>
1221
	<div class="give-repeatable-field-section <?php echo esc_attr( $wrapper_class ); ?>" id="<?php echo "{$fields['id']}_field"; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '"{$fields['id']}_field"'
Loading history...
1222
	     data-group-numbering="<?php echo $group_numbering; ?>" data-close-tabs="<?php echo $close_tabs; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$group_numbering'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$close_tabs'
Loading history...
1223
		<?php if ( ! empty( $fields['name'] ) ) : ?>
1224
			<p class="give-repeater-field-name"><?php echo $fields['name']; ?></p>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$fields'
Loading history...
1225
		<?php endif; ?>
1226
1227
		<?php if ( ! empty( $fields['description'] ) ) : ?>
1228
			<p class="give-repeater-field-description"><?php echo $fields['description']; ?></p>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$fields'
Loading history...
1229
		<?php endif; ?>
1230
1231
		<table class="give-repeatable-fields-section-wrapper" cellspacing="0">
1232
			<?php
1233
			$repeater_field_values = give_get_meta( $thepostid, $fields['id'], true );
1234
			$header_title          = isset( $fields['options']['header_title'] )
1235
				? $fields['options']['header_title']
1236
				: esc_attr__( 'Group', 'give' );
1237
1238
			$add_default_donation_field = false;
1239
1240
			// Check if level is not created or we have to add default level.
1241
			if ( is_array( $repeater_field_values ) && ( $fields_count = count( $repeater_field_values ) ) ) {
1242
				$repeater_field_values = array_values( $repeater_field_values );
1243
			} else {
1244
				$fields_count               = 1;
1245
				$add_default_donation_field = true;
1246
			}
1247
			?>
1248
			<tbody class="container"<?php echo " data-rf-row-count=\"{$fields_count}\""; ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '" data-rf-row-count=\"{$fields_count}\""'
Loading history...
1249
			<!--Repeater field group template-->
1250
			<tr class="give-template give-row">
1251
				<td class="give-repeater-field-wrap give-column" colspan="2">
1252
					<div class="give-row-head give-move">
1253
						<button type="button" class="handlediv button-link"><span class="toggle-indicator"></span>
1254
						</button>
1255
						<span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-</span>
1256
						<h2>
1257
							<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$header_title'
Loading history...
1258
						</h2>
1259
					</div>
1260
					<div class="give-row-body">
1261
						<?php foreach ( $fields['fields'] as $field ) : ?>
1262
							<?php
1263
							if ( ! give_is_field_callback_exist( $field ) ) {
1264
								continue;
1265
							}
1266
							?>
1267
							<?php
1268
							$field['repeat']              = true;
1269
							$field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields );
1270
							$field['id']                  = str_replace(
1271
								array( '[', ']' ),
1272
								array( '_', '', ),
1273
								$field['repeatable_field_id']
1274
							);
1275
							?>
1276
							<?php give_render_field( $field ); ?>
1277
						<?php endforeach; ?>
1278
					</div>
1279
				</td>
1280
			</tr>
1281
1282
			<?php if ( ! empty( $repeater_field_values ) ) : ?>
1283
				<!--Stored repeater field group-->
1284
				<?php foreach ( $repeater_field_values as $index => $field_group ) : ?>
1285
					<tr class="give-row">
1286
						<td class="give-repeater-field-wrap give-column" colspan="2">
1287
							<div class="give-row-head give-move">
1288
								<button type="button" class="handlediv button-link">
1289
									<span class="toggle-indicator"></span></button>
1290
								<span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
1291
								</span>
1292
								<h2>
1293
									<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$header_title'
Loading history...
1294
								</h2>
1295
							</div>
1296
							<div class="give-row-body">
1297
								<?php foreach ( $fields['fields'] as $field ) : ?>
1298
									<?php if ( ! give_is_field_callback_exist( $field ) ) {
1299
										continue;
1300
									} ?>
1301
									<?php
1302
									$field['repeat']              = true;
1303
									$field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields, $index );
1304
									$field['attributes']['value'] = give_get_repeater_field_value( $field, $field_group, $fields );
1305
									$field['id']                  = str_replace(
1306
										array( '[', ']' ),
1307
										array( '_', '', ),
1308
										$field['repeatable_field_id']
1309
									);
1310
									?>
1311
									<?php give_render_field( $field ); ?>
1312
								<?php endforeach; ?>
1313
							</div>
1314
						</td>
1315
					</tr>
1316
				<?php endforeach;; ?>
1317
1318
			<?php elseif ( $add_default_donation_field ) : ?>
1319
				<!--Default repeater field group-->
1320
				<tr class="give-row">
1321
					<td class="give-repeater-field-wrap give-column" colspan="2">
1322
						<div class="give-row-head give-move">
1323
							<button type="button" class="handlediv button-link">
1324
								<span class="toggle-indicator"></span></button>
1325
							<span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
1326
							</span>
1327
							<h2>
1328
								<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$header_title'
Loading history...
1329
							</h2>
1330
						</div>
1331
						<div class="give-row-body">
1332
							<?php
1333
							foreach ( $fields['fields'] as $field ) :
1334
								if ( ! give_is_field_callback_exist( $field ) ) {
1335
									continue;
1336
								}
1337
1338
								$field['repeat']              = true;
1339
								$field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields, 0 );
1340
								$field['attributes']['value'] = apply_filters(
1341
									"give_default_field_group_field_{$field['id']}_value",
1342
									( ! empty( $field['default'] ) ? $field['default'] : '' ),
1343
									$field,
1344
									$fields
1345
								);
1346
								$field['id']                  = str_replace(
1347
									array( '[', ']' ),
1348
									array( '_', '', ),
1349
									$field['repeatable_field_id']
1350
								);
1351
								give_render_field( $field );
1352
1353
							endforeach;
1354
							?>
1355
						</div>
1356
					</td>
1357
				</tr>
1358
			<?php endif; ?>
1359
			</tbody>
1360
			<tfoot>
1361
			<tr>
1362
				<?php
1363
				$add_row_btn_title = isset( $fields['options']['add_button'] )
1364
					? $add_row_btn_title = $fields['options']['add_button']
1365
					: esc_html__( 'Add Row', 'give' );
1366
				?>
1367
				<td colspan="2" class="give-add-repeater-field-section-row-wrap">
1368
					<span class="button button-primary give-add-repeater-field-section-row"><?php echo $add_row_btn_title; ?></span>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$add_row_btn_title'
Loading history...
1369
				</td>
1370
			</tr>
1371
			</tfoot>
1372
		</table>
1373
	</div>
1374
	<?php
1375
}
1376
1377
1378
/**
1379
 * Get current setting tab.
1380
 *
1381
 * @since  1.8
1382
 * @return string
1383
 */
1384 View Code Duplication
function give_get_current_setting_tab() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1385
	// Get current setting page.
1386
	$current_setting_page = give_get_current_setting_page();
1387
1388
	/**
1389
	 * Filter the default tab for current setting page.
1390
	 *
1391
	 * @since 1.8
1392
	 *
1393
	 * @param string
1394
	 */
1395
	$default_current_tab = apply_filters( "give_default_setting_tab_{$current_setting_page}", 'general' );
1396
1397
	// Get current tab.
1398
	$current_tab = empty( $_GET['tab'] ) ? $default_current_tab : urldecode( $_GET['tab'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
1399
1400
	// Output.
1401
	return $current_tab;
1402
}
1403
1404
1405
/**
1406
 * Get current setting section.
1407
 *
1408
 * @since  1.8
1409
 * @return string
1410
 */
1411 View Code Duplication
function give_get_current_setting_section() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1412
	// Get current tab.
1413
	$current_tab = give_get_current_setting_tab();
1414
1415
	/**
1416
	 * Filter the default section for current setting page tab.
1417
	 *
1418
	 * @since 1.8
1419
	 *
1420
	 * @param string
1421
	 */
1422
	$default_current_section = apply_filters( "give_default_setting_tab_section_{$current_tab}", '' );
1423
1424
	// Get current section.
1425
	$current_section = empty( $_REQUEST['section'] ) ? $default_current_section : urldecode( $_REQUEST['section'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
1426
1427
	// Output.
1428
	return $current_section;
1429
}
1430
1431
/**
1432
 * Get current setting page.
1433
 *
1434
 * @since  1.8
1435
 * @return string
1436
 */
1437
function give_get_current_setting_page() {
1438
	// Get current page.
1439
	$setting_page = ! empty( $_GET['page'] ) ? urldecode( $_GET['page'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
1440
1441
	// Output.
1442
	return $setting_page;
1443
}
1444
1445
/**
1446
 * Set value for Form content --> Display content field setting.
1447
 *
1448
 * Backward compatibility:  set value by _give_content_option form meta field value if _give_display_content is not set
1449
 * yet.
1450
 *
1451
 * @since  1.8
1452
 *
1453
 * @param  mixed $field_value Field Value.
1454
 * @param  array $field       Field args.
1455
 * @param  int   $postid      Form/Post ID.
1456
 *
1457
 * @return string
1458
 */
1459
function _give_display_content_field_value( $field_value, $field, $postid ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
1460
	$show_content = give_get_meta( $postid, '_give_content_option', true );
1461
1462
	if (
1463
		! give_get_meta( $postid, '_give_display_content', true )
1464
		&& $show_content
1465
		&& ( 'none' !== $show_content )
1466
	) {
1467
		$field_value = 'enabled';
1468
	}
1469
1470
	return $field_value;
1471
}
1472
1473
add_filter( '_give_display_content_field_value', '_give_display_content_field_value', 10, 3 );
1474
1475
1476
/**
1477
 * Set value for Form content --> Content placement field setting.
1478
 *
1479
 * Backward compatibility:  set value by _give_content_option form meta field value if _give_content_placement is not
1480
 * set yet.
1481
 *
1482
 * @since  1.8
1483
 *
1484
 * @param  mixed $field_value Field Value.
1485
 * @param  array $field       Field args.
1486
 * @param  int   $postid      Form/Post ID.
1487
 *
1488
 * @return string
1489
 */
1490
function _give_content_placement_field_value( $field_value, $field, $postid ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
1491
	$show_content = give_get_meta( $postid, '_give_content_option', true );
1492
1493
	if (
1494
		! give_get_meta( $postid, '_give_content_placement', true )
1495
		&& ( 'none' !== $show_content )
1496
	) {
1497
		$field_value = $show_content;
1498
	}
1499
1500
	return $field_value;
1501
}
1502
1503
add_filter( '_give_content_placement_field_value', '_give_content_placement_field_value', 10, 3 );
1504
1505
1506
/**
1507
 * Set value for Terms and Conditions --> Terms and Conditions field setting.
1508
 *
1509
 * Backward compatibility:  set value by _give_terms_option form meta field value if it's value is none.
1510
 *
1511
 * @since  1.8
1512
 *
1513
 * @param  mixed $field_value Field Value.
1514
 * @param  array $field       Field args.
1515
 * @param  int   $postid      Form/Post ID.
1516
 *
1517
 * @return string
1518
 */
1519 View Code Duplication
function _give_terms_option_field_value( $field_value, $field, $postid ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1520
	$term_option = give_get_meta( $postid, '_give_terms_option', true );
1521
1522
	if ( in_array( $term_option, array( 'none', 'yes' ) ) ) {
1523
		$field_value = ( 'yes' === $term_option ? 'enabled' : 'disabled' );
1524
	}
1525
1526
	return $field_value;
1527
}
1528
1529
add_filter( '_give_terms_option_field_value', '_give_terms_option_field_value', 10, 3 );
1530
1531
1532
/**
1533
 * Set value for Form Display --> Offline Donation --> Billing Fields.
1534
 *
1535
 * Backward compatibility:  set value by _give_offline_donation_enable_billing_fields_single form meta field value if
1536
 * it's value is on.
1537
 *
1538
 * @since  1.8
1539
 *
1540
 * @param  mixed $field_value Field Value.
1541
 * @param  array $field       Field args.
1542
 * @param  int   $postid      Form/Post ID.
1543
 *
1544
 * @return string
1545
 */
1546
function _give_offline_donation_enable_billing_fields_single_field_value( $field_value, $field, $postid ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
1547
	$offline_donation = give_get_meta( $postid, '_give_offline_donation_enable_billing_fields_single', true );
1548
1549
	if ( 'on' === $offline_donation ) {
1550
		$field_value = 'enabled';
1551
	}
1552
1553
	return $field_value;
1554
}
1555
1556
add_filter( '_give_offline_donation_enable_billing_fields_single_field_value', '_give_offline_donation_enable_billing_fields_single_field_value', 10, 3 );
1557
1558
1559
/**
1560
 * Set value for Donation Options --> Custom Amount.
1561
 *
1562
 * Backward compatibility:  set value by _give_custom_amount form meta field value if it's value is yes or no.
1563
 *
1564
 * @since  1.8
1565
 *
1566
 * @param  mixed $field_value Field Value.
1567
 * @param  array $field       Field args.
1568
 * @param  int   $postid      Form/Post ID.
1569
 *
1570
 * @return string
1571
 */
1572 View Code Duplication
function _give_custom_amount_field_value( $field_value, $field, $postid ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1573
	$custom_amount = give_get_meta( $postid, '_give_custom_amount', true );
1574
1575
	if ( in_array( $custom_amount, array( 'yes', 'no' ) ) ) {
1576
		$field_value = ( 'yes' === $custom_amount ? 'enabled' : 'disabled' );
1577
	}
1578
1579
	return $field_value;
1580
}
1581
1582
add_filter( '_give_custom_amount_field_value', '_give_custom_amount_field_value', 10, 3 );
1583
1584
1585
/**
1586
 * Set value for Donation Goal --> Donation Goal.
1587
 *
1588
 * Backward compatibility:  set value by _give_goal_option form meta field value if it's value is yes or no.
1589
 *
1590
 * @since  1.8
1591
 *
1592
 * @param  mixed $field_value Field Value.
1593
 * @param  array $field       Field args.
1594
 * @param  int   $postid      Form/Post ID.
1595
 *
1596
 * @return string
1597
 */
1598 View Code Duplication
function _give_goal_option_field_value( $field_value, $field, $postid ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1599
	$goal_option = give_get_meta( $postid, '_give_goal_option', true );
1600
1601
	if ( in_array( $goal_option, array( 'yes', 'no' ) ) ) {
1602
		$field_value = ( 'yes' === $goal_option ? 'enabled' : 'disabled' );
1603
	}
1604
1605
	return $field_value;
1606
}
1607
1608
add_filter( '_give_goal_option_field_value', '_give_goal_option_field_value', 10, 3 );
1609
1610
/**
1611
 * Set value for Donation Goal --> close Form.
1612
 *
1613
 * Backward compatibility:  set value by _give_close_form_when_goal_achieved form meta field value if it's value is yes
1614
 * or no.
1615
 *
1616
 * @since  1.8
1617
 *
1618
 * @param  mixed $field_value Field Value.
1619
 * @param  array $field       Field args.
1620
 * @param  int   $postid      Form/Post ID.
1621
 *
1622
 * @return string
1623
 */
1624 View Code Duplication
function _give_close_form_when_goal_achieved_value( $field_value, $field, $postid ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1625
	$close_form = give_get_meta( $postid, '_give_close_form_when_goal_achieved', true );
1626
1627
	if ( in_array( $close_form, array( 'yes', 'no' ) ) ) {
1628
		$field_value = ( 'yes' === $close_form ? 'enabled' : 'disabled' );
1629
	}
1630
1631
	return $field_value;
1632
}
1633
1634
add_filter( '_give_close_form_when_goal_achieved_field_value', '_give_close_form_when_goal_achieved_value', 10, 3 );
1635
1636
1637
/**
1638
 * Set value for Form display --> Guest Donation.
1639
 *
1640
 * Backward compatibility:  set value by _give_logged_in_only form meta field value if it's value is yes or no.
1641
 *
1642
 * @since  1.8
1643
 *
1644
 * @param  mixed $field_value Field Value.
1645
 * @param  array $field       Field args.
1646
 * @param  int   $postid      Form/Post ID.
1647
 *
1648
 * @return string
1649
 */
1650 View Code Duplication
function _give_logged_in_only_value( $field_value, $field, $postid ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1651
	$guest_donation = give_get_meta( $postid, '_give_logged_in_only', true );
1652
1653
	if ( in_array( $guest_donation, array( 'yes', 'no' ) ) ) {
1654
		$field_value = ( 'yes' === $guest_donation ? 'enabled' : 'disabled' );
1655
	}
1656
1657
	return $field_value;
1658
}
1659
1660
add_filter( '_give_logged_in_only_field_value', '_give_logged_in_only_value', 10, 3 );
1661
1662
/**
1663
 * Set value for Offline Donations --> Offline Donations.
1664
 *
1665
 * Backward compatibility:  set value by _give_customize_offline_donations form meta field value if it's value is yes
1666
 * or no.
1667
 *
1668
 * @since  1.8
1669
 *
1670
 * @param  mixed $field_value Field Value.
1671
 * @param  array $field       Field args.
1672
 * @param  int   $postid      Form/Post ID.
1673
 *
1674
 * @return string
1675
 */
1676 View Code Duplication
function _give_customize_offline_donations_value( $field_value, $field, $postid ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1677
	$customize_offline_text = give_get_meta( $postid, '_give_customize_offline_donations', true );
1678
1679
	if ( in_array( $customize_offline_text, array( 'yes', 'no' ) ) ) {
1680
		$field_value = ( 'yes' === $customize_offline_text ? 'enabled' : 'disabled' );
1681
	}
1682
1683
	return $field_value;
1684
}
1685
1686
add_filter( '_give_customize_offline_donations_field_value', '_give_customize_offline_donations_value', 10, 3 );
1687
1688
1689
/**
1690
 * Set repeater field id for multi donation form.
1691
 *
1692
 * @since 1.8
1693
 *
1694
 * @param int   $field_id
1695
 * @param array $field
1696
 * @param array $fields
1697
 * @param bool  $default
1698
 *
1699
 * @return mixed
1700
 */
1701
function _give_set_multi_level_repeater_field_id( $field_id, $field, $fields, $default ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field_id 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...
1702
	$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1703
	$field_id        = "{$fields['id']}[{$row_placeholder}][{$field['id']}][level_id]";
1704
1705
	return $field_id;
1706
}
1707
1708
add_filter( 'give_get_repeater_field__give_id_id', '_give_set_multi_level_repeater_field_id', 10, 4 );
1709
1710
/**
1711
 * Set repeater field value for multi donation form.
1712
 *
1713
 * @since 1.8
1714
 *
1715
 * @param string $field_value
1716
 * @param array  $field
1717
 * @param array  $field_group
1718
 * @param array  $fields
1719
 *
1720
 * @return mixed
1721
 */
1722
function _give_set_multi_level_repeater_field_value( $field_value, $field, $field_group, $fields ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field_value 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 $fields 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...
1723
	$field_value = $field_group[ $field['id'] ]['level_id'];
1724
1725
	return $field_value;
1726
}
1727
1728
add_filter( 'give_get_repeater_field__give_id_value', '_give_set_multi_level_repeater_field_value', 10, 4 );
1729
1730
/**
1731
 * Set default value for _give_id field.
1732
 *
1733
 * @since 1.8
1734
 *
1735
 * @param $field
1736
 *
1737
 * @return string
1738
 */
1739
function _give_set_field_give_id_default_value( $field ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
1740
	return 0;
1741
}
1742
1743
add_filter( 'give_default_field_group_field__give_id_value', '_give_set_field_give_id_default_value' );
1744
1745
/**
1746
 * Set default value for _give_default field.
1747
 *
1748
 * @since 1.8
1749
 *
1750
 * @param $field
1751
 *
1752
 * @return string
1753
 */
1754
function _give_set_field_give_default_default_value( $field ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field 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...
1755
	return 'default';
1756
}
1757
1758
add_filter( 'give_default_field_group_field__give_default_value', '_give_set_field_give_default_default_value' );
1759
1760
/**
1761
 * Set repeater field editor id for field type wysiwyg.
1762
 *
1763
 * @since 1.8
1764
 *
1765
 * @param $field_name
1766
 * @param $field
1767
 *
1768
 * @return string
1769
 */
1770
function give_repeater_field_set_editor_id( $field_name, $field ) {
1771
	if ( isset( $field['repeatable_field_id'] ) && 'wysiwyg' == $field['type'] ) {
1772
		$field_name = '_give_repeater_' . uniqid() . '_wysiwyg';
1773
	}
1774
1775
	return $field_name;
1776
}
1777
1778
add_filter( 'give_get_field_name', 'give_repeater_field_set_editor_id', 10, 2 );
1779
1780
/**
1781
 * Output Donation form radio input box.
1782
 *
1783
 * @since  2.1.3
1784
 *
1785
 * @param  array $field {
1786
 *                              Optional. Array of radio field arguments.
1787
 *
1788
 * @type string $id Field ID. Default ''.
1789
 * @type string $style CSS style for input field. Default ''.
1790
 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
1791
 * @type string $value Value of input field. Default ''.
1792
 * @type string $name Name of input field. Default ''.
1793
 * @type string $description Description of input field. Default ''.
1794
 * @type array $attributes List of attributes of input field. Default array().
1795
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
1796
 *                                               => '****' )
1797
 * @type array $options List of options. Default array().
1798
 *                                               for example: 'options' => array( 'enable' => 'Enable', 'disable' =>
1799
 *                                               'Disable' )
1800
 * }
1801
 * @return void
1802
 */
1803
function give_donation_form_goal( $field ) {
1804
	global $thepostid, $post;
1805
1806
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
1807
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
1808
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
1809
	$field['value']         = give_get_field_value( $field, $thepostid );
1810
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
1811
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1812
1813
	printf(
1814
		'<fieldset class="give-field-wrap %s_field %s">',
1815
		esc_attr( $field['id'] ),
1816
		esc_attr( $field['wrapper_class'] )
1817
	);
1818
1819
	printf(
1820
		'<span class="give-field-label">%s</span>',
1821
		esc_html( $field['name'] )
1822
	);
1823
1824
	printf(
1825
		'<legend class="screen-reader-text">%s</legend>',
1826
		esc_html( $field['name'] )
1827
	);
1828
	?>
1829
1830
    <ul class="give-radios">
1831
		<?php
1832
		foreach ( $field['options'] as $key => $value ) {
1833
			$attributes = empty( $field['attributes'] ) ? '' : give_get_attribute_str( $field['attributes'] );
1834
			printf(
1835
				'<li><label><input name="%s" value="%s" type="radio" style="%s" %s %s /> %s </label></li>',
1836
				give_get_field_name( $field ),
1837
				esc_attr( $key ),
1838
				esc_attr( $field['style'] ),
1839
				checked( esc_attr( $field['value'] ), esc_attr( $key ), false ),
1840
				$attributes,
1841
				esc_html( $value )
1842
			);
1843
		}
1844
		?>
1845
    </ul>
1846
1847
	<?php
1848
	/**
1849
	 * Action to add HTML after donation form radio button is display and before description.
1850
	 *
1851
	 * @since 2.1.3
1852
	 *
1853
	 * @param array $field Array of radio field arguments.
1854
	 */
1855
	do_action( 'give_donation_form_goal_before_description', $field );
1856
1857
	echo give_get_field_description( $field );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_field_description'
Loading history...
1858
1859
	echo '</fieldset>';
1860
}
1861