Completed
Pull Request — master (#1412)
by Ravinder
17:25
created

Give_MetaBox_Form_Data::save()   D

Complexity

Conditions 22
Paths 7

Size

Total Lines 99
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 45
nc 7
nop 2
dl 0
loc 99
rs 4.6625
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 1061.

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