PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.2
GiveWP – Donation Plugin and Fundraising Platform v2.0.2
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / admin / forms / class-metabox-form-data.php
give / includes / admin / forms Last commit date
class-metabox-form-data.php 8 years ago dashboard-columns.php 8 years ago metabox.php 9 years ago
class-metabox-form-data.php
1199 lines
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 = __( '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' ) );
62 // Add offline donations options.
63 add_filter( 'give_metabox_form_data_settings', array( $this, 'add_offline_donations_setting_tab' ), 0, 1 );
64
65 // Maintain active tab query parameter after save.
66 add_filter( 'redirect_post_location', array( $this, 'maintain_active_tab' ), 10, 2 );
67 }
68
69 /**
70 * Setup metabox related data.
71 *
72 * @since 1.8
73 *
74 * @return void
75 */
76 function setup() {
77 $this->settings = $this->get_settings();
78 }
79
80
81 /**
82 * Get metabox settings
83 *
84 * @since 1.8
85 *
86 * @return array
87 */
88 function get_settings() {
89 $post_id = give_get_admin_post_id();
90 $price_placeholder = give_format_decimal( '1.00', false, false );
91
92 // Start with an underscore to hide fields from custom fields list
93 $prefix = '_give_';
94
95 $settings = array(
96 /**
97 * Repeatable Field Groups
98 */
99 'form_field_options' => apply_filters( 'give_forms_field_options', array(
100 'id' => 'form_field_options',
101 'title' => __( 'Donation Options', 'give' ),
102 'icon-html' => '<span class="give-icon give-icon-heart"></span>',
103 'fields' => apply_filters( 'give_forms_donation_form_metabox_fields', array(
104 // Donation Option.
105 array(
106 'name' => __( 'Donation Option', 'give' ),
107 'description' => __( 'Do you want this form to have one set donation price or multiple levels (for example, $10, $20, $50)?', 'give' ),
108 'id' => $prefix . 'price_option',
109 'type' => 'radio_inline',
110 'default' => 'multi',
111 'options' => apply_filters( 'give_forms_price_options', array(
112 'multi' => __( 'Multi-level Donation', 'give' ),
113 'set' => __( 'Set Donation', 'give' ),
114 ) ),
115 ),
116 array(
117 'name' => __( 'Set Donation', 'give' ),
118 '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' ),
119 'id' => $prefix . 'set_price',
120 'type' => 'text_small',
121 'data_type' => 'price',
122 'attributes' => array(
123 'placeholder' => $price_placeholder,
124 'class' => 'give-money-field',
125 ),
126 ),
127 // Display Style.
128 array(
129 'name' => __( 'Display Style', 'give' ),
130 'description' => __( 'Set how the donations levels will display on the form.', 'give' ),
131 'id' => $prefix . 'display_style',
132 'type' => 'radio_inline',
133 'default' => 'buttons',
134 'options' => array(
135 'buttons' => __( 'Buttons', 'give' ),
136 'radios' => __( 'Radios', 'give' ),
137 'dropdown' => __( 'Dropdown', 'give' ),
138 ),
139 'wrapper_class' => 'give-hidden',
140 ),
141 // Custom Amount.
142 array(
143 'name' => __( 'Custom Amount', 'give' ),
144 'description' => __( 'Do you want the user to be able to input their own donation amount?', 'give' ),
145 'id' => $prefix . 'custom_amount',
146 'type' => 'radio_inline',
147 'default' => 'disabled',
148 'options' => array(
149 'enabled' => __( 'Enabled', 'give' ),
150 'disabled' => __( 'Disabled', 'give' ),
151 ),
152 ),
153 array(
154 'name' => __( 'Minimum Amount', 'give' ),
155 'description' => __( 'Enter the minimum custom donation amount.', 'give' ),
156 'id' => $prefix . 'custom_amount_minimum',
157 'type' => 'text_small',
158 'data_type' => 'price',
159 'attributes' => array(
160 'placeholder' => $price_placeholder,
161 'class' => 'give-money-field',
162 ),
163 'wrapper_class' => 'give-hidden',
164 ),
165 array(
166 'name' => __( 'Custom Amount Text', 'give' ),
167 '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' ),
168 'id' => $prefix . 'custom_amount_text',
169 'type' => 'text_medium',
170 'attributes' => array(
171 'rows' => 3,
172 'placeholder' => __( 'Give a Custom Amount', 'give' ),
173 ),
174 'wrapper_class' => 'give-hidden',
175 ),
176 // Donation Levels.
177 array(
178 'id' => $prefix . 'donation_levels',
179 'type' => 'group',
180 'options' => array(
181 'add_button' => __( 'Add Level', 'give' ),
182 'header_title' => __( 'Donation Level', 'give' ),
183 'remove_button' => '<span class="dashicons dashicons-no"></span>',
184 ),
185 'wrapper_class' => 'give-hidden',
186 // Fields array works the same, except id's only need to be unique for this group.
187 // Prefix is not needed.
188 'fields' => apply_filters( 'give_donation_levels_table_row', array(
189 array(
190 'name' => __( 'ID', 'give' ),
191 'id' => $prefix . 'id',
192 'type' => 'levels_id',
193 ),
194 array(
195 'name' => __( 'Amount', 'give' ),
196 'id' => $prefix . 'amount',
197 'type' => 'text_small',
198 'data_type' => 'price',
199 'attributes' => array(
200 'placeholder' => $price_placeholder,
201 'class' => 'give-money-field',
202 ),
203 ),
204 array(
205 'name' => __( 'Text', 'give' ),
206 'id' => $prefix . 'text',
207 'type' => 'text',
208 'attributes' => array(
209 'placeholder' => __( 'Donation Level', 'give' ),
210 'class' => 'give-multilevel-text-field',
211 ),
212 ),
213 array(
214 'name' => __( 'Default', 'give' ),
215 'id' => $prefix . 'default',
216 'type' => 'give_default_radio_inline',
217 ),
218 ) ),
219 ),
220 array(
221 'name' => 'donation_options_docs',
222 'type' => 'docs_link',
223 'url' => 'http://docs.givewp.com/form-donation-options',
224 'title' => __( 'Donation Options', 'give' ),
225 ),
226 ),
227 $post_id
228 ),
229 ) ),
230
231 /**
232 * Display Options
233 */
234 'form_display_options' => apply_filters( 'give_form_display_options', array(
235 'id' => 'form_display_options',
236 'title' => __( 'Form Display', 'give' ),
237 'icon-html' => '<span class="give-icon give-icon-display"></span>',
238 'fields' => apply_filters( 'give_forms_display_options_metabox_fields', array(
239 array(
240 'name' => __( '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' => __( 'All Fields', 'give' ),
246 'modal' => __( 'Modal', 'give' ),
247 'reveal' => __( 'Reveal', 'give' ),
248 'button' => __( 'Button', 'give' ),
249 ),
250 'default' => 'onpage',
251 ),
252 array(
253 'id' => $prefix . 'reveal_label',
254 'name' => __( 'Continue Button', 'give' ),
255 'desc' => __( 'The button label for displaying the additional payment fields.', 'give' ),
256 'type' => 'text_small',
257 'attributes' => array(
258 'placeholder' => __( 'Donate Now', 'give' ),
259 ),
260 'wrapper_class' => 'give-hidden',
261 ),
262 array(
263 'id' => $prefix . 'checkout_label',
264 'name' => __( 'Submit Button', 'give' ),
265 'desc' => __( 'The button label for completing a donation.', 'give' ),
266 'type' => 'text_small',
267 'attributes' => array(
268 'placeholder' => __( 'Donate Now', 'give' ),
269 ),
270 ),
271 array(
272 'name' => __( 'Default Gateway', 'give' ),
273 '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' ),
274 'id' => $prefix . 'default_gateway',
275 'type' => 'default_gateway',
276 ),
277 array(
278 'name' => __( 'Guest Donations', 'give' ),
279 'desc' => __( 'Do you want to allow non-logged-in users to make donations?', 'give' ),
280 'id' => $prefix . 'logged_in_only',
281 'type' => 'radio_inline',
282 'default' => 'enabled',
283 'options' => array(
284 'enabled' => __( 'Enabled', 'give' ),
285 'disabled' => __( 'Disabled', 'give' ),
286 ),
287 ),
288 array(
289 'name' => __( 'Registration', 'give' ),
290 'desc' => __( 'Display the registration and login forms in the payment section for non-logged-in users.', 'give' ),
291 'id' => $prefix . 'show_register_form',
292 'type' => 'radio',
293 'options' => array(
294 'none' => __( 'None', 'give' ),
295 'registration' => __( 'Registration', 'give' ),
296 'login' => __( 'Login', 'give' ),
297 'both' => __( 'Registration + Login', 'give' ),
298 ),
299 'default' => 'none',
300 ),
301 array(
302 'name' => __( 'Floating Labels', 'give' ),
303 /* translators: %s: forms http://docs.givewp.com/form-floating-labels */
304 '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' ) ),
305 'id' => $prefix . 'form_floating_labels',
306 'type' => 'radio_inline',
307 'options' => array(
308 'global' => __( 'Global Option', 'give' ),
309 'enabled' => __( 'Enabled', 'give' ),
310 'disabled' => __( 'Disabled', 'give' ),
311 ),
312 'default' => 'global',
313 ),
314 array(
315 'name' => 'form_display_docs',
316 'type' => 'docs_link',
317 'url' => 'http://docs.givewp.com/form-display-options',
318 'title' => __( 'Form Display', 'give' ),
319 ),
320 ),
321 $post_id
322 ),
323 )
324 ),
325
326 /**
327 * Donation Goals
328 */
329 'donation_goal_options' => apply_filters( 'give_donation_goal_options', array(
330 'id' => 'donation_goal_options',
331 'title' => __( 'Donation Goal', 'give' ),
332 'icon-html' => '<span class="give-icon give-icon-target"></span>',
333 'fields' => apply_filters( 'give_forms_donation_goal_metabox_fields', array(
334 // Goals
335 array(
336 'name' => __( 'Donation Goal', 'give' ),
337 'description' => __( 'Do you want to set a donation goal for this form?', 'give' ),
338 'id' => $prefix . 'goal_option',
339 'type' => 'radio_inline',
340 'default' => 'disabled',
341 'options' => array(
342 'enabled' => __( 'Enabled', 'give' ),
343 'disabled' => __( 'Disabled', 'give' ),
344 ),
345 ),
346
347 array(
348 'name' => __( 'Goal Format', 'give' ),
349 '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" or "1 of 5 donations".', 'give' ),
350 'id' => $prefix . 'goal_format',
351 'type' => 'radio_inline',
352 'default' => 'amount',
353 'options' => array(
354 'amount' => __( 'Amount', 'give' ),
355 'percentage' => __( 'Percentage', 'give' ),
356 'donation' => __( 'Number of Donations', 'give' ),
357 ),
358 ),
359
360 array(
361 'name' => __( 'Goal Amount', 'give' ),
362 'description' => __( 'This is the monetary goal amount you want to reach for this form.', 'give' ),
363 'id' => $prefix . 'set_goal',
364 'type' => 'text_small',
365 'data_type' => 'price',
366 'attributes' => array(
367 'placeholder' => $price_placeholder,
368 'class' => 'give-money-field',
369 ),
370 'wrapper_class' => 'give-hidden',
371 ),
372
373 array(
374 'id' => $prefix . 'number_of_donation_goal',
375 'name' => __( 'Donation Goal', 'give' ),
376 'desc' => __( 'Set total number of donations as a goal.', 'give' ),
377 'type' => 'number',
378 'default' => 1,
379 'attributes' => array(
380 'placeholder' => 1,
381 ),
382 ),
383
384 array(
385 'name' => __( 'Progress Bar Color', 'give' ),
386 'desc' => __( 'Customize the color of the goal progress bar.', 'give' ),
387 'id' => $prefix . 'goal_color',
388 'type' => 'colorpicker',
389 'default' => '#2bc253',
390 'wrapper_class' => 'give-hidden',
391 ),
392
393 array(
394 'name' => __( 'Close Form', 'give' ),
395 'desc' => __( 'Do you want to close the donation forms and stop accepting donations once this goal has been met?', 'give' ),
396 'id' => $prefix . 'close_form_when_goal_achieved',
397 'type' => 'radio_inline',
398 'default' => 'disabled',
399 'options' => array(
400 'enabled' => __( 'Enabled', 'give' ),
401 'disabled' => __( 'Disabled', 'give' ),
402 ),
403 'wrapper_class' => 'give-hidden',
404 ),
405 array(
406 'name' => __( 'Goal Achieved Message', 'give' ),
407 'desc' => __( 'Do you want to display a custom message when the goal is closed?', 'give' ),
408 'id' => $prefix . 'form_goal_achieved_message',
409 'type' => 'wysiwyg',
410 'default' => __( 'Thank you to all our donors, we have met our fundraising goal.', 'give' ),
411 'wrapper_class' => 'give-hidden',
412 ),
413 array(
414 'name' => 'donation_goal_docs',
415 'type' => 'docs_link',
416 'url' => 'http://docs.givewp.com/form-donation-goal',
417 'title' => __( 'Donation Goal', 'give' ),
418 ),
419 ),
420 $post_id
421 ),
422 ) ),
423
424 /**
425 * Content Field
426 */
427 'form_content_options' => apply_filters( 'give_forms_content_options', array(
428 'id' => 'form_content_options',
429 'title' => __( 'Form Content', 'give' ),
430 'icon-html' => '<span class="give-icon give-icon-edit"></span>',
431 'fields' => apply_filters( 'give_forms_content_options_metabox_fields', array(
432
433 // Donation content.
434 array(
435 'name' => __( 'Display Content', 'give' ),
436 'description' => __( 'Do you want to add custom content to this form?', 'give' ),
437 'id' => $prefix . 'display_content',
438 'type' => 'radio_inline',
439 'options' => array(
440 'enabled' => __( 'Enabled', 'give' ),
441 'disabled' => __( 'Disabled', 'give' ),
442 ),
443 'default' => 'disabled',
444 ),
445
446 // Content placement.
447 array(
448 'name' => __( 'Content Placement', 'give' ),
449 'description' => __( 'This option controls where the content appears within the donation form.', 'give' ),
450 'id' => $prefix . 'content_placement',
451 'type' => 'radio_inline',
452 'options' => apply_filters( 'give_forms_content_options_select', array(
453 'give_pre_form' => __( 'Above fields', 'give' ),
454 'give_post_form' => __( 'Below fields', 'give' ),
455 )
456 ),
457 'default' => 'give_pre_form',
458 'wrapper_class' => 'give-hidden',
459 ),
460 array(
461 'name' => __( 'Content', 'give' ),
462 'description' => __( 'This content will display on the single give form page.', 'give' ),
463 'id' => $prefix . 'form_content',
464 'type' => 'wysiwyg',
465 'wrapper_class' => 'give-hidden',
466 ),
467 array(
468 'name' => 'form_content_docs',
469 'type' => 'docs_link',
470 'url' => 'http://docs.givewp.com/form-content',
471 'title' => __( 'Form Content', 'give' ),
472 ),
473 ),
474 $post_id
475 ),
476 ) ),
477
478 /**
479 * Terms & Conditions
480 */
481 'form_terms_options' => apply_filters( 'give_forms_terms_options', array(
482 'id' => 'form_terms_options',
483 'title' => __( 'Terms & Conditions', 'give' ),
484 'icon-html' => '<span class="give-icon give-icon-checklist"></span>',
485 'fields' => apply_filters( 'give_forms_terms_options_metabox_fields', array(
486 // Donation Option
487 array(
488 'name' => __( 'Terms and Conditions', 'give' ),
489 'description' => __( 'Do you want to require the donor to accept terms prior to being able to complete their donation?', 'give' ),
490 'id' => $prefix . 'terms_option',
491 'type' => 'radio_inline',
492 'options' => apply_filters( 'give_forms_content_options_select', array(
493 'global' => __( 'Global Option', 'give' ),
494 'enabled' => __( 'Customize', 'give' ),
495 'disabled' => __( 'Disable', 'give' ),
496 )
497 ),
498 'default' => 'global',
499 ),
500 array(
501 'id' => $prefix . 'agree_label',
502 'name' => __( 'Agreement Label', 'give' ),
503 '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' ),
504 'type' => 'text',
505 'size' => 'regular',
506 'attributes' => array(
507 'placeholder' => __( 'Agree to Terms?', 'give' ),
508 ),
509 'wrapper_class' => 'give-hidden',
510 ),
511 array(
512 'id' => $prefix . 'agree_text',
513 'name' => __( 'Agreement Text', 'give' ),
514 'desc' => __( 'This is the actual text which the user will have to agree to in order to make a donation.', 'give' ),
515 'default' => give_get_option( 'agreement_text' ),
516 'type' => 'wysiwyg',
517 'wrapper_class' => 'give-hidden',
518 ),
519 array(
520 'name' => 'terms_docs',
521 'type' => 'docs_link',
522 'url' => 'http://docs.givewp.com/form-terms',
523 'title' => __( 'Terms and Conditions', 'give' ),
524 ),
525 ),
526 $post_id
527 ),
528 ) ),
529 );
530
531 /**
532 * Filter the metabox tabbed panel settings.
533 */
534 $settings = apply_filters( 'give_metabox_form_data_settings', $settings, $post_id );
535
536 // Output.
537 return $settings;
538 }
539
540 /**
541 * Add metabox.
542 *
543 * @since 1.8
544 *
545 * @return void
546 */
547 public function add_meta_box() {
548 add_meta_box(
549 $this->get_metabox_ID(),
550 $this->get_metabox_label(),
551 array( $this, 'output' ),
552 array( 'give_forms' ),
553 'normal',
554 'high'
555 );
556 }
557
558
559 /**
560 * Enqueue scripts.
561 *
562 * @since 1.8
563 *
564 * @return void
565 */
566 function enqueue_script() {
567 global $post;
568
569 if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
570
571 }
572 }
573
574 /**
575 * Get metabox id.
576 *
577 * @since 1.8
578 *
579 * @return string
580 */
581 function get_metabox_ID() {
582 return $this->metabox_id;
583 }
584
585 /**
586 * Get metabox label.
587 *
588 * @since 1.8
589 *
590 * @return string
591 */
592 function get_metabox_label() {
593 return $this->metabox_label;
594 }
595
596
597 /**
598 * Get metabox tabs.
599 *
600 * @since 1.8
601 *
602 * @return array
603 */
604 public function get_tabs() {
605 $tabs = array();
606
607 if ( ! empty( $this->settings ) ) {
608 foreach ( $this->settings as $setting ) {
609 if ( ! isset( $setting['id'] ) || ! isset( $setting['title'] ) ) {
610 continue;
611 }
612 $tab = array(
613 'id' => $setting['id'],
614 'label' => $setting['title'],
615 'icon-html' => ( ! empty( $setting['icon-html'] ) ? $setting['icon-html'] : '' ),
616 );
617
618 if ( $this->has_sub_tab( $setting ) ) {
619 if ( empty( $setting['sub-fields'] ) ) {
620 $tab = array();
621 } else {
622 foreach ( $setting['sub-fields'] as $sub_fields ) {
623 $tab['sub-fields'][] = array(
624 'id' => $sub_fields['id'],
625 'label' => $sub_fields['title'],
626 'icon-html' => ( ! empty( $sub_fields['icon-html'] ) ? $sub_fields['icon-html'] : '' ),
627 );
628 }
629 }
630 }
631
632 if ( ! empty( $tab ) ) {
633 $tabs[] = $tab;
634 }
635 }
636 }
637
638 return $tabs;
639 }
640
641 /**
642 * Output metabox settings.
643 *
644 * @since 1.8
645 *
646 * @return void
647 */
648 public function output() {
649 // Bailout.
650 if ( $form_data_tabs = $this->get_tabs() ) :
651 $active_tab = ! empty( $_GET['give_tab'] ) ? give_clean( $_GET['give_tab'] ) : 'form_field_options';
652 wp_nonce_field( 'give_save_form_meta', 'give_form_meta_nonce' );
653 ?>
654 <input id="give_form_active_tab" type="hidden" name="give_form_active_tab">
655 <div class="give-metabox-panel-wrap">
656 <ul class="give-form-data-tabs give-metabox-tabs">
657 <?php foreach ( $form_data_tabs as $index => $form_data_tab ) : ?>
658 <?php
659 // Determine if current tab is active.
660 $is_active = $active_tab === $form_data_tab['id'] ? true : false;
661 ?>
662 <li class="<?php echo "{$form_data_tab['id']}_tab" . ( $is_active ? ' active' : '' ) . ( $this->has_sub_tab( $form_data_tab ) ? ' has-sub-fields' : '' ); ?>">
663 <a href="#<?php echo $form_data_tab['id']; ?>" data-tab-id="<?php echo $form_data_tab['id']; ?>">
664 <?php if ( ! empty( $form_data_tab['icon-html'] ) ) : ?>
665 <?php echo $form_data_tab['icon-html']; ?>
666 <?php else : ?>
667 <span class="give-icon give-icon-default"></span>
668 <?php endif; ?>
669 <span class="give-label"><?php echo $form_data_tab['label']; ?></span>
670 </a>
671 <?php if ( $this->has_sub_tab( $form_data_tab ) ) : ?>
672 <ul class="give-metabox-sub-tabs give-hidden">
673 <?php foreach ( $form_data_tab['sub-fields'] as $sub_tab ) : ?>
674 <li class="<?php echo "{$sub_tab['id']}_tab"; ?>">
675 <a href="#<?php echo $sub_tab['id']; ?>" data-tab-id="<?php echo $sub_tab['id']; ?>">
676 <?php if ( ! empty( $sub_tab['icon-html'] ) ) : ?>
677 <?php echo $sub_tab['icon-html']; ?>
678 <?php else : ?>
679 <span class="give-icon give-icon-default"></span>
680 <?php endif; ?>
681 <span class="give-label"><?php echo $sub_tab['label']; ?></span>
682 </a>
683 </li>
684 <?php endforeach; ?>
685 </ul>
686 <?php endif; ?>
687 </li>
688 <?php endforeach; ?>
689 </ul>
690
691 <?php foreach ( $this->settings as $setting ) : ?>
692 <?php do_action( "give_before_{$setting['id']}_settings" ); ?>
693 <?php
694 // Determine if current panel is active.
695 $is_active = $active_tab === $setting['id'] ? true : false;
696 ?>
697 <div id="<?php echo $setting['id']; ?>" class="panel give_options_panel<?php echo( $is_active ? ' active' : '' ); ?>">
698 <?php if ( ! empty( $setting['fields'] ) ) : ?>
699 <?php foreach ( $setting['fields'] as $field ) : ?>
700 <?php give_render_field( $field ); ?>
701 <?php endforeach; ?>
702 <?php endif; ?>
703 </div>
704 <?php do_action( "give_after_{$setting['id']}_settings" ); ?>
705
706
707 <?php if ( $this->has_sub_tab( $setting ) ) : ?>
708 <?php if ( ! empty( $setting['sub-fields'] ) ) : ?>
709 <?php foreach ( $setting['sub-fields'] as $index => $sub_fields ) : ?>
710 <div id="<?php echo $sub_fields['id']; ?>" class="panel give_options_panel give-hidden">
711 <?php if ( ! empty( $sub_fields['fields'] ) ) : ?>
712 <?php foreach ( $sub_fields['fields'] as $sub_field ) : ?>
713 <?php give_render_field( $sub_field ); ?>
714 <?php endforeach; ?>
715 <?php endif; ?>
716 </div>
717 <?php endforeach; ?>
718 <?php endif; ?>
719 <?php endif; ?>
720 <?php endforeach; ?>
721 </div>
722 <?php
723 endif; // End if().
724 }
725
726
727 /**
728 * Check if setting field has sub tabs/fields
729 *
730 * @param array $field_setting Field Settings.
731 *
732 * @since 1.8
733 *
734 * @return bool
735 */
736 private function has_sub_tab( $field_setting ) {
737 $has_sub_tab = false;
738 if ( array_key_exists( 'sub-fields', $field_setting ) ) {
739 $has_sub_tab = true;
740 }
741
742 return $has_sub_tab;
743 }
744
745 /**
746 * CMB2 settings loader.
747 *
748 * @since 1.8
749 *
750 * @return array
751 */
752 function cmb2_metabox_settings() {
753 $all_cmb2_settings = apply_filters( 'cmb2_meta_boxes', array() );
754 $give_forms_settings = $all_cmb2_settings;
755
756 // Filter settings: Use only give forms related settings.
757 foreach ( $all_cmb2_settings as $index => $setting ) {
758 if ( ! in_array( 'give_forms', $setting['object_types'] ) ) {
759 unset( $give_forms_settings[ $index ] );
760 }
761 }
762
763 return $give_forms_settings;
764
765 }
766
767 /**
768 * Check if we're saving, the trigger an action based on the post type.
769 *
770 * @param int $post_id Post ID.
771 * @param int|object $post Post Object.
772 *
773 * @since 1.8
774 *
775 * @return void
776 */
777 public function save( $post_id, $post ) {
778
779 // $post_id and $post are required.
780 if ( empty( $post_id ) || empty( $post ) ) {
781 return;
782 }
783
784 // Don't save meta boxes for revisions or autosaves.
785 if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
786 return;
787 }
788
789 // Check the nonce.
790 if ( empty( $_POST['give_form_meta_nonce'] ) || ! wp_verify_nonce( $_POST['give_form_meta_nonce'], 'give_save_form_meta' ) ) {
791 return;
792 }
793
794 // Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
795 if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
796 return;
797 }
798
799 // Check user has permission to edit.
800 if ( ! current_user_can( 'edit_post', $post_id ) ) {
801 return;
802 }
803
804 // Fire action before saving form meta.
805 do_action( 'give_pre_process_give_forms_meta', $post_id, $post );
806
807 /**
808 * Filter the meta key to save.
809 * Third party addon developer can remove there meta keys from this array to handle saving data on there own.
810 */
811 $form_meta_keys = apply_filters( 'give_process_form_meta_keys', $this->get_meta_keys_from_settings() );
812
813 // Save form meta data.
814 if ( ! empty( $form_meta_keys ) ) {
815 foreach ( $form_meta_keys as $form_meta_key ) {
816
817 // Set default value for checkbox fields.
818 if (
819 ! isset( $_POST[ $form_meta_key ] )
820 && ( 'checkbox' === $this->get_field_type( $form_meta_key ) )
821 ) {
822 $_POST[ $form_meta_key ] = '';
823 }
824
825 if ( isset( $_POST[ $form_meta_key ] ) ) {
826 $setting_field = $this->get_setting_field( $form_meta_key );
827 if ( ! empty( $setting_field['type'] ) ) {
828 switch ( $setting_field['type'] ) {
829 case 'textarea':
830 case 'wysiwyg':
831 $form_meta_value = wp_kses_post( $_POST[ $form_meta_key ] );
832 break;
833
834 case 'group':
835 $form_meta_value = array();
836
837 foreach ( $_POST[ $form_meta_key ] as $index => $group ) {
838
839 // Do not save template input field values.
840 if ( '{{row-count-placeholder}}' === $index ) {
841 continue;
842 }
843
844 $group_meta_value = array();
845 foreach ( $group as $field_id => $field_value ) {
846 switch ( $this->get_field_type( $field_id, $form_meta_key ) ) {
847 case 'wysiwyg':
848 $group_meta_value[ $field_id ] = wp_kses_post( $field_value );
849 break;
850
851 default:
852 $group_meta_value[ $field_id ] = give_clean( $field_value );
853 }
854 }
855
856 if ( ! empty( $group_meta_value ) ) {
857 $form_meta_value[ $index ] = $group_meta_value;
858 }
859 }
860
861 // Arrange repeater field keys in order.
862 $form_meta_value = array_values( $form_meta_value );
863 break;
864
865 default:
866 $form_meta_value = give_clean( $_POST[ $form_meta_key ] );
867 }// End switch().
868
869 /**
870 * Filter the form meta value before saving
871 *
872 * @since 1.8.9
873 */
874 $form_meta_value = apply_filters(
875 'give_pre_save_form_meta_value',
876 $this->sanitize_form_meta( $form_meta_value, $setting_field ),
877 $form_meta_key,
878 $this,
879 $post_id
880 );
881
882 // Save data.
883 give_update_meta( $post_id, $form_meta_key, $form_meta_value );
884
885 // Fire after saving form meta key.
886 do_action( "give_save_{$form_meta_key}", $form_meta_key, $form_meta_value, $post_id, $post );
887 }// End if().
888 }// End if().
889 }// End foreach().
890 }// End if().
891
892 // Fire action after saving form meta.
893 do_action( 'give_post_process_give_forms_meta', $post_id, $post );
894 }
895
896
897 /**
898 * Get field ID.
899 *
900 * @param array $field Array of Fields.
901 *
902 * @since 1.8
903 *
904 * @return string
905 */
906 private function get_field_id( $field ) {
907 $field_id = '';
908
909 if ( array_key_exists( 'id', $field ) ) {
910 $field_id = $field['id'];
911
912 }
913
914 return $field_id;
915 }
916
917 /**
918 * Get fields ID.
919 *
920 * @param array $setting Array of settings.
921 *
922 * @since 1.8
923 *
924 * @return array
925 */
926 private function get_fields_id( $setting ) {
927 $meta_keys = array();
928
929 if ( ! empty( $setting ) ) {
930 foreach ( $setting['fields'] as $field ) {
931 if ( $field_id = $this->get_field_id( $field ) ) {
932 $meta_keys[] = $field_id;
933 }
934 }
935 }
936
937 return $meta_keys;
938 }
939
940 /**
941 * Get sub fields ID.
942 *
943 * @param array $setting Array of settings.
944 *
945 * @since 1.8
946 *
947 * @return array
948 */
949 private function get_sub_fields_id( $setting ) {
950 $meta_keys = array();
951
952 if ( $this->has_sub_tab( $setting ) && ! empty( $setting['sub-fields'] ) ) {
953 foreach ( $setting['sub-fields'] as $fields ) {
954 if ( ! empty( $fields['fields'] ) ) {
955 foreach ( $fields['fields'] as $field ) {
956 if ( $field_id = $this->get_field_id( $field ) ) {
957 $meta_keys[] = $field_id;
958 }
959 }
960 }
961 }
962 }
963
964 return $meta_keys;
965 }
966
967
968 /**
969 * Get all setting field ids.
970 *
971 * @since 1.8
972 *
973 * @return array
974 */
975 private function get_meta_keys_from_settings() {
976 $meta_keys = array();
977
978 foreach ( $this->settings as $setting ) {
979 $meta_key = $this->get_fields_id( $setting );
980
981 if ( $this->has_sub_tab( $setting ) ) {
982 $meta_key = array_merge( $meta_key, $this->get_sub_fields_id( $setting ) );
983 }
984
985 $meta_keys = array_merge( $meta_keys, $meta_key );
986 }
987
988 return $meta_keys;
989 }
990
991
992 /**
993 * Get field type.
994 *
995 * @param string $field_id Field ID.
996 * @param string $group_id Field Group ID.
997 *
998 * @since 1.8
999 *
1000 * @return string
1001 */
1002 function get_field_type( $field_id, $group_id = '' ) {
1003 $field = $this->get_setting_field( $field_id, $group_id );
1004
1005 $type = array_key_exists( 'type', $field )
1006 ? $field['type']
1007 : '';
1008
1009 return $type;
1010 }
1011
1012
1013 /**
1014 * Get Field
1015 *
1016 * @param array $setting Settings array.
1017 * @param string $field_id Field ID.
1018 *
1019 * @since 1.8
1020 *
1021 * @return array
1022 */
1023 private function get_field( $setting, $field_id ) {
1024 $setting_field = array();
1025
1026 if ( ! empty( $setting['fields'] ) ) {
1027 foreach ( $setting['fields'] as $field ) {
1028 if ( array_key_exists( 'id', $field ) && $field['id'] === $field_id ) {
1029 $setting_field = $field;
1030 break;
1031 }
1032 }
1033 }
1034
1035 return $setting_field;
1036 }
1037
1038 /**
1039 * Get Sub Field
1040 *
1041 * @param array $setting Settings array.
1042 * @param string $field_id Field ID.
1043 *
1044 * @since 1.8
1045 *
1046 * @return array
1047 */
1048 private function get_sub_field( $setting, $field_id ) {
1049 $setting_field = array();
1050
1051 if ( ! empty( $setting['sub-fields'] ) ) {
1052 foreach ( $setting['sub-fields'] as $fields ) {
1053 if ( $field = $this->get_field( $fields, $field_id ) ) {
1054 $setting_field = $field;
1055 break;
1056 }
1057 }
1058 }
1059
1060 return $setting_field;
1061 }
1062
1063 /**
1064 * Get setting field.
1065 *
1066 * @param string $field_id Field ID.
1067 * @param string $group_id Get sub field from group.
1068 *
1069 * @since 1.8
1070 *
1071 * @return array
1072 */
1073 function get_setting_field( $field_id, $group_id = '' ) {
1074 $setting_field = array();
1075
1076 $_field_id = $field_id;
1077 $field_id = empty( $group_id ) ? $field_id : $group_id;
1078
1079 if ( ! empty( $this->settings ) ) {
1080 foreach ( $this->settings as $setting ) {
1081 if (
1082 ( $this->has_sub_tab( $setting ) && ( $setting_field = $this->get_sub_field( $setting, $field_id ) ) )
1083 || ( $setting_field = $this->get_field( $setting, $field_id ) )
1084 ) {
1085 break;
1086 }
1087 }
1088 }
1089
1090 // Get field from group.
1091 if ( ! empty( $group_id ) ) {
1092 foreach ( $setting_field['fields'] as $field ) {
1093 if ( array_key_exists( 'id', $field ) && $field['id'] === $_field_id ) {
1094 $setting_field = $field;
1095 }
1096 }
1097 }
1098
1099 return $setting_field;
1100 }
1101
1102
1103 /**
1104 * Add offline donations setting tab to donation form options metabox.
1105 *
1106 * @param array $settings List of form settings.
1107 *
1108 * @since 1.8
1109 *
1110 * @return mixed
1111 */
1112 function add_offline_donations_setting_tab( $settings ) {
1113 if ( give_is_gateway_active( 'offline' ) ) {
1114 $settings['offline_donations_options'] = apply_filters( 'give_forms_offline_donations_options', array(
1115 'id' => 'offline_donations_options',
1116 'title' => __( 'Offline Donations', 'give' ),
1117 'icon-html' => '<span class="give-icon give-icon-purse"></span>',
1118 'fields' => apply_filters( 'give_forms_offline_donations_metabox_fields', array() ),
1119 ) );
1120 }
1121
1122 return $settings;
1123 }
1124
1125
1126 /**
1127 * Sanitize form meta values before saving.
1128 *
1129 * @param mixed $meta_value Meta Value for sanitizing before saving.
1130 * @param array $setting_field Setting Field.
1131 *
1132 * @since 1.8.9
1133 * @access public
1134 *
1135 * @return mixed
1136 */
1137 function sanitize_form_meta( $meta_value, $setting_field ) {
1138 switch ( $setting_field['type'] ) {
1139 case 'group':
1140 if ( ! empty( $setting_field['fields'] ) ) {
1141 foreach ( $setting_field['fields'] as $field ) {
1142 if ( empty( $field['data_type'] ) || 'price' !== $field['data_type'] ) {
1143 continue;
1144 }
1145
1146 foreach ( $meta_value as $index => $meta_data ) {
1147 if ( ! isset( $meta_value[ $index ][ $field['id'] ] ) ) {
1148 continue;
1149 }
1150
1151 $meta_value[ $index ][ $field['id'] ] = ! empty( $meta_value[ $index ][ $field['id'] ] ) ?
1152 give_sanitize_amount_for_db( $meta_value[ $index ][ $field['id'] ] ) :
1153 ( ( '_give_amount' === $field['id'] && empty( $field_value ) ) ?
1154 give_sanitize_amount_for_db( '1.00' ) :
1155 0 );
1156 }
1157 }
1158 }
1159 break;
1160
1161 default:
1162 if ( ! empty( $setting_field['data_type'] ) && 'price' === $setting_field['data_type'] ) {
1163 $meta_value = $meta_value ?
1164 give_sanitize_amount_for_db( $meta_value ) :
1165 ( in_array( $setting_field['id'], array( '_give_set_price', '_give_custom_amount_minimum', '_give_set_goal' ) ) ?
1166 give_sanitize_amount_for_db( '1.00' ) :
1167 0 );
1168 }
1169 }
1170
1171 return $meta_value;
1172 }
1173
1174 /**
1175 * Maintain the active tab after save.
1176 *
1177 * @param string $location The destination URL.
1178 * @param int $post_id The post ID.
1179 *
1180 * @since 1.8.13
1181 * @access public
1182 *
1183 * @return string The URL after redirect.
1184 */
1185 public function maintain_active_tab( $location, $post_id ) {
1186 if (
1187 'give_forms' === get_post_type( $post_id ) &&
1188 ! empty( $_POST['give_form_active_tab'] )
1189 ) {
1190 $location = add_query_arg( 'give_tab', give_clean( $_POST['give_form_active_tab'] ), $location );
1191 }
1192
1193 return $location;
1194 }
1195 }
1196
1197 new Give_MetaBox_Form_Data();
1198
1199