Completed
Pull Request — master (#986)
by Rami
20:36
created

class-give-settings.php ➔ give_license_key_callback()   F

Complexity

Conditions 34
Paths 10240

Size

Total Lines 222
Code Lines 149

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 1190

Importance

Changes 8
Bugs 3 Features 1
Metric Value
cc 34
eloc 149
nc 10240
nop 5
dl 0
loc 222
ccs 0
cts 41
cp 0
crap 1190
rs 2
c 8
b 3
f 1

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
1 ignored issue
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 13 and the first side effect is on line 811.

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
/**
4
 * Class Give_Plugin_Settings
5
 *
6
 * Register settings Include and setup custom metaboxes and fields.
7
 *
8
 * @package    Give
9
 * @subpackage Admin
10
 * @license    http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
11
 * @link       https://github.com/webdevstudios/Custom-Metaboxes-and-Fields-for-WordPress
12
 */
13
class Give_Plugin_Settings {
14
15
	/**
16
	 * Option key, and option page slug
17
	 * @var string
18
	 */
19
	private $key = 'give_settings';
20
21
	/**
22
	 * Array of metaboxes/fields
23
	 * @var array
24
	 */
25
	protected $option_metabox = array();
26
27
	/**
28
	 * Options Page title
29
	 * @var string
30
	 */
31
	protected $title = '';
32
33
	/**
34
	 * Options Page hook
35
	 * @var string
36
	 */
37
	protected $options_page = '';
38
39
	/**
40
	 * Give_Plugin_Settings constructor.
41
	 */
42
	public function __construct() {
43
44
		add_action( 'admin_init', array( $this, 'init' ) );
45
46
		//Customize CMB2 URL
47
		add_filter( 'cmb2_meta_box_url', array( $this, 'give_update_cmb_meta_box_url' ) );
48
49
		//Custom CMB2 Settings Fields
50
		add_action( 'cmb2_render_give_title', 'give_title_callback', 10, 5 );
51
		add_action( 'cmb2_render_give_description', 'give_description_callback', 10, 5 );
52
		add_action( 'cmb2_render_enabled_gateways', 'give_enabled_gateways_callback', 10, 5 );
53
		add_action( 'cmb2_render_default_gateway', 'give_default_gateway_callback', 10, 5 );
54
		add_action( 'cmb2_render_email_preview_buttons', 'give_email_preview_buttons_callback', 10, 5 );
55
		add_action( 'cmb2_render_system_info', 'give_system_info_callback', 10, 5 );
56
		add_action( 'cmb2_render_api', 'give_api_callback', 10, 5 );
57
		add_action( 'cmb2_render_license_key', 'give_license_key_callback', 10, 5 );
58
		add_action( 'admin_notices', array( $this, 'settings_notices' ) );
59
60
		// Include CMB CSS in the head to avoid FOUC
61
		add_action( 'admin_print_styles-give_forms_page_give-settings', array( 'CMB2_hookup', 'enqueue_cmb_css' ) );
62
63
		add_filter( 'cmb2_get_metabox_form_format', array( $this, 'give_modify_cmb2_form_output' ), 10, 3 );
64
65
	}
66
67
68
	/**
69
	 * Register our setting to WP
70
	 *
71
	 * @since  1.0
72
	 */
73
	public function init() {
74
		register_setting( $this->key, $this->key );
75
76
	}
77
78
79
	/**
80
	 * Filter CMB2 URL
81
	 *
82
	 * Required for CMB2 to properly load CSS/JS.
83
	 *
84
	 * @param $url
85
	 *
86
	 * @return mixed
87
	 */
88
	public function give_update_cmb_meta_box_url( $url ) {
0 ignored issues
show
Unused Code introduced by
The parameter $url 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...
89
		//Path to Give's CMB
90
		return GIVE_PLUGIN_URL . '/includes/libraries/cmb2';
91
	}
92
93
94
	/**
95
	 * Retrieve settings tabs
96
	 *
97
	 * @since 1.0
98
	 * @return array $tabs
99
	 */
100
	public function give_get_settings_tabs() {
101
102
		$settings = $this->give_settings( null );
103
104
		$tabs             = array();
105
		$tabs['general']  = esc_html__( 'General', 'give' );
106
		$tabs['gateways'] = esc_html__( 'Payment Gateways', 'give' );
107
		$tabs['display']  = esc_html__( 'Display Options', 'give' );
108
		$tabs['emails']   = esc_html__( 'Emails', 'give' );
109
110
		if ( ! empty( $settings['addons']['fields'] ) ) {
111
			$tabs['addons'] = esc_html__( 'Add-ons', 'give' );
112
		}
113
114
		if ( ! empty( $settings['licenses']['fields'] ) ) {
115
			$tabs['licenses'] = esc_html__( 'Licenses', 'give' );
116
		}
117
118
		$tabs['advanced']    = esc_html__( 'Advanced', 'give' );
119
		$tabs['api']         = esc_html__( 'API', 'give' );
120
		$tabs['system_info'] = esc_html__( 'System Info', 'give' );
121
122
		return apply_filters( 'give_settings_tabs', $tabs );
123
	}
124
125
126
	/**
127
	 * Admin page markup. Mostly handled by CMB2
128
	 * @since  1.0
129
	 */
130
	public function admin_page_display() {
131
132
		$active_tab = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $this->give_get_settings_tabs() ) ? $_GET['tab'] : 'general';
133
134
		?>
135
136
		<div class="wrap give_settings_page cmb2_options_page <?php echo $this->key; ?>">
137
138
			<h1 class="screen-reader-text"><?php esc_html_e( 'Give Settings', 'give' ); ?></h1>
139
140
			<h2 class="nav-tab-wrapper">
141
				<?php
142
				foreach ( $this->give_get_settings_tabs() as $tab_id => $tab_name ) {
143
144
					$tab_url = esc_url( add_query_arg( array(
145
						'settings-updated' => false,
146
						'tab'              => $tab_id
147
					) ) );
148
149
					$active = $active_tab == $tab_id ? ' nav-tab-active' : '';
150
151
					echo '<a href="' . esc_url( $tab_url ) . '" class="nav-tab' . $active . '" id="tab-' . $tab_id . '">' . esc_html( $tab_name ) . '</a>';
152
153
				}
154
				?>
155
			</h2>
156
157
			<?php cmb2_metabox_form( $this->give_settings( $active_tab ), $this->key ); ?>
158
159
		</div><!-- .wrap -->
160
161
		<?php
162
	}
163
164
165
	/**
166
	 *
167
	 * Modify CMB2 Default Form Output
168
	 *
169
	 * @param string @args
170
	 *
171
	 * @since 1.0
172
	 *
173
	 * @param $form_format
174
	 * @param $object_id
175
	 * @param $cmb
176
	 *
177
	 * @return string
178
	 */
179
	function give_modify_cmb2_form_output( $form_format, $object_id, $cmb ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Unused Code introduced by
The parameter $cmb 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...
180
181
		//only modify the give settings form
182
		if ( 'give_settings' == $object_id ) {
183
184
			return '<form class="cmb-form" method="post" id="%1$s" enctype="multipart/form-data" encoding="multipart/form-data"><input type="hidden" name="give_settings_saved" value="true"><input type="hidden" name="object_id" value="%2$s">%3$s<div class="give-submit-wrap"><input type="submit" name="submit-cmb" value="' . __( 'Save Settings', 'give' ) . '" class="button-primary"></div></form>';
185
186
		}
187
188
		return $form_format;
189
190
	}
191
192
	/**
193
	 * Define General Settings Metabox and field configurations.
194
	 *
195
	 * Filters are provided for each settings section to allow add-ons and other plugins to add their own settings
196
	 *
197
	 * @param $active_tab |string active tab settings; null returns full array
198
	 *
199
	 * @return array
200
	 */
201
	public function give_settings( $active_tab ) {
202
203
		$give_settings = array(
204
			/**
205
			 * General Settings
206
			 */
207
			'general'     => array(
208
				'id'         => 'general_settings',
209
				'give_title' => esc_html__( 'General Settings', 'give' ),
210
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
211
				'fields'     => apply_filters( 'give_settings_general', array(
212
						array(
213
							'name' => esc_html__( 'General Settings', 'give' ),
214
							'desc' => '',
215
							'type' => 'give_title',
216
							'id'   => 'give_title_general_settings_1'
217
						),
218
						array(
219
							'name'    => esc_html__( 'Success Page', 'give' ),
220
							/* translators: %s: [give_receipt] */
221
							'desc'    => sprintf( __( 'The page donors are sent to after completing their donations. The %s shortcode should be on this page.', 'give' ), '<code>[give_receipt]</code>' ),
222
							'id'      => 'success_page',
223
							'type'    => 'select',
224
							'options' => give_cmb2_get_post_options( array(
225
								'post_type'   => 'page',
226
								'numberposts' => - 1
227
							) ),
228
						),
229
						array(
230
							'name'    => esc_html__( 'Failed Donation Page', 'give' ),
231
							'desc'    => esc_html__( 'The page donors are sent to if their donation is cancelled or fails.', 'give' ),
232
							'id'      => 'failure_page',
233
							'type'    => 'select',
234
							'options' => give_cmb2_get_post_options( array(
235
								'post_type'   => 'page',
236
								'numberposts' => - 1
237
							) ),
238
						),
239
						array(
240
							'name'    => esc_html__( 'Donation History Page', 'give' ),
241
							/* translators: %s: [donation_history] */
242
							'desc'    => sprintf( __( 'The page showing a complete donation history for the current user. The %s shortcode should be on this page.', 'give' ), '<code>[donation_history]</code>' ),
243
							'id'      => 'history_page',
244
							'type'    => 'select',
245
							'options' => give_cmb2_get_post_options( array(
246
								'post_type'   => 'page',
247
								'numberposts' => - 1
248
							) ),
249
						),
250
						array(
251
							'name'    => esc_html__( 'Base Country', 'give' ),
252
							'desc'    => esc_html__( 'The country your site operates from.', 'give' ),
253
							'id'      => 'base_country',
254
							'type'    => 'select',
255
							'options' => give_get_country_list(),
256
						),
257
						array(
258
							'name' => esc_html__( 'Currency Settings', 'give' ),
259
							'desc' => '',
260
							'type' => 'give_title',
261
							'id'   => 'give_title_general_settings_2'
262
						),
263
						array(
264
							'name'    => esc_html__( 'Currency', 'give' ),
265
							'desc'    => esc_html__( 'The donation currency. Note that some payment gateways have currency restrictions.', 'give' ),
266
							'id'      => 'currency',
267
							'type'    => 'select',
268
							'options' => give_get_currencies(),
269
							'default' => 'USD',
270
						),
271
						array(
272
							'name'    => esc_html__( 'Currency Position', 'give' ),
273
							'desc'    => esc_html__( 'The position of the currency symbol.', 'give' ),
274
							'id'      => 'currency_position',
275
							'type'    => 'select',
276
							'options' => array(
277
								/* translators: %s: currency symbol */
278
								'before' => sprintf( esc_html__( 'Before - %s10', 'give' ), give_currency_symbol( give_get_currency() ) ),
279
								/* translators: %s: currency symbol */
280
								'after'  => sprintf( esc_html__( 'After - 10%s', 'give' ), give_currency_symbol( give_get_currency() ) )
281
							),
282
							'default' => 'before',
283
						),
284
						array(
285
							'name'            => esc_html__( 'Thousands Separator', 'give' ),
286
							'desc'            => esc_html__( 'The symbol (typically , or .) to separate thousands.', 'give' ),
287
							'id'              => 'thousands_separator',
288
							'type'            => 'text_small',
289
							'sanitization_cb' => 'give_sanitize_thousand_separator',
290
							'default'         => ',',
291
						),
292
						array(
293
							'name'    => esc_html__( 'Decimal Separator', 'give' ),
294
							'desc'    => esc_html__( 'The symbol (usually , or .) to separate decimal points.', 'give' ),
295
							'id'      => 'decimal_separator',
296
							'type'    => 'text_small',
297
							'default' => '.',
298
						),
299
						array(
300
							'name'            => __( 'Number of Decimals', 'give' ),
301
							'desc'            => __( 'The number of decimal points displayed in amounts.', 'give' ),
302
							'id'              => 'number_decimals',
303
							'type'            => 'text_small',
304
							'default'         => 2,
305
							'sanitization_cb' => 'give_sanitize_number_decimals',
306
						),
307
					)
308
				)
309
			),
310
			/**
311
			 * Payment Gateways
312
			 */
313
			'gateways'    => array(
314
				'id'         => 'payment_gateways',
315
				'give_title' => esc_html__( 'Payment Gateways', 'give' ),
316
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
317
				'fields'     => apply_filters( 'give_settings_gateways', array(
318
						array(
319
							'name' => esc_html__( 'Gateways Settings', 'give' ),
320
							'desc' => '',
321
							'id'   => 'give_title_gateway_settings_1',
322
							'type' => 'give_title'
323
						),
324
						array(
325
							'name' => esc_html__( 'Test Mode', 'give' ),
326
							'desc' => esc_html__( 'While in test mode no live donations are processed. To fully use test mode, you must have a sandbox (test) account for the payment gateway you are testing.', 'give' ),
327
							'id'   => 'test_mode',
328
							'type' => 'checkbox'
329
						),
330
						array(
331
							'name' => esc_html__( 'Enabled Gateways', 'give' ),
332
							'desc' => esc_html__( 'Enable your payment gateway. Can be ordered by dragging.', 'give' ),
333
							'id'   => 'gateways',
334
							'type' => 'enabled_gateways'
335
						),
336
						array(
337
							'name' => esc_html__( 'Default Gateway', 'give' ),
338
							'desc' => esc_html__( 'The gateway that will be selected by default.', 'give' ),
339
							'id'   => 'default_gateway',
340
							'type' => 'default_gateway'
341
						),
342
						array(
343
							'name' => esc_html__( 'PayPal Standard', 'give' ),
344
							'desc' => '',
345
							'type' => 'give_title',
346
							'id'   => 'give_title_gateway_settings_2',
347
						),
348
						array(
349
							'name' => esc_html__( 'PayPal Email', 'give' ),
350
							'desc' => esc_html__( 'Enter your PayPal account\'s email.', 'give' ),
351
							'id'   => 'paypal_email',
352
							'type' => 'text_email',
353
						),
354
						array(
355
							'name' => esc_html__( 'PayPal Page Style', 'give' ),
356
							'desc' => esc_html__( 'Enter the name of the page style to use, or leave blank to use the default.', 'give' ),
357
							'id'   => 'paypal_page_style',
358
							'type' => 'text',
359
						),
360
						array(
361
							'name'    => esc_html__( 'PayPal Transaction Type', 'give' ),
362
							'desc'    => esc_html__( 'Nonprofits must verify their status to withdraw donations they receive via PayPal. PayPal users that are not verified nonprofits must demonstrate how their donations will be used, once they raise more than $10,000. By default, Give transactions are sent to PayPal as donations. You may change the transaction type using this option if you feel you may not meet PayPal\'s donation requirements.', 'give' ),
363
							'id'      => 'paypal_button_type',
364
							'type'    => 'radio_inline',
365
							'options' => array(
366
								'donation' => esc_html__( 'Donation', 'give' ),
367
								'standard' => esc_html__( 'Standard Transaction', 'give' )
368
							),
369
							'default' => 'donation',
370
						),
371
						array(
372
							'name' => esc_html__( 'Disable PayPal IPN Verification', 'give' ),
373
							'desc' => esc_html__( 'If donations are not getting marked as complete, then check this box. This forces the site to use a slightly less secure method of verifying donations.', 'give' ),
374
							'id'   => 'disable_paypal_verification',
375
							'type' => 'checkbox'
376
						),
377
						array(
378
							'name' => esc_html__( 'Offline Donations', 'give' ),
379
							'desc' => '',
380
							'type' => 'give_title',
381
							'id'   => 'give_title_gateway_settings_3',
382
						),
383
						array(
384
							'name' => esc_html__( 'Collect Billing Details', 'give' ),
385
							'desc' => esc_html__( 'Enable to request billing details for offline donations. Will appear above offline donation instructions. Can be enabled/disabled per form.', 'give' ),
386
							'id'   => 'give_offline_donation_enable_billing_fields',
387
							'type' => 'checkbox'
388
						),
389
						array(
390
							'name'    => esc_html__( 'Offline Donation Instructions', 'give' ),
391
							'desc'    => esc_html__( 'The following content will appear for all forms when the user selects the offline donation payment option. Note: You may customize the content per form as needed.', 'give' ),
392
							'id'      => 'global_offline_donation_content',
393
							'default' => give_get_default_offline_donation_content(),
394
							'type'    => 'wysiwyg',
395
							'options' => array(
396
								'textarea_rows' => 6,
397
							)
398
						),
399
						array(
400
							'name'    => esc_html__( 'Offline Donation Email Instructions Subject', 'give' ),
401
							'desc'    => esc_html__( 'Enter the subject line for the donation receipt email.', 'give' ),
402
							'id'      => 'offline_donation_subject',
403
							'default' => esc_attr__( '{donation} - Offline Donation Instructions', 'give' ),
404
							'type'    => 'text'
405
						),
406
						array(
407
							'name'    => esc_html__( 'Offline Donation Email Instructions', 'give' ),
408
							'desc'    => esc_html__( 'Enter the instructions you want emailed to the donor after they have submitted the donation form. Most likely this would include important information like mailing address and who to make the check out to.', 'give' ),
409
							'id'      => 'global_offline_donation_email',
410
							'default' => give_get_default_offline_donation_email_content(),
411
							'type'    => 'wysiwyg',
412
							'options' => array(
413
								'textarea_rows' => 6,
414
							)
415
						)
416
					)
417
				)
418
			),
419
			/** Display Settings */
420
			'display'     => array(
421
				'id'         => 'display_settings',
422
				'give_title' => esc_html__( 'Display Settings', 'give' ),
423
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
424
				'fields'     => apply_filters( 'give_settings_display', array(
425
						array(
426
							'name' => esc_html__( 'Display Settings', 'give' ),
427
							'desc' => '',
428
							'id'   => 'give_title_display_settings_1',
429
							'type' => 'give_title'
430
						),
431
						array(
432
							'name' => esc_html__( 'Disable CSS', 'give' ),
433
							'desc' => esc_html__( 'Enable this option if you would like to disable all of Give\'s included CSS stylesheets.', 'give' ),
434
							'id'   => 'disable_css',
435
							'type' => 'checkbox'
436
						),
437
						array(
438
							'name' => esc_html__( 'Enable Floating Labels', 'give' ),
439
							/* translators: %s: https://givewp.com/documentation/core/give-forms/creating-give-forms/#floating-labels */
440
							'desc' => sprintf( wp_kses( __( 'Enable <a href="%s" target="_blank">floating labels</a> in Give\'s donation forms. Note that if the "Disable CSS" option is enabled, you will need to style the floating labels yourself.', 'give' ), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ), esc_url( 'https://givewp.com/documentation/core/give-forms/creating-give-forms/#floating-labels' ) ),
441
							'id'   => 'enable_floatlabels',
442
							'type' => 'checkbox'
443
						),
444
						array(
445
							'name' => esc_html__( 'Disable Welcome Screen', 'give' ),
446
							/* translators: %s: about page URL */
447
							'desc' => sprintf( wp_kses( __( 'Enable this option if you would like to disable the <a href="%s" target="_blank">Give Welcome screen</a> every time Give is activated and/or updated.', 'give' ), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ), esc_url( admin_url( 'index.php?page=give-about' ) ) ),
448
							'id'   => 'disable_welcome',
449
							'type' => 'checkbox'
450
						),
451
						array(
452
							'name' => esc_html__( 'Post Types', 'give' ),
453
							'desc' => '',
454
							'id'   => 'give_title_display_settings_2',
455
							'type' => 'give_title'
456
						),
457
						array(
458
							'name' => esc_html__( 'Disable Form Single Views', 'give' ),
459
							'desc' => esc_html__( 'By default, all forms have single views enabled which create a specific URL on your website for that form. This option disables the singular and archive views from being publicly viewable. Note: you will need to embed forms using a shortcode or widget if enabled.', 'give' ),
460
							'id'   => 'disable_forms_singular',
461
							'type' => 'checkbox'
462
						),
463
						array(
464
							'name' => esc_html__( 'Disable Form Archives', 'give' ),
465
							'desc' => esc_html__( 'Archives pages list all the forms you have created. This option will disable only the form\'s archive page(s). The single form\'s view will remain in place. Note: you will need to refresh your permalinks after this option has been enabled.', 'give' ),
466
							'id'   => 'disable_forms_archives',
467
							'type' => 'checkbox'
468
						),
469
						array(
470
							'name' => esc_html__( 'Disable Form Excerpts', 'give' ),
471
							'desc' => esc_html__( 'The excerpt is an optional summary or description of a donation form; in short, a summary as to why the user should give.', 'give' ),
472
							'id'   => 'disable_forms_excerpt',
473
							'type' => 'checkbox'
474
						),
475
476
						array(
477
							'name'    => esc_html__( 'Featured Image Size', 'give' ),
478
							'desc'    => esc_html__( 'The Featured Image is an image that is chosen as the representative image for a donation form. Some themes may have custom featured image sizes. Please select the size you would like to display for your single donation forms\' featured image.', 'give' ),
479
							'id'      => 'featured_image_size',
480
							'type'    => 'select',
481
							'default' => 'large',
482
							'options' => give_get_featured_image_sizes()
483
						),
484
						array(
485
							'name' => esc_html__( 'Disable Form Featured Image', 'give' ),
486
							'desc' => esc_html__( 'If you do not wish to use the featured image functionality you can disable it using this option and it will not be displayed for single donation forms.', 'give' ),
487
							'id'   => 'disable_form_featured_img',
488
							'type' => 'checkbox'
489
						),
490
						array(
491
							'name' => esc_html__( 'Disable Single Form Sidebar', 'give' ),
492
							'desc' => esc_html__( 'The sidebar allows you to add additional widget to the Give single form view. If you don\'t plan on using the sidebar you may disable it with this option.', 'give' ),
493
							'id'   => 'disable_form_sidebar',
494
							'type' => 'checkbox'
495
						),
496
						array(
497
							'name' => esc_html__( 'Taxonomies', 'give' ),
498
							'desc' => '',
499
							'id'   => 'give_title_display_settings_3',
500
							'type' => 'give_title'
501
						),
502
						array(
503
							'name' => esc_html__( 'Enable Form Categories', 'give' ),
504
							'desc' => esc_html__( 'Enables the "Category" taxonomy for all Give forms.', 'give' ),
505
							'id'   => 'enable_categories',
506
							'type' => 'checkbox'
507
						),
508
						array(
509
							'name' => esc_html__( 'Enable Form Tags', 'give' ),
510
							'desc' => esc_html__( 'Enables the "Tag" taxonomy for all Give forms.', 'give' ),
511
							'id'   => 'enable_tags',
512
							'type' => 'checkbox'
513
						),
514
					)
515
				)
516
517
			),
518
			/**
519
			 * Emails Options
520
			 */
521
			'emails'      => array(
522
				'id'         => 'email_settings',
523
				'give_title' => esc_html__( 'Email Settings', 'give' ),
524
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
525
				'fields'     => apply_filters( 'give_settings_emails', array(
526
						array(
527
							'name' => esc_html__( 'Email Settings', 'give' ),
528
							'desc' => '',
529
							'id'   => 'give_title_email_settings_1',
530
							'type' => 'give_title'
531
						),
532
						array(
533
							'id'      => 'email_template',
534
							'name'    => esc_html__( 'Email Template', 'give' ),
535
							'desc'    => esc_html__( 'Choose a template. Click "Save Changes" then "Preview Donation Receipt" to see the new template.', 'give' ),
536
							'type'    => 'select',
537
							'options' => give_get_email_templates()
538
						),
539
						array(
540
							'id'   => 'email_logo',
541
							'name' => esc_html__( 'Logo', 'give' ),
542
							'desc' => esc_html__( 'Upload or choose a logo to be displayed at the top of the donation receipt emails. Displayed on HTML emails only.', 'give' ),
543
							'type' => 'file'
544
						),
545
						array(
546
							'id'      => 'from_name',
547
							'name'    => esc_html__( 'From Name', 'give' ),
548
							'desc'    => esc_html__( 'The name which appears in the "From" field in donation receipt emails.', 'give' ),
549
							'default' => get_bloginfo( 'name' ),
550
							'type'    => 'text'
551
						),
552
						array(
553
							'id'      => 'from_email',
554
							'name'    => esc_html__( 'From Email', 'give' ),
555
							'desc'    => esc_html__( 'Email to send donation receipts from. This will act as the "from" and "reply-to" address.', 'give' ),
556
							'default' => get_bloginfo( 'admin_email' ),
557
							'type'    => 'text'
558
						),
559
						array(
560
							'name' => esc_html__( 'Donation Receipt', 'give' ),
561
							'desc' => '',
562
							'id'   => 'give_title_email_settings_2',
563
							'type' => 'give_title'
564
						),
565
						array(
566
							'id'      => 'donation_subject',
567
							'name'    => esc_html__( 'Donation Email Subject', 'give' ),
568
							'desc'    => esc_html__( 'Enter the subject line for the donation receipt email.', 'give' ),
569
							'default' => esc_attr__( 'Donation Receipt', 'give' ),
570
							'type'    => 'text'
571
						),
572
						array(
573
							'id'      => 'donation_receipt',
574
							'name'    => esc_html__( 'Donation Receipt', 'give' ),
575
							'desc'    => sprintf(
576
								/* translators: %s: emails tags list */
577
								esc_html__( 'Enter the email that is sent to users after completing a successful donation. HTML is accepted. Available template tags: %s', 'give' ),
578
								'<br/>'.give_get_emails_tags_list()
579
							),
580
							'type'    => 'wysiwyg',
581
							'default' => give_get_default_donation_receipt_email()
582
						),
583
						array(
584
							'name' => esc_html__( 'New Donation Notification', 'give' ),
585
							'desc' => '',
586
							'id'   => 'give_title_email_settings_3',
587
							'type' => 'give_title'
588
						),
589
						array(
590
							'id'      => 'donation_notification_subject',
591
							'name'    => esc_html__( 'Donation Notification Subject', 'give' ),
592
							'desc'    => esc_html__( 'Enter the subject line for the donation notification email.', 'give' ),
593
							'type'    => 'text',
594
							'default' => esc_attr__( 'New Donation - #{payment_id}', 'give' )
595
						),
596
						array(
597
							'id'      => 'donation_notification',
598
							'name'    => esc_html__( 'Donation Notification', 'give' ),
599
							'desc'    => sprintf(
600
								/* translators: %s: emails tags list */
601
								esc_html__( 'Enter the email that is sent to donation notification emails after completion of a donation. HTML is accepted. Available template tags: %s', 'give' ),
602
								'<br/>'.give_get_emails_tags_list()
603
							),
604
							'type'    => 'wysiwyg',
605
							'default' => give_get_default_donation_notification_email()
606
						),
607
						array(
608
							'id'      => 'admin_notice_emails',
609
							'name'    => esc_html__( 'Donation Notification Emails', 'give' ),
610
							'desc'    => __( 'Enter the email address(es) that should receive a notification anytime a donation is made, please only enter <span class="give-underline">one email address per line</span> and <strong>not separated by commas</strong>.', 'give' ),
611
							'type'    => 'textarea',
612
							'default' => get_bloginfo( 'admin_email' )
613
						),
614
						array(
615
							'id'   => 'disable_admin_notices',
616
							'name' => esc_html__( 'Disable Admin Notifications', 'give' ),
617
							'desc' => esc_html__( 'Check this box if you do not want to receive emails when new donations are made.', 'give' ),
618
							'type' => 'checkbox'
619
						)
620
					)
621
				)
622
			),
623
			/** Extension Settings */
624
			'addons'      => array(
625
				'id'         => 'addons',
626
				'give_title' => esc_html__( 'Give Add-ons Settings', 'give' ),
627
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
628
				'fields'     => apply_filters( 'give_settings_addons', array()
629
				)
630
			),
631
			/** Licenses Settings */
632
			'licenses'    => array(
633
				'id'         => 'licenses',
634
				'give_title' => esc_html__( 'Give Licenses', 'give' ),
635
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
636
				'fields'     => apply_filters( 'give_settings_licenses', array()
637
				)
638
			),
639
			/** Advanced Options */
640
			'advanced'    => array(
641
				'id'         => 'advanced_options',
642
				'give_title' => esc_html__( 'Advanced Options', 'give' ),
643
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
644
				'fields'     => apply_filters( 'give_settings_advanced', array(
645
						array(
646
							'name' => esc_html__( 'Access Control', 'give' ),
647
							'desc' => '',
648
							'id'   => 'give_title_session_control_1',
649
							'type' => 'give_title'
650
						),
651
						array(
652
							'id'      => 'session_lifetime',
653
							'name'    => esc_html__( 'Session Lifetime', 'give' ),
654
							'desc'    => esc_html__( 'The length of time a user\'s session is kept alive. Give starts a new session per user upon donation. Sessions allow donors to view their donation receipts without being logged in.', 'give' ),
655
							'type'    => 'select',
656
							'options' => array(
657
								'86400'  => esc_html__( '24 Hours', 'give' ),
658
								'172800' => esc_html__( '48 Hours', 'give' ),
659
								'259200' => esc_html__( '72 Hours', 'give' ),
660
								'604800' => esc_html__( '1 Week', 'give' ),
661
							)
662
						),
663
						array(
664
							'name' => esc_html__( 'Email Access', 'give' ),
665
							'desc' => esc_html__( 'Would you like your donors to be able to access their donation history using only email? Donors whose sessions have expired and do not have an account may still access their donation history via a temporary email access link.', 'give' ),
666
							'id'   => 'email_access',
667
							'type' => 'checkbox',
668
						),
669
						array(
670
							'id'      => 'recaptcha_key',
671
							'name'    => esc_html__( 'reCAPTCHA Site Key', 'give' ),
672
							/* translators: %s: https://www.google.com/recaptcha/ */
673
							'desc'    => sprintf( __( 'If you would like to prevent spam on the email access form navigate to <a href="%s" target="_blank">the reCAPTCHA website</a> and sign up for an API key. The reCAPTCHA uses Google\'s user-friendly single click verification method.', 'give' ), esc_url( 'https://www.google.com/recaptcha/' ) ),
674
							'default' => '',
675
							'type'    => 'text'
676
						),
677
						array(
678
							'id'      => 'recaptcha_secret',
679
							'name'    => esc_html__( 'reCAPTCHA Secret Key', 'give' ),
680
							'desc'    => esc_html__( 'Please paste the reCAPTCHA secret key here from your manage reCAPTCHA API Keys panel.', 'give' ),
681
							'default' => '',
682
							'type'    => 'text'
683
						),
684
						array(
685
							'name' => esc_html__( 'Data Control', 'give' ),
686
							'desc' => '',
687
							'id'   => 'give_title_data_control_2',
688
							'type' => 'give_title'
689
						),
690
						array(
691
							'name' => esc_html__( 'Remove All Data on Uninstall?', 'give' ),
692
							'desc' => esc_html__( 'When the plugin is deleted, completely remove all Give data.', 'give' ),
693
							'id'   => 'uninstall_on_delete',
694
							'type' => 'checkbox'
695
						),
696
						array(
697
							'name' => esc_html__( 'Filter Control', 'give' ),
698
							'desc' => '',
699
							'id'   => 'give_title_filter_control',
700
							'type' => 'give_title'
701
						),
702
						array(
703
							/* translators: %s: the_content */
704
							'name' => sprintf( __( 'Disable %s filter', 'give' ), '<code>the_content</code>' ),
705
							/* translators: 1: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content 2: the_content */
706
							'desc' => sprintf( __( 'If you are seeing extra social buttons, related posts, or other unwanted elements appearing within your forms then you can disable WordPress\' content filter. <a href="%1$s" target="_blank">Learn more</a> about %2$s filter.', 'give' ), esc_url( 'https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content' ), '<code>the_content</code>' ),
707
							'id'   => 'disable_the_content_filter',
708
							'type' => 'checkbox'
709
						),
710
						array(
711
							'name' => esc_html__( 'Script Loading', 'give' ),
712
							'desc' => '',
713
							'id'   => 'give_title_script_control',
714
							'type' => 'give_title'
715
						),
716
						array(
717
							'name' => esc_html__( 'Load Scripts in Footer?', 'give' ),
718
							'desc' => esc_html__( 'Check this box if you would like Give to load all frontend JavaScript files in the footer.', 'give' ),
719
							'id'   => 'scripts_footer',
720
							'type' => 'checkbox'
721
						)
722
					)
723
				)
724
			),
725
			/** API Settings */
726
			'api'         => array(
727
				'id'         => 'api',
728
				'give_title' => esc_html__( 'API', 'give' ),
729
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
730
				'show_names' => false, // Hide field names on the left
731
				'fields'     => apply_filters( 'give_settings_system', array(
732
						array(
733
							'id'   => 'api',
734
							'name' => esc_html__( 'API', 'give' ),
735
							'type' => 'api'
736
						)
737
					)
738
				)
739
			),
740
			/** Licenses Settings */
741
			'system_info' => array(
742
				'id'         => 'system_info',
743
				'give_title' => esc_html__( 'System Info', 'give' ),
744
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
745
				'fields'     => apply_filters( 'give_settings_system', array(
746
						array(
747
							'id'   => 'system-info-textarea',
748
							'name' => esc_html__( 'System Info', 'give' ),
749
							'desc' => esc_html__( 'Please copy and paste this information in your ticket when contacting support.', 'give' ),
750
							'type' => 'system_info'
751
						)
752
					)
753
				)
754
			),
755
		);
756
757
		//Return all settings array if no active tab
758
		if ( $active_tab === null || ! isset( $give_settings[ $active_tab ] ) ) {
759
760
			return apply_filters( 'give_registered_settings', $give_settings );
761
762
		}
763
764
765
		// Add other tabs and settings fields as needed
766
		return apply_filters( 'give_registered_settings', $give_settings[ $active_tab ] );
767
768
	}
769
770
	/**
771
	 * Show Settings Notices
772
	 */
773
	public function settings_notices() {
774
775
		if ( ! isset( $_POST['give_settings_saved'] ) ) {
776
			return;
777
		}
778
779
		add_settings_error( 'give-notices', 'global-settings-updated', esc_html__( 'Settings updated.', 'give' ), 'updated' );
780
781
	}
782
783
784
	/**
785
	 * Public getter method for retrieving protected/private variables
786
	 *
787
	 * @since  1.0
788
	 *
789
	 * @param  string $field Field to retrieve
790
	 *
791
	 * @return mixed         Field value or exception is thrown.
792
	 * @throws Exception     Throws an exception if the field is invalid.
793
	 */
794
	public function __get( $field ) {
795
796
		// Allowed fields to retrieve
797
		if ( in_array( $field, array( 'key', 'fields', 'give_title', 'options_page' ), true ) ) {
798
			return $this->{$field};
799
		}
800
		if ( 'option_metabox' === $field ) {
801
			return $this->option_metabox();
0 ignored issues
show
Bug introduced by
The method option_metabox() does not seem to exist on object<Give_Plugin_Settings>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
802
		}
803
804
		throw new Exception( sprintf( esc_html__( 'Invalid property: %s', 'give' ), $field ) );
805
	}
806
807
808
}
809
810
// Get it started
811
$Give_Settings = new Give_Plugin_Settings();
812
813
/**
814
 * Wrapper function around cmb2_get_option
815
 * @since  0.1.0
816
 *
817
 * @param  string $key Options array key
818
 * @param  string $default The default option if the option isn't set
819
 *
820
 * @return mixed        Option value
821
 */
822
function give_get_option( $key = '', $default = false ) {
823
	global $give_options;
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...
824
	$value = ! empty( $give_options[ $key ] ) ? $give_options[ $key ] : $default;
825
	$value = apply_filters( 'give_get_option', $value, $key, $default );
826
827
	return apply_filters( "give_get_option_{$key}", $value, $key, $default );
828
}
829
830
831
/**
832
 * Update an option
833
 *
834
 * Updates an give setting value in both the db and the global variable.
835
 * Warning: Passing in an empty, false or null string value will remove
836
 *          the key from the give_options array.
837
 *
838
 * @since 1.0
839
 *
840
 * @param string          $key The Key to update
841
 * @param string|bool|int $value The value to set the key to
842
 *
843
 * @return boolean True if updated, false if not.
844
 */
845
function give_update_option( $key = '', $value = false ) {
846
847
	// If no key, exit
848 61
	if ( empty( $key ) ) {
849 61
		return false;
850 61
	}
851
852 61
	if ( empty( $value ) ) {
853
		$remove_option = give_delete_option( $key );
854
855
		return $remove_option;
856
	}
857
858
	// First let's grab the current settings
859
	$options = get_option( 'give_settings' );
860
861
	// Let's let devs alter that value coming in
862
	$value = apply_filters( 'give_update_option', $value, $key );
863
864
	// Next let's try to update the value
865
	$options[ $key ] = $value;
866
	$did_update      = update_option( 'give_settings', $options );
867
868
	// If it updated, let's update the global variable
869
	if ( $did_update ) {
870
		global $give_options;
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...
871
		$give_options[ $key ] = $value;
872
	}
873 2
874
	return $did_update;
875
}
876
877 2
/**
878 2
 * Remove an option
879
 *
880 2
 * Removes an give setting value in both the db and the global variable.
881
 *
882
 * @since 1.0
883
 *
884 1
 * @param string $key The Key to delete
885
 *
886
 * @return boolean True if updated, false if not.
887 1
 */
888
function give_delete_option( $key = '' ) {
889
890 1
	// If no key, exit
891 1
	if ( empty( $key ) ) {
892
		return false;
893
	}
894 1
895 1
	// First let's grab the current settings
896 1
	$options = get_option( 'give_settings' );
897 1
898
	// Next let's try to update the value
899 1
	if ( isset( $options[ $key ] ) ) {
900
901
		unset( $options[ $key ] );
902
903
	}
904
905
	$did_update = update_option( 'give_settings', $options );
906
907
	// If it updated, let's update the global variable
908
	if ( $did_update ) {
909
		global $give_options;
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...
910
		$give_options = $options;
911
	}
912
913
	return $did_update;
914
}
915
916 2
917
/**
918
 * Get Settings
919
 *
920
 * Retrieves all Give plugin settings
921 2
 *
922
 * @since 1.0
923
 * @return array Give settings
924 2
 */
925
function give_get_settings() {
926 1
927
	$settings = get_option( 'give_settings' );
928 1
929
	return (array) apply_filters( 'give_get_settings', $settings );
930 2
931
}
932
933 2
934 1
/**
935 1
 * Give Settings Array Insert.
936 1
 *
937
 * Allows other Add-ons and plugins to insert Give settings at a desired position.
938 2
 *
939
 * @since      1.3.5
940
 *
941
 * @param $array
942
 * @param $position |int|string Expects an array key or 'id' of the settings field to appear after
943
 * @param $insert |array a valid array of options to insert
944
 *
945
 * @return array
946
 */
947
function give_settings_array_insert( $array, $position, $insert ) {
948
	if ( is_int( $position ) ) {
949
		array_splice( $array, $position, 0, $insert );
950
	} else {
951
952
		foreach ( $array as $index => $subarray ) {
953
			if ( isset( $subarray['id'] ) && $subarray['id'] == $position ) {
954
				$pos = $index;
955
			}
956
		}
957
958
		if ( ! isset( $pos ) ) {
959
			return $array;
960
		}
961
962
		$array = array_merge(
963
			array_slice( $array, 0, $pos ),
964
			$insert,
965
			array_slice( $array, $pos )
966
		);
967
	}
968
969
	return $array;
970
}
971
972
973
/**
974
 * Gateways Callback
975
 *
976
 * Renders gateways fields.
977
 *
978
 * @since 1.0
979
 *
980
 * @param $field_object
981
 * @param $escaped_value
982
 * @param $object_id
983
 * @param $object_type
984
 * @param $field_type_object
985
 *
986
 * @return void
987
 */
988
function give_enabled_gateways_callback( $field_object, $escaped_value, $object_id, $object_type, $field_type_object ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field_object 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 $object_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...
Unused Code introduced by
The parameter $object_type 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...
989
990
	$id                = $field_type_object->field->args['id'];
991
	$field_description = $field_type_object->field->args['desc'];
992
	$gateways          = give_get_ordered_payment_gateways( give_get_payment_gateways() );
993
994
	echo '<ul class="cmb2-checkbox-list cmb2-list">';
995
996
	foreach ( $gateways as $key => $option ) :
997
998
		if ( is_array( $escaped_value ) && array_key_exists( $key, $escaped_value ) ) {
999
			$enabled = '1';
1000
		} else {
1001
			$enabled = null;
1002
		}
1003
1004
		echo '<li><span class="give-drag-handle"><span class="dashicons dashicons-menu"></span></span><input name="' . $id . '[' . $key . ']" id="' . $id . '[' . $key . ']" type="checkbox" value="1" ' . checked( '1', $enabled, false ) . '/>&nbsp;';
1005
		echo '<label for="' . $id . '[' . $key . ']">' . $option['admin_label'] . '</label></li>';
1006
1007
	endforeach;
1008
1009
	echo '</ul>';
1010
1011
	if ( $field_description ) {
1012
		echo '<p class="cmb2-metabox-description">' . $field_description . '</p>';
1013
	}
1014
1015
}
1016
1017
/**
1018
 * Gateways Callback (drop down)
1019
 *
1020
 * Renders gateways select menu
1021
 *
1022
 * @since 1.0
1023
 *
1024
 * @param $field_object , $escaped_value, $object_id, $object_type, $field_type_object Arguments passed by CMB2
1025
 *
1026
 * @return void
1027
 */
1028
function give_default_gateway_callback( $field_object, $escaped_value, $object_id, $object_type, $field_type_object ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field_object 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 $object_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...
Unused Code introduced by
The parameter $object_type 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...
1029
1030
	$id                = $field_type_object->field->args['id'];
1031
	$field_description = $field_type_object->field->args['desc'];
1032
	$gateways          = give_get_enabled_payment_gateways();
1033
1034
	echo '<select class="cmb2_select" name="' . $id . '" id="' . $id . '">';
1035
1036
	//Add a field to the Give Form admin single post view of this field
1037
	if ( $field_type_object->field->object_type === 'post' ) {
1038
		echo '<option value="global">' . esc_html__( 'Global Default', 'give' ) . '</option>';
1039
	}
1040
1041
	foreach ( $gateways as $key => $option ) :
1042
1043
		$selected = isset( $escaped_value ) ? selected( $key, $escaped_value, false ) : '';
1044
1045
1046
		echo '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>';
1047
1048
	endforeach;
1049
1050
	echo '</select>';
1051
1052
	echo '<p class="cmb2-metabox-description">' . $field_description . '</p>';
1053
1054
}
1055
1056
/**
1057
 * Give Title
1058
 *
1059
 * Renders custom section titles output; Really only an  because CMB2's output is a bit funky
1060
 *
1061
 * @since 1.0
1062
 *
1063
 * @param       $field_object , $escaped_value, $object_id, $object_type, $field_type_object
1064
 *
1065
 * @return void
1066
 */
1067
function give_title_callback( $field_object, $escaped_value, $object_id, $object_type, $field_type_object ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field_object 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 $escaped_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 $object_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...
Unused Code introduced by
The parameter $object_type 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...
1068
1069
	$id                = $field_type_object->field->args['id'];
1070
	$title             = $field_type_object->field->args['name'];
1071
	$field_description = $field_type_object->field->args['desc'];
1072
1073
	echo '<hr>' . $field_description;
1074
1075
}
1076
1077
/**
1078
 * Give Description
1079
 *
1080
 * Renders custom description text which any plugin can use to output content, html, php, etc.
1081
 *
1082
 * @since      1.3.5
1083
 *
1084
 * @param       $field_object , $escaped_value, $object_id, $object_type, $field_type_object
1085
 *
1086
 * @return void
1087
 */
1088
function give_description_callback( $field_object, $escaped_value, $object_id, $object_type, $field_type_object ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field_object 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 $escaped_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 $object_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...
Unused Code introduced by
The parameter $object_type 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...
1089
1090
	$id                = $field_type_object->field->args['id'];
1091
	$title             = $field_type_object->field->args['name'];
1092
	$field_description = $field_type_object->field->args['desc'];
1093
1094
1095
	echo $field_description;
1096
1097
}
1098
1099
/**
1100
 * Gets a number of posts and displays them as options
1101
 *
1102
 * @param  array $query_args Optional. Overrides defaults.
1103
 * @param  bool  $force Force the pages to be loaded even if not on settings
1104
 *
1105
 * @see: https://github.com/WebDevStudios/CMB2/wiki/Adding-your-own-field-types
1106
 * @return array An array of options that matches the CMB2 options array
1107
 */
1108
function give_cmb2_get_post_options( $query_args, $force = false ) {
1109
1110
	$post_options = array( '' => '' ); // Blank option
1111
1112
	if ( ( ! isset( $_GET['page'] ) || 'give-settings' != $_GET['page'] ) && ! $force ) {
1113
		return $post_options;
1114
	}
1115
1116
	$args = wp_parse_args( $query_args, array(
1117
		'post_type'   => 'page',
1118
		'numberposts' => 10,
1119
	) );
1120
1121
	$posts = get_posts( $args );
1122
1123
	if ( $posts ) {
1124
		foreach ( $posts as $post ) {
1125
1126
			$post_options[ $post->ID ] = $post->post_title;
1127
1128
		}
1129
	}
1130
1131
	return $post_options;
1132
}
1133
1134
1135
/**
1136
 * Featured Image Sizes
1137
 *
1138
 * Outputs an array for the "Featured Image Size" option found under Settings > Display Options.
1139
 *
1140
 * @since 1.4
1141
 */
1142
function give_get_featured_image_sizes() {
1143
	global $_wp_additional_image_sizes;
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...
1144
	$sizes = array();
1145
1146
	foreach ( get_intermediate_image_sizes() as $_size ) {
1147
1148
		if ( in_array( $_size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
1149
			$sizes[ $_size ] = $_size . ' - ' . get_option( "{$_size}_size_w" ) . 'x' . get_option( "{$_size}_size_h" );
1150
		} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
1151
			$sizes[ $_size ] = $_size . ' - ' . $_wp_additional_image_sizes[ $_size ]['width'] . 'x' . $_wp_additional_image_sizes[ $_size ]['height'];
1152
		}
1153
1154
	}
1155
1156
	return apply_filters( 'give_get_featured_image_sizes', $sizes );
1157
}
1158
1159
1160
/**
1161
 * Give License Key Callback
1162
 *
1163
 * Registers the license field callback for EDD's Software Licensing.
1164
 *
1165
 * @since       1.0
1166
 *
1167
 * @param array $field_object , $escaped_value, $object_id, $object_type, $field_type_object Arguments passed by CMB2
1168
 *
1169
 * @return void
1170
 */
1171
function give_license_key_callback( $field_object, $escaped_value, $object_id, $object_type, $field_type_object ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field_object 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 $object_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...
Unused Code introduced by
The parameter $object_type 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...
1172
    /* @var CMB2_Types $field_type_object*/
1173
1174
    $id                   = $field_type_object->field->args['id'];
1175
	$field_description    = $field_type_object->field->args['desc'];
1176
	$license              = $field_type_object->field->args['options']['license'];
1177
    $license_key          = $escaped_value;
1178
    $is_license_key       = apply_filters( 'give_is_license_key', ( is_object( $license ) && ! empty( $license ) ) );
1179
    $is_valid_license     = apply_filters( 'give_is_valid_license', ( $is_license_key && property_exists( $license, 'license' ) && 'valid' === $license->license ) );
1180
    $shortname            = $field_type_object->field->args['options']['shortname'];
1181
	$field_classes        = 'regular-text give-license-field';
1182
	$type                 = empty( $escaped_value ) || ! $is_valid_license ? 'text' : 'password';
1183
    $custom_html          = '';
1184
    $value                = $escaped_value;
1185
    $messages             = array();
1186
    $class                = '';
1187
    $account_page_link    = $field_type_object->field->args['options']['account_url'];
1188
    $checkout_page_link   = $field_type_object->field->args['options']['checkout_url'];
1189
    $addon_name           = $field_type_object->field->args['options']['item_name'];
1190
    $license_status       = null;
1191
    $is_in_subscription   = null;
1192
1193
	// By default query on edd api url will return license object which contain status and message property, this can break below functionality.
1194
	// To combat that check if status is set to error or not, if yes then set $is_license_key to false.
1195
	if( $is_license_key && property_exists( $license, 'status' ) && 'error' === $license->status ) {
1196
		$is_license_key = false;
1197
	}
1198
1199
1200
    // Check if current license is part of subscription or not.
1201
    $subscriptions = get_option( 'give_subscriptions' );
1202
1203
    if( $is_license_key && $subscriptions ) {
1204
        foreach ( $subscriptions as $subscription ) {
1205
            if( in_array( $license_key, $subscription['licenses'] ) ) {
1206
                $is_in_subscription = $subscription['id'];
1207
                break;
1208
            }
1209
        }
1210
    }
1211
1212
1213
    if( $is_license_key ) {
1214
        if( $is_in_subscription ) {
1215
            $subscription_expires = strtotime( $subscriptions[$is_in_subscription]['expires'] );
1216
            $subscription_status  = __( 'renew', 'give' );
1217
1218
            if( ( 'active' !== $subscriptions[$is_in_subscription]['status'] ) ){
1219
                $subscription_status = __( 'expire', 'give' );
1220
            }
1221
1222
            if( $subscription_expires < current_time( 'timestamp', 1 ) ) {
1223
                $messages[] = sprintf(
1224
                    __( 'Your subscription (<a href="%s" target="_blank">#%d</a>) expired. Please <a href="%s" target="_blank" title="Renew your license key">renew your license key</a>', 'give' ),
1225
                    urldecode( $subscriptions[$is_in_subscription]['invoice_url'] ),
1226
                    $subscriptions[$is_in_subscription]['payment_id'],
1227
                    $checkout_page_link . '?edd_license_key=' . $subscriptions[$is_in_subscription]['license_key'] . '&utm_campaign=admin&utm_source=licenses&utm_medium=expired'
1228
                );
1229
                $license_status = 'license-expired';
1230
            } elseif( strtotime( '- 7 days', $subscription_expires ) < current_time( 'timestamp', 1 ) ) {
1231
                $messages[] = sprintf(
1232
                    __( 'Your subscription (<a href="%s" target="_blank">#%d</a>) will %s in %s.', 'give' ),
1233
                    urldecode( $subscriptions[$is_in_subscription]['invoice_url'] ),
1234
                    $subscriptions[$is_in_subscription]['payment_id'],
1235
                    $subscription_status,
1236
                    human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscriptions[$is_in_subscription]['expires'] ) )
1237
                );
1238
                $license_status = 'license-expires-soon';
1239
            } else {
1240
                $messages[] = sprintf(
1241
                    __( 'Your subscription (<a href="%s" target="_blank">#%d</a>) will %s on %s.', 'give' ),
1242
                    urldecode( $subscriptions[$is_in_subscription]['invoice_url'] ),
1243
                    $subscriptions[$is_in_subscription]['payment_id'],
1244
                    $subscription_status,
1245
                    date_i18n( get_option( 'date_format' ), strtotime( $subscriptions[$is_in_subscription]['expires'], current_time( 'timestamp' ) ) )
1246
                );
1247
                $license_status = 'license-expiration-date';
1248
            }
1249
1250
1251
        } elseif ( empty( $license->success ) && property_exists( $license, 'error' ) ) {
1252
1253
            // activate_license 'invalid' on anything other than valid, so if there was an error capture it
1254
            switch(   $license->error ) {
1255
                case 'expired' :
1256
					error_log(print_r( $license->error, true) . "\n", 3, WP_CONTENT_DIR . '/debug_new.log');
1257
                    $class = $license->error;
1258
                    $messages[] = sprintf(
1259
                        __( 'Your license key expired on %s. Please <a href="%s" target="_blank" title="Renew your license key">renew your license key</a>.', 'give' ),
1260
                        date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ),
1261
                        $checkout_page_link . '?edd_license_key=' . $value . '&utm_campaign=admin&utm_source=licenses&utm_medium=expired'
1262
                    );
1263
                    $license_status = 'license-' . $class;
1264
                    break;
1265
1266
                case 'missing' :
1267
                    $class = $license->error;
1268
                    $messages[] = sprintf(
1269
                        __( 'Invalid license. Please <a href="%s" target="_blank" title="Visit account page">visit your account page</a> and verify it.', 'give' ),
1270
                        $account_page_link . '?utm_campaign=admin&utm_source=licenses&utm_medium=missing'
1271
                    );
1272
                    $license_status = 'license-' . $class;
1273
                    break;
1274
1275
                case 'invalid' :
1276
                    $class = $license->error;
1277
                    $messages[] = sprintf(
1278
                        __( 'Your %s is not active for this URL. Please <a href="%s" target="_blank" title="Visit account page">visit your account page</a> to manage your license key URLs.', 'give' ),
1279
                        $addon_name,
1280
                        $account_page_link . '?utm_campaign=admin&utm_source=licenses&utm_medium=invalid'
1281
                    );
1282
                    $license_status = 'license-' . $class;
1283
                    break;
1284
1285
                case 'site_inactive' :
1286
                    $class = $license->error;
1287
                    $messages[] = sprintf(
1288
                        __( 'Your %s is not active for this URL. Please <a href="%s" target="_blank" title="Visit account page">visit your account page</a> to manage your license key URLs.', 'give' ),
1289
                        $addon_name,
1290
                        $account_page_link . '?utm_campaign=admin&utm_source=licenses&utm_medium=invalid'
1291
                    );
1292
                    $license_status = 'license-' . $class;
1293
                    break;
1294
1295
                case 'item_name_mismatch' :
1296
                    $class = $license->error;
1297
                    $messages[] = sprintf( __( 'This license %s does not belong to %s.', 'give' ), $value, $addon_name );
1298
                    $license_status = 'license-' . $class;
1299
                    break;
1300
1301
                case 'no_activations_left':
1302
                    $class = $license->error;
1303
                    $messages[] = sprintf( __( 'Your license key has reached it\'s activation limit. <a href="%s">View possible upgrades</a> now.', 'give' ), $account_page_link );
1304
                    $license_status = 'license-' . $class;
1305
                    break;
1306
            }
1307
        } else {
1308
            switch( $license->license ) {
1309
                case 'valid' :
1310
                default:
1311
                    $class = 'valid';
1312
                    $now        = current_time( 'timestamp' );
1313
                    $expiration = strtotime( $license->expires, current_time( 'timestamp' ) );
1314
1315
                    if( 'lifetime' === $license->expires ) {
1316
                        $messages[] = __( 'License key never expires.', 'give' );
1317
                        $license_status = 'license-lifetime-notice';
1318
                    } elseif( $expiration > $now && $expiration - $now < ( DAY_IN_SECONDS * 30 ) ) {
1319
                        $messages[] = sprintf(
1320
                            __( 'Your license key expires soon! It expires on %s. <a href="%s" target="_blank" title="Renew license">Renew your license key</a>.', 'give' ),
1321
                            date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ),
1322
                            $checkout_page_link . '?edd_license_key=' . $value . '&utm_campaign=admin&utm_source=licenses&utm_medium=renew'
1323
                        );
1324
                        $license_status = 'license-expires-soon';
1325
                    } else {
1326
                        $messages[] = sprintf(
1327
                            __( 'Your license key expires on %s.', 'give' ),
1328
                            date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) )
1329
                        );
1330
                        $license_status = 'license-expiration-date';
1331
                    }
1332
                    break;
1333
            }
1334
        }
1335
    } else{
1336
		$class = 'empty';
1337
		$messages[] = sprintf(
1338
			__( 'To receive updates, please enter your valid %s license key.', 'give' ),
1339
			$addon_name
1340
		);
1341
		$license_status = null;
1342
	}
1343
1344
1345
    // Add class for input field if license is active.
1346
	if ( $is_valid_license ) {
1347
		$field_classes .= ' give-license-active';
1348
	}
1349
1350
	// Get input filed html.
1351
	$input_field_html = $field_type_object->input( array(
1352
		'class' => $field_classes,
1353
		'type'  => $type
1354
	) );
1355
1356
	// If license is active so show deactivate button
1357
	if ( $is_valid_license ) {
1358
        // Get input filed html.
1359
        $input_field_html = $field_type_object->input( array(
1360
            'class' => $field_classes,
1361
            'type'  => $type,
1362
            'readonly' => 'readonly',
1363
        ) );
1364
1365
		$custom_html = '<input type="submit" class="button-secondary give-license-deactivate" name="' . $id . '_deactivate" value="' . esc_attr__( 'Deactivate License', 'give' ) . '"/>';
1366
	}
1367
1368
	// Field description.
1369
	$custom_html .= '<label for="give_settings[' . $id . ']"> ' . $field_description . '</label>';
1370
1371
    // If no messages found then inform user that to get updated in future register yourself.
1372
    if ( empty( $messages ) ) {
1373
        $messages[] = apply_filters( "{$shortname}_default_addon_notice", esc_html__( 'To receive updates, please enter your valid Software Licensing license key.', 'give' ) );
1374
    }
1375
1376
    foreach( $messages as $message ) {
1377
        $custom_html .= '<div class="give-license-notice give-' . $license_status . '">';
1378
        $custom_html .= '<p>' . $message . '</p>';
1379
        $custom_html .= '</div>';
1380
    }
1381
1382
1383
1384
    // Field html.
1385
    $custom_html = apply_filters('give_license_key_field_html', $input_field_html.$custom_html, $field_type_object );
1386
1387
    // Nonce.
1388
	wp_nonce_field( $id . '-nonce', $id . '-nonce' );
1389
1390
    // Print field html.
1391
    echo '<div>' . $custom_html . '</div>';
1392
}
1393
1394
1395
/**
1396
 * Display the API Keys
1397
 *
1398
 * @since       1.0
1399
 * @return      void
1400
 */
1401
function give_api_callback() {
1402
1403
	if ( ! current_user_can( 'manage_give_settings' ) ) {
1404
		return;
1405
	}
1406
1407
	/**
1408
	 * Fires before displaying API keys.
1409
	 *
1410
	 * @since 1.0
1411
	 */
1412
	do_action( 'give_tools_api_keys_before' );
1413
1414
	require_once GIVE_PLUGIN_DIR . 'includes/admin/class-api-keys-table.php';
1415
1416
	$api_keys_table = new Give_API_Keys_Table();
1417
	$api_keys_table->prepare_items();
1418
	$api_keys_table->display();
1419
	?>
1420
	<span class="cmb2-metabox-description api-description">
1421
		<?php echo sprintf(
1422
		/* translators: 1: https://givewp.com/documentation/give-api-reference/ 2: https://givewp.com/addons/zapier/ */
1423
			__( 'You can create API keys for individual users within their profile edit screen. API keys allow users to use the <a href="%1$s" target="_blank">Give REST API</a> to retrieve donation data in JSON or XML for external applications or devices, such as <a href="%2$s" target="_blank">Zapier</a>.', 'give' ),
1424
			esc_url( 'https://givewp.com/documentation/give-api-reference/' ),
1425
			esc_url( 'https://givewp.com/addons/zapier/' )
1426
		); ?>
1427
	</span>
1428
	<?php
1429
1430
	/**
1431
	 * Fires after displaying API keys.
1432
	 *
1433
	 * @since 1.0
1434
	 */
1435
	do_action( 'give_tools_api_keys_after' );
1436
}
1437
1438
add_action( 'give_settings_tab_api_keys', 'give_api_callback' );
1439
1440
/**
1441
 * Hook Callback
1442
 *
1443
 * Adds a do_action() hook in place of the field.
1444
 *
1445
 * @since 1.0
1446
 *
1447
 * @param array $args Arguments passed by the setting
1448
 *
1449
 * @return void
1450
 */
1451
function give_hook_callback( $args ) {
1452
1453
	$id = $args['id'];
1454
1455
	/**
1456
	 * Fires in give field.
1457
	 *
1458
	 * @since 1.0
1459
	 */
1460
	do_action( "give_{$id}" );
1461
1462
}
1463
1464
/**
1465
 * Get the CMB2 bootstrap!
1466
 *
1467
 * Checks to see if CMB2 plugin is installed first the uses included CMB2.
1468
 * We can still use it even it it's not active.
1469
 * This prevents fatal error conflicts with other themes and users of the CMB2 WP.org plugin.
1470
 */
1471
1472
if ( file_exists( WP_PLUGIN_DIR . '/cmb2/init.php' ) && ! defined( 'CMB2_LOADED' ) ) {
1473
	require_once WP_PLUGIN_DIR . '/cmb2/init.php';
1474
} elseif ( file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/cmb2/init.php' ) && ! defined( 'CMB2_LOADED' ) ) {
1475
	require_once GIVE_PLUGIN_DIR . '/includes/libraries/cmb2/init.php';
1476
} elseif ( file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/CMB2/init.php' ) && ! defined( 'CMB2_LOADED' ) ) {
1477
	require_once GIVE_PLUGIN_DIR . '/includes/libraries/CMB2/init.php';
1478
}