Completed
Push — master ( 1e8296...316c10 )
by Devin
37:55 queued 17:58
created

Give_MetaBox_Form_Data   D

Complexity

Total Complexity 112

Size/Duplication

Total Lines 1079
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 1079
rs 4.4169
c 0
b 0
f 0
wmc 112
lcom 2
cbo 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
A setup() 0 3 1
B get_settings() 0 441 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
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
D save() 0 109 25
A get_field_id() 0 10 2
A get_fields_id() 0 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() 0 14 5
A get_sub_field() 0 14 4
C get_setting_field() 0 29 11
A add_offline_donations_setting_tab() 0 12 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 1097.

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
				'icon-html' => '<span class="give-icon give-icon-heart"></span>',
114
				'fields'    => apply_filters( 'give_forms_donation_form_metabox_fields', array(
115
					// Donation Option
116
					array(
117
						'name'        => esc_html__( 'Donation Option', 'give' ),
118
						'description' => esc_html__( 'Do you want this form to have one set donation price or multiple levels (for example, $10, $20, $50)?', 'give' ),
119
						'id'          => $prefix . 'price_option',
120
						'type'        => 'radio_inline',
121
						'default'     => 'set',
122
						'options'     => apply_filters( 'give_forms_price_options', array(
123
							'set'   => esc_html__( 'Set Donation', 'give' ),
124
							'multi' => esc_html__( 'Multi-level Donation', 'give' ),
125
						) ),
126
					),
127
					array(
128
						'name'        => esc_html__( 'Set Donation', 'give' ),
129
						'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' ),
130
						'id'          => $prefix . 'set_price',
131
						'type'        => 'text_small',
132
						'data_type'   => 'price',
133
						'attributes'  => array(
134
							'placeholder' => give_format_decimal( '1.00' ),
135
							'value'       => give_format_decimal( $price ),
136
							'class'       => 'give-money-field',
137
						),
138
					),
139
					// Display Style
140
					array(
141
						'name'        => esc_html__( 'Display Style', 'give' ),
142
						'description' => esc_html__( 'Set how the donations levels will display on the form.', 'give' ),
143
						'id'          => $prefix . 'display_style',
144
						'type'        => 'radio_inline',
145
						'default'     => 'buttons',
146
						'options'     => array(
147
							'buttons'  => esc_html__( 'Buttons', 'give' ),
148
							'radios'   => esc_html__( 'Radios', 'give' ),
149
							'dropdown' => esc_html__( 'Dropdown', 'give' ),
150
						),
151
					),
152
					// Custom Amount
153
					array(
154
						'name'        => esc_html__( 'Custom Amount', 'give' ),
155
						'description' => esc_html__( 'Do you want the user to be able to input their own donation amount?', 'give' ),
156
						'id'          => $prefix . 'custom_amount',
157
						'type'        => 'radio_inline',
158
						'default'     => 'disabled',
159
						'options'     => array(
160
							'enabled'  => esc_html__( 'Enabled', 'give' ),
161
							'disabled' => esc_html__( 'Disabled', 'give' ),
162
						),
163
					),
164
					array(
165
						'name'        => esc_html__( 'Minimum Amount', 'give' ),
166
						'description' => esc_html__( 'Enter the minimum custom donation amount.', 'give' ),
167
						'id'          => $prefix . 'custom_amount_minimum',
168
						'type'        => 'text_small',
169
						'data_type'   => 'price',
170
						'attributes'  => array(
171
							'placeholder' => give_format_decimal( '1.00' ),
172
							'value'       => give_format_decimal( $custom_amount_minimum ),
173
							'class'       => 'give-money-field',
174
						),
175
					),
176
					array(
177
						'name'        => esc_html__( 'Custom Amount Text', 'give' ),
178
						'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' ),
179
						'id'          => $prefix . 'custom_amount_text',
180
						'type'        => 'text_medium',
181
						'attributes'  => array(
182
							'rows'        => 3,
183
							'placeholder' => esc_attr__( 'Give a Custom Amount', 'give' ),
184
						),
185
					),
186
					// Donation Levels: Repeatable CMB2 Group
187
					array(
188
						'id'      => $prefix . 'donation_levels',
189
						'type'    => 'group',
190
						'options' => array(
191
							'add_button'    => esc_html__( 'Add Level', 'give' ),
192
							'header_title'  => esc_html__( 'Donation Level', 'give' ),
193
							'remove_button' => '<span class="dashicons dashicons-no"></span>',
194
						),
195
						// Fields array works the same, except id's only need to be unique for this group.
196
						// Prefix is not needed.
197
						'fields'  => apply_filters( 'give_donation_levels_table_row', array(
198
							array(
199
								'name' => esc_html__( 'ID', 'give' ),
200
								'id'   => $prefix . 'id',
201
								'type' => 'levels_id',
202
							),
203
							array(
204
								'name'       => esc_html__( 'Amount', 'give' ),
205
								'id'         => $prefix . 'amount',
206
								'type'       => 'text_small',
207
								'data_type'  => 'price',
208
								'attributes' => array(
209
									'placeholder' => give_format_decimal( '1.00' ),
210
									'class'       => 'give-money-field',
211
								),
212
							),
213
							array(
214
								'name'       => esc_html__( 'Text', 'give' ),
215
								'id'         => $prefix . 'text',
216
								'type'       => 'text',
217
								'attributes' => array(
218
									'placeholder' => esc_html__( 'Donation Level', 'give' ),
219
									'class'       => 'give-multilevel-text-field',
220
								),
221
							),
222
							array(
223
								'name' => esc_html__( 'Default', 'give' ),
224
								'id'   => $prefix . 'default',
225
								'type' => 'give_default_radio_inline',
226
							),
227
						) ),
228
					),
229
					array(
230
						'name'  => 'donation_options_docs',
231
						'type'  => 'docs_link',
232
						'url'   => 'http://docs.givewp.com/form-donation-options',
233
						'title' => esc_html__( 'Donation Options', 'give' ),
234
					),
235
				),
236
					$post_id
237
				),
238
			) ),
239
240
			/**
241
			 * Display Options
242
			 */
243
			'form_display_options'  => apply_filters( 'give_form_display_options', array(
244
					'id'        => 'form_display_options',
245
					'title'     => esc_html__( 'Form Display', 'give' ),
246
					'icon-html' => '<span class="give-icon give-icon-display"></span>',
247
					'fields'    => apply_filters( 'give_forms_display_options_metabox_fields', array(
248
						array(
249
							'name'    => esc_html__( 'Display Options', 'give' ),
250
							'desc'    => sprintf( __( 'How would you like to display donation information for this form?', 'give' ), '#' ),
251
							'id'      => $prefix . 'payment_display',
252
							'type'    => 'radio_inline',
253
							'options' => array(
254
								'onpage' => esc_html__( 'All Fields', 'give' ),
255
								'modal'  => esc_html__( 'Modal', 'give' ),
256
								'reveal' => esc_html__( 'Reveal', 'give' ),
257
								'button' => esc_html__( 'Button', 'give' ),
258
							),
259
							'default' => 'onpage',
260
						),
261
						array(
262
							'id'         => $prefix . 'reveal_label',
263
							'name'       => esc_html__( 'Continue Button', 'give' ),
264
							'desc'       => esc_html__( 'The button label for displaying the additional payment fields.', 'give' ),
265
							'type'       => 'text_small',
266
							'attributes' => array(
267
								'placeholder' => esc_attr__( 'Donate Now', 'give' ),
268
							),
269
						),
270
						array(
271
							'id'         => $prefix . 'checkout_label',
272
							'name'       => esc_html__( 'Submit Button', 'give' ),
273
							'desc'       => esc_html__( 'The button label for completing a donation.', 'give' ),
274
							'type'       => 'text_small',
275
							'attributes' => array(
276
								'placeholder' => esc_html__( 'Donate Now', 'give' ),
277
							),
278
						),
279
						array(
280
							'name' => esc_html__( 'Default Gateway', 'give' ),
281
							'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' ),
282
							'id'   => $prefix . 'default_gateway',
283
							'type' => 'default_gateway',
284
						),
285
						array(
286
							'name'    => esc_html__( 'Guest Donations', 'give' ),
287
							'desc'    => esc_html__( 'Do you want to allow non-logged-in users to make donations?', 'give' ),
288
							'id'      => $prefix . 'logged_in_only',
289
							'type'    => 'radio_inline',
290
							'default' => 'enabled',
291
							'options' => array(
292
								'enabled'  => esc_html__( 'Enabled', 'give' ),
293
								'disabled' => esc_html__( 'Disabled', 'give' ),
294
							),
295
						),
296
						array(
297
							'name'    => esc_html__( 'Registration', 'give' ),
298
							'desc'    => esc_html__( 'Display the registration and login forms in the payment section for non-logged-in users.', 'give' ),
299
							'id'      => $prefix . 'show_register_form',
300
							'type'    => 'radio',
301
							'options' => array(
302
								'none'         => esc_html__( 'None', 'give' ),
303
								'registration' => esc_html__( 'Registration', 'give' ),
304
								'login'        => esc_html__( 'Login', 'give' ),
305
								'both'         => esc_html__( 'Registration + Login', 'give' ),
306
							),
307
							'default' => 'none',
308
						),
309
						array(
310
							'name'    => esc_html__( 'Floating Labels', 'give' ),
311
							/* translators: %s: forms http://docs.givewp.com/form-floating-labels */
312
							'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' ) ),
313
							'id'      => $prefix . 'form_floating_labels',
314
							'type'    => 'radio_inline',
315
							'options' => array(
316
								'global'   => esc_html__( 'Global Option', 'give' ),
317
								'enabled'  => esc_html__( 'Enabled', 'give' ),
318
								'disabled' => esc_html__( 'Disabled', 'give' ),
319
							),
320
							'default' => 'global',
321
						),
322
						array(
323
							'name'  => 'form_display_docs',
324
							'type'  => 'docs_link',
325
							'url'   => 'http://docs.givewp.com/form-display-options',
326
							'title' => esc_html__( 'Form Display', 'give' ),
327
						),
328
					),
329
						$post_id
330
					),
331
				)
332
			),
333
334
			/**
335
			 * Donation Goals
336
			 */
337
			'donation_goal_options' => apply_filters( 'give_donation_goal_options', array(
338
				'id'        => 'donation_goal_options',
339
				'title'     => esc_html__( 'Donation Goal', 'give' ),
340
				'icon-html' => '<span class="give-icon give-icon-target"></span>',
341
				'fields'    => apply_filters( 'give_forms_donation_goal_metabox_fields', array(
342
					// Goals
343
					array(
344
						'name'        => esc_html__( 'Donation Goal', 'give' ),
345
						'description' => esc_html__( 'Do you want to set a donation goal for this form?', 'give' ),
346
						'id'          => $prefix . 'goal_option',
347
						'type'        => 'radio_inline',
348
						'default'     => 'disabled',
349
						'options'     => array(
350
							'enabled'  => esc_html__( 'Enabled', 'give' ),
351
							'disabled' => esc_html__( 'Disabled', 'give' ),
352
						),
353
					),
354
					array(
355
						'name'        => esc_html__( 'Goal Amount', 'give' ),
356
						'description' => esc_html__( 'This is the monetary goal amount you want to reach for this form.', 'give' ),
357
						'id'          => $prefix . 'set_goal',
358
						'type'        => 'text_small',
359
						'data_type'   => 'price',
360
						'attributes'  => array(
361
							'placeholder' => give_format_decimal( '0.00' ),
362
							'value'       => give_format_decimal( $goal ),
363
							'class'       => 'give-money-field',
364
						),
365
					),
366
367
					array(
368
						'name'        => esc_html__( 'Goal Format', 'give' ),
369
						'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' ),
370
						'id'          => $prefix . 'goal_format',
371
						'type'        => 'radio_inline',
372
						'default'     => 'amount',
373
						'options'     => array(
374
							'amount'     => esc_html__( 'Amount', 'give' ),
375
							'percentage' => esc_html__( 'Percentage', 'give' ),
376
						),
377
					),
378
					array(
379
						'name'    => esc_html__( 'Progress Bar Color', 'give' ),
380
						'desc'    => esc_html__( 'Customize the color of the goal progress bar.', 'give' ),
381
						'id'      => $prefix . 'goal_color',
382
						'type'    => 'colorpicker',
383
						'default' => '#2bc253',
384
					),
385
386
					array(
387
						'name'    => esc_html__( 'Close Form', 'give' ),
388
						'desc'    => esc_html__( 'Do you want to close the donation forms and stop accepting donations once this goal has been met?', 'give' ),
389
						'id'      => $prefix . 'close_form_when_goal_achieved',
390
						'type'    => 'radio_inline',
391
						'default' => 'disabled',
392
						'options' => array(
393
							'enabled'  => esc_html__( 'Enabled', 'give' ),
394
							'disabled' => esc_html__( 'Disabled', 'give' ),
395
						),
396
					),
397
					array(
398
						'name'       => esc_html__( 'Goal Achieved Message', 'give' ),
399
						'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' ),
400
						'id'         => $prefix . 'form_goal_achieved_message',
401
						'type'       => 'textarea',
402
						'attributes' => array(
403
							'placeholder' => esc_attr__( 'Thank you to all our donors, we have met our fundraising goal.', 'give' ),
404
						),
405
					),
406
					array(
407
						'name'  => 'donation_goal_docs',
408
						'type'  => 'docs_link',
409
						'url'   => 'http://docs.givewp.com/form-donation-goal',
410
						'title' => esc_html__( 'Donation Goal', 'give' ),
411
					),
412
				),
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'     => esc_html__( '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'        => esc_html__( 'Display Content', 'give' ),
429
						'description' => esc_html__( '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'  => esc_html__( 'Enabled', 'give' ),
434
							'disabled' => esc_html__( 'Disabled', 'give' ),
435
						),
436
						'default'     => 'disabled',
437
					),
438
439
					// Content placement.
440
					array(
441
						'name'        => esc_html__( 'Content Placement', 'give' ),
442
						'description' => esc_html__( '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'  => esc_html__( 'Above fields', 'give' ),
447
								'give_post_form' => esc_html__( 'Below fields', 'give' ),
448
							)
449
						),
450
						'default'     => 'give_pre_form',
451
					),
452
					array(
453
						'name'        => esc_html__( 'Content', 'give' ),
454
						'description' => esc_html__( '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' => esc_html__( 'Form Content', 'give' ),
463
					),
464
				),
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'     => esc_html__( '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'        => esc_html__( 'Terms & Conditions', 'give' ),
480
						'description' => esc_html__( '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'   => esc_html__( 'Global Option', 'give' ),
485
								'enabled'  => esc_html__( 'Customize', 'give' ),
486
								'disabled' => esc_html__( 'Disable', 'give' ),
487
							)
488
						),
489
						'default'     => 'global',
490
					),
491
					array(
492
						'id'         => $prefix . 'agree_label',
493
						'name'       => esc_html__( 'Agreement Label', 'give' ),
494
						'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' ),
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' => esc_html__( 'Agreement Text', 'give' ),
504
						'desc' => esc_html__( 'This is the actual text which the user will have to agree to in order to make a donation.', 'give' ),
505
						'type' => 'wysiwyg',
506
					),
507
					array(
508
						'name'  => 'terms_docs',
509
						'type'  => 'docs_link',
510
						'url'   => 'http://docs.givewp.com/form-terms',
511
						'title' => esc_html__( 'Terms & Conditions', 'give' ),
512
					),
513
				),
514
					$post_id
515
				),
516
			) ),
517
		);
518
519
520
		/**
521
		 * Filter the metabox tabbed panel settings.
522
		 */
523
		$settings = apply_filters( 'give_metabox_form_data_settings', $settings, $post_id );
524
525
		// Output.
526
		return $settings;
527
	}
528
529
	/**
530
	 * Add metabox.
531
	 *
532
	 * @since  1.8
533
	 * @return void
534
	 */
535
	public function add_meta_box() {
536
		add_meta_box(
537
			$this->get_metabox_ID(),
538
			$this->get_metabox_label(),
539
			array( $this, 'output' ),
540
			array( 'give_forms' ),
541
			'normal',
542
			'high'
543
		);
544
	}
545
546
547
	/**
548
	 * Enqueue scripts.
549
	 *
550
	 * @since  1.8
551
	 * @return void
552
	 */
553
	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...
554
		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...
555
556
		if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
557
			wp_enqueue_style( 'wp-color-picker' );
558
			wp_enqueue_script( 'wp-color-picker' );
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...
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' : '' ); ?>">
641
							<a href="#<?php echo $form_data_tab['id']; ?>">
642
								<?php if ( ! empty( $form_data_tab['icon-html'] ) ) : ?>
643
									<?php echo $form_data_tab['icon-html']; ?>
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>
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"; ?>">
653
											<a href="#<?php echo $sub_tab['id']; ?>">
654
												<?php if ( ! empty( $sub_tab['icon-html'] ) ) : ?>
655
													<?php echo $sub_tab['icon-html']; ?>
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>
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']; ?>"
675
							 class="panel give_options_panel<?php echo( $show_first_tab_content ? '' : ' give-hidden' );
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: ?>
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']; ?>"
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' ) ) {
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 ) {
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
					if ( $field_type = $this->get_field_type( $form_meta_key ) ) {
807
						switch ( $field_type ) {
808
							case 'textarea':
809
							case 'wysiwyg':
810
								$form_meta_value = wp_kses_post( $_POST[ $form_meta_key ] );
811
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
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...
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
841
842
								// Arrange repeater field keys in order.
843
								$form_meta_value = array_values( $form_meta_value );
844
845
								// Save data.
846
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
847
								break;
848
849
							default:
850
								$form_meta_value = give_clean( $_POST[ $form_meta_key ] );
851
852
								// Save data.
853
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
854
						}
855
856
						// Fire after saving form meta key.
857
						do_action( "give_save_{$form_meta_key}", $form_meta_key, $form_meta_value, $post_id, $post );
858
					}
859
				}
860
			}
861
		}
862
863
		// Fire action after saving form meta.
864
		do_action( 'give_post_process_give_forms_meta', $post_id, $post );
865
	}
866
867
868
	/**
869
	 * Get field ID.
870
	 *
871
	 * @since 1.8
872
	 *
873
	 * @param array $field
874
	 *
875
	 * @return string
876
	 */
877
	private function get_field_id( $field ) {
878
		$field_id = '';
879
880
		if ( array_key_exists( 'id', $field ) ) {
881
			$field_id = $field['id'];
882
883
		}
884
885
		return $field_id;
886
	}
887
888
	/**
889
	 * Get fields ID.
890
	 *
891
	 * @since 1.8
892
	 *
893
	 * @param $setting
894
	 *
895
	 * @return array
896
	 */
897
	private function get_fields_id( $setting ) {
898
		$meta_keys = array();
899
900
		if ( ! empty( $setting ) ) {
901
			foreach ( $setting['fields'] as $field ) {
902
				if ( $field_id = $this->get_field_id( $field ) ) {
903
					$meta_keys[] = $field_id;
904
				}
905
			}
906
		}
907
908
		return $meta_keys;
909
	}
910
911
	/**
912
	 * Get sub fields ID.
913
	 *
914
	 * @since 1.8
915
	 *
916
	 * @param $setting
917
	 *
918
	 * @return array
919
	 */
920
	private function get_sub_fields_id( $setting ) {
921
		$meta_keys = array();
922
923
		if ( $this->has_sub_tab( $setting ) && ! empty( $setting['sub-fields'] ) ) {
924
			foreach ( $setting['sub-fields'] as $fields ) {
925
				if ( ! empty( $fields['fields'] ) ) {
926
					foreach ( $fields['fields'] as $field ) {
927
						if ( $field_id = $this->get_field_id( $field ) ) {
928
							$meta_keys[] = $field_id;
929
						}
930
					}
931
				}
932
			}
933
		}
934
935
		return $meta_keys;
936
	}
937
938
939
	/**
940
	 * Get all setting field ids.
941
	 *
942
	 * @since  1.8
943
	 * @return array
944
	 */
945
	private function get_meta_keys_from_settings() {
946
		$meta_keys = array();
947
948
		foreach ( $this->settings as $setting ) {
949
			if ( $this->has_sub_tab( $setting ) ) {
950
				$meta_key = $this->get_sub_fields_id( $setting );
951
			} else {
952
				$meta_key = $this->get_fields_id( $setting );
953
			}
954
955
			$meta_keys = array_merge( $meta_keys, $meta_key );
956
		}
957
958
		return $meta_keys;
959
	}
960
961
962
	/**
963
	 * Get field type.
964
	 *
965
	 * @since  1.8
966
	 *
967
	 * @param  string $field_id
968
	 * @param  string $group_id
969
	 *
970
	 * @return string
971
	 */
972
	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...
973
		$field = $this->get_setting_field( $field_id, $group_id );
974
975
		$type = array_key_exists( 'type', $field )
976
			? $field['type']
977
			: '';
978
979
		return $type;
980
	}
981
982
983
	/**
984
	 * Get Field
985
	 *
986
	 * @since 1.8
987
	 *
988
	 * @param array  $setting
989
	 * @param string $field_id
990
	 *
991
	 * @return array
992
	 */
993
	private function get_field( $setting, $field_id ) {
994
		$setting_field = array();
995
996
		if ( ! empty( $setting['fields'] ) ) {
997
			foreach ( $setting['fields'] as $field ) {
998
				if ( array_key_exists( 'id', $field ) && $field['id'] === $field_id ) {
999
					$setting_field = $field;
1000
					break;
1001
				}
1002
			}
1003
		}
1004
1005
		return $setting_field;
1006
	}
1007
1008
	/**
1009
	 * Get Sub Field
1010
	 *
1011
	 * @since 1.8
1012
	 *
1013
	 * @param array  $setting
1014
	 * @param string $field_id
1015
	 *
1016
	 * @return array
1017
	 */
1018
	private function get_sub_field( $setting, $field_id ) {
1019
		$setting_field = array();
1020
1021
		if ( ! empty( $setting['sub-fields'] ) ) {
1022
			foreach ( $setting['sub-fields'] as $fields ) {
1023
				if ( $field = $this->get_field( $fields, $field_id ) ) {
1024
					$setting_field = $field;
1025
					break;
1026
				}
1027
			}
1028
		}
1029
1030
		return $setting_field;
1031
	}
1032
1033
	/**
1034
	 * Get setting field.
1035
	 *
1036
	 * @since  1.8
1037
	 *
1038
	 * @param  string $field_id
1039
	 * @param  string $group_id Get sub field from group.
1040
	 *
1041
	 * @return array
1042
	 */
1043
	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...
1044
		$setting_field = array();
1045
1046
		$_field_id = $field_id;
1047
		$field_id  = empty( $group_id ) ? $field_id : $group_id;
1048
1049
		if ( ! empty( $this->settings ) ) {
1050
			foreach ( $this->settings as $setting ) {
1051
				if (
1052
					( $this->has_sub_tab( $setting ) && ( $setting_field = $this->get_sub_field( $setting, $field_id ) ) )
1053
					|| ( $setting_field = $this->get_field( $setting, $field_id ) )
1054
				) {
1055
					break;
1056
				}
1057
			}
1058
		}
1059
1060
1061
		// Get field from group.
1062
		if ( ! empty( $group_id ) ) {
1063
			foreach ( $setting_field['fields'] as $field ) {
1064
				if ( array_key_exists( 'id', $field ) && $field['id'] === $_field_id ) {
1065
					$setting_field = $field;
1066
				}
1067
			}
1068
		}
1069
1070
		return $setting_field;
1071
	}
1072
1073
1074
	/**
1075
	 * Add offline donations setting tab to donation form options metabox.
1076
	 *
1077
	 * @since  1.8
1078
	 *
1079
	 * @param  array $settings List of form settings.
1080
	 *
1081
	 * @return mixed
1082
	 */
1083
	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...
1084
		if ( give_is_gateway_active( 'offline' ) ) {
1085
			$settings['offline_donations_options'] = apply_filters( 'give_forms_offline_donations_options', array(
1086
				'id'        => 'offline_donations_options',
1087
				'title'     => esc_html__( 'Offline Donations', 'give' ),
1088
				'icon-html' => '<span class="give-icon give-icon-purse"></span>',
1089
				'fields'    => apply_filters( 'give_forms_offline_donations_metabox_fields', array() ),
1090
			) );
1091
		}
1092
1093
		return $settings;
1094
	}
1095
}
1096
1097
new Give_MetaBox_Form_Data();
1098
1099