Completed
Push — subtab-metabox ( cb33d7 )
by Ravinder
19:29
created

Give_MetaBox_Form_Data   D

Complexity

Total Complexity 86

Size/Duplication

Total Lines 915
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 915
rs 4.4444
c 0
b 0
f 0
wmc 86
lcom 2
cbo 0

16 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
D get_tabs() 0 34 9
C output() 0 57 17
A has_sub_tab() 0 8 2
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 933.

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