Completed
Push — issues/1038 ( b3f622...a20520 )
by Ravinder
17:30
created

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

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 19
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
	$field = give_backward_compatibility_setting_api_1_8( $field );
126
127
	// Call render function.
128
	if ( is_array( $func_name ) ) {
129
		$func_name[0]->$func_name[1]( $field );
130
	} else {
131
		$func_name( $field );
132
	}
133
134
	return true;
135
}
136
137
/**
138
 * Output a text input box.
139
 *
140
 * @since  1.8
141
 * @since  1.9 Render field with field api
142
 *
143
 * @param array $field Field arguments
144
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
145
 *
146
 * @return void
147
 */
148
function give_text_input( $field ) {
149
	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...
150
151
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
152
	$field['value'] = give_get_field_value( $field, $thepostid );
153
	$data_type      = empty( $field['data_type'] ) ? '' : $field['data_type'];
154
155
	switch ( $data_type ) {
156
		case 'price' :
157
			$field['value'] = ( ! empty( $field['value'] ) ? give_format_amount( $field['value'] ) : $field['value'] );
158
159
			$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>' : '' );
160
			$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>' : '' );
161
			break;
162
163
		case 'decimal' :
164
			$field['attributes']['class'] .= ' give_input_decimal';
165
			$field['value'] = ( ! empty( $field['value'] ) ? give_format_decimal( $field['value'] ) : $field['value'] );
166
			break;
167
168
		default :
169
			break;
170
	}
171
172
	// $field = give_backward_compatibility_setting_api_1_8( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
173
174
	// Set description.
175
	// Backward compatibility ( 1.8=<version>1.9).
176
	$field['after_field'] = ! empty( $field['after_field'] )
177
		? $field['after_field'] . give_get_field_description( $field )
178
		: give_get_field_description( $field );
179
180
	// Reset label for repeater field compatibility.
181
	$field['name'] = give_get_field_name( $field );
182
183
	// Render Field.
184
	echo Give_Fields_API::render_tag( $field );
185
}
186
187
/**
188
 * Output a hidden input box.
189
 *
190
 * @since  1.8
191
 * @since  1.9 Render field with field api
192
 *
193
 * @param array $field Field arguments
194
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
195
 *
196
 * @return void
197
 */
198
function give_hidden_input( $field ) {
199
	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...
200
201
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
202
	$field['value'] = give_get_field_value( $field, $thepostid );
203
204
	// $field = give_backward_compatibility_setting_api_1_8( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
205
206
	// Reset label for repeater field compatibility.
207
	$field['name'] = give_get_field_name( $field );
208
209
	// Render Field.
210
	echo Give_Fields_API::render_tag( $field );
211
}
212
213
/**
214
 * Output a textarea input box.
215
 *
216
 * @since  1.8
217
 * @since  1.9 Render field with field api
218
 *
219
 * @param array $field Field arguments
220
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
221
 *
222
 * @return void
223
 */
224
function give_textarea_input( $field ) {
225
	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...
226
227
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
228
	$field['value'] = give_get_field_value( $field, $thepostid );
229
230
	// $field = give_backward_compatibility_setting_api_1_8( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
231
232
	// Set description.
233
	// Backward compatibility ( 1.8=<version>1.9).
234
	$field['after_field'] = ! empty( $field['after_field'] )
235
		? $field['after_field'] . give_get_field_description( $field )
236
		: give_get_field_description( $field );
237
238
	// Reset label for repeater field compatibility.
239
	$field['name'] = give_get_field_name( $field );
240
241
	// Render Field.
242
	echo Give_Fields_API::render_tag( $field );
243
}
244
245
/**
246
 * Output a wysiwyg.
247
 *
248
 * @since  1.8
249
 *
250
 * @param  array $field         {
251
 *                              Optional. Array of WordPress editor field arguments.
252
 *
253
 * @type string  $id            Field ID. Default ''.
254
 * @type string  $style         CSS style for input field. Default ''.
255
 * @type string  $wrapper_class CSS class to use for wrapper of input field. Default ''.
256
 * @type string  $value         Value of input field. Default ''.
257
 * @type string  $name          Name of input field. Default ''.
258
 * @type string  $description   Description of input field. Default ''.
259
 * @type array   $attributes    List of attributes of input field. Default array().
260
 *                                               for example: 'attributes' => array( 'placeholder' => '*****', 'class'
261
 *                                               => '****' )
262
 * }
263
 * @return void
264
 */
265
function give_wysiwyg( $field ) {
266
	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...
267
268
	$thepostid                = empty( $thepostid ) ? $post->ID : $thepostid;
269
	$field['value']           = give_get_field_value( $field, $thepostid );
270
	$field['unique_field_id'] = give_get_field_name( $field );
271
	$field['wrapper_type']    = 'div';
272
273
	// $field = give_backward_compatibility_setting_api_1_8( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
274
275
	// Set description.
276
	// Backward compatibility ( 1.8=<version>1.9).
277
	$field['after_field'] = ! empty( $field['after_field'] )
278
		? $field['after_field'] . give_get_field_description( $field )
279
		: give_get_field_description( $field );
280
281
	// Render Field.
282
	echo Give_Fields_API::render_tag( $field );
283
}
284
285
/**
286
 * Output a checkbox input box.
287
 *
288
 * @since  1.8
289
 * @since  1.9 Render field with field api
290
 *
291
 * @param array $field Field arguments
292
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
293
 *
294
 * @return void
295
 */
296
function give_checkbox( $field ) {
297
	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...
298
299
	$thepostid        = empty( $thepostid ) ? $post->ID : $thepostid;
300
	$field['value']   = give_get_field_value( $field, $thepostid );
301
	$field['cbvalue'] = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'on';
302
303
	// $field = give_backward_compatibility_setting_api_1_8( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
304
305
	// Set description.
306
	// Backward compatibility ( 1.8=<version>1.9).
307
	$field['after_field'] = ! empty( $field['after_field'] )
308
		? $field['after_field'] . give_get_field_description( $field )
309
		: give_get_field_description( $field );
310
311
	// Reset label for repeater field compatibility.
312
	$field['name'] = give_get_field_name( $field );
313
314
	// Render Field.
315
	echo Give_Fields_API::render_tag( $field );
316
}
317
318
/**
319
 * Output a select input box.
320
 *
321
 * @since  1.8
322
 * @since  1.9 Render field with field api
323
 *
324
 * @param array $field Field arguments
325
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
326
 *
327
 * @return void
328
 */
329
function give_select( $field ) {
330
	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...
331
332
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
333
	$field['value'] = give_get_field_value( $field, $thepostid );
334
335
	// $field = give_backward_compatibility_setting_api_1_8( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
336
337
	// Set description.
338
	// Backward compatibility ( 1.8=<version>1.9).
339
	$field['after_field'] = ! empty( $field['after_field'] )
340
		? $field['after_field'] . give_get_field_description( $field )
341
		: give_get_field_description( $field );
342
343
	// Reset label for repeater field compatibility.
344
	$field['name'] = give_get_field_name( $field );
345
346
	// Render Field.
347
	echo Give_Fields_API::render_tag( $field );
348
}
349
350
/**
351
 * Output a radio input box.
352
 *
353
 * @since  1.8
354
 * @since  1.9 Render field with field api
355
 *
356
 * @param array $field Field arguments
357
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
358
 * @param array $field
359
 *
360
 * @return void
361
 */
362
function give_radio( $field ) {
363
	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...
364
365
	$thepostid             = empty( $thepostid ) ? $post->ID : $thepostid;
366
	$field['value']        = give_get_field_value( $field, $thepostid );
367
	$field['wrapper_type'] = 'fieldset';
368
369
	// $field = give_backward_compatibility_setting_api_1_8( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
370
371
	// Set description.
372
	// Backward compatibility ( 1.8=<version>1.9).
373
	$field['after_field'] = ! empty( $field['after_field'] )
374
		? $field['after_field'] . give_get_field_description( $field )
375
		: give_get_field_description( $field );
376
377
	// Reset label for repeater field compatibility.
378
	$field['name'] = give_get_field_name( $field );
379
380
	// Render Field.
381
	echo Give_Fields_API::render_tag( $field );
382
}
383
384
/**
385
 * Output a colorpicker.
386
 *
387
 * @since  1.8
388
 * @since  1.9 Render field with field api
389
 *
390
 * @param array $field Field arguments
391
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
392
 * @param array $field
393
 *
394
 * @return void
395
 */
396
function give_colorpicker( $field ) {
397
	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...
398
399
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
400
	$field['value'] = give_get_field_value( $field, $thepostid );
401
	$field['type']  = 'text';
402
403
	// $field = give_backward_compatibility_setting_api_1_8( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
404
405
	// Set description.
406
	// Backward compatibility ( 1.8=<version>1.9).
407
	$field['after_field'] = ! empty( $field['after_field'] )
408
		? $field['after_field'] . give_get_field_description( $field )
409
		: give_get_field_description( $field );
410
411
	// Reset label for repeater field compatibility.
412
	$field['name'] = give_get_field_name( $field );
413
414
	// Render Field.
415
	echo Give_Fields_API::render_tag( $field );
416
}
417
418
419
/**
420
 * Output a media upload field.
421
 *
422
 * @since  1.8
423
 * @since  1.9 Render field with field api
424
 *
425
 * @param array $field Field arguments
426
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
427
 * @param array $field
428
 *
429
 * @return void
430
 */
431
function give_media( $field ) {
432
	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...
433
434
	// $field = give_backward_compatibility_setting_api_1_8( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
435
436
	$thepostid      = empty( $thepostid ) ? $post->ID : $thepostid;
437
	$field['value'] = give_get_field_value( $field, $thepostid );
438
439
	// Set description.
440
	// Backward compatibility ( 1.8=<version>1.9).
441
	$field['after_field'] = ! empty( $field['after_field'] )
442
		? $field['after_field'] . give_get_field_description( $field )
443
		: give_get_field_description( $field );
444
445
	// Reset label for repeater field compatibility.
446
	$field['name'] = give_get_field_name( $field );
447
448
	// Render Field.
449
	echo Give_Fields_API::render_tag( $field );
450
}
451
452
/**
453
 * Output a select field with payment options list.
454
 *
455
 * @since  1.8
456
 *
457
 * @param  array $field
458
 *
459
 * @return void
460
 */
461
function give_default_gateway( $field ) {
462
	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...
463
464
	// get all active payment gateways.
465
	$gateways         = give_get_enabled_payment_gateways( $thepostid );
466
	$field['options'] = array();
467
468
	// Set field option value.
469
	if ( ! empty( $gateways ) ) {
470
		foreach ( $gateways as $key => $option ) {
471
			$field['options'][ $key ] = $option['admin_label'];
472
		}
473
	}
474
475
	// Add a field to the Give Form admin single post view of this field
476
	if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
477
		$field['options'] = array_merge( array( 'global' => esc_html__( 'Global Default', 'give' ) ), $field['options'] );
478
	}
479
480
	$field['type'] = 'select';
481
482
	// Render select field.
483
	give_select( $field );
484
}
485
486
/**
487
 * Output the documentation link.
488
 *
489
 * @since  1.8
490
 * @since  1.9 Render field with field api
491
 *
492
 * @param array $field Field arguments
493
 *                     Check includes/forms/api/class-give-field-api.php:28 for arguments.
494
 *
495
 * @return void
496
 */
497
498
function give_docs_link( $field ) {
499
	// $field = give_backward_compatibility_setting_api_1_8( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
500
501
	// Set default class.
502
	// Backward compatibility ( 1.8=<version>1.9).
503
	$field['wrapper_attributes']['class'] = ! empty( $field['wrapper_attributes']['class'] )
504
		? "{$field['wrapper_attributes']['class']} give-docs-link"
505
		: 'give-docs-link';
506
507
	// Render Field.
508
	echo Give_Fields_API::render_tag( $field );
509
}
510
511
/**
512
 * Get setting field value.
513
 *
514
 * Note: Use only for single post, page or custom post type.
515
 *
516
 * @since  1.8
517
 *
518
 * @param  array $field
519
 * @param  int   $postid
520
 *
521
 * @return mixed
522
 */
523
function give_get_field_value( $field, $postid ) {
524
	if ( isset( $field['attributes']['value'] ) ) {
525
		return $field['attributes']['value'];
526
	}
527
528
	// Get value from db.
529
	$field_value = get_post_meta( $postid, $field['id'], true );
530
531
	/**
532
	 * Filter the field value before apply default value.
533
	 *
534
	 * @since 1.8
535
	 *
536
	 * @param mixed $field_value Field value.
537
	 */
538
	$field_value = apply_filters( "{$field['id']}_field_value", $field_value, $field, $postid );
539
540
	// Set default value if no any data saved to db.
541
	if ( ! $field_value && isset( $field['default'] ) ) {
542
		$field_value = $field['default'];
543
	}
544
545
	return $field_value;
546
}
547
548
549
/**
550
 * Get field description html.
551
 *
552
 * @since 1.8
553
 *
554
 * @param $field
555
 *
556
 * @return string
557
 */
558
function give_get_field_description( $field ) {
559
	$field_desc_html = '';
560
	if ( ! empty( $field['description'] ) ) {
561
		$field_desc_html = '<span class="give-field-description">' . wp_kses_post( $field['description'] ) . '</span>';
562
	}
563
564
	return $field_desc_html;
565
}
566
567
568
/**
569
 * Get field custom attributes as string.
570
 *
571
 * @since 1.8
572
 *
573
 * @param $field
574
 *
575
 * @return string
576
 */
577
function give_get_custom_attributes( $field ) {
578
	// Custom attribute handling
579
	$custom_attributes = array();
580
581
	if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
582
583
		foreach ( $field['attributes'] as $attribute => $value ) {
584
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
585
		}
586
	}
587
588
	return implode( ' ', $custom_attributes );
589
}
590
591
/**
592
 * Get repeater field value.
593
 *
594
 * Note: Use only for single post, page or custom post type.
595
 *
596
 * @since  1.8
597
 *
598
 * @param array $field
599
 * @param array $field_group
600
 * @param array $fields
601
 *
602
 * @return string
603
 */
604
function give_get_repeater_field_value( $field, $field_group, $fields ) {
605
	$field_value = ( isset( $field_group[ $field['id'] ] ) ? $field_group[ $field['id'] ] : '' );
606
607
	/**
608
	 * Filter the specific repeater field value
609
	 *
610
	 * @since 1.8
611
	 *
612
	 * @param string $field_id
613
	 */
614
	$field_value = apply_filters( "give_get_repeater_field_{$field['id']}_value", $field_value, $field, $field_group, $fields );
615
616
	/**
617
	 * Filter the repeater field value
618
	 *
619
	 * @since 1.8
620
	 *
621
	 * @param string $field_id
622
	 */
623
	$field_value = apply_filters( 'give_get_repeater_field_value', $field_value, $field, $field_group, $fields );
624
625
	return $field_value;
626
}
627
628
/**
629
 * Get repeater field id.
630
 *
631
 * Note: Use only for single post, page or custom post type.
632
 *
633
 * @since  1.8
634
 *
635
 * @param array    $field
636
 * @param array    $fields
637
 * @param int|bool $default
638
 *
639
 * @return string
640
 */
641
function give_get_repeater_field_id( $field, $fields, $default = false ) {
642
	$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
643
644
	// Get field id.
645
	$field_id = "{$fields['id']}[{$row_placeholder}][{$field['id']}]";
646
647
	/**
648
	 * Filter the specific repeater field id
649
	 *
650
	 * @since 1.8
651
	 *
652
	 * @param string $field_id
653
	 */
654
	$field_id = apply_filters( "give_get_repeater_field_{$field['id']}_id", $field_id, $field, $fields, $default );
655
656
	/**
657
	 * Filter the repeater field id
658
	 *
659
	 * @since 1.8
660
	 *
661
	 * @param string $field_id
662
	 */
663
	$field_id = apply_filters( 'give_get_repeater_field_id', $field_id, $field, $fields, $default );
664
665
	return $field_id;
666
}
667
668
669
/**
670
 * Get field name.
671
 *
672
 * @since  1.8
673
 *
674
 * @param  array $field
675
 *
676
 * @return string
677
 */
678
function give_get_field_name( $field ) {
679
	$field_name = esc_attr( empty( $field['repeat'] ) ? $field['id'] : $field['repeatable_field_id'] );
680
681
	/**
682
	 * Filter the field name.
683
	 *
684
	 * @since 1.8
685
	 *
686
	 * @param string $field_name
687
	 */
688
	$field_name = apply_filters( 'give_get_field_name', $field_name, $field );
689
690
	return $field_name;
691
}
692
693
/**
694
 * Output repeater field or multi donation type form on donation from edit screen.
695
 * Note: internal use only.
696
 *
697
 * @since  1.8
698
 *
699
 * @param  array $fields
700
 *
701
 * @return void
702
 */
703
function _give_metabox_form_data_repeater_fields( $fields ) {
704
	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...
705
	$fields            = give_backward_compatibility_setting_api_1_8( $fields );
706
	$fields['value']   = get_post_meta( $thepostid, $fields['id'], true );
707
	$fields['wrapper'] = false;
708
709
	echo Give_Fields_API::render_tag( $fields );
710
}
711
712
713
/**
714
 * Get current setting tab.
715
 *
716
 * @since  1.8
717
 * @return string
718
 */
719
function give_get_current_setting_tab() {
720
	// Get current setting page.
721
	$current_setting_page = give_get_current_setting_page();
722
723
	/**
724
	 * Filter the default tab for current setting page.
725
	 *
726
	 * @since 1.8
727
	 *
728
	 * @param string
729
	 */
730
	$default_current_tab = apply_filters( "give_default_setting_tab_{$current_setting_page}", 'general' );
731
732
	// Get current tab.
733
	$current_tab = empty( $_GET['tab'] ) ? $default_current_tab : urldecode( $_GET['tab'] );
734
735
	// Output.
736
	return $current_tab;
737
}
738
739
740
/**
741
 * Get current setting section.
742
 *
743
 * @since  1.8
744
 * @return string
745
 */
746
function give_get_current_setting_section() {
747
	// Get current tab.
748
	$current_tab = give_get_current_setting_tab();
749
750
	/**
751
	 * Filter the default section for current setting page tab.
752
	 *
753
	 * @since 1.8
754
	 *
755
	 * @param string
756
	 */
757
	$default_current_section = apply_filters( "give_default_setting_tab_section_{$current_tab}", '' );
758
759
	// Get current section.
760
	$current_section = empty( $_REQUEST['section'] ) ? $default_current_section : urldecode( $_REQUEST['section'] );
761
762
	// Output.
763
	return $current_section;
764
}
765
766
/**
767
 * Get current setting page.
768
 *
769
 * @since  1.8
770
 * @return string
771
 */
772
function give_get_current_setting_page() {
773
	// Get current page.
774
	$setting_page = ! empty( $_GET['page'] ) ? urldecode( $_GET['page'] ) : '';
775
776
	// Output.
777
	return $setting_page;
778
}
779
780
/**
781
 * Set value for Form content --> Display content field setting.
782
 *
783
 * Backward compatibility:  set value by _give_content_option form meta field value if _give_display_content is not set
784
 * yet.
785
 *
786
 * @since  1.8
787
 *
788
 * @param  mixed $field_value Field Value.
789
 * @param  array $field       Field args.
790
 * @param  int   $postid      Form/Post ID.
791
 *
792
 * @return string
793
 */
794
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...
795
	$show_content = get_post_meta( $postid, '_give_content_option', true );
796
797
	if (
798
		! get_post_meta( $postid, '_give_display_content', true )
799
		&& $show_content
800
		&& ( 'none' !== $show_content )
801
	) {
802
		$field_value = 'enabled';
803
	}
804
805
	return $field_value;
806
}
807
808
add_filter( '_give_display_content_field_value', '_give_display_content_field_value', 10, 3 );
809
810
811
/**
812
 * Set value for Form content --> Content placement field setting.
813
 *
814
 * Backward compatibility:  set value by _give_content_option form meta field value if _give_content_placement is not
815
 * set yet.
816
 *
817
 * @since  1.8
818
 *
819
 * @param  mixed $field_value Field Value.
820
 * @param  array $field       Field args.
821
 * @param  int   $postid      Form/Post ID.
822
 *
823
 * @return string
824
 */
825
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...
826
	$show_content = get_post_meta( $postid, '_give_content_option', true );
827
828
	if (
829
		! get_post_meta( $postid, '_give_content_placement', true )
830
		&& ( 'none' !== $show_content )
831
	) {
832
		$field_value = $show_content;
833
	}
834
835
	return $field_value;
836
}
837
838
add_filter( '_give_content_placement_field_value', '_give_content_placement_field_value', 10, 3 );
839
840
841
/**
842
 * Set value for Terms and Conditions --> Terms and Conditions field setting.
843
 *
844
 * Backward compatibility:  set value by _give_terms_option form meta field value if it's value is none.
845
 *
846
 * @since  1.8
847
 *
848
 * @param  mixed $field_value Field Value.
849
 * @param  array $field       Field args.
850
 * @param  int   $postid      Form/Post ID.
851
 *
852
 * @return string
853
 */
854
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...
855
	$term_option = get_post_meta( $postid, '_give_terms_option', true );
856
857
	if ( in_array( $term_option, array( 'none', 'yes' ) ) ) {
858
		$field_value = ( 'yes' === $term_option ? 'enabled' : 'disabled' );
859
	}
860
861
	return $field_value;
862
}
863
864
add_filter( '_give_terms_option_field_value', '_give_terms_option_field_value', 10, 3 );
865
866
867
/**
868
 * Set value for Form Display --> Offline Donation --> Billing Fields.
869
 *
870
 * Backward compatibility:  set value by _give_offline_donation_enable_billing_fields_single form meta field value if
871
 * it's value is on.
872
 *
873
 * @since  1.8
874
 *
875
 * @param  mixed $field_value Field Value.
876
 * @param  array $field       Field args.
877
 * @param  int   $postid      Form/Post ID.
878
 *
879
 * @return string
880
 */
881
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...
882
	$offline_donation = get_post_meta( $postid, '_give_offline_donation_enable_billing_fields_single', true );
883
884
	if ( 'on' === $offline_donation ) {
885
		$field_value = 'enabled';
886
	}
887
888
	return $field_value;
889
}
890
891
add_filter( '_give_offline_donation_enable_billing_fields_single_field_value', '_give_offline_donation_enable_billing_fields_single_field_value', 10, 3 );
892
893
894
/**
895
 * Set value for Donation Options --> Custom Amount.
896
 *
897
 * Backward compatibility:  set value by _give_custom_amount form meta field value if it's value is yes or no.
898
 *
899
 * @since  1.8
900
 *
901
 * @param  mixed $field_value Field Value.
902
 * @param  array $field       Field args.
903
 * @param  int   $postid      Form/Post ID.
904
 *
905
 * @return string
906
 */
907
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...
908
	$custom_amount = get_post_meta( $postid, '_give_custom_amount', true );
909
910
	if ( in_array( $custom_amount, array( 'yes', 'no' ) ) ) {
911
		$field_value = ( 'yes' === $custom_amount ? 'enabled' : 'disabled' );
912
	}
913
914
	return $field_value;
915
}
916
917
add_filter( '_give_custom_amount_field_value', '_give_custom_amount_field_value', 10, 3 );
918
919
920
/**
921
 * Set value for Donation Goal --> Donation Goal.
922
 *
923
 * Backward compatibility:  set value by _give_goal_option form meta field value if it's value is yes or no.
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_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...
934
	$goal_option = get_post_meta( $postid, '_give_goal_option', true );
935
936
	if ( in_array( $goal_option, array( 'yes', 'no' ) ) ) {
937
		$field_value = ( 'yes' === $goal_option ? 'enabled' : 'disabled' );
938
	}
939
940
	return $field_value;
941
}
942
943
add_filter( '_give_goal_option_field_value', '_give_goal_option_field_value', 10, 3 );
944
945
/**
946
 * Set value for Donation Goal --> close Form.
947
 *
948
 * Backward compatibility:  set value by _give_close_form_when_goal_achieved form meta field value if it's value is yes
949
 * or no.
950
 *
951
 * @since  1.8
952
 *
953
 * @param  mixed $field_value Field Value.
954
 * @param  array $field       Field args.
955
 * @param  int   $postid      Form/Post ID.
956
 *
957
 * @return string
958
 */
959
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...
960
	$close_form = get_post_meta( $postid, '_give_close_form_when_goal_achieved', true );
961
962
	if ( in_array( $close_form, array( 'yes', 'no' ) ) ) {
963
		$field_value = ( 'yes' === $close_form ? 'enabled' : 'disabled' );
964
	}
965
966
	return $field_value;
967
}
968
969
add_filter( '_give_close_form_when_goal_achieved_field_value', '_give_close_form_when_goal_achieved_value', 10, 3 );
970
971
972
/**
973
 * Set value for Form display --> Guest Donation.
974
 *
975
 * Backward compatibility:  set value by _give_logged_in_only form meta field value if it's value is yes or no.
976
 *
977
 * @since  1.8
978
 *
979
 * @param  mixed $field_value Field Value.
980
 * @param  array $field       Field args.
981
 * @param  int   $postid      Form/Post ID.
982
 *
983
 * @return string
984
 */
985
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...
986
	$guest_donation = get_post_meta( $postid, '_give_logged_in_only', true );
987
988
	if ( in_array( $guest_donation, array( 'yes', 'no' ) ) ) {
989
		$field_value = ( 'yes' === $guest_donation ? 'enabled' : 'disabled' );
990
	}
991
992
	return $field_value;
993
}
994
995
add_filter( '_give_logged_in_only_field_value', '_give_logged_in_only_value', 10, 3 );
996
997
/**
998
 * Set value for Offline Donations --> Offline Donations.
999
 *
1000
 * Backward compatibility:  set value by _give_customize_offline_donations form meta field value if it's value is yes
1001
 * or no.
1002
 *
1003
 * @since  1.8
1004
 *
1005
 * @param  mixed $field_value Field Value.
1006
 * @param  array $field       Field args.
1007
 * @param  int   $postid      Form/Post ID.
1008
 *
1009
 * @return string
1010
 */
1011
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...
1012
	$customize_offline_text = get_post_meta( $postid, '_give_customize_offline_donations', true );
1013
1014
	if ( in_array( $customize_offline_text, array( 'yes', 'no' ) ) ) {
1015
		$field_value = ( 'yes' === $customize_offline_text ? 'enabled' : 'disabled' );
1016
	}
1017
1018
	return $field_value;
1019
}
1020
1021
add_filter( '_give_customize_offline_donations_field_value', '_give_customize_offline_donations_value', 10, 3 );
1022
1023
1024
/**
1025
 * Set repeater field id for multi donation form.
1026
 *
1027
 * @since 1.8
1028
 *
1029
 * @param int   $field_id
1030
 * @param array $field
1031
 * @param array $fields
1032
 * @param bool  $default
1033
 *
1034
 * @return mixed
1035
 */
1036
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...
1037
	$row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1038
	$field_id        = "{$fields['id']}[{$row_placeholder}][{$field['id']}][level_id]";
1039
1040
	return $field_id;
1041
}
1042
1043
add_filter( 'give_get_repeater_field__give_id_id', '_give_set_multi_level_repeater_field_id', 10, 4 );
1044
1045
/**
1046
 * Set repeater field value for multi donation form.
1047
 *
1048
 * @since 1.8
1049
 *
1050
 * @param string $field_value
1051
 * @param array  $field
1052
 * @param array  $field_group
1053
 * @param array  $fields
1054
 *
1055
 * @return mixed
1056
 */
1057
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...
1058
	$field_value = $field_group[ $field['id'] ]['level_id'];
1059
1060
	return $field_value;
1061
}
1062
1063
add_filter( 'give_get_repeater_field__give_id_value', '_give_set_multi_level_repeater_field_value', 10, 4 );
1064
1065
/**
1066
 * Set default value for _give_id field.
1067
 *
1068
 * @since 1.8
1069
 *
1070
 * @param $field
1071
 *
1072
 * @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...
1073
 */
1074
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...
1075
	return 0;
1076
}
1077
1078
add_filter( 'give_default_field_group_field__give_id_value', '_give_set_field_give_id_default_value' );
1079
1080
/**
1081
 * Set default value for _give_default field.
1082
 *
1083
 * @since 1.8
1084
 *
1085
 * @param $field
1086
 *
1087
 * @return string
1088
 */
1089
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...
1090
	return 'default';
1091
}
1092
1093
add_filter( 'give_default_field_group_field__give_default_value', '_give_set_field_give_default_default_value' );
1094