Completed
Push — issue/2250 ( d83be4 )
by Ravinder
253:30 queued 246:15
created

Give_MetaBox_Form_Data::output_goal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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, WordImpress
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'          => __( 'Max/Min. Amount', 'give' ),
155
						'description'   => __( 'Set minimum and maximum amount limit.', 'give' ),
156
						'id'            => $prefix . 'custom_amount_range',
157
						'type'          => 'range_slider',
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'       => 1.00,
167
							'maximum'       => 999999.99,
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
				),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 16.
Loading history...
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'    => __( 'Company Donations', 'give' ),
284
							'desc'    => __( 'Do you want a Company field to appear after First Name and Last Name?', 'give' ),
285
							'id'      => $prefix . 'company_field',
286
							'type'    => 'radio_inline',
287
							'default' => 'global',
288
							'options' => array(
289
								'global' => __( 'Global Option', 'give' ),
290
								'required' => __( 'Required', 'give' ),
291
								'optional' => __( 'Optional', 'give' ),
292
								'disabled' => __( 'Disabled', 'give' ),
293
294
							),
295
						),
296
						array(
297
							'name'    => __( 'Guest Donations', 'give' ),
298
							'desc'    => __( 'Do you want to allow non-logged-in users to make donations?', 'give' ),
299
							'id'      => $prefix . 'logged_in_only',
300
							'type'    => 'radio_inline',
301
							'default' => 'enabled',
302
							'options' => array(
303
								'enabled'  => __( 'Enabled', 'give' ),
304
								'disabled' => __( 'Disabled', 'give' ),
305
							),
306
						),
307
						array(
308
							'name'    => __( 'Registration', 'give' ),
309
							'desc'    => __( 'Display the registration and login forms in the payment section for non-logged-in users.', 'give' ),
310
							'id'      => $prefix . 'show_register_form',
311
							'type'    => 'radio',
312
							'options' => array(
313
								'none'         => __( 'None', 'give' ),
314
								'registration' => __( 'Registration', 'give' ),
315
								'login'        => __( 'Login', 'give' ),
316
								'both'         => __( 'Registration + Login', 'give' ),
317
							),
318
							'default' => 'none',
319
						),
320
						array(
321
							'name'    => __( 'Floating Labels', 'give' ),
322
							/* translators: %s: forms http://docs.givewp.com/form-floating-labels */
323
							'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' ) ),
324
							'id'      => $prefix . 'form_floating_labels',
325
							'type'    => 'radio_inline',
326
							'options' => array(
327
								'global'   => __( 'Global Option', 'give' ),
328
								'enabled'  => __( 'Enabled', 'give' ),
329
								'disabled' => __( 'Disabled', 'give' ),
330
							),
331
							'default' => 'global',
332
						),
333
						array(
334
							'name'  => 'form_display_docs',
335
							'type'  => 'docs_link',
336
							'url'   => 'http://docs.givewp.com/form-display-options',
337
							'title' => __( 'Form Display', 'give' ),
338
						),
339
					),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 24 spaces, but found 20.
Loading history...
340
						$post_id
341
					),
342
				)
343
			),
344
345
			/**
346
			 * Donation Goals
347
			 */
348
			'donation_goal_options' => apply_filters( 'give_donation_goal_options', array(
349
				'id'        => 'donation_goal_options',
350
				'title'     => __( 'Donation Goal', 'give' ),
351
				'icon-html' => '<span class="give-icon give-icon-target"></span>',
352
				'fields'    => apply_filters( 'give_forms_donation_goal_metabox_fields', array(
353
					// Goals
354
					array(
355
						'name'        => __( 'Donation Goal', 'give' ),
356
						'description' => __( 'Do you want to set a donation goal for this form?', 'give' ),
357
						'id'          => $prefix . 'goal_option',
358
						'type'        => 'radio_inline',
359
						'default'     => 'disabled',
360
						'options'     => array(
361
							'enabled'  => __( 'Enabled', 'give' ),
362
							'disabled' => __( 'Disabled', 'give' ),
363
						),
364
					),
365
366
					array(
367
						'name'        => __( 'Goal Format', 'give' ),
368
						'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' ),
369
						'id'          => $prefix . 'goal_format',
370
						'type'        => 'radio',
371
						'default'     => 'amount',
372
						'options'     => array(
373
							'amount'     => __( 'Amount Raised', 'give' ),
374
							'percentage' => __( 'Percentage Raised', 'give' ),
375
							'donation'   => __( 'Number of Donations', 'give' ),
376
							'donors'     => __( 'Number of Donors', 'give' ),
377
						),
378
					),
379
380
					array(
381
						'name'          => __( 'Goal Amount', 'give' ),
382
						'description'   => __( 'This is the monetary goal amount you want to reach for this form.', 'give' ),
383
						'id'            => $prefix . 'set_goal',
384
						'type'          => 'text_small',
385
						'data_type'     => 'price',
386
						'attributes'    => array(
387
							'placeholder' => $price_placeholder,
388
							'class'       => 'give-money-field',
389
						),
390
						'wrapper_class' => 'give-hidden',
391
					),
392
					array(
393
						'id'         => $prefix . 'number_of_donation_goal',
394
						'name'       => __( 'Donation Goal', 'give' ),
395
						'desc'       => __( 'Set the total number of donations as a goal.', 'give' ),
396
						'type'       => 'number',
397
						'default'    => 1,
398
						'attributes' => array(
399
							'placeholder' => 1,
400
						),
401
					),
402
					array(
403
						'id'         => $prefix . 'number_of_donor_goal',
404
						'name'       => __( 'Donor Goal', 'give' ),
405
						'desc'       => __( 'Set the total number of donors as a goal.', 'give' ),
406
						'type'       => 'number',
407
						'default'    => 1,
408
						'attributes' => array(
409
							'placeholder' => 1,
410
						),
411
					),
412
					array(
413
						'name'          => __( 'Progress Bar Color', 'give' ),
414
						'desc'          => __( 'Customize the color of the goal progress bar.', 'give' ),
415
						'id'            => $prefix . 'goal_color',
416
						'type'          => 'colorpicker',
417
						'default'       => '#2bc253',
418
						'wrapper_class' => 'give-hidden',
419
					),
420
421
					array(
422
						'name'          => __( 'Close Form', 'give' ),
423
						'desc'          => __( 'Do you want to close the donation forms and stop accepting donations once this goal has been met?', 'give' ),
424
						'id'            => $prefix . 'close_form_when_goal_achieved',
425
						'type'          => 'radio_inline',
426
						'default'       => 'disabled',
427
						'options'       => array(
428
							'enabled'  => __( 'Enabled', 'give' ),
429
							'disabled' => __( 'Disabled', 'give' ),
430
						),
431
						'wrapper_class' => 'give-hidden',
432
					),
433
					array(
434
						'name'          => __( 'Goal Achieved Message', 'give' ),
435
						'desc'          => __( 'Do you want to display a custom message when the goal is closed?', 'give' ),
436
						'id'            => $prefix . 'form_goal_achieved_message',
437
						'type'          => 'wysiwyg',
438
						'default'       => __( 'Thank you to all our donors, we have met our fundraising goal.', 'give' ),
439
						'wrapper_class' => 'give-hidden',
440
					),
441
					array(
442
						'name'  => 'donation_goal_docs',
443
						'type'  => 'docs_link',
444
						'url'   => 'http://docs.givewp.com/form-donation-goal',
445
						'title' => __( 'Donation Goal', 'give' ),
446
					),
447
				),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 16.
Loading history...
448
					$post_id
449
				),
450
			) ),
451
452
			/**
453
			 * Content Field
454
			 */
455
			'form_content_options'  => apply_filters( 'give_forms_content_options', array(
456
				'id'        => 'form_content_options',
457
				'title'     => __( 'Form Content', 'give' ),
458
				'icon-html' => '<span class="give-icon give-icon-edit"></span>',
459
				'fields'    => apply_filters( 'give_forms_content_options_metabox_fields', array(
460
461
					// Donation content.
462
					array(
463
						'name'        => __( 'Display Content', 'give' ),
464
						'description' => __( 'Do you want to add custom content to this form?', 'give' ),
465
						'id'          => $prefix . 'display_content',
466
						'type'        => 'radio_inline',
467
						'options'     => array(
468
							'enabled'  => __( 'Enabled', 'give' ),
469
							'disabled' => __( 'Disabled', 'give' ),
470
						),
471
						'default'     => 'disabled',
472
					),
473
474
					// Content placement.
475
					array(
476
						'name'          => __( 'Content Placement', 'give' ),
477
						'description'   => __( 'This option controls where the content appears within the donation form.', 'give' ),
478
						'id'            => $prefix . 'content_placement',
479
						'type'          => 'radio_inline',
480
						'options'       => apply_filters( 'give_forms_content_options_select', array(
481
								'give_pre_form'  => __( 'Above fields', 'give' ),
482
								'give_post_form' => __( 'Below fields', 'give' ),
483
							)
484
						),
485
						'default'       => 'give_pre_form',
486
						'wrapper_class' => 'give-hidden',
487
					),
488
					array(
489
						'name'          => __( 'Content', 'give' ),
490
						'description'   => __( 'This content will display on the single give form page.', 'give' ),
491
						'id'            => $prefix . 'form_content',
492
						'type'          => 'wysiwyg',
493
						'wrapper_class' => 'give-hidden',
494
					),
495
					array(
496
						'name'  => 'form_content_docs',
497
						'type'  => 'docs_link',
498
						'url'   => 'http://docs.givewp.com/form-content',
499
						'title' => __( 'Form Content', 'give' ),
500
					),
501
				),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 16.
Loading history...
502
					$post_id
503
				),
504
			) ),
505
506
			/**
507
			 * Terms & Conditions
508
			 */
509
			'form_terms_options'    => apply_filters( 'give_forms_terms_options', array(
510
				'id'        => 'form_terms_options',
511
				'title'     => __( 'Terms & Conditions', 'give' ),
512
				'icon-html' => '<span class="give-icon give-icon-checklist"></span>',
513
				'fields'    => apply_filters( 'give_forms_terms_options_metabox_fields', array(
514
					// Donation Option
515
					array(
516
						'name'        => __( 'Terms and Conditions', 'give' ),
517
						'description' => __( 'Do you want to require the donor to accept terms prior to being able to complete their donation?', 'give' ),
518
						'id'          => $prefix . 'terms_option',
519
						'type'        => 'radio_inline',
520
						'options'     => apply_filters( 'give_forms_content_options_select', array(
521
								'global'   => __( 'Global Option', 'give' ),
522
								'enabled'  => __( 'Customize', 'give' ),
523
								'disabled' => __( 'Disable', 'give' ),
524
							)
525
						),
526
						'default'     => 'global',
527
					),
528
					array(
529
						'id'            => $prefix . 'agree_label',
530
						'name'          => __( 'Agreement Label', 'give' ),
531
						'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' ),
532
						'type'          => 'text',
533
						'size'          => 'regular',
534
						'attributes'    => array(
535
							'placeholder' => __( 'Agree to Terms?', 'give' ),
536
						),
537
						'wrapper_class' => 'give-hidden',
538
					),
539
					array(
540
						'id'            => $prefix . 'agree_text',
541
						'name'          => __( 'Agreement Text', 'give' ),
542
						'desc'          => __( 'This is the actual text which the user will have to agree to in order to make a donation.', 'give' ),
543
						'default'       => give_get_option( 'agreement_text' ),
544
						'type'          => 'wysiwyg',
545
						'wrapper_class' => 'give-hidden',
546
					),
547
					array(
548
						'name'  => 'terms_docs',
549
						'type'  => 'docs_link',
550
						'url'   => 'http://docs.givewp.com/form-terms',
551
						'title' => __( 'Terms and Conditions', 'give' ),
552
					),
553
				),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 16.
Loading history...
554
					$post_id
555
				),
556
			) ),
557
		);
558
559
		/**
560
		 * Filter the metabox tabbed panel settings.
561
		 */
562
		$settings = apply_filters( 'give_metabox_form_data_settings', $settings, $post_id );
563
564
		// Output.
565
		return $settings;
566
	}
567
568
	/**
569
	 * Add metabox.
570
	 *
571
	 * @since 1.8
572
	 *
573
	 * @return void
574
	 */
575
	public function add_meta_box() {
576
		add_meta_box(
577
			$this->get_metabox_ID(),
578
			$this->get_metabox_label(),
579
			array( $this, 'output' ),
580
			array( 'give_forms' ),
581
			'normal',
582
			'high'
583
		);
584
585
		// Show Goal Metabox only if goal is enabled.
586
		if ( give_is_setting_enabled( give_get_meta( give_get_admin_post_id(), '_give_goal_option', true ) ) ) {
587
			add_meta_box(
588
				'give-form-goal-stats',
589
				__( 'Goal Statistics', 'give' ),
590
				array( $this, 'output_goal' ),
591
				array( 'give_forms' ),
592
				'side',
593
				'low'
594
			);
595
		}
596
597
	}
598
599
600
	/**
601
	 * Enqueue scripts.
602
	 *
603
	 * @since 1.8
604
	 *
605
	 * @return void
606
	 */
607
	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...
608
		global $post;
609
610
		if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
611
612
		}
613
	}
614
615
	/**
616
	 * Get metabox id.
617
	 *
618
	 * @since 1.8
619
	 *
620
	 * @return string
621
	 */
622
	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...
Coding Style introduced by
The function name get_metabox_ID is in camel caps, but expected get_metabox_i_d instead as per the coding standard.
Loading history...
623
		return $this->metabox_id;
624
	}
625
626
	/**
627
	 * Get metabox label.
628
	 *
629
	 * @since 1.8
630
	 *
631
	 * @return string
632
	 */
633
	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...
634
		return $this->metabox_label;
635
	}
636
637
638
	/**
639
	 * Get metabox tabs.
640
	 *
641
	 * @since 1.8
642
	 *
643
	 * @return array
644
	 */
645
	public function get_tabs() {
646
		$tabs = array();
647
648
		if ( ! empty( $this->settings ) ) {
649
			foreach ( $this->settings as $setting ) {
650
				if ( ! isset( $setting['id'] ) || ! isset( $setting['title'] ) ) {
651
					continue;
652
				}
653
				$tab = array(
654
					'id'        => $setting['id'],
655
					'label'     => $setting['title'],
656
					'icon-html' => ( ! empty( $setting['icon-html'] ) ? $setting['icon-html'] : '' ),
657
				);
658
659
				if ( $this->has_sub_tab( $setting ) ) {
660
					if ( empty( $setting['sub-fields'] ) ) {
661
						$tab = array();
662
					} else {
663
						foreach ( $setting['sub-fields'] as $sub_fields ) {
664
							$tab['sub-fields'][] = array(
665
								'id'        => $sub_fields['id'],
666
								'label'     => $sub_fields['title'],
667
								'icon-html' => ( ! empty( $sub_fields['icon-html'] ) ? $sub_fields['icon-html'] : '' ),
668
							);
669
						}
670
					}
671
				}
672
673
				if ( ! empty( $tab ) ) {
674
					$tabs[] = $tab;
675
				}
676
			}
677
		}
678
679
		return $tabs;
680
	}
681
682
	/**
683
	 * Output metabox settings.
684
	 *
685
	 * @since 1.8
686
	 *
687
	 * @return void
688
	 */
689
	public function output() {
690
		// Bailout.
691
		if ( $form_data_tabs = $this->get_tabs() ) :
692
			$active_tab = ! empty( $_GET['give_tab'] ) ? give_clean( $_GET['give_tab'] ) : 'form_field_options';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
693
			wp_nonce_field( 'give_save_form_meta', 'give_form_meta_nonce' );
694
			?>
695
			<input id="give_form_active_tab" type="hidden" name="give_form_active_tab">
696
			<div class="give-metabox-panel-wrap">
697
				<ul class="give-form-data-tabs give-metabox-tabs">
698
					<?php foreach ( $form_data_tabs as $index => $form_data_tab ) : ?>
699
						<?php
700
						// Determine if current tab is active.
701
						$is_active = $active_tab === $form_data_tab['id'] ? true : false;
702
						?>
703
						<li class="<?php echo "{$form_data_tab['id']}_tab" . ( $is_active ? ' active' : '' ) . ( $this->has_sub_tab( $form_data_tab ) ? ' has-sub-fields' : '' ); ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '"{$form_data_tab['id']}_tab"'
Loading history...
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
704
							<a href="#<?php echo $form_data_tab['id']; ?>" data-tab-id="<?php echo $form_data_tab['id']; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$form_data_tab'
Loading history...
705
								<?php if ( ! empty( $form_data_tab['icon-html'] ) ) : ?>
706
									<?php echo $form_data_tab['icon-html']; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$form_data_tab'
Loading history...
707
								<?php else : ?>
708
									<span class="give-icon give-icon-default"></span>
709
								<?php endif; ?>
710
								<span class="give-label"><?php echo $form_data_tab['label']; ?></span>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$form_data_tab'
Loading history...
711
							</a>
712
							<?php if ( $this->has_sub_tab( $form_data_tab ) ) : ?>
713
								<ul class="give-metabox-sub-tabs give-hidden">
714
									<?php foreach ( $form_data_tab['sub-fields'] as $sub_tab ) : ?>
715
										<li class="<?php echo "{$sub_tab['id']}_tab"; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '"{$sub_tab['id']}_tab"'
Loading history...
716
											<a href="#<?php echo $sub_tab['id']; ?>" data-tab-id="<?php echo $sub_tab['id']; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$sub_tab'
Loading history...
717
												<?php if ( ! empty( $sub_tab['icon-html'] ) ) : ?>
718
													<?php echo $sub_tab['icon-html']; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$sub_tab'
Loading history...
719
												<?php else : ?>
720
													<span class="give-icon give-icon-default"></span>
721
												<?php endif; ?>
722
												<span class="give-label"><?php echo $sub_tab['label']; ?></span>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$sub_tab'
Loading history...
723
											</a>
724
										</li>
725
									<?php endforeach; ?>
726
								</ul>
727
							<?php endif; ?>
728
						</li>
729
					<?php endforeach; ?>
730
				</ul>
731
732
				<?php foreach ( $this->settings as $setting ) : ?>
733
					<?php do_action( "give_before_{$setting['id']}_settings" ); ?>
734
					<?php
735
					// Determine if current panel is active.
736
					$is_active = $active_tab === $setting['id'] ? true : false;
737
					?>
738
					<div id="<?php echo $setting['id']; ?>" class="panel give_options_panel<?php echo( $is_active ? ' active' : '' ); ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$setting'
Loading history...
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
739
						<?php if ( ! empty( $setting['fields'] ) ) : ?>
740
							<?php foreach ( $setting['fields'] as $field ) : ?>
741
								<?php give_render_field( $field ); ?>
742
							<?php endforeach; ?>
743
						<?php endif; ?>
744
					</div>
745
					<?php do_action( "give_after_{$setting['id']}_settings" ); ?>
746
747
748
					<?php if ( $this->has_sub_tab( $setting ) ) : ?>
749
						<?php if ( ! empty( $setting['sub-fields'] ) ) : ?>
750
							<?php foreach ( $setting['sub-fields'] as $index => $sub_fields ) : ?>
751
								<div id="<?php echo $sub_fields['id']; ?>" class="panel give_options_panel give-hidden">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$sub_fields'
Loading history...
752
									<?php if ( ! empty( $sub_fields['fields'] ) ) : ?>
753
										<?php foreach ( $sub_fields['fields'] as $sub_field ) : ?>
754
											<?php give_render_field( $sub_field ); ?>
755
										<?php endforeach; ?>
756
									<?php endif; ?>
757
								</div>
758
							<?php endforeach; ?>
759
						<?php endif; ?>
760
					<?php endif; ?>
761
				<?php endforeach; ?>
762
			</div>
763
		<?php
764
		endif; // End if().
765
	}
766
767
	/**
768
	 * Output Goal meta-box settings.
769
	 *
770
	 * @param object $post Post Object.
771
	 *
772
	 * @access public
773
	 * @since  2.1.0
774
	 *
775
	 * @return void
776
	 */
777
	public function output_goal( $post ) {
778
779
		echo give_admin_form_goal_stats( $post->ID );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_admin_form_goal_stats'
Loading history...
780
781
	}
782
783
	/**
784
	 * Check if setting field has sub tabs/fields
785
	 *
786
	 * @param array $field_setting Field Settings.
787
	 *
788
	 * @since 1.8
789
	 *
790
	 * @return bool
791
	 */
792
	private function has_sub_tab( $field_setting ) {
793
		$has_sub_tab = false;
794
		if ( array_key_exists( 'sub-fields', $field_setting ) ) {
795
			$has_sub_tab = true;
796
		}
797
798
		return $has_sub_tab;
799
	}
800
801
	/**
802
	 * CMB2 settings loader.
803
	 *
804
	 * @since 1.8
805
	 *
806
	 * @return array
807
	 */
808
	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...
809
		$all_cmb2_settings   = apply_filters( 'cmb2_meta_boxes', array() );
810
		$give_forms_settings = $all_cmb2_settings;
811
812
		// Filter settings: Use only give forms related settings.
813
		foreach ( $all_cmb2_settings as $index => $setting ) {
814
			if ( ! in_array( 'give_forms', $setting['object_types'] ) ) {
815
				unset( $give_forms_settings[ $index ] );
816
			}
817
		}
818
819
		return $give_forms_settings;
820
821
	}
822
823
	/**
824
	 * Check if we're saving, the trigger an action based on the post type.
825
	 *
826
	 * @param int        $post_id Post ID.
827
	 * @param int|object $post    Post Object.
828
	 *
829
	 * @since 1.8
830
	 *
831
	 * @return void
832
	 */
833
	public function save( $post_id, $post ) {
834
835
		// $post_id and $post are required.
836
		if ( empty( $post_id ) || empty( $post ) ) {
837
			return;
838
		}
839
840
		// Don't save meta boxes for revisions or autosaves.
841
		if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
842
			return;
843
		}
844
845
		// Check the nonce.
846
		if ( empty( $_POST['give_form_meta_nonce'] ) || ! wp_verify_nonce( $_POST['give_form_meta_nonce'], 'give_save_form_meta' ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
847
			return;
848
		}
849
850
		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
851
		if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
852
			return;
853
		}
854
855
		// Check user has permission to edit.
856
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
857
			return;
858
		}
859
860
		// Fire action before saving form meta.
861
		do_action( 'give_pre_process_give_forms_meta', $post_id, $post );
862
863
		/**
864
		 * Filter the meta key to save.
865
		 * Third party addon developer can remove there meta keys from this array to handle saving data on there own.
866
		 */
867
		$form_meta_keys = apply_filters( 'give_process_form_meta_keys', $this->get_meta_keys_from_settings() );
868
869
		// Save form meta data.
870
		if ( ! empty( $form_meta_keys ) ) {
871
			foreach ( $form_meta_keys as $form_meta_key ) {
872
873
				// Set default value for checkbox fields.
874
				if (
875
					! isset( $_POST[ $form_meta_key ] )
876
					&& ( 'checkbox' === $this->get_field_type( $form_meta_key ) )
877
				) {
878
					$_POST[ $form_meta_key ] = '';
879
				}
880
881
				if ( isset( $_POST[ $form_meta_key ] ) ) {
882
					$setting_field = $this->get_setting_field( $form_meta_key );
883
					if ( ! empty( $setting_field['type'] ) ) {
884
						switch ( $setting_field['type'] ) {
885
							case 'textarea':
886
							case 'wysiwyg':
887
								$form_meta_value = wp_kses_post( $_POST[ $form_meta_key ] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
888
								break;
889
890
							case 'range_slider' :
891
								$form_meta_value = $_POST[ $form_meta_key ];
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
892
								break;
893
894
							case 'group':
895
								$form_meta_value = array();
896
897
								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...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
898
899
									// Do not save template input field values.
900
									if ( '{{row-count-placeholder}}' === $index ) {
901
										continue;
902
									}
903
904
									$group_meta_value = array();
905
									foreach ( $group as $field_id => $field_value ) {
906
										switch ( $this->get_field_type( $field_id, $form_meta_key ) ) {
907
											case 'wysiwyg':
908
												$group_meta_value[ $field_id ] = wp_kses_post( $field_value );
909
												break;
910
911
											default:
912
												$group_meta_value[ $field_id ] = give_clean( $field_value );
913
										}
914
									}
915
916
									if ( ! empty( $group_meta_value ) ) {
917
										$form_meta_value[ $index ] = $group_meta_value;
918
									}
919
								}
920
921
								// Arrange repeater field keys in order.
922
								$form_meta_value = array_values( $form_meta_value );
923
								break;
924
925
							default:
926
								$form_meta_value = give_clean( $_POST[ $form_meta_key ] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
927
						}// End switch().
928
929
						/**
930
						 * Filter the form meta value before saving
931
						 *
932
						 * @since 1.8.9
933
						 */
934
						$form_meta_value = apply_filters(
935
							'give_pre_save_form_meta_value',
936
							$this->sanitize_form_meta( $form_meta_value, $setting_field ),
937
							$form_meta_key,
938
							$this,
939
							$post_id
940
						);
941
942
						// Range slider.
943
						if ( 'range_slider' === $setting_field['type'] ) {
944
945
							// Sanitize amount for db.
946
							$form_meta_value = array_map( 'give_sanitize_amount_for_db', $form_meta_value );
947
948
							// Store it to form meta.
949
							give_update_meta( $post_id, $form_meta_key . '_minimum', $form_meta_value['minimum'] );
950
							give_update_meta( $post_id, $form_meta_key . '_maximum', $form_meta_value['maximum'] );
951
						} else {
952
							// Save data.
953
							give_update_meta( $post_id, $form_meta_key, $form_meta_value );
954
						}
955
956
						// Verify and delete form meta based on the form status.
957
						give_set_form_closed_status( $post_id );
958
959
						// Fire after saving form meta key.
960
						do_action( "give_save_{$form_meta_key}", $form_meta_key, $form_meta_value, $post_id, $post );
961
					}// End if().
962
				}// End if().
963
			}// End foreach().
964
		}// End if().
965
966
		// Fire action after saving form meta.
967
		do_action( 'give_post_process_give_forms_meta', $post_id, $post );
968
	}
969
970
971
	/**
972
	 * Get field ID.
973
	 *
974
	 * @param array $field Array of Fields.
975
	 *
976
	 * @since 1.8
977
	 *
978
	 * @return string
979
	 */
980
	private function get_field_id( $field ) {
981
		$field_id = '';
982
983
		if ( array_key_exists( 'id', $field ) ) {
984
			$field_id = $field['id'];
985
986
		}
987
988
		return $field_id;
989
	}
990
991
	/**
992
	 * Get fields ID.
993
	 *
994
	 * @param array $setting Array of settings.
995
	 *
996
	 * @since 1.8
997
	 *
998
	 * @return array
999
	 */
1000 View Code Duplication
	private function get_fields_id( $setting ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
1001
		$meta_keys = array();
1002
1003
		if ( ! empty( $setting ) ) {
1004
			foreach ( $setting['fields'] as $field ) {
1005
				if ( $field_id = $this->get_field_id( $field ) ) {
1006
					$meta_keys[] = $field_id;
1007
				}
1008
			}
1009
		}
1010
1011
		return $meta_keys;
1012
	}
1013
1014
	/**
1015
	 * Get sub fields ID.
1016
	 *
1017
	 * @param array $setting Array of settings.
1018
	 *
1019
	 * @since 1.8
1020
	 *
1021
	 * @return array
1022
	 */
1023
	private function get_sub_fields_id( $setting ) {
1024
		$meta_keys = array();
1025
1026
		if ( $this->has_sub_tab( $setting ) && ! empty( $setting['sub-fields'] ) ) {
1027
			foreach ( $setting['sub-fields'] as $fields ) {
1028
				if ( ! empty( $fields['fields'] ) ) {
1029
					foreach ( $fields['fields'] as $field ) {
1030
						if ( $field_id = $this->get_field_id( $field ) ) {
1031
							$meta_keys[] = $field_id;
1032
						}
1033
					}
1034
				}
1035
			}
1036
		}
1037
1038
		return $meta_keys;
1039
	}
1040
1041
1042
	/**
1043
	 * Get all setting field ids.
1044
	 *
1045
	 * @since 1.8
1046
	 *
1047
	 * @return array
1048
	 */
1049
	private function get_meta_keys_from_settings() {
1050
		$meta_keys = array();
1051
1052
		foreach ( $this->settings as $setting ) {
1053
			$meta_key = $this->get_fields_id( $setting );
1054
1055
			if ( $this->has_sub_tab( $setting ) ) {
1056
				$meta_key = array_merge( $meta_key, $this->get_sub_fields_id( $setting ) );
1057
			}
1058
1059
			$meta_keys = array_merge( $meta_keys, $meta_key );
1060
		}
1061
1062
		return $meta_keys;
1063
	}
1064
1065
1066
	/**
1067
	 * Get field type.
1068
	 *
1069
	 * @param string $field_id Field ID.
1070
	 * @param string $group_id Field Group ID.
1071
	 *
1072
	 * @since 1.8
1073
	 *
1074
	 * @return string
1075
	 */
1076
	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...
1077
		$field = $this->get_setting_field( $field_id, $group_id );
1078
1079
		$type = array_key_exists( 'type', $field )
1080
			? $field['type']
1081
			: '';
1082
1083
		return $type;
1084
	}
1085
1086
1087
	/**
1088
	 * Get Field
1089
	 *
1090
	 * @param array  $setting  Settings array.
1091
	 * @param string $field_id Field ID.
1092
	 *
1093
	 * @since 1.8
1094
	 *
1095
	 * @return array
1096
	 */
1097
	private function get_field( $setting, $field_id ) {
1098
		$setting_field = array();
1099
1100 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...
1101
			foreach ( $setting['fields'] as $field ) {
1102
				if ( array_key_exists( 'id', $field ) && $field['id'] === $field_id ) {
1103
					$setting_field = $field;
1104
					break;
1105
				}
1106
			}
1107
		}
1108
1109
		return $setting_field;
1110
	}
1111
1112
	/**
1113
	 * Get Sub Field
1114
	 *
1115
	 * @param array  $setting  Settings array.
1116
	 * @param string $field_id Field ID.
1117
	 *
1118
	 * @since 1.8
1119
	 *
1120
	 * @return array
1121
	 */
1122 View Code Duplication
	private function get_sub_field( $setting, $field_id ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
1123
		$setting_field = array();
1124
1125
		if ( ! empty( $setting['sub-fields'] ) ) {
1126
			foreach ( $setting['sub-fields'] as $fields ) {
1127
				if ( $field = $this->get_field( $fields, $field_id ) ) {
1128
					$setting_field = $field;
1129
					break;
1130
				}
1131
			}
1132
		}
1133
1134
		return $setting_field;
1135
	}
1136
1137
	/**
1138
	 * Get setting field.
1139
	 *
1140
	 * @param string $field_id Field ID.
1141
	 * @param string $group_id Get sub field from group.
1142
	 *
1143
	 * @since 1.8
1144
	 *
1145
	 * @return array
1146
	 */
1147
	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...
1148
		$setting_field = array();
1149
1150
		$_field_id = $field_id;
1151
		$field_id  = empty( $group_id ) ? $field_id : $group_id;
1152
1153
		if ( ! empty( $this->settings ) ) {
1154
			foreach ( $this->settings as $setting ) {
1155
				if (
1156
					( $this->has_sub_tab( $setting ) && ( $setting_field = $this->get_sub_field( $setting, $field_id ) ) )
1157
					|| ( $setting_field = $this->get_field( $setting, $field_id ) )
1158
				) {
1159
					break;
1160
				}
1161
			}
1162
		}
1163
1164
		// Get field from group.
1165 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...
1166
			foreach ( $setting_field['fields'] as $field ) {
1167
				if ( array_key_exists( 'id', $field ) && $field['id'] === $_field_id ) {
1168
					$setting_field = $field;
1169
				}
1170
			}
1171
		}
1172
1173
		return $setting_field;
1174
	}
1175
1176
1177
	/**
1178
	 * Add offline donations setting tab to donation form options metabox.
1179
	 *
1180
	 * @param array $settings List of form settings.
1181
	 *
1182
	 * @since 1.8
1183
	 *
1184
	 * @return mixed
1185
	 */
1186
	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...
1187
		if ( give_is_gateway_active( 'offline' ) ) {
1188
			$settings['offline_donations_options'] = apply_filters( 'give_forms_offline_donations_options', array(
1189
				'id'        => 'offline_donations_options',
1190
				'title'     => __( 'Offline Donations', 'give' ),
1191
				'icon-html' => '<span class="give-icon give-icon-purse"></span>',
1192
				'fields'    => apply_filters( 'give_forms_offline_donations_metabox_fields', array() ),
1193
			) );
1194
		}
1195
1196
		return $settings;
1197
	}
1198
1199
1200
	/**
1201
	 * Sanitize form meta values before saving.
1202
	 *
1203
	 * @param mixed $meta_value    Meta Value for sanitizing before saving.
1204
	 * @param array $setting_field Setting Field.
1205
	 *
1206
	 * @since  1.8.9
1207
	 * @access public
1208
	 *
1209
	 * @return mixed
1210
	 */
1211
	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...
1212
		switch ( $setting_field['type'] ) {
1213
			case 'group':
1214
				if ( ! empty( $setting_field['fields'] ) ) {
1215
					foreach ( $setting_field['fields'] as $field ) {
1216
						if ( empty( $field['data_type'] ) || 'price' !== $field['data_type'] ) {
1217
							continue;
1218
						}
1219
1220
						foreach ( $meta_value as $index => $meta_data ) {
1221
							if ( ! isset( $meta_value[ $index ][ $field['id'] ] ) ) {
1222
								continue;
1223
							}
1224
1225
							$meta_value[ $index ][ $field['id'] ] = ! empty( $meta_value[ $index ][ $field['id'] ] ) ?
1226
								give_sanitize_amount_for_db( $meta_value[ $index ][ $field['id'] ] ) :
1227
								( ( '_give_amount' === $field['id'] && empty( $field_value ) ) ?
1228
									give_sanitize_amount_for_db( '1.00' ) :
1229
									0 );
1230
						}
1231
					}
1232
				}
1233
				break;
1234
1235
			default:
1236
				if ( ! empty( $setting_field['data_type'] ) && 'price' === $setting_field['data_type'] ) {
1237
					$meta_value = $meta_value ?
1238
						give_sanitize_amount_for_db( $meta_value ) :
1239
						( in_array( $setting_field['id'], array( '_give_set_price', '_give_custom_amount_minimum', '_give_set_goal' ) ) ?
1240
							give_sanitize_amount_for_db( '1.00' ) :
1241
							0 );
1242
				}
1243
		}
1244
1245
		return $meta_value;
1246
	}
1247
1248
	/**
1249
	 * Maintain the active tab after save.
1250
	 *
1251
	 * @param string $location The destination URL.
1252
	 * @param int    $post_id  The post ID.
1253
	 *
1254
	 * @since  1.8.13
1255
	 * @access public
1256
	 *
1257
	 * @return string The URL after redirect.
1258
	 */
1259
	public function maintain_active_tab( $location, $post_id ) {
1260
		if (
1261
			'give_forms' === get_post_type( $post_id ) &&
1262
			! empty( $_POST['give_form_active_tab'] )
1263
		) {
1264
			$location = add_query_arg( 'give_tab', give_clean( $_POST['give_form_active_tab'] ), $location );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
1265
		}
1266
1267
		return $location;
1268
	}
1269
}
1270
1271
new Give_MetaBox_Form_Data();
1272
1273