Completed
Push — issues/1038 ( 82778e...ccd3e1 )
by Ravinder
19:16
created

give-metabox-functions.php ➔ give_select()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
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
		case 'textarea' :
57
			$func_name = "{$func_name_prefix}_textarea_input";
58
			break;
59
60
		case 'colorpicker' :
61
			$func_name = "{$func_name_prefix}_{$field['type']}";
62
			break;
63
64
		case 'hidden':
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
79
			if (
80
				array_key_exists( 'callback', $field )
81
				&& ! empty( $field['callback'] )
82
			) {
83
				$func_name = $field['callback'];
84
			} else {
85
				$func_name = "{$func_name_prefix}_{$field['type']}";
86
			}
87
	}
88
89
	/**
90
	 * Filter the metabox setting render function
91
	 *
92
	 * @since 1.8
93
	 */
94
	$func_name = apply_filters( 'give_get_field_callback', $func_name, $field );
95
96
	// Exit if not any function exist.
97
	// Check if render callback exist or not.
98
	if ( empty( $func_name ) ) {
99
		return false;
100
	} elseif ( is_string( $func_name ) && ! function_exists( "$func_name" ) ) {
101
		return false;
102
	} elseif ( is_array( $func_name ) && ! method_exists( $func_name[0], "$func_name[1]" ) ) {
103
		return false;
104
	}
105
106
	return $func_name;
107
}
108
109
/**
110
 * This function adds backward compatibility to render cmb2 type field type.
111
 *
112
 * @since  1.8
113
 *
114
 * @param  array $field Field argument array.
115
 *
116
 * @return bool
117
 */
118
function give_render_field( $field ) {
119
120
	// Check if render callback exist or not.
121
	if ( ! ( $func_name = give_get_field_callback( $field ) ) ) {
122
		return false;
123
	}
124
125
	// CMB2 compatibility: Push all classes to attributes's class key
126
	if ( empty( $field['class'] ) ) {
127
		$field['class'] = '';
128
	}
129
130
	if ( empty( $field['attributes']['class'] ) ) {
131
		$field['attributes']['class'] = '';
132
	}
133
134
	$field['attributes']['class'] = trim( "give-field {$field['attributes']['class']} give-{$field['type']} {$field['class']}" );
135
	unset( $field['class'] );
136
137
	// CMB2 compatibility: Set wrapper class if any.
138
	if ( ! empty( $field['row_classes'] ) ) {
139
		$field['wrapper_class'] = $field['row_classes'];
140
		unset( $field['row_classes'] );
141
	}
142
143
	// Set field params on basis of cmb2 field name.
144
	switch ( $field['type'] ) {
145
		case 'radio_inline':
146
			if ( empty( $field['wrapper_class'] ) ) {
147
				$field['wrapper_class'] = '';
148
			}
149
			$field['wrapper_class'] .= ' give-inline-radio-fields';
150
			$field['type'] = 'radio';
151
152
			break;
153
154
		case 'text':
155
		case 'text-medium':
156
		case 'text_medium':
157
		case 'text-small' :
158
		case 'text_small' :
159
			// CMB2 compatibility: Set field type to text.
160
			$field['type'] = isset( $field['attributes']['type'] ) ? $field['attributes']['type'] : 'text';
161
162
			// CMB2 compatibility: Set data type to price.
163
			if (
164
				empty( $field['data_type'] )
165
				&& ! empty( $field['attributes']['class'] )
166
				&& (
167
					false !== strpos( $field['attributes']['class'], 'money' )
168
					|| false !== strpos( $field['attributes']['class'], 'amount' )
169
				)
170
			) {
171
				$field['data_type'] = 'decimal';
172
			}
173
			break;
174
175
		case 'levels_id':
176
			$field['type'] = 'hidden';
177
			break;
178
179
		case 'colorpicker' :
180
			$field['type']  = 'text';
181
			$field['class'] = 'give-colorpicker';
182
			break;
183
184
		case 'give_default_radio_inline':
185
			$field['type']    = 'radio';
186
			$field['options'] = array(
187
				'default' => __( 'Default' ),
188
			);
189
			break;
190
	}
191
192
	// CMB2 compatibility: Add support to define field description by desc & description param.
193
	// We encourage you to use description param.
194
	$field['description'] = ( ! empty( $field['description'] )
195
		? $field['description']
196
		: ( ! empty( $field['desc'] ) ? $field['desc'] : '' ) );
197
198
	// Call render function.
199
	if ( is_array( $func_name ) ) {
200
		$func_name[0]->$func_name[1]( $field );
201
	} else {
202
		$func_name( $field );
203
	}
204
205
	return true;
206
}
207
208
/**
209
 * Output a text input box.
210
 *
211
 * @since  1.8
212
 * @since  1.9 Render field with field api
213
 *
214
 * @param array $field Field arguments
215
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
216
 *
217
 * @return void
218
 */
219
function give_text_input( $field ) {
220
	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...
221
222
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
223
	$field['value'] = give_get_field_value( $field, $thepostid );
224
	$data_type      = empty( $field['data_type'] ) ? '' : $field['data_type'];
225
226
	switch ( $data_type ) {
227
		case 'price' :
228
			$field['value'] = ( ! empty( $field['value'] ) ? give_format_amount( $field['value'] ) : $field['value'] );
229
230
			$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>' : '' );
231
			$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>' : '' );
232
			break;
233
234
		case 'decimal' :
235
			$field['attributes']['class'] .= ' give_input_decimal';
236
			$field['value'] = ( ! empty( $field['value'] ) ? give_format_decimal( $field['value'] ) : $field['value'] );
237
			break;
238
239
		default :
240
			break;
241
	}
242
243
	$field = give_backward_compatibility_setting_api_1_8( $field );
244
245
	// Set description.
246
	// Backward compatibility ( 1.8=<version>1.9).
247
	$field['after_field'] = ! empty( $field['after_field'] )
248
		? $field['after_field'] . give_get_field_description( $field )
249
		: give_get_field_description( $field );
250
251
	// Reset label for repeater field compatibility.
252
	$field['name'] = give_get_field_name( $field );
253
254
	// Render Field.
255
	echo Give_Fields_API::render_tag( $field );
256
}
257
258
/**
259
 * Output a hidden input box.
260
 *
261
 * @since  1.8
262
 * @since  1.9 Render field with field api
263
 *
264
 * @param array $field Field arguments
265
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
266
 *
267
 * @return void
268
 */
269
function give_hidden_input( $field ) {
270
	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...
271
272
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
273
	$field['value'] = give_get_field_value( $field, $thepostid );
274
275
	$field = give_backward_compatibility_setting_api_1_8( $field );
276
277
	// Reset label for repeater field compatibility.
278
	$field['name'] = give_get_field_name( $field );
279
280
	// Render Field.
281
	echo Give_Fields_API::render_tag( $field );
282
}
283
284
/**
285
 * Output a textarea input box.
286
 *
287
 * @since  1.8
288
 * @since  1.9 Render field with field api
289
 *
290
 * @param array $field Field arguments
291
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
292
 *
293
 * @return void
294
 */
295
function give_textarea_input( $field ) {
296
	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...
297
298
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
299
	$field['value'] = give_get_field_value( $field, $thepostid );
300
301
	$field = give_backward_compatibility_setting_api_1_8( $field );
302
303
	// Set description.
304
	// Backward compatibility ( 1.8=<version>1.9).
305
	$field['after_field'] = ! empty( $field['after_field'] )
306
		? $field['after_field'] . give_get_field_description( $field )
307
		: give_get_field_description( $field );
308
309
	// Reset label for repeater field compatibility.
310
	$field['name'] = give_get_field_name( $field );
311
312
	// Render Field.
313
	echo Give_Fields_API::render_tag( $field );
314
}
315
316
/**
317
 * Output a wysiwyg.
318
 *
319
 * @since  1.8
320
 *
321
 * @param  array $field         {
322
 *                              Optional. Array of WordPress editor field arguments.
323
 *
324
 * @type string  $id            Field ID. Default ''.
325
 * @type string  $style         CSS style for input field. Default ''.
326
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
327
 * @type string  $value         Value of input field. Default ''.
328
 * @type string  $name          Name of input field. Default ''.
329
 * @type string  $description   Description of input field. Default ''.
330
 * @type array   $attributes    List of attributes of input field. Default array().
331
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
332
 *                                               => '****' )
333
 * }
334
 * @return void
335
 */
336
function give_wysiwyg( $field ) {
337
	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...
338
339
	$thepostid                = empty( $thepostid ) ? $post->ID : $thepostid;
340
	$field['value']           = give_get_field_value( $field, $thepostid );
341
	$field['unique_field_id'] = give_get_field_name( $field );
342
	$field['wrapper_type']    = 'div';
343
344
	$field = give_backward_compatibility_setting_api_1_8( $field );
345
346
	$field['wrapper_attributes']['data-wp-editor'] = base64_encode( json_encode( array(
347
			$field['value'],
348
			$field['unique_field_id'],
349
			//$field['editor_attributes'],
350
		) ) ) . '"';
351
352
	// Set description.
353
	// Backward compatibility ( 1.8=<version>1.9).
354
	$field['after_field'] = ! empty( $field['after_field'] )
355
		? $field['after_field'] . give_get_field_description( $field )
356
		: give_get_field_description( $field );
357
358
	// Render Field.
359
	echo Give_Fields_API::render_tag( $field );
360
361
	// @todo: label must be linked to wordpress editor.
362
}
363
364
/**
365
 * Output a checkbox input box.
366
 *
367
 * @since  1.8
368
 * @since  1.9 Render field with field api
369
 *
370
 * @param array $field Field arguments
371
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
372
 *
373
 * @return void
374
 */
375
function give_checkbox( $field ) {
376
	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...
377
378
	$thepostid        = empty( $thepostid ) ? $post->ID : $thepostid;
379
	$field['value']   = give_get_field_value( $field, $thepostid );
380
	$field['cbvalue'] = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'on';
381
382
	$field = give_backward_compatibility_setting_api_1_8( $field );
383
384
	// Set description.
385
	// Backward compatibility ( 1.8=<version>1.9).
386
	$field['after_field'] = ! empty( $field['after_field'] )
387
		? $field['after_field'] . give_get_field_description( $field )
388
		: give_get_field_description( $field );
389
390
	// Reset label for repeater field compatibility.
391
	$field['name'] = give_get_field_name( $field );
392
393
	// Render Field.
394
	echo Give_Fields_API::render_tag( $field );
395
}
396
397
/**
398
 * Output a select input box.
399
 *
400
 * @since  1.8
401
 * @since  1.9 Render field with field api
402
 *
403
 * @param array $field Field arguments
404
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
405
 *
406
 * @return void
407
 */
408
function give_select( $field ) {
409
	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...
410
411
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
412
	$field['value'] = give_get_field_value( $field, $thepostid );
413
414
	$field = give_backward_compatibility_setting_api_1_8( $field );
415
416
	// Set description.
417
	// Backward compatibility ( 1.8=<version>1.9).
418
	$field['after_field'] = ! empty( $field['after_field'] )
419
		? $field['after_field'] . give_get_field_description( $field )
420
		: give_get_field_description( $field );
421
422
	// Reset label for repeater field compatibility.
423
	$field['name'] = give_get_field_name( $field );
424
425
	// Render Field.
426
	echo Give_Fields_API::render_tag( $field );
427
}
428
429
/**
430
 * Output a radio input box.
431
 *
432
 * @since  1.8
433
 * @since  1.9 Render field with field api
434
 *
435
 * @param array $field Field arguments
436
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
437
 * @param array $field
438
 *
439
 * @return void
440
 */
441
function give_radio( $field ) {
442
	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...
443
444
	$thepostid             = empty( $thepostid ) ? $post->ID : $thepostid;
445
	$field['value']        = give_get_field_value( $field, $thepostid );
446
	$field['wrapper_type'] = 'fieldset';
447
448
	$field = give_backward_compatibility_setting_api_1_8( $field );
449
450
	// Set description.
451
	// Backward compatibility ( 1.8=<version>1.9).
452
	$field['after_field'] = ! empty( $field['after_field'] )
453
		? $field['after_field'] . give_get_field_description( $field )
454
		: give_get_field_description( $field );
455
456
	// Reset label for repeater field compatibility.
457
	$field['name'] = give_get_field_name( $field );
458
459
	// Render Field.
460
	echo Give_Fields_API::render_tag( $field );
461
}
462
463
/**
464
 * Output a colorpicker.
465
 *
466
 * @since  1.8
467
 * @since  1.9 Render field with field api
468
 *
469
 * @param array $field Field arguments
470
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
471
 * @param array $field
472
 *
473
 * @return void
474
 */
475
function give_colorpicker( $field ) {
476
	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...
477
478
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
479
	$field['value'] = give_get_field_value( $field, $thepostid );
480
	$field['type']  = 'text';
481
482
	$field = give_backward_compatibility_setting_api_1_8( $field );
483
484
	// Set description.
485
	// Backward compatibility ( 1.8=<version>1.9).
486
	$field['after_field'] = ! empty( $field['after_field'] )
487
		? $field['after_field'] . give_get_field_description( $field )
488
		: give_get_field_description( $field );
489
490
	// Reset label for repeater field compatibility.
491
	$field['name'] = give_get_field_name( $field );
492
493
	// Render Field.
494
	echo Give_Fields_API::render_tag( $field );
495
}
496
497
498
/**
499
 * Output a media upload field.
500
 *
501
 * @since  1.8
502
 * @since  1.9 Render field with field api
503
 *
504
 * @param array $field Field arguments
505
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
506
 * @param array $field
507
 *
508
 * @return void
509
 */
510
function give_media( $field ) {
511
	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...
512
513
	$field = give_backward_compatibility_setting_api_1_8( $field );
514
515
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
516
	$field['value'] = give_get_field_value( $field, $thepostid );
517
518
	// Set description.
519
	// Backward compatibility ( 1.8=<version>1.9).
520
	$field['after_field'] = ! empty( $field['after_field'] )
521
		? $field['after_field'] . give_get_field_description( $field )
522
		: give_get_field_description( $field );
523
524
	// Reset label for repeater field compatibility.
525
	$field['name'] = give_get_field_name( $field );
526
527
	// Render Field.
528
	echo Give_Fields_API::render_tag( $field );
529
}
530
531
/**
532
 * Output a select field with payment options list.
533
 *
534
 * @since  1.8
535
 *
536
 * @param  array $field
537
 *
538
 * @return void
539
 */
540
function give_default_gateway( $field ) {
541
	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...
542
543
	// get all active payment gateways.
544
	$gateways         = give_get_enabled_payment_gateways( $thepostid );
545
	$field['options'] = array();
546
547
	// Set field option value.
548
	if ( ! empty( $gateways ) ) {
549
		foreach ( $gateways as $key => $option ) {
550
			$field['options'][ $key ] = $option['admin_label'];
551
		}
552
	}
553
554
	// Add a field to the Give Form admin single post view of this field
555
	if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
556
		$field['options'] = array_merge( array( 'global' => esc_html__( 'Global Default', 'give' ) ), $field['options'] );
557
	}
558
559
	$field['type'] = 'select';
560
561
	// Render select field.
562
	give_select( $field );
563
}
564
565
/**
566
 * Output the documentation link.
567
 *
568
 * @since  1.8
569
 * @since  1.9 Render field with field api
570
 *
571
 * @param array $field Field arguments
572
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
573
 *
574
 * @return void
575
 */
576
577
function give_docs_link( $field ) {
578
	$field = give_backward_compatibility_setting_api_1_8( $field );
579
580
	// Set default class.
581
	// Backward compatibility ( 1.8=<version>1.9).
582
	$field['wrapper_attributes']['class'] = ! empty( $field['wrapper_attributes']['class'] )
583
		? "{$field['wrapper_attributes']['class']} give-docs-link"
584
		: 'give-docs-link';
585
586
	// Render Field.
587
	echo Give_Fields_API::render_tag( $field );
588
}
589
590
/**
591
 * Get setting field value.
592
 *
593
 * Note: Use only for single post, page or custom post type.
594
 *
595
 * @since  1.8
596
 *
597
 * @param  array $field
598
 * @param  int   $postid
599
 *
600
 * @return mixed
601
 */
602
function give_get_field_value( $field, $postid ) {
603
	if ( isset( $field['attributes']['value'] ) ) {
604
		return $field['attributes']['value'];
605
	}
606
607
	// Get value from db.
608
	$field_value = get_post_meta( $postid, $field['id'], true );
609
610
	/**
611
	 * Filter the field value before apply default value.
612
	 *
613
	 * @since 1.8
614
	 *
615
	 * @param mixed $field_value Field value.
616
	 */
617
	$field_value = apply_filters( "{$field['id']}_field_value", $field_value, $field, $postid );
618
619
	// Set default value if no any data saved to db.
620
	if ( ! $field_value && isset( $field['default'] ) ) {
621
		$field_value = $field['default'];
622
	}
623
624
	return $field_value;
625
}
626
627
628
/**
629
 * Get field description html.
630
 *
631
 * @since 1.8
632
 *
633
 * @param $field
634
 *
635
 * @return string
636
 */
637
function give_get_field_description( $field ) {
638
	$field_desc_html = '';
639
	if ( ! empty( $field['description'] ) ) {
640
		$field_desc_html = '<span class="give-field-description">' . wp_kses_post( $field['description'] ) . '</span>';
641
	}
642
643
	return $field_desc_html;
644
}
645
646
647
/**
648
 * Get field custom attributes as string.
649
 *
650
 * @since 1.8
651
 *
652
 * @param $field
653
 *
654
 * @return string
655
 */
656
function give_get_custom_attributes( $field ) {
657
	// Custom attribute handling
658
	$custom_attributes = array();
659
660
	if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
661
662
		foreach ( $field['attributes'] as $attribute => $value ) {
663
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
664
		}
665
	}
666
667
	return implode( ' ', $custom_attributes );
668
}
669
670
/**
671
 * Get repeater field value.
672
 *
673
 * Note: Use only for single post, page or custom post type.
674
 *
675
 * @since  1.8
676
 *
677
 * @param array $field
678
 * @param array $field_group
679
 * @param array $fields
680
 *
681
 * @return string
682
 */
683
function give_get_repeater_field_value( $field, $field_group, $fields ) {
684
	$field_value = ( isset( $field_group[ $field['id'] ] ) ? $field_group[ $field['id'] ] : '' );
685
686
	/**
687
	 * Filter the specific repeater field value
688
	 *
689
	 * @since 1.8
690
	 *
691
	 * @param string $field_id
692
	 */
693
	$field_value = apply_filters( "give_get_repeater_field_{$field['id']}_value", $field_value, $field, $field_group, $fields );
694
695
	/**
696
	 * Filter the repeater field value
697
	 *
698
	 * @since 1.8
699
	 *
700
	 * @param string $field_id
701
	 */
702
	$field_value = apply_filters( 'give_get_repeater_field_value', $field_value, $field, $field_group, $fields );
703
704
	return $field_value;
705
}
706
707
/**
708
 * Get repeater field id.
709
 *
710
 * Note: Use only for single post, page or custom post type.
711
 *
712
 * @since  1.8
713
 *
714
 * @param array    $field
715
 * @param array    $fields
716
 * @param int|bool $default
717
 *
718
 * @return string
719
 */
720
function give_get_repeater_field_id( $field, $fields, $default = false ) {
721
	$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
722
723
	// Get field id.
724
	$field_id = "{$fields['id']}[{$row_placeholder}][{$field['id']}]";
725
726
	/**
727
	 * Filter the specific repeater field id
728
	 *
729
	 * @since 1.8
730
	 *
731
	 * @param string $field_id
732
	 */
733
	$field_id = apply_filters( "give_get_repeater_field_{$field['id']}_id", $field_id, $field, $fields, $default );
734
735
	/**
736
	 * Filter the repeater field id
737
	 *
738
	 * @since 1.8
739
	 *
740
	 * @param string $field_id
741
	 */
742
	$field_id = apply_filters( 'give_get_repeater_field_id', $field_id, $field, $fields, $default );
743
744
	return $field_id;
745
}
746
747
748
/**
749
 * Get field name.
750
 *
751
 * @since  1.8
752
 *
753
 * @param  array $field
754
 *
755
 * @return string
756
 */
757
function give_get_field_name( $field ) {
758
	$field_name = esc_attr( empty( $field['repeat'] ) ? $field['id'] : $field['repeatable_field_id'] );
759
760
	/**
761
	 * Filter the field name.
762
	 *
763
	 * @since 1.8
764
	 *
765
	 * @param string $field_name
766
	 */
767
	$field_name = apply_filters( 'give_get_field_name', $field_name, $field );
768
769
	return $field_name;
770
}
771
772
/**
773
 * Output repeater field or multi donation type form on donation from edit screen.
774
 * Note: internal use only.
775
 *
776
 * @since  1.8
777
 *
778
 * @param  array $fields
779
 *
780
 * @return void
781
 */
782
function _give_metabox_form_data_repeater_fields( $fields ) {
783
	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...
784
	$fields            = give_backward_compatibility_setting_api_1_8( $fields );
785
	$fields['value']   = get_post_meta( $thepostid, $fields['id'], true );
786
	$fields['wrapper'] = false;
787
788
	echo Give_Fields_API::render_tag( $fields );
789
}
790
791
792
/**
793
 * Get current setting tab.
794
 *
795
 * @since  1.8
796
 * @return string
797
 */
798
function give_get_current_setting_tab() {
799
	// Get current setting page.
800
	$current_setting_page = give_get_current_setting_page();
801
802
	/**
803
	 * Filter the default tab for current setting page.
804
	 *
805
	 * @since 1.8
806
	 *
807
	 * @param string
808
	 */
809
	$default_current_tab = apply_filters( "give_default_setting_tab_{$current_setting_page}", 'general' );
810
811
	// Get current tab.
812
	$current_tab = empty( $_GET['tab'] ) ? $default_current_tab : urldecode( $_GET['tab'] );
813
814
	// Output.
815
	return $current_tab;
816
}
817
818
819
/**
820
 * Get current setting section.
821
 *
822
 * @since  1.8
823
 * @return string
824
 */
825
function give_get_current_setting_section() {
826
	// Get current tab.
827
	$current_tab = give_get_current_setting_tab();
828
829
	/**
830
	 * Filter the default section for current setting page tab.
831
	 *
832
	 * @since 1.8
833
	 *
834
	 * @param string
835
	 */
836
	$default_current_section = apply_filters( "give_default_setting_tab_section_{$current_tab}", '' );
837
838
	// Get current section.
839
	$current_section = empty( $_REQUEST['section'] ) ? $default_current_section : urldecode( $_REQUEST['section'] );
840
841
	// Output.
842
	return $current_section;
843
}
844
845
/**
846
 * Get current setting page.
847
 *
848
 * @since  1.8
849
 * @return string
850
 */
851
function give_get_current_setting_page() {
852
	// Get current page.
853
	$setting_page = ! empty( $_GET['page'] ) ? urldecode( $_GET['page'] ) : '';
854
855
	// Output.
856
	return $setting_page;
857
}
858
859
/**
860
 * Set value for Form content --> Display content field setting.
861
 *
862
 * Backward compatibility:  set value by _give_content_option form meta field value if _give_display_content is not set
863
 * yet.
864
 *
865
 * @since  1.8
866
 *
867
 * @param  mixed $field_value Field Value.
868
 * @param  array $field       Field args.
869
 * @param  int   $postid      Form/Post ID.
870
 *
871
 * @return string
872
 */
873
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...
874
	$show_content = get_post_meta( $postid, '_give_content_option', true );
875
876
	if (
877
		! get_post_meta( $postid, '_give_display_content', true )
878
		&& $show_content
879
		&& ( 'none' !== $show_content )
880
	) {
881
		$field_value = 'enabled';
882
	}
883
884
	return $field_value;
885
}
886
887
add_filter( '_give_display_content_field_value', '_give_display_content_field_value', 10, 3 );
888
889
890
/**
891
 * Set value for Form content --> Content placement field setting.
892
 *
893
 * Backward compatibility:  set value by _give_content_option form meta field value if _give_content_placement is not
894
 * set yet.
895
 *
896
 * @since  1.8
897
 *
898
 * @param  mixed $field_value Field Value.
899
 * @param  array $field       Field args.
900
 * @param  int   $postid      Form/Post ID.
901
 *
902
 * @return string
903
 */
904
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...
905
	$show_content = get_post_meta( $postid, '_give_content_option', true );
906
907
	if (
908
		! get_post_meta( $postid, '_give_content_placement', true )
909
		&& ( 'none' !== $show_content )
910
	) {
911
		$field_value = $show_content;
912
	}
913
914
	return $field_value;
915
}
916
917
add_filter( '_give_content_placement_field_value', '_give_content_placement_field_value', 10, 3 );
918
919
920
/**
921
 * Set value for Terms and Conditions --> Terms and Conditions field setting.
922
 *
923
 * Backward compatibility:  set value by _give_terms_option form meta field value if it's value is none.
924
 *
925
 * @since  1.8
926
 *
927
 * @param  mixed $field_value Field Value.
928
 * @param  array $field       Field args.
929
 * @param  int   $postid      Form/Post ID.
930
 *
931
 * @return string
932
 */
933
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...
934
	$term_option = get_post_meta( $postid, '_give_terms_option', true );
935
936
	if ( in_array( $term_option, array( 'none', 'yes' ) ) ) {
937
		$field_value = ( 'yes' === $term_option ? 'enabled' : 'disabled' );
938
	}
939
940
	return $field_value;
941
}
942
943
add_filter( '_give_terms_option_field_value', '_give_terms_option_field_value', 10, 3 );
944
945
946
/**
947
 * Set value for Form Display --> Offline Donation --> Billing Fields.
948
 *
949
 * Backward compatibility:  set value by _give_offline_donation_enable_billing_fields_single form meta field value if
950
 * it's value is on.
951
 *
952
 * @since  1.8
953
 *
954
 * @param  mixed $field_value Field Value.
955
 * @param  array $field       Field args.
956
 * @param  int   $postid      Form/Post ID.
957
 *
958
 * @return string
959
 */
960
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...
961
	$offline_donation = get_post_meta( $postid, '_give_offline_donation_enable_billing_fields_single', true );
962
963
	if ( 'on' === $offline_donation ) {
964
		$field_value = 'enabled';
965
	}
966
967
	return $field_value;
968
}
969
970
add_filter( '_give_offline_donation_enable_billing_fields_single_field_value', '_give_offline_donation_enable_billing_fields_single_field_value', 10, 3 );
971
972
973
/**
974
 * Set value for Donation Options --> Custom Amount.
975
 *
976
 * Backward compatibility:  set value by _give_custom_amount form meta field value if it's value is yes or no.
977
 *
978
 * @since  1.8
979
 *
980
 * @param  mixed $field_value Field Value.
981
 * @param  array $field       Field args.
982
 * @param  int   $postid      Form/Post ID.
983
 *
984
 * @return string
985
 */
986
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...
987
	$custom_amount = get_post_meta( $postid, '_give_custom_amount', true );
988
989
	if ( in_array( $custom_amount, array( 'yes', 'no' ) ) ) {
990
		$field_value = ( 'yes' === $custom_amount ? 'enabled' : 'disabled' );
991
	}
992
993
	return $field_value;
994
}
995
996
add_filter( '_give_custom_amount_field_value', '_give_custom_amount_field_value', 10, 3 );
997
998
999
/**
1000
 * Set value for Donation Goal --> Donation Goal.
1001
 *
1002
 * Backward compatibility:  set value by _give_goal_option form meta field value if it's value is yes or no.
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_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...
1013
	$goal_option = get_post_meta( $postid, '_give_goal_option', true );
1014
1015
	if ( in_array( $goal_option, array( 'yes', 'no' ) ) ) {
1016
		$field_value = ( 'yes' === $goal_option ? 'enabled' : 'disabled' );
1017
	}
1018
1019
	return $field_value;
1020
}
1021
1022
add_filter( '_give_goal_option_field_value', '_give_goal_option_field_value', 10, 3 );
1023
1024
/**
1025
 * Set value for Donation Goal --> close Form.
1026
 *
1027
 * Backward compatibility:  set value by _give_close_form_when_goal_achieved form meta field value if it's value is yes
1028
 * or no.
1029
 *
1030
 * @since  1.8
1031
 *
1032
 * @param  mixed $field_value Field Value.
1033
 * @param  array $field       Field args.
1034
 * @param  int   $postid      Form/Post ID.
1035
 *
1036
 * @return string
1037
 */
1038
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...
1039
	$close_form = get_post_meta( $postid, '_give_close_form_when_goal_achieved', true );
1040
1041
	if ( in_array( $close_form, array( 'yes', 'no' ) ) ) {
1042
		$field_value = ( 'yes' === $close_form ? 'enabled' : 'disabled' );
1043
	}
1044
1045
	return $field_value;
1046
}
1047
1048
add_filter( '_give_close_form_when_goal_achieved_field_value', '_give_close_form_when_goal_achieved_value', 10, 3 );
1049
1050
1051
/**
1052
 * Set value for Form display --> Guest Donation.
1053
 *
1054
 * Backward compatibility:  set value by _give_logged_in_only form meta field value if it's value is yes or no.
1055
 *
1056
 * @since  1.8
1057
 *
1058
 * @param  mixed $field_value Field Value.
1059
 * @param  array $field       Field args.
1060
 * @param  int   $postid      Form/Post ID.
1061
 *
1062
 * @return string
1063
 */
1064
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...
1065
	$guest_donation = get_post_meta( $postid, '_give_logged_in_only', true );
1066
1067
	if ( in_array( $guest_donation, array( 'yes', 'no' ) ) ) {
1068
		$field_value = ( 'yes' === $guest_donation ? 'enabled' : 'disabled' );
1069
	}
1070
1071
	return $field_value;
1072
}
1073
1074
add_filter( '_give_logged_in_only_field_value', '_give_logged_in_only_value', 10, 3 );
1075
1076
/**
1077
 * Set value for Offline Donations --> Offline Donations.
1078
 *
1079
 * Backward compatibility:  set value by _give_customize_offline_donations form meta field value if it's value is yes
1080
 * or no.
1081
 *
1082
 * @since  1.8
1083
 *
1084
 * @param  mixed $field_value Field Value.
1085
 * @param  array $field       Field args.
1086
 * @param  int   $postid      Form/Post ID.
1087
 *
1088
 * @return string
1089
 */
1090
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...
1091
	$customize_offline_text = get_post_meta( $postid, '_give_customize_offline_donations', true );
1092
1093
	if ( in_array( $customize_offline_text, array( 'yes', 'no' ) ) ) {
1094
		$field_value = ( 'yes' === $customize_offline_text ? 'enabled' : 'disabled' );
1095
	}
1096
1097
	return $field_value;
1098
}
1099
1100
add_filter( '_give_customize_offline_donations_field_value', '_give_customize_offline_donations_value', 10, 3 );
1101
1102
1103
/**
1104
 * Set repeater field id for multi donation form.
1105
 *
1106
 * @since 1.8
1107
 *
1108
 * @param int   $field_id
1109
 * @param array $field
1110
 * @param array $fields
1111
 * @param bool  $default
1112
 *
1113
 * @return mixed
1114
 */
1115
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...
1116
	$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1117
	$field_id        = "{$fields['id']}[{$row_placeholder}][{$field['id']}][level_id]";
1118
1119
	return $field_id;
1120
}
1121
1122
add_filter( 'give_get_repeater_field__give_id_id', '_give_set_multi_level_repeater_field_id', 10, 4 );
1123
1124
/**
1125
 * Set repeater field value for multi donation form.
1126
 *
1127
 * @since 1.8
1128
 *
1129
 * @param string $field_value
1130
 * @param array  $field
1131
 * @param array  $field_group
1132
 * @param array  $fields
1133
 *
1134
 * @return mixed
1135
 */
1136
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...
1137
	$field_value = $field_group[ $field['id'] ]['level_id'];
1138
1139
	return $field_value;
1140
}
1141
1142
add_filter( 'give_get_repeater_field__give_id_value', '_give_set_multi_level_repeater_field_value', 10, 4 );
1143
1144
/**
1145
 * Set default value for _give_id field.
1146
 *
1147
 * @since 1.8
1148
 *
1149
 * @param $field
1150
 *
1151
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
1152
 */
1153
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...
1154
	return 0;
1155
}
1156
1157
add_filter( 'give_default_field_group_field__give_id_value', '_give_set_field_give_id_default_value' );
1158
1159
/**
1160
 * Set default value for _give_default field.
1161
 *
1162
 * @since 1.8
1163
 *
1164
 * @param $field
1165
 *
1166
 * @return string
1167
 */
1168
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...
1169
	return 'default';
1170
}
1171
1172
add_filter( 'give_default_field_group_field__give_default_value', '_give_set_field_give_default_default_value' );
1173
1174
/**
1175
 * Set repeater field editor id for field type wysiwyg.
1176
 *
1177
 * @since 1.8
1178
 *
1179
 * @param $field_name
1180
 * @param $field
1181
 *
1182
 * @return string
1183
 */
1184
function give_repeater_field_set_editor_id( $field_name, $field ) {
1185
	if ( isset( $field['repeatable_field_id'] ) && 'wysiwyg' == $field['type'] ) {
1186
		$field_name = '_give_repeater_' . uniqid() . '_wysiwyg';
1187
	}
1188
1189
	return $field_name;
1190
}
1191
1192
add_filter( 'give_get_field_name', 'give_repeater_field_set_editor_id', 10, 2 );
1193