Completed
Push — issues/1122 ( 0db41a )
by Ravinder
16:39
created

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

Complexity

Conditions 14
Paths 24

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 35
nc 24
nop 1
dl 0
loc 52
rs 5.9535
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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        = '';
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
			$func_name = "{$func_name_prefix}_text_input";
54
			break;
55
56
57
		case 'textarea' :
58
			$func_name = "{$func_name_prefix}_textarea_input";
59
			break;
60
61
		case 'colorpicker' :
62
			$func_name = "{$func_name_prefix}_{$field['type']}";
63
			break;
64
65
		case 'levels_id':
66
			$func_name = "{$func_name_prefix}_hidden_input";
67
			break;
68
69
		case 'group' :
70
			$func_name = "_{$func_name_prefix}_metabox_form_data_repeater_fields";
71
			break;
72
73
		case 'give_default_radio_inline':
74
			$func_name = "{$func_name_prefix}_radio";
75
			break;
76
77
		default:
78
			$func_name = "{$func_name_prefix}_{$field['type']}";
79
	}
80
81
	$func_name = apply_filters( 'give_setting_callback', $func_name, $field );
82
83
	// Check if render callback exist or not.
84
	if ( ! function_exists( "$func_name" ) || empty( $func_name ) ) {
85
		return false;
86
	}
87
88
	return apply_filters( 'give_setting_callback', $func_name, $field );
89
}
90
91
/**
92
 * This function add backward compatibility to render cmb2 type field type.
93
 *
94
 * @since  1.8
95
 *
96
 * @param  array $field Field argument array.
97
 *
98
 * @return bool
99
 */
100
function give_render_field( $field ) {
101
	$func_name = give_get_field_callback( $field );
102
103
	// Check if render callback exist or not.
104
	if ( ! $func_name ) {
105
		return false;
106
	}
107
108
	// CMB2 compatibility: Push all classes to attributes's class key
109
	if ( empty( $field['class'] ) ) {
110
		$field['class'] = '';
111
	}
112
113
	if ( empty( $field['attributes']['class'] ) ) {
114
		$field['attributes']['class'] = '';
115
	}
116
117
	$field['attributes']['class'] = trim( "give-field {$field['attributes']['class']} give-{$field['type']} {$field['class']}" );
118
	unset( $field['class'] );
119
120
121
	// CMB2 compatibility: Set wrapper class if any.
122
	if ( ! empty( $field['row_classes'] ) ) {
123
		$field['wrapper_class'] = $field['row_classes'];
124
		unset( $field['row_classes'] );
125
	}
126
127
	// Set field params on basis of cmb2 field name.
128
	switch ( $field['type'] ) {
129
		case 'radio_inline':
130
			if ( empty( $field['wrapper_class'] ) ) {
131
				$field['wrapper_class'] = '';
132
			}
133
			$field['wrapper_class'] .= ' give-inline-radio-fields';
134
135
			break;
136
137
		case 'text':
138
		case 'text-medium':
139
		case 'text_medium':
140
		case 'text-small' :
141
		case 'text_small' :
142
			// CMB2 compatibility: Set field type to text.
143
			$field['type'] = isset( $field['attributes']['type'] ) ? $field['attributes']['type'] : 'text';
144
145
			// CMB2 compatibility: Set data type to price.
146
			if (
147
				empty( $field['data_type'] )
148
				&& ! empty( $field['attributes']['class'] )
149
				&& (
150
					false !== strpos( $field['attributes']['class'], 'money' )
151
					|| false !== strpos( $field['attributes']['class'], 'amount' )
152
				)
153
			) {
154
				$field['data_type'] = 'decimal';
155
			}
156
			break;
157
158
		case 'levels_id':
159
			$field['type'] = 'hidden';
160
			break;
161
162
		case 'colorpicker' :
163
			$field['type']  = 'text';
164
			$field['class'] = 'give-colorpicker';
165
			break;
166
167
		case 'give_default_radio_inline':
168
			$field['type']    = 'radio';
169
			$field['options'] = array(
170
				'default' => __( 'Default' ),
171
			);
172
			break;
173
	}
174
175
	// CMB2 compatibility: Add support to define field description by desc & description param.
176
	// We encourage you to use description param.
177
	$field['description'] = ( ! empty( $field['description'] )
178
		? $field['description']
179
		: ( ! empty( $field['desc'] ) ? $field['desc'] : '' ) );
180
181
	// Call render function.
182
	$func_name( $field );
183
184
	return true;
185
}
186
187
/**
188
 * Output a text input box.
189
 *
190
 * @since  1.8
191
 * @param  array $field {
192
 *     Optional. Array of text input field arguments.
193
 *
194
 *     @type string             $id              Field ID. Default ''.
195
 *     @type string             $style           CSS style for input field. Default ''.
196
 *     @type string             $wrapper_class   CSS class to use for wrapper of input field. Default ''.
197
 *     @type string             $value           Value of input field. Default ''.
198
 *     @type string             $name            Name of input field. Default ''.
199
 *     @type string             $type            Type of input field. Default 'text'.
200
 *     @type string             $before_field    Text/HTML to add before input field. Default ''.
201
 *     @type string             $after_field     Text/HTML to add after input field. Default ''.
202
 *     @type string             $data_type       Define data type for value of input to filter it properly. Default ''.
203
 *     @type string             $description     Description of input field. Default ''.
204
 *     @type array              $attributes      List of attributes of input field. Default array().
205
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class' => '****' )
206
 * }
207
 * @return void
208
 */
209
function give_text_input( $field ) {
210
	global $thepostid, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
211
212
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
213
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
214
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
215
	$field['value']         = give_get_field_value( $field, $thepostid );
216
	$field['type']          = isset( $field['type'] ) ? $field['type'] : 'text';
217
	$field['before_field']  = '';
218
	$field['after_field']   = '';
219
	$data_type              = empty( $field['data_type'] ) ? '' : $field['data_type'];
220
221
	switch ( $data_type ) {
222
		case 'price' :
223
			$field['value'] = ( ! empty( $field['value'] ) ? give_format_amount( $field['value'] ) : $field['value'] );
224
225
			$field['before_field'] = ! empty( $field['before_field'] ) ? $field['before_field'] : ( give_get_option( 'currency_position' ) == 'before' ? '<span class="give-money-symbol give-money-symbol-before">' . give_currency_symbol() . '</span>' : '' );
226
			$field['after_field']  = ! empty( $field['after_field'] ) ? $field['after_field'] : ( give_get_option( 'currency_position' ) == 'after' ? '<span class="give-money-symbol give-money-symbol-after">' . give_currency_symbol() . '</span>' : '' );
227
			break;
228
229
		case 'decimal' :
230
			$field['attributes']['class'] .= ' give_input_decimal';
231
			$field['value'] = ( ! empty( $field['value'] ) ? give_format_decimal( $field['value'] ) : $field['value'] );
232
			break;
233
234
		default :
235
			break;
236
	}
237
238
	// Custom attribute handling
239
	$custom_attributes = array();
240
241
	if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
242
243
		foreach ( $field['attributes'] as $attribute => $value ) {
244
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
245
		}
246
	}
247
248
	echo '<p class="give-field-wrap ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . give_get_field_name( $field ) . '">' . wp_kses_post( $field['name'] ) . '</label>' . $field['before_field'] . '<input type="' . esc_attr( $field['type'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . give_get_field_name( $field ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) . '" ' . implode( ' ', $custom_attributes ) . ' />' . $field['after_field'];
249
250
	if ( ! empty( $field['description'] ) ) {
251
		echo '<span class="give-field-description">' . wp_kses_post( $field['description'] ) . '</span>';
252
	}
253
	echo '</p>';
254
}
255
256
/**
257
 * Output a hidden input box.
258
 *
259
 * @since  1.8
260
 * @param  array $field {
261
 *     Optional. Array of hidden text input field arguments.
262
 *
263
 *     @type string             $id              Field ID. Default ''.
264
 *     @type string             $value           Value of input field. Default ''.
265
 *     @type string             $name            Name of input field. Default ''.
266
 *     @type string             $type            Type of input field. Default 'text'.
267
 *     @type array              $attributes      List of attributes of input field. Default array().
268
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class' => '****' )
269
 * }
270
 * @return void
271
 */
272
function give_hidden_input( $field ) {
273
	global $thepostid, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
274
275
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
276
	$field['value'] = give_get_field_value( $field, $thepostid );
277
278
	// Custom attribute handling
279
	$custom_attributes = array();
280
281
	if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
282
283
		foreach ( $field['attributes'] as $attribute => $value ) {
284
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
285
		}
286
	}
287
288
	echo '<input type="hidden" name="' . give_get_field_name( $field ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) . '" ' . implode( ' ', $custom_attributes ) . '/> ';
289
}
290
291
/**
292
 * Output a textarea input box.
293
 *
294
 * @since  1.8
295
 * @since  1.8
296
 * @param  array $field {
297
 *     Optional. Array of textarea input field arguments.
298
 *
299
 *     @type string             $id              Field ID. Default ''.
300
 *     @type string             $style           CSS style for input field. Default ''.
301
 *     @type string             $wrapper_class   CSS class to use for wrapper of input field. Default ''.
302
 *     @type string             $value           Value of input field. Default ''.
303
 *     @type string             $name            Name of input field. Default ''.
304
 *     @type string             $description     Description of input field. Default ''.
305
 *     @type array              $attributes      List of attributes of input field. Default array().
306
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class' => '****' )
307
 * }
308
 * @return void
309
 */
310
function give_textarea_input( $field ) {
311
	global $thepostid, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
312
313
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
314
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
315
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
316
	$field['value']         = give_get_field_value( $field, $thepostid );
317
318
	// Custom attribute handling
319
	$custom_attributes = array();
320
321
	if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
322
323
		foreach ( $field['attributes'] as $attribute => $value ) {
324
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
325
		}
326
	}
327
328
	echo '<p class="give-field-wrap ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . give_get_field_name( $field ) . '">' . wp_kses_post( $field['name'] ) . '</label><textarea style="' . esc_attr( $field['style'] ) . '"  name="' . give_get_field_name( $field ) . '" id="' . esc_attr( $field['id'] ) . '" rows="10" cols="20" ' . implode( ' ', $custom_attributes ) . '>' . esc_textarea( $field['value'] ) . '</textarea> ';
329
330
	if ( ! empty( $field['description'] ) ) {
331
		echo '<span class="give-field-description">' . wp_kses_post( $field['description'] ) . '</span>';
332
	}
333
	echo '</p>';
334
}
335
336
/**
337
 * Output a wysiwyg.
338
 *
339
 * @since  1.8
340
 * @param  array $field {
341
 *     Optional. Array of WordPress editor field arguments.
342
 *
343
 *     @type string             $id              Field ID. Default ''.
344
 *     @type string             $style           CSS style for input field. Default ''.
345
 *     @type string             $wrapper_class   CSS class to use for wrapper of input field. Default ''.
346
 *     @type string             $value           Value of input field. Default ''.
347
 *     @type string             $name            Name of input field. Default ''.
348
 *     @type string             $description     Description of input field. Default ''.
349
 *     @type array              $attributes      List of attributes of input field. Default array().
350
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class' => '****' )
351
 * }
352
 * @return void
353
 */
354
function give_wysiwyg( $field ) {
355
	global $thepostid, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
356
357
	$thepostid                = empty( $thepostid ) ? $post->ID : $thepostid;
358
	$field['style']           = isset( $field['style'] ) ? $field['style'] : '';
359
	$field['wrapper_class']   = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
360
	$field['value']           = give_get_field_value( $field, $thepostid );
361
	$field['unique_field_id'] = give_get_field_name( $field );
362
	$editor_attributes        = array(
363
		'textarea_name' => isset( $field['repeatable_field_id'] ) ? $field['repeatable_field_id'] : $field['id'],
364
		'textarea_rows' => '10',
365
		'editor_css'    => esc_attr( $field['style'] ),
366
		'editor_class'  => $field['attributes']['class']
367
	);
368
	$data_wp_editor           = ' data-wp-editor="'. base64_encode( json_encode( array( $field['value'], $field['unique_field_id'],$editor_attributes ) ) ) .'"';
369
	$data_wp_editor           = isset( $field['repeatable_field_id'] ) ? $data_wp_editor : '';
370
371
	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>';
372
373
	wp_editor(
374
		$field['value'],
375
		$field['unique_field_id'],
376
		$editor_attributes
377
	);
378
379
	if ( ! empty( $field['description'] ) ) {
380
		echo '<span class="give-field-description">' . wp_kses_post( $field['description'] ) . '</span>';
381
	}
382
	echo '</div>';
383
}
384
385
/**
386
 * Output a checkbox input box.
387
 *
388
 * @since  1.8
389
 * @param  array $field {
390
 *     Optional. Array of checkbox field arguments.
391
 *
392
 *     @type string             $id              Field ID. Default ''.
393
 *     @type string             $style           CSS style for input field. Default ''.
394
 *     @type string             $wrapper_class   CSS class to use for wrapper of input field. Default ''.
395
 *     @type string             $value           Value of input field. Default ''.
396
 *     @type string             $cbvalue         Checkbox value. Default 'on'.
397
 *     @type string             $name            Name of input field. Default ''.
398
 *     @type string             $description     Description of input field. Default ''.
399
 *     @type array              $attributes      List of attributes of input field. Default array().
400
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class' => '****' )
401
 * }
402
 * @return void
403
 */
404
function give_checkbox( $field ) {
405
	global $thepostid, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
406
407
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
408
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
409
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
410
	$field['value']         = give_get_field_value( $field, $thepostid );
411
	$field['cbvalue']       = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'on';
412
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
413
414
	// Custom attribute handling.
415
	$custom_attributes = array();
416
417
	if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
418
419
		foreach ( $field['attributes'] as $attribute => $value ) {
420
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
421
		}
422
	}
423
424
	echo '<p class="give-field-wrap ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . give_get_field_name( $field ) . '">' . wp_kses_post( $field['name'] ) . '</label><input type="checkbox" style="' . esc_attr( $field['style'] ) . '" name="' . give_get_field_name( $field ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['cbvalue'] ) . '" ' . checked( $field['value'], $field['cbvalue'], false ) . '  ' . implode( ' ', $custom_attributes ) . '/> ';
425
426
	if ( ! empty( $field['description'] ) ) {
427
		echo '<span class="give-field-description">' . wp_kses_post( $field['description'] ) . '</span>';
428
	}
429
430
	echo '</p>';
431
}
432
433
/**
434
 * Output a select input box.
435
 *
436
 * @since  1.8
437
 * @param  array $field {
438
 *     Optional. Array of select field arguments.
439
 *
440
 *     @type string             $id              Field ID. Default ''.
441
 *     @type string             $style           CSS style for input field. Default ''.
442
 *     @type string             $wrapper_class   CSS class to use for wrapper of input field. Default ''.
443
 *     @type string             $value           Value of input field. Default ''.
444
 *     @type string             $name            Name of input field. Default ''.
445
 *     @type string             $description     Description of input field. Default ''.
446
 *     @type array              $attributes      List of attributes of input field. Default array().
447
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class' => '****' )
448
 *     @type array              $options         List of options. Default array().
449
 *                                               for example: 'options' => array( '' => 'None', 'yes' => 'Yes' )
450
 * }
451
 * @return void
452
 */
453
function give_select( $field ) {
454
	global $thepostid, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
455
456
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
457
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
458
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
459
	$field['value']         = give_get_field_value( $field, $thepostid );
460
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
461
462
	// Custom attribute handling
463
	$custom_attributes = array();
464
465
	if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
466
467
		foreach ( $field['attributes'] as $attribute => $value ) {
468
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
469
		}
470
	}
471
472
	echo '<p class="give-field-wrap ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . give_get_field_name( $field ) . '">' . wp_kses_post( $field['name'] ) . '</label><select id="' . esc_attr( $field['id'] ) . '" name="' . give_get_field_name( $field ) . '" style="' . esc_attr( $field['style'] ) . '" ' . implode( ' ', $custom_attributes ) . '>';
473
474
	foreach ( $field['options'] as $key => $value ) {
475
		echo '<option value="' . esc_attr( $key ) . '" ' . selected( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
476
	}
477
478
	echo '</select> ';
479
480
	if ( ! empty( $field['description'] ) ) {
481
		echo '<span class="give-field-description">' . wp_kses_post( $field['description'] ) . '</span>';
482
	}
483
	echo '</p>';
484
}
485
486
/**
487
 * Output a radio input box.
488
 *
489
 * @since  1.8
490
 * @param  array $field {
491
 *     Optional. Array of radio field arguments.
492
 *
493
 *     @type string             $id              Field ID. Default ''.
494
 *     @type string             $style           CSS style for input field. Default ''.
495
 *     @type string             $wrapper_class   CSS class to use for wrapper of input field. Default ''.
496
 *     @type string             $value           Value of input field. Default ''.
497
 *     @type string             $name            Name of input field. Default ''.
498
 *     @type string             $description     Description of input field. Default ''.
499
 *     @type array              $attributes      List of attributes of input field. Default array().
500
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class' => '****' )
501
 *     @type array              $options         List of options. Default array().
502
 *                                               for example: 'options' => array( 'enable' => 'Enable', 'disable' => 'Disable' )
503
 * }
504
 * @return void
505
 */
506
function give_radio( $field ) {
507
	global $thepostid, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
508
509
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
510
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
511
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
512
	$field['value']         = give_get_field_value( $field, $thepostid );
513
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
514
515
	// Custom attribute handling
516
	$custom_attributes = array();
517
518
	if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
519
520
		foreach ( $field['attributes'] as $attribute => $value ) {
521
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
522
		}
523
	}
524
525
	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">';
526
527
	foreach ( $field['options'] as $key => $value ) {
528
529
		echo '<li><label><input
530
				name="' . give_get_field_name( $field ) . '"
531
				value="' . esc_attr( $key ) . '"
532
				type="radio"
533
				style="' . esc_attr( $field['style'] ) . '"
534
				' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . ' '
535
				. implode( ' ', $custom_attributes ) . '
536
				/> ' . esc_html( $value ) . '</label>
537
		</li>';
538
	}
539
	echo '</ul>';
540
541
	if ( ! empty( $field['description'] ) ) {
542
		echo '<span class="give-field-description">' . wp_kses_post( $field['description'] ) . '</span>';
543
	}
544
545
	echo '</fieldset>';
546
}
547
548
/**
549
 * Output a colorpicker.
550
 *
551
 * @since  1.8
552
 * @param  array $field {
553
 *     Optional. Array of colorpicker field arguments.
554
 *
555
 *     @type string             $id              Field ID. Default ''.
556
 *     @type string             $style           CSS style for input field. Default ''.
557
 *     @type string             $wrapper_class   CSS class to use for wrapper of input field. Default ''.
558
 *     @type string             $value           Value of input field. Default ''.
559
 *     @type string             $name            Name of input field. Default ''.
560
 *     @type string             $description     Description of input field. Default ''.
561
 *     @type array              $attributes      List of attributes of input field. Default array().
562
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class' => '****' )
563
 * }
564
 * @return void
565
 */
566
function give_colorpicker( $field ) {
567
	global $thepostid, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
568
569
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
570
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
571
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
572
	$field['value']         = give_get_field_value( $field, $thepostid );
573
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
574
	$field['type']          = 'text';
575
576
	// Custom attribute handling
577
	$custom_attributes = array();
578
579
	if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
580
581
		foreach ( $field['attributes'] as $attribute => $value ) {
582
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
583
		}
584
	}
585
586
	echo '<p class="give-field-wrap ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . give_get_field_name( $field ) . '">' . wp_kses_post( $field['name'] ) . '</label><input type="' . esc_attr( $field['type'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . give_get_field_name( $field ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) . '" ' . implode( ' ', $custom_attributes ) . ' /> ';
587
588
	if ( ! empty( $field['description'] ) ) {
589
		echo '<span class="give-field-description">' . wp_kses_post( $field['description'] ) . '</span>';
590
	}
591
	echo '</p>';
592
}
593
594
/**
595
 * Output a select field with payment options list.
596
 *
597
 * @since  1.8
598
 *
599
 * @param  array $field
600
 *
601
 * @return void
602
 */
603
function give_default_gateway( $field ) {
604
	global $thepostid, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
605
606
	// get all active payment gateways.
607
	$gateways = give_get_enabled_payment_gateways( $thepostid );
608
609
	// Set field option value.
610
	foreach ( $gateways as $key => $option ) {
611
		$field['options'][ $key ] = $option['admin_label'];
612
	}
613
614
	//Add a field to the Give Form admin single post view of this field
615
	if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
616
		$field['options'] = array_merge( array( 'global' => esc_html__( 'Global Default', 'give' ) ), $field['options'] );
617
	}
618
619
	// Render select field.
620
	give_select( $field );
621
}
622
623
624
/**
625
 * Get setting field value.
626
 *
627
 * Note: Use only for single post, page or custom post type.
628
 *
629
 * @since  1.8
630
 *
631
 * @param  array $field
632
 * @param  int   $postid
633
 *
634
 * @return mixed
635
 */
636
function give_get_field_value( $field, $postid ) {
637
	if ( isset( $field['attributes']['value'] ) ) {
638
		return $field['attributes']['value'];
639
	}
640
641
	// Get value from db.
642
	$field_value = get_post_meta( $postid, $field['id'], true );
643
644
	/**
645
	 * Filter the field value before apply default value.
646
	 *
647
	 * @since 1.8
648
	 *
649
	 * @param mixed $field_value Field value.
650
	 */
651
	$field_value = apply_filters( "{$field['id']}_field_value", $field_value, $field, $postid );
652
653
654
	// Set default value if no any data saved to db.
655
	if ( ! $field_value && isset( $field['default'] ) ) {
656
		$field_value = $field['default'];
657
	}
658
659
	return $field_value;
660
}
661
662
/**
663
 * Get repeater field value.
664
 *
665
 * Note: Use only for single post, page or custom post type.
666
 *
667
 * @since  1.8
668
 *
669
 * @param array $field
670
 * @param array $field_group
671
 * @param array $fields
672
 *
673
 * @return string
674
 */
675
function give_get_repeater_field_value( $field, $field_group, $fields ) {
676
	$field_value = ( isset( $field_group[ $field['id'] ] ) ? $field_group[ $field['id'] ] : '' );
677
678
	/**
679
	 * Filter the specific repeater field value
680
	 *
681
	 * @since 1.8
682
	 *
683
	 * @param string $field_id
684
	 */
685
	$field_value = apply_filters( "give_get_repeater_field_{$field['id']}_value", $field_value, $field, $field_group, $fields );
686
687
	/**
688
	 * Filter the repeater field value
689
	 *
690
	 * @since 1.8
691
	 *
692
	 * @param string $field_id
693
	 */
694
	$field_value = apply_filters( 'give_get_repeater_field_value', $field_value, $field, $field_group, $fields );
695
696
	return $field_value;
697
}
698
699
/**
700
 * Get repeater field id.
701
 *
702
 * Note: Use only for single post, page or custom post type.
703
 *
704
 * @since  1.8
705
 *
706
 * @param array    $field
707
 * @param array    $fields
708
 * @param int|bool $default
709
 *
710
 * @return string
711
 */
712
function give_get_repeater_field_id( $field, $fields, $default = false ) {
713
	$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
714
715
	// Get field id.
716
	$field_id = "{$fields['id']}[{$row_placeholder}][{$field['id']}]";
717
718
	/**
719
	 * Filter the specific repeater field id
720
	 *
721
	 * @since 1.8
722
	 *
723
	 * @param string $field_id
724
	 */
725
	$field_id = apply_filters( "give_get_repeater_field_{$field['id']}_id", $field_id, $field, $fields, $default );
726
727
728
	/**
729
	 * Filter the repeater field id
730
	 *
731
	 * @since 1.8
732
	 *
733
	 * @param string $field_id
734
	 */
735
	$field_id = apply_filters( 'give_get_repeater_field_id', $field_id, $field, $fields, $default );
736
737
	return $field_id;
738
}
739
740
741
/**
742
 * Get field name.
743
 *
744
 * @since  1.8
745
 *
746
 * @param  array $field
747
 *
748
 * @return string
749
 */
750
function give_get_field_name( $field ) {
751
	$field_name = esc_attr( empty( $field['repeat'] ) ? $field['id'] : $field['repeatable_field_id'] );
752
753
	/**
754
	 * Filter the field name.
755
	 *
756
	 * @since 1.8
757
	 *
758
	 * @param string $field_name
759
	 */
760
	$field_name = apply_filters( 'give_get_field_name', $field_name, $field );
761
762
	return $field_name;
763
}
764
765
/**
766
 * Output repeater field or multi donation type form on donation from edit screen.
767
 * Note: internal use only.
768
 * @TODO   : Add support for wysiwyg type field.
769
 *
770
 * @since  1.8
771
 *
772
 * @param  array $fields
773
 *
774
 * @return void
775
 */
776
function _give_metabox_form_data_repeater_fields( $fields ) {
777
	global $thepostid, $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
778
779
	// Bailout.
780
	if ( ! isset( $fields['fields'] ) || empty( $fields['fields'] ) ) {
781
		return;
782
	}
783
784
	$group_numbering = isset( $fields['options']['group_numbering'] ) ? (int) $fields['options']['group_numbering'] : 0;
785
	$close_tabs      = isset( $fields['options']['close_tabs'] )      ? (int) $fields['options']['close_tabs']      : 0;
786
	?>
787
	<div class="give-repeatable-field-section" id="<?php echo "{$fields['id']}_field"; ?>" data-group-numbering="<?php echo $group_numbering; ?>" data-close-tabs="<?php echo $close_tabs; ?>">
788
		<?php if ( ! empty( $fields['name'] ) ) : ?>
789
			<p class="give-repeater-field-name"><?php echo $fields['name']; ?></p>
790
		<?php endif; ?>
791
792
		<?php if ( ! empty( $fields['description'] ) ) : ?>
793
			<p class="give-repeater-field-description"><?php echo $fields['description']; ?></p>
794
		<?php endif; ?>
795
796
		<table class="give-repeatable-fields-section-wrapper" cellspacing="0">
797
			<?php
798
			$repeater_field_values = get_post_meta( $thepostid, $fields['id'], true );
799
			$header_title          = isset( $fields['options']['header_title'] )
800
				? $fields['options']['header_title']
801
				: esc_attr__( 'Group', 'give' );
802
803
			$add_default_donation_field = false;
804
805
			// Check if level is not created or we have to add default level.
806
			if ( is_array( $repeater_field_values ) && ( $fields_count = count( $repeater_field_values ) ) ) {
807
				$repeater_field_values = array_values( $repeater_field_values );
808
			} else {
809
				$fields_count               = 1;
810
				$add_default_donation_field = true;
811
			}
812
			?>
813
			<tbody class="container"<?php echo " data-rf-row-count=\"{$fields_count}\""; ?>>
814
				<!--Repeater field group template-->
815
				<tr class="give-template give-row">
816
					<td class="give-repeater-field-wrap give-column" colspan="2">
817
						<div class="give-row-head give-move">
818
							<button type="button" class="handlediv button-link"><span class="toggle-indicator"></span>
819
							</button>
820
							<span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-</span>
821
							<h2>
822
								<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
823
							</h2>
824
						</div>
825
						<div class="give-row-body">
826
							<?php foreach ( $fields['fields'] as $field ) : ?>
827
								<?php if ( ! give_is_field_callback_exist( $field ) ) {
828
									continue;
829
								} ?>
830
								<?php
831
								$field['repeat']              = true;
832
								$field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields );
833
								$field['id']                  = str_replace( array( '[', ']' ), array(
834
									'_',
835
									'',
836
								), $field['repeatable_field_id'] );
837
								?>
838
								<?php give_render_field( $field ); ?>
839
							<?php endforeach; ?>
840
						</div>
841
					</td>
842
				</tr>
843
844
				<?php if ( ! empty( $repeater_field_values ) ) : ?>
845
					<!--Stored repeater field group-->
846
					<?php foreach ( $repeater_field_values as $index => $field_group ) : ?>
847
						<tr class="give-row">
848
							<td class="give-repeater-field-wrap give-column" colspan="2">
849
								<div class="give-row-head give-move">
850
									<button type="button" class="handlediv button-link">
851
										<span class="toggle-indicator"></span></button>
852
									<sapn class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
853
									</sapn>
854
									<h2>
855
										<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
856
									</h2>
857
								</div>
858
								<div class="give-row-body">
859
									<?php foreach ( $fields['fields'] as $field ) : ?>
860
										<?php if ( ! give_is_field_callback_exist( $field ) ) {
861
											continue;
862
										} ?>
863
										<?php
864
										$field['repeat']              = true;
865
										$field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields, $index );
866
										$field['attributes']['value'] = give_get_repeater_field_value( $field, $field_group, $fields );
867
										$field['id']                  = str_replace( array( '[', ']' ), array(
868
											'_',
869
											'',
870
										), $field['repeatable_field_id'] );
871
										?>
872
										<?php give_render_field( $field ); ?>
873
									<?php endforeach; ?>
874
								</div>
875
							</td>
876
						</tr>
877
					<?php endforeach;; ?>
878
879
				<?php elseif ( $add_default_donation_field ) : ?>
880
					<!--Default repeater field group-->
881
					<tr class="give-row">
882
						<td class="give-repeater-field-wrap give-column" colspan="2">
883
							<div class="give-row-head give-move">
884
								<button type="button" class="handlediv button-link">
885
									<span class="toggle-indicator"></span></button>
886
								<sapn class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
887
								</sapn>
888
								<h2>
889
									<span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
890
								</h2>
891
							</div>
892
							<div class="give-row-body">
893
								<?php
894
								foreach ( $fields['fields'] as $field ) :
895
									if ( ! give_is_field_callback_exist( $field ) ) {
896
										continue;
897
									}
898
899
									$field['repeat']              = true;
900
									$field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields, 0 );
901
									$field['attributes']['value'] = apply_filters( "give_default_field_group_field_{$field['id']}_value", ( ! empty( $field['default'] ) ? $field['default'] : '' ), $field );
902
									$field['id']                  = str_replace( array( '[', ']' ), array(
903
										'_',
904
										'',
905
									), $field['repeatable_field_id'] );
906
									give_render_field( $field );
907
								endforeach;
908
								?>
909
							</div>
910
						</td>
911
					</tr>
912
				<?php endif; ?>
913
			</tbody>
914
			<tfoot>
915
				<tr>
916
					<?php
917
					$add_row_btn_title = isset( $fields['options']['add_button'] )
918
						? $add_row_btn_title = $fields['options']['add_button']
919
						: esc_html__( 'Add Row', 'give' );
920
					?>
921
					<td colspan="2" class="give-add-repeater-field-section-row-wrap">
922
						<span class="button button-primary give-add-repeater-field-section-row"><?php echo $add_row_btn_title; ?></span>
923
					</td>
924
				</tr>
925
			</tfoot>
926
		</table>
927
	</div>
928
	<?php
929
}
930
931
932
/**
933
 * Get current setting tab.
934
 *
935
 * @since  1.8
936
 * @return string
937
 */
938
function give_get_current_setting_tab() {
939
	// Get current setting page.
940
	$current_setting_page = give_get_current_setting_page();
941
942
	/**
943
	 * Filter the default tab for current setting page.
944
	 *
945
	 * @since 1.8
946
	 *
947
	 * @param string
948
	 */
949
	$default_current_tab = apply_filters( "give_default_setting_tab_{$current_setting_page}", 'general' );
950
951
	// Get current tab.
952
	$current_tab = empty( $_GET['tab'] ) ? $default_current_tab : urldecode( $_GET['tab'] );
953
954
	// Output.
955
	return $current_tab;
956
}
957
958
959
/**
960
 * Get current setting section.
961
 *
962
 * @since  1.8
963
 * @return string
964
 */
965
function give_get_current_setting_section() {
966
	// Get current tab.
967
	$current_tab = give_get_current_setting_tab();
968
969
	/**
970
	 * Filter the default section for current setting page tab.
971
	 *
972
	 * @since 1.8
973
	 *
974
	 * @param string
975
	 */
976
	$default_current_section = apply_filters( "give_default_setting_tab_section_{$current_tab}", '' );
977
978
	// Get current section.
979
	$current_section = empty( $_REQUEST['section'] ) ? $default_current_section : urldecode( $_REQUEST['section'] );
980
981
	//Output.
982
	return $current_section;
983
}
984
985
/**
986
 * Get current setting page.
987
 *
988
 * @since  1.8
989
 * @return string
990
 */
991
function give_get_current_setting_page() {
992
	// Get current page.
993
	$setting_page = ! empty( $_GET['page'] ) ? urldecode( $_GET['page'] ) : '';
994
995
	//Output.
996
	return $setting_page;
997
}
998
999
/**
1000
 * Set value for Form content --> Display content field setting.
1001
 *
1002
 * Backward compatibility:  set value by _give_content_option form meta field value if _give_display_content is not set yet.
1003
 *
1004
 * @since  1.8
1005
 *
1006
 * @param  mixed $field_value Field Value.
1007
 * @param  array $field       Field args.
1008
 * @param  int   $postid      Form/Post ID.
1009
 *
1010
 * @return string
1011
 */
1012
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...
1013
	$show_content = get_post_meta( $postid, '_give_content_option', true );
1014
1015
	if (
1016
		! get_post_meta( $postid, '_give_display_content', true )
1017
		&& $show_content
1018
		&& ( 'none' !== $show_content )
1019
	) {
1020
		$field_value = 'enabled';
1021
	}
1022
1023
	return $field_value;
1024
}
1025
1026
add_filter( '_give_display_content_field_value', '_give_display_content_field_value', 10, 3 );
1027
1028
1029
/**
1030
 * Set value for Form content --> Content placement field setting.
1031
 *
1032
 * Backward compatibility:  set value by _give_content_option form meta field value if _give_content_placement is not set yet.
1033
 *
1034
 * @since  1.8
1035
 *
1036
 * @param  mixed $field_value Field Value.
1037
 * @param  array $field       Field args.
1038
 * @param  int   $postid      Form/Post ID.
1039
 *
1040
 * @return string
1041
 */
1042
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...
1043
	$show_content = get_post_meta( $postid, '_give_content_option', true );
1044
1045
	if (
1046
		! get_post_meta( $postid, '_give_content_placement', true )
1047
		&& ( 'none' !== $show_content )
1048
	) {
1049
		$field_value = $show_content;
1050
	}
1051
1052
	return $field_value;
1053
}
1054
1055
add_filter( '_give_content_placement_field_value', '_give_content_placement_field_value', 10, 3 );
1056
1057
1058
/**
1059
 * Set value for Terms and Conditions --> Terms and Conditions field setting.
1060
 *
1061
 * Backward compatibility:  set value by _give_terms_option form meta field value if it's value is none.
1062
 *
1063
 * @since  1.8
1064
 *
1065
 * @param  mixed $field_value Field Value.
1066
 * @param  array $field       Field args.
1067
 * @param  int   $postid      Form/Post ID.
1068
 *
1069
 * @return string
1070
 */
1071
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...
1072
	$term_option = get_post_meta( $postid, '_give_terms_option', true );
1073
1074
	if ( in_array( $term_option, array( 'none', 'yes' ) ) ) {
1075
		$field_value = ( 'yes' === $term_option ? 'enabled' : 'disabled' );
1076
	}
1077
1078
	return $field_value;
1079
}
1080
1081
add_filter( '_give_terms_option_field_value', '_give_terms_option_field_value', 10, 3 );
1082
1083
1084
/**
1085
 * Set value for Form Display --> Offline Donation --> Billing Fields.
1086
 *
1087
 * Backward compatibility:  set value by _give_offline_donation_enable_billing_fields_single form meta field value if it's value is on.
1088
 *
1089
 * @since  1.8
1090
 *
1091
 * @param  mixed $field_value Field Value.
1092
 * @param  array $field       Field args.
1093
 * @param  int   $postid      Form/Post ID.
1094
 *
1095
 * @return string
1096
 */
1097
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...
1098
	$offline_donation = get_post_meta( $postid, '_give_offline_donation_enable_billing_fields_single', true );
1099
1100
	if ( 'on' === $offline_donation ) {
1101
		$field_value = 'enabled';
1102
	}
1103
1104
	return $field_value;
1105
}
1106
1107
add_filter( '_give_offline_donation_enable_billing_fields_single_field_value', '_give_offline_donation_enable_billing_fields_single_field_value', 10, 3 );
1108
1109
1110
/**
1111
 * Set value for Donation Options --> Custom Amount.
1112
 *
1113
 * Backward compatibility:  set value by _give_custom_amount form meta field value if it's value is yes or no.
1114
 *
1115
 * @since  1.8
1116
 *
1117
 * @param  mixed $field_value Field Value.
1118
 * @param  array $field       Field args.
1119
 * @param  int   $postid      Form/Post ID.
1120
 *
1121
 * @return string
1122
 */
1123
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...
1124
	$custom_amount = get_post_meta( $postid, '_give_custom_amount', true );
1125
1126
	if ( in_array( $custom_amount, array( 'yes', 'no' ) ) ) {
1127
		$field_value = ( 'yes' === $custom_amount ? 'enabled' : 'disabled' );
1128
	}
1129
1130
	return $field_value;
1131
}
1132
1133
add_filter( '_give_custom_amount_field_value', '_give_custom_amount_field_value', 10, 3 );
1134
1135
1136
/**
1137
 * Set value for Donation Goal --> Donation Goal.
1138
 *
1139
 * Backward compatibility:  set value by _give_goal_option form meta field value if it's value is yes or no.
1140
 *
1141
 * @since  1.8
1142
 *
1143
 * @param  mixed $field_value Field Value.
1144
 * @param  array $field       Field args.
1145
 * @param  int   $postid      Form/Post ID.
1146
 *
1147
 * @return string
1148
 */
1149
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...
1150
	$goal_option = get_post_meta( $postid, '_give_goal_option', true );
1151
1152
	if ( in_array( $goal_option, array( 'yes', 'no' ) ) ) {
1153
		$field_value = ( 'yes' === $goal_option ? 'enabled' : 'disabled' );
1154
	}
1155
1156
	return $field_value;
1157
}
1158
1159
add_filter( '_give_goal_option_field_value', '_give_goal_option_field_value', 10, 3 );
1160
1161
/**
1162
 * Set value for Donation Goal --> close Form.
1163
 *
1164
 * Backward compatibility:  set value by _give_close_form_when_goal_achieved form meta field value if it's value is yes or no.
1165
 *
1166
 * @since  1.8
1167
 *
1168
 * @param  mixed $field_value Field Value.
1169
 * @param  array $field       Field args.
1170
 * @param  int   $postid      Form/Post ID.
1171
 *
1172
 * @return string
1173
 */
1174
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...
1175
	$close_form = get_post_meta( $postid, '_give_close_form_when_goal_achieved', true );
1176
1177
	if ( in_array( $close_form, array( 'yes', 'no' ) ) ) {
1178
		$field_value = ( 'yes' === $close_form ? 'enabled' : 'disabled' );
1179
	}
1180
1181
	return $field_value;
1182
}
1183
1184
add_filter( '_give_close_form_when_goal_achieved_field_value', '_give_close_form_when_goal_achieved_value', 10, 3 );
1185
1186
1187
/**
1188
 * Set value for Form display --> Guest Donation.
1189
 *
1190
 * Backward compatibility:  set value by _give_logged_in_only form meta field value if it's value is yes or no.
1191
 *
1192
 * @since  1.8
1193
 *
1194
 * @param  mixed $field_value Field Value.
1195
 * @param  array $field       Field args.
1196
 * @param  int   $postid      Form/Post ID.
1197
 *
1198
 * @return string
1199
 */
1200
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...
1201
	$guest_donation = get_post_meta( $postid, '_give_logged_in_only', true );
1202
1203
	if ( in_array( $guest_donation, array( 'yes', 'no' ) ) ) {
1204
		$field_value = ( 'yes' === $guest_donation ? 'enabled' : 'disabled' );
1205
	}
1206
1207
	return $field_value;
1208
}
1209
1210
add_filter( '_give_logged_in_only_field_value', '_give_logged_in_only_value', 10, 3 );
1211
1212
/**
1213
 * Set value for Offline Donations --> Offline Donations.
1214
 *
1215
 * Backward compatibility:  set value by _give_customize_offline_donations form meta field value if it's value is yes or no.
1216
 *
1217
 * @since  1.8
1218
 *
1219
 * @param  mixed $field_value Field Value.
1220
 * @param  array $field       Field args.
1221
 * @param  int   $postid      Form/Post ID.
1222
 *
1223
 * @return string
1224
 */
1225
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...
1226
	$customize_offline_text = get_post_meta( $postid, '_give_customize_offline_donations', true );
1227
1228
	if ( in_array( $customize_offline_text, array( 'yes', 'no' ) ) ) {
1229
		$field_value = ( 'yes' === $customize_offline_text ? 'enabled' : 'disabled' );
1230
	}
1231
1232
	return $field_value;
1233
}
1234
1235
add_filter( '_give_customize_offline_donations_field_value', '_give_customize_offline_donations_value', 10, 3 );
1236
1237
1238
/**
1239
 * Set repeater field id for multi donation form.
1240
 *
1241
 * @since 1.8
1242
 *
1243
 * @param int   $field_id
1244
 * @param array $field
1245
 * @param array $fields
1246
 * @param bool  $default
1247
 *
1248
 * @return mixed
1249
 */
1250
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...
1251
	$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1252
	$field_id = "{$fields['id']}[{$row_placeholder}][{$field['id']}][level_id]";
1253
1254
	return $field_id;
1255
}
1256
1257
add_filter( 'give_get_repeater_field__give_id_id', '_give_set_multi_level_repeater_field_id', 10, 4 );
1258
1259
/**
1260
 * Set repeater field value for multi donation form.
1261
 *
1262
 * @since 1.8
1263
 *
1264
 * @param string $field_value
1265
 * @param array  $field
1266
 * @param array  $field_group
1267
 * @param array  $fields
1268
 *
1269
 * @return mixed
1270
 */
1271
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...
1272
	$field_value = $field_group[ $field['id'] ]['level_id'];
1273
1274
	return $field_value;
1275
}
1276
1277
add_filter( 'give_get_repeater_field__give_id_value', '_give_set_multi_level_repeater_field_value', 10, 4 );
1278
1279
/**
1280
 * Set default value for _give_id field.
1281
 *
1282
 * @since 1.8
1283
 *
1284
 * @param $field
1285
 *
1286
 * @return string
1287
 */
1288
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...
1289
	return 0;
1290
}
1291
1292
add_filter( 'give_default_field_group_field__give_id_value', '_give_set_field_give_id_default_value' );
1293
1294
/**
1295
 * Set default value for _give_default field.
1296
 *
1297
 * @since 1.8
1298
 *
1299
 * @param $field
1300
 *
1301
 * @return string
1302
 */
1303
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...
1304
	return 'default';
1305
}
1306
1307
add_filter( 'give_default_field_group_field__give_default_value', '_give_set_field_give_default_default_value' );
1308
1309
/**
1310
 * Set repeater field editor id for field type wysiwyg.
1311
 *
1312
 * @since 1.8
1313
 *
1314
 * @param $field_name
1315
 * @param $field
1316
 *
1317
 * @return string
1318
 */
1319
function give_repeater_field_set_editor_id( $field_name, $field ) {
1320
	if ( isset( $field['repeatable_field_id'] ) &&  'wysiwyg' == $field['type'] ) {
1321
		$field_name = '_give_repeater_' . uniqid() . '_wysiwyg';
1322
	}
1323
1324
	return $field_name;
1325
}
1326
add_filter( 'give_get_field_name', 'give_repeater_field_set_editor_id', 10, 2 );