Completed
Push — issues/611 ( 8b97ee...19900c )
by Ravinder
18:30
created

Give_MetaBox_Form_Data::get_tabs()   D

Complexity

Conditions 9
Paths 2

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 20
nc 2
nop 0
dl 0
loc 34
rs 4.909
c 0
b 0
f 0
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 1053.

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' );
646
						$show_first_tab_content = false; ?>">
647
							<?php if ( ! empty( $setting['fields'] ) ) : ?>
648
								<?php foreach ( $setting['fields'] as $field ) : ?>
649
									<?php give_render_field( $field ); ?>
650
								<?php endforeach; ?>
651
							<?php endif; ?>
652
						</div>
653
654
						<?php do_action( "give_after_{$setting['id']}_settings" ); ?>
655
					<?php else: ?>
656
						<?php if ( $this->has_sub_tab( $setting ) ) : ?>
657
							<?php if ( ! empty( $setting['sub-fields'] ) ) : ?>
658
								<?php foreach ( $setting['sub-fields'] as $index => $sub_fields ) : ?>
659
									<div id="<?php echo $sub_fields['id']; ?>" class="panel give_options_panel give-hidden">
660
										<?php if ( ! empty( $sub_fields['fields'] ) ) : ?>
661
											<?php foreach ( $sub_fields['fields'] as $sub_field ) : ?>
662
												<?php give_render_field( $sub_field ); ?>
663
											<?php endforeach; ?>
664
										<?php endif; ?>
665
									</div>
666
								<?php endforeach; ?>
667
							<?php endif; ?>
668
						<?php endif; ?>
669
					<?php endif; ?>
670
				<?php endforeach; ?>
671
			</div>
672
			<?php
673
		}
674
	}
675
676
677
	/**
678
	 * Check if setting field has sub tabs/fields
679
	 *
680
	 * @since 1.8
681
	 *
682
	 * @param $field_setting
683
	 *
684
	 * @return bool
685
	 */
686
	private function has_sub_tab( $field_setting ) {
687
		$has_sub_tab = false;
688
		if ( array_key_exists( 'sub-fields', $field_setting ) ) {
689
			$has_sub_tab = true;
690
		}
691
692
		return $has_sub_tab;
693
	}
694
695
	/**
696
	 * CMB2 settings loader.
697
	 *
698
	 * @since  1.8
699
	 * @return array
700
	 */
701
	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...
702
		$all_cmb2_settings   = apply_filters( 'cmb2_meta_boxes', array() );
703
		$give_forms_settings = $all_cmb2_settings;
704
705
		// Filter settings: Use only give forms related settings.
706
		foreach ( $all_cmb2_settings as $index => $setting ) {
707
			if ( ! in_array( 'give_forms', $setting['object_types'] ) ) {
708
				unset( $give_forms_settings[ $index ] );
709
			}
710
		}
711
712
		return $give_forms_settings;
713
714
	}
715
716
	/**
717
	 * Check if we're saving, the trigger an action based on the post type.
718
	 *
719
	 * @since  1.8
720
	 *
721
	 * @param  int    $post_id
722
	 * @param  object $post
723
	 *
724
	 * @return void
725
	 */
726
	public function save( $post_id, $post ) {
727
728
		// $post_id and $post are required.
729
		if ( empty( $post_id ) || empty( $post ) ) {
730
			return;
731
		}
732
733
		// Don't save meta boxes for revisions or autosaves.
734
		if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
735
			return;
736
		}
737
738
		// Check the nonce.
739
		if ( empty( $_POST['give_form_meta_nonce'] ) || ! wp_verify_nonce( $_POST['give_form_meta_nonce'], 'give_save_form_meta' ) ) {
740
			return;
741
		}
742
743
		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
744
		if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
745
			return;
746
		}
747
748
		// Check user has permission to edit.
749
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
750
			return;
751
		}
752
753
		// Fire action before saving form meta.
754
		do_action( 'give_pre_process_give_forms_meta', $post_id, $post );
755
756
		/**
757
		 * Filter the meta key to save.
758
		 * Third party addon developer can remove there meta keys from this array to handle saving data on there own.
759
		 */
760
		$form_meta_keys = apply_filters( 'give_process_form_meta_keys', $this->get_meta_keys_from_settings() );
761
762
		// Save form meta data.
763
		if ( ! empty( $form_meta_keys ) ) {
764
			foreach ( $form_meta_keys as $form_meta_key ) {
765
				if ( isset( $_POST[ $form_meta_key ] ) ) {
766
					if ( $field_type = $this->get_field_type( $form_meta_key ) ) {
767
						switch ( $field_type ) {
768
							case 'wysiwyg':
769
								$form_meta_value = wp_kses_post( $_POST[ $form_meta_key ] );
770
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
771
								break;
772
773
							case 'group':
774
								$form_meta_value = array();
775
776
								foreach ( $_POST[ $form_meta_key ] as $index => $group ) {
777
778
									// Do not save template input field values.
779
									if ( '{{row-count-placeholder}}' === $index ) {
780
										continue;
781
									}
782
783
									$group_meta_value = array();
784
									foreach ( $group as $field_id => $field_value ) {
785
										switch ( $this->get_field_type( $field_id, $form_meta_key ) ) {
786
											case 'wysiwyg':
787
												$group_meta_value[ $field_id ] = wp_kses_post( $field_value );
788
												break;
789
790
											default:
791
												$group_meta_value[ $field_id ] = give_clean( $field_value );
792
										}
793
									}
794
795
									if ( ! empty( $group_meta_value ) ) {
796
										$form_meta_value[ $index ] = $group_meta_value;
797
									}
798
								}
799
800
801
								// Arrange repeater field keys in order.
802
								$form_meta_value = array_values( $form_meta_value );
803
804
								// Save data.
805
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
806
								break;
807
808
							default:
809
								$form_meta_value = give_clean( $_POST[ $form_meta_key ] );
810
811
								// Save data.
812
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
813
						}
814
815
						// Fire after saving form meta key.
816
						do_action( "give_save_{$form_meta_key}", $form_meta_key, $form_meta_value, $post_id, $post );
817
					}
818
				}
819
			}
820
		}
821
822
		// Fire action after saving form meta.
823
		do_action( 'give_post_process_give_forms_meta', $post_id, $post );
824
	}
825
826
827
	/**
828
	 * Get field ID.
829
	 *
830
	 * @since 1.8
831
	 * @param array $field
832
	 *
833
	 * @return string
834
	 */
835
	private function get_field_id( $field ) {
836
		$field_id = '';
837
838
		if ( array_key_exists( 'id', $field ) ) {
839
			$field_id = $field['id'];
840
841
		}
842
843
		return $field_id;
844
	}
845
846
	/**
847
	 * Get fields ID.
848
	 *
849
	 * @since 1.8
850
	 * @param $setting
851
	 *
852
	 * @return array
853
	 */
854
	private function get_fields_id( $setting ) {
855
		$meta_keys = array();
856
857
		if( ! empty( $setting ) ) {
858
			foreach ( $setting['fields'] as $field ) {
859
				if ( $field_id = $this->get_field_id( $field ) ) {
860
					$meta_keys[] = $field_id;
861
				}
862
			}
863
		}
864
865
		return $meta_keys;
866
	}
867
868
	/**
869
	 * Get sub fields ID.
870
	 *
871
	 * @since 1.8
872
	 * @param $setting
873
	 *
874
	 * @return array
875
	 */
876
	private function get_sub_fields_id( $setting ) {
877
		$meta_keys = array();
878
879
		if ( $this->has_sub_tab( $setting ) && ! empty( $setting['sub-fields'] ) ) {
880
			foreach ( $setting['sub-fields'] as $fields ) {
881
				if ( ! empty( $fields['fields'] ) ) {
882
					foreach ( $fields['fields'] as $field ) {
883
						if ( $field_id = $this->get_field_id( $field ) ) {
884
							$meta_keys[] = $field_id;
885
						}
886
					}
887
				}
888
			}
889
		}
890
891
		return $meta_keys;
892
	}
893
894
895
	/**
896
	 * Get all setting field ids.
897
	 *
898
	 * @since  1.8
899
	 * @return array
900
	 */
901
	private function get_meta_keys_from_settings() {
902
		$meta_keys = array();
903
904
		foreach ( $this->settings as $setting ) {
905
			if ( $this->has_sub_tab( $setting ) ) {
906
				$meta_key = $this->get_sub_fields_id( $setting );
907
			} else {
908
				$meta_key = $this->get_fields_id( $setting );
909
			}
910
911
			$meta_keys = array_merge( $meta_keys, $meta_key );
912
		}
913
914
		return $meta_keys;
915
	}
916
917
918
	/**
919
	 * Get field type.
920
	 *
921
	 * @since  1.8
922
	 *
923
	 * @param  string $field_id
924
	 * @param  string $group_id
925
	 *
926
	 * @return string
927
	 */
928
	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...
929
		$field = $this->get_setting_field( $field_id, $group_id );
930
931
		$type  = array_key_exists( 'type', $field )
932
			? $field['type']
933
			: '';
934
935
		return $type;
936
	}
937
938
939
	/**
940
	 * Get Field
941
	 *
942
	 * @since 1.8
943
	 *
944
	 * @param array  $setting
945
	 * @param string $field_id
946
	 *
947
	 * @return array
948
	 */
949
	private function get_field( $setting, $field_id ) {
950
		$setting_field = array();
951
952
		if ( ! empty( $setting['fields'] ) ) {
953
			foreach ( $setting['fields'] as $field ) {
954
				if ( array_key_exists( 'id', $field ) && $field['id'] === $field_id ) {
955
					$setting_field = $field;
956
					break;
957
				}
958
			}
959
		}
960
961
		return $setting_field;
962
	}
963
964
	/**
965
	 * Get Sub Field
966
	 *
967
	 * @since 1.8
968
	 *
969
	 * @param array  $setting
970
	 * @param string $field_id
971
	 *
972
	 * @return array
973
	 */
974
	private function get_sub_field( $setting, $field_id ) {
975
		$setting_field = array();
976
977
		if ( ! empty( $setting['sub-fields'] ) ) {
978
			foreach ( $setting['sub-fields'] as $fields ) {
979
				if ( $field = $this->get_field( $fields, $field_id ) ) {
980
					$setting_field = $field;
981
					break;
982
				}
983
			}
984
		}
985
		
986
		return $setting_field;
987
	}
988
989
	/**
990
	 * Get setting field.
991
	 *
992
	 * @since  1.8
993
	 *
994
	 * @param  string $field_id
995
	 * @param  string $group_id Get sub field from group.
996
	 *
997
	 * @return array
998
	 */
999
	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...
1000
		$setting_field = array();
1001
1002
		$_field_id = $field_id;
1003
		$field_id  = empty( $group_id ) ? $field_id : $group_id;
1004
		
1005
		if ( ! empty( $this->settings ) ) {
1006
			foreach ( $this->settings as $setting ) {
1007
				if( $this->has_sub_tab( $setting ) ) {
1008
					$setting_field = $this->get_sub_field( $setting, $field_id );
1009
					break;
1010
				} elseif ( $field = $this->get_field( $setting, $field_id ) ) {
1011
					$setting_field = $field;
1012
					break;
1013
				}
1014
			}
1015
		}
1016
1017
1018
		// Get field from group.
1019
		if ( ! empty( $group_id ) ) {
1020
			foreach ( $setting_field['fields'] as $field ) {
1021
				if ( array_key_exists( 'id', $field ) && $field['id'] === $_field_id ) {
1022
					$setting_field = $field;
1023
				}
1024
			}
1025
		}
1026
1027
		return $setting_field;
1028
	}
1029
1030
1031
	/**
1032
	 * Add offline donations setting tab to donation form options metabox.
1033
	 *
1034
	 * @since  1.8
1035
	 *
1036
	 * @param  array $settings List of form settings.
1037
	 *
1038
	 * @return mixed
1039
	 */
1040
	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...
1041
		if ( give_is_gateway_active( 'offline' ) ) {
1042
			$settings['offline_donations_options'] = apply_filters( 'give_forms_offline_donations_options', array(
1043
				'id'     => 'offline_donations_options',
1044
				'title'  => esc_html__( 'Offline Donations', 'give' ),
1045
				'fields' => apply_filters( 'give_forms_offline_donations_metabox_fields', array() ),
1046
			) );
1047
		}
1048
1049
		return $settings;
1050
	}
1051
}
1052
1053
new Give_MetaBox_Form_Data();
1054
1055