Completed
Push — master ( 0af146...0e0d02 )
by Devin
13s
created

Give_MetaBox_Form_Data::output()   C

Complexity

Conditions 17
Paths 19

Size

Total Lines 62
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 53
nc 19
nop 0
dl 0
loc 62
rs 6.1162
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 1077.

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