Test Failed
Push — release/1.8.12 ( b58a2f...d255b1 )
by Ravinder
375:09 queued 372:17
created

Give_MetaBox_Form_Data   D

Complexity

Total Complexity 124

Size/Duplication

Total Lines 1133
Duplicated Lines 3.71 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 42
loc 1133
rs 4.4102
c 0
b 0
f 0
wmc 124
lcom 2
cbo 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A get_metabox_ID() 0 3 1
A get_metabox_label() 0 3 1
C get_tabs() 0 36 11
C output() 0 74 19
A has_sub_tab() 0 8 2
A cmb2_metabox_settings() 0 14 3
A __construct() 0 18 1
A setup() 0 3 1
B get_settings() 0 445 4
A add_meta_box() 0 10 1
A enqueue_script() 0 7 3
D save() 0 120 25
A get_field_id() 0 10 2
A get_fields_id() 13 13 4
B get_sub_fields_id() 0 17 7
A get_meta_keys_from_settings() 0 15 3
A get_field_type() 0 9 2
B get_field() 8 14 5
A get_sub_field() 14 14 4
C get_setting_field() 7 29 11
A add_offline_donations_setting_tab() 0 12 2
C sanitize_form_meta() 0 30 12

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Give_MetaBox_Form_Data often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Give_MetaBox_Form_Data, and based on these observations, apply Extract Interface, too.

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
66
67
	/**
68
	 * Setup metabox related data.
69
	 *
70
	 * @since  1.8
71
	 * @return void
72
	 */
73
	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...
74
		$this->settings = $this->get_settings();
75
	}
76
77
78
	/**
79
	 * Get metabox settings
80
	 *
81
	 * @since  1.8
82
	 * @return array
83
	 */
84
	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...
85
		$post_id               = give_get_admin_post_id();
86
		$price                 = give_get_form_price( $post_id );
87
		$custom_amount_minimum = give_get_form_minimum_price( $post_id );
88
		$goal                  = give_format_amount( give_get_form_goal( $post_id ), array( 'sanitize' => false ) );
0 ignored issues
show
Documentation introduced by
give_get_form_goal($post_id) is of type false|double, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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