Give_MetaBox_Form_Data::save()   F
last analyzed

Complexity

Conditions 27
Paths 7

Size

Total Lines 139

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 27
nc 7
nop 2
dl 0
loc 139
rs 3.3333
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
2
/**
3
 * Donation Form Data
4
 *
5
 * Displays the form data box, tabbed, with several panels.
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Give_MetaBox_Form_Data
9
 * @copyright   Copyright (c) 2016, GiveWP
10
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
11
 * @since       1.8
12
 */
13
14
/**
15
 * Give_Meta_Box_Form_Data Class.
16
 */
17
class Give_MetaBox_Form_Data {
18
19
	/**
20
	 * Meta box settings.
21
	 *
22
	 * @since 1.8
23
	 * @var   array
24
	 */
25
	private $settings = array();
26
27
	/**
28
	 * Metabox ID.
29
	 *
30
	 * @since 1.8
31
	 * @var   string
32
	 */
33
	private $metabox_id;
34
35
	/**
36
	 * Metabox Label.
37
	 *
38
	 * @since 1.8
39
	 * @var   string
40
	 */
41
	private $metabox_label;
42
43
44
	/**
45
	 * Give_MetaBox_Form_Data constructor.
46
	 */
47
	function __construct() {
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...
48
		$this->metabox_id    = 'give-metabox-form-data';
49
		$this->metabox_label = __( 'Donation Form Options', 'give' );
50
51
		// Setup.
52
		add_action( 'admin_init', array( $this, 'setup' ) );
53
54
		// Add metabox.
55
		add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ), 10 );
56
57
		// Save form meta.
58
		add_action( 'save_post_give_forms', array( $this, 'save' ), 10, 2 );
59
60
		// cmb2 old setting loaders.
61
		// add_filter( 'give_metabox_form_data_settings', array( $this, 'cmb2_metabox_settings' ) );
62
		// Add offline donations options.
63
		add_filter( 'give_metabox_form_data_settings', array( $this, 'add_offline_donations_setting_tab' ), 0, 1 );
64
65
		// Maintain active tab query parameter after save.
66
		add_filter( 'redirect_post_location', array( $this, 'maintain_active_tab' ), 10, 2 );
67
	}
68
69
	/**
70
	 * Setup metabox related data.
71
	 *
72
	 * @since 1.8
73
	 *
74
	 * @return void
75
	 */
76
	function setup() {
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...
77
		$this->settings = $this->get_settings();
78
	}
79
80
81
	/**
82
	 * Get metabox settings
83
	 *
84
	 * @since 1.8
85
	 *
86
	 * @return array
87
	 */
88
	function get_settings() {
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...
89
		$post_id           = give_get_admin_post_id();
90
		$price_placeholder = give_format_decimal( '1.00', false, false );
91
92
		// Start with an underscore to hide fields from custom fields list
93
		$prefix = '_give_';
94
95
		$settings = array(
96
			/**
97
			 * Repeatable Field Groups
98
			 */
99
			'form_field_options'    => apply_filters( 'give_forms_field_options', array(
100
				'id'        => 'form_field_options',
101
				'title'     => __( 'Donation Options', 'give' ),
102
				'icon-html' => '<span class="give-icon give-icon-heart"></span>',
103
				'fields'    => apply_filters( 'give_forms_donation_form_metabox_fields', array(
104
					// Donation Option.
105
					array(
106
						'name'        => __( 'Donation Option', 'give' ),
107
						'description' => __( 'Do you want this form to have one set donation price or multiple levels (for example, $10, $20, $50)?', 'give' ),
108
						'id'          => $prefix . 'price_option',
109
						'type'        => 'radio_inline',
110
						'default'     => 'multi',
111
						'options'     => apply_filters( 'give_forms_price_options', array(
112
							'multi' => __( 'Multi-level Donation', 'give' ),
113
							'set'   => __( 'Set Donation', 'give' ),
114
						) ),
115
					),
116
					array(
117
						'name'        => __( 'Set Donation', 'give' ),
118
						'description' => __( 'This is the set donation amount for this form. If you have a "Custom Amount Minimum" set, make sure it is less than this amount.', 'give' ),
119
						'id'          => $prefix . 'set_price',
120
						'type'        => 'text_small',
121
						'data_type'   => 'price',
122
						'attributes'  => array(
123
							'placeholder' => $price_placeholder,
124
							'class'       => 'give-money-field',
125
						),
126
					),
127
					// Display Style.
128
					array(
129
						'name'          => __( 'Display Style', 'give' ),
130
						'description'   => __( 'Set how the donations levels will display on the form.', 'give' ),
131
						'id'            => $prefix . 'display_style',
132
						'type'          => 'radio_inline',
133
						'default'       => 'buttons',
134
						'options'       => array(
135
							'buttons'  => __( 'Buttons', 'give' ),
136
							'radios'   => __( 'Radios', 'give' ),
137
							'dropdown' => __( 'Dropdown', 'give' ),
138
						),
139
						'wrapper_class' => 'give-hidden',
140
					),
141
					// Custom Amount.
142
					array(
143
						'name'        => __( 'Custom Amount', 'give' ),
144
						'description' => __( 'Do you want the user to be able to input their own donation amount?', 'give' ),
145
						'id'          => $prefix . 'custom_amount',
146
						'type'        => 'radio_inline',
147
						'default'     => 'disabled',
148
						'options'     => array(
149
							'enabled'  => __( 'Enabled', 'give' ),
150
							'disabled' => __( 'Disabled', 'give' ),
151
						),
152
					),
153
					array(
154
						'name'          => __( 'Donation Limit', 'give' ),
155
						'description'   => __( 'Set the minimum and maximum amount for all gateways.', 'give' ),
156
						'id'            => $prefix . 'custom_amount_range',
157
						'type'          => 'donation_limit',
158
						'wrapper_class' => 'give-hidden',
159
						'data_type'     => 'price',
160
						'attributes'    => array(
161
							'placeholder' => $price_placeholder,
162
							'class'       => 'give-money-field',
163
						),
164
						'options'       => array(
165
							'display_label' => __( 'Donation Limits: ', 'give' ),
166
							'minimum'       => give_format_decimal( '1.00', false, false ),
167
							'maximum'       => give_format_decimal( '999999.99', false, false ),
168
						),
169
					),
170
					array(
171
						'name'          => __( 'Custom Amount Text', 'give' ),
172
						'description'   => __( 'This text appears as a label below the custom amount field for set donation forms. For multi-level forms the text will appear as it\'s own level (ie button, radio, or select option).', 'give' ),
173
						'id'            => $prefix . 'custom_amount_text',
174
						'type'          => 'text_medium',
175
						'attributes'    => array(
176
							'rows'        => 3,
177
							'placeholder' => __( 'Give a Custom Amount', 'give' ),
178
						),
179
						'wrapper_class' => 'give-hidden',
180
					),
181
					// Donation Levels.
182
					array(
183
						'id'            => $prefix . 'donation_levels',
184
						'type'          => 'group',
185
						'options'       => array(
186
							'add_button'    => __( 'Add Level', 'give' ),
187
							'header_title'  => __( 'Donation Level', 'give' ),
188
							'remove_button' => '<span class="dashicons dashicons-no"></span>',
189
						),
190
						'wrapper_class' => 'give-hidden',
191
						// Fields array works the same, except id's only need to be unique for this group.
192
						// Prefix is not needed.
193
						'fields'        => apply_filters( 'give_donation_levels_table_row', array(
194
							array(
195
								'name' => __( 'ID', 'give' ),
196
								'id'   => $prefix . 'id',
197
								'type' => 'levels_id',
198
							),
199
							array(
200
								'name'       => __( 'Amount', 'give' ),
201
								'id'         => $prefix . 'amount',
202
								'type'       => 'text_small',
203
								'data_type'  => 'price',
204
								'attributes' => array(
205
									'placeholder' => $price_placeholder,
206
									'class'       => 'give-money-field',
207
								),
208
							),
209
							array(
210
								'name'       => __( 'Text', 'give' ),
211
								'id'         => $prefix . 'text',
212
								'type'       => 'text',
213
								'attributes' => array(
214
									'placeholder' => __( 'Donation Level', 'give' ),
215
									'class'       => 'give-multilevel-text-field',
216
								),
217
							),
218
							array(
219
								'name' => __( 'Default', 'give' ),
220
								'id'   => $prefix . 'default',
221
								'type' => 'give_default_radio_inline',
222
							),
223
						) ),
224
					),
225
					array(
226
						'name'  => 'donation_options_docs',
227
						'type'  => 'docs_link',
228
						'url'   => 'http://docs.givewp.com/form-donation-options',
229
						'title' => __( 'Donation Options', 'give' ),
230
					),
231
				),
232
					$post_id
233
				),
234
			) ),
235
236
			/**
237
			 * Display Options
238
			 */
239
			'form_display_options'  => apply_filters( 'give_form_display_options', array(
240
					'id'        => 'form_display_options',
241
					'title'     => __( 'Form Display', 'give' ),
242
					'icon-html' => '<span class="give-icon give-icon-display"></span>',
243
					'fields'    => apply_filters( 'give_forms_display_options_metabox_fields', array(
244
						array(
245
							'name'    => __( 'Display Options', 'give' ),
246
							'desc'    => sprintf( __( 'How would you like to display donation information for this form?', 'give' ), '#' ),
247
							'id'      => $prefix . 'payment_display',
248
							'type'    => 'radio_inline',
249
							'options' => array(
250
								'onpage' => __( 'All Fields', 'give' ),
251
								'modal'  => __( 'Modal', 'give' ),
252
								'reveal' => __( 'Reveal', 'give' ),
253
								'button' => __( 'Button', 'give' ),
254
							),
255
							'default' => 'onpage',
256
						),
257
						array(
258
							'id'            => $prefix . 'reveal_label',
259
							'name'          => __( 'Continue Button', 'give' ),
260
							'desc'          => __( 'The button label for displaying the additional payment fields.', 'give' ),
261
							'type'          => 'text_small',
262
							'attributes'    => array(
263
								'placeholder' => __( 'Donate Now', 'give' ),
264
							),
265
							'wrapper_class' => 'give-hidden',
266
						),
267
						array(
268
							'id'         => $prefix . 'checkout_label',
269
							'name'       => __( 'Submit Button', 'give' ),
270
							'desc'       => __( 'The button label for completing a donation.', 'give' ),
271
							'type'       => 'text_small',
272
							'attributes' => array(
273
								'placeholder' => __( 'Donate Now', 'give' ),
274
							),
275
						),
276
						array(
277
							'name' => __( 'Default Gateway', 'give' ),
278
							'desc' => __( 'By default, the gateway for this form will inherit the global default gateway (set under Give > Settings > Payment Gateways). This option allows you to customize the default gateway for this form only.', 'give' ),
279
							'id'   => $prefix . 'default_gateway',
280
							'type' => 'default_gateway',
281
						),
282
						array(
283
							'name'    => __( 'Name Title Prefix', 'give' ),
284
							'desc'    => __( 'Do you want to add a name title prefix dropdown field before the donor\'s first name field? This will display a dropdown with options such as Mrs, Miss, Ms, Sir, and Dr for donor to choose from.', 'give' ),
285
							'id'      => $prefix . 'name_title_prefix',
286
							'type'    => 'radio_inline',
287
							'options' => array(
288
								'global' => __( 'Global Option', 'give' ),
289
								'required' => __( 'Required', 'give' ),
290
								'optional' => __( 'Optional', 'give' ),
291
								'disabled' => __( 'Disabled', 'give' ),
292
							),
293
							'default' => 'global',
294
						),
295
						array(
296
							'name'          => __( 'Title Prefixes', 'give' ),
297
							'desc'          => __( 'Add or remove salutations from the dropdown using the field above.', 'give' ),
298
							'id'            => $prefix . 'title_prefixes',
299
							'type'          => 'chosen',
300
							'data_type'     => 'multiselect',
301
							'style'         => 'width: 100%',
302
							'wrapper_class' => 'give-hidden give-title-prefixes-wrap',
303
							'options'       => give_get_default_title_prefixes(),
304
						),
305
						array(
306
							'name'    => __( 'Company Donations', 'give' ),
307
							'desc'    => __( 'Do you want a Company field to appear after First Name and Last Name?', 'give' ),
308
							'id'      => $prefix . 'company_field',
309
							'type'    => 'radio_inline',
310
							'default' => 'global',
311
							'options' => array(
312
								'global'   => __( 'Global Option', 'give' ),
313
								'required' => __( 'Required', 'give' ),
314
								'optional' => __( 'Optional', 'give' ),
315
								'disabled' => __( 'Disabled', 'give' ),
316
317
							),
318
						),
319
						array(
320
							'name'    => __( 'Anonymous Donations', 'give' ),
321
							'desc'    => __( 'Do you want to provide donors the ability mark themselves anonymous while giving. This will prevent their information from appearing publicly on your website but you will still receive their information for your records in the admin panel.', 'give' ),
322
							'id'      => "{$prefix}anonymous_donation",
323
							'type'    => 'radio_inline',
324
							'default' => 'global',
325
							'options' => array(
326
								'global'   => __( 'Global Option', 'give' ),
327
								'enabled'  => __( 'Enabled', 'give' ),
328
								'disabled' => __( 'Disabled', 'give' ),
329
							),
330
						),
331
						array(
332
							'name'    => __( 'Donor Comments', 'give' ),
333
							'desc'    => __( 'Do you want to provide donors the ability to add a comment to their donation? The comment will display publicly on the donor wall if they do not select to give anonymously.', 'give' ),
334
							'id'      => "{$prefix}donor_comment",
335
							'type'    => 'radio_inline',
336
							'default' => 'global',
337
							'options' => array(
338
								'global'   => __( 'Global Option', 'give' ),
339
								'enabled'  => __( 'Enabled', 'give' ),
340
								'disabled' => __( 'Disabled', 'give' ),
341
							),
342
						),
343
						array(
344
							'name'    => __( 'Guest Donations', 'give' ),
345
							'desc'    => __( 'Do you want to allow non-logged-in users to make donations?', 'give' ),
346
							'id'      => $prefix . 'logged_in_only',
347
							'type'    => 'radio_inline',
348
							'default' => 'enabled',
349
							'options' => array(
350
								'enabled'  => __( 'Enabled', 'give' ),
351
								'disabled' => __( 'Disabled', 'give' ),
352
							),
353
						),
354
						array(
355
							'name'    => __( 'Registration', 'give' ),
356
							'desc'    => __( 'Display the registration and login forms in the payment section for non-logged-in users.', 'give' ),
357
							'id'      => $prefix . 'show_register_form',
358
							'type'    => 'radio',
359
							'options' => array(
360
								'none'         => __( 'None', 'give' ),
361
								'registration' => __( 'Registration', 'give' ),
362
								'login'        => __( 'Login', 'give' ),
363
								'both'         => __( 'Registration + Login', 'give' ),
364
							),
365
							'default' => 'none',
366
						),
367
						array(
368
							'name'    => __( 'Floating Labels', 'give' ),
369
							/* translators: %s: forms http://docs.givewp.com/form-floating-labels */
370
							'desc'    => sprintf( __( 'Select the <a href="%s" target="_blank">floating labels</a> setting for this Give form. Be aware that if you have the "Disable CSS" option enabled, you will need to style the floating labels yourself.', 'give' ), esc_url( 'http://docs.givewp.com/form-floating-labels' ) ),
371
							'id'      => $prefix . 'form_floating_labels',
372
							'type'    => 'radio_inline',
373
							'options' => array(
374
								'global'   => __( 'Global Option', 'give' ),
375
								'enabled'  => __( 'Enabled', 'give' ),
376
								'disabled' => __( 'Disabled', 'give' ),
377
							),
378
							'default' => 'global',
379
						),
380
						array(
381
							'name'  => 'form_display_docs',
382
							'type'  => 'docs_link',
383
							'url'   => 'http://docs.givewp.com/form-display-options',
384
							'title' => __( 'Form Display', 'give' ),
385
						),
386
					),
387
						$post_id
388
					),
389
				)
390
			),
391
392
			/**
393
			 * Donation Goals
394
			 */
395
			'donation_goal_options' => apply_filters( 'give_donation_goal_options', array(
396
				'id'        => 'donation_goal_options',
397
				'title'     => __( 'Donation Goal', 'give' ),
398
				'icon-html' => '<span class="give-icon give-icon-target"></span>',
399
				'fields'    => apply_filters( 'give_forms_donation_goal_metabox_fields', array(
400
					// Goals
401
					array(
402
						'name'        => __( 'Donation Goal', 'give' ),
403
						'description' => __( 'Do you want to set a donation goal for this form?', 'give' ),
404
						'id'          => $prefix . 'goal_option',
405
						'type'        => 'radio_inline',
406
						'default'     => 'disabled',
407
						'options'     => array(
408
							'enabled'  => __( 'Enabled', 'give' ),
409
							'disabled' => __( 'Disabled', 'give' ),
410
						),
411
					),
412
413
					array(
414
						'name'        => __( 'Goal Format', 'give' ),
415
						'description' => __( 'Do you want to display the total amount raised based on your monetary goal or a percentage? For instance, "$500 of $1,000 raised" or "50% funded" or "1 of 5 donations". You can also display a donor-based goal, such as "100 of 1,000 donors have given".', 'give' ),
416
						'id'          => $prefix . 'goal_format',
417
						'type'        => 'donation_form_goal',
418
						'default'     => 'amount',
419
						'options'     => array(
420
							'amount'     => __( 'Amount Raised', 'give' ),
421
							'percentage' => __( 'Percentage Raised', 'give' ),
422
							'donation'   => __( 'Number of Donations', 'give' ),
423
							'donors'     => __( 'Number of Donors', 'give' ),
424
						),
425
					),
426
427
					array(
428
						'name'          => __( 'Goal Amount', 'give' ),
429
						'description'   => __( 'This is the monetary goal amount you want to reach for this form.', 'give' ),
430
						'id'            => $prefix . 'set_goal',
431
						'type'          => 'text_small',
432
						'data_type'     => 'price',
433
						'attributes'    => array(
434
							'placeholder' => $price_placeholder,
435
							'class'       => 'give-money-field',
436
						),
437
						'wrapper_class' => 'give-hidden',
438
					),
439
					array(
440
						'id'         => $prefix . 'number_of_donation_goal',
441
						'name'       => __( 'Donation Goal', 'give' ),
442
						'desc'       => __( 'Set the total number of donations as a goal.', 'give' ),
443
						'type'       => 'number',
444
						'default'    => 1,
445
						'attributes' => array(
446
							'placeholder' => 1,
447
						),
448
					),
449
					array(
450
						'id'         => $prefix . 'number_of_donor_goal',
451
						'name'       => __( 'Donor Goal', 'give' ),
452
						'desc'       => __( 'Set the total number of donors as a goal.', 'give' ),
453
						'type'       => 'number',
454
						'default'    => 1,
455
						'attributes' => array(
456
							'placeholder' => 1,
457
						),
458
					),
459
					array(
460
						'name'          => __( 'Progress Bar Color', 'give' ),
461
						'desc'          => __( 'Customize the color of the goal progress bar.', 'give' ),
462
						'id'            => $prefix . 'goal_color',
463
						'type'          => 'colorpicker',
464
						'default'       => '#2bc253',
465
						'wrapper_class' => 'give-hidden',
466
					),
467
468
					array(
469
						'name'          => __( 'Close Form', 'give' ),
470
						'desc'          => __( 'Do you want to close the donation forms and stop accepting donations once this goal has been met?', 'give' ),
471
						'id'            => $prefix . 'close_form_when_goal_achieved',
472
						'type'          => 'radio_inline',
473
						'default'       => 'disabled',
474
						'options'       => array(
475
							'enabled'  => __( 'Enabled', 'give' ),
476
							'disabled' => __( 'Disabled', 'give' ),
477
						),
478
						'wrapper_class' => 'give-hidden',
479
					),
480
					array(
481
						'name'          => __( 'Goal Achieved Message', 'give' ),
482
						'desc'          => __( 'Do you want to display a custom message when the goal is closed?', 'give' ),
483
						'id'            => $prefix . 'form_goal_achieved_message',
484
						'type'          => 'wysiwyg',
485
						'default'       => __( 'Thank you to all our donors, we have met our fundraising goal.', 'give' ),
486
						'wrapper_class' => 'give-hidden',
487
					),
488
					array(
489
						'name'  => 'donation_goal_docs',
490
						'type'  => 'docs_link',
491
						'url'   => 'http://docs.givewp.com/form-donation-goal',
492
						'title' => __( 'Donation Goal', 'give' ),
493
					),
494
				),
495
					$post_id
496
				),
497
			) ),
498
499
			/**
500
			 * Content Field
501
			 */
502
			'form_content_options'  => apply_filters( 'give_forms_content_options', array(
503
				'id'        => 'form_content_options',
504
				'title'     => __( 'Form Content', 'give' ),
505
				'icon-html' => '<span class="give-icon give-icon-edit"></span>',
506
				'fields'    => apply_filters( 'give_forms_content_options_metabox_fields', array(
507
508
					// Donation content.
509
					array(
510
						'name'        => __( 'Display Content', 'give' ),
511
						'description' => __( 'Do you want to add custom content to this form?', 'give' ),
512
						'id'          => $prefix . 'display_content',
513
						'type'        => 'radio_inline',
514
						'options'     => array(
515
							'enabled'  => __( 'Enabled', 'give' ),
516
							'disabled' => __( 'Disabled', 'give' ),
517
						),
518
						'default'     => 'disabled',
519
					),
520
521
					// Content placement.
522
					array(
523
						'name'          => __( 'Content Placement', 'give' ),
524
						'description'   => __( 'This option controls where the content appears within the donation form.', 'give' ),
525
						'id'            => $prefix . 'content_placement',
526
						'type'          => 'radio_inline',
527
						'options'       => apply_filters( 'give_forms_content_options_select', array(
528
								'give_pre_form'  => __( 'Above fields', 'give' ),
529
								'give_post_form' => __( 'Below fields', 'give' ),
530
							)
531
						),
532
						'default'       => 'give_pre_form',
533
						'wrapper_class' => 'give-hidden',
534
					),
535
					array(
536
						'name'          => __( 'Content', 'give' ),
537
						'description'   => __( 'This content will display on the single give form page.', 'give' ),
538
						'id'            => $prefix . 'form_content',
539
						'type'          => 'wysiwyg',
540
						'wrapper_class' => 'give-hidden',
541
					),
542
					array(
543
						'name'  => 'form_content_docs',
544
						'type'  => 'docs_link',
545
						'url'   => 'http://docs.givewp.com/form-content',
546
						'title' => __( 'Form Content', 'give' ),
547
					),
548
				),
549
					$post_id
550
				),
551
			) ),
552
553
			/**
554
			 * Terms & Conditions
555
			 */
556
			'form_terms_options'    => apply_filters( 'give_forms_terms_options', array(
557
				'id'        => 'form_terms_options',
558
				'title'     => __( 'Terms & Conditions', 'give' ),
559
				'icon-html' => '<span class="give-icon give-icon-checklist"></span>',
560
				'fields'    => apply_filters( 'give_forms_terms_options_metabox_fields', array(
561
					// Donation Option
562
					array(
563
						'name'        => __( 'Terms and Conditions', 'give' ),
564
						'description' => __( 'Do you want to require the donor to accept terms prior to being able to complete their donation?', 'give' ),
565
						'id'          => $prefix . 'terms_option',
566
						'type'        => 'radio_inline',
567
						'options'     => apply_filters( 'give_forms_content_options_select', array(
568
								'global'   => __( 'Global Option', 'give' ),
569
								'enabled'  => __( 'Customize', 'give' ),
570
								'disabled' => __( 'Disable', 'give' ),
571
							)
572
						),
573
						'default'     => 'global',
574
					),
575
					array(
576
						'id'            => $prefix . 'agree_label',
577
						'name'          => __( 'Agreement Label', 'give' ),
578
						'desc'          => __( '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.', 'give' ),
579
						'type'          => 'textarea',
580
						'attributes'    => array(
581
							'placeholder' => __( 'Agree to Terms?', 'give' ),
582
							'rows'        => 1
583
						),
584
						'wrapper_class' => 'give-hidden',
585
					),
586
					array(
587
						'id'            => $prefix . 'agree_text',
588
						'name'          => __( 'Agreement Text', 'give' ),
589
						'desc'          => __( 'This is the actual text which the user will have to agree to in order to make a donation.', 'give' ),
590
						'default'       => give_get_option( 'agreement_text' ),
591
						'type'          => 'wysiwyg',
592
						'wrapper_class' => 'give-hidden',
593
					),
594
					array(
595
						'name'  => 'terms_docs',
596
						'type'  => 'docs_link',
597
						'url'   => 'http://docs.givewp.com/form-terms',
598
						'title' => __( 'Terms and Conditions', 'give' ),
599
					),
600
				),
601
					$post_id
602
				),
603
			) ),
604
		);
605
606
		/**
607
		 * Filter the metabox tabbed panel settings.
608
		 */
609
		$settings = apply_filters( 'give_metabox_form_data_settings', $settings, $post_id );
610
611
		// Output.
612
		return $settings;
613
	}
614
615
	/**
616
	 * Add metabox.
617
	 *
618
	 * @since 1.8
619
	 *
620
	 * @return void
621
	 */
622
	public function add_meta_box() {
623
		add_meta_box(
624
			$this->get_metabox_ID(),
625
			$this->get_metabox_label(),
626
			array( $this, 'output' ),
627
			array( 'give_forms' ),
628
			'normal',
629
			'high'
630
		);
631
632
		// Show Goal Metabox only if goal is enabled.
633
		if ( give_is_setting_enabled( give_get_meta( give_get_admin_post_id(), '_give_goal_option', true ) ) ) {
634
			add_meta_box(
635
				'give-form-goal-stats',
636
				__( 'Goal Statistics', 'give' ),
637
				array( $this, 'output_goal' ),
638
				array( 'give_forms' ),
639
				'side',
640
				'low'
641
			);
642
		}
643
644
	}
645
646
647
	/**
648
	 * Enqueue scripts.
649
	 *
650
	 * @since 1.8
651
	 *
652
	 * @return void
653
	 */
654
	function enqueue_script() {
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...
655
		global $post;
656
657
		if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
658
659
		}
660
	}
661
662
	/**
663
	 * Get metabox id.
664
	 *
665
	 * @since 1.8
666
	 *
667
	 * @return string
668
	 */
669
	function get_metabox_ID() {
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...
670
		return $this->metabox_id;
671
	}
672
673
	/**
674
	 * Get metabox label.
675
	 *
676
	 * @since 1.8
677
	 *
678
	 * @return string
679
	 */
680
	function get_metabox_label() {
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...
681
		return $this->metabox_label;
682
	}
683
684
685
	/**
686
	 * Get metabox tabs.
687
	 *
688
	 * @since 1.8
689
	 *
690
	 * @return array
691
	 */
692
	public function get_tabs() {
693
		$tabs = array();
694
695
		if ( ! empty( $this->settings ) ) {
696
			foreach ( $this->settings as $setting ) {
697
				if ( ! isset( $setting['id'] ) || ! isset( $setting['title'] ) ) {
698
					continue;
699
				}
700
				$tab = array(
701
					'id'        => $setting['id'],
702
					'label'     => $setting['title'],
703
					'icon-html' => ( ! empty( $setting['icon-html'] ) ? $setting['icon-html'] : '' ),
704
				);
705
706
				if ( $this->has_sub_tab( $setting ) ) {
707
					if ( empty( $setting['sub-fields'] ) ) {
708
						$tab = array();
709
					} else {
710
						foreach ( $setting['sub-fields'] as $sub_fields ) {
711
							$tab['sub-fields'][] = array(
712
								'id'        => $sub_fields['id'],
713
								'label'     => $sub_fields['title'],
714
								'icon-html' => ( ! empty( $sub_fields['icon-html'] ) ? $sub_fields['icon-html'] : '' ),
715
							);
716
						}
717
					}
718
				}
719
720
				if ( ! empty( $tab ) ) {
721
					$tabs[] = $tab;
722
				}
723
			}
724
		}
725
726
		return $tabs;
727
	}
728
729
	/**
730
	 * Output metabox settings.
731
	 *
732
	 * @since 1.8
733
	 *
734
	 * @return void
735
	 */
736
	public function output() {
737
		// Bailout.
738
		if ( $form_data_tabs = $this->get_tabs() ) :
739
			$active_tab = ! empty( $_GET['give_tab'] ) ? give_clean( $_GET['give_tab'] ) : 'form_field_options';
740
			wp_nonce_field( 'give_save_form_meta', 'give_form_meta_nonce' );
741
			?>
742
			<input id="give_form_active_tab" type="hidden" name="give_form_active_tab">
743
			<div class="give-metabox-panel-wrap">
744
				<ul class="give-form-data-tabs give-metabox-tabs">
745
					<?php foreach ( $form_data_tabs as $index => $form_data_tab ) : ?>
746
						<?php
747
						// Determine if current tab is active.
748
						$is_active = $active_tab === $form_data_tab['id'] ? true : false;
749
						?>
750
						<li class="<?php echo "{$form_data_tab['id']}_tab" . ( $is_active ? ' active' : '' ) . ( $this->has_sub_tab( $form_data_tab ) ? ' has-sub-fields' : '' ); ?>">
751
							<a href="#<?php echo $form_data_tab['id']; ?>"
752
							   data-tab-id="<?php echo $form_data_tab['id']; ?>">
753
								<?php if ( ! empty( $form_data_tab['icon-html'] ) ) : ?>
754
									<?php echo $form_data_tab['icon-html']; ?>
755
								<?php else : ?>
756
									<span class="give-icon give-icon-default"></span>
757
								<?php endif; ?>
758
								<span class="give-label"><?php echo $form_data_tab['label']; ?></span>
759
							</a>
760
							<?php if ( $this->has_sub_tab( $form_data_tab ) ) : ?>
761
								<ul class="give-metabox-sub-tabs give-hidden">
762
									<?php foreach ( $form_data_tab['sub-fields'] as $sub_tab ) : ?>
763
										<li class="<?php echo "{$sub_tab['id']}_tab"; ?>">
764
											<a href="#<?php echo $sub_tab['id']; ?>"
765
											   data-tab-id="<?php echo $sub_tab['id']; ?>">
766
												<?php if ( ! empty( $sub_tab['icon-html'] ) ) : ?>
767
													<?php echo $sub_tab['icon-html']; ?>
768
												<?php else : ?>
769
													<span class="give-icon give-icon-default"></span>
770
												<?php endif; ?>
771
												<span class="give-label"><?php echo $sub_tab['label']; ?></span>
772
											</a>
773
										</li>
774
									<?php endforeach; ?>
775
								</ul>
776
							<?php endif; ?>
777
						</li>
778
					<?php endforeach; ?>
779
				</ul>
780
781
				<?php foreach ( $this->settings as $setting ) : ?>
782
					<?php do_action( "give_before_{$setting['id']}_settings" ); ?>
783
					<?php
784
					// Determine if current panel is active.
785
					$is_active = $active_tab === $setting['id'] ? true : false;
786
					?>
787
					<div id="<?php echo $setting['id']; ?>"
788
						 class="panel give_options_panel<?php echo( $is_active ? ' active' : '' ); ?>">
789
						<?php if ( ! empty( $setting['fields'] ) ) : ?>
790
							<?php foreach ( $setting['fields'] as $field ) : ?>
791
								<?php give_render_field( $field ); ?>
792
							<?php endforeach; ?>
793
						<?php endif; ?>
794
					</div>
795
					<?php do_action( "give_after_{$setting['id']}_settings" ); ?>
796
797
798
					<?php if ( $this->has_sub_tab( $setting ) ) : ?>
799
						<?php if ( ! empty( $setting['sub-fields'] ) ) : ?>
800
							<?php foreach ( $setting['sub-fields'] as $index => $sub_fields ) : ?>
801
								<div id="<?php echo $sub_fields['id']; ?>" class="panel give_options_panel give-hidden">
802
									<?php if ( ! empty( $sub_fields['fields'] ) ) : ?>
803
										<?php foreach ( $sub_fields['fields'] as $sub_field ) : ?>
804
											<?php give_render_field( $sub_field ); ?>
805
										<?php endforeach; ?>
806
									<?php endif; ?>
807
								</div>
808
							<?php endforeach; ?>
809
						<?php endif; ?>
810
					<?php endif; ?>
811
				<?php endforeach; ?>
812
			</div>
813
		<?php
814
		endif; // End if().
815
	}
816
817
	/**
818
	 * Output Goal meta-box settings.
819
	 *
820
	 * @param object $post Post Object.
821
	 *
822
	 * @access public
823
	 * @since  2.1.0
824
	 *
825
	 * @return void
826
	 */
827
	public function output_goal( $post ) {
828
829
		echo give_admin_form_goal_stats( $post->ID );
830
831
	}
832
833
	/**
834
	 * Check if setting field has sub tabs/fields
835
	 *
836
	 * @param array $field_setting Field Settings.
837
	 *
838
	 * @since 1.8
839
	 *
840
	 * @return bool
841
	 */
842
	private function has_sub_tab( $field_setting ) {
843
		$has_sub_tab = false;
844
		if ( array_key_exists( 'sub-fields', $field_setting ) ) {
845
			$has_sub_tab = true;
846
		}
847
848
		return $has_sub_tab;
849
	}
850
851
	/**
852
	 * CMB2 settings loader.
853
	 *
854
	 * @since 1.8
855
	 *
856
	 * @return array
857
	 */
858
	function cmb2_metabox_settings() {
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...
859
		$all_cmb2_settings   = apply_filters( 'cmb2_meta_boxes', array() );
860
		$give_forms_settings = $all_cmb2_settings;
861
862
		// Filter settings: Use only give forms related settings.
863
		foreach ( $all_cmb2_settings as $index => $setting ) {
864
			if ( ! in_array( 'give_forms', $setting['object_types'] ) ) {
865
				unset( $give_forms_settings[ $index ] );
866
			}
867
		}
868
869
		return $give_forms_settings;
870
871
	}
872
873
	/**
874
	 * Check if we're saving, the trigger an action based on the post type.
875
	 *
876
	 * @param int        $post_id Post ID.
877
	 * @param int|object $post    Post Object.
878
	 *
879
	 * @since 1.8
880
	 *
881
	 * @return void
882
	 */
883
	public function save( $post_id, $post ) {
884
885
		// $post_id and $post are required.
886
		if ( empty( $post_id ) || empty( $post ) ) {
887
			return;
888
		}
889
890
		// Don't save meta boxes for revisions or autosaves.
891
		if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
892
			return;
893
		}
894
895
		// Check the nonce.
896
		if ( empty( $_POST['give_form_meta_nonce'] ) || ! wp_verify_nonce( $_POST['give_form_meta_nonce'], 'give_save_form_meta' ) ) {
897
			return;
898
		}
899
900
		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
901
		if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
902
			return;
903
		}
904
905
		// Check user has permission to edit.
906
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
907
			return;
908
		}
909
910
		// Fire action before saving form meta.
911
		do_action( 'give_pre_process_give_forms_meta', $post_id, $post );
912
913
		/**
914
		 * Filter the meta key to save.
915
		 * Third party addon developer can remove there meta keys from this array to handle saving data on there own.
916
		 */
917
		$form_meta_keys = apply_filters( 'give_process_form_meta_keys', $this->get_meta_keys_from_settings() );
918
919
		// Save form meta data.
920
		if ( ! empty( $form_meta_keys ) ) {
921
			foreach ( $form_meta_keys as $form_meta_key ) {
922
923
				// Set default value for checkbox fields.
924
				if (
925
					! isset( $_POST[ $form_meta_key ] ) &&
926
					in_array( $this->get_field_type( $form_meta_key ), array( 'checkbox', 'chosen' ) )
927
				) {
928
					$_POST[ $form_meta_key ] = '';
929
				}
930
931
				if ( isset( $_POST[ $form_meta_key ] ) ) {
932
					$setting_field = $this->get_setting_field( $form_meta_key );
933
					if ( ! empty( $setting_field['type'] ) ) {
934
						switch ( $setting_field['type'] ) {
935
							case 'textarea':
936
							case 'wysiwyg':
937
								$form_meta_value = wp_kses_post( $_POST[ $form_meta_key ] );
938
								break;
939
940
							case 'donation_limit' :
941
								$form_meta_value = $_POST[ $form_meta_key ];
942
								break;
943
944
							case 'group':
945
								$form_meta_value = array();
946
947
								foreach ( $_POST[ $form_meta_key ] as $index => $group ) {
0 ignored issues
show
Bug introduced by
The expression $_POST[$form_meta_key] of type string is not traversable.
Loading history...
948
949
									// Do not save template input field values.
950
									if ( '{{row-count-placeholder}}' === $index ) {
951
										continue;
952
									}
953
954
									$group_meta_value = array();
955
									foreach ( $group as $field_id => $field_value ) {
956
										switch ( $this->get_field_type( $field_id, $form_meta_key ) ) {
957
											case 'wysiwyg':
958
												$group_meta_value[ $field_id ] = wp_kses_post( $field_value );
959
												break;
960
961
											default:
962
												$group_meta_value[ $field_id ] = give_clean( $field_value );
963
										}
964
									}
965
966
									if ( ! empty( $group_meta_value ) ) {
967
										$form_meta_value[ $index ] = $group_meta_value;
968
									}
969
								}
970
971
								// Arrange repeater field keys in order.
972
								$form_meta_value = array_values( $form_meta_value );
973
								break;
974
975
							default:
976
								$form_meta_value = give_clean( $_POST[ $form_meta_key ] );
977
						}// End switch().
978
979
						/**
980
						 * Filter the form meta value before saving
981
						 *
982
						 * @since 1.8.9
983
						 */
984
						$form_meta_value = apply_filters(
985
							'give_pre_save_form_meta_value',
986
							$this->sanitize_form_meta( $form_meta_value, $setting_field ),
987
							$form_meta_key,
988
							$this,
989
							$post_id
990
						);
991
992
						// Range slider.
993
						if ( 'donation_limit' === $setting_field['type'] ) {
994
995
							// Sanitize amount for db.
996
							$form_meta_value = array_map( 'give_sanitize_amount_for_db', $form_meta_value );
997
998
							// Store it to form meta.
999
							give_update_meta( $post_id, $form_meta_key . '_minimum', $form_meta_value['minimum'] );
1000
							give_update_meta( $post_id, $form_meta_key . '_maximum', $form_meta_value['maximum'] );
1001
						} else {
1002
							// Save data.
1003
							give_update_meta( $post_id, $form_meta_key, $form_meta_value );
1004
						}
1005
1006
						// Verify and delete form meta based on the form status.
1007
						give_set_form_closed_status( $post_id );
1008
1009
						// Fire after saving form meta key.
1010
						do_action( "give_save_{$form_meta_key}", $form_meta_key, $form_meta_value, $post_id, $post );
1011
					}// End if().
1012
				}// End if().
1013
			}// End foreach().
1014
		}// End if().
1015
1016
		// Update the goal progress for donation form.
1017
		give_update_goal_progress( $post_id );
1018
1019
		// Fire action after saving form meta.
1020
		do_action( 'give_post_process_give_forms_meta', $post_id, $post );
1021
	}
1022
1023
1024
	/**
1025
	 * Get field ID.
1026
	 *
1027
	 * @param array $field Array of Fields.
1028
	 *
1029
	 * @since 1.8
1030
	 *
1031
	 * @return string
1032
	 */
1033
	private function get_field_id( $field ) {
1034
		$field_id = '';
1035
1036
		if ( array_key_exists( 'id', $field ) ) {
1037
			$field_id = $field['id'];
1038
1039
		}
1040
1041
		return $field_id;
1042
	}
1043
1044
	/**
1045
	 * Get fields ID.
1046
	 *
1047
	 * @param array $setting Array of settings.
1048
	 *
1049
	 * @since 1.8
1050
	 *
1051
	 * @return array
1052
	 */
1053
	private function get_fields_id( $setting ) {
1054
		$meta_keys = array();
1055
1056
		if (
1057
			! empty( $setting )
1058
			&& array_key_exists( 'fields', $setting )
1059
			&& ! empty( $setting['fields'] )
1060
		) {
1061
			foreach ( $setting['fields'] as $field ) {
1062
				if ( $field_id = $this->get_field_id( $field ) ) {
1063
					$meta_keys[] = $field_id;
1064
				}
1065
			}
1066
		}
1067
1068
		return $meta_keys;
1069
	}
1070
1071
	/**
1072
	 * Get sub fields ID.
1073
	 *
1074
	 * @param array $setting Array of settings.
1075
	 *
1076
	 * @since 1.8
1077
	 *
1078
	 * @return array
1079
	 */
1080
	private function get_sub_fields_id( $setting ) {
1081
		$meta_keys = array();
1082
1083
		if ( $this->has_sub_tab( $setting ) && ! empty( $setting['sub-fields'] ) ) {
1084
			foreach ( $setting['sub-fields'] as $fields ) {
1085
				if ( ! empty( $fields['fields'] ) ) {
1086
					foreach ( $fields['fields'] as $field ) {
1087
						if ( $field_id = $this->get_field_id( $field ) ) {
1088
							$meta_keys[] = $field_id;
1089
						}
1090
					}
1091
				}
1092
			}
1093
		}
1094
1095
		return $meta_keys;
1096
	}
1097
1098
1099
	/**
1100
	 * Get all setting field ids.
1101
	 *
1102
	 * @since 1.8
1103
	 *
1104
	 * @return array
1105
	 */
1106
	private function get_meta_keys_from_settings() {
1107
		$meta_keys = array();
1108
1109
		foreach ( $this->settings as $setting ) {
1110
			$meta_key = $this->get_fields_id( $setting );
1111
1112
			if ( $this->has_sub_tab( $setting ) ) {
1113
				$meta_key = array_merge( $meta_key, $this->get_sub_fields_id( $setting ) );
1114
			}
1115
1116
			$meta_keys = array_merge( $meta_keys, $meta_key );
1117
		}
1118
1119
		return $meta_keys;
1120
	}
1121
1122
1123
	/**
1124
	 * Get field type.
1125
	 *
1126
	 * @param string $field_id Field ID.
1127
	 * @param string $group_id Field Group ID.
1128
	 *
1129
	 * @since 1.8
1130
	 *
1131
	 * @return string
1132
	 */
1133
	function get_field_type( $field_id, $group_id = '' ) {
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...
1134
		$field = $this->get_setting_field( $field_id, $group_id );
1135
1136
		$type = array_key_exists( 'type', $field )
1137
			? $field['type']
1138
			: '';
1139
1140
		return $type;
1141
	}
1142
1143
1144
	/**
1145
	 * Get Field
1146
	 *
1147
	 * @param array  $setting  Settings array.
1148
	 * @param string $field_id Field ID.
1149
	 *
1150
	 * @since 1.8
1151
	 *
1152
	 * @return array
1153
	 */
1154
	private function get_field( $setting, $field_id ) {
1155
		$setting_field = array();
1156
1157 View Code Duplication
		if ( ! empty( $setting['fields'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1158
			foreach ( $setting['fields'] as $field ) {
1159
				if ( array_key_exists( 'id', $field ) && $field['id'] === $field_id ) {
1160
					$setting_field = $field;
1161
					break;
1162
				}
1163
			}
1164
		}
1165
1166
		return $setting_field;
1167
	}
1168
1169
	/**
1170
	 * Get Sub Field
1171
	 *
1172
	 * @param array  $setting  Settings array.
1173
	 * @param string $field_id Field ID.
1174
	 *
1175
	 * @since 1.8
1176
	 *
1177
	 * @return array
1178
	 */
1179
	private function get_sub_field( $setting, $field_id ) {
1180
		$setting_field = array();
1181
1182
		if ( ! empty( $setting['sub-fields'] ) ) {
1183
			foreach ( $setting['sub-fields'] as $fields ) {
1184
				if ( $field = $this->get_field( $fields, $field_id ) ) {
1185
					$setting_field = $field;
1186
					break;
1187
				}
1188
			}
1189
		}
1190
1191
		return $setting_field;
1192
	}
1193
1194
	/**
1195
	 * Get setting field.
1196
	 *
1197
	 * @param string $field_id Field ID.
1198
	 * @param string $group_id Get sub field from group.
1199
	 *
1200
	 * @since 1.8
1201
	 *
1202
	 * @return array
1203
	 */
1204
	function get_setting_field( $field_id, $group_id = '' ) {
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...
1205
		$setting_field = array();
1206
1207
		$_field_id = $field_id;
1208
		$field_id  = empty( $group_id ) ? $field_id : $group_id;
1209
1210
		if ( ! empty( $this->settings ) ) {
1211
			foreach ( $this->settings as $setting ) {
1212
				if (
1213
					( $this->has_sub_tab( $setting ) && ( $setting_field = $this->get_sub_field( $setting, $field_id ) ) )
1214
					|| ( $setting_field = $this->get_field( $setting, $field_id ) )
1215
				) {
1216
					break;
1217
				}
1218
			}
1219
		}
1220
1221
		// Get field from group.
1222 View Code Duplication
		if ( ! empty( $group_id ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1223
			foreach ( $setting_field['fields'] as $field ) {
1224
				if ( array_key_exists( 'id', $field ) && $field['id'] === $_field_id ) {
1225
					$setting_field = $field;
1226
				}
1227
			}
1228
		}
1229
1230
		return $setting_field;
1231
	}
1232
1233
1234
	/**
1235
	 * Add offline donations setting tab to donation form options metabox.
1236
	 *
1237
	 * @param array $settings List of form settings.
1238
	 *
1239
	 * @since 1.8
1240
	 *
1241
	 * @return mixed
1242
	 */
1243
	function add_offline_donations_setting_tab( $settings ) {
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...
1244
		if ( give_is_gateway_active( 'offline' ) ) {
1245
			$settings['offline_donations_options'] = apply_filters( 'give_forms_offline_donations_options', array(
1246
				'id'        => 'offline_donations_options',
1247
				'title'     => __( 'Offline Donations', 'give' ),
1248
				'icon-html' => '<span class="give-icon give-icon-purse"></span>',
1249
				'fields'    => apply_filters( 'give_forms_offline_donations_metabox_fields', array() ),
1250
			) );
1251
		}
1252
1253
		return $settings;
1254
	}
1255
1256
1257
	/**
1258
	 * Sanitize form meta values before saving.
1259
	 *
1260
	 * @param mixed $meta_value    Meta Value for sanitizing before saving.
1261
	 * @param array $setting_field Setting Field.
1262
	 *
1263
	 * @since  1.8.9
1264
	 * @access public
1265
	 *
1266
	 * @return mixed
1267
	 */
1268
	function sanitize_form_meta( $meta_value, $setting_field ) {
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...
1269
		switch ( $setting_field['type'] ) {
1270
			case 'group':
1271
				if ( ! empty( $setting_field['fields'] ) ) {
1272
					foreach ( $setting_field['fields'] as $field ) {
1273
						if ( empty( $field['data_type'] ) || 'price' !== $field['data_type'] ) {
1274
							continue;
1275
						}
1276
1277
						foreach ( $meta_value as $index => $meta_data ) {
1278
							if ( ! isset( $meta_value[ $index ][ $field['id'] ] ) ) {
1279
								continue;
1280
							}
1281
1282
							$meta_value[ $index ][ $field['id'] ] = ! empty( $meta_value[ $index ][ $field['id'] ] ) ?
1283
								give_sanitize_amount_for_db( $meta_value[ $index ][ $field['id'] ] ) :
1284
								( ( '_give_amount' === $field['id'] && empty( $field_value ) ) ?
1285
									give_sanitize_amount_for_db( '1.00' ) :
1286
									0 );
1287
						}
1288
					}
1289
				}
1290
				break;
1291
1292
			default:
1293
				if ( ! empty( $setting_field['data_type'] ) && 'price' === $setting_field['data_type'] ) {
1294
					$meta_value = $meta_value ?
1295
						give_sanitize_amount_for_db( $meta_value ) :
1296
						( in_array( $setting_field['id'], array(
1297
							'_give_set_price',
1298
							'_give_custom_amount_minimum',
1299
							'_give_set_goal'
1300
						) ) ?
1301
							give_sanitize_amount_for_db( '1.00' ) :
1302
							0 );
1303
				}
1304
		}
1305
1306
		return $meta_value;
1307
	}
1308
1309
	/**
1310
	 * Maintain the active tab after save.
1311
	 *
1312
	 * @param string $location The destination URL.
1313
	 * @param int    $post_id  The post ID.
1314
	 *
1315
	 * @since  1.8.13
1316
	 * @access public
1317
	 *
1318
	 * @return string The URL after redirect.
1319
	 */
1320
	public function maintain_active_tab( $location, $post_id ) {
1321
		if (
1322
			'give_forms' === get_post_type( $post_id ) &&
1323
			! empty( $_POST['give_form_active_tab'] )
1324
		) {
1325
			$location = add_query_arg( 'give_tab', give_clean( $_POST['give_form_active_tab'] ), $location );
1326
		}
1327
1328
		return $location;
1329
	}
1330
}
1331
1332
new Give_MetaBox_Form_Data();
1333
1334