Completed
Pull Request — master (#1055)
by Rami
19:01
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 0
Metric Value
cc 34
eloc 149
nc 10240
nop 5
dl 0
loc 222
ccs 0
cts 32
cp 0
crap 1190
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
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 832.

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 echo get_admin_page_title(); ?></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, 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
						array(
515
							'name' => esc_html__( 'Term and Conditions', 'give' ),
516
							'desc' => '',
517
							'id'   => 'give_title_display_settings_4',
518
							'type' => 'give_title'
519
						),
520
						array(
521
							'name' => esc_html__( 'Agree to Terms Label', 'give' ),
522
							'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' ),
523
							'id'   => 'agree_to_terms_label',
524
							'attributes'  => array(
525
								'placeholder' => esc_attr__( 'Agree to Terms?', 'give' ),
526
							),
527
							'type' => 'text'
528
						),
529
						array(
530
							'name' => esc_html__( 'Agreement Text', 'give' ),
531
							'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' ),
532
							'id'   => 'agreement_text',
533
							'type' => 'wysiwyg'
534
						),
535
					)
536
				)
537
538
			),
539
			/**
540
			 * Emails Options
541
			 */
542
			'emails'      => array(
543
				'id'         => 'email_settings',
544
				'give_title' => esc_html__( 'Email Settings', 'give' ),
545
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
546
				'fields'     => apply_filters( 'give_settings_emails', array(
547
						array(
548
							'name' => esc_html__( 'Email Settings', 'give' ),
549
							'desc' => '',
550
							'id'   => 'give_title_email_settings_1',
551
							'type' => 'give_title'
552
						),
553
						array(
554
							'id'      => 'email_template',
555
							'name'    => esc_html__( 'Email Template', 'give' ),
556
							'desc'    => esc_html__( 'Choose a template. Click "Save Changes" then "Preview Donation Receipt" to see the new template.', 'give' ),
557
							'type'    => 'select',
558
							'options' => give_get_email_templates()
559
						),
560
						array(
561
							'id'   => 'email_logo',
562
							'name' => esc_html__( 'Logo', 'give' ),
563
							'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' ),
564
							'type' => 'file'
565
						),
566
						array(
567
							'id'      => 'from_name',
568
							'name'    => esc_html__( 'From Name', 'give' ),
569
							'desc'    => esc_html__( 'The name which appears in the "From" field in donation receipt emails.', 'give' ),
570
							'default' => get_bloginfo( 'name' ),
571
							'type'    => 'text'
572
						),
573
						array(
574
							'id'      => 'from_email',
575
							'name'    => esc_html__( 'From Email', 'give' ),
576
							'desc'    => esc_html__( 'Email to send donation receipts from. This will act as the "from" and "reply-to" address.', 'give' ),
577
							'default' => get_bloginfo( 'admin_email' ),
578
							'type'    => 'text'
579
						),
580
						array(
581
							'name' => esc_html__( 'Donation Receipt', 'give' ),
582
							'desc' => '',
583
							'id'   => 'give_title_email_settings_2',
584
							'type' => 'give_title'
585
						),
586
						array(
587
							'id'      => 'donation_subject',
588
							'name'    => esc_html__( 'Donation Email Subject', 'give' ),
589
							'desc'    => esc_html__( 'Enter the subject line for the donation receipt email.', 'give' ),
590
							'default' => esc_attr__( 'Donation Receipt', 'give' ),
591
							'type'    => 'text'
592
						),
593
						array(
594
							'id'      => 'donation_receipt',
595
							'name'    => esc_html__( 'Donation Receipt', 'give' ),
596
							'desc'    => sprintf(
597
								/* translators: %s: emails tags list */
598
								esc_html__( 'Enter the email that is sent to users after completing a successful donation. HTML is accepted. Available template tags: %s', 'give' ),
599
								'<br/>'.give_get_emails_tags_list()
600
							),
601
							'type'    => 'wysiwyg',
602
							'default' => give_get_default_donation_receipt_email()
603
						),
604
						array(
605
							'name' => esc_html__( 'New Donation Notification', 'give' ),
606
							'desc' => '',
607
							'id'   => 'give_title_email_settings_3',
608
							'type' => 'give_title'
609
						),
610
						array(
611
							'id'      => 'donation_notification_subject',
612
							'name'    => esc_html__( 'Donation Notification Subject', 'give' ),
613
							'desc'    => esc_html__( 'Enter the subject line for the donation notification email.', 'give' ),
614
							'type'    => 'text',
615
							'default' => esc_attr__( 'New Donation - #{payment_id}', 'give' )
616
						),
617
						array(
618
							'id'      => 'donation_notification',
619
							'name'    => esc_html__( 'Donation Notification', 'give' ),
620
							'desc'    => sprintf(
621
								/* translators: %s: emails tags list */
622
								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' ),
623
								'<br/>'.give_get_emails_tags_list()
624
							),
625
							'type'    => 'wysiwyg',
626
							'default' => give_get_default_donation_notification_email()
627
						),
628
						array(
629
							'id'      => 'admin_notice_emails',
630
							'name'    => esc_html__( 'Donation Notification Emails', 'give' ),
631
							'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' ),
632
							'type'    => 'textarea',
633
							'default' => get_bloginfo( 'admin_email' )
634
						),
635
						array(
636
							'id'   => 'disable_admin_notices',
637
							'name' => esc_html__( 'Disable Admin Notifications', 'give' ),
638
							'desc' => esc_html__( 'Check this box if you do not want to receive emails when new donations are made.', 'give' ),
639
							'type' => 'checkbox'
640
						)
641
					)
642
				)
643
			),
644
			/** Extension Settings */
645
			'addons'      => array(
646
				'id'         => 'addons',
647
				'give_title' => esc_html__( 'Give Add-ons Settings', 'give' ),
648
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
649
				'fields'     => apply_filters( 'give_settings_addons', array()
650
				)
651
			),
652
			/** Licenses Settings */
653
			'licenses'    => array(
654
				'id'         => 'licenses',
655
				'give_title' => esc_html__( 'Give Licenses', 'give' ),
656
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
657
				'fields'     => apply_filters( 'give_settings_licenses', array()
658
				)
659
			),
660
			/** Advanced Options */
661
			'advanced'    => array(
662
				'id'         => 'advanced_options',
663
				'give_title' => esc_html__( 'Advanced Options', 'give' ),
664
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
665
				'fields'     => apply_filters( 'give_settings_advanced', array(
666
						array(
667
							'name' => esc_html__( 'Access Control', 'give' ),
668
							'desc' => '',
669
							'id'   => 'give_title_session_control_1',
670
							'type' => 'give_title'
671
						),
672
						array(
673
							'id'      => 'session_lifetime',
674
							'name'    => esc_html__( 'Session Lifetime', 'give' ),
675
							'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' ),
676
							'type'    => 'select',
677
							'options' => array(
678
								'86400'  => esc_html__( '24 Hours', 'give' ),
679
								'172800' => esc_html__( '48 Hours', 'give' ),
680
								'259200' => esc_html__( '72 Hours', 'give' ),
681
								'604800' => esc_html__( '1 Week', 'give' ),
682
							)
683
						),
684
						array(
685
							'name' => esc_html__( 'Email Access', 'give' ),
686
							'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' ),
687
							'id'   => 'email_access',
688
							'type' => 'checkbox',
689
						),
690
						array(
691
							'id'      => 'recaptcha_key',
692
							'name'    => esc_html__( 'reCAPTCHA Site Key', 'give' ),
693
							/* translators: %s: https://www.google.com/recaptcha/ */
694
							'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/' ) ),
695
							'default' => '',
696
							'type'    => 'text'
697
						),
698
						array(
699
							'id'      => 'recaptcha_secret',
700
							'name'    => esc_html__( 'reCAPTCHA Secret Key', 'give' ),
701
							'desc'    => esc_html__( 'Please paste the reCAPTCHA secret key here from your manage reCAPTCHA API Keys panel.', 'give' ),
702
							'default' => '',
703
							'type'    => 'text'
704
						),
705
						array(
706
							'name' => esc_html__( 'Data Control', 'give' ),
707
							'desc' => '',
708
							'id'   => 'give_title_data_control_2',
709
							'type' => 'give_title'
710
						),
711
						array(
712
							'name' => esc_html__( 'Remove All Data on Uninstall?', 'give' ),
713
							'desc' => esc_html__( 'When the plugin is deleted, completely remove all Give data.', 'give' ),
714
							'id'   => 'uninstall_on_delete',
715
							'type' => 'checkbox'
716
						),
717
						array(
718
							'name' => esc_html__( 'Filter Control', 'give' ),
719
							'desc' => '',
720
							'id'   => 'give_title_filter_control',
721
							'type' => 'give_title'
722
						),
723
						array(
724
							/* translators: %s: the_content */
725
							'name' => sprintf( __( 'Disable %s filter', 'give' ), '<code>the_content</code>' ),
726
							/* translators: 1: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content 2: the_content */
727
							'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>' ),
728
							'id'   => 'disable_the_content_filter',
729
							'type' => 'checkbox'
730
						),
731
						array(
732
							'name' => esc_html__( 'Script Loading', 'give' ),
733
							'desc' => '',
734
							'id'   => 'give_title_script_control',
735
							'type' => 'give_title'
736
						),
737
						array(
738
							'name' => esc_html__( 'Load Scripts in Footer?', 'give' ),
739
							'desc' => esc_html__( 'Check this box if you would like Give to load all frontend JavaScript files in the footer.', 'give' ),
740
							'id'   => 'scripts_footer',
741
							'type' => 'checkbox'
742
						)
743
					)
744
				)
745
			),
746
			/** API Settings */
747
			'api'         => array(
748
				'id'         => 'api',
749
				'give_title' => esc_html__( 'API', 'give' ),
750
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
751
				'show_names' => false, // Hide field names on the left
752
				'fields'     => apply_filters( 'give_settings_system', array(
753
						array(
754
							'id'   => 'api',
755
							'name' => esc_html__( 'API', 'give' ),
756
							'type' => 'api'
757
						)
758
					)
759
				)
760
			),
761
			/** Licenses Settings */
762
			'system_info' => array(
763
				'id'         => 'system_info',
764
				'give_title' => esc_html__( 'System Info', 'give' ),
765
				'show_on'    => array( 'key' => 'options-page', 'value' => array( $this->key, ), ),
766
				'fields'     => apply_filters( 'give_settings_system', array(
767
						array(
768
							'id'   => 'system-info-textarea',
769
							'name' => esc_html__( 'System Info', 'give' ),
770
							'desc' => esc_html__( 'Please copy and paste this information in your ticket when contacting support.', 'give' ),
771
							'type' => 'system_info'
772
						)
773
					)
774
				)
775
			),
776
		);
777
778
		//Return all settings array if no active tab
779
		if ( $active_tab === null || ! isset( $give_settings[ $active_tab ] ) ) {
780
781
			return apply_filters( 'give_registered_settings', $give_settings );
782
783
		}
784
785
786
		// Add other tabs and settings fields as needed
787
		return apply_filters( 'give_registered_settings', $give_settings[ $active_tab ] );
788
789
	}
790
791
	/**
792
	 * Show Settings Notices
793
	 */
794
	public function settings_notices() {
795
796
		if ( ! isset( $_POST['give_settings_saved'] ) ) {
797
			return;
798
		}
799
800
		add_settings_error( 'give-notices', 'global-settings-updated', esc_html__( 'Settings updated.', 'give' ), 'updated' );
801
802
	}
803
804
805
	/**
806
	 * Public getter method for retrieving protected/private variables
807
	 *
808
	 * @since  1.0
809
	 *
810
	 * @param  string $field Field to retrieve
811
	 *
812
	 * @return mixed         Field value or exception is thrown.
813
	 * @throws Exception     Throws an exception if the field is invalid.
814
	 */
815
	public function __get( $field ) {
816
817
		// Allowed fields to retrieve
818
		if ( in_array( $field, array( 'key', 'fields', 'give_title', 'options_page' ), true ) ) {
819
			return $this->{$field};
820
		}
821
		if ( 'option_metabox' === $field ) {
822
			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...
823
		}
824
825
		throw new Exception( sprintf( esc_html__( 'Invalid property: %s', 'give' ), $field ) );
826
	}
827
828
829
}
830
831
// Get it started
832
$Give_Settings = new Give_Plugin_Settings();
833
834
/**
835
 * Wrapper function around cmb2_get_option
836
 * @since  0.1.0
837
 *
838
 * @param  string $key Options array key
839
 * @param  string $default The default option if the option isn't set
840
 *
841
 * @return mixed        Option value
842
 */
843
function give_get_option( $key = '', $default = false ) {
844
	$give_options = give_get_settings();
845
	$value = ! empty( $give_options[ $key ] ) ? $give_options[ $key ] : $default;
846
	$value = apply_filters( 'give_get_option', $value, $key, $default );
847
848 61
	return apply_filters( "give_get_option_{$key}", $value, $key, $default );
849 61
}
850 61
851
852 61
/**
853
 * Update an option
854
 *
855
 * Updates an give setting value in both the db and the global variable.
856
 * Warning: Passing in an empty, false or null string value will remove
857
 *          the key from the give_options array.
858
 *
859
 * @since 1.0
860
 *
861
 * @global $give_options
862
 *
863
 * @param string          $key The Key to update
864
 * @param string|bool|int $value The value to set the key to
865
 *
866
 * @return boolean True if updated, false if not.
867
 */
868
function give_update_option( $key = '', $value = false ) {
869
870
	// If no key, exit
871
	if ( empty( $key ) ) {
872
		return false;
873 2
	}
874
875
	if ( empty( $value ) ) {
876
		$remove_option = give_delete_option( $key );
877 2
878 2
		return $remove_option;
879
	}
880 2
881
	// First let's grab the current settings
882
	$options = get_option( 'give_settings' );
883
884 1
	// Let's let devs alter that value coming in
885
	$value = apply_filters( 'give_update_option', $value, $key );
886
887 1
	// Next let's try to update the value
888
	$options[ $key ] = $value;
889
	$did_update      = update_option( 'give_settings', $options );
890 1
891 1
	// If it updated, let's update the global variable
892
	if ( $did_update ) {
893
		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...
894 1
		$give_options[ $key ] = $value;
895 1
	}
896 1
897 1
	return $did_update;
898
}
899 1
900
/**
901
 * Remove an option
902
 *
903
 * Removes an give setting value in both the db and the global variable.
904
 *
905
 * @since 1.0
906
 *
907
 * @global $give_options
908
 *
909
 * @param string $key The Key to delete
910
 *
911
 * @return boolean True if updated, false if not.
912
 */
913
function give_delete_option( $key = '' ) {
914
915
	// If no key, exit
916 2
	if ( empty( $key ) ) {
917
		return false;
918
	}
919
920
	// First let's grab the current settings
921 2
	$options = get_option( 'give_settings' );
922
923
	// Next let's try to update the value
924 2
	if ( isset( $options[ $key ] ) ) {
925
926 1
		unset( $options[ $key ] );
927
928 1
	}
929
930 2
	$did_update = update_option( 'give_settings', $options );
931
932
	// If it updated, let's update the global variable
933 2
	if ( $did_update ) {
934 1
		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...
935 1
		$give_options = $options;
936 1
	}
937
938 2
	return $did_update;
939
}
940
941
942
/**
943
 * Get Settings
944
 *
945
 * Retrieves all Give plugin settings
946
 *
947
 * @since 1.0
948
 * @return array Give settings
949
 */
950
function give_get_settings() {
951
952
	$settings = get_option( 'give_settings' );
953
954
	return (array) apply_filters( 'give_get_settings', $settings );
955
956
}
957
958
959
/**
960
 * Give Settings Array Insert.
961
 *
962
 * Allows other Add-ons and plugins to insert Give settings at a desired position.
963
 *
964
 * @since      1.3.5
965
 *
966
 * @param $array
967
 * @param $position |int|string Expects an array key or 'id' of the settings field to appear after
968
 * @param $insert |array a valid array of options to insert
969
 *
970
 * @return array
971
 */
972
function give_settings_array_insert( $array, $position, $insert ) {
973
	if ( is_int( $position ) ) {
974
		array_splice( $array, $position, 0, $insert );
975
	} else {
976
977
		foreach ( $array as $index => $subarray ) {
978
			if ( isset( $subarray['id'] ) && $subarray['id'] == $position ) {
979
				$pos = $index;
980
			}
981
		}
982
983
		if ( ! isset( $pos ) ) {
984
			return $array;
985
		}
986
987
		$array = array_merge(
988
			array_slice( $array, 0, $pos ),
989
			$insert,
990
			array_slice( $array, $pos )
991
		);
992
	}
993
994
	return $array;
995
}
996
997
998
/**
999
 * Gateways Callback
1000
 *
1001
 * Renders gateways fields.
1002
 *
1003
 * @since 1.0
1004
 *
1005
 * @param $field_object
1006
 * @param $escaped_value
1007
 * @param $object_id
1008
 * @param $object_type
1009
 * @param $field_type_object
1010
 *
1011
 * @return void
1012
 */
1013
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...
1014
1015
	$id                = $field_type_object->field->args['id'];
1016
	$field_description = $field_type_object->field->args['desc'];
1017
	$gateways          = give_get_ordered_payment_gateways( give_get_payment_gateways() );
1018
1019
	echo '<ul class="cmb2-checkbox-list cmb2-list">';
1020
1021
	foreach ( $gateways as $key => $option ) :
1022
1023
		if ( is_array( $escaped_value ) && array_key_exists( $key, $escaped_value ) ) {
1024
			$enabled = '1';
1025
		} else {
1026
			$enabled = null;
1027
		}
1028
1029
		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;';
1030
		echo '<label for="' . $id . '[' . $key . ']">' . $option['admin_label'] . '</label></li>';
1031
1032
	endforeach;
1033
1034
	echo '</ul>';
1035
1036
	if ( $field_description ) {
1037
		echo '<p class="cmb2-metabox-description">' . $field_description . '</p>';
1038
	}
1039
1040
}
1041
1042
/**
1043
 * Gateways Callback (drop down)
1044
 *
1045
 * Renders gateways select menu
1046
 *
1047
 * @since 1.0
1048
 *
1049
 * @param $field_object , $escaped_value, $object_id, $object_type, $field_type_object Arguments passed by CMB2
1050
 *
1051
 * @return void
1052
 */
1053
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...
1054
1055
	$id                = $field_type_object->field->args['id'];
1056
	$field_description = $field_type_object->field->args['desc'];
1057
	$gateways          = give_get_enabled_payment_gateways();
1058
1059
	echo '<select class="cmb2_select" name="' . $id . '" id="' . $id . '">';
1060
1061
	//Add a field to the Give Form admin single post view of this field
1062
	if ( $field_type_object->field->object_type === 'post' ) {
1063
		echo '<option value="global">' . esc_html__( 'Global Default', 'give' ) . '</option>';
1064
	}
1065
1066
	foreach ( $gateways as $key => $option ) :
1067
1068
		$selected = isset( $escaped_value ) ? selected( $key, $escaped_value, false ) : '';
1069
1070
1071
		echo '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>';
1072
1073
	endforeach;
1074
1075
	echo '</select>';
1076
1077
	echo '<p class="cmb2-metabox-description">' . $field_description . '</p>';
1078
1079
}
1080
1081
/**
1082
 * Give Title
1083
 *
1084
 * Renders custom section titles output; Really only an  because CMB2's output is a bit funky
1085
 *
1086
 * @since 1.0
1087
 *
1088
 * @param       $field_object , $escaped_value, $object_id, $object_type, $field_type_object
1089
 *
1090
 * @return void
1091
 */
1092
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...
1093
1094
	$id                = $field_type_object->field->args['id'];
1095
	$title             = $field_type_object->field->args['name'];
1096
	$field_description = $field_type_object->field->args['desc'];
1097
1098
	echo '<hr>' . $field_description;
1099
1100
}
1101
1102
/**
1103
 * Give Description
1104
 *
1105
 * Renders custom description text which any plugin can use to output content, html, php, etc.
1106
 *
1107
 * @since      1.3.5
1108
 *
1109
 * @param       $field_object , $escaped_value, $object_id, $object_type, $field_type_object
1110
 *
1111
 * @return void
1112
 */
1113
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...
1114
1115
	$id                = $field_type_object->field->args['id'];
1116
	$title             = $field_type_object->field->args['name'];
1117
	$field_description = $field_type_object->field->args['desc'];
1118
1119
1120
	echo $field_description;
1121
1122
}
1123
1124
/**
1125
 * Gets a number of posts and displays them as options
1126
 *
1127
 * @param  array $query_args Optional. Overrides defaults.
1128
 * @param  bool  $force Force the pages to be loaded even if not on settings
1129
 *
1130
 * @see: https://github.com/WebDevStudios/CMB2/wiki/Adding-your-own-field-types
1131
 * @return array An array of options that matches the CMB2 options array
1132
 */
1133
function give_cmb2_get_post_options( $query_args, $force = false ) {
1134
1135
	$post_options = array( '' => '' ); // Blank option
1136
1137
	if ( ( ! isset( $_GET['page'] ) || 'give-settings' != $_GET['page'] ) && ! $force ) {
1138
		return $post_options;
1139
	}
1140
1141
	$args = wp_parse_args( $query_args, array(
1142
		'post_type'   => 'page',
1143
		'numberposts' => 10,
1144
	) );
1145
1146
	$posts = get_posts( $args );
1147
1148
	if ( $posts ) {
1149
		foreach ( $posts as $post ) {
1150
1151
			$post_options[ $post->ID ] = $post->post_title;
1152
1153
		}
1154
	}
1155
1156
	return $post_options;
1157
}
1158
1159
1160
/**
1161
 * Featured Image Sizes
1162
 *
1163
 * Outputs an array for the "Featured Image Size" option found under Settings > Display Options.
1164
 *
1165
 * @since 1.4
1166
 *
1167
 * @global $_wp_additional_image_sizes
1168
 */
1169
function give_get_featured_image_sizes() {
1170
	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...
1171
	$sizes = array();
1172
1173
	foreach ( get_intermediate_image_sizes() as $_size ) {
1174
1175
		if ( in_array( $_size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
1176
			$sizes[ $_size ] = $_size . ' - ' . get_option( "{$_size}_size_w" ) . 'x' . get_option( "{$_size}_size_h" );
1177
		} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
1178
			$sizes[ $_size ] = $_size . ' - ' . $_wp_additional_image_sizes[ $_size ]['width'] . 'x' . $_wp_additional_image_sizes[ $_size ]['height'];
1179
		}
1180
1181
	}
1182
1183
	return apply_filters( 'give_get_featured_image_sizes', $sizes );
1184
}
1185
1186
1187
/**
1188
 * Give License Key Callback
1189
 *
1190
 * Registers the license field callback for EDD's Software Licensing.
1191
 *
1192
 * @since       1.0
1193
 *
1194
 * @param array $field_object , $escaped_value, $object_id, $object_type, $field_type_object Arguments passed by CMB2
1195
 *
1196
 * @return void
1197
 */
1198
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...
1199
    /* @var CMB2_Types $field_type_object*/
1200
1201
    $id                   = $field_type_object->field->args['id'];
1202
	$field_description    = $field_type_object->field->args['desc'];
1203
	$license              = $field_type_object->field->args['options']['license'];
1204
    $license_key          = $escaped_value;
1205
    $is_license_key       = apply_filters( 'give_is_license_key', ( is_object( $license ) && ! empty( $license ) ) );
1206
    $is_valid_license     = apply_filters( 'give_is_valid_license', ( $is_license_key && property_exists( $license, 'license' ) && 'valid' === $license->license ) );
1207
    $shortname            = $field_type_object->field->args['options']['shortname'];
1208
	$field_classes        = 'regular-text give-license-field';
1209
	$type                 = empty( $escaped_value ) || ! $is_valid_license ? 'text' : 'password';
1210
    $custom_html          = '';
1211
    $value                = $escaped_value;
1212
    $messages             = array();
1213
    $class                = '';
1214
    $account_page_link    = $field_type_object->field->args['options']['account_url'];
1215
    $checkout_page_link   = $field_type_object->field->args['options']['checkout_url'];
1216
    $addon_name           = $field_type_object->field->args['options']['item_name'];
1217
    $license_status       = null;
1218
    $is_in_subscription   = null;
1219
1220
	// By default query on edd api url will return license object which contain status and message property, this can break below functionality.
1221
	// To combat that check if status is set to error or not, if yes then set $is_license_key to false.
1222
	if( $is_license_key && property_exists( $license, 'status' ) && 'error' === $license->status ) {
1223
		$is_license_key = false;
1224
	}
1225
1226
1227
    // Check if current license is part of subscription or not.
1228
    $subscriptions = get_option( 'give_subscriptions' );
1229
1230
    if( $is_license_key && $subscriptions ) {
1231
        foreach ( $subscriptions as $subscription ) {
1232
            if( in_array( $license_key, $subscription['licenses'] ) ) {
1233
                $is_in_subscription = $subscription['id'];
1234
                break;
1235
            }
1236
        }
1237
    }
1238
1239
1240
    if( $is_license_key ) {
1241
        if( $is_in_subscription ) {
1242
            $subscription_expires = strtotime( $subscriptions[$is_in_subscription]['expires'] );
1243
            $subscription_status  = __( 'renew', 'give' );
1244
1245
            if( ( 'active' !== $subscriptions[$is_in_subscription]['status'] ) ){
1246
                $subscription_status = __( 'expire', 'give' );
1247
            }
1248
1249
            if( $subscription_expires < current_time( 'timestamp', 1 ) ) {
1250
                $messages[] = sprintf(
1251
                    __( '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' ),
1252
                    urldecode( $subscriptions[$is_in_subscription]['invoice_url'] ),
1253
                    $subscriptions[$is_in_subscription]['payment_id'],
1254
                    $checkout_page_link . '?edd_license_key=' . $subscriptions[$is_in_subscription]['license_key'] . '&utm_campaign=admin&utm_source=licenses&utm_medium=expired'
1255
                );
1256
                $license_status = 'license-expired';
1257
            } elseif( strtotime( '- 7 days', $subscription_expires ) < current_time( 'timestamp', 1 ) ) {
1258
                $messages[] = sprintf(
1259
                    __( 'Your subscription (<a href="%s" target="_blank">#%d</a>) will %s in %s.', 'give' ),
1260
                    urldecode( $subscriptions[$is_in_subscription]['invoice_url'] ),
1261
                    $subscriptions[$is_in_subscription]['payment_id'],
1262
                    $subscription_status,
1263
                    human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscriptions[$is_in_subscription]['expires'] ) )
1264
                );
1265
                $license_status = 'license-expires-soon';
1266
            } else {
1267
                $messages[] = sprintf(
1268
                    __( 'Your subscription (<a href="%s" target="_blank">#%d</a>) will %s on %s.', 'give' ),
1269
                    urldecode( $subscriptions[$is_in_subscription]['invoice_url'] ),
1270
                    $subscriptions[$is_in_subscription]['payment_id'],
1271
                    $subscription_status,
1272
                    date_i18n( get_option( 'date_format' ), strtotime( $subscriptions[$is_in_subscription]['expires'], current_time( 'timestamp' ) ) )
1273
                );
1274
                $license_status = 'license-expiration-date';
1275
            }
1276
1277
1278
        } elseif ( empty( $license->success ) && property_exists( $license, 'error' ) ) {
1279
1280
            // activate_license 'invalid' on anything other than valid, so if there was an error capture it
1281
            switch(   $license->error ) {
1282
                case 'expired' :
1283
					error_log(print_r( $license->error, true) . "\n", 3, WP_CONTENT_DIR . '/debug_new.log');
1284
                    $class = $license->error;
1285
                    $messages[] = sprintf(
1286
                        __( 'Your license key expired on %s. Please <a href="%s" target="_blank" title="Renew your license key">renew your license key</a>.', 'give' ),
1287
                        date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ),
1288
                        $checkout_page_link . '?edd_license_key=' . $value . '&utm_campaign=admin&utm_source=licenses&utm_medium=expired'
1289
                    );
1290
                    $license_status = 'license-' . $class;
1291
                    break;
1292
1293
                case 'missing' :
1294
                    $class = $license->error;
1295
                    $messages[] = sprintf(
1296
                        __( 'Invalid license. Please <a href="%s" target="_blank" title="Visit account page">visit your account page</a> and verify it.', 'give' ),
1297
                        $account_page_link . '?utm_campaign=admin&utm_source=licenses&utm_medium=missing'
1298
                    );
1299
                    $license_status = 'license-' . $class;
1300
                    break;
1301
1302
                case 'invalid' :
1303
                    $class = $license->error;
1304
                    $messages[] = sprintf(
1305
                        __( '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' ),
1306
                        $addon_name,
1307
                        $account_page_link . '?utm_campaign=admin&utm_source=licenses&utm_medium=invalid'
1308
                    );
1309
                    $license_status = 'license-' . $class;
1310
                    break;
1311
1312
                case 'site_inactive' :
1313
                    $class = $license->error;
1314
                    $messages[] = sprintf(
1315
                        __( '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' ),
1316
                        $addon_name,
1317
                        $account_page_link . '?utm_campaign=admin&utm_source=licenses&utm_medium=invalid'
1318
                    );
1319
                    $license_status = 'license-' . $class;
1320
                    break;
1321
1322
                case 'item_name_mismatch' :
1323
                    $class = $license->error;
1324
                    $messages[] = sprintf( __( 'This license %s does not belong to %s.', 'give' ), $value, $addon_name );
1325
                    $license_status = 'license-' . $class;
1326
                    break;
1327
1328
                case 'no_activations_left':
1329
                    $class = $license->error;
1330
                    $messages[] = sprintf( __( 'Your license key has reached it\'s activation limit. <a href="%s">View possible upgrades</a> now.', 'give' ), $account_page_link );
1331
                    $license_status = 'license-' . $class;
1332
                    break;
1333
            }
1334
        } else {
1335
            switch( $license->license ) {
1336
                case 'valid' :
1337
                default:
1338
                    $class = 'valid';
1339
                    $now        = current_time( 'timestamp' );
1340
                    $expiration = strtotime( $license->expires, current_time( 'timestamp' ) );
1341
1342
                    if( 'lifetime' === $license->expires ) {
1343
                        $messages[] = __( 'License key never expires.', 'give' );
1344
                        $license_status = 'license-lifetime-notice';
1345
                    } elseif( $expiration > $now && $expiration - $now < ( DAY_IN_SECONDS * 30 ) ) {
1346
                        $messages[] = sprintf(
1347
                            __( 'Your license key expires soon! It expires on %s. <a href="%s" target="_blank" title="Renew license">Renew your license key</a>.', 'give' ),
1348
                            date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ),
1349
                            $checkout_page_link . '?edd_license_key=' . $value . '&utm_campaign=admin&utm_source=licenses&utm_medium=renew'
1350
                        );
1351
                        $license_status = 'license-expires-soon';
1352
                    } else {
1353
                        $messages[] = sprintf(
1354
                            __( 'Your license key expires on %s.', 'give' ),
1355
                            date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) )
1356
                        );
1357
                        $license_status = 'license-expiration-date';
1358
                    }
1359
                    break;
1360
            }
1361
        }
1362
    } else{
1363
		$class = 'empty';
1364
		$messages[] = sprintf(
1365
			__( 'To receive updates, please enter your valid %s license key.', 'give' ),
1366
			$addon_name
1367
		);
1368
		$license_status = null;
1369
	}
1370
1371
1372
    // Add class for input field if license is active.
1373
	if ( $is_valid_license ) {
1374
		$field_classes .= ' give-license-active';
1375
	}
1376
1377
	// Get input filed html.
1378
	$input_field_html = $field_type_object->input( array(
1379
		'class' => $field_classes,
1380
		'type'  => $type
1381
	) );
1382
1383
	// If license is active so show deactivate button
1384
	if ( $is_valid_license ) {
1385
        // Get input filed html.
1386
        $input_field_html = $field_type_object->input( array(
1387
            'class' => $field_classes,
1388
            'type'  => $type,
1389
            'readonly' => 'readonly',
1390
        ) );
1391
1392
		$custom_html = '<input type="submit" class="button-secondary give-license-deactivate" name="' . $id . '_deactivate" value="' . esc_attr__( 'Deactivate License', 'give' ) . '"/>';
1393
	}
1394
1395
	// Field description.
1396
	$custom_html .= '<label for="give_settings[' . $id . ']"> ' . $field_description . '</label>';
1397
1398
    // If no messages found then inform user that to get updated in future register yourself.
1399
    if ( empty( $messages ) ) {
1400
        $messages[] = apply_filters( "{$shortname}_default_addon_notice", esc_html__( 'To receive updates, please enter your valid Software Licensing license key.', 'give' ) );
1401
    }
1402
1403
    foreach( $messages as $message ) {
1404
        $custom_html .= '<div class="give-license-notice give-' . $license_status . '">';
1405
        $custom_html .= '<p>' . $message . '</p>';
1406
        $custom_html .= '</div>';
1407
    }
1408
1409
1410
1411
    // Field html.
1412
    $custom_html = apply_filters('give_license_key_field_html', $input_field_html.$custom_html, $field_type_object );
1413
1414
    // Nonce.
1415
	wp_nonce_field( $id . '-nonce', $id . '-nonce' );
1416
1417
    // Print field html.
1418
    echo '<div>' . $custom_html . '</div>';
1419
}
1420
1421
1422
/**
1423
 * Display the API Keys
1424
 *
1425
 * @since       1.0
1426
 * @return      void
1427
 */
1428
function give_api_callback() {
1429
1430
	if ( ! current_user_can( 'manage_give_settings' ) ) {
1431
		return;
1432
	}
1433
1434
	/**
1435
	 * Fires before displaying API keys.
1436
	 *
1437
	 * @since 1.0
1438
	 */
1439
	do_action( 'give_tools_api_keys_before' );
1440
1441
	require_once GIVE_PLUGIN_DIR . 'includes/admin/class-api-keys-table.php';
1442
1443
	$api_keys_table = new Give_API_Keys_Table();
1444
	$api_keys_table->prepare_items();
1445
	$api_keys_table->display();
1446
	?>
1447
	<span class="cmb2-metabox-description api-description">
1448
		<?php echo sprintf(
1449
		/* translators: 1: https://givewp.com/documentation/give-api-reference/ 2: https://givewp.com/addons/zapier/ */
1450
			__( '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' ),
1451
			esc_url( 'https://givewp.com/documentation/give-api-reference/' ),
1452
			esc_url( 'https://givewp.com/addons/zapier/' )
1453
		); ?>
1454
	</span>
1455
	<?php
1456
1457
	/**
1458
	 * Fires after displaying API keys.
1459
	 *
1460
	 * @since 1.0
1461
	 */
1462
	do_action( 'give_tools_api_keys_after' );
1463
}
1464
1465
add_action( 'give_settings_tab_api_keys', 'give_api_callback' );
1466
1467
/**
1468
 * Hook Callback
1469
 *
1470
 * Adds a do_action() hook in place of the field.
1471
 *
1472
 * @since 1.0
1473
 *
1474
 * @param array $args Arguments passed by the setting
1475
 *
1476
 * @return void
1477
 */
1478
function give_hook_callback( $args ) {
1479
1480
	$id = $args['id'];
1481
1482
	/**
1483
	 * Fires in give field.
1484
	 *
1485
	 * @since 1.0
1486
	 */
1487
	do_action( "give_{$id}" );
1488
1489
}
1490
1491
/**
1492
 * Get the CMB2 bootstrap!
1493
 *
1494
 * Checks to see if CMB2 plugin is installed first the uses included CMB2.
1495
 * We can still use it even it it's not active.
1496
 * This prevents fatal error conflicts with other themes and users of the CMB2 WP.org plugin.
1497
 */
1498
1499
if ( file_exists( WP_PLUGIN_DIR . '/cmb2/init.php' ) && ! defined( 'CMB2_LOADED' ) ) {
1500
	require_once WP_PLUGIN_DIR . '/cmb2/init.php';
1501
} elseif ( file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/cmb2/init.php' ) && ! defined( 'CMB2_LOADED' ) ) {
1502
	require_once GIVE_PLUGIN_DIR . '/includes/libraries/cmb2/init.php';
1503
} elseif ( file_exists( GIVE_PLUGIN_DIR . '/includes/libraries/CMB2/init.php' ) && ! defined( 'CMB2_LOADED' ) ) {
1504
	require_once GIVE_PLUGIN_DIR . '/includes/libraries/CMB2/init.php';
1505
}