Completed
Push — issues/611 ( 661115...758b1c )
by Ravinder
21:11
created

includes/admin/forms/class-metabox-form-data.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
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() {
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
		// Save form meta.
58
		add_action( 'save_post_give_forms', array( $this, 'save' ), 10, 2 );
59
60
		// cmb2 old setting loaders.
61
		// add_filter( 'give_metabox_form_data_settings', array( $this, 'cmb2_metabox_settings' ) );
62
		// Add offline donations options.
63
		add_filter( 'give_metabox_form_data_settings', array( $this, 'add_offline_donations_setting_tab' ), 0, 1 );
64
	}
65
66
67
	/**
68
	 * Setup metabox related data.
69
	 *
70
	 * @since  1.8
71
	 * @return void
72
	 */
73
	function setup() {
74
		$this->settings = $this->get_settings();
75
	}
76
77
78
	/**
79
	 * Get metabox settings
80
	 *
81
	 * @since  1.8
82
	 * @return array
83
	 */
84
	function get_settings() {
85
		$post_id               = give_get_admin_post_id();
86
		$price                 = give_get_form_price( $post_id );
87
		$custom_amount_minimum = give_get_form_minimum_price( $post_id );
88
		$goal                  = give_get_form_goal( $post_id );
89
90
		// No empty prices - min. 1.00 for new forms
91
		if ( empty( $price ) && is_null( $post_id ) ) {
92
			$price = esc_attr( give_format_decimal( '1.00' ) );
93
		}
94
95
		// Min. $1.00 for new forms
96
		if ( empty( $custom_amount_minimum ) ) {
97
			$custom_amount_minimum = esc_attr( give_format_decimal( '1.00' ) );
98
		}
99
100
		// Start with an underscore to hide fields from custom fields list
101
		$prefix = '_give_';
102
103
		$settings = array(
104
			/**
105
			 * Repeatable Field Groups
106
			 */
107
			'form_field_options'    => apply_filters( 'give_forms_field_options', array(
108
				'id'        => 'form_field_options',
109
				'title'     => esc_html__( 'Donation Options', 'give' ),
110
				'icon-html' => '<span class="give-icon give-icon-heart"></span>',
111
				'fields'    => apply_filters( 'give_forms_donation_form_metabox_fields', array(
112
					// Donation Option
113
					array(
114
						'name'        => esc_html__( 'Donation Option', 'give' ),
115
						'description' => esc_html__( 'Do you want this form to have one set donation price or multiple levels (for example, $10, $20, $50)?', 'give' ),
116
						'id'          => $prefix . 'price_option',
117
						'type'        => 'radio_inline',
118
						'default'     => 'set',
119
						'options'     => apply_filters( 'give_forms_price_options', array(
120
							'set'   => esc_html__( 'Set Donation', 'give' ),
121
							'multi' => esc_html__( 'Multi-level Donation', 'give' ),
122
						) ),
123
					),
124
					array(
125
						'name'        => esc_html__( 'Set Donation', 'give' ),
126
						'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' ),
127
						'id'          => $prefix . 'set_price',
128
						'type'        => 'text_small',
129
						'data_type'   => 'price',
130
						'attributes'  => array(
131
							'placeholder' => give_format_decimal( '1.00' ),
132
							'value'       => give_format_decimal( $price ),
133
							'class'       => 'give-money-field',
134
						),
135
					),
136
					// Display Style
137
					array(
138
						'name'        => esc_html__( 'Display Style', 'give' ),
139
						'description' => esc_html__( 'Set how the donations levels will display on the form.', 'give' ),
140
						'id'          => $prefix . 'display_style',
141
						'type'        => 'radio_inline',
142
						'default'     => 'buttons',
143
						'options'     => array(
144
							'buttons'  => esc_html__( 'Buttons', 'give' ),
145
							'radios'   => esc_html__( 'Radios', 'give' ),
146
							'dropdown' => esc_html__( 'Dropdown', 'give' ),
147
						),
148
					),
149
					// Custom Amount
150
					array(
151
						'name'        => esc_html__( 'Custom Amount', 'give' ),
152
						'description' => esc_html__( 'Do you want the user to be able to input their own donation amount?', 'give' ),
153
						'id'          => $prefix . 'custom_amount',
154
						'type'        => 'radio_inline',
155
						'default'     => 'disabled',
156
						'options'     => array(
157
							'enabled'  => esc_html__( 'Enabled', 'give' ),
158
							'disabled' => esc_html__( 'Disabled', 'give' ),
159
						),
160
					),
161
					array(
162
						'name'        => esc_html__( 'Minimum Amount', 'give' ),
163
						'description' => esc_html__( 'Enter the minimum custom donation amount.', 'give' ),
164
						'id'          => $prefix . 'custom_amount_minimum',
165
						'type'        => 'text_small',
166
						'data_type'   => 'price',
167
						'attributes'  => array(
168
							'placeholder' => give_format_decimal( '1.00' ),
169
							'value'       => give_format_decimal( $custom_amount_minimum ),
170
							'class'       => 'give-money-field',
171
						),
172
					),
173
					array(
174
						'name'        => esc_html__( 'Custom Amount Text', 'give' ),
175
						'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' ),
176
						'id'          => $prefix . 'custom_amount_text',
177
						'type'        => 'text_medium',
178
						'attributes'  => array(
179
							'rows'        => 3,
180
							'placeholder' => esc_attr__( 'Give a Custom Amount', 'give' ),
181
						),
182
					),
183
					// Donation Levels: Repeatable CMB2 Group
184
					array(
185
						'id'      => $prefix . 'donation_levels',
186
						'type'    => 'group',
187
						'options' => array(
188
							'add_button'    => esc_html__( 'Add Level', 'give' ),
189
							'header_title'  => esc_html__( 'Donation Level', 'give' ),
190
							'remove_button' => '<span class="dashicons dashicons-no"></span>',
191
						),
192
						// Fields array works the same, except id's only need to be unique for this group.
193
						// Prefix is not needed.
194
						'fields'  => apply_filters( 'give_donation_levels_table_row', array(
195
							array(
196
								'name' => esc_html__( 'ID', 'give' ),
197
								'id'   => $prefix . 'id',
198
								'type' => 'levels_id',
199
							),
200
							array(
201
								'name'       => esc_html__( 'Amount', 'give' ),
202
								'id'         => $prefix . 'amount',
203
								'type'       => 'text_small',
204
								'data_type'  => 'price',
205
								'attributes' => array(
206
									'placeholder' => give_format_decimal( '1.00' ),
207
									'class'       => 'give-money-field',
208
								),
209
							),
210
							array(
211
								'name'       => esc_html__( 'Text', 'give' ),
212
								'id'         => $prefix . 'text',
213
								'type'       => 'text',
214
								'attributes' => array(
215
									'placeholder' => esc_html__( 'Donation Level', 'give' ),
216
									'class'       => 'give-multilevel-text-field',
217
								),
218
							),
219
							array(
220
								'name' => esc_html__( 'Default', 'give' ),
221
								'id'   => $prefix . 'default',
222
								'type' => 'give_default_radio_inline',
223
							),
224
						) ),
225
					),
226
					array(
227
						'name'  => 'donation_options_docs',
228
						'type'  => 'docs_link',
229
						'url'   => 'http://docs.givewp.com/form-donation-options',
230
						'title' => esc_html__( 'Donation Options', 'give' ),
231
					),
232
				),
233
					$post_id
234
				),
235
			) ),
236
237
			/**
238
			 * Display Options
239
			 */
240
			'form_display_options'  => apply_filters( 'give_form_display_options', array(
241
					'id'        => 'form_display_options',
242
					'title'     => esc_html__( 'Form Display', 'give' ),
243
					'icon-html' => '<span class="give-icon give-icon-display"></span>',
244
					'fields'    => apply_filters( 'give_forms_display_options_metabox_fields', array(
245
						array(
246
							'name'    => esc_html__( 'Display Options', 'give' ),
247
							'desc'    => sprintf( __( 'How would you like to display donation information for this form?', 'give' ), '#' ),
248
							'id'      => $prefix . 'payment_display',
249
							'type'    => 'radio_inline',
250
							'options' => array(
251
								'onpage' => esc_html__( 'All Fields', 'give' ),
252
								'modal'  => esc_html__( 'Modal', 'give' ),
253
								'reveal' => esc_html__( 'Reveal', 'give' ),
254
								'button' => esc_html__( 'Button', 'give' ),
255
							),
256
							'default' => 'onpage',
257
						),
258
						array(
259
							'id'         => $prefix . 'reveal_label',
260
							'name'       => esc_html__( 'Continue Button', 'give' ),
261
							'desc'       => esc_html__( 'The button label for displaying the additional payment fields.', 'give' ),
262
							'type'       => 'text_small',
263
							'attributes' => array(
264
								'placeholder' => esc_attr__( 'Donate Now', 'give' ),
265
							),
266
						),
267
						array(
268
							'id'         => $prefix . 'checkout_label',
269
							'name'       => esc_html__( 'Submit Button', 'give' ),
270
							'desc'       => esc_html__( 'The button label for completing a donation.', 'give' ),
271
							'type'       => 'text_small',
272
							'attributes' => array(
273
								'placeholder' => esc_html__( 'Donate Now', 'give' ),
274
							),
275
						),
276
						array(
277
							'name' => esc_html__( 'Default Gateway', 'give' ),
278
							'desc' => esc_html__( 'By default, the gateway for this form will inherit the global default gateway (set under Give > Settings > Payment Gateways). This option allows you to customize the default gateway for this form only.', 'give' ),
279
							'id'   => $prefix . 'default_gateway',
280
							'type' => 'default_gateway',
281
						),
282
						array(
283
							'name'    => esc_html__( 'Guest Donations', 'give' ),
284
							'desc'    => esc_html__( 'Do you want to allow non-logged-in users to make donations?', 'give' ),
285
							'id'      => $prefix . 'logged_in_only',
286
							'type'    => 'radio_inline',
287
							'default' => 'enabled',
288
							'options' => array(
289
								'enabled'  => esc_html__( 'Enabled', 'give' ),
290
								'disabled' => esc_html__( 'Disabled', 'give' ),
291
							),
292
						),
293
						array(
294
							'name'    => esc_html__( 'Registration', 'give' ),
295
							'desc'    => esc_html__( 'Display the registration and login forms in the payment section for non-logged-in users.', 'give' ),
296
							'id'      => $prefix . 'show_register_form',
297
							'type'    => 'radio',
298
							'options' => array(
299
								'none'         => esc_html__( 'None', 'give' ),
300
								'registration' => esc_html__( 'Registration', 'give' ),
301
								'login'        => esc_html__( 'Login', 'give' ),
302
								'both'         => esc_html__( 'Registration + Login', 'give' ),
303
							),
304
							'default' => 'none',
305
						),
306
						array(
307
							'name'    => esc_html__( 'Floating Labels', 'give' ),
308
							/* translators: %s: forms http://docs.givewp.com/form-floating-labels */
309
							'desc'    => sprintf( __( 'Select the <a href="%s" target="_blank">floating labels</a> setting for this Give form. Be aware that if you have the "Disable CSS" option enabled, you will need to style the floating labels yourself.', 'give' ), esc_url( 'http://docs.givewp.com/form-floating-labels' ) ),
310
							'id'      => $prefix . 'form_floating_labels',
311
							'type'    => 'radio_inline',
312
							'options' => array(
313
								'global'   => esc_html__( 'Global Option', 'give' ),
314
								'enabled'  => esc_html__( 'Enabled', 'give' ),
315
								'disabled' => esc_html__( 'Disabled', 'give' ),
316
							),
317
							'default' => 'global',
318
						),
319
						array(
320
							'name'  => 'form_display_docs',
321
							'type'  => 'docs_link',
322
							'url'   => 'http://docs.givewp.com/form-display-options',
323
							'title' => esc_html__( 'Form Display', 'give' ),
324
						),
325
					),
326
						$post_id
327
					),
328
				)
329
			),
330
331
			/**
332
			 * Donation Goals
333
			 */
334
			'donation_goal_options' => apply_filters( 'give_donation_goal_options', array(
335
				'id'        => 'donation_goal_options',
336
				'title'     => esc_html__( 'Donation Goal', 'give' ),
337
				'icon-html' => '<span class="give-icon give-icon-target"></span>',
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'       => __( 'Goal Achieved Message', 'give' ),
396
						'desc'       => __( 'Do you want to display a custom message when the goal is closed?', 'give' ),
397
						'id'         => $prefix . 'form_goal_achieved_message',
398
						'type'       => 'wysiwyg',
399
                        'default' => __( 'Thank you to all our donors, we have met our fundraising goal.', 'give' ),
400
					),
401
					array(
402
						'name'  => 'donation_goal_docs',
403
						'type'  => 'docs_link',
404
						'url'   => 'http://docs.givewp.com/form-donation-goal',
405
						'title' => esc_html__( 'Donation Goal', 'give' ),
406
					),
407
				),
408
					$post_id
409
				),
410
			) ),
411
412
			/**
413
			 * Content Field
414
			 */
415
			'form_content_options'  => apply_filters( 'give_forms_content_options', array(
416
				'id'        => 'form_content_options',
417
				'title'     => esc_html__( 'Form Content', 'give' ),
418
				'icon-html' => '<span class="give-icon give-icon-edit"></span>',
419
				'fields'    => apply_filters( 'give_forms_content_options_metabox_fields', array(
420
421
					// Donation content.
422
					array(
423
						'name'        => esc_html__( 'Display Content', 'give' ),
424
						'description' => esc_html__( 'Do you want to add custom content to this form?', 'give' ),
425
						'id'          => $prefix . 'display_content',
426
						'type'        => 'radio_inline',
427
						'options'     => array(
428
							'enabled'  => esc_html__( 'Enabled', 'give' ),
429
							'disabled' => esc_html__( 'Disabled', 'give' ),
430
						),
431
						'default'     => 'disabled',
432
					),
433
434
					// Content placement.
435
					array(
436
						'name'        => esc_html__( 'Content Placement', 'give' ),
437
						'description' => esc_html__( 'This option controls where the content appears within the donation form.', 'give' ),
438
						'id'          => $prefix . 'content_placement',
439
						'type'        => 'radio_inline',
440
						'options'     => apply_filters( 'give_forms_content_options_select', array(
441
								'give_pre_form'  => esc_html__( 'Above fields', 'give' ),
442
								'give_post_form' => esc_html__( 'Below fields', 'give' ),
443
							)
444
						),
445
						'default'     => 'give_pre_form',
446
					),
447
					array(
448
						'name'        => esc_html__( 'Content', 'give' ),
449
						'description' => esc_html__( 'This content will display on the single give form page.', 'give' ),
450
						'id'          => $prefix . 'form_content',
451
						'type'        => 'wysiwyg',
452
					),
453
					array(
454
						'name'  => 'form_content_docs',
455
						'type'  => 'docs_link',
456
						'url'   => 'http://docs.givewp.com/form-content',
457
						'title' => esc_html__( 'Form Content', 'give' ),
458
					),
459
				),
460
					$post_id
461
				),
462
			) ),
463
464
			/**
465
			 * Terms & Conditions
466
			 */
467
			'form_terms_options'    => apply_filters( 'give_forms_terms_options', array(
468
				'id'        => 'form_terms_options',
469
				'title'     => esc_html__( 'Terms & Conditions', 'give' ),
470
				'icon-html' => '<span class="give-icon give-icon-checklist"></span>',
471
				'fields'    => apply_filters( 'give_forms_terms_options_metabox_fields', array(
472
					// Donation Option
473
					array(
474
						'name'        => esc_html__( 'Terms and 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 and 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() {
549
		global $post;
550
551
		if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
0 ignored issues
show
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

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