Completed
Push — release/2.1 ( c669cb...a27eea )
by Ravinder
995:32 queued 989:33
created

give-metabox-functions.php ➔ give_donation_limit()   F

Complexity

Conditions 15
Paths 560

Size

Total Lines 105

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
nc 560
nop 1
dl 0
loc 105
rs 1.8888
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
		default:
83
84
			if (
85
				array_key_exists( 'callback', $field )
86
				&& ! empty( $field['callback'] )
87
			) {
88
				$func_name = $field['callback'];
89
			} else {
90
				$func_name = "{$func_name_prefix}_{$field['type']}";
91
			}
92
	}
93
94
	/**
95
	 * Filter the metabox setting render function
96
	 *
97
	 * @since 1.8
98
	 */
99
	$func_name = apply_filters( 'give_get_field_callback', $func_name, $field );
100
101
	// Exit if not any function exist.
102
	// Check if render callback exist or not.
103
	if ( empty( $func_name ) ) {
104
		return false;
105
	} elseif ( is_string( $func_name ) && ! function_exists( "$func_name" ) ) {
106
		return false;
107
	} elseif ( is_array( $func_name ) && ! method_exists( $func_name[0], "$func_name[1]" ) ) {
108
		return false;
109
	}
110
111
	return $func_name;
112
}
113
114
/**
115
 * This function adds backward compatibility to render cmb2 type field type.
116
 *
117
 * @since  1.8
118
 *
119
 * @param  array $field Field argument array.
120
 *
121
 * @return bool
122
 */
123
function give_render_field( $field ) {
124
125
	// Check if render callback exist or not.
126
	if ( ! ( $func_name = give_get_field_callback( $field ) ) ) {
127
		return false;
128
	}
129
130
	// CMB2 compatibility: Push all classes to attributes's class key
131
	if ( empty( $field['class'] ) ) {
132
		$field['class'] = '';
133
	}
134
135
	if ( empty( $field['attributes']['class'] ) ) {
136
		$field['attributes']['class'] = '';
137
	}
138
139
	$field['attributes']['class'] = trim( "give-field {$field['attributes']['class']} give-{$field['type']} {$field['class']}" );
140
	unset( $field['class'] );
141
142
	// CMB2 compatibility: Set wrapper class if any.
143 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...
144
		$field['wrapper_class'] = $field['row_classes'];
145
		unset( $field['row_classes'] );
146
	}
147
148
	// Set field params on basis of cmb2 field name.
149
	switch ( $field['type'] ) {
150
		case 'radio_inline':
151
			if ( empty( $field['wrapper_class'] ) ) {
152
				$field['wrapper_class'] = '';
153
			}
154
			$field['wrapper_class'] .= ' give-inline-radio-fields';
155
156
			break;
157
158
		case 'text':
159
		case 'text-medium':
160
		case 'text_medium':
161
		case 'text-small' :
162
		case 'text_small' :
163
			// CMB2 compatibility: Set field type to text.
164
			$field['type'] = isset( $field['attributes']['type'] ) ? $field['attributes']['type'] : 'text';
165
166
			// CMB2 compatibility: Set data type to price.
167
			if (
168
				empty( $field['data_type'] )
169
				&& ! empty( $field['attributes']['class'] )
170
				&& (
171
					false !== strpos( $field['attributes']['class'], 'money' )
172
					|| false !== strpos( $field['attributes']['class'], 'amount' )
173
				)
174
			) {
175
				$field['data_type'] = 'decimal';
176
			}
177
			break;
178
179
		case 'levels_id':
180
			$field['type'] = 'hidden';
181
			break;
182
183
		case 'colorpicker' :
184
			$field['type']  = 'text';
185
			$field['class'] = 'give-colorpicker';
186
			break;
187
188
		case 'give_default_radio_inline':
189
			$field['type']    = 'radio';
190
			$field['options'] = array(
191
				'default' => __( 'Default' ),
192
			);
193
			break;
194
195
		case 'donation_limit':
196
			$field['type']  = 'donation_limit';
197
			break;
198
	}
199
200
	// CMB2 compatibility: Add support to define field description by desc & description param.
201
	// We encourage you to use description param.
202
	$field['description'] = ( ! empty( $field['description'] )
203
		? $field['description']
204
		: ( ! empty( $field['desc'] ) ? $field['desc'] : '' ) );
205
206
	// Call render function.
207
	if ( is_array( $func_name ) ) {
208
		$func_name[0]->{$func_name[1]}( $field );
209
	} else {
210
		$func_name( $field );
211
	}
212
213
	return true;
214
}
215
216
/**
217
 * Output a text input box.
218
 *
219
 * @since  1.8
220
 *
221
 * @param  array $field         {
222
 *                              Optional. Array of text input field arguments.
223
 *
224
 * @type string  $id            Field ID. Default ''.
225
 * @type string  $style         CSS style for input field. Default ''.
226
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
227
 * @type string  $value         Value of input field. Default ''.
228
 * @type string  $name          Name of input field. Default ''.
229
 * @type string  $type          Type of input field. Default 'text'.
230
 * @type string  $before_field  Text/HTML to add before input field. Default ''.
231
 * @type string  $after_field   Text/HTML to add after input field. Default ''.
232
 * @type string  $data_type     Define data type for value of input to filter it properly. Default ''.
233
 * @type string  $description   Description of input field. Default ''.
234
 * @type array   $attributes    List of attributes of input field. Default array().
235
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
236
 *                                               => '****' )
237
 * }
238
 * @return void
239
 */
240
function give_text_input( $field ) {
241
	global $thepostid, $post;
242
243
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
244
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
245
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
246
	$field['value']         = give_get_field_value( $field, $thepostid );
247
	$field['type']          = isset( $field['type'] ) ? $field['type'] : 'text';
248
	$field['before_field']  = '';
249
	$field['after_field']   = '';
250
	$data_type              = empty( $field['data_type'] ) ? '' : $field['data_type'];
251
252
	switch ( $data_type ) {
253
		case 'price' :
254
			$field['value'] = ( ! empty( $field['value'] ) ? give_format_amount( give_maybe_sanitize_amount( $field['value'] ), array( 'sanitize' => false ) ) : $field['value'] );
255
256
			$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>' : '' );
257
			$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>' : '' );
258
			break;
259
260
		case 'decimal' :
261
			$field['attributes']['class'] .= ' give_input_decimal';
262
			$field['value']               = ( ! empty( $field['value'] ) ? give_format_decimal( give_maybe_sanitize_amount( $field['value'] ), false, false ) : $field['value'] );
263
			break;
264
265
		default :
266
			break;
267
	}
268
269
	?>
270
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
271
	<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...
272
	<?php echo $field['before_field']; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$field'
Loading history...
273
	<input
274
			type="<?php echo esc_attr( $field['type'] ); ?>"
275
			style="<?php echo esc_attr( $field['style'] ); ?>"
276
			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...
277
			id="<?php echo esc_attr( $field['id'] ); ?>"
278
			value="<?php echo esc_attr( $field['value'] ); ?>"
279
		<?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...
280
	/>
281
	<?php echo $field['after_field']; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$field'
Loading history...
282
	<?php
283
	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...
284
	echo '</p>';
285
}
286
287
/**
288
 * Give range slider field.
289
 * Note: only for internal logic
290
 *
291
 * @since 2.1
292
 *
293
 * @param  array $field         {
294
 *                              Optional. Array of text input field arguments.
295
 *
296
 * @type string  $id            Field ID. Default ''.
297
 * @type string  $style         CSS style for input field. Default ''.
298
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
299
 * @type string  $value         Value of input field. Default ''.
300
 * @type string  $name          Name of input field. Default ''.
301
 * @type string  $type          Type of input field. Default 'text'.
302
 * @type string  $before_field  Text/HTML to add before input field. Default ''.
303
 * @type string  $after_field   Text/HTML to add after input field. Default ''.
304
 * @type string  $data_type     Define data type for value of input to filter it properly. Default ''.
305
 * @type string  $description   Description of input field. Default ''.
306
 * @type array   $attributes    List of attributes of input field. Default array().
307
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
308
 *                                               => '****' )
309
 * }
310
 *
311
 * @return void
312
 */
313
function give_donation_limit( $field ) {
314
	global $thepostid, $post;
315
316
	// Get Give donation form ID.
317
	$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
318
319
	// Default arguments.
320
	$default_options = array(
321
		'style'         => '',
322
		'wrapper_class' => '',
323
		'value'         => give_get_field_value( $field, $thepostid ),
324
		'data_type'     => 'decimal',
325
		'before_field'  => '',
326
		'after_field'   => '',
327
	);
328
329
	// Field options.
330
	$field['options'] = ! empty( $field['options'] ) ? $field['options'] : array();
331
332
	// Default field option arguments.
333
	$field['options'] = wp_parse_args( $field['options'], array(
334
			'display_label' => '',
335
			'minimum'       => 1.00,
336
			'maximum'       => 999999.99,
337
		)
338
	);
339
340
	// Set default field options.
341
	$field_options = wp_parse_args( $field, $default_options );
342
343
	// Get default minimum value, if empty.
344
	$field_options['value']['minimum'] = ! empty( $field_options['value']['minimum'] )
345
		? $field_options['value']['minimum']
346
		: $field_options['options']['minimum'];
347
348
	// Get default maximum value, if empty.
349
	$field_options['value']['maximum'] = ! empty( $field_options['value']['maximum'] )
350
		? $field_options['value']['maximum']
351
		: $field_options['options']['maximum'];
352
	?>
353
	<p class="give-field-wrap <?php echo esc_attr( $field_options['id'] ); ?>_field <?php echo esc_attr( $field_options['wrapper_class'] ); ?>">
354
	<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...
355
	<span class="give_donation_limit_display">
356
		<?php
357
		foreach ( $field_options['value'] as $amount_range => $amount_value ) {
358
359
			switch ( $field_options['data_type'] ) {
360
				case 'price' :
361
					$currency_position = give_get_option( 'currency_position', 'before' );
362
					$price_field_labels     = 'minimum' === $amount_range ? __( 'Minimum amount', 'give' ) : __( 'Maximum amount', 'give' );
363
364
					$tooltip_html = array(
365
						'before' => Give()->tooltips->render_span( array(
366
							'label'       => $price_field_labels,
367
							'tag_content' => sprintf( '<span class="give-money-symbol give-money-symbol-before">%s</span>', give_currency_symbol() ),
368
						) ),
369
						'after'  => Give()->tooltips->render_span( array(
370
							'label'       => $price_field_labels,
371
							'tag_content' => sprintf( '<span class="give-money-symbol give-money-symbol-after">%s</span>', give_currency_symbol() ),
372
						) ),
373
					);
374
375
					$before_html = ! empty( $field_options['before_field'] )
376
						? $field_options['before_field']
377
						: ( 'before' === $currency_position ? $tooltip_html['before'] : '' );
378
379
					$after_html = ! empty( $field_options['after_field'] )
380
						? $field_options['after_field']
381
						: ( 'after' === $currency_position ? $tooltip_html['after'] : '' );
382
383
					$field_options['attributes']['class']    .= ' give-text_small';
384
					$field_options['value'][ $amount_range ] = give_maybe_sanitize_amount( $amount_value );
385
					break;
386
				case 'decimal' :
387
					$field_options['attributes']['class']    .= ' give_input_decimal give-text_small';
388
					$field_options['value'][ $amount_range ] = $amount_value;
389
					break;
390
			}
391
392
			$amount = give_format_amount( give_maybe_sanitize_amount( $field_options['value'][ $amount_range ] ), array( 'sanitize' => false ) );
393
394
			echo '<span class=give-minmax-wrap>';
395
			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 ) );
396
397
			echo isset( $before_html ) ? $before_html : '';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'isset'
Loading history...
398
			?>
399
			<input
400
					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...
401
					type="text"
402
					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...
403
					data-range_type="<?php echo esc_attr( $amount_range ); ?>"
404
					value="<?php echo esc_attr( $amount ); ?>"
405
					placeholder="<?php echo $field_options['options'][ $amount_range ]; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$field_options'
Loading history...
406
				<?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...
407
			/>
408
			<?php
409
			echo isset( $after_html ) ? $after_html : '';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'isset'
Loading history...
410
			echo '</span>';
411
		}
412
		?>
413
	</span>
414
		<?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...
415
	</p>
416
	<?php
417
}
418
419
/**
420
 * Output a hidden input box.
421
 *
422
 * @since  1.8
423
 *
424
 * @param  array $field      {
425
 *                           Optional. Array of hidden text input field arguments.
426
 *
427
 * @type string  $id         Field ID. Default ''.
428
 * @type string  $value      Value of input field. Default ''.
429
 * @type string  $name       Name of input field. Default ''.
430
 * @type string  $type       Type of input field. Default 'text'.
431
 * @type array   $attributes List of attributes of input field. Default array().
432
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
433
 *                                               => '****' )
434
 * }
435
 * @return void
436
 */
437
function give_hidden_input( $field ) {
438
	global $thepostid, $post;
439
440
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
441
	$field['value'] = give_get_field_value( $field, $thepostid );
442
443
	// Custom attribute handling
444
	$custom_attributes = array();
445
446 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...
447
448
		foreach ( $field['attributes'] as $attribute => $value ) {
449
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
450
		}
451
	}
452
	?>
453
454
	<input
455
			type="hidden"
456
			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...
457
			id="<?php echo esc_attr( $field['id'] ); ?>"
458
			value="<?php echo esc_attr( $field['value'] ); ?>"
459
		<?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...
460
	/>
461
	<?php
462
}
463
464
/**
465
 * Output a textarea input box.
466
 *
467
 * @since  1.8
468
 * @since  1.8
469
 *
470
 * @param  array $field         {
471
 *                              Optional. Array of textarea input field arguments.
472
 *
473
 * @type string  $id            Field ID. Default ''.
474
 * @type string  $style         CSS style for input field. Default ''.
475
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
476
 * @type string  $value         Value of input field. Default ''.
477
 * @type string  $name          Name of input field. Default ''.
478
 * @type string  $description   Description of input field. Default ''.
479
 * @type array   $attributes    List of attributes of input field. Default array().
480
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
481
 *                                               => '****' )
482
 * }
483
 * @return void
484
 */
485
function give_textarea_input( $field ) {
486
	global $thepostid, $post;
487
488
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
489
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
490
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
491
	$field['value']         = give_get_field_value( $field, $thepostid );
492
493
	?>
494
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
495
	<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...
496
	<textarea
497
			style="<?php echo esc_attr( $field['style'] ); ?>"
498
			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...
499
			id="<?php echo esc_attr( $field['id'] ); ?>"
500
			rows="10"
501
			cols="20"
502
		<?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...
503
	><?php echo esc_textarea( $field['value'] ); ?></textarea>
504
	<?php
505
	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...
506
	echo '</p>';
507
}
508
509
/**
510
 * Output a wysiwyg.
511
 *
512
 * @since  1.8
513
 *
514
 * @param  array $field         {
515
 *                              Optional. Array of WordPress editor field arguments.
516
 *
517
 * @type string  $id            Field ID. Default ''.
518
 * @type string  $style         CSS style for input field. Default ''.
519
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
520
 * @type string  $value         Value of input field. Default ''.
521
 * @type string  $name          Name of input field. Default ''.
522
 * @type string  $description   Description of input field. Default ''.
523
 * @type array   $attributes    List of attributes of input field. Default array().
524
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
525
 *                                               => '****' )
526
 * }
527
 * @return void
528
 */
529
function give_wysiwyg( $field ) {
530
	global $thepostid, $post;
531
532
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
533
	$field['value']         = give_get_field_value( $field, $thepostid );
534
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
535
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
536
537
	$field['unique_field_id'] = give_get_field_name( $field );
538
	$editor_attributes        = array(
539
		'textarea_name' => isset( $field['repeatable_field_id'] ) ? $field['repeatable_field_id'] : $field['id'],
540
		'textarea_rows' => '10',
541
		'editor_css'    => esc_attr( $field['style'] ),
542
		'editor_class'  => $field['attributes']['class'],
543
	);
544
	$data_wp_editor           = ' data-wp-editor="' . base64_encode( json_encode( array(
545
			$field['value'],
546
			$field['unique_field_id'],
547
			$editor_attributes,
548
		) ) ) . '"';
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...
549
	$data_wp_editor           = isset( $field['repeatable_field_id'] ) ? $data_wp_editor : '';
550
551
	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...
552
553
	wp_editor(
554
		$field['value'],
555
		$field['unique_field_id'],
556
		$editor_attributes
557
	);
558
559
	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...
560
	echo '</div>';
561
}
562
563
/**
564
 * Output a checkbox input box.
565
 *
566
 * @since  1.8
567
 *
568
 * @param  array $field         {
569
 *                              Optional. Array of checkbox field arguments.
570
 *
571
 * @type string  $id            Field ID. Default ''.
572
 * @type string  $style         CSS style for input field. Default ''.
573
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
574
 * @type string  $value         Value of input field. Default ''.
575
 * @type string  $cbvalue       Checkbox value. Default 'on'.
576
 * @type string  $name          Name of input field. Default ''.
577
 * @type string  $description   Description of input field. Default ''.
578
 * @type array   $attributes    List of attributes of input field. Default array().
579
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
580
 *                                               => '****' )
581
 * }
582
 * @return void
583
 */
584
function give_checkbox( $field ) {
585
	global $thepostid, $post;
586
587
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
588
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
589
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
590
	$field['value']         = give_get_field_value( $field, $thepostid );
591
	$field['cbvalue']       = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'on';
592
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
593
	?>
594
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
595
	<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...
596
	<input
597
			type="checkbox"
598
			style="<?php echo esc_attr( $field['style'] ); ?>"
599
			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...
600
			id="<?php echo esc_attr( $field['id'] ); ?>"
601
			value="<?php echo esc_attr( $field['cbvalue'] ); ?>"
602
		<?php echo checked( $field['value'], $field['cbvalue'], false ); ?>
603
		<?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...
604
	/>
605
	<?php
606
	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...
607
	echo '</p>';
608
}
609
610
/**
611
 * Output a select input box.
612
 *
613
 * @since  1.8
614
 *
615
 * @param  array $field         {
616
 *                              Optional. Array of select field arguments.
617
 *
618
 * @type string  $id            Field ID. Default ''.
619
 * @type string  $style         CSS style for input field. Default ''.
620
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
621
 * @type string  $value         Value of input field. Default ''.
622
 * @type string  $name          Name of input field. Default ''.
623
 * @type string  $description   Description of input field. Default ''.
624
 * @type array   $attributes    List of attributes of input field. Default array().
625
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
626
 *                                               => '****' )
627
 * @type array   $options       List of options. Default array().
628
 *                                               for example: 'options' => array( '' => 'None', 'yes' => 'Yes' )
629
 * }
630
 * @return void
631
 */
632
function give_select( $field ) {
633
	global $thepostid, $post;
634
635
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
636
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
637
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
638
	$field['value']         = give_get_field_value( $field, $thepostid );
639
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
640
	?>
641
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
642
	<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...
643
	<select
644
	id="<?php echo esc_attr( $field['id'] ); ?>"
645
	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...
646
	style="<?php echo esc_attr( $field['style'] ) ?>"
647
	<?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...
648
	>
649
	<?php
650
	foreach ( $field['options'] as $key => $value ) {
651
		echo '<option value="' . esc_attr( $key ) . '" ' . selected( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
652
	}
653
	echo '</select>';
654
	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...
655
	echo '</p>';
656
}
657
658
/**
659
 * Output a radio input box.
660
 *
661
 * @since  1.8
662
 *
663
 * @param  array $field         {
664
 *                              Optional. Array of radio field arguments.
665
 *
666
 * @type string  $id            Field ID. Default ''.
667
 * @type string  $style         CSS style for input field. Default ''.
668
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
669
 * @type string  $value         Value of input field. Default ''.
670
 * @type string  $name          Name of input field. Default ''.
671
 * @type string  $description   Description of input field. Default ''.
672
 * @type array   $attributes    List of attributes of input field. Default array().
673
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
674
 *                                               => '****' )
675
 * @type array   $options       List of options. Default array().
676
 *                                               for example: 'options' => array( 'enable' => 'Enable', 'disable' =>
677
 *                                               'Disable' )
678
 * }
679
 * @return void
680
 */
681
function give_radio( $field ) {
682
	global $thepostid, $post;
683
684
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
685
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
686
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
687
	$field['value']         = give_get_field_value( $field, $thepostid );
688
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
689
690
	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">';
691
692
	foreach ( $field['options'] as $key => $value ) {
693
694
		echo '<li><label><input
695
				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...
696
				value="' . esc_attr( $key ) . '"
697
				type="radio"
698
				style="' . esc_attr( $field['style'] ) . '"
699
				' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . ' '
700
		     . 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...
701
				/> ' . esc_html( $value ) . '</label>
702
		</li>';
703
	}
704
	echo '</ul>';
705
706
	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...
707
	echo '</fieldset>';
708
}
709
710
/**
711
 * Output a colorpicker.
712
 *
713
 * @since  1.8
714
 *
715
 * @param  array $field         {
716
 *                              Optional. Array of colorpicker field arguments.
717
 *
718
 * @type string  $id            Field ID. Default ''.
719
 * @type string  $style         CSS style for input field. Default ''.
720
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
721
 * @type string  $value         Value of input field. Default ''.
722
 * @type string  $name          Name of input field. Default ''.
723
 * @type string  $description   Description of input field. Default ''.
724
 * @type array   $attributes    List of attributes of input field. Default array().
725
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
726
 *                                               => '****' )
727
 * }
728
 * @return void
729
 */
730
function give_colorpicker( $field ) {
731
	global $thepostid, $post;
732
733
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
734
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
735
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
736
	$field['value']         = give_get_field_value( $field, $thepostid );
737
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
738
	$field['type']          = 'text';
739
	?>
740
	<p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
741
	<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...
742
	<input
743
			type="<?php echo esc_attr( $field['type'] ); ?>"
744
			style="<?php echo esc_attr( $field['style'] ); ?>"
745
			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...
746
			id="' . esc_attr( $field['id'] ) . '" value="<?php echo esc_attr( $field['value'] ); ?>"
747
		<?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...
748
	/>
749
	<?php
750
	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...
751
	echo '</p>';
752
}
753
754
/**
755
 * Output a file upload field.
756
 *
757
 * @since  1.8.9
758
 *
759
 * @param array $field
760
 */
761
function give_file( $field ) {
762
	give_media( $field );
763
}
764
765
766
/**
767
 * Output a media upload field.
768
 *
769
 * @since  1.8
770
 *
771
 * @param array $field
772
 */
773
function give_media( $field ) {
774
	global $thepostid, $post;
775
776
	$thepostid    = empty( $thepostid ) ? $post->ID : $thepostid;
777
	$button_label = sprintf( __( 'Add or Upload %s', 'give' ), ( 'file' === $field['type'] ? __( 'File', 'give' ) : __( 'Image', 'give' ) ) );
778
779
	$field['style']               = isset( $field['style'] ) ? $field['style'] : '';
780
	$field['wrapper_class']       = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
781
	$field['value']               = give_get_field_value( $field, $thepostid );
782
	$field['name']                = isset( $field['name'] ) ? $field['name'] : $field['id'];
783
	$field['attributes']['class'] = "{$field['attributes']['class']} give-text-medium";
784
785
	// Allow developer to save attachment ID or attachment url as metadata.
786
	$field['fvalue'] = isset( $field['fvalue'] ) ? $field['fvalue'] : 'url';
787
788
	$allow_media_preview_tags = array( 'jpg', 'jpeg', 'png', 'gif', 'ico' );
789
	$preview_image_src        = $field['value'] ? ( 'id' === $field['fvalue'] ? wp_get_attachment_url( $field['value'] ) : $field['value'] ) : '#';
790
	$preview_image_extension  = $preview_image_src ? pathinfo( $preview_image_src, PATHINFO_EXTENSION ) : '';
791
	$is_show_preview          = in_array( $preview_image_extension, $allow_media_preview_tags );
792
	?>
793
	<fieldset class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
794
		<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...
795
		<input
796
				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...
797
				id="<?php echo esc_attr( $field['id'] ); ?>"
798
				type="text"
799
				value="<?php echo $field['value']; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$field'
Loading history...
800
				style="<?php echo esc_attr( $field['style'] ); ?>"
801
			<?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...
802
		/>&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...
803
		<?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...
804
		<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...
805
			<span class="give-delete-image-thumb dashicons dashicons-no-alt"></span>
806
			<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...
807
		</div>
808
	</fieldset>
809
	<?php
810
}
811
812
/**
813
 * Output a select field with payment options list.
814
 *
815
 * @since  1.8
816
 *
817
 * @param  array $field
818
 *
819
 * @return void
820
 */
821
function give_default_gateway( $field ) {
822
	global $thepostid, $post;
823
824
	// get all active payment gateways.
825
	$gateways         = give_get_enabled_payment_gateways( $thepostid );
826
	$field['options'] = array();
827
828
	// Set field option value.
829
	if ( ! empty( $gateways ) ) {
830
		foreach ( $gateways as $key => $option ) {
831
			$field['options'][ $key ] = $option['admin_label'];
832
		}
833
	}
834
835
	// Add a field to the Give Form admin single post view of this field
836
	if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
837
		$field['options'] = array_merge( array( 'global' => esc_html__( 'Global Default', 'give' ) ), $field['options'] );
838
	}
839
840
	// Render select field.
841
	give_select( $field );
842
}
843
844
/**
845
 * Output the documentation link.
846
 *
847
 * @since  1.8
848
 *
849
 * @param  array $field      {
850
 *                           Optional. Array of customizable link attributes.
851
 *
852
 * @type string  $name       Name of input field. Default ''.
853
 * @type string  $type       Type of input field. Default 'text'.
854
 * @type string  $url        Value to be passed as a link. Default 'https://givewp.com/documentation'.
855
 * @type string  $title      Value to be passed as text of link. Default 'Documentation'.
856
 * @type array   $attributes List of attributes of input field. Default array().
857
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
858
 *                                               => '****' )
859
 * }
860
 * @return void
861
 */
862
863
function give_docs_link( $field ) {
864
	$field['url']   = isset( $field['url'] ) ? $field['url'] : 'https://givewp.com/documentation';
865
	$field['title'] = isset( $field['title'] ) ? $field['title'] : 'Documentation';
866
867
	echo '<p class="give-docs-link"><a href="' . esc_url( $field['url'] )
868
	     . '" target="_blank">'
869
	     . 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...
870
	     . '<span class="dashicons dashicons-editor-help"></span></a></p>';
871
}
872
873
874
/**
875
 * Output preview buttons.
876
 *
877
 * @since 2.0
878
 *
879
 * @param $field
880
 */
881
function give_email_preview_buttons( $field ) {
882
	/* @var WP_Post $post */
883
	global $post;
884
885
	$field_id = str_replace( array( '_give_', '_preview_buttons' ), '', $field['id'] );
886
887
	ob_start();
888
889
	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...
890
891
	echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
892
		'<a href="%1$s" class="button-secondary" target="_blank">%2$s</a>',
893
		wp_nonce_url(
894
			add_query_arg(
895
				array(
896
					'give_action' => 'preview_email',
897
					'email_type'  => $field_id,
898
					'form_id'     => $post->ID,
899
				),
900
				home_url()
901
			), 'give-preview-email'
902
		),
903
		$field['name']
904
	);
905
906
	echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
907
		' <a href="%1$s" aria-label="%2$s" class="button-secondary">%3$s</a>',
908
		wp_nonce_url(
909
			add_query_arg(
910
				array(
911
					'give_action'  => 'send_preview_email',
912
					'email_type'   => $field_id,
913
					'give-message' => 'sent-test-email',
914
					'form_id'      => $post->ID,
915
				)
916
			), '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...
917
		esc_attr__( 'Send Test Email.', 'give' ),
918
		esc_html__( 'Send Test Email', 'give' )
919
	);
920
921
	if ( ! empty( $field['description'] ) ) {
922
		echo '<span class="give-field-description">' . wp_kses_post( $field['desc'] ) . '</span>';
923
	}
924
925
	echo '</p>';
926
927
	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...
928
}
929
930
/**
931
 * Get setting field value.
932
 *
933
 * Note: Use only for single post, page or custom post type.
934
 *
935
 * @since  1.8
936
 * @since  2.1 Added support for donation_limit.
937
 *
938
 * @param  array $field
939
 * @param  int   $postid
940
 *
941
 * @return mixed
942
 */
943
function give_get_field_value( $field, $postid ) {
944
	if ( isset( $field['attributes']['value'] ) ) {
945
		return $field['attributes']['value'];
946
	}
947
948
	// If field is range slider.
949
	if ( 'donation_limit' === $field['type'] ) {
950
951
		// Get minimum value.
952
		$minimum = give_get_meta( $postid, $field['id'] . '_minimum', true );
953
954
		// Give < 2.1
955
		if ( '_give_custom_amount_range' === $field['id'] && empty( $minimum ) ) {
956
			$minimum = give_get_meta( $postid, '_give_custom_amount_minimum', true );
957
		}
958
959
		$field_value = array(
960
			'minimum' => $minimum,
961
			'maximum' => give_get_meta( $postid, $field['id'] . '_maximum', true ),
962
		);
963
	} else {
964
		// Get value from db.
965
		$field_value = give_get_meta( $postid, $field['id'], true );
966
	}
967
968
	/**
969
	 * Filter the field value before apply default value.
970
	 *
971
	 * @since 1.8
972
	 *
973
	 * @param mixed $field_value Field value.
974
	 */
975
	$field_value = apply_filters( "{$field['id']}_field_value", $field_value, $field, $postid );
976
977
	// Set default value if no any data saved to db.
978
	if ( ! $field_value && isset( $field['default'] ) ) {
979
		$field_value = $field['default'];
980
	}
981
982
	return $field_value;
983
}
984
985
986
/**
987
 * Get field description html.
988
 *
989
 * @since 1.8
990
 *
991
 * @param $field
992
 *
993
 * @return string
994
 */
995
function give_get_field_description( $field ) {
996
	$field_desc_html = '';
997
	$description     = '';
998
999
	// Check for both `description` and `desc`.
1000
	if ( isset( $field['description'] ) ) {
1001
		$description = $field['description'];
1002
	} elseif ( isset( $field['desc'] ) ) {
1003
		$description = $field['desc'];
1004
	}
1005
1006
	// Set if there is a description.
1007
	if ( ! empty( $description ) ) {
1008
		$field_desc_html = '<span class="give-field-description">' . wp_kses_post( $description ) . '</span>';
1009
	}
1010
1011
	return $field_desc_html;
1012
}
1013
1014
1015
/**
1016
 * Get repeater field value.
1017
 *
1018
 * Note: Use only for single post, page or custom post type.
1019
 *
1020
 * @since  1.8
1021
 *
1022
 * @param array $field
1023
 * @param array $field_group
1024
 * @param array $fields
1025
 *
1026
 * @return string
1027
 */
1028
function give_get_repeater_field_value( $field, $field_group, $fields ) {
1029
	$field_value = ( isset( $field_group[ $field['id'] ] ) ? $field_group[ $field['id'] ] : '' );
1030
1031
	/**
1032
	 * Filter the specific repeater field value
1033
	 *
1034
	 * @since 1.8
1035
	 *
1036
	 * @param string $field_id
1037
	 */
1038
	$field_value = apply_filters( "give_get_repeater_field_{$field['id']}_value", $field_value, $field, $field_group, $fields );
1039
1040
	/**
1041
	 * Filter the repeater field value
1042
	 *
1043
	 * @since 1.8
1044
	 *
1045
	 * @param string $field_id
1046
	 */
1047
	$field_value = apply_filters( 'give_get_repeater_field_value', $field_value, $field, $field_group, $fields );
1048
1049
	return $field_value;
1050
}
1051
1052
/**
1053
 * Get repeater field id.
1054
 *
1055
 * Note: Use only for single post, page or custom post type.
1056
 *
1057
 * @since  1.8
1058
 *
1059
 * @param array    $field
1060
 * @param array    $fields
1061
 * @param int|bool $default
1062
 *
1063
 * @return string
1064
 */
1065
function give_get_repeater_field_id( $field, $fields, $default = false ) {
1066
	$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1067
1068
	// Get field id.
1069
	$field_id = "{$fields['id']}[{$row_placeholder}][{$field['id']}]";
1070
1071
	/**
1072
	 * Filter the specific repeater field id
1073
	 *
1074
	 * @since 1.8
1075
	 *
1076
	 * @param string $field_id
1077
	 */
1078
	$field_id = apply_filters( "give_get_repeater_field_{$field['id']}_id", $field_id, $field, $fields, $default );
1079
1080
	/**
1081
	 * Filter the repeater field id
1082
	 *
1083
	 * @since 1.8
1084
	 *
1085
	 * @param string $field_id
1086
	 */
1087
	$field_id = apply_filters( 'give_get_repeater_field_id', $field_id, $field, $fields, $default );
1088
1089
	return $field_id;
1090
}
1091
1092
1093
/**
1094
 * Get field name.
1095
 *
1096
 * @since  1.8
1097
 *
1098
 * @param  array $field
1099
 *
1100
 * @return string
1101
 */
1102
function give_get_field_name( $field ) {
1103
	$field_name = esc_attr( empty( $field['repeat'] ) ? $field['id'] : $field['repeatable_field_id'] );
1104
1105
	/**
1106
	 * Filter the field name.
1107
	 *
1108
	 * @since 1.8
1109
	 *
1110
	 * @param string $field_name
1111
	 */
1112
	$field_name = apply_filters( 'give_get_field_name', $field_name, $field );
1113
1114
	return $field_name;
1115
}
1116
1117
/**
1118
 * Output repeater field or multi donation type form on donation from edit screen.
1119
 * Note: internal use only.
1120
 *
1121
 * @TODO   : Add support for wysiwyg type field.
1122
 *
1123
 * @since  1.8
1124
 *
1125
 * @param  array $fields
1126
 *
1127
 * @return void
1128
 */
1129
function _give_metabox_form_data_repeater_fields( $fields ) {
1130
	global $thepostid, $post;
1131
1132
	// Bailout.
1133
	if ( ! isset( $fields['fields'] ) || empty( $fields['fields'] ) ) {
1134
		return;
1135
	}
1136
1137
	$group_numbering = isset( $fields['options']['group_numbering'] ) ? (int) $fields['options']['group_numbering'] : 0;
1138
	$close_tabs      = isset( $fields['options']['close_tabs'] ) ? (int) $fields['options']['close_tabs'] : 0;
1139
	$wrapper_class   = isset( $fields['wrapper_class'] ) ? $fields['wrapper_class'] : '';
1140
	?>
1141
	<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...
1142
	     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...
1143
		<?php if ( ! empty( $fields['name'] ) ) : ?>
1144
			<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...
1145
		<?php endif; ?>
1146
1147
		<?php if ( ! empty( $fields['description'] ) ) : ?>
1148
			<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...
1149
		<?php endif; ?>
1150
1151
		<table class="give-repeatable-fields-section-wrapper" cellspacing="0">
1152
			<?php
1153
			$repeater_field_values = give_get_meta( $thepostid, $fields['id'], true );
1154
			$header_title          = isset( $fields['options']['header_title'] )
1155
				? $fields['options']['header_title']
1156
				: esc_attr__( 'Group', 'give' );
1157
1158
			$add_default_donation_field = false;
1159
1160
			// Check if level is not created or we have to add default level.
1161
			if ( is_array( $repeater_field_values ) && ( $fields_count = count( $repeater_field_values ) ) ) {
1162
				$repeater_field_values = array_values( $repeater_field_values );
1163
			} else {
1164
				$fields_count               = 1;
1165
				$add_default_donation_field = true;
1166
			}
1167
			?>
1168
			<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...
1169
			<!--Repeater field group template-->
1170
			<tr class="give-template give-row">
1171
				<td class="give-repeater-field-wrap give-column" colspan="2">
1172
					<div class="give-row-head give-move">
1173
						<button type="button" class="handlediv button-link"><span class="toggle-indicator"></span>
1174
						</button>
1175
						<span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-</span>
1176
						<h2>
1177
							<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...
1178
						</h2>
1179
					</div>
1180
					<div class="give-row-body">
1181
						<?php foreach ( $fields['fields'] as $field ) : ?>
1182
							<?php
1183
							if ( ! give_is_field_callback_exist( $field ) ) {
1184
								continue;
1185
							}
1186
							?>
1187
							<?php
1188
							$field['repeat']              = true;
1189
							$field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields );
1190
							$field['id']                  = str_replace(
1191
								array( '[', ']' ),
1192
								array( '_', '', ),
1193
								$field['repeatable_field_id']
1194
							);
1195
							?>
1196
							<?php give_render_field( $field ); ?>
1197
						<?php endforeach; ?>
1198
					</div>
1199
				</td>
1200
			</tr>
1201
1202
			<?php if ( ! empty( $repeater_field_values ) ) : ?>
1203
				<!--Stored repeater field group-->
1204
				<?php foreach ( $repeater_field_values as $index => $field_group ) : ?>
1205
					<tr class="give-row">
1206
						<td class="give-repeater-field-wrap give-column" colspan="2">
1207
							<div class="give-row-head give-move">
1208
								<button type="button" class="handlediv button-link">
1209
									<span class="toggle-indicator"></span></button>
1210
								<span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
1211
								</span>
1212
								<h2>
1213
									<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...
1214
								</h2>
1215
							</div>
1216
							<div class="give-row-body">
1217
								<?php foreach ( $fields['fields'] as $field ) : ?>
1218
									<?php if ( ! give_is_field_callback_exist( $field ) ) {
1219
										continue;
1220
									} ?>
1221
									<?php
1222
									$field['repeat']              = true;
1223
									$field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields, $index );
1224
									$field['attributes']['value'] = give_get_repeater_field_value( $field, $field_group, $fields );
1225
									$field['id']                  = str_replace(
1226
										array( '[', ']' ),
1227
										array( '_', '', ),
1228
										$field['repeatable_field_id']
1229
									);
1230
									?>
1231
									<?php give_render_field( $field ); ?>
1232
								<?php endforeach; ?>
1233
							</div>
1234
						</td>
1235
					</tr>
1236
				<?php endforeach;; ?>
1237
1238
			<?php elseif ( $add_default_donation_field ) : ?>
1239
				<!--Default repeater field group-->
1240
				<tr class="give-row">
1241
					<td class="give-repeater-field-wrap give-column" colspan="2">
1242
						<div class="give-row-head give-move">
1243
							<button type="button" class="handlediv button-link">
1244
								<span class="toggle-indicator"></span></button>
1245
							<span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
1246
							</span>
1247
							<h2>
1248
								<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...
1249
							</h2>
1250
						</div>
1251
						<div class="give-row-body">
1252
							<?php
1253
							foreach ( $fields['fields'] as $field ) :
1254
								if ( ! give_is_field_callback_exist( $field ) ) {
1255
									continue;
1256
								}
1257
1258
								$field['repeat']              = true;
1259
								$field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields, 0 );
1260
								$field['attributes']['value'] = apply_filters(
1261
									"give_default_field_group_field_{$field['id']}_value",
1262
									( ! empty( $field['default'] ) ? $field['default'] : '' ),
1263
									$field,
1264
									$fields
1265
								);
1266
								$field['id']                  = str_replace(
1267
									array( '[', ']' ),
1268
									array( '_', '', ),
1269
									$field['repeatable_field_id']
1270
								);
1271
								give_render_field( $field );
1272
1273
							endforeach;
1274
							?>
1275
						</div>
1276
					</td>
1277
				</tr>
1278
			<?php endif; ?>
1279
			</tbody>
1280
			<tfoot>
1281
			<tr>
1282
				<?php
1283
				$add_row_btn_title = isset( $fields['options']['add_button'] )
1284
					? $add_row_btn_title = $fields['options']['add_button']
1285
					: esc_html__( 'Add Row', 'give' );
1286
				?>
1287
				<td colspan="2" class="give-add-repeater-field-section-row-wrap">
1288
					<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...
1289
				</td>
1290
			</tr>
1291
			</tfoot>
1292
		</table>
1293
	</div>
1294
	<?php
1295
}
1296
1297
1298
/**
1299
 * Get current setting tab.
1300
 *
1301
 * @since  1.8
1302
 * @return string
1303
 */
1304 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...
1305
	// Get current setting page.
1306
	$current_setting_page = give_get_current_setting_page();
1307
1308
	/**
1309
	 * Filter the default tab for current setting page.
1310
	 *
1311
	 * @since 1.8
1312
	 *
1313
	 * @param string
1314
	 */
1315
	$default_current_tab = apply_filters( "give_default_setting_tab_{$current_setting_page}", 'general' );
1316
1317
	// Get current tab.
1318
	$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...
1319
1320
	// Output.
1321
	return $current_tab;
1322
}
1323
1324
1325
/**
1326
 * Get current setting section.
1327
 *
1328
 * @since  1.8
1329
 * @return string
1330
 */
1331 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...
1332
	// Get current tab.
1333
	$current_tab = give_get_current_setting_tab();
1334
1335
	/**
1336
	 * Filter the default section for current setting page tab.
1337
	 *
1338
	 * @since 1.8
1339
	 *
1340
	 * @param string
1341
	 */
1342
	$default_current_section = apply_filters( "give_default_setting_tab_section_{$current_tab}", '' );
1343
1344
	// Get current section.
1345
	$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...
1346
1347
	// Output.
1348
	return $current_section;
1349
}
1350
1351
/**
1352
 * Get current setting page.
1353
 *
1354
 * @since  1.8
1355
 * @return string
1356
 */
1357
function give_get_current_setting_page() {
1358
	// Get current page.
1359
	$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...
1360
1361
	// Output.
1362
	return $setting_page;
1363
}
1364
1365
/**
1366
 * Set value for Form content --> Display content field setting.
1367
 *
1368
 * Backward compatibility:  set value by _give_content_option form meta field value if _give_display_content is not set
1369
 * yet.
1370
 *
1371
 * @since  1.8
1372
 *
1373
 * @param  mixed $field_value Field Value.
1374
 * @param  array $field       Field args.
1375
 * @param  int   $postid      Form/Post ID.
1376
 *
1377
 * @return string
1378
 */
1379
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...
1380
	$show_content = give_get_meta( $postid, '_give_content_option', true );
1381
1382
	if (
1383
		! give_get_meta( $postid, '_give_display_content', true )
1384
		&& $show_content
1385
		&& ( 'none' !== $show_content )
1386
	) {
1387
		$field_value = 'enabled';
1388
	}
1389
1390
	return $field_value;
1391
}
1392
1393
add_filter( '_give_display_content_field_value', '_give_display_content_field_value', 10, 3 );
1394
1395
1396
/**
1397
 * Set value for Form content --> Content placement field setting.
1398
 *
1399
 * Backward compatibility:  set value by _give_content_option form meta field value if _give_content_placement is not
1400
 * set yet.
1401
 *
1402
 * @since  1.8
1403
 *
1404
 * @param  mixed $field_value Field Value.
1405
 * @param  array $field       Field args.
1406
 * @param  int   $postid      Form/Post ID.
1407
 *
1408
 * @return string
1409
 */
1410
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...
1411
	$show_content = give_get_meta( $postid, '_give_content_option', true );
1412
1413
	if (
1414
		! give_get_meta( $postid, '_give_content_placement', true )
1415
		&& ( 'none' !== $show_content )
1416
	) {
1417
		$field_value = $show_content;
1418
	}
1419
1420
	return $field_value;
1421
}
1422
1423
add_filter( '_give_content_placement_field_value', '_give_content_placement_field_value', 10, 3 );
1424
1425
1426
/**
1427
 * Set value for Terms and Conditions --> Terms and Conditions field setting.
1428
 *
1429
 * Backward compatibility:  set value by _give_terms_option form meta field value if it's value is none.
1430
 *
1431
 * @since  1.8
1432
 *
1433
 * @param  mixed $field_value Field Value.
1434
 * @param  array $field       Field args.
1435
 * @param  int   $postid      Form/Post ID.
1436
 *
1437
 * @return string
1438
 */
1439 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...
1440
	$term_option = give_get_meta( $postid, '_give_terms_option', true );
1441
1442
	if ( in_array( $term_option, array( 'none', 'yes' ) ) ) {
1443
		$field_value = ( 'yes' === $term_option ? 'enabled' : 'disabled' );
1444
	}
1445
1446
	return $field_value;
1447
}
1448
1449
add_filter( '_give_terms_option_field_value', '_give_terms_option_field_value', 10, 3 );
1450
1451
1452
/**
1453
 * Set value for Form Display --> Offline Donation --> Billing Fields.
1454
 *
1455
 * Backward compatibility:  set value by _give_offline_donation_enable_billing_fields_single form meta field value if
1456
 * it's value is on.
1457
 *
1458
 * @since  1.8
1459
 *
1460
 * @param  mixed $field_value Field Value.
1461
 * @param  array $field       Field args.
1462
 * @param  int   $postid      Form/Post ID.
1463
 *
1464
 * @return string
1465
 */
1466
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...
1467
	$offline_donation = give_get_meta( $postid, '_give_offline_donation_enable_billing_fields_single', true );
1468
1469
	if ( 'on' === $offline_donation ) {
1470
		$field_value = 'enabled';
1471
	}
1472
1473
	return $field_value;
1474
}
1475
1476
add_filter( '_give_offline_donation_enable_billing_fields_single_field_value', '_give_offline_donation_enable_billing_fields_single_field_value', 10, 3 );
1477
1478
1479
/**
1480
 * Set value for Donation Options --> Custom Amount.
1481
 *
1482
 * Backward compatibility:  set value by _give_custom_amount form meta field value if it's value is yes or no.
1483
 *
1484
 * @since  1.8
1485
 *
1486
 * @param  mixed $field_value Field Value.
1487
 * @param  array $field       Field args.
1488
 * @param  int   $postid      Form/Post ID.
1489
 *
1490
 * @return string
1491
 */
1492 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...
1493
	$custom_amount = give_get_meta( $postid, '_give_custom_amount', true );
1494
1495
	if ( in_array( $custom_amount, array( 'yes', 'no' ) ) ) {
1496
		$field_value = ( 'yes' === $custom_amount ? 'enabled' : 'disabled' );
1497
	}
1498
1499
	return $field_value;
1500
}
1501
1502
add_filter( '_give_custom_amount_field_value', '_give_custom_amount_field_value', 10, 3 );
1503
1504
1505
/**
1506
 * Set value for Donation Goal --> Donation Goal.
1507
 *
1508
 * Backward compatibility:  set value by _give_goal_option form meta field value if it's value is yes or no.
1509
 *
1510
 * @since  1.8
1511
 *
1512
 * @param  mixed $field_value Field Value.
1513
 * @param  array $field       Field args.
1514
 * @param  int   $postid      Form/Post ID.
1515
 *
1516
 * @return string
1517
 */
1518 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...
1519
	$goal_option = give_get_meta( $postid, '_give_goal_option', true );
1520
1521
	if ( in_array( $goal_option, array( 'yes', 'no' ) ) ) {
1522
		$field_value = ( 'yes' === $goal_option ? 'enabled' : 'disabled' );
1523
	}
1524
1525
	return $field_value;
1526
}
1527
1528
add_filter( '_give_goal_option_field_value', '_give_goal_option_field_value', 10, 3 );
1529
1530
/**
1531
 * Set value for Donation Goal --> close Form.
1532
 *
1533
 * Backward compatibility:  set value by _give_close_form_when_goal_achieved form meta field value if it's value is yes
1534
 * or no.
1535
 *
1536
 * @since  1.8
1537
 *
1538
 * @param  mixed $field_value Field Value.
1539
 * @param  array $field       Field args.
1540
 * @param  int   $postid      Form/Post ID.
1541
 *
1542
 * @return string
1543
 */
1544 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...
1545
	$close_form = give_get_meta( $postid, '_give_close_form_when_goal_achieved', true );
1546
1547
	if ( in_array( $close_form, array( 'yes', 'no' ) ) ) {
1548
		$field_value = ( 'yes' === $close_form ? 'enabled' : 'disabled' );
1549
	}
1550
1551
	return $field_value;
1552
}
1553
1554
add_filter( '_give_close_form_when_goal_achieved_field_value', '_give_close_form_when_goal_achieved_value', 10, 3 );
1555
1556
1557
/**
1558
 * Set value for Form display --> Guest Donation.
1559
 *
1560
 * Backward compatibility:  set value by _give_logged_in_only form meta field value if it's value is yes or no.
1561
 *
1562
 * @since  1.8
1563
 *
1564
 * @param  mixed $field_value Field Value.
1565
 * @param  array $field       Field args.
1566
 * @param  int   $postid      Form/Post ID.
1567
 *
1568
 * @return string
1569
 */
1570 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...
1571
	$guest_donation = give_get_meta( $postid, '_give_logged_in_only', true );
1572
1573
	if ( in_array( $guest_donation, array( 'yes', 'no' ) ) ) {
1574
		$field_value = ( 'yes' === $guest_donation ? 'enabled' : 'disabled' );
1575
	}
1576
1577
	return $field_value;
1578
}
1579
1580
add_filter( '_give_logged_in_only_field_value', '_give_logged_in_only_value', 10, 3 );
1581
1582
/**
1583
 * Set value for Offline Donations --> Offline Donations.
1584
 *
1585
 * Backward compatibility:  set value by _give_customize_offline_donations form meta field value if it's value is yes
1586
 * or no.
1587
 *
1588
 * @since  1.8
1589
 *
1590
 * @param  mixed $field_value Field Value.
1591
 * @param  array $field       Field args.
1592
 * @param  int   $postid      Form/Post ID.
1593
 *
1594
 * @return string
1595
 */
1596 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...
1597
	$customize_offline_text = give_get_meta( $postid, '_give_customize_offline_donations', true );
1598
1599
	if ( in_array( $customize_offline_text, array( 'yes', 'no' ) ) ) {
1600
		$field_value = ( 'yes' === $customize_offline_text ? 'enabled' : 'disabled' );
1601
	}
1602
1603
	return $field_value;
1604
}
1605
1606
add_filter( '_give_customize_offline_donations_field_value', '_give_customize_offline_donations_value', 10, 3 );
1607
1608
1609
/**
1610
 * Set repeater field id for multi donation form.
1611
 *
1612
 * @since 1.8
1613
 *
1614
 * @param int   $field_id
1615
 * @param array $field
1616
 * @param array $fields
1617
 * @param bool  $default
1618
 *
1619
 * @return mixed
1620
 */
1621
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...
1622
	$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1623
	$field_id        = "{$fields['id']}[{$row_placeholder}][{$field['id']}][level_id]";
1624
1625
	return $field_id;
1626
}
1627
1628
add_filter( 'give_get_repeater_field__give_id_id', '_give_set_multi_level_repeater_field_id', 10, 4 );
1629
1630
/**
1631
 * Set repeater field value for multi donation form.
1632
 *
1633
 * @since 1.8
1634
 *
1635
 * @param string $field_value
1636
 * @param array  $field
1637
 * @param array  $field_group
1638
 * @param array  $fields
1639
 *
1640
 * @return mixed
1641
 */
1642
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...
1643
	$field_value = $field_group[ $field['id'] ]['level_id'];
1644
1645
	return $field_value;
1646
}
1647
1648
add_filter( 'give_get_repeater_field__give_id_value', '_give_set_multi_level_repeater_field_value', 10, 4 );
1649
1650
/**
1651
 * Set default value for _give_id field.
1652
 *
1653
 * @since 1.8
1654
 *
1655
 * @param $field
1656
 *
1657
 * @return string
1658
 */
1659
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...
1660
	return 0;
1661
}
1662
1663
add_filter( 'give_default_field_group_field__give_id_value', '_give_set_field_give_id_default_value' );
1664
1665
/**
1666
 * Set default value for _give_default field.
1667
 *
1668
 * @since 1.8
1669
 *
1670
 * @param $field
1671
 *
1672
 * @return string
1673
 */
1674
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...
1675
	return 'default';
1676
}
1677
1678
add_filter( 'give_default_field_group_field__give_default_value', '_give_set_field_give_default_default_value' );
1679
1680
/**
1681
 * Set repeater field editor id for field type wysiwyg.
1682
 *
1683
 * @since 1.8
1684
 *
1685
 * @param $field_name
1686
 * @param $field
1687
 *
1688
 * @return string
1689
 */
1690
function give_repeater_field_set_editor_id( $field_name, $field ) {
1691
	if ( isset( $field['repeatable_field_id'] ) && 'wysiwyg' == $field['type'] ) {
1692
		$field_name = '_give_repeater_' . uniqid() . '_wysiwyg';
1693
	}
1694
1695
	return $field_name;
1696
}
1697
1698
add_filter( 'give_get_field_name', 'give_repeater_field_set_editor_id', 10, 2 );
1699