Completed
Push — release/2.0 ( 4d845f...d9fa8a )
by Ravinder
18:24
created

Give_MetaBox_Form_Data::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
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 1146.

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 = __( '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' ), 10 );
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' ) );
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...
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() {
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...
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() {
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...
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'     => __( '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'        => __( 'Donation Option', 'give' ),
115
						'description' => __( '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'   => __( 'Set Donation', 'give' ),
121
							'multi' => __( 'Multi-level Donation', 'give' ),
122
						) ),
123
					),
124
					array(
125
						'name'        => __( 'Set Donation', 'give' ),
126
						'description' => __( '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'        => __( 'Display Style', 'give' ),
139
						'description' => __( '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'  => __( 'Buttons', 'give' ),
145
							'radios'   => __( 'Radios', 'give' ),
146
							'dropdown' => __( 'Dropdown', 'give' ),
147
						),
148
					),
149
					// Custom Amount
150
					array(
151
						'name'        => __( 'Custom Amount', 'give' ),
152
						'description' => __( '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'  => __( 'Enabled', 'give' ),
158
							'disabled' => __( 'Disabled', 'give' ),
159
						),
160
					),
161
					array(
162
						'name'        => __( 'Minimum Amount', 'give' ),
163
						'description' => __( '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'        => __( 'Custom Amount Text', 'give' ),
175
						'description' => __( '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'    => __( 'Add Level', 'give' ),
189
							'header_title'  => __( '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' => __( 'ID', 'give' ),
197
								'id'   => $prefix . 'id',
198
								'type' => 'levels_id',
199
							),
200
							array(
201
								'name'       => __( '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'       => __( 'Text', 'give' ),
212
								'id'         => $prefix . 'text',
213
								'type'       => 'text',
214
								'attributes' => array(
215
									'placeholder' => __( 'Donation Level', 'give' ),
216
									'class'       => 'give-multilevel-text-field',
217
								),
218
							),
219
							array(
220
								'name' => __( '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' => __( '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'     => __( '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'    => __( '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' => __( 'All Fields', 'give' ),
252
								'modal'  => __( 'Modal', 'give' ),
253
								'reveal' => __( 'Reveal', 'give' ),
254
								'button' => __( 'Button', 'give' ),
255
							),
256
							'default' => 'onpage',
257
						),
258
						array(
259
							'id'         => $prefix . 'reveal_label',
260
							'name'       => __( 'Continue Button', 'give' ),
261
							'desc'       => __( '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'       => __( 'Submit Button', 'give' ),
270
							'desc'       => __( 'The button label for completing a donation.', 'give' ),
271
							'type'       => 'text_small',
272
							'attributes' => array(
273
								'placeholder' => __( 'Donate Now', 'give' ),
274
							),
275
						),
276
						array(
277
							'name' => __( 'Default Gateway', 'give' ),
278
							'desc' => __( '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'    => __( 'Guest Donations', 'give' ),
284
							'desc'    => __( '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'  => __( 'Enabled', 'give' ),
290
								'disabled' => __( 'Disabled', 'give' ),
291
							),
292
						),
293
						array(
294
							'name'    => __( 'Registration', 'give' ),
295
							'desc'    => __( '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'         => __( 'None', 'give' ),
300
								'registration' => __( 'Registration', 'give' ),
301
								'login'        => __( 'Login', 'give' ),
302
								'both'         => __( 'Registration + Login', 'give' ),
303
							),
304
							'default' => 'none',
305
						),
306
						array(
307
							'name'    => __( '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'   => __( 'Global Option', 'give' ),
314
								'enabled'  => __( 'Enabled', 'give' ),
315
								'disabled' => __( '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' => __( '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'     => __( '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'        => __( 'Donation Goal', 'give' ),
342
						'description' => __( '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'  => __( 'Enabled', 'give' ),
348
							'disabled' => __( 'Disabled', 'give' ),
349
						),
350
					),
351
					array(
352
						'name'        => __( 'Goal Amount', 'give' ),
353
						'description' => __( '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'        => __( 'Goal Format', 'give' ),
366
						'description' => __( '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'     => __( 'Amount', 'give' ),
372
							'percentage' => __( 'Percentage', 'give' ),
373
						),
374
					),
375
					array(
376
						'name'    => __( 'Progress Bar Color', 'give' ),
377
						'desc'    => __( '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'    => __( 'Close Form', 'give' ),
385
						'desc'    => __( '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'  => __( 'Enabled', 'give' ),
391
							'disabled' => __( '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' => __( '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'     => __( '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'        => __( 'Display Content', 'give' ),
424
						'description' => __( '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'  => __( 'Enabled', 'give' ),
429
							'disabled' => __( 'Disabled', 'give' ),
430
						),
431
						'default'     => 'disabled',
432
					),
433
434
					// Content placement.
435
					array(
436
						'name'        => __( 'Content Placement', 'give' ),
437
						'description' => __( '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'  => __( 'Above fields', 'give' ),
442
								'give_post_form' => __( 'Below fields', 'give' ),
443
							)
444
						),
445
						'default'     => 'give_pre_form',
446
					),
447
					array(
448
						'name'        => __( 'Content', 'give' ),
449
						'description' => __( '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' => __( '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'     => __( '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'        => __( 'Terms and Conditions', 'give' ),
475
						'description' => __( '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'   => __( 'Global Option', 'give' ),
480
								'enabled'  => __( 'Customize', 'give' ),
481
								'disabled' => __( 'Disable', 'give' ),
482
							)
483
						),
484
						'default'     => 'global',
485
					),
486
					array(
487
						'id'         => $prefix . 'agree_label',
488
						'name'       => __( 'Agreement Label', 'give' ),
489
						'desc'       => __( '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' => __( 'Agreement Text', 'give' ),
499
						'desc' => __( 'This is the actual text which the user will have to agree to in order to make a donation.', 'give' ),
500
						'default' => give_get_option('agreement_text'),
501
						'type' => 'wysiwyg',
502
					),
503
					array(
504
						'name'  => 'terms_docs',
505
						'type'  => 'docs_link',
506
						'url'   => 'http://docs.givewp.com/form-terms',
507
						'title' => __( 'Terms and Conditions', 'give' ),
508
					),
509
				),
510
					$post_id
511
				),
512
			) ),
513
		);
514
515
516
		/**
517
		 * Filter the metabox tabbed panel settings.
518
		 */
519
		$settings = apply_filters( 'give_metabox_form_data_settings', $settings, $post_id );
520
521
		// Output.
522
		return $settings;
523
	}
524
525
	/**
526
	 * Add metabox.
527
	 *
528
	 * @since  1.8
529
	 * @return void
530
	 */
531
	public function add_meta_box() {
532
		add_meta_box(
533
			$this->get_metabox_ID(),
534
			$this->get_metabox_label(),
535
			array( $this, 'output' ),
536
			array( 'give_forms' ),
537
			'normal',
538
			'high'
539
		);
540
	}
541
542
543
	/**
544
	 * Enqueue scripts.
545
	 *
546
	 * @since  1.8
547
	 * @return void
548
	 */
549
	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...
550
		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...
551
552
		if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
0 ignored issues
show
Unused Code introduced by
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...
553
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
					'icon-html' => ( ! empty( $setting['icon-html'] ) ? $setting['icon-html'] : '' ),
596
				);
597
598
				if ( $this->has_sub_tab( $setting ) ) {
599
					if ( empty( $setting['sub-fields'] ) ) {
600
						$tab = array();
601
					} else {
602
						foreach ( $setting['sub-fields'] as $sub_fields ) {
603
							$tab['sub-fields'][] = array(
604
								'id'        => $sub_fields['id'],
605
								'label'     => $sub_fields['title'],
606
								'icon-html' => ( ! empty( $sub_fields['icon-html'] ) ? $sub_fields['icon-html'] : '' ),
607
							);
608
						}
609
					}
610
				}
611
612
				if ( ! empty( $tab ) ) {
613
					$tabs[] = $tab;
614
				}
615
			}
616
		}
617
618
		return $tabs;
619
	}
620
621
	/**
622
	 * Output metabox settings.
623
	 *
624
	 * @since  1.8
625
	 * @return void
626
	 */
627
	public function output() {
628
		// Bailout.
629
		if ( $form_data_tabs = $this->get_tabs() ) {
630
			wp_nonce_field( 'give_save_form_meta', 'give_form_meta_nonce' );
631
			?>
632
			<div class="give-metabox-panel-wrap">
633
				<ul class="give-form-data-tabs give-metabox-tabs">
634
					<?php foreach ( $form_data_tabs as $index => $form_data_tab ) : ?>
635
						<li class="<?php echo "{$form_data_tab['id']}_tab" . ( ! $index ? ' active' : '' ) . ( $this->has_sub_tab( $form_data_tab ) ? ' has-sub-fields' : '' ); ?>">
636
							<a href="#<?php echo $form_data_tab['id']; ?>">
637
								<?php if ( ! empty( $form_data_tab['icon-html'] ) ) : ?>
638
									<?php echo $form_data_tab['icon-html']; ?>
639
								<?php else : ?>
640
									<span class="give-icon give-icon-default"></span>
641
								<?php endif; ?>
642
								<span class="give-label"><?php echo $form_data_tab['label']; ?></span>
643
							</a>
644
							<?php if ( $this->has_sub_tab( $form_data_tab ) ) : ?>
645
								<ul class="give-metabox-sub-tabs give-hidden">
646
									<?php foreach ( $form_data_tab['sub-fields'] as $sub_tab ) : ?>
647
										<li class="<?php echo "{$sub_tab['id']}_tab"; ?>">
648
											<a href="#<?php echo $sub_tab['id']; ?>">
649
												<?php if ( ! empty( $sub_tab['icon-html'] ) ) : ?>
650
													<?php echo $sub_tab['icon-html']; ?>
651
												<?php else : ?>
652
													<span class="give-icon give-icon-default"></span>
653
												<?php endif; ?>
654
												<span class="give-label"><?php echo $sub_tab['label']; ?></span>
655
											</a>
656
										</li>
657
									<?php endforeach; ?>
658
								</ul>
659
							<?php endif; ?>
660
						</li>
661
					<?php endforeach; ?>
662
				</ul>
663
664
				<?php $show_first_tab_content = true; ?>
665
				<?php foreach ( $this->settings as $setting ) : ?>
666
					<?php if ( ! $this->has_sub_tab( $setting ) ) : ?>
667
						<?php do_action( "give_before_{$setting['id']}_settings" ); ?>
668
669
						<div id="<?php echo $setting['id']; ?>"
670
							 class="panel give_options_panel<?php echo( $show_first_tab_content ? '' : ' give-hidden' );
671
						     $show_first_tab_content = false; ?>">
672
							<?php if ( ! empty( $setting['fields'] ) ) : ?>
673
								<?php foreach ( $setting['fields'] as $field ) : ?>
674
									<?php give_render_field( $field ); ?>
675
								<?php endforeach; ?>
676
							<?php endif; ?>
677
						</div>
678
679
						<?php do_action( "give_after_{$setting['id']}_settings" ); ?>
680
					<?php else: ?>
681
						<?php if ( $this->has_sub_tab( $setting ) ) : ?>
682
							<?php if ( ! empty( $setting['sub-fields'] ) ) : ?>
683
								<?php foreach ( $setting['sub-fields'] as $index => $sub_fields ) : ?>
684
									<div id="<?php echo $sub_fields['id']; ?>"
685
										 class="panel give_options_panel give-hidden">
686
										<?php if ( ! empty( $sub_fields['fields'] ) ) : ?>
687
											<?php foreach ( $sub_fields['fields'] as $sub_field ) : ?>
688
												<?php give_render_field( $sub_field ); ?>
689
											<?php endforeach; ?>
690
										<?php endif; ?>
691
									</div>
692
								<?php endforeach; ?>
693
							<?php endif; ?>
694
						<?php endif; ?>
695
					<?php endif; ?>
696
				<?php endforeach; ?>
697
			</div>
698
			<?php
699
		}
700
	}
701
702
703
	/**
704
	 * Check if setting field has sub tabs/fields
705
	 *
706
	 * @since 1.8
707
	 *
708
	 * @param $field_setting
709
	 *
710
	 * @return bool
711
	 */
712
	private function has_sub_tab( $field_setting ) {
713
		$has_sub_tab = false;
714
		if ( array_key_exists( 'sub-fields', $field_setting ) ) {
715
			$has_sub_tab = true;
716
		}
717
718
		return $has_sub_tab;
719
	}
720
721
	/**
722
	 * CMB2 settings loader.
723
	 *
724
	 * @since  1.8
725
	 * @return array
726
	 */
727
	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...
728
		$all_cmb2_settings   = apply_filters( 'cmb2_meta_boxes', array() );
729
		$give_forms_settings = $all_cmb2_settings;
730
731
		// Filter settings: Use only give forms related settings.
732
		foreach ( $all_cmb2_settings as $index => $setting ) {
733
			if ( ! in_array( 'give_forms', $setting['object_types'] ) ) {
734
				unset( $give_forms_settings[ $index ] );
735
			}
736
		}
737
738
		return $give_forms_settings;
739
740
	}
741
742
	/**
743
	 * Check if we're saving, the trigger an action based on the post type.
744
	 *
745
	 * @since  1.8
746
	 *
747
	 * @param  int    $post_id
748
	 * @param  object $post
749
	 *
750
	 * @return void
751
	 */
752
	public function save( $post_id, $post ) {
753
754
		// $post_id and $post are required.
755
		if ( empty( $post_id ) || empty( $post ) ) {
756
			return;
757
		}
758
759
		// Don't save meta boxes for revisions or autosaves.
760
		if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
761
			return;
762
		}
763
764
		// Check the nonce.
765
		if ( empty( $_POST['give_form_meta_nonce'] ) || ! wp_verify_nonce( $_POST['give_form_meta_nonce'], 'give_save_form_meta' ) ) {
766
			return;
767
		}
768
769
		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
770
		if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
771
			return;
772
		}
773
774
		// Check user has permission to edit.
775
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
776
			return;
777
		}
778
779
		// Fire action before saving form meta.
780
		do_action( 'give_pre_process_give_forms_meta', $post_id, $post );
781
782
		/**
783
		 * Filter the meta key to save.
784
		 * Third party addon developer can remove there meta keys from this array to handle saving data on there own.
785
		 */
786
		$form_meta_keys = apply_filters( 'give_process_form_meta_keys', $this->get_meta_keys_from_settings() );
787
788
		// Save form meta data.
789
		if ( ! empty( $form_meta_keys ) ) {
790
			foreach ( $form_meta_keys as $form_meta_key ) {
791
792
				// Set default value for checkbox fields.
793
				if (
794
					! isset( $_POST[ $form_meta_key ] )
795
					&& ( 'checkbox' === $this->get_field_type( $form_meta_key ) )
796
				) {
797
					$_POST[ $form_meta_key ] = '';
798
				}
799
800
				if ( isset( $_POST[ $form_meta_key ] ) ) {
801
					$setting_field = $this->get_setting_field( $form_meta_key );
802
					if ( ! empty( $setting_field['type'] ) ) {
803
						switch ( $setting_field['type'] ) {
804
							case 'textarea':
805
							case 'wysiwyg':
806
								$form_meta_value = wp_kses_post( $_POST[ $form_meta_key ] );
807
								break;
808
809
							case 'group':
810
								$form_meta_value = array();
811
812
								foreach ( $_POST[ $form_meta_key ] as $index => $group ) {
0 ignored issues
show
Bug introduced by
The expression $_POST[$form_meta_key] of type string is not traversable.
Loading history...
813
814
									// Do not save template input field values.
815
									if ( '{{row-count-placeholder}}' === $index ) {
816
										continue;
817
									}
818
819
									$group_meta_value = array();
820
									foreach ( $group as $field_id => $field_value ) {
821
										switch ( $this->get_field_type( $field_id, $form_meta_key ) ) {
822
											case 'wysiwyg':
823
												$group_meta_value[ $field_id ] = wp_kses_post( $field_value );
824
												break;
825
826
											default:
827
												$group_meta_value[ $field_id ] = give_clean( $field_value );
828
										}
829
									}
830
831
									if ( ! empty( $group_meta_value ) ) {
832
										$form_meta_value[ $index ] = $group_meta_value;
833
									}
834
								}
835
836
837
								// Arrange repeater field keys in order.
838
								$form_meta_value = array_values( $form_meta_value );
839
								break;
840
841
							default:
842
								$form_meta_value = give_clean( $_POST[ $form_meta_key ] );
843
						}
844
845
846
						/**
847
						 * Filter the form meta value before saving
848
						 *
849
						 * @since 1.8.9
850
						 */
851
						$form_meta_value = apply_filters(
852
								'give_pre_save_form_meta_value',
853
								$this->sanitize_form_meta( $form_meta_value, $setting_field ),
854
								$form_meta_key,
855
								$this,
856
								$post_id
857
						);
858
859
						// Save data.
860
						give_update_meta( $post_id, $form_meta_key, $form_meta_value );
861
862
						// Fire after saving form meta key.
863
						do_action( "give_save_{$form_meta_key}", $form_meta_key, $form_meta_value, $post_id, $post );
864
					}
865
				}
866
			}
867
		}
868
869
		// Fire action after saving form meta.
870
		do_action( 'give_post_process_give_forms_meta', $post_id, $post );
871
	}
872
873
874
	/**
875
	 * Get field ID.
876
	 *
877
	 * @since 1.8
878
	 *
879
	 * @param array $field
880
	 *
881
	 * @return string
882
	 */
883
	private function get_field_id( $field ) {
884
		$field_id = '';
885
886
		if ( array_key_exists( 'id', $field ) ) {
887
			$field_id = $field['id'];
888
889
		}
890
891
		return $field_id;
892
	}
893
894
	/**
895
	 * Get fields ID.
896
	 *
897
	 * @since 1.8
898
	 *
899
	 * @param $setting
900
	 *
901
	 * @return array
902
	 */
903
	private function get_fields_id( $setting ) {
904
		$meta_keys = array();
905
906
		if ( ! empty( $setting ) ) {
907
			foreach ( $setting['fields'] as $field ) {
908
				if ( $field_id = $this->get_field_id( $field ) ) {
909
					$meta_keys[] = $field_id;
910
				}
911
			}
912
		}
913
914
		return $meta_keys;
915
	}
916
917
	/**
918
	 * Get sub fields ID.
919
	 *
920
	 * @since 1.8
921
	 *
922
	 * @param $setting
923
	 *
924
	 * @return array
925
	 */
926
	private function get_sub_fields_id( $setting ) {
927
		$meta_keys = array();
928
929
		if ( $this->has_sub_tab( $setting ) && ! empty( $setting['sub-fields'] ) ) {
930
			foreach ( $setting['sub-fields'] as $fields ) {
931
				if ( ! empty( $fields['fields'] ) ) {
932
					foreach ( $fields['fields'] as $field ) {
933
						if ( $field_id = $this->get_field_id( $field ) ) {
934
							$meta_keys[] = $field_id;
935
						}
936
					}
937
				}
938
			}
939
		}
940
941
		return $meta_keys;
942
	}
943
944
945
	/**
946
	 * Get all setting field ids.
947
	 *
948
	 * @since  1.8
949
	 * @return array
950
	 */
951
	private function get_meta_keys_from_settings() {
952
		$meta_keys = array();
953
954
		foreach ( $this->settings as $setting ) {
955
			if ( $this->has_sub_tab( $setting ) ) {
956
				$meta_key = $this->get_sub_fields_id( $setting );
957
			} else {
958
				$meta_key = $this->get_fields_id( $setting );
959
			}
960
961
			$meta_keys = array_merge( $meta_keys, $meta_key );
962
		}
963
964
		return $meta_keys;
965
	}
966
967
968
	/**
969
	 * Get field type.
970
	 *
971
	 * @since  1.8
972
	 *
973
	 * @param  string $field_id
974
	 * @param  string $group_id
975
	 *
976
	 * @return string
977
	 */
978
	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...
979
		$field = $this->get_setting_field( $field_id, $group_id );
980
981
		$type = array_key_exists( 'type', $field )
982
			? $field['type']
983
			: '';
984
985
		return $type;
986
	}
987
988
989
	/**
990
	 * Get Field
991
	 *
992
	 * @since 1.8
993
	 *
994
	 * @param array  $setting
995
	 * @param string $field_id
996
	 *
997
	 * @return array
998
	 */
999
	private function get_field( $setting, $field_id ) {
1000
		$setting_field = array();
1001
1002
		if ( ! empty( $setting['fields'] ) ) {
1003
			foreach ( $setting['fields'] as $field ) {
1004
				if ( array_key_exists( 'id', $field ) && $field['id'] === $field_id ) {
1005
					$setting_field = $field;
1006
					break;
1007
				}
1008
			}
1009
		}
1010
1011
		return $setting_field;
1012
	}
1013
1014
	/**
1015
	 * Get Sub Field
1016
	 *
1017
	 * @since 1.8
1018
	 *
1019
	 * @param array  $setting
1020
	 * @param string $field_id
1021
	 *
1022
	 * @return array
1023
	 */
1024
	private function get_sub_field( $setting, $field_id ) {
1025
		$setting_field = array();
1026
1027
		if ( ! empty( $setting['sub-fields'] ) ) {
1028
			foreach ( $setting['sub-fields'] as $fields ) {
1029
				if ( $field = $this->get_field( $fields, $field_id ) ) {
1030
					$setting_field = $field;
1031
					break;
1032
				}
1033
			}
1034
		}
1035
1036
		return $setting_field;
1037
	}
1038
1039
	/**
1040
	 * Get setting field.
1041
	 *
1042
	 * @since  1.8
1043
	 *
1044
	 * @param  string $field_id
1045
	 * @param  string $group_id Get sub field from group.
1046
	 *
1047
	 * @return array
1048
	 */
1049
	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...
1050
		$setting_field = array();
1051
1052
		$_field_id = $field_id;
1053
		$field_id  = empty( $group_id ) ? $field_id : $group_id;
1054
1055
		if ( ! empty( $this->settings ) ) {
1056
			foreach ( $this->settings as $setting ) {
1057
				if (
1058
					( $this->has_sub_tab( $setting ) && ( $setting_field = $this->get_sub_field( $setting, $field_id ) ) )
1059
					|| ( $setting_field = $this->get_field( $setting, $field_id ) )
1060
				) {
1061
					break;
1062
				}
1063
			}
1064
		}
1065
1066
1067
		// Get field from group.
1068
		if ( ! empty( $group_id ) ) {
1069
			foreach ( $setting_field['fields'] as $field ) {
1070
				if ( array_key_exists( 'id', $field ) && $field['id'] === $_field_id ) {
1071
					$setting_field = $field;
1072
				}
1073
			}
1074
		}
1075
1076
		return $setting_field;
1077
	}
1078
1079
1080
	/**
1081
	 * Add offline donations setting tab to donation form options metabox.
1082
	 *
1083
	 * @since  1.8
1084
	 *
1085
	 * @param  array $settings List of form settings.
1086
	 *
1087
	 * @return mixed
1088
	 */
1089
	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...
1090
		if ( give_is_gateway_active( 'offline' ) ) {
1091
			$settings['offline_donations_options'] = apply_filters( 'give_forms_offline_donations_options', array(
1092
				'id'        => 'offline_donations_options',
1093
				'title'     => __( 'Offline Donations', 'give' ),
1094
				'icon-html' => '<span class="give-icon give-icon-purse"></span>',
1095
				'fields'    => apply_filters( 'give_forms_offline_donations_metabox_fields', array() ),
1096
			) );
1097
		}
1098
1099
		return $settings;
1100
	}
1101
1102
1103
	/**
1104
	 * Sanitize form meta values before saving.
1105
	 *
1106
	 * @since  1.8.9
1107
	 * @access public
1108
	 *
1109
	 * @param mixed $meta_value
1110
	 * @param array $setting_field
1111
	 *
1112
	 * @return mixed
1113
	 */
1114
	function sanitize_form_meta( $meta_value, $setting_field ) {
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...
1115
		switch ( $setting_field['type'] ) {
1116
			case 'group':
1117
				if ( ! empty( $setting_field['fields'] ) ) {
1118
					foreach ( $setting_field['fields'] as $field ) {
1119
						if ( empty( $field['data_type'] ) || 'price' !== $field['data_type'] ) {
1120
							continue;
1121
						}
1122
1123
						foreach ( $meta_value as $index => $meta_data ) {
1124
							if( ! isset( $meta_value[ $index ][ $field['id'] ] ) ) {
1125
								continue;
1126
							}
1127
1128
							$meta_value[ $index ][ $field['id'] ] = ! empty( $meta_value[ $index ][ $field['id'] ] )
1129
								? give_sanitize_amount( $meta_value[ $index ][ $field['id'] ] )
1130
								: 0;
1131
						}
1132
					}
1133
				}
1134
				break;
1135
1136
			default:
1137
				if ( ! empty( $setting_field['data_type'] ) && 'price' === $setting_field['data_type'] ) {
1138
					$meta_value = $meta_value ? give_sanitize_amount( $meta_value ) : 0;
1139
				}
1140
		}
1141
1142
		return $meta_value;
1143
	}
1144
}
1145
1146
new Give_MetaBox_Form_Data();
1147
1148