Completed
Push — hotfix/fix_notices ( 9d56c9...88019d )
by Ravinder
17:04
created

Give_MetaBox_Form_Data   C

Complexity

Total Complexity 71

Size/Duplication

Total Lines 855
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 855
rs 5
c 0
b 0
f 0
wmc 71
lcom 2
cbo 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
A setup() 0 3 1
B get_settings() 0 428 4
A add_meta_box() 0 10 1
A enqueue_script() 0 8 3
A get_metabox_ID() 0 3 1
A get_metabox_label() 0 3 1
B get_tabs() 0 18 5
C output() 0 32 8
A cmb2_metabox_settings() 0 14 3
D save() 0 99 22
B get_meta_keys_from_settings() 0 16 5
A get_field_type() 0 8 2
C get_setting_field() 0 30 12
A add_offline_donations_setting_tab() 0 11 2

How to fix   Complexity   

Complex Class

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 17 and the first side effect is on line 873.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * 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 = esc_html__( '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' ), 30 );
56
57
		// Load required scripts.
58
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_script' ) );
59
60
		// Save form meta.
61
		add_action( 'save_post_give_forms', array( $this, 'save' ), 10, 2 );
62
63
		// cmb2 old setting loaders.
64
		// add_filter( 'give_metabox_form_data_settings', array( $this, 'cmb2_metabox_settings' ) );
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
		// Add offline donations options.
66
		add_filter( 'give_metabox_form_data_settings', array( $this, 'add_offline_donations_setting_tab' ), 0, 1 );
67
	}
68
69
70
	/**
71
	 * Setup metabox related data.
72
	 *
73
	 * @since  1.8
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
	 * @return array
86
	 */
87
	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...
88
		$post_id               = give_get_admin_post_id();
89
		$price                 = give_get_form_price( $post_id );
90
		$custom_amount_minimum = give_get_form_minimum_price( $post_id );
91
		$goal                  = give_get_form_goal( $post_id );
92
93
		// No empty prices - min. 1.00 for new forms
94
		if ( empty( $price ) && is_null( $post_id ) ) {
95
			$price = esc_attr( give_format_decimal( '1.00' ) );
96
		}
97
98
		// Min. $1.00 for new forms
99
		if ( empty( $custom_amount_minimum ) ) {
100
			$custom_amount_minimum = esc_attr( give_format_decimal( '1.00' ) );
101
		}
102
103
		// Start with an underscore to hide fields from custom fields list
104
		$prefix = '_give_';
105
106
		$settings = array(
107
			/**
108
			 * Repeatable Field Groups
109
			 */
110
			'form_field_options'    => apply_filters( 'give_forms_field_options', array(
111
				'id'     => 'form_field_options',
112
				'title'  => esc_html__( 'Donation Options', 'give' ),
113
				'fields' => apply_filters( 'give_forms_donation_form_metabox_fields', array(
114
						// Donation Option
115
						array(
116
							'name'        => esc_html__( 'Donation Option', 'give' ),
117
							'description' => esc_html__( 'Do you want this form to have one set donation price or multiple levels (for example, $10, $20, $50)?', 'give' ),
118
							'id'          => $prefix . 'price_option',
119
							'type'        => 'radio_inline',
120
							'default'     => 'set',
121
							'options'     => apply_filters( 'give_forms_price_options', array(
122
								'set'   => esc_html__( 'Set Donation', 'give' ),
123
								'multi' => esc_html__( 'Multi-level Donation', 'give' ),
124
							) ),
125
						),
126
						array(
127
							'name'        => esc_html__( 'Set Donation', 'give' ),
128
							'description' => esc_html__( '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' ),
129
							'id'          => $prefix . 'set_price',
130
							'type'        => 'text_small',
131
							'data_type'   => 'price',
132
							'attributes'  => array(
133
								'placeholder' => give_format_decimal( '1.00' ),
134
								'value'       => give_format_decimal( $price ),
135
								'class'       => 'give-money-field',
136
							),
137
						),
138
						// Donation Levels: Repeatable CMB2 Group
139
						array(
140
							'id'      => $prefix . 'donation_levels',
141
							'type'    => 'group',
142
							'options' => array(
143
								'add_button'    => esc_html__( 'Add Level', 'give' ),
144
								'header_title'  => esc_html__( 'Donation Level', 'give' ),
145
								'remove_button' => '<span class="dashicons dashicons-no"></span>',
146
							),
147
							// Fields array works the same, except id's only need to be unique for this group.
148
							// Prefix is not needed.
149
							'fields'  => apply_filters( 'give_donation_levels_table_row', array(
150
								array(
151
									'name' => esc_html__( 'ID', 'give' ),
152
									'id'   => $prefix . 'id',
153
									'type' => 'levels_id',
154
								),
155
								array(
156
									'name'       => esc_html__( 'Amount', 'give' ),
157
									'id'         => $prefix . 'amount',
158
									'type'       => 'text_small',
159
									'data_type'  => 'price',
160
									'attributes' => array(
161
										'placeholder' => give_format_decimal( '1.00' ),
162
										'class'       => 'give-money-field',
163
									),
164
								),
165
								array(
166
									'name'       => esc_html__( 'Text', 'give' ),
167
									'id'         => $prefix . 'text',
168
									'type'       => 'text',
169
									'attributes' => array(
170
										'placeholder' => esc_html__( 'Donation Level', 'give' ),
171
										'class'       => 'give-multilevel-text-field',
172
									),
173
								),
174
								array(
175
									'name' => esc_html__( 'Default', 'give' ),
176
									'id'   => $prefix . 'default',
177
									'type' => 'give_default_radio_inline',
178
								),
179
							) ),
180
						),
181
						// Display Style
182
						array(
183
							'name'        => esc_html__( 'Display Style', 'give' ),
184
							'description' => esc_html__( 'Set how the donations levels will display on the form.', 'give' ),
185
							'id'          => $prefix . 'display_style',
186
							'type'        => 'radio_inline',
187
							'default'     => 'buttons',
188
							'options'     => array(
189
								'buttons'  => esc_html__( 'Buttons', 'give' ),
190
								'radios'   => esc_html__( 'Radios', 'give' ),
191
								'dropdown' => esc_html__( 'Dropdown', 'give' ),
192
							),
193
						),
194
						// Custom Amount
195
						array(
196
							'name'        => esc_html__( 'Custom Amount', 'give' ),
197
							'description' => esc_html__( 'Do you want the user to be able to input their own donation amount?', 'give' ),
198
							'id'          => $prefix . 'custom_amount',
199
							'type'        => 'radio_inline',
200
							'default'     => 'disabled',
201
							'options'     => array(
202
								'enabled'  => esc_html__( 'Enabled', 'give' ),
203
								'disabled' => esc_html__( 'Disabled', 'give' ),
204
							),
205
						),
206
						array(
207
							'name'        => esc_html__( 'Minimum Amount', 'give' ),
208
							'description' => esc_html__( 'Enter the minimum custom donation amount.', 'give' ),
209
							'id'          => $prefix . 'custom_amount_minimum',
210
							'type'        => 'text_small',
211
							'data_type'   => 'price',
212
							'attributes'  => array(
213
								'placeholder' => give_format_decimal( '1.00' ),
214
								'value'       => give_format_decimal( $custom_amount_minimum ),
215
								'class'       => 'give-money-field',
216
							),
217
						),
218
						array(
219
							'name'        => esc_html__( 'Custom Amount Text', 'give' ),
220
							'description' => esc_html__( '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' ),
221
							'id'          => $prefix . 'custom_amount_text',
222
							'type'        => 'text',
223
							'attributes'  => array(
224
								'rows'        => 3,
225
								'placeholder' => esc_attr__( 'Give a Custom Amount', 'give' ),
226
							),
227
						),
228
						array(
229
							'name'		  => 'donation_options_docs',
230
							'type'        => 'docs_link',
231
							'url'		  => 'https://docs.givewp.com/donationoptions',
232
							'title'		  => esc_html__('Donation Options', 'give'),
233
						)
234
					)
235
				),
236
			) ),
237
238
			/**
239
			 * Display Options
240
			 */
241
			'form_display_options'  => apply_filters( 'give_form_display_options', array(
242
					'id'     => 'form_display_options',
243
					'title'  => esc_html__( 'Form Display', 'give' ),
244
					'fields' => apply_filters( 'give_forms_display_options_metabox_fields', array(
245
							array(
246
								'name'    => esc_html__( 'Display Options', 'give' ),
247
								'desc'    => sprintf( __( 'How would you like to display donation information for this form?', 'give' ), '#' ),
248
								'id'      => $prefix . 'payment_display',
249
								'type'    => 'radio_inline',
250
								'options' => array(
251
									'onpage' => esc_html__( 'All Fields', 'give' ),
252
									'modal'  => esc_html__( 'Modal', 'give' ),
253
									'reveal' => esc_html__( 'Reveal', 'give' ),
254
									'button' => esc_html__( 'Button', 'give' ),
255
								),
256
								'default' => 'onpage',
257
							),
258
							array(
259
								'id'         => $prefix . 'reveal_label',
260
								'name'       => esc_html__( 'Continue Button', 'give' ),
261
								'desc'       => esc_html__( 'The button label for displaying the additional payment fields.', 'give' ),
262
								'type'       => 'text_small',
263
								'attributes' => array(
264
									'placeholder' => esc_attr__( 'Donate Now', 'give' ),
265
								),
266
							),
267
							array(
268
								'id'         => $prefix . 'checkout_label',
269
								'name'       => esc_html__( 'Submit Button', 'give' ),
270
								'desc'       => esc_html__( 'The button label for completing a donation.', 'give' ),
271
								'type'       => 'text_small',
272
								'attributes' => array(
273
									'placeholder' => esc_html__( 'Donate Now', 'give' ),
274
								),
275
							),
276
							array(
277
								'name' => esc_html__( 'Default Gateway', 'give' ),
278
								'desc' => esc_html__( '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'    => esc_html__( 'Guest Donations', 'give' ),
284
								'desc'    => esc_html__( 'Do you want to require users be logged-in to make donations?', 'give' ),
285
								'id'      => $prefix . 'logged_in_only',
286
								'type'    => 'radio_inline',
287
								'default' => 'enabled',
288
								'options' => array(
289
									'enabled'  => esc_html__( 'Enabled', 'give' ),
290
									'disabled' => esc_html__( 'Disabled', 'give' ),
291
								),
292
							),
293
							array(
294
								'name'    => esc_html__( 'Registration', 'give' ),
295
								'desc'    => esc_html__( 'Display the registration and login forms in the payment section for non-logged-in users.', 'give' ),
296
								'id'      => $prefix . 'show_register_form',
297
								'type'    => 'radio',
298
								'options' => array(
299
									'none'         => esc_html__( 'None', 'give' ),
300
									'registration' => esc_html__( 'Registration', 'give' ),
301
									'login'        => esc_html__( 'Login', 'give' ),
302
									'both'         => esc_html__( 'Registration + Login', 'give' ),
303
								),
304
								'default' => 'none',
305
							),
306
							array(
307
								'name'    => esc_html__( 'Floating Labels', 'give' ),
308
								/* translators: %s: forms https://givewp.com/documentation/core/give-forms/creating-give-forms/#floating-labels */
309
								'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( 'https://givewp.com/documentation/core/give-forms/creating-give-forms/#floating-labels' ) ),
310
								'id'      => $prefix . 'form_floating_labels',
311
								'type'    => 'radio_inline',
312
								'options' => array(
313
									'global'   => esc_html__( 'Global Options', 'give' ),
314
									'enabled'  => esc_html__( 'Enabled', 'give' ),
315
									'disabled' => esc_html__( 'Disabled', 'give' ),
316
								),
317
								'default' => 'global',
318
							),
319
							array(
320
								'name'		  => 'form_display_docs',
321
								'type'        => 'docs_link',
322
								'url'		  => 'https://docs.givewp.com/formdisplay',
323
								'title'		  => esc_html__('Form Display', 'give'),
324
							),
325
						)
326
					),
327
				)
328
			),
329
330
			/**
331
			 * Donation Goals
332
			 */
333
			'donation_goal_options' => apply_filters( 'give_donation_goal_options', array(
334
				'id'     => 'donation_goal_options',
335
				'title'  => esc_html__( 'Donation Goal', 'give' ),
336
				'fields' => apply_filters( 'give_forms_donation_goal_metabox_fields', array(
337
					// Goals
338
					array(
339
						'name'        => esc_html__( 'Donation Goal', 'give' ),
340
						'description' => esc_html__( 'Do you want to set a donation goal for this form?', 'give' ),
341
						'id'          => $prefix . 'goal_option',
342
						'type'        => 'radio_inline',
343
						'default'     => 'disabled',
344
						'options'     => array(
345
							'enabled'  => esc_html__( 'Enabled', 'give' ),
346
							'disabled' => esc_html__( 'Disabled', 'give' ),
347
						),
348
					),
349
					array(
350
						'name'        => esc_html__( 'Goal Amount', 'give' ),
351
						'description' => esc_html__( 'This is the monetary goal amount you want to reach for this form.', 'give' ),
352
						'id'          => $prefix . 'set_goal',
353
						'type'        => 'text_small',
354
						'data_type'   => 'price',
355
						'attributes'  => array(
356
							'placeholder' => give_format_decimal( '0.00' ),
357
							'value'       => give_format_decimal( $goal ),
358
							'class'       => 'give-money-field',
359
						),
360
					),
361
362
					array(
363
						'name'        => esc_html__( 'Goal Format', 'give' ),
364
						'description' => esc_html__( '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' ),
365
						'id'          => $prefix . 'goal_format',
366
						'type'        => 'radio_inline',
367
						'default'     => 'amount',
368
						'options'     => array(
369
							'amount'     => esc_html__( 'Amount', 'give' ),
370
							'percentage' => esc_html__( 'Percentage', 'give' ),
371
						),
372
					),
373
					array(
374
						'name'    => esc_html__( 'Progress Bar Color', 'give' ),
375
						'desc'    => esc_html__( 'Customize the color of the goal progress bar.', 'give' ),
376
						'id'      => $prefix . 'goal_color',
377
						'type'    => 'colorpicker',
378
						'default' => '#2bc253',
379
					),
380
381
					array(
382
						'name'    => esc_html__( 'Close Form', 'give' ),
383
						'desc'    => esc_html__( 'Do you want to close the donation forms and stop accepting donations once this goal has been met?', 'give' ),
384
						'id'      => $prefix . 'close_form_when_goal_achieved',
385
						'type'    => 'radio_inline',
386
						'default' => 'disabled',
387
						'options' => array(
388
							'enabled'  => esc_html__( 'Enabled', 'give' ),
389
							'disabled' => esc_html__( 'Disabled', 'give' ),
390
						),
391
					),
392
					array(
393
						'name'       => esc_html__( 'Goal Achieved Message', 'give' ),
394
						'desc'       => esc_html__( 'Do you want to display a custom message when the goal is closed? If none is provided the default message will be displayed', 'give' ),
395
						'id'         => $prefix . 'form_goal_achieved_message',
396
						'type'       => 'textarea',
397
						'attributes' => array(
398
							'placeholder' => esc_attr__( 'Thank you to all our donors, we have met our fundraising goal.', 'give' ),
399
						),
400
					),
401
					array(
402
						'name'		  => 'donation_goal_docs',
403
						'type'        => 'docs_link',
404
						'url'		  => 'https://docs.givewp.com/donationgoal',
405
						'title'		  => esc_html__('Donation Goal', 'give'),
406
					),
407
				) ),
408
			) ),
409
410
			/**
411
			 * Content Field
412
			 */
413
			'form_content_options'  => apply_filters( 'give_forms_content_options', array(
414
				'id'     => 'form_content_options',
415
				'title'  => esc_html__( 'Form Content', 'give' ),
416
				'fields' => apply_filters( 'give_forms_content_options_metabox_fields', array(
417
418
						// Donation content.
419
						array(
420
							'name'        => esc_html__( 'Display Content', 'give' ),
421
							'description' => esc_html__( 'Do you want to add custom content to this form?', 'give' ),
422
							'id'          => $prefix . 'display_content',
423
							'type'        => 'radio_inline',
424
							'options'     => array(
425
								'enabled'  => esc_html__( 'Enabled', 'give' ),
426
								'disabled' => esc_html__( 'Disabled', 'give' ),
427
							),
428
							'default'     => 'disabled',
429
						),
430
431
						// Content placement.
432
						array(
433
							'name'        => esc_html__( 'Content Placement', 'give' ),
434
							'description' => esc_html__( 'This option controls where the content appears within the donation form.', 'give' ),
435
							'id'          => $prefix . 'content_placement',
436
							'type'        => 'radio_inline',
437
							'options'     => apply_filters( 'give_forms_content_options_select', array(
438
									'give_pre_form'  => esc_html__( 'Above fields', 'give' ),
439
									'give_post_form' => esc_html__( 'Below fields', 'give' ),
440
								)
441
							),
442
							'default'     => 'give_pre_form',
443
						),
444
						array(
445
							'name'        => esc_html__( 'Content', 'give' ),
446
							'description' => esc_html__( 'This content will display on the single give form page.', 'give' ),
447
							'id'          => $prefix . 'form_content',
448
							'type'        => 'wysiwyg',
449
						),
450
						array(
451
							'name'		  => 'form_content_docs',
452
							'type'        => 'docs_link',
453
							'url'		  => 'http://docs.givewp.com/formcontent',
454
							'title'		  => esc_html__('Form Content', 'give'),
455
						),
456
					)
457
				),
458
			) ),
459
460
			/**
461
			 * Terms & Conditions
462
			 */
463
			'form_terms_options'    => apply_filters( 'give_forms_terms_options', array(
464
				'id'     => 'form_terms_options',
465
				'title'  => esc_html__( 'Terms & Conditions', 'give' ),
466
				'fields' => apply_filters( 'give_forms_terms_options_metabox_fields', array(
467
						// Donation Option
468
						array(
469
							'name'        => esc_html__( 'Terms & Conditions', 'give' ),
470
							'description' => esc_html__( 'Do you want to require the donor to accept terms prior to being able to complete their donation?', 'give' ),
471
							'id'          => $prefix . 'terms_option',
472
							'type'        => 'radio_inline',
473
							'options'     => apply_filters( 'give_forms_content_options_select', array(
474
									'global'   => esc_html__( 'Global Options', 'give' ),
475
									'enabled'  => esc_html__( 'Customize', 'give' ),
476
									'disabled' => esc_html__( 'Disable', 'give' ),
477
								)
478
							),
479
							'default'     => 'global',
480
						),
481
						array(
482
							'id'         => $prefix . 'agree_label',
483
							'name'       => esc_html__( 'Agreement Label', 'give' ),
484
							'desc'       => esc_html__( 'The label shown next to the agree to terms check box. Add your own to customize or leave blank to use the default text placeholder.', 'give' ),
485
							'type'       => 'text',
486
							'size'       => 'regular',
487
							'attributes' => array(
488
								'placeholder' => esc_attr__( 'Agree to Terms?', 'give' ),
489
							),
490
						),
491
						array(
492
							'id'   => $prefix . 'agree_text',
493
							'name' => esc_html__( 'Agreement Text', 'give' ),
494
							'desc' => esc_html__( 'This is the actual text which the user will have to agree to in order to make a donation.', 'give' ),
495
							'type' => 'wysiwyg',
496
						),
497
						array(
498
							'name'		  => 'terms_docs',
499
							'type'        => 'docs_link',
500
							'url'		  => 'https://docs.givewp.com/terms',
501
							'title'		  => esc_html__('Terms & Conditions', 'give'),
502
						),
503
					)
504
				),
505
			) ),
506
		);
507
		/**
508
		 * Filter the metabox tabbed panel settings.
509
		 */
510
		$settings = apply_filters( 'give_metabox_form_data_settings', $settings );
511
512
		// Output.
513
		return $settings;
514
	}
515
516
	/**
517
	 * Add metabox.
518
	 *
519
	 * @since  1.8
520
	 * @return void
521
	 */
522
	public function add_meta_box() {
523
		add_meta_box(
524
			$this->get_metabox_ID(),
525
			$this->get_metabox_label(),
526
			array( $this, 'output' ),
527
			array( 'give_forms' ),
528
			'normal',
529
			'high'
530
		);
531
	}
532
533
534
	/**
535
	 * Enqueue scripts.
536
	 *
537
	 * @since  1.8
538
	 * @return void
539
	 */
540
	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...
541
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
542
543
		if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
544
			wp_enqueue_style( 'wp-color-picker' );
545
			wp_enqueue_script( 'wp-color-picker' );
546
		}
547
	}
548
549
	/**
550
	 * Get metabox id.
551
	 *
552
	 * @since  1.8
553
	 * @return string
554
	 */
555
	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...
556
		return $this->metabox_id;
557
	}
558
559
	/**
560
	 * Get metabox label.
561
	 *
562
	 * @since  1.8
563
	 * @return string
564
	 */
565
	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...
566
		return $this->metabox_label;
567
	}
568
569
570
	/**
571
	 * Get metabox tabs.
572
	 *
573
	 * @since  1.8
574
	 * @return array
575
	 */
576
	public function get_tabs() {
577
		$tabs = array();
578
579
		if ( ! empty( $this->settings ) ) {
580
			foreach ( $this->settings as $setting ) {
581
				if ( ! isset( $setting['id'] ) || ! isset( $setting['title'] ) ) {
582
					continue;
583
				}
584
585
				$tabs[] = array(
586
					'id'    => $setting['id'],
587
					'label' => $setting['title'],
588
				);
589
			}
590
		}
591
592
		return $tabs;
593
	}
594
595
	/**
596
	 * Output metabox settings.
597
	 *
598
	 * @since  1.8
599
	 * @return void
600
	 */
601
	public function output() {
602
		// Bailout.
603
		if ( $form_data_tabs = $this->get_tabs() ) {
604
			wp_nonce_field( 'give_save_form_meta', 'give_form_meta_nonce' );
605
			?>
606
			<div class="give-metabox-panel-wrap">
607
				<ul class="give-form-data-tabs give-metabox-tabs">
608
					<?php foreach ( $form_data_tabs as $index => $form_data_tab ) : ?>
609
						<li class="<?php echo "{$form_data_tab['id']}_tab" . ( ! $index ? ' active' : '' ); ?>">
610
							<a href="#<?php echo $form_data_tab['id']; ?>"><?php echo $form_data_tab['label']; ?></a>
611
						</li>
612
					<?php endforeach; ?>
613
				</ul>
614
615
				<?php $show_first_tab_content = true; ?>
616
				<?php foreach ( $this->settings as $setting ) : ?>
617
					<?php do_action( "give_before_{$setting['id']}_settings" ); ?>
618
619
					<div id="<?php echo $setting['id']; ?>" class="panel give_options_panel <?php echo( $show_first_tab_content ? '' : 'give-hidden' );$show_first_tab_content = false; ?>">
620
						<?php if ( ! empty( $setting['fields'] ) ) : ?>
621
							<?php foreach ( $setting['fields'] as $field ) : ?>
622
								<?php give_render_field( $field ); ?>
623
							<?php endforeach; ?>
624
						<?php endif; ?>
625
					</div>
626
627
					<?php do_action( "give_after_{$setting['id']}_settings" ); ?>
628
				<?php endforeach; ?>
629
			</div>
630
			<?php
631
		}
632
	}
633
634
	/**
635
	 * CMB2 settings loader.
636
	 *
637
	 * @since  1.8
638
	 * @return array
639
	 */
640
	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...
641
		$all_cmb2_settings   = apply_filters( 'cmb2_meta_boxes', array() );
642
		$give_forms_settings = $all_cmb2_settings;
643
644
		// Filter settings: Use only give forms related settings.
645
		foreach ( $all_cmb2_settings as $index => $setting ) {
646
			if ( ! in_array( 'give_forms', $setting['object_types'] ) ) {
647
				unset( $give_forms_settings[ $index ] );
648
			}
649
		}
650
651
		return $give_forms_settings;
652
653
	}
654
655
	/**
656
	 * Check if we're saving, the trigger an action based on the post type.
657
	 *
658
	 * @since  1.8
659
	 *
660
	 * @param  int    $post_id
661
	 * @param  object $post
662
	 *
663
	 * @return void
664
	 */
665
	public function save( $post_id, $post ) {
666
667
		// $post_id and $post are required.
668
		if ( empty( $post_id ) || empty( $post ) ) {
669
			return;
670
		}
671
672
		// Don't save meta boxes for revisions or autosaves.
673
		if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
674
			return;
675
		}
676
677
		// Check the nonce.
678
		if ( empty( $_POST['give_form_meta_nonce'] ) || ! wp_verify_nonce( $_POST['give_form_meta_nonce'], 'give_save_form_meta' ) ) {
679
			return;
680
		}
681
682
		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
683
		if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
684
			return;
685
		}
686
687
		// Check user has permission to edit.
688
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
689
			return;
690
		}
691
692
		// Fire action before saving form meta.
693
		do_action( 'give_pre_process_give_forms_meta', $post_id, $post );
694
695
		/**
696
		 * Filter the meta key to save.
697
		 * Third party addon developer can remove there meta keys from this array to handle saving data on there own.
698
		 */
699
		$form_meta_keys = apply_filters( 'give_process_form_meta_keys', $this->get_meta_keys_from_settings() );
700
701
		// Save form meta data.
702
		if ( ! empty( $form_meta_keys ) ) {
703
			foreach ( $form_meta_keys as $form_meta_key ) {
704
				if ( isset( $_POST[ $form_meta_key ] ) ) {
705
					if ( $field_type = $this->get_field_type( $form_meta_key ) ) {
706
						switch ( $field_type ) {
707
							case 'wysiwyg':
708
								$form_meta_value = wp_kses_post( $_POST[ $form_meta_key ] );
709
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
710
								break;
711
712
							case 'group':
713
								$form_meta_value = array();
714
715
								foreach ( $_POST[ $form_meta_key ] as $index => $group ) {
716
717
									// Do not save template input field values.
718
									if ( '{{row-count-placeholder}}' === $index ) {
719
										continue;
720
									}
721
722
									$group_meta_value = array();
723
									foreach ( $group as $field_id => $field_value ) {
724
										switch ( $this->get_field_type( $field_id, $form_meta_key ) ) {
725
											case 'wysiwyg':
726
												$group_meta_value[ $field_id ] = wp_kses_post( $field_value );
727
												break;
728
729
											default:
730
												$group_meta_value[ $field_id ] = give_clean( $field_value );
731
										}
732
									}
733
734
									if ( ! empty( $group_meta_value ) ) {
735
										$form_meta_value[ $index ] = $group_meta_value;
736
									}
737
								}
738
739
740
								// Arrange repeater field keys in order.
741
								$form_meta_value = array_values( $form_meta_value );
742
743
								// Save data.
744
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
745
								break;
746
747
							default:
748
								$form_meta_value = give_clean( $_POST[ $form_meta_key ] );
749
750
								// Save data.
751
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
752
						}
753
754
						// Fire after saving form meta key.
755
						do_action( "give_save_{$form_meta_key}", $form_meta_key, $form_meta_value, $post_id, $post );
756
					}
757
				}
758
			}
759
		}
760
761
		// Fire action after saving form meta.
762
		do_action( 'give_post_process_give_forms_meta', $post_id, $post );
763
	}
764
765
766
	/**
767
	 * Get all setting field ids.
768
	 *
769
	 * @since  1.8
770
	 * @return array
771
	 */
772
	private function get_meta_keys_from_settings() {
773
		$meta_keys = array();
774
		foreach ( $this->settings as $setting ) {
775
			if ( ! empty( $setting['fields'] ) ) {
776
				foreach ( $setting['fields'] as $field ) {
777
					if( ! array_key_exists( 'id', $field ) ) {
778
						continue;
779
					}
780
781
					$meta_keys[] = $field['id'];
782
				}
783
			}
784
		}
785
786
		return $meta_keys;
787
	}
788
789
790
	/**
791
	 * Get field type.
792
	 *
793
	 * @since  1.8
794
	 *
795
	 * @param  string $field_id
796
	 * @param  string $group_id
797
	 *
798
	 * @return string
799
	 */
800
	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...
801
		$settings = $this->get_setting_field( $field_id, $group_id );
802
		$type     = array_key_exists( 'type', $this->get_setting_field( $field_id, $group_id ) )
803
			? $settings['type']
804
			: '';
805
806
		return $type;
807
	}
808
809
	/**
810
	 * Get setting field.
811
	 *
812
	 * @since  1.8
813
	 *
814
	 * @param  string $field_id
815
	 * @param  string $group_id Get sub field from group.
816
	 *
817
	 * @return array
818
	 */
819
	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...
820
		$setting_field = array();
821
822
		$_field_id = $field_id;
823
		$field_id  = empty( $group_id ) ? $field_id : $group_id;
824
825
		if ( ! empty( $this->settings ) ) {
826
			foreach ( $this->settings as $setting ) {
827
				if ( ! empty( $setting['fields'] ) ) {
828
					foreach ( $setting['fields'] as $field ) {
829
						if ( array_key_exists( 'id', $field ) && $field['id'] === $field_id ) {
830
							$setting_field = $field;
831
						}
832
					}
833
				}
834
			}
835
		}
836
837
838
		// Get field from group.
839
		if ( ! empty( $group_id ) ) {
840
			foreach ( $setting_field['fields'] as $field ) {
841
				if ( array_key_exists( 'id', $field ) && $field['id'] === $_field_id ) {
842
					$setting_field = $field;
843
				}
844
			}
845
		}
846
847
		return $setting_field;
848
	}
849
850
851
	/**
852
	 * Add offline donations setting tab to donation form options metabox.
853
	 *
854
	 * @since  1.8
855
	 *
856
	 * @param  array $settings List of form settings.
857
	 *
858
	 * @return mixed
859
	 */
860
	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...
861
		if ( give_is_gateway_active( 'offline' ) ) {
862
			$settings['offline_donations_options'] = apply_filters( 'give_forms_offline_donations_options', array(
863
				'id'     => 'offline_donations_options',
864
				'title'  => esc_html__( 'Offline Donations', 'give' ),
865
				'fields' => apply_filters( 'give_forms_offline_donations_metabox_fields', array() ),
866
			) );
867
		}
868
869
		return $settings;
870
	}
871
}
872
873
new Give_MetaBox_Form_Data();
874
875