Completed
Pull Request — master (#1412)
by Ravinder
17:25
created

Give_Plugin_Settings::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
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    https://opensource.org/licenses/gpl-license GNU Public License
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
	 *
18
	 * @var string
19
	 */
20
	private $key = 'give_settings';
21
22
	/**
23
	 * Array of metaboxes/fields.
24
	 *
25
	 * @var array
26
	 */
27
	protected $option_metabox = array();
28
29
	/**
30
	 * Options Page title.
31
	 *
32
	 * @var string
33
	 */
34
	protected $title = '';
35
36
	/**
37
	 * Options Page hook.
38
	 *
39
	 * @var string
40
	 */
41
	protected $options_page = '';
42
43
	/**
44
	 * Give_Plugin_Settings constructor.
45
	 */
46
	public function __construct() {
47
48
		//Custom CMB2 Settings Fields
49
		add_action( 'cmb2_render_give_title', 'give_title_callback', 10, 5 );
50
		add_action( 'cmb2_render_give_description', 'give_description_callback', 10, 5 );
51
		add_action( 'cmb2_render_enabled_gateways', 'give_enabled_gateways_callback', 10, 5 );
52
		add_action( 'cmb2_render_default_gateway', 'give_default_gateway_callback', 10, 5 );
53
		add_action( 'cmb2_render_email_preview_buttons', 'give_email_preview_buttons_callback', 10, 5 );
54
		add_action( 'cmb2_render_system_info', 'give_system_info_callback', 10, 5 );
55
		add_action( 'cmb2_render_api', 'give_api_callback', 10, 5 );
56
		add_action( 'cmb2_render_license_key', 'give_license_key_callback', 10, 5 );
57
	}
58
59
60
	/**
61
	 * Register our setting to WP
62
	 *
63
	 * @since  1.0
64
	 */
65
	public function init() {
66
		register_setting( $this->key, $this->key );
67
68
	}
69
70
71
	/**
72
	 * Filter CMB2 URL
73
	 *
74
	 * Required for CMB2 to properly load CSS/JS.
75
	 *
76
	 * @param $url
77
	 *
78
	 * @return mixed
79
	 */
80
	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...
81
		//Path to Give's CMB
82
		return GIVE_PLUGIN_URL . '/includes/libraries/cmb2';
83
	}
84
85
86
	/**
87
	 * Retrieve settings tabs
88
	 *
89
	 * @since 1.0
90
	 * @return array $tabs
91
	 */
92
	public function give_get_settings_tabs() {
93
94
		$settings = $this->give_settings( null );
95
96
		$tabs             = array();
97
		$tabs['general']  = esc_html__( 'General', 'give' );
98
		$tabs['gateways'] = esc_html__( 'Payment Gateways', 'give' );
99
		$tabs['display']  = esc_html__( 'Display Options', 'give' );
100
		$tabs['emails']   = esc_html__( 'Emails', 'give' );
101
102
		if ( ! empty( $settings['addons']['fields'] ) ) {
103
			$tabs['addons'] = esc_html__( 'Add-ons', 'give' );
104
		}
105
106
		if ( ! empty( $settings['licenses']['fields'] ) ) {
107
			$tabs['licenses'] = esc_html__( 'Licenses', 'give' );
108
		}
109
110
		$tabs['advanced']    = esc_html__( 'Advanced', 'give' );
111
		$tabs['api']         = esc_html__( 'API', 'give' );
112
		$tabs['system_info'] = esc_html__( 'System Info', 'give' );
113
114
		return apply_filters( 'give_settings_tabs', $tabs );
115
	}
116
117
118
	/**
119
	 * Admin page markup. Mostly handled by CMB2
120
	 * @since  1.0
121
	 */
122
	public function admin_page_display() {
123
124
		$active_tab = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $this->give_get_settings_tabs() ) ? $_GET['tab'] : 'general';
125
126
		?>
127
128
		<div class="wrap give_settings_page cmb2_options_page <?php echo $this->key; ?>">
129
130
			<h1 class="screen-reader-text"><?php echo get_admin_page_title(); ?></h1>
131
132
			<h2 class="nav-tab-wrapper">
133
				<?php
134
				foreach ( $this->give_get_settings_tabs() as $tab_id => $tab_name ) {
135
136
					$tab_url = esc_url( add_query_arg( array(
137
						'settings-updated' => false,
138
						'tab'              => $tab_id
139
					) ) );
140
141
					$active = $active_tab == $tab_id ? ' nav-tab-active' : '';
142
143
					echo '<a href="' . esc_url( $tab_url ) . '" class="nav-tab' . $active . '" id="tab-' . $tab_id . '">' . esc_html( $tab_name ) . '</a>';
144
145
				}
146
				?>
147
			</h2>
148
149
			<?php cmb2_metabox_form( $this->give_settings( $active_tab ), $this->key ); ?>
150
151
		</div><!-- .wrap -->
152
153
		<?php
154
	}
155
156
157
	/**
158
	 *
159
	 * Modify CMB2 Default Form Output
160
	 *
161
	 * @param string @args
162
	 *
163
	 * @since 1.0
164
	 *
165
	 * @param $form_format
166
	 * @param $object_id
167
	 * @param $cmb
168
	 *
169
	 * @return string
170
	 */
171
	function give_modify_cmb2_form_output( $form_format, $object_id, $cmb ) {
0 ignored issues
show
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...
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...
172
173
		//only modify the give settings form
174
		if ( 'give_settings' == $object_id ) {
175
176
			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="' . esc_attr__( 'Save Settings', 'give' ) . '" class="button-primary"></div></form>';
177
178
		}
179
180
		return $form_format;
181
182
	}
183
184
	/**
185
	 * Define General Settings Metabox and field configurations.
186
	 *
187
	 * Filters are provided for each settings section to allow add-ons and other plugins to add their own settings
188
	 *
189
	 * @param $active_tab |string active tab settings; null returns full array
190
	 *
191
	 * @return array
192
	 */
193
	public function give_settings( $active_tab ) {
194
195
		$give_settings = array(
196
			/**
197
			 * General Settings
198
			 */
199
			'general'     => array(
200
				'id'         => 'general_settings',
201
				'give_title' => esc_html__( 'General Settings', 'give' ),
202
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
203
				'fields'     => apply_filters( 'give_settings_general', array(
204
						array(
205
							'name' => esc_html__( 'General Settings', 'give' ),
206
							'desc' => '',
207
							'type' => 'give_title',
208
							'id'   => 'give_title_general_settings_1'
209
						),
210
						array(
211
							'name'    => esc_html__( 'Success Page', 'give' ),
212
							/* translators: %s: [give_receipt] */
213
							'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>' ),
214
							'id'      => 'success_page',
215
							'type'    => 'select',
216
							'options' => give_cmb2_get_post_options( array(
217
								'post_type'   => 'page',
218
								'numberposts' => - 1
219
							) ),
220
						),
221
						array(
222
							'name'    => esc_html__( 'Failed Donation Page', 'give' ),
223
							'desc'    => esc_html__( 'The page donors are sent to if their donation is cancelled or fails.', 'give' ),
224
							'id'      => 'failure_page',
225
							'type'    => 'select',
226
							'options' => give_cmb2_get_post_options( array(
227
								'post_type'   => 'page',
228
								'numberposts' => - 1
229
							) ),
230
						),
231
						array(
232
							'name'    => esc_html__( 'Donation History Page', 'give' ),
233
							/* translators: %s: [donation_history] */
234
							'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>' ),
235
							'id'      => 'history_page',
236
							'type'    => 'select',
237
							'options' => give_cmb2_get_post_options( array(
238
								'post_type'   => 'page',
239
								'numberposts' => - 1
240
							) ),
241
						),
242
						array(
243
							'name'    => esc_html__( 'Base Country', 'give' ),
244
							'desc'    => esc_html__( 'The country your site operates from.', 'give' ),
245
							'id'      => 'base_country',
246
							'type'    => 'select',
247
							'options' => give_get_country_list(),
248
						),
249
						array(
250
							'name' => esc_html__( 'Currency Settings', 'give' ),
251
							'desc' => '',
252
							'type' => 'give_title',
253
							'id'   => 'give_title_general_settings_2'
254
						),
255
						array(
256
							'name'    => esc_html__( 'Currency', 'give' ),
257
							'desc'    => esc_html__( 'The donation currency. Note that some payment gateways have currency restrictions.', 'give' ),
258
							'id'      => 'currency',
259
							'type'    => 'select',
260
							'options' => give_get_currencies(),
261
							'default' => 'USD',
262
						),
263
						array(
264
							'name'    => esc_html__( 'Currency Position', 'give' ),
265
							'desc'    => esc_html__( 'The position of the currency symbol.', 'give' ),
266
							'id'      => 'currency_position',
267
							'type'    => 'select',
268
							'options' => array(
269
								/* translators: %s: currency symbol */
270
								'before' => sprintf( esc_html__( 'Before - %s10', 'give' ), give_currency_symbol( give_get_currency() ) ),
271
								/* translators: %s: currency symbol */
272
								'after'  => sprintf( esc_html__( 'After - 10%s', 'give' ), give_currency_symbol( give_get_currency() ) )
273
							),
274
							'default' => 'before',
275
						),
276
						array(
277
							'name'            => esc_html__( 'Thousands Separator', 'give' ),
278
							'desc'            => esc_html__( 'The symbol (usually , or .) to separate thousands.', 'give' ),
279
							'id'              => 'thousands_separator',
280
							'type'            => 'text_small',
281
							'sanitization_cb' => 'give_sanitize_thousand_separator',
282
							'default'         => ',',
283
						),
284
						array(
285
							'name'    => esc_html__( 'Decimal Separator', 'give' ),
286
							'desc'    => esc_html__( 'The symbol (usually , or .) to separate decimal points.', 'give' ),
287
							'id'      => 'decimal_separator',
288
							'type'    => 'text_small',
289
							'default' => '.',
290
						),
291
						array(
292
							'name'            => esc_html__( 'Number of Decimals', 'give' ),
293
							'desc'            => esc_html__( 'The number of decimal points displayed in amounts.', 'give' ),
294
							'id'              => 'number_decimals',
295
							'type'            => 'text_small',
296
							'default'         => 2,
297
							'sanitization_cb' => 'give_sanitize_number_decimals',
298
						),
299
					)
300
				)
301
			),
302
			/**
303
			 * Payment Gateways
304
			 */
305
			'gateways'    => array(
306
				'id'         => 'payment_gateways',
307
				'give_title' => esc_html__( 'Payment Gateways', 'give' ),
308
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
309
				'fields'     => apply_filters( 'give_settings_gateways', array(
310
						array(
311
							'name' => esc_html__( 'Gateways Settings', 'give' ),
312
							'desc' => '',
313
							'id'   => 'give_title_gateway_settings_1',
314
							'type' => 'give_title'
315
						),
316
						array(
317
							'name' => esc_html__( 'Test Mode', 'give' ),
318
							'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' ),
319
							'id'   => 'test_mode',
320
							'type' => 'checkbox'
321
						),
322
						array(
323
							'name' => esc_html__( 'Enabled Gateways', 'give' ),
324
							'desc' => esc_html__( 'Enable your payment gateway. Can be ordered by dragging.', 'give' ),
325
							'id'   => 'gateways',
326
							'type' => 'enabled_gateways'
327
						),
328
						array(
329
							'name' => esc_html__( 'Default Gateway', 'give' ),
330
							'desc' => esc_html__( 'The gateway that will be selected by default.', 'give' ),
331
							'id'   => 'default_gateway',
332
							'type' => 'default_gateway'
333
						),
334
						array(
335
							'name' => esc_html__( 'PayPal Standard', 'give' ),
336
							'desc' => '',
337
							'type' => 'give_title',
338
							'id'   => 'give_title_gateway_settings_2',
339
						),
340
						array(
341
							'name' => esc_html__( 'PayPal Email', 'give' ),
342
							'desc' => esc_html__( 'Enter your PayPal account\'s email.', 'give' ),
343
							'id'   => 'paypal_email',
344
							'type' => 'text_email',
345
						),
346
						array(
347
							'name' => esc_html__( 'PayPal Page Style', 'give' ),
348
							'desc' => esc_html__( 'Enter the name of the page style to use, or leave blank to use the default.', 'give' ),
349
							'id'   => 'paypal_page_style',
350
							'type' => 'text',
351
						),
352
						array(
353
							'name'    => esc_html__( 'PayPal Transaction Type', 'give' ),
354
							'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' ),
355
							'id'      => 'paypal_button_type',
356
							'type'    => 'radio_inline',
357
							'options' => array(
358
								'donation' => esc_html__( 'Donation', 'give' ),
359
								'standard' => esc_html__( 'Standard Transaction', 'give' )
360
							),
361
							'default' => 'donation',
362
						),
363
						array(
364
							'name' => esc_html__( 'Disable PayPal IPN Verification', 'give' ),
365
							'desc' => esc_html__( 'If donations are not getting marked as complete, use a slightly less secure method of verifying donations.', 'give' ),
366
							'id'   => 'disable_paypal_verification',
367
							'type' => 'checkbox'
368
						),
369
						array(
370
							'name' => esc_html__( 'Offline Donations', 'give' ),
371
							'desc' => '',
372
							'type' => 'give_title',
373
							'id'   => 'give_title_gateway_settings_3',
374
						),
375
						array(
376
							'name' => esc_html__( 'Collect Billing Details', 'give' ),
377
							'desc' => esc_html__( 'Enable to request billing details for offline donations. Will appear above offline donation instructions. Can be enabled/disabled per form.', 'give' ),
378
							'id'   => 'give_offline_donation_enable_billing_fields',
379
							'type' => 'checkbox'
380
						),
381
						array(
382
							'name'    => esc_html__( 'Offline Donation Instructions', 'give' ),
383
							'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' ),
384
							'id'      => 'global_offline_donation_content',
385
							'default' => give_get_default_offline_donation_content(),
386
							'type'    => 'wysiwyg',
387
							'options' => array(
388
								'textarea_rows' => 6,
389
							)
390
						),
391
						array(
392
							'name'    => esc_html__( 'Offline Donation Email Instructions Subject', 'give' ),
393
							'desc'    => esc_html__( 'Enter the subject line for the donation receipt email.', 'give' ),
394
							'id'      => 'offline_donation_subject',
395
							'default' => esc_attr__( '{donation} - Offline Donation Instructions', 'give' ),
396
							'type'    => 'text'
397
						),
398
						array(
399
							'name'    => esc_html__( 'Offline Donation Email Instructions', 'give' ),
400
							'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' ),
401
							'id'      => 'global_offline_donation_email',
402
							'default' => give_get_default_offline_donation_email_content(),
403
							'type'    => 'wysiwyg',
404
							'options' => array(
405
								'textarea_rows' => 6,
406
							)
407
						)
408
					)
409
				)
410
			),
411
			/** Display Settings */
412
			'display'     => array(
413
				'id'         => 'display_settings',
414
				'give_title' => esc_html__( 'Display Settings', 'give' ),
415
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
416
				'fields'     => apply_filters( 'give_settings_display', array(
417
						array(
418
							'name' => esc_html__( 'Display Settings', 'give' ),
419
							'desc' => '',
420
							'id'   => 'give_title_display_settings_1',
421
							'type' => 'give_title'
422
						),
423
						array(
424
							'name' => esc_html__( 'Disable CSS', 'give' ),
425
							'desc' => esc_html__( 'Enable this option if you would like to disable all of Give\'s included CSS stylesheets.', 'give' ),
426
							'id'   => 'disable_css',
427
							'type' => 'checkbox'
428
						),
429
						array(
430
							'name' => esc_html__( 'Enable Floating Labels', 'give' ),
431
							/* translators: %s: https://givewp.com/documentation/core/give-forms/creating-give-forms/#floating-labels */
432
							'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' ) ),
433
							'id'   => 'floatlabels',
434
							'type' => 'checkbox'
435
						),
436
						array(
437
							'name' => esc_html__( 'Disable Welcome Screen', 'give' ),
438
							/* translators: %s: about page URL */
439
							'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(
440
								'a' => array(
441
									'href'   => array(),
442
									'target' => array()
443
								)
444
							) ), esc_url( admin_url( 'index.php?page=give-about' ) ) ),
445
							'id'   => 'disable_welcome',
446
							'type' => 'checkbox'
447
						),
448
						array(
449
							'name' => esc_html__( 'Post Types', 'give' ),
450
							'desc' => '',
451
							'id'   => 'give_title_display_settings_2',
452
							'type' => 'give_title'
453
						),
454
						array(
455
							'name' => esc_html__( 'Disable Form Single Views', 'give' ),
456
							'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' ),
457
							'id'   => 'disable_forms_singular',
458
							'type' => 'checkbox'
459
						),
460
						array(
461
							'name' => esc_html__( 'Disable Form Archives', 'give' ),
462
							'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' ),
463
							'id'   => 'disable_forms_archives',
464
							'type' => 'checkbox'
465
						),
466
						array(
467
							'name' => esc_html__( 'Disable Form Excerpts', 'give' ),
468
							'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' ),
469
							'id'   => 'disable_forms_excerpt',
470
							'type' => 'checkbox'
471
						),
472
473
						array(
474
							'name'    => esc_html__( 'Featured Image Size', 'give' ),
475
							'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 form\'s featured image.', 'give' ),
476
							'id'      => 'featured_image_size',
477
							'type'    => 'select',
478
							'default' => 'large',
479
							'options' => give_get_featured_image_sizes()
480
						),
481
						array(
482
							'name' => esc_html__( 'Disable Form Featured Image', 'give' ),
483
							'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' ),
484
							'id'   => 'disable_form_featured_img',
485
							'type' => 'checkbox'
486
						),
487
						array(
488
							'name' => esc_html__( 'Disable Single Form Sidebar', 'give' ),
489
							'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' ),
490
							'id'   => 'disable_form_sidebar',
491
							'type' => 'checkbox'
492
						),
493
						array(
494
							'name' => esc_html__( 'Taxonomies', 'give' ),
495
							'desc' => '',
496
							'id'   => 'give_title_display_settings_3',
497
							'type' => 'give_title'
498
						),
499
						array(
500
							'name' => esc_html__( 'Enable Form Categories', 'give' ),
501
							'desc' => esc_html__( 'Enables the "Category" taxonomy for all Give forms.', 'give' ),
502
							'id'   => 'categories',
503
							'type' => 'checkbox'
504
						),
505
						array(
506
							'name' => esc_html__( 'Enable Form Tags', 'give' ),
507
							'desc' => esc_html__( 'Enables the "Tag" taxonomy for all Give forms.', 'give' ),
508
							'id'   => 'tags',
509
							'type' => 'checkbox'
510
						),
511
						// array(
512
						// 	'name' => esc_html__( 'Term and Conditions', 'give' ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
513
						// 	'desc' => '',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
514
						// 	'id'   => 'give_title_display_settings_4',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
515
						// 	'type' => 'give_title'
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
516
						// ),
517
						// array(
518
						// 	'name' => esc_html__( 'Agree to Terms Label', 'give' ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
519
						// 	'desc' => esc_html__( 'The label shown next to the agree to terms check box. Add your own to customize or leave blank to use the default text placeholder. Note: You can customize the label per form as needed.', 'give' ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
520
						// 	'id'   => 'agree_to_terms_label',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
521
						// 	'attributes'  => array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
522
						// 		'placeholder' => esc_attr__( 'Agree to Terms?', 'give' ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
523
						// 	),
524
						// 	'type' => 'text'
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
525
						// ),
526
						// array(
527
						// 	'name' => esc_html__( 'Agreement Text', 'give' ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
528
						// 	'desc' => esc_html__( 'This is the actual text which the user will have to agree to in order to make a donation. Note: You can customize the content per form as needed.', 'give' ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
529
						// 	'id'   => 'agreement_text',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
530
						// 	'type' => 'wysiwyg'
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
531
						// ),
532
					)
533
				)
534
535
			),
536
			/**
537
			 * Emails Options
538
			 */
539
			'emails'      => array(
540
				'id'         => 'email_settings',
541
				'give_title' => esc_html__( 'Email Settings', 'give' ),
542
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
543
				'fields'     => apply_filters( 'give_settings_emails', array(
544
						array(
545
							'name' => esc_html__( 'Email Settings', 'give' ),
546
							'desc' => '',
547
							'id'   => 'give_title_email_settings_1',
548
							'type' => 'give_title'
549
						),
550
						array(
551
							'id'      => 'email_template',
552
							'name'    => esc_html__( 'Email Template', 'give' ),
553
							'desc'    => esc_html__( 'Choose a template. Click "Save Changes" then "Preview Donation Receipt" to see the new template.', 'give' ),
554
							'type'    => 'select',
555
							'options' => give_get_email_templates()
556
						),
557
						array(
558
							'id'   => 'email_logo',
559
							'name' => esc_html__( 'Logo', 'give' ),
560
							'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' ),
561
							'type' => 'file'
562
						),
563
						array(
564
							'id'      => 'from_name',
565
							'name'    => esc_html__( 'From Name', 'give' ),
566
							'desc'    => esc_html__( 'The name that appears in the "From" field in donation receipt emails.', 'give' ),
567
							'default' => get_bloginfo( 'name' ),
568
							'type'    => 'text'
569
						),
570
						array(
571
							'id'      => 'from_email',
572
							'name'    => esc_html__( 'From Email', 'give' ),
573
							'desc'    => esc_html__( 'Email to send donation receipts from. This will act as the "from" and "reply-to" address.', 'give' ),
574
							'default' => get_bloginfo( 'admin_email' ),
575
							'type'    => 'text'
576
						),
577
						array(
578
							'name' => esc_html__( 'Donation Receipt', 'give' ),
579
							'desc' => '',
580
							'id'   => 'give_title_email_settings_2',
581
							'type' => 'give_title'
582
						),
583
						array(
584
							'id'      => 'donation_subject',
585
							'name'    => esc_html__( 'Donation Email Subject', 'give' ),
586
							'desc'    => esc_html__( 'Enter the subject line for the donation receipt email.', 'give' ),
587
							'default' => esc_attr__( 'Donation Receipt', 'give' ),
588
							'type'    => 'text'
589
						),
590
						array(
591
							'id'      => 'donation_receipt',
592
							'name'    => esc_html__( 'Donation Receipt', 'give' ),
593
							'desc'    => sprintf(
594
							/* translators: %s: emails tags list */
595
								esc_html__( 'Enter the email that is sent to users after completing a successful donation. HTML is accepted. Available template tags: %s', 'give' ),
596
								'<br/>' . give_get_emails_tags_list()
597
							),
598
							'type'    => 'wysiwyg',
599
							'default' => give_get_default_donation_receipt_email()
600
						),
601
						array(
602
							'name' => esc_html__( 'New Donation Notification', 'give' ),
603
							'desc' => '',
604
							'id'   => 'give_title_email_settings_3',
605
							'type' => 'give_title'
606
						),
607
						array(
608
							'id'      => 'donation_notification_subject',
609
							'name'    => esc_html__( 'Donation Notification Subject', 'give' ),
610
							'desc'    => esc_html__( 'Enter the subject line for the donation notification email.', 'give' ),
611
							'type'    => 'text',
612
							'default' => esc_attr__( 'New Donation - #{payment_id}', 'give' )
613
						),
614
						array(
615
							'id'      => 'donation_notification',
616
							'name'    => esc_html__( 'Donation Notification', 'give' ),
617
							'desc'    => sprintf(
618
							/* translators: %s: emails tags list */
619
								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' ),
620
								'<br/>' . give_get_emails_tags_list()
621
							),
622
							'type'    => 'wysiwyg',
623
							'default' => give_get_default_donation_notification_email()
624
						),
625
						array(
626
							'id'      => 'admin_notice_emails',
627
							'name'    => esc_html__( 'Donation Notification Emails', 'give' ),
628
							'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' ),
629
							'type'    => 'textarea',
630
							'default' => get_bloginfo( 'admin_email' )
631
						),
632
						array(
633
							'id'   => 'disable_admin_notices',
634
							'name' => esc_html__( 'Disable Admin Notifications', 'give' ),
635
							'desc' => esc_html__( 'Check this box if you do not want to receive emails when new donations are made.', 'give' ),
636
							'type' => 'checkbox'
637
						)
638
					)
639
				)
640
			),
641
			/** Extension Settings */
642
			'addons'      => array(
643
				'id'         => 'addons',
644
				'give_title' => esc_html__( 'Give Add-ons Settings', 'give' ),
645
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
646
				'fields'     => apply_filters( 'give_settings_addons', array()
647
				)
648
			),
649
			/** Licenses Settings */
650
			'licenses'    => array(
651
				'id'         => 'licenses',
652
				'give_title' => esc_html__( 'Give Licenses', 'give' ),
653
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
654
				'fields'     => apply_filters( 'give_settings_licenses', array()
655
				)
656
			),
657
			/** Advanced Options */
658
			'advanced'    => array(
659
				'id'         => 'advanced_options',
660
				'give_title' => esc_html__( 'Advanced Options', 'give' ),
661
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
662
				'fields'     => apply_filters( 'give_settings_advanced', array(
663
						array(
664
							'name' => esc_html__( 'Access Control', 'give' ),
665
							'desc' => '',
666
							'id'   => 'give_title_session_control_1',
667
							'type' => 'give_title'
668
						),
669
						array(
670
							'id'      => 'session_lifetime',
671
							'name'    => esc_html__( 'Session Lifetime', 'give' ),
672
							'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' ),
673
							'type'    => 'select',
674
							'options' => array(
675
								'86400'  => esc_html__( '24 Hours', 'give' ),
676
								'172800' => esc_html__( '48 Hours', 'give' ),
677
								'259200' => esc_html__( '72 Hours', 'give' ),
678
								'604800' => esc_html__( '1 Week', 'give' ),
679
							)
680
						),
681
						array(
682
							'name' => esc_html__( 'Email Access', 'give' ),
683
							'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' ),
684
							'id'   => 'email_access',
685
							'type' => 'checkbox',
686
						),
687
						array(
688
							'id'      => 'recaptcha_key',
689
							'name'    => esc_html__( 'reCAPTCHA Site Key', 'give' ),
690
							/* translators: %s: https://www.google.com/recaptcha/ */
691
							'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/' ) ),
692
							'default' => '',
693
							'type'    => 'text'
694
						),
695
						array(
696
							'id'      => 'recaptcha_secret',
697
							'name'    => esc_html__( 'reCAPTCHA Secret Key', 'give' ),
698
							'desc'    => esc_html__( 'Please paste the reCAPTCHA secret key here from your manage reCAPTCHA API Keys panel.', 'give' ),
699
							'default' => '',
700
							'type'    => 'text'
701
						),
702
						array(
703
							'name' => esc_html__( 'Data Control', 'give' ),
704
							'desc' => '',
705
							'id'   => 'give_title_data_control_2',
706
							'type' => 'give_title'
707
						),
708
						array(
709
							'name' => esc_html__( 'Remove All Data on Uninstall?', 'give' ),
710
							'desc' => esc_html__( 'When the plugin is deleted, completely remove all Give data.', 'give' ),
711
							'id'   => 'uninstall_on_delete',
712
							'type' => 'checkbox'
713
						),
714
						array(
715
							'name' => esc_html__( 'Filter Control', 'give' ),
716
							'desc' => '',
717
							'id'   => 'give_title_filter_control',
718
							'type' => 'give_title'
719
						),
720
						array(
721
							/* translators: %s: the_content */
722
							'name' => sprintf( __( 'Disable %s filter', 'give' ), '<code>the_content</code>' ),
723
							/* translators: 1: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content 2: the_content */
724
							'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>' ),
725
							'id'   => 'disable_the_content_filter',
726
							'type' => 'checkbox'
727
						),
728
						array(
729
							'name' => esc_html__( 'Script Loading', 'give' ),
730
							'desc' => '',
731
							'id'   => 'give_title_script_control',
732
							'type' => 'give_title'
733
						),
734
						array(
735
							'name' => esc_html__( 'Load Scripts in Footer?', 'give' ),
736
							'desc' => esc_html__( 'Check this box if you would like Give to load all frontend JavaScript files in the footer.', 'give' ),
737
							'id'   => 'scripts_footer',
738
							'type' => 'checkbox'
739
						)
740
					)
741
				)
742
			),
743
			/** API Settings */
744
			'api'         => array(
745
				'id'         => 'api',
746
				'give_title' => esc_html__( 'API', 'give' ),
747
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
748
				'show_names' => false, // Hide field names on the left
749
				'fields'     => apply_filters( 'give_settings_system', array(
750
						array(
751
							'id'   => 'api',
752
							'name' => esc_html__( 'API', 'give' ),
753
							'type' => 'api'
754
						)
755
					)
756
				)
757
			),
758
			/** Licenses Settings */
759
			'system_info' => array(
760
				'id'         => 'system_info',
761
				'give_title' => esc_html__( 'System Info', 'give' ),
762
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
763
				'fields'     => apply_filters( 'give_settings_system', array(
764
						array(
765
							'id'   => 'system-info-textarea',
766
							'name' => esc_html__( 'System Info', 'give' ),
767
							'desc' => esc_html__( 'Please copy and paste this information in your ticket when contacting support.', 'give' ),
768
							'type' => 'system_info'
769
						)
770
					)
771
				)
772
			),
773
		);
774
775
		$give_settings = apply_filters( 'give_registered_settings', $give_settings );
776
777
		//Return all settings array if no active tab
778
		if (  empty( $active_tab ) || ! isset( $give_settings[ $active_tab ] ) ) {
779
			return $give_settings;
780
		}
781
782
783
		// Add other tabs and settings fields as needed
784
		return $give_settings[ $active_tab ];
785
786
	}
787
788
	/**
789
	 * Show Settings Notices
790
	 */
791
	public function settings_notices() {
792
793
		if ( ! isset( $_POST['give_settings_saved'] ) ) {
794
			return;
795
		}
796
797
		add_settings_error( 'give-notices', 'global-settings-updated', esc_html__( 'Settings updated.', 'give' ), 'updated' );
798
799
	}
800
801
802
	/**
803
	 * Public getter method for retrieving protected/private variables
804
	 *
805
	 * @since  1.0
806
	 *
807
	 * @param  string $field Field to retrieve
808
	 *
809
	 * @return mixed         Field value or exception is thrown.
810
	 * @throws Exception     Throws an exception if the field is invalid.
811
	 */
812
	public function __get( $field ) {
813
814
		// Allowed fields to retrieve
815
		if ( in_array( $field, array( 'key', 'fields', 'give_title', 'options_page' ), true ) ) {
816
			return $this->{$field};
817
		}
818
		if ( 'option_metabox' === $field ) {
819
			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...
820
		}
821
822
		throw new Exception( sprintf( esc_html__( 'Invalid property: %s', 'give' ), $field ) );
823
	}
824
825
826
}
827
828
// Get it started
829
$Give_Settings = new Give_Plugin_Settings();
830
831
/**
832
 * Wrapper function around cmb2_get_option
833
 * @since  0.1.0
834
 *
835
 * @param  string $key     Options array key
836
 * @param  string $default The default option if the option isn't set
837
 *
838
 * @return mixed        Option value
839
 */
840
function give_get_option( $key = '', $default = false ) {
841
	$give_options = give_get_settings();
842
	$value        = ! empty( $give_options[ $key ] ) ? $give_options[ $key ] : $default;
843
	$value        = apply_filters( 'give_get_option', $value, $key, $default );
844
845
	return apply_filters( "give_get_option_{$key}", $value, $key, $default );
846
}
847
848 61
849 61
/**
850 61
 * Update an option
851
 *
852 61
 * Updates an give setting value in both the db and the global variable.
853
 * Warning: Passing in an empty, false or null string value will remove
854
 *          the key from the give_options array.
855
 *
856
 * @since 1.0
857
 *
858
 * @param string          $key   The Key to update
859
 * @param string|bool|int $value The value to set the key to
860
 *
861
 * @return boolean True if updated, false if not.
862
 */
863
function give_update_option( $key = '', $value = false ) {
864
865
	// If no key, exit
866
	if ( empty( $key ) ) {
867
		return false;
868
	}
869
870
	if ( empty( $value ) ) {
871
		$remove_option = give_delete_option( $key );
872
873 2
		return $remove_option;
874
	}
875
876
	// First let's grab the current settings
877 2
	$options = get_option( 'give_settings' );
878 2
879
	// Let's let devs alter that value coming in
880 2
	$value = apply_filters( 'give_update_option', $value, $key );
881
882
	// Next let's try to update the value
883
	$options[ $key ] = $value;
884 1
	$did_update      = update_option( 'give_settings', $options );
885
886
	// If it updated, let's update the global variable
887 1
	if ( $did_update ) {
888
		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...
889
		$give_options[ $key ] = $value;
890 1
	}
891 1
892
	return $did_update;
893
}
894 1
895 1
/**
896 1
 * Remove an option
897 1
 *
898
 * Removes an give setting value in both the db and the global variable.
899 1
 *
900
 * @since 1.0
901
 *
902
 * @global       $give_options
903
 *
904
 * @param string $key The Key to delete
905
 *
906
 * @return boolean True if updated, false if not.
907
 */
908
function give_delete_option( $key = '' ) {
909
910
	// If no key, exit
911
	if ( empty( $key ) ) {
912
		return false;
913
	}
914
915
	// First let's grab the current settings
916 2
	$options = get_option( 'give_settings' );
917
918
	// Next let's try to update the value
919
	if ( isset( $options[ $key ] ) ) {
920
921 2
		unset( $options[ $key ] );
922
923
	}
924 2
925
	$did_update = update_option( 'give_settings', $options );
926 1
927
	// If it updated, let's update the global variable
928 1
	if ( $did_update ) {
929
		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...
930 2
		$give_options = $options;
931
	}
932
933 2
	return $did_update;
934 1
}
935 1
936 1
937
/**
938 2
 * Get Settings
939
 *
940
 * Retrieves all Give plugin settings
941
 *
942
 * @since 1.0
943
 * @return array Give settings
944
 */
945
function give_get_settings() {
946
947
	$settings = get_option( 'give_settings' );
948
949
	return (array) apply_filters( 'give_get_settings', $settings );
950
951
}
952
953
954
/**
955
 * Give Settings Array Insert.
956
 *
957
 * Allows other Add-ons and plugins to insert Give settings at a desired position.
958
 *
959
 * @since      1.3.5
960
 *
961
 * @param $array
962
 * @param $position |int|string Expects an array key or 'id' of the settings field to appear after
963
 * @param $insert   |array a valid array of options to insert
964
 *
965
 * @return array
966
 */
967
function give_settings_array_insert( $array, $position, $insert ) {
968
	if ( is_int( $position ) ) {
969
		array_splice( $array, $position, 0, $insert );
970
	} else {
971
972
		foreach ( $array as $index => $subarray ) {
973
			if ( isset( $subarray['id'] ) && $subarray['id'] == $position ) {
974
				$pos = $index;
975
			}
976
		}
977
978
		if ( ! isset( $pos ) ) {
979
			return $array;
980
		}
981
982
		$array = array_merge(
983
			array_slice( $array, 0, $pos ),
984
			$insert,
985
			array_slice( $array, $pos )
986
		);
987
	}
988
989
	return $array;
990
}
991
992
993
/**
994
 * Gateways Callback
995
 *
996
 * Renders gateways fields.
997
 *
998
 * @since 1.0
999
 * @param array $field_arr
1000
 * @param array $saved_values
1001
 * @return void
1002
 */
1003
function give_enabled_gateways_callback( $field_arr, $saved_values = array() ) {
1004
1005
	$id       = $field_arr['id'];
1006
	$gateways = give_get_ordered_payment_gateways( give_get_payment_gateways() );
1007
1008
	echo '<ul class="give-checklist-fields give-payment-gatways-list">';
1009
1010
	foreach ( $gateways as $key => $option ) :
1011
1012
		if ( is_array( $saved_values ) && array_key_exists( $key, $saved_values ) ) {
1013
			$enabled = '1';
1014
		} else {
1015
			$enabled = null;
1016
		}
1017
1018
		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;';
1019
		echo '<label for="' . $id . '[' . $key . ']">' . $option['admin_label'] . '</label></li>';
1020
1021
	endforeach;
1022
1023
	echo '</ul>';
1024
}
1025
1026
/**
1027
 * Gateways Callback (drop down)
1028
 *
1029
 * Renders gateways select menu
1030
 *
1031
 * @since  1.0
1032
 * @param  array $field_arr
1033
 * @param  array $saved_value
1034
 * @return void
1035
 */
1036
function give_default_gateway_callback( $field_arr, $saved_value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $saved_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...
1037
	$id                = $field_arr['id'];
1038
	$gateways          = give_get_enabled_payment_gateways();
1039
	$saved_value       = give_get_default_gateway( null );
1040
1041
	echo '<select class="give-select" name="' . $id . '" id="' . $id . '">';
1042
1043
		foreach ( $gateways as $key => $option ) :
1044
			$selected = isset( $saved_value ) ? selected( $key, $saved_value, false ) : '';
1045
			echo '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>';
1046
		endforeach;
1047
1048
	echo '</select>';
1049
1050
}
1051
1052
/**
1053
 * Give Title
1054
 *
1055
 * Renders custom section titles output; Really only an  because CMB2's output is a bit funky
1056
 *
1057
 * @since 1.0
1058
 *
1059
 * @param       $field_object , $escaped_value, $object_id, $object_type, $field_type_object
1060
 *
1061
 * @return void
1062
 */
1063
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...
1064
1065
	$id                = $field_type_object->field->args['id'];
1066
	$title             = $field_type_object->field->args['name'];
1067
	$field_description = $field_type_object->field->args['desc'];
1068
1069
	echo '<hr>' . $field_description;
1070
1071
}
1072
1073
/**
1074
 * Give Description
1075
 *
1076
 * Renders custom description text which any plugin can use to output content, html, php, etc.
1077
 *
1078
 * @since      1.3.5
1079
 *
1080
 * @param       $field_object , $escaped_value, $object_id, $object_type, $field_type_object
1081
 *
1082
 * @return void
1083
 */
1084
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...
1085
1086
	$id                = $field_type_object->field->args['id'];
1087
	$title             = $field_type_object->field->args['name'];
1088
	$field_description = $field_type_object->field->args['desc'];
1089
1090
1091
	echo $field_description;
1092
1093
}
1094
1095
/**
1096
 * Gets a number of posts and displays them as options
1097
 *
1098
 * @param  array $query_args Optional. Overrides defaults.
1099
 * @param  bool  $force      Force the pages to be loaded even if not on settings
1100
 *
1101
 * @see: https://github.com/WebDevStudios/CMB2/wiki/Adding-your-own-field-types
1102
 * @return array An array of options that matches the CMB2 options array
1103
 */
1104
function give_cmb2_get_post_options( $query_args, $force = false ) {
1105
1106
	$post_options = array( '' => '' ); // Blank option
1107
1108
	if ( ( ! isset( $_GET['page'] ) || 'give-settings' != $_GET['page'] ) && ! $force ) {
1109
		return $post_options;
1110
	}
1111
1112
	$args = wp_parse_args( $query_args, array(
1113
		'post_type'   => 'page',
1114
		'numberposts' => 10,
1115
	) );
1116
1117
	$posts = get_posts( $args );
1118
1119
	if ( $posts ) {
1120
		foreach ( $posts as $post ) {
1121
1122
			$post_options[ $post->ID ] = $post->post_title;
1123
1124
		}
1125
	}
1126
1127
	return $post_options;
1128
}
1129
1130
1131
/**
1132
 * Featured Image Sizes
1133
 *
1134
 * Outputs an array for the "Featured Image Size" option found under Settings > Display Options.
1135
 *
1136
 * @since 1.4
1137
 *
1138
 * @global $_wp_additional_image_sizes
1139
 */
1140
function give_get_featured_image_sizes() {
1141
	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...
1142
	$sizes     = array();
1143
	$get_sizes = get_intermediate_image_sizes();
1144
1145
	// check whether intermediate image sizes exist first
1146
	if ( empty( $get_sizes ) ) {
1147
		$get_sizes = array( 'thumbnail', 'medium', 'medium_large', 'large' );
1148
	}
1149
1150
	foreach ( $get_sizes as $_size ) {
1151
1152
		if ( in_array( $_size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
1153
			$sizes[ $_size ] = $_size . ' - ' . get_option( "{$_size}_size_w" ) . 'x' . get_option( "{$_size}_size_h" );
1154
		} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
1155
			$sizes[ $_size ] = $_size . ' - ' . $_wp_additional_image_sizes[ $_size ]['width'] . 'x' . $_wp_additional_image_sizes[ $_size ]['height'];
1156
		}
1157
1158
	}
1159
1160
	return apply_filters( 'give_get_featured_image_sizes', $sizes );
1161
}
1162
1163
1164
/**
1165
 * Give License Key Callback
1166
 *
1167
 * Registers the license field callback for EDD's Software Licensing.
1168
 *
1169
 * @since       1.0
1170
 *
1171
 * @param array $field_object , $escaped_value, $object_id, $object_type, $field_type_object Arguments passed by CMB2
1172
 *
1173
 * @return void
1174
 */
1175
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...
1176
	/* @var CMB2_Types $field_type_object */
1177
1178
    $id                   = $field_type_object->field->args['id'];
1179
	$field_description    = $field_type_object->field->args['desc'];
1180
	$license              = $field_type_object->field->args['options']['license'];
1181
    $license_key          = $escaped_value;
1182
    $is_license_key       = apply_filters( 'give_is_license_key', ( is_object( $license ) && ! empty( $license ) ) );
1183
    $is_valid_license     = apply_filters( 'give_is_valid_license', ( $is_license_key && property_exists( $license, 'license' ) && 'valid' === $license->license ) );
1184
    $shortname            = $field_type_object->field->args['options']['shortname'];
1185
	$field_classes        = 'regular-text give-license-field';
1186
	$type                 = empty( $escaped_value ) || ! $is_valid_license ? 'text' : 'password';
1187
    $custom_html          = '';
1188
    $messages             = array();
1189
    $class                = '';
1190
    $account_page_link    = $field_type_object->field->args['options']['account_url'];
1191
    $checkout_page_link   = $field_type_object->field->args['options']['checkout_url'];
1192
    $addon_name           = $field_type_object->field->args['options']['item_name'];
1193
    $license_status       = null;
1194
    $is_in_subscription   = null;
1195
1196
	// By default query on edd api url will return license object which contain status and message property, this can break below functionality.
1197
	// To combat that check if status is set to error or not, if yes then set $is_license_key to false.
1198
	if ( $is_license_key && property_exists( $license, 'status' ) && 'error' === $license->status ) {
1199
		$is_license_key = false;
1200
	}
1201
1202
1203
	// Check if current license is part of subscription or not.
1204
	$subscriptions = get_option( 'give_subscriptions' );
1205
1206
	if ( $is_license_key && $subscriptions ) {
1207
		foreach ( $subscriptions as $subscription ) {
1208
			if ( in_array( $license_key, $subscription['licenses'] ) ) {
1209
				$is_in_subscription = $subscription['id'];
1210
				break;
1211
			}
1212
		}
1213
	}
1214
1215
1216
	if ( $is_license_key ) {
1217
		if ( $is_in_subscription ) {
1218
			$subscription_expires = strtotime( $subscriptions[ $is_in_subscription ]['expires'] );
1219
			$subscription_status  = esc_html__( 'renew', 'give' );
1220
1221
			if ( ( 'active' !== $subscriptions[ $is_in_subscription ]['status'] ) ) {
1222
				$subscription_status = esc_html__( 'expire', 'give' );
1223
			}
1224
1225
			if ( $subscription_expires < current_time( 'timestamp', 1 ) ) {
1226
				$messages[]     = sprintf(
1227
					__( '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' ),
1228
					urldecode( $subscriptions[ $is_in_subscription ]['invoice_url'] ),
1229
					$subscriptions[ $is_in_subscription ]['payment_id'],
1230
					$checkout_page_link . '?edd_license_key=' . $subscriptions[ $is_in_subscription ]['license_key'] . '&utm_campaign=admin&utm_source=licenses&utm_medium=expired'
1231
				);
1232
				$license_status = 'license-expired';
1233
			} elseif ( strtotime( '- 7 days', $subscription_expires ) < current_time( 'timestamp', 1 ) ) {
1234
				$messages[]     = sprintf(
1235
					__( 'Your subscription (<a href="%s" target="_blank">#%d</a>) will %s in %s.', 'give' ),
1236
					urldecode( $subscriptions[ $is_in_subscription ]['invoice_url'] ),
1237
					$subscriptions[ $is_in_subscription ]['payment_id'],
1238
					$subscription_status,
1239
					human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscriptions[ $is_in_subscription ]['expires'] ) )
1240
				);
1241
				$license_status = 'license-expires-soon';
1242
			} else {
1243
				$messages[]     = sprintf(
1244
					__( 'Your subscription (<a href="%s" target="_blank">#%d</a>) will %s on %s.', 'give' ),
1245
					urldecode( $subscriptions[ $is_in_subscription ]['invoice_url'] ),
1246
					$subscriptions[ $is_in_subscription ]['payment_id'],
1247
					$subscription_status,
1248
					date_i18n( get_option( 'date_format' ), strtotime( $subscriptions[ $is_in_subscription ]['expires'], current_time( 'timestamp' ) ) )
1249
				);
1250
				$license_status = 'license-expiration-date';
1251
			}
1252
1253
1254
		} elseif ( empty( $license->success ) && property_exists( $license, 'error' ) ) {
1255
1256
            // activate_license 'invalid' on anything other than valid, so if there was an error capture it
1257
            switch(   $license->error ) {
1258
                case 'expired' :
1259
                    $class = $license->error;
1260
                    $messages[] = sprintf(
1261
                        __( 'Your license key expired on %s. Please <a href="%s" target="_blank" title="Renew your license key">renew your license key</a>.', 'give' ),
1262
                        date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ),
1263
                        $checkout_page_link . '?edd_license_key=' . $license_key . '&utm_campaign=admin&utm_source=licenses&utm_medium=expired'
1264
                    );
1265
                    $license_status = 'license-' . $class;
1266
                    break;
1267
1268
				case 'missing' :
1269
					$class          = $license->error;
1270
					$messages[]     = sprintf(
1271
						__( 'Invalid license. Please <a href="%s" target="_blank" title="Visit account page">visit your account page</a> and verify it.', 'give' ),
1272
						$account_page_link . '?utm_campaign=admin&utm_source=licenses&utm_medium=missing'
1273
					);
1274
					$license_status = 'license-' . $class;
1275
					break;
1276
1277
				case 'invalid' :
1278
					$class          = $license->error;
1279
					$messages[]     = sprintf(
1280
						__( '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' ),
1281
						$addon_name,
1282
						$account_page_link . '?utm_campaign=admin&utm_source=licenses&utm_medium=invalid'
1283
					);
1284
					$license_status = 'license-' . $class;
1285
					break;
1286
1287
				case 'site_inactive' :
1288
					$class          = $license->error;
1289
					$messages[]     = sprintf(
1290
						__( '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' ),
1291
						$addon_name,
1292
						$account_page_link . '?utm_campaign=admin&utm_source=licenses&utm_medium=invalid'
1293
					);
1294
					$license_status = 'license-' . $class;
1295
					break;
1296
1297
                case 'item_name_mismatch' :
1298
                    $class = $license->error;
1299
                    $messages[] = sprintf( __( 'This license %s does not belong to %s.', 'give' ), $license_key, $addon_name );
1300
                    $license_status = 'license-' . $class;
1301
                    break;
1302
1303
				case 'no_activations_left':
1304
					$class          = $license->error;
1305
					$messages[]     = sprintf( __( 'Your license key has reached it\'s activation limit. <a href="%s">View possible upgrades</a> now.', 'give' ), $account_page_link );
1306
					$license_status = 'license-' . $class;
1307
					break;
1308
			}
1309
		} else {
1310
			switch ( $license->license ) {
1311
				case 'valid' :
1312
				default:
1313
					$class      = 'valid';
1314
					$now        = current_time( 'timestamp' );
1315
					$expiration = strtotime( $license->expires, current_time( 'timestamp' ) );
1316
1317
					if ( 'lifetime' === $license->expires ) {
1318
						$messages[]     = esc_html__( 'License key never expires.', 'give' );
1319
						$license_status = 'license-lifetime-notice';
1320
					} elseif ( $expiration > $now && $expiration - $now < ( DAY_IN_SECONDS * 30 ) ) {
1321
						$messages[]     = sprintf(
1322
							__( 'Your license key expires soon! It expires on %s. <a href="%s" target="_blank" title="Renew license">Renew your license key</a>.', 'give' ),
1323
							date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ),
1324
							$checkout_page_link . '?edd_license_key=' . $value . '&utm_campaign=admin&utm_source=licenses&utm_medium=renew'
0 ignored issues
show
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
1325
						);
1326
						$license_status = 'license-expires-soon';
1327
					} else {
1328
						$messages[]     = sprintf(
1329
							__( 'Your license key expires on %s.', 'give' ),
1330
							date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) )
1331
						);
1332
						$license_status = 'license-expiration-date';
1333
					}
1334
					break;
1335
			}
1336
		}
1337
	} else {
1338
		$messages[]     = sprintf(
1339
			__( 'To receive updates, please enter your valid %s license key.', 'give' ),
1340
			$addon_name
1341
		);
1342
		$license_status = 'inactive';
1343
	}
1344
1345
1346
	// Add class for input field if license is active.
1347
	if ( $is_valid_license ) {
1348
		$field_classes .= ' give-license-active';
1349
	}
1350
1351
	// Get input field html.
1352
	$input_field_html = "<input type=\"{$type}\" name=\"{$id}\" class=\"{$field_classes}\" value=\"{$license_key}\">";
1353
1354
	// If license is active so show deactivate button.
1355
	if ( $is_valid_license ) {
1356
        // Get input field html.
1357
		$input_field_html = "<input type=\"{$type}\" name=\"{$id}\" class=\"{$field_classes}\" value=\"{$license_key}\" readonly=\"readonly\">";
1358
1359
		$custom_html = '<input type="submit" class="button button-small give-license-deactivate" name="' . $id . '_deactivate" value="' . esc_attr__( 'Deactivate License', 'give' ) . '"/>';
1360
1361
1362
	}
1363
1364
	// Field description.
1365
	$custom_html .= '<label for="give_settings[' . $id . ']"> ' . $field_description . '</label>';
1366
1367
	// If no messages found then inform user that to get updated in future register yourself.
1368
	if ( empty( $messages ) ) {
1369
		$messages[] = apply_filters( "{$shortname}_default_addon_notice", esc_html__( 'To receive updates, please enter your valid license key.', 'give' ) );
1370
	}
1371
1372
    foreach( $messages as $message ) {
1373
        $custom_html .= '<div class="give-license-status-notice give-' . $license_status . '">';
1374
        $custom_html .= '<p>' . $message . '</p>';
1375
        $custom_html .= '</div>';
1376
    }
1377
1378
1379
	// Field html.
1380
	$custom_html = apply_filters( 'give_license_key_field_html', $input_field_html . $custom_html, $field_type_object );
1381
1382
	// Nonce.
1383
	wp_nonce_field( $id . '-nonce', $id . '-nonce' );
1384
1385
    // Print field html.
1386
    echo "<div class=\"give-license-key\"><label for=\"{$id}\">{$addon_name }</label></div><div class=\"give-license-block\">{$custom_html}</div>";
1387
}
1388
1389
1390
/**
1391
 * Display the API Keys
1392
 *
1393
 * @since       1.0
1394
 * @return      void
1395
 */
1396
function give_api_callback() {
1397
1398
	if ( ! current_user_can( 'manage_give_settings' ) ) {
1399
		return;
1400
	}
1401
1402
	/**
1403
	 * Fires before displaying API keys.
1404
	 *
1405
	 * @since 1.0
1406
	 */
1407
	do_action( 'give_tools_api_keys_before' );
1408
1409
	require_once GIVE_PLUGIN_DIR . 'includes/admin/class-api-keys-table.php';
1410
1411
	$api_keys_table = new Give_API_Keys_Table();
1412
	$api_keys_table->prepare_items();
1413
	$api_keys_table->display();
1414
	?>
1415
	<span class="cmb2-metabox-description api-description">
1416
		<?php echo sprintf(
1417
		/* translators: 1: https://givewp.com/documentation/give-api-reference/ 2: https://givewp.com/addons/zapier/ */
1418
			__( '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' ),
1419
			esc_url( 'https://givewp.com/documentation/give-api-reference/' ),
1420
			esc_url( 'https://givewp.com/addons/zapier/' )
1421
		); ?>
1422
	</span>
1423
	<?php
1424
1425
	/**
1426
	 * Fires after displaying API keys.
1427
	 *
1428
	 * @since 1.0
1429
	 */
1430
	do_action( 'give_tools_api_keys_after' );
1431
}
1432
1433
add_action( 'give_settings_tab_api_keys', 'give_api_callback' );
1434
1435
/**
1436
 * Hook Callback
1437
 *
1438
 * Adds a do_action() hook in place of the field.
1439
 *
1440
 * @since 1.0
1441
 *
1442
 * @param array $args Arguments passed by the setting
1443
 *
1444
 * @return void
1445
 */
1446
function give_hook_callback( $args ) {
1447
1448
	$id = $args['id'];
1449
1450
	/**
1451
	 * Fires in give field.
1452
	 *
1453
	 * @since 1.0
1454
	 */
1455
	do_action( "give_{$id}" );
1456
1457
}
1458
1459
1460
/**
1461
 * Check if radio(enabled/disabled) and checkbox(on) is active or not.
1462
 *
1463
 * @since  1.8
1464
 * @param  string $value
1465
 * @param  string $compare_with
1466
 * @return bool
1467
 */
1468
function give_is_setting_enabled( $value, $compare_with = null ) {
1469
	if( ! is_null( $compare_with ) ) {
1470
1471
		if( is_array( $compare_with ) ) {
1472
			// Output.
1473
			return in_array( $value, $compare_with );
1474
		}
1475
1476
		// Output.
1477
		return ( $value === $compare_with );
1478
	}
1479
1480
	// Backward compatibility: From version 1.8 most of setting is modified to enabled/disabled
1481
	// Output.
1482
	return ( in_array( $value, array( 'enabled', 'on', 'yes' ) ) ? true : false );
1483
}