Completed
Push — issues/1122 ( 0db41a )
by Ravinder
16:39
created

Give_MetaBox_Form_Data::get_tabs()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 2
nop 0
dl 0
loc 18
rs 8.8571
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 835.

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
					)
229
				),
230
			) ),
231
232
			/**
233
			 * Display Options
234
			 */
235
			'form_display_options'  => apply_filters( 'give_form_display_options', array(
236
					'id'     => 'form_display_options',
237
					'title'  => esc_html__( 'Form Display', 'give' ),
238
					'fields' => apply_filters( 'give_forms_display_options_metabox_fields', array(
239
							array(
240
								'name'    => esc_html__( 'Display Options', 'give' ),
241
								'desc'    => sprintf( __( 'How would you like to display donation information for this form?', 'give' ), '#' ),
242
								'id'      => $prefix . 'payment_display',
243
								'type'    => 'radio_inline',
244
								'options' => array(
245
									'onpage' => esc_html__( 'All Fields', 'give' ),
246
									'modal'  => esc_html__( 'Modal', 'give' ),
247
									'reveal' => esc_html__( 'Reveal', 'give' ),
248
									'button' => esc_html__( 'Button', 'give' ),
249
								),
250
								'default' => 'onpage',
251
							),
252
							array(
253
								'id'         => $prefix . 'reveal_label',
254
								'name'       => esc_html__( 'Continue Button', 'give' ),
255
								'desc'       => esc_html__( 'The button label for displaying the additional payment fields.', 'give' ),
256
								'type'       => 'text_small',
257
								'attributes' => array(
258
									'placeholder' => esc_attr__( 'Donate Now', 'give' ),
259
								),
260
							),
261
							array(
262
								'id'         => $prefix . 'checkout_label',
263
								'name'       => esc_html__( 'Submit Button', 'give' ),
264
								'desc'       => esc_html__( 'The button label for completing a donation.', 'give' ),
265
								'type'       => 'text_small',
266
								'attributes' => array(
267
									'placeholder' => esc_html__( 'Donate Now', 'give' ),
268
								),
269
							),
270
							array(
271
								'name' => esc_html__( 'Default Gateway', 'give' ),
272
								'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' ),
273
								'id'   => $prefix . 'default_gateway',
274
								'type' => 'default_gateway',
275
							),
276
							array(
277
								'name'    => esc_html__( 'Guest Donations', 'give' ),
278
								'desc'    => esc_html__( 'Do you want to require users be logged-in to make donations?', 'give' ),
279
								'id'      => $prefix . 'logged_in_only',
280
								'type'    => 'radio_inline',
281
								'default' => 'enabled',
282
								'options' => array(
283
									'enabled'  => esc_html__( 'Enabled', 'give' ),
284
									'disabled' => esc_html__( 'Disabled', 'give' ),
285
								),
286
							),
287
							array(
288
								'name'    => esc_html__( 'Registration', 'give' ),
289
								'desc'    => esc_html__( 'Display the registration and login forms in the payment section for non-logged-in users.', 'give' ),
290
								'id'      => $prefix . 'show_register_form',
291
								'type'    => 'radio',
292
								'options' => array(
293
									'none'         => esc_html__( 'None', 'give' ),
294
									'registration' => esc_html__( 'Registration', 'give' ),
295
									'login'        => esc_html__( 'Login', 'give' ),
296
									'both'         => esc_html__( 'Registration + Login', 'give' ),
297
								),
298
								'default' => 'none',
299
							),
300
							array(
301
								'name'    => esc_html__( 'Floating Labels', 'give' ),
302
								/* translators: %s: forms https://givewp.com/documentation/core/give-forms/creating-give-forms/#floating-labels */
303
								'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' ) ),
304
								'id'      => $prefix . 'form_floating_labels',
305
								'type'    => 'radio_inline',
306
								'options' => array(
307
									'global'   => esc_html__( 'Global Options', 'give' ),
308
									'enabled'  => esc_html__( 'Enabled', 'give' ),
309
									'disabled' => esc_html__( 'Disabled', 'give' ),
310
								),
311
								'default' => 'global',
312
							),
313
						)
314
					),
315
				)
316
			),
317
318
			/**
319
			 * Donation Goals
320
			 */
321
			'donation_goal_options' => apply_filters( 'give_donation_goal_options', array(
322
				'id'     => 'donation_goal_options',
323
				'title'  => esc_html__( 'Donation Goal', 'give' ),
324
				'fields' => apply_filters( 'give_forms_donation_goal_metabox_fields', array(
325
					// Goals
326
					array(
327
						'name'        => esc_html__( 'Donation Goal', 'give' ),
328
						'description' => esc_html__( 'Do you want to set a donation goal for this form?', 'give' ),
329
						'id'          => $prefix . 'goal_option',
330
						'type'        => 'radio_inline',
331
						'default'     => 'disabled',
332
						'options'     => array(
333
							'enabled'  => esc_html__( 'Enabled', 'give' ),
334
							'disabled' => esc_html__( 'Disabled', 'give' ),
335
						),
336
					),
337
					array(
338
						'name'        => esc_html__( 'Goal Amount', 'give' ),
339
						'description' => esc_html__( 'This is the monetary goal amount you want to reach for this form.', 'give' ),
340
						'id'          => $prefix . 'set_goal',
341
						'type'        => 'text_small',
342
						'data_type'   => 'price',
343
						'attributes'  => array(
344
							'placeholder' => give_format_decimal( '0.00' ),
345
							'value'       => give_format_decimal( $goal ),
346
							'class'       => 'give-money-field',
347
						),
348
					),
349
350
					array(
351
						'name'        => esc_html__( 'Goal Format', 'give' ),
352
						'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' ),
353
						'id'          => $prefix . 'goal_format',
354
						'type'        => 'radio_inline',
355
						'default'     => 'amount',
356
						'options'     => array(
357
							'amount'     => esc_html__( 'Amount', 'give' ),
358
							'percentage' => esc_html__( 'Percentage', 'give' ),
359
						),
360
					),
361
					array(
362
						'name'    => esc_html__( 'Progress Bar Color', 'give' ),
363
						'desc'    => esc_html__( 'Customize the color of the goal progress bar.', 'give' ),
364
						'id'      => $prefix . 'goal_color',
365
						'type'    => 'colorpicker',
366
						'default' => '#2bc253',
367
					),
368
369
					array(
370
						'name'    => esc_html__( 'Close Form', 'give' ),
371
						'desc'    => esc_html__( 'Do you want to close the donation forms and stop accepting donations once this goal has been met?', 'give' ),
372
						'id'      => $prefix . 'close_form_when_goal_achieved',
373
						'type'    => 'radio_inline',
374
						'default' => 'disabled',
375
						'options' => array(
376
							'enabled'  => esc_html__( 'Enabled', 'give' ),
377
							'disabled' => esc_html__( 'Disabled', 'give' ),
378
						),
379
					),
380
					array(
381
						'name'       => esc_html__( 'Goal Achieved Message', 'give' ),
382
						'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' ),
383
						'id'         => $prefix . 'form_goal_achieved_message',
384
						'type'       => 'textarea',
385
						'attributes' => array(
386
							'placeholder' => esc_attr__( 'Thank you to all our donors, we have met our fundraising goal.', 'give' ),
387
						),
388
					),
389
				) ),
390
			) ),
391
392
			/**
393
			 * Content Field
394
			 */
395
			'form_content_options'  => apply_filters( 'give_forms_content_options', array(
396
				'id'     => 'form_content_options',
397
				'title'  => esc_html__( 'Form Content', 'give' ),
398
				'fields' => apply_filters( 'give_forms_content_options_metabox_fields', array(
399
400
						// Donation content.
401
						array(
402
							'name'        => esc_html__( 'Display Content', 'give' ),
403
							'description' => esc_html__( 'Do you want to add custom content to this form?', 'give' ),
404
							'id'          => $prefix . 'display_content',
405
							'type'        => 'radio_inline',
406
							'options'     => array(
407
								'enabled'  => esc_html__( 'Enabled', 'give' ),
408
								'disabled' => esc_html__( 'Disabled', 'give' ),
409
							),
410
							'default'     => 'disabled',
411
						),
412
413
						// Content placement.
414
						array(
415
							'name'        => esc_html__( 'Content Placement', 'give' ),
416
							'description' => esc_html__( 'This option controls where the content appears within the donation form.', 'give' ),
417
							'id'          => $prefix . 'content_placement',
418
							'type'        => 'radio_inline',
419
							'options'     => apply_filters( 'give_forms_content_options_select', array(
420
									'give_pre_form'  => esc_html__( 'Above fields', 'give' ),
421
									'give_post_form' => esc_html__( 'Below fields', 'give' ),
422
								)
423
							),
424
							'default'     => 'give_pre_form',
425
						),
426
						array(
427
							'name'        => esc_html__( 'Content', 'give' ),
428
							'description' => esc_html__( 'This content will display on the single give form page.', 'give' ),
429
							'id'          => $prefix . 'form_content',
430
							'type'        => 'wysiwyg',
431
						),
432
					)
433
				),
434
			) ),
435
436
			/**
437
			 * Terms & Conditions
438
			 */
439
			'form_terms_options'    => apply_filters( 'give_forms_terms_options', array(
440
				'id'     => 'form_terms_options',
441
				'title'  => esc_html__( 'Terms & Conditions', 'give' ),
442
				'fields' => apply_filters( 'give_forms_terms_options_metabox_fields', array(
443
						// Donation Option
444
						array(
445
							'name'        => esc_html__( 'Terms & Conditions', 'give' ),
446
							'description' => esc_html__( 'Do you want to require the donor to accept terms prior to being able to complete their donation?', 'give' ),
447
							'id'          => $prefix . 'terms_option',
448
							'type'        => 'radio_inline',
449
							'options'     => apply_filters( 'give_forms_content_options_select', array(
450
									'global'   => esc_html__( 'Global Options', 'give' ),
451
									'enabled'  => esc_html__( 'Customize', 'give' ),
452
									'disabled' => esc_html__( 'Disable', 'give' ),
453
								)
454
							),
455
							'default'     => 'global',
456
						),
457
						array(
458
							'id'         => $prefix . 'agree_label',
459
							'name'       => esc_html__( 'Agreement Label', 'give' ),
460
							'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' ),
461
							'type'       => 'text',
462
							'size'       => 'regular',
463
							'attributes' => array(
464
								'placeholder' => esc_attr__( 'Agree to Terms?', 'give' ),
465
							),
466
						),
467
						array(
468
							'id'   => $prefix . 'agree_text',
469
							'name' => esc_html__( 'Agreement Text', 'give' ),
470
							'desc' => esc_html__( 'This is the actual text which the user will have to agree to in order to make a donation.', 'give' ),
471
							'type' => 'wysiwyg',
472
						),
473
					)
474
				),
475
			) ),
476
		);
477
		/**
478
		 * Filter the metabox tabbed panel settings.
479
		 */
480
		$settings = apply_filters( 'give_metabox_form_data_settings', $settings );
481
482
		// Output.
483
		return $settings;
484
	}
485
486
	/**
487
	 * Add metabox.
488
	 *
489
	 * @since  1.8
490
	 * @return void
491
	 */
492
	public function add_meta_box() {
493
		add_meta_box(
494
			$this->get_metabox_ID(),
495
			$this->get_metabox_label(),
496
			array( $this, 'output' ),
497
			array( 'give_forms' ),
498
			'normal',
499
			'high'
500
		);
501
	}
502
503
504
	/**
505
	 * Enqueue scripts.
506
	 *
507
	 * @since  1.8
508
	 * @return void
509
	 */
510
	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...
511
		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...
512
513
		if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
514
			wp_enqueue_style( 'wp-color-picker' );
515
			wp_enqueue_script( 'wp-color-picker' );
516
		}
517
	}
518
519
	/**
520
	 * Get metabox id.
521
	 *
522
	 * @since  1.8
523
	 * @return string
524
	 */
525
	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...
526
		return $this->metabox_id;
527
	}
528
529
	/**
530
	 * Get metabox label.
531
	 *
532
	 * @since  1.8
533
	 * @return string
534
	 */
535
	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...
536
		return $this->metabox_label;
537
	}
538
539
540
	/**
541
	 * Get metabox tabs.
542
	 *
543
	 * @since  1.8
544
	 * @return array
545
	 */
546
	public function get_tabs() {
547
		$tabs = array();
548
549
		if ( ! empty( $this->settings ) ) {
550
			foreach ( $this->settings as $setting ) {
551
				if ( ! isset( $setting['id'] ) || ! isset( $setting['title'] ) ) {
552
					continue;
553
				}
554
555
				$tabs[] = array(
556
					'id'    => $setting['id'],
557
					'label' => $setting['title'],
558
				);
559
			}
560
		}
561
562
		return $tabs;
563
	}
564
565
	/**
566
	 * Output metabox settings.
567
	 *
568
	 * @since  1.8
569
	 * @return void
570
	 */
571
	public function output() {
572
		// Bailout.
573
		if ( $form_data_tabs = $this->get_tabs() ) {
574
			wp_nonce_field( 'give_save_form_meta', 'give_form_meta_nonce' );
575
			?>
576
			<div class="give-metabox-panel-wrap">
577
				<ul class="give-form-data-tabs give-metabox-tabs">
578
					<?php foreach ( $form_data_tabs as $index => $form_data_tab ) : ?>
579
						<li class="<?php echo "{$form_data_tab['id']}_tab" . ( ! $index ? ' active' : '' ); ?>">
580
							<a href="#<?php echo $form_data_tab['id']; ?>"><?php echo $form_data_tab['label']; ?></a>
581
						</li>
582
					<?php endforeach; ?>
583
				</ul>
584
585
				<?php $show_first_tab_content = true; ?>
586
				<?php foreach ( $this->settings as $setting ) : ?>
587
					<?php do_action( "give_before_{$setting['id']}_settings" ); ?>
588
589
					<div id="<?php echo $setting['id']; ?>" class="panel give_options_panel <?php echo( $show_first_tab_content ? '' : 'give-hidden' );$show_first_tab_content = false; ?>">
590
						<?php if ( ! empty( $setting['fields'] ) ) : ?>
591
							<?php foreach ( $setting['fields'] as $field ) : ?>
592
								<?php give_render_field( $field ); ?>
593
							<?php endforeach; ?>
594
						<?php endif; ?>
595
					</div>
596
597
					<?php do_action( "give_after_{$setting['id']}_settings" ); ?>
598
				<?php endforeach; ?>
599
			</div>
600
			<?php
601
		}
602
	}
603
604
	/**
605
	 * CMB2 settings loader.
606
	 *
607
	 * @since  1.8
608
	 * @return array
609
	 */
610
	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...
611
		$all_cmb2_settings   = apply_filters( 'cmb2_meta_boxes', array() );
612
		$give_forms_settings = $all_cmb2_settings;
613
614
		// Filter settings: Use only give forms related settings.
615
		foreach ( $all_cmb2_settings as $index => $setting ) {
616
			if ( ! in_array( 'give_forms', $setting['object_types'] ) ) {
617
				unset( $give_forms_settings[ $index ] );
618
			}
619
		}
620
621
		return $give_forms_settings;
622
623
	}
624
625
	/**
626
	 * Check if we're saving, the trigger an action based on the post type.
627
	 *
628
	 * @since  1.8
629
	 *
630
	 * @param  int    $post_id
631
	 * @param  object $post
632
	 *
633
	 * @return void
634
	 */
635
	public function save( $post_id, $post ) {
636
637
		// $post_id and $post are required.
638
		if ( empty( $post_id ) || empty( $post ) ) {
639
			return;
640
		}
641
642
		// Don't save meta boxes for revisions or autosaves.
643
		if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
644
			return;
645
		}
646
647
		// Check the nonce.
648
		if ( empty( $_POST['give_form_meta_nonce'] ) || ! wp_verify_nonce( $_POST['give_form_meta_nonce'], 'give_save_form_meta' ) ) {
649
			return;
650
		}
651
652
		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
653
		if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
654
			return;
655
		}
656
657
		// Check user has permission to edit.
658
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
659
			return;
660
		}
661
662
		// Fire action before saving form meta.
663
		do_action( 'give_pre_process_give_forms_meta', $post_id, $post );
664
665
		/**
666
		 * Filter the meta key to save.
667
		 * Third party addon developer can remove there meta keys from this array to handle saving data on there own.
668
		 */
669
		$form_meta_keys = apply_filters( 'give_process_form_meta_keys', $this->get_meta_keys_from_settings() );
670
671
		// Save form meta data.
672
		if ( ! empty( $form_meta_keys ) ) {
673
			foreach ( $form_meta_keys as $form_meta_key ) {
674
				if ( isset( $_POST[ $form_meta_key ] ) ) {
675
					if ( $field_type = $this->get_field_type( $form_meta_key ) ) {
676
						switch ( $field_type ) {
677
							case 'wysiwyg':
678
								$form_meta_value = wp_kses_post( $_POST[ $form_meta_key ] );
679
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
680
								break;
681
682
							case 'group':
683
								$form_meta_value = array();
684
685
								foreach ( $_POST[ $form_meta_key ] as $index => $group ) {
686
687
									// Do not save template input field values.
688
									if ( '{{row-count-placeholder}}' === $index ) {
689
										continue;
690
									}
691
692
									$group_meta_value = array();
693
									foreach ( $group as $field_id => $field_value ) {
694
										switch ( $this->get_field_type( $field_id, $form_meta_key ) ) {
695
											case 'wysiwyg':
696
												$group_meta_value[ $field_id ] = wp_kses_post( $field_value );
697
												break;
698
699
											default:
700
												$group_meta_value[ $field_id ] = give_clean( $field_value );
701
										}
702
									}
703
704
									if ( ! empty( $group_meta_value ) ) {
705
										$form_meta_value[ $index ] = $group_meta_value;
706
									}
707
								}
708
709
710
								// Arrange repeater field keys in order.
711
								$form_meta_value = array_values( $form_meta_value );
712
713
								// Save data.
714
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
715
								break;
716
717
							default:
718
								$form_meta_value = give_clean( $_POST[ $form_meta_key ] );
719
720
								// Save data.
721
								update_post_meta( $post_id, $form_meta_key, $form_meta_value );
722
						}
723
724
						// Fire after saving form meta key.
725
						do_action( "give_save_{$form_meta_key}", $form_meta_key, $form_meta_value, $post_id, $post );
726
					}
727
				}
728
			}
729
		}
730
731
		// Fire action after saving form meta.
732
		do_action( 'give_post_process_give_forms_meta', $post_id, $post );
733
	}
734
735
736
	/**
737
	 * Get all setting field ids.
738
	 *
739
	 * @since  1.8
740
	 * @return array
741
	 */
742
	private function get_meta_keys_from_settings() {
743
		$meta_keys = array();
744
		foreach ( $this->settings as $setting ) {
745
			if ( ! empty( $setting['fields'] ) ) {
746
				foreach ( $setting['fields'] as $field ) {
747
					$meta_keys[] = $field['id'];
748
				}
749
			}
750
		}
751
752
		return $meta_keys;
753
	}
754
755
756
	/**
757
	 * Get field type.
758
	 *
759
	 * @since  1.8
760
	 *
761
	 * @param  string $field_id
762
	 * @param  string $group_id
763
	 *
764
	 * @return string
765
	 */
766
	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...
767
768
		return $this->get_setting_field( $field_id, $group_id )['type'];
769
	}
770
771
	/**
772
	 * Get setting field.
773
	 *
774
	 * @since  1.8
775
	 *
776
	 * @param  string $field_id
777
	 * @param  string $group_id Get sub field from group.
778
	 *
779
	 * @return array
780
	 */
781
	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...
782
		$setting_field = array();
783
784
		$_field_id = $field_id;
785
		$field_id  = empty( $group_id ) ? $field_id : $group_id;
786
787
		if ( ! empty( $this->settings ) ) {
788
			foreach ( $this->settings as $setting ) {
789
				if ( ! empty( $setting['fields'] ) ) {
790
					foreach ( $setting['fields'] as $field ) {
791
						if ( $field['id'] === $field_id ) {
792
							$setting_field = $field;
793
						}
794
					}
795
				}
796
			}
797
		}
798
799
800
		// Get field from group.
801
		if ( ! empty( $group_id ) ) {
802
			foreach ( $setting_field['fields'] as $field ) {
803
				if ( $field['id'] === $_field_id ) {
804
					$setting_field = $field;
805
				}
806
			}
807
		}
808
809
		return $setting_field;
810
	}
811
812
813
	/**
814
	 * Add offline donations setting tab to donation form options metabox.
815
	 *
816
	 * @since  1.8
817
	 *
818
	 * @param  array $settings List of form settings.
819
	 *
820
	 * @return mixed
821
	 */
822
	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...
823
		if ( give_is_gateway_active( 'offline' ) ) {
824
			$settings['offline_donations_options'] = apply_filters( 'give_forms_offline_donations_options', array(
825
				'id'     => 'offline_donations_options',
826
				'title'  => esc_html__( 'Offline Donations', 'give' ),
827
				'fields' => apply_filters( 'give_forms_offline_donations_metabox_fields', array() ),
828
			) );
829
		}
830
831
		return $settings;
832
	}
833
}
834
835
new Give_MetaBox_Form_Data();
836
837