PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.7.3
GiveWP – Donation Plugin and Fundraising Platform v2.7.3
4.16.4 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 / give-metabox-functions.php
give / includes / admin Last commit date
add-ons 6 years ago donors 6 years ago emails 6 years ago forms 5 years ago payments 6 years ago reports 6 years ago settings 5 years ago shortcodes 6 years ago tools 6 years ago upgrades 6 years ago views 6 years ago abstract-admin-settings-page.php 6 years ago admin-actions.php 6 years ago admin-filters.php 6 years ago admin-footer.php 6 years ago admin-pages.php 6 years ago class-addon-activation-banner.php 6 years ago class-admin-settings.php 5 years ago class-api-keys-table.php 6 years ago class-blank-slate.php 6 years ago class-give-admin.php 6 years ago class-give-html-elements.php 6 years ago class-give-welcome.php 6 years ago class-i18n-module.php 6 years ago dashboard-widgets.php 6 years ago give-metabox-functions.php 5 years ago import-functions.php 6 years ago misc-functions.php 6 years ago plugins.php 6 years ago setting-page-functions.php 6 years ago
give-metabox-functions.php
1627 lines
1 <?php
2 /**
3 * Give Meta Box Functions
4 *
5 * @package Give
6 * @subpackage Functions
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.8
10 */
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly
13 }
14
15
16 /**
17 * Check if field callback exist or not.
18 *
19 * @since 1.8
20 *
21 * @param $field
22 *
23 * @return bool|string
24 */
25 function give_is_field_callback_exist( $field ) {
26 return ( give_get_field_callback( $field ) ? true : false );
27 }
28
29 /**
30 * Get field callback.
31 *
32 * @since 1.8
33 *
34 * @param $field
35 *
36 * @return bool|string
37 */
38 function give_get_field_callback( $field ) {
39 $func_name_prefix = 'give';
40 $func_name = '';
41
42 // Set callback function on basis of cmb2 field name.
43 switch ( $field['type'] ) {
44 case 'radio_inline':
45 $func_name = "{$func_name_prefix}_radio";
46 break;
47
48 case 'text':
49 case 'text-medium':
50 case 'text_medium':
51 case 'text-small':
52 case 'text_small':
53 case 'number':
54 case 'email':
55 $func_name = "{$func_name_prefix}_text_input";
56 break;
57
58 case 'textarea':
59 $func_name = "{$func_name_prefix}_textarea_input";
60 break;
61
62 case 'colorpicker':
63 $func_name = "{$func_name_prefix}_{$field['type']}";
64 break;
65
66 case 'hidden':
67 case 'levels_id':
68 $func_name = "{$func_name_prefix}_hidden_input";
69 break;
70
71 case 'group':
72 $func_name = "_{$func_name_prefix}_metabox_form_data_repeater_fields";
73 break;
74
75 case 'give_default_radio_inline':
76 $func_name = "{$func_name_prefix}_radio";
77 break;
78
79 case 'donation_limit':
80 $func_name = "{$func_name_prefix}_donation_limit";
81 break;
82
83 case 'chosen':
84 $func_name = "{$func_name_prefix}_chosen_input";
85 break;
86
87 case 'label':
88 $func_name = "{$func_name_prefix}_label_field";
89 break;
90
91 default:
92 if (
93 array_key_exists( 'callback', $field )
94 && ! empty( $field['callback'] )
95 ) {
96 $func_name = $field['callback'];
97 } else {
98 $func_name = "{$func_name_prefix}_{$field['type']}";
99 }
100 }
101
102 /**
103 * Filter the metabox setting render function
104 *
105 * @since 1.8
106 */
107 $func_name = apply_filters( 'give_get_field_callback', $func_name, $field );
108
109 // Exit if not any function exist.
110 // Check if render callback exist or not.
111 if ( empty( $func_name ) ) {
112 return false;
113 } elseif ( is_string( $func_name ) && ! function_exists( "$func_name" ) ) {
114 return false;
115 } elseif ( is_array( $func_name ) && ! method_exists( $func_name[0], "$func_name[1]" ) ) {
116 return false;
117 }
118
119 return $func_name;
120 }
121
122 /**
123 * This function adds backward compatibility to render cmb2 type field type.
124 *
125 * @since 1.8
126 *
127 * @param array $field Field argument array.
128 *
129 * @return bool
130 */
131 function give_render_field( $field ) {
132
133 // Check if render callback exist or not.
134 if ( ! ( $func_name = give_get_field_callback( $field ) ) ) {
135 return false;
136 }
137
138 // CMB2 compatibility: Push all classes to attributes's class key
139 if ( empty( $field['class'] ) ) {
140 $field['class'] = '';
141 }
142
143 if ( empty( $field['attributes']['class'] ) ) {
144 $field['attributes']['class'] = '';
145 }
146
147 $field['attributes']['class'] = trim( "give-field {$field['attributes']['class']} give-{$field['type']} {$field['class']}" );
148 unset( $field['class'] );
149
150 // CMB2 compatibility: Set wrapper class if any.
151 if ( ! empty( $field['row_classes'] ) ) {
152 $field['wrapper_class'] = $field['row_classes'];
153 unset( $field['row_classes'] );
154 }
155
156 // Set field params on basis of cmb2 field name.
157 switch ( $field['type'] ) {
158 case 'radio_inline':
159 if ( empty( $field['wrapper_class'] ) ) {
160 $field['wrapper_class'] = '';
161 }
162 $field['wrapper_class'] .= ' give-inline-radio-fields';
163
164 break;
165
166 case 'text':
167 case 'text-medium':
168 case 'text_medium':
169 case 'text-small':
170 case 'text_small':
171 // CMB2 compatibility: Set field type to text.
172 $field['type'] = isset( $field['attributes']['type'] ) ? $field['attributes']['type'] : 'text';
173
174 // CMB2 compatibility: Set data type to price.
175 if (
176 empty( $field['data_type'] )
177 && ! empty( $field['attributes']['class'] )
178 && (
179 false !== strpos( $field['attributes']['class'], 'money' )
180 || false !== strpos( $field['attributes']['class'], 'amount' )
181 )
182 ) {
183 $field['data_type'] = 'decimal';
184 }
185 break;
186
187 case 'levels_id':
188 $field['type'] = 'hidden';
189 break;
190
191 case 'colorpicker':
192 $field['type'] = 'text';
193 $field['class'] = 'give-colorpicker';
194 break;
195
196 case 'give_default_radio_inline':
197 $field['type'] = 'radio';
198 $field['options'] = [
199 'default' => __( 'Default', 'give' ),
200 ];
201 break;
202
203 case 'donation_limit':
204 $field['type'] = 'donation_limit';
205 break;
206 } // End switch().
207
208 // CMB2 compatibility: Add support to define field description by desc & description param.
209 // We encourage you to use description param.
210 $field['description'] = ( ! empty( $field['description'] )
211 ? $field['description']
212 : ( ! empty( $field['desc'] ) ? $field['desc'] : '' ) );
213
214 // Call render function.
215 if ( is_array( $func_name ) ) {
216 $func_name[0]->{$func_name[1]}( $field );
217 } else {
218 $func_name( $field );
219 }
220
221 return true;
222 }
223
224 /**
225 * Output a text input box.
226 *
227 * @since 1.8
228 *
229 * @param array $field {
230 * Optional. Array of text input field arguments.
231 *
232 * @type string $id Field ID. Default ''.
233 * @type string $style CSS style for input field. Default ''.
234 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
235 * @type string $value Value of input field. Default ''.
236 * @type string $name Name of input field. Default ''.
237 * @type string $type Type of input field. Default 'text'.
238 * @type string $before_field Text/HTML to add before input field. Default ''.
239 * @type string $after_field Text/HTML to add after input field. Default ''.
240 * @type string $data_type Define data type for value of input to filter it properly. Default ''.
241 * @type string $description Description of input field. Default ''.
242 * @type array $attributes List of attributes of input field. Default array().
243 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
244 * => '****' )
245 * }
246 * @return void
247 */
248 function give_text_input( $field ) {
249 global $thepostid, $post;
250
251 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
252 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
253 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
254 $field['value'] = give_get_field_value( $field, $thepostid );
255 $field['type'] = isset( $field['type'] ) ? $field['type'] : 'text';
256 $field['before_field'] = '';
257 $field['after_field'] = '';
258 $data_type = empty( $field['data_type'] ) ? '' : $field['data_type'];
259
260 switch ( $data_type ) {
261 case 'price':
262 $field['value'] = ( ! empty( $field['value'] ) ? give_format_decimal( give_maybe_sanitize_amount( $field['value'] ), false, false ) : $field['value'] );
263
264 $field['before_field'] = ! empty( $field['before_field'] ) ? $field['before_field'] : ( give_get_option( 'currency_position', 'before' ) == 'before' ? '<span class="give-money-symbol give-money-symbol-before">' . give_currency_symbol() . '</span>' : '' );
265 $field['after_field'] = ! empty( $field['after_field'] ) ? $field['after_field'] : ( give_get_option( 'currency_position', 'before' ) == 'after' ? '<span class="give-money-symbol give-money-symbol-after">' . give_currency_symbol() . '</span>' : '' );
266 break;
267
268 case 'decimal':
269 $field['attributes']['class'] .= ' give_input_decimal';
270 $field['value'] = ( ! empty( $field['value'] ) ? give_format_decimal( give_maybe_sanitize_amount( $field['value'] ), false, false ) : $field['value'] );
271 break;
272
273 default:
274 break;
275 }
276
277 ?>
278 <p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
279 <label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
280 <?php echo $field['before_field']; ?>
281 <input
282 type="<?php echo esc_attr( $field['type'] ); ?>"
283 style="<?php echo esc_attr( $field['style'] ); ?>"
284 name="<?php echo give_get_field_name( $field ); ?>"
285 id="<?php echo esc_attr( $field['id'] ); ?>"
286 value="<?php echo esc_attr( $field['value'] ); ?>"
287 <?php echo give_get_attribute_str( $field ); ?>
288 />
289 <?php echo $field['after_field']; ?>
290 <?php
291 echo give_get_field_description( $field );
292 echo '</p>';
293 }
294
295 /**
296 * Output a chosen input box.
297 * Note: only for internal use.
298 *
299 * @param array $field {
300 * Optional. Array of text input field arguments.
301 *
302 * @type string $id Field ID. Default ''.
303 * @type string $style CSS style for input field. Default ''.
304 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
305 * @type string $value Value of input field. Default ''.
306 * @type string $name Name of input field. Default ''.
307 * @type string $type Type of input field. Default 'text'.
308 * @type string $before_field Text/HTML to add before input field. Default ''.
309 * @type string $after_field Text/HTML to add after input field. Default ''.
310 * @type string $data_type Define data type for value of input to filter it properly. Default ''.
311 * @type string $description Description of input field. Default ''.
312 * @type array $attributes List of attributes of input field. Default array().
313 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
314 * => '****' )
315 * }
316 *
317 * @since 2.1
318 *
319 * @return void
320 */
321 function give_chosen_input( $field ) {
322 global $thepostid, $post;
323
324 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
325 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
326 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
327 $field['before_field'] = '';
328 $field['after_field'] = '';
329 $placeholder = isset( $field['placeholder'] ) ? 'data-placeholder="' . $field['placeholder'] . '"' : '';
330 $data_type = ! empty( $field['data_type'] ) ? $field['data_type'] : '';
331 $type = '';
332 $field['value'] = array_filter( (array) give_get_field_value( $field, $thepostid ) );
333 $choices = $field['options'];
334 $fieldName = esc_attr( give_get_field_name( $field ) );
335 $allow_new_values = ! empty( $field['allow-custom-values'] ) && (bool) $field['allow-custom-values'] ? 'data-allows-new-values="true"' : '';
336
337 // Set attributes based on multiselect datatype.
338 if ( 'multiselect' === $data_type ) {
339 $type = 'multiple';
340 $field['value'] = empty( $field['value'] ) ? [] : $field['value'];
341 $fieldName .= '[]';
342 }
343
344 if ( $allow_new_values && $field['value'] && ( $missing = array_diff( $field['value'], array_keys( $choices ) ) ) ) {
345 $choices = array_merge( $choices, array_combine( $missing, $missing ) );
346 }
347 ?>
348 <p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
349 <label for="<?php echo esc_attr( give_get_field_name( $field ) ); ?>">
350 <?php echo wp_kses_post( $field['name'] ); ?>
351 </label>
352 <?php echo esc_attr( $field['before_field'] ); ?>
353 <select
354 class="give-select-chosen give-chosen-settings"
355 style="<?php echo esc_attr( $field['style'] ); ?>"
356 name="<?php echo $fieldName; ?>"
357 id="<?php echo esc_attr( $field['id'] ); ?>"
358 <?php echo "{$type} {$allow_new_values} {$placeholder}"; ?>
359 >
360 <?php
361 foreach ( $choices as $key => $name ) {
362 echo sprintf(
363 '<option %1$s value="%2$s">%3$s</option>',
364 in_array( $key, $field['value'] ) ? 'selected="selected"' : '',
365 esc_attr( $key ),
366 $name
367 );
368 }
369 ?>
370 </select>
371 <?php echo esc_attr( $field['after_field'] ); ?>
372 <?php echo give_get_field_description( $field ); ?>
373 </p>
374 <?php
375 }
376
377 /**
378 * Output a label field
379 * Note: only for internal use.
380 *
381 * @param array $field {
382 * Optional. Array of text input field arguments.
383 *
384 * @type string $id Field ID. Default ''.
385 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
386 * @type string $value Value of input field. Default ''.
387 * @type string $title Text to display. HTML supported Default ''.
388 * @type string $before_field Text/HTML to add before input field. Default ''.
389 * @type string $after_field Text/HTML to add after input field. Default ''.
390 * }
391 *
392 * @since 2.7.0
393 *
394 * @return void
395 */
396 function give_label_field( $field ) {
397 global $thepostid, $post;
398
399 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
400 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
401 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
402 $field['before_field'] = '';
403 $field['after_field'] = '';
404 ?>
405 <p class=" <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
406 <?php echo esc_attr( $field['before_field'] ); ?>
407 <?php echo $field['title']; ?>
408 <?php echo esc_attr( $field['after_field'] ); ?>
409 <?php echo give_get_field_description( $field ); ?>
410 </p>
411 <?php
412 }
413
414 /**
415 * Give range slider field.
416 * Note: only for internal logic
417 *
418 * @since 2.1
419 *
420 * @param array $field {
421 * Optional. Array of text input field arguments.
422 *
423 * @type string $id Field ID. Default ''.
424 * @type string $style CSS style for input field. Default ''.
425 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
426 * @type string $value Value of input field. Default ''.
427 * @type string $name Name of input field. Default ''.
428 * @type string $type Type of input field. Default 'text'.
429 * @type string $before_field Text/HTML to add before input field. Default ''.
430 * @type string $after_field Text/HTML to add after input field. Default ''.
431 * @type string $data_type Define data type for value of input to filter it properly. Default ''.
432 * @type string $description Description of input field. Default ''.
433 * @type array $attributes List of attributes of input field. Default array().
434 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
435 * => '****' )
436 * }
437 *
438 * @return void
439 */
440 function give_donation_limit( $field ) {
441 global $thepostid, $post;
442
443 // Get Give donation form ID.
444 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
445
446 // Default arguments.
447 $default_options = [
448 'style' => '',
449 'wrapper_class' => '',
450 'value' => give_get_field_value( $field, $thepostid ),
451 'data_type' => 'decimal',
452 'before_field' => '',
453 'after_field' => '',
454 ];
455
456 // Field options.
457 $field['options'] = ! empty( $field['options'] ) ? $field['options'] : [];
458
459 // Default field option arguments.
460 $field['options'] = wp_parse_args(
461 $field['options'],
462 [
463 'display_label' => '',
464 'minimum' => give_format_decimal( '1.00', false, false ),
465 'maximum' => give_format_decimal( '999999.99', false, false ),
466 ]
467 );
468
469 // Set default field options.
470 $field_options = wp_parse_args( $field, $default_options );
471
472 // Get default minimum value, if empty.
473 $field_options['value']['minimum'] = ! empty( $field_options['value']['minimum'] )
474 ? $field_options['value']['minimum']
475 : $field_options['options']['minimum'];
476
477 // Get default maximum value, if empty.
478 $field_options['value']['maximum'] = ! empty( $field_options['value']['maximum'] )
479 ? $field_options['value']['maximum']
480 : $field_options['options']['maximum'];
481 ?>
482 <p class="give-field-wrap <?php echo esc_attr( $field_options['id'] ); ?>_field <?php echo esc_attr( $field_options['wrapper_class'] ); ?>">
483 <label for="<?php echo give_get_field_name( $field_options ); ?>"><?php echo wp_kses_post( $field_options['name'] ); ?></label>
484 <span class="give_donation_limit_display">
485 <?php
486 foreach ( $field_options['value'] as $amount_range => $amount_value ) {
487
488 switch ( $field_options['data_type'] ) {
489 case 'price':
490 $currency_position = give_get_option( 'currency_position', 'before' );
491 $price_field_labels = 'minimum' === $amount_range ? __( 'Minimum amount', 'give' ) : __( 'Maximum amount', 'give' );
492
493 $tooltip_html = [
494 'before' => Give()->tooltips->render_span(
495 [
496 'label' => $price_field_labels,
497 'tag_content' => sprintf( '<span class="give-money-symbol give-money-symbol-before">%s</span>', give_currency_symbol() ),
498 ]
499 ),
500 'after' => Give()->tooltips->render_span(
501 [
502 'label' => $price_field_labels,
503 'tag_content' => sprintf( '<span class="give-money-symbol give-money-symbol-after">%s</span>', give_currency_symbol() ),
504 ]
505 ),
506 ];
507
508 $before_html = ! empty( $field_options['before_field'] )
509 ? $field_options['before_field']
510 : ( 'before' === $currency_position ? $tooltip_html['before'] : '' );
511
512 $after_html = ! empty( $field_options['after_field'] )
513 ? $field_options['after_field']
514 : ( 'after' === $currency_position ? $tooltip_html['after'] : '' );
515
516 $field_options['attributes']['class'] .= ' give-text_small';
517 $field_options['value'][ $amount_range ] = $amount_value;
518 break;
519
520 case 'decimal':
521 $field_options['attributes']['class'] .= ' give_input_decimal give-text_small';
522 $field_options['value'][ $amount_range ] = $amount_value;
523 break;
524 }
525
526 echo '<span class=give-minmax-wrap>';
527 printf( '<label for="%1$s_give_donation_limit_%2$s">%3$s</label>', esc_attr( $field_options['id'] ), esc_attr( $amount_range ), esc_html( $price_field_labels ) );
528
529 echo isset( $before_html ) ? $before_html : '';
530 ?>
531 <input
532 name="<?php echo give_get_field_name( $field_options ); ?>[<?php echo esc_attr( $amount_range ); ?>]"
533 type="text"
534 id="<?php echo $field_options['id']; ?>_give_donation_limit_<?php echo $amount_range; ?>"
535 data-range_type="<?php echo esc_attr( $amount_range ); ?>"
536 value="<?php echo give_format_decimal( esc_attr( $field_options['value'][ $amount_range ] ) ); ?>"
537 placeholder="<?php echo give_format_decimal( $field_options['options'][ $amount_range ] ); ?>"
538 <?php echo give_get_attribute_str( $field_options ); ?>
539 />
540 <?php
541 echo isset( $after_html ) ? $after_html : '';
542 echo '</span>';
543 }
544 ?>
545 </span>
546 <?php echo give_get_field_description( $field_options ); ?>
547 </p>
548 <?php
549 }
550
551 /**
552 * Output a hidden input box.
553 *
554 * @since 1.8
555 *
556 * @param array $field {
557 * Optional. Array of hidden text input field arguments.
558 *
559 * @type string $id Field ID. Default ''.
560 * @type string $value Value of input field. Default ''.
561 * @type string $name Name of input field. Default ''.
562 * @type string $type Type of input field. Default 'text'.
563 * @type array $attributes List of attributes of input field. Default array().
564 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
565 * => '****' )
566 * }
567 * @return void
568 */
569 function give_hidden_input( $field ) {
570 global $thepostid, $post;
571
572 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
573 $field['value'] = give_get_field_value( $field, $thepostid );
574
575 // Custom attribute handling
576 $custom_attributes = [];
577
578 if ( ! empty( $field['attributes'] ) && is_array( $field['attributes'] ) ) {
579
580 foreach ( $field['attributes'] as $attribute => $value ) {
581 $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
582 }
583 }
584 ?>
585
586 <input
587 type="hidden"
588 name="<?php echo give_get_field_name( $field ); ?>"
589 id="<?php echo esc_attr( $field['id'] ); ?>"
590 value="<?php echo esc_attr( $field['value'] ); ?>"
591 <?php echo give_get_attribute_str( $field ); ?>
592 />
593 <?php
594 }
595
596 /**
597 * Output a textarea input box.
598 *
599 * @since 1.8
600 * @since 1.8
601 *
602 * @param array $field {
603 * Optional. Array of textarea input field arguments.
604 *
605 * @type string $id Field ID. Default ''.
606 * @type string $style CSS style for input field. Default ''.
607 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
608 * @type string $value Value of input field. Default ''.
609 * @type string $name Name of input field. Default ''.
610 * @type string $description Description of input field. Default ''.
611 * @type array $attributes List of attributes of input field. Default array().
612 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
613 * => '****' )
614 * }
615 * @return void
616 */
617 function give_textarea_input( $field ) {
618 global $thepostid, $post;
619
620 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
621 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
622 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
623 $field['value'] = give_get_field_value( $field, $thepostid );
624 $default_attributes = [
625 'cols' => 20,
626 'rows' => 10,
627 ];
628 ?>
629 <div class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
630 <label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
631 <textarea
632 style="<?php echo esc_attr( $field['style'] ); ?>"
633 name="<?php echo give_get_field_name( $field ); ?>"
634 id="<?php echo esc_attr( $field['id'] ); ?>"
635 <?php echo give_get_attribute_str( $field, $default_attributes ); ?>
636 ><?php echo esc_textarea( $field['value'] ); ?></textarea>
637 <?php
638 echo give_get_field_description( $field );
639 echo '</div>';
640 }
641
642 /**
643 * Output a wysiwyg.
644 *
645 * @since 1.8
646 *
647 * @param array $field {
648 * Optional. Array of WordPress editor field arguments.
649 *
650 * @type string $id Field ID. Default ''.
651 * @type string $style CSS style for input field. Default ''.
652 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
653 * @type string $value Value of input field. Default ''.
654 * @type string $name Name of input field. Default ''.
655 * @type string $description Description of input field. Default ''.
656 * @type array $attributes List of attributes of input field. Default array().
657 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
658 * => '****' )
659 * }
660 * @return void
661 */
662 function give_wysiwyg( $field ) {
663 global $thepostid, $post;
664
665 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
666 $field['value'] = give_get_field_value( $field, $thepostid );
667 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
668 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
669
670 // Since WP 3.9.0 WP does not allow square brackets in field id.
671 // If we pass square brackets in field id then code will work as expected but you will get PHP warnings.
672 // wp-includes/class-wp-editor.php::parse_settings::106
673 $field['unique_field_id'] = str_replace( [ '[', ']' ], [ '_', '' ], give_get_field_name( $field ) );
674
675 $editor_attributes = [
676 'textarea_name' => isset( $field['repeatable_field_id'] ) ? $field['repeatable_field_id'] : $field['id'],
677 'textarea_rows' => '10',
678 'editor_css' => esc_attr( $field['style'] ),
679 'editor_class' => $field['attributes']['class'],
680 ];
681 $data_wp_editor = ' data-wp-editor="' . base64_encode(
682 json_encode(
683 [
684 $field['value'],
685 $field['unique_field_id'],
686 $editor_attributes,
687 ]
688 )
689 ) . '"';
690 $data_wp_editor = isset( $field['repeatable_field_id'] ) ? $data_wp_editor : '';
691
692 echo '<div class="give-field-wrap ' . $field['unique_field_id'] . '_field ' . esc_attr( $field['wrapper_class'] ) . '"' . $data_wp_editor . '><label for="' . $field['unique_field_id'] . '">' . wp_kses_post( $field['name'] ) . '</label>';
693
694 wp_editor(
695 $field['value'],
696 $field['unique_field_id'],
697 $editor_attributes
698 );
699
700 echo give_get_field_description( $field );
701 echo '</div>';
702 }
703
704 /**
705 * Output a checkbox input box.
706 *
707 * @since 1.8
708 *
709 * @param array $field {
710 * Optional. Array of checkbox field arguments.
711 *
712 * @type string $id Field ID. Default ''.
713 * @type string $style CSS style for input field. Default ''.
714 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
715 * @type string $value Value of input field. Default ''.
716 * @type string $cbvalue Checkbox value. Default 'on'.
717 * @type string $name Name of input field. Default ''.
718 * @type string $description Description of input field. Default ''.
719 * @type array $attributes List of attributes of input field. Default array().
720 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
721 * => '****' )
722 * }
723 * @return void
724 */
725 function give_checkbox( $field ) {
726 global $thepostid, $post;
727
728 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
729 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
730 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
731 $field['value'] = give_get_field_value( $field, $thepostid );
732 $field['cbvalue'] = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'on';
733 $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
734 ?>
735 <p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
736 <label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
737 <input
738 type="checkbox"
739 style="<?php echo esc_attr( $field['style'] ); ?>"
740 name="<?php echo give_get_field_name( $field ); ?>"
741 id="<?php echo esc_attr( $field['id'] ); ?>"
742 value="<?php echo esc_attr( $field['cbvalue'] ); ?>"
743 <?php echo checked( $field['value'], $field['cbvalue'], false ); ?>
744 <?php echo give_get_attribute_str( $field ); ?>
745 />
746 <?php
747 echo give_get_field_description( $field );
748 echo '</p>';
749 }
750
751 /**
752 * Output a select input box.
753 *
754 * @since 1.8
755 *
756 * @param array $field {
757 * Optional. Array of select field arguments.
758 *
759 * @type string $id Field ID. Default ''.
760 * @type string $style CSS style for input field. Default ''.
761 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
762 * @type string $value Value of input field. Default ''.
763 * @type string $name Name of input field. Default ''.
764 * @type string $description Description of input field. Default ''.
765 * @type array $attributes List of attributes of input field. Default array().
766 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
767 * => '****' )
768 * @type array $options List of options. Default array().
769 * for example: 'options' => array( '' => 'None', 'yes' => 'Yes' )
770 * }
771 * @return void
772 */
773 function give_select( $field ) {
774 global $thepostid, $post;
775
776 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
777 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
778 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
779 $field['value'] = give_get_field_value( $field, $thepostid );
780 $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
781 ?>
782 <p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
783 <label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
784 <select
785 id="<?php echo esc_attr( $field['id'] ); ?>"
786 name="<?php echo give_get_field_name( $field ); ?>"
787 style="<?php echo esc_attr( $field['style'] ); ?>"
788 <?php echo give_get_attribute_str( $field ); ?>
789 >
790 <?php
791 foreach ( $field['options'] as $key => $value ) {
792 echo '<option value="' . esc_attr( $key ) . '" ' . selected( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
793 }
794 echo '</select>';
795 echo give_get_field_description( $field );
796 echo '</p>';
797 }
798
799 /**
800 * Output a radio input box.
801 *
802 * @since 1.8
803 *
804 * @param array $field {
805 * Optional. Array of radio field arguments.
806 *
807 * @type string $id Field ID. Default ''.
808 * @type string $style CSS style for input field. Default ''.
809 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
810 * @type string $value Value of input field. Default ''.
811 * @type string $name Name of input field. Default ''.
812 * @type string $description Description of input field. Default ''.
813 * @type array $attributes List of attributes of input field. Default array().
814 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
815 * => '****' )
816 * @type array $options List of options. Default array().
817 * for example: 'options' => array( 'enable' => 'Enable', 'disable' =>
818 * 'Disable' )
819 * }
820 * @return void
821 */
822 function give_radio( $field ) {
823 global $thepostid, $post;
824
825 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
826 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
827 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
828 $field['value'] = give_get_field_value( $field, $thepostid );
829 $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
830
831 echo '<fieldset class="give-field-wrap ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><span class="give-field-label">' . wp_kses_post( $field['name'] ) . '</span><legend class="screen-reader-text">' . wp_kses_post( $field['name'] ) . '</legend><ul class="give-radios">';
832
833 foreach ( $field['options'] as $key => $value ) {
834
835 echo '<li><label><input
836 name="' . give_get_field_name( $field ) . '"
837 value="' . esc_attr( $key ) . '"
838 type="radio"
839 style="' . esc_attr( $field['style'] ) . '"
840 ' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . ' '
841 . give_get_attribute_str( $field ) . '
842 /> ' . esc_html( $value ) . '</label>
843 </li>';
844 }
845 echo '</ul>';
846
847 echo give_get_field_description( $field );
848 echo '</fieldset>';
849 }
850
851 /**
852 * Output a colorpicker.
853 *
854 * @since 1.8
855 *
856 * @param array $field {
857 * Optional. Array of colorpicker field arguments.
858 *
859 * @type string $id Field ID. Default ''.
860 * @type string $style CSS style for input field. Default ''.
861 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
862 * @type string $value Value of input field. Default ''.
863 * @type string $name Name of input field. Default ''.
864 * @type string $description Description of input field. Default ''.
865 * @type array $attributes List of attributes of input field. Default array().
866 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
867 * => '****' )
868 * }
869 * @return void
870 */
871 function give_colorpicker( $field ) {
872 global $thepostid, $post;
873
874 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
875 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
876 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
877 $field['value'] = give_get_field_value( $field, $thepostid );
878 $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
879 $field['type'] = 'text';
880 ?>
881 <p class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
882 <label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
883 <input
884 type="<?php echo esc_attr( $field['type'] ); ?>"
885 style="<?php echo esc_attr( $field['style'] ); ?>"
886 name="<?php echo give_get_field_name( $field ); ?>"
887 id="<?php echo esc_attr( $field['id'] ); ?>"
888 value="<?php echo esc_attr( $field['value'] ); ?>"
889 <?php echo give_get_attribute_str( $field ); ?>
890 />
891 <?php
892 echo give_get_field_description( $field );
893 echo '</p>';
894 }
895
896 /**
897 * Output a file upload field.
898 *
899 * @since 1.8.9
900 *
901 * @param array $field
902 */
903 function give_file( $field ) {
904 give_media( $field );
905 }
906
907
908 /**
909 * Output a media upload field.
910 *
911 * @since 1.8
912 *
913 * @param array $field
914 */
915 function give_media( $field ) {
916 global $thepostid, $post;
917
918 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
919 $button_label = sprintf( __( 'Add or Upload %s', 'give' ), ( 'file' === $field['type'] ? __( 'File', 'give' ) : __( 'Image', 'give' ) ) );
920
921 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
922 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
923 $field['value'] = give_get_field_value( $field, $thepostid );
924 $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
925 $field['attributes']['class'] = "{$field['attributes']['class']} give-text-medium";
926
927 // Allow developer to save attachment ID or attachment url as metadata.
928 $field['fvalue'] = isset( $field['fvalue'] ) ? $field['fvalue'] : 'url';
929
930 $allow_media_preview_tags = [ 'jpg', 'jpeg', 'png', 'gif', 'ico' ];
931 $preview_image_src = $field['value'] ? ( 'id' === $field['fvalue'] ? wp_get_attachment_url( $field['value'] ) : $field['value'] ) : '#';
932 $preview_image_extension = $preview_image_src ? pathinfo( $preview_image_src, PATHINFO_EXTENSION ) : '';
933 $is_show_preview = in_array( $preview_image_extension, $allow_media_preview_tags );
934 ?>
935 <fieldset class="give-field-wrap <?php echo esc_attr( $field['id'] ); ?>_field <?php echo esc_attr( $field['wrapper_class'] ); ?>">
936 <label for="<?php echo give_get_field_name( $field ); ?>"><?php echo wp_kses_post( $field['name'] ); ?></label>
937 <input
938 name="<?php echo give_get_field_name( $field ); ?>"
939 id="<?php echo esc_attr( $field['id'] ); ?>"
940 type="text"
941 value="<?php echo $field['value']; ?>"
942 style="<?php echo esc_attr( $field['style'] ); ?>"
943 <?php echo give_get_attribute_str( $field ); ?>
944 />&nbsp;&nbsp;&nbsp;&nbsp;<input class="give-upload-button button" type="button" value="<?php echo $button_label; ?>" data-fvalue="<?php echo $field['fvalue']; ?>" data-field-type="<?php echo $field['type']; ?>">
945 <?php echo give_get_field_description( $field ); ?>
946 <div class="give-image-thumb<?php echo ! $field['value'] || ! $is_show_preview ? ' give-hidden' : ''; ?>">
947 <span class="give-delete-image-thumb dashicons dashicons-no-alt"></span>
948 <img src="<?php echo $preview_image_src; ?>" alt="">
949 </div>
950 </fieldset>
951 <?php
952 }
953
954 /**
955 * Output a select field with payment options list.
956 *
957 * @since 1.8
958 *
959 * @param array $field
960 *
961 * @return void
962 */
963 function give_default_gateway( $field ) {
964 global $thepostid, $post;
965
966 // get all active payment gateways.
967 $gateways = give_get_enabled_payment_gateways( $thepostid );
968 $field['options'] = [];
969
970 // Set field option value.
971 if ( ! empty( $gateways ) ) {
972 foreach ( $gateways as $key => $option ) {
973 $field['options'][ $key ] = $option['admin_label'];
974 }
975 }
976
977 // Add a field to the Give Form admin single post view of this field
978 if ( is_object( $post ) && 'give_forms' === $post->post_type ) {
979 $field['options'] = array_merge( [ 'global' => esc_html__( 'Global Default', 'give' ) ], $field['options'] );
980 }
981
982 // Render select field.
983 give_select( $field );
984 }
985
986 /**
987 * Output the documentation link.
988 *
989 * @since 1.8
990 *
991 * @param array $field {
992 * Optional. Array of customizable link attributes.
993 *
994 * @type string $name Name of input field. Default ''.
995 * @type string $type Type of input field. Default 'text'.
996 * @type string $url Value to be passed as a link. Default 'https://givewp.com/documentation'.
997 * @type string $title Value to be passed as text of link. Default 'Documentation'.
998 * @type array $attributes List of attributes of input field. Default array().
999 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
1000 * => '****' )
1001 * }
1002 * @return void
1003 */
1004
1005 function give_docs_link( $field ) {
1006 $field['url'] = isset( $field['url'] ) ? $field['url'] : 'https://givewp.com/documentation';
1007 $field['title'] = isset( $field['title'] ) ? $field['title'] : 'Documentation';
1008
1009 echo '<p class="give-docs-link"><a href="' . esc_url( $field['url'] )
1010 . '" target="_blank">'
1011 . sprintf( esc_html__( 'Need Help? See docs on "%s"', 'give' ), $field['title'] )
1012 . '<span class="dashicons dashicons-editor-help"></span></a></p>';
1013 }
1014
1015
1016 /**
1017 * Output preview buttons.
1018 *
1019 * @since 2.0
1020 *
1021 * @param $field
1022 */
1023 function give_email_preview_buttons( $field ) {
1024 /* @var WP_Post $post */
1025 global $post;
1026
1027 $field_id = str_replace( [ '_give_', '_preview_buttons' ], '', $field['id'] );
1028
1029 ob_start();
1030
1031 echo '<p class="give-field-wrap ' . esc_attr( $field['id'] ) . '_field"><label for="' . give_get_field_name( $field ) . '">' . wp_kses_post( $field['name'] ) . '</label>';
1032
1033 echo sprintf(
1034 '<a href="%1$s" class="button-secondary" target="_blank">%2$s</a>',
1035 wp_nonce_url(
1036 add_query_arg(
1037 [
1038 'give_action' => 'preview_email',
1039 'email_type' => $field_id,
1040 'form_id' => $post->ID,
1041 ],
1042 home_url()
1043 ),
1044 'give-preview-email'
1045 ),
1046 $field['name']
1047 );
1048
1049 echo sprintf(
1050 ' <a href="%1$s" aria-label="%2$s" class="button-secondary">%3$s</a>',
1051 wp_nonce_url(
1052 add_query_arg(
1053 [
1054 'give_action' => 'send_preview_email',
1055 'email_type' => $field_id,
1056 'give-messages[]' => 'sent-test-email',
1057 'form_id' => $post->ID,
1058 ]
1059 ),
1060 'give-send-preview-email'
1061 ),
1062 esc_attr__( 'Send Test Email.', 'give' ),
1063 esc_html__( 'Send Test Email', 'give' )
1064 );
1065
1066 if ( ! empty( $field['description'] ) ) {
1067 echo '<span class="give-field-description">' . wp_kses_post( $field['desc'] ) . '</span>';
1068 }
1069
1070 echo '</p>';
1071
1072 echo ob_get_clean();
1073 }
1074
1075 /**
1076 * Get setting field value.
1077 *
1078 * Note: Use only for single post, page or custom post type.
1079 *
1080 * @since 1.8
1081 * @since 2.1 Added support for donation_limit.
1082 *
1083 * @param array $field
1084 * @param int $postid
1085 *
1086 * @return mixed
1087 */
1088 function give_get_field_value( $field, $postid ) {
1089 if ( isset( $field['attributes']['value'] ) ) {
1090 return $field['attributes']['value'];
1091 }
1092
1093 // If field is range slider.
1094 if ( 'donation_limit' === $field['type'] ) {
1095
1096 // Get minimum value.
1097 $minimum = give_get_meta( $postid, $field['id'] . '_minimum', true );
1098
1099 // Give < 2.1
1100 if ( '_give_custom_amount_range' === $field['id'] && empty( $minimum ) ) {
1101 $minimum = give_get_meta( $postid, '_give_custom_amount_minimum', true );
1102 }
1103
1104 $field_value = [
1105 'minimum' => $minimum,
1106 'maximum' => give_get_meta( $postid, $field['id'] . '_maximum', true ),
1107 ];
1108 } else {
1109 // Get value from db.
1110 $field_value = give_get_meta( $postid, $field['id'], true );
1111 }
1112
1113 /**
1114 * Filter the field value before apply default value.
1115 *
1116 * @since 1.8
1117 *
1118 * @param mixed $field_value Field value.
1119 */
1120 $field_value = apply_filters( "{$field['id']}_field_value", $field_value, $field, $postid );
1121
1122 // Set default value if no any data saved to db.
1123 if ( ! $field_value && isset( $field['default'] ) ) {
1124 $field_value = $field['default'];
1125 }
1126
1127 return $field_value;
1128 }
1129
1130
1131 /**
1132 * Get field description html.
1133 *
1134 * @since 1.8
1135 *
1136 * @param $field
1137 *
1138 * @return string
1139 */
1140 function give_get_field_description( $field ) {
1141 $field_desc_html = '';
1142 $description = '';
1143
1144 // Check for both `description` and `desc`.
1145 if ( isset( $field['description'] ) ) {
1146 $description = $field['description'];
1147 } elseif ( isset( $field['desc'] ) ) {
1148 $description = $field['desc'];
1149 }
1150
1151 // Set if there is a description.
1152 if ( ! empty( $description ) ) {
1153 $field_desc_html = '<span class="give-field-description">' . wp_kses_post( $description ) . '</span>';
1154 }
1155
1156 return $field_desc_html;
1157 }
1158
1159
1160 /**
1161 * Get repeater field value.
1162 *
1163 * Note: Use only for single post, page or custom post type.
1164 *
1165 * @since 1.8
1166 *
1167 * @param array $field
1168 * @param array $field_group
1169 * @param array $fields
1170 *
1171 * @return string
1172 */
1173 function give_get_repeater_field_value( $field, $field_group, $fields ) {
1174 $field_value = ( isset( $field_group[ $field['id'] ] ) ? $field_group[ $field['id'] ] : '' );
1175
1176 /**
1177 * Filter the specific repeater field value
1178 *
1179 * @since 1.8
1180 *
1181 * @param string $field_id
1182 */
1183 $field_value = apply_filters( "give_get_repeater_field_{$field['id']}_value", $field_value, $field, $field_group, $fields );
1184
1185 /**
1186 * Filter the repeater field value
1187 *
1188 * @since 1.8
1189 *
1190 * @param string $field_id
1191 */
1192 $field_value = apply_filters( 'give_get_repeater_field_value', $field_value, $field, $field_group, $fields );
1193
1194 return $field_value;
1195 }
1196
1197 /**
1198 * Get repeater field id.
1199 *
1200 * Note: Use only for single post, page or custom post type.
1201 *
1202 * @since 1.8
1203 *
1204 * @param array $field
1205 * @param array $fields
1206 * @param int|bool $default
1207 *
1208 * @return string
1209 */
1210 function give_get_repeater_field_id( $field, $fields, $default = false ) {
1211 $row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1212
1213 // Get field id.
1214 $field_id = "{$fields['id']}[{$row_placeholder}][{$field['id']}]";
1215
1216 /**
1217 * Filter the specific repeater field id
1218 *
1219 * @since 1.8
1220 *
1221 * @param string $field_id
1222 */
1223 $field_id = apply_filters( "give_get_repeater_field_{$field['id']}_id", $field_id, $field, $fields, $default );
1224
1225 /**
1226 * Filter the repeater field id
1227 *
1228 * @since 1.8
1229 *
1230 * @param string $field_id
1231 */
1232 $field_id = apply_filters( 'give_get_repeater_field_id', $field_id, $field, $fields, $default );
1233
1234 return $field_id;
1235 }
1236
1237
1238 /**
1239 * Get field name.
1240 *
1241 * @since 1.8
1242 *
1243 * @param array $field
1244 *
1245 * @return string
1246 */
1247 function give_get_field_name( $field ) {
1248 $field_name = esc_attr( empty( $field['repeat'] ) ? $field['id'] : $field['repeatable_field_id'] );
1249
1250 /**
1251 * Filter the field name.
1252 *
1253 * @since 1.8
1254 *
1255 * @param string $field_name
1256 */
1257 $field_name = apply_filters( 'give_get_field_name', $field_name, $field );
1258
1259 return $field_name;
1260 }
1261
1262 /**
1263 * Output repeater field or multi donation type form on donation from edit screen.
1264 * Note: internal use only.
1265 *
1266 * @TODO : Add support for wysiwyg type field.
1267 *
1268 * @since 1.8
1269 *
1270 * @param array $fields
1271 *
1272 * @return void
1273 */
1274 function _give_metabox_form_data_repeater_fields( $fields ) {
1275 global $thepostid, $post;
1276
1277 // Bailout.
1278 if ( ! isset( $fields['fields'] ) || empty( $fields['fields'] ) ) {
1279 return;
1280 }
1281
1282 $group_numbering = isset( $fields['options']['group_numbering'] ) ? (int) $fields['options']['group_numbering'] : 0;
1283 $close_tabs = isset( $fields['options']['close_tabs'] ) ? (int) $fields['options']['close_tabs'] : 0;
1284 $wrapper_class = isset( $fields['wrapper_class'] ) ? $fields['wrapper_class'] : '';
1285 ?>
1286 <div class="give-repeatable-field-section <?php echo esc_attr( $wrapper_class ); ?>" id="<?php echo "{$fields['id']}_field"; ?>"
1287 data-group-numbering="<?php echo $group_numbering; ?>" data-close-tabs="<?php echo $close_tabs; ?>">
1288 <?php if ( ! empty( $fields['name'] ) ) : ?>
1289 <p class="give-repeater-field-name"><?php echo $fields['name']; ?></p>
1290 <?php endif; ?>
1291
1292 <?php if ( ! empty( $fields['description'] ) ) : ?>
1293 <p class="give-repeater-field-description"><?php echo $fields['description']; ?></p>
1294 <?php endif; ?>
1295
1296 <table class="give-repeatable-fields-section-wrapper" cellspacing="0">
1297 <?php
1298 // Get value.
1299 $repeater_field_values = ! empty( $fields['attributes']['value'] )
1300 ? $fields['attributes']['value']
1301 : give_get_meta( $thepostid, $fields['id'], true );
1302
1303 // Setup default value.
1304 if ( empty( $repeater_field_values ) && ! empty( $fields['default'] ) ) {
1305 $repeater_field_values = $fields['default'];
1306 }
1307
1308 $header_title = isset( $fields['options']['header_title'] )
1309 ? $fields['options']['header_title']
1310 : esc_attr__( 'Group', 'give' );
1311
1312 $add_default_donation_field = false;
1313
1314 // Check if level is not created or we have to add default level.
1315 if ( is_array( $repeater_field_values ) && ( $fields_count = count( $repeater_field_values ) ) ) {
1316 $repeater_field_values = array_values( $repeater_field_values );
1317 } else {
1318 $fields_count = 1;
1319 $add_default_donation_field = true;
1320 }
1321 ?>
1322 <tbody class="container"<?php echo " data-rf-row-count=\"{$fields_count}\""; ?>>
1323 <!--Repeater field group template-->
1324 <tr class="give-template give-row">
1325 <td class="give-repeater-field-wrap give-column" colspan="2">
1326 <div class="give-row-head give-move">
1327 <button type="button" class="handlediv button-link"><span class="toggle-indicator"></span>
1328 </button>
1329 <span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-</span>
1330 <h2>
1331 <span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
1332 </h2>
1333 </div>
1334 <div class="give-row-body">
1335 <?php foreach ( $fields['fields'] as $field ) : ?>
1336 <?php
1337 if ( ! give_is_field_callback_exist( $field ) ) {
1338 continue;
1339 }
1340 ?>
1341 <?php
1342 $field['repeat'] = true;
1343 $field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields );
1344 $field['id'] = str_replace(
1345 [ '[', ']' ],
1346 [ '_', '' ],
1347 $field['repeatable_field_id']
1348 );
1349 ?>
1350 <?php give_render_field( $field ); ?>
1351 <?php endforeach; ?>
1352 </div>
1353 </td>
1354 </tr>
1355
1356 <?php if ( ! empty( $repeater_field_values ) ) : ?>
1357 <!--Stored repeater field group-->
1358 <?php foreach ( $repeater_field_values as $index => $field_group ) : ?>
1359 <tr class="give-row">
1360 <td class="give-repeater-field-wrap give-column" colspan="2">
1361 <div class="give-row-head give-move">
1362 <button type="button" class="handlediv button-link">
1363 <span class="toggle-indicator"></span></button>
1364 <span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
1365 </span>
1366 <h2>
1367 <span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
1368 </h2>
1369 </div>
1370 <div class="give-row-body">
1371 <?php foreach ( $fields['fields'] as $field ) : ?>
1372 <?php
1373 if ( ! give_is_field_callback_exist( $field ) ) {
1374 continue;
1375 }
1376 ?>
1377 <?php
1378 $field['repeat'] = true;
1379 $field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields, $index );
1380 $field['attributes']['value'] = give_get_repeater_field_value( $field, $field_group, $fields );
1381 $field['id'] = str_replace(
1382 [ '[', ']' ],
1383 [ '_', '' ],
1384 $field['repeatable_field_id']
1385 );
1386 ?>
1387 <?php give_render_field( $field ); ?>
1388 <?php endforeach; ?>
1389 </div>
1390 </td>
1391 </tr>
1392 <?php
1393 endforeach;
1394 ?>
1395
1396 <?php elseif ( $add_default_donation_field ) : ?>
1397 <!--Default repeater field group-->
1398 <tr class="give-row">
1399 <td class="give-repeater-field-wrap give-column" colspan="2">
1400 <div class="give-row-head give-move">
1401 <button type="button" class="handlediv button-link">
1402 <span class="toggle-indicator"></span></button>
1403 <span class="give-remove" title="<?php esc_html_e( 'Remove Group', 'give' ); ?>">-
1404 </span>
1405 <h2>
1406 <span data-header-title="<?php echo $header_title; ?>"><?php echo $header_title; ?></span>
1407 </h2>
1408 </div>
1409 <div class="give-row-body">
1410 <?php
1411 foreach ( $fields['fields'] as $field ) :
1412 if ( ! give_is_field_callback_exist( $field ) ) {
1413 continue;
1414 }
1415
1416 $field['repeat'] = true;
1417 $field['repeatable_field_id'] = give_get_repeater_field_id( $field, $fields, 0 );
1418 $field['attributes']['value'] = apply_filters(
1419 "give_default_field_group_field_{$field['id']}_value",
1420 ( ! empty( $field['default'] ) ? $field['default'] : '' ),
1421 $field,
1422 $fields
1423 );
1424 $field['id'] = str_replace(
1425 [ '[', ']' ],
1426 [ '_', '' ],
1427 $field['repeatable_field_id']
1428 );
1429 give_render_field( $field );
1430
1431 endforeach;
1432 ?>
1433 </div>
1434 </td>
1435 </tr>
1436 <?php endif; ?>
1437 </tbody>
1438 <tfoot>
1439 <tr>
1440 <?php
1441 $add_row_btn_title = isset( $fields['options']['add_button'] )
1442 ? $add_row_btn_title = $fields['options']['add_button']
1443 : esc_html__( 'Add Row', 'give' );
1444 ?>
1445 <td colspan="2" class="give-add-repeater-field-section-row-wrap">
1446 <span class="button button-primary give-add-repeater-field-section-row"><?php echo $add_row_btn_title; ?></span>
1447 </td>
1448 </tr>
1449 </tfoot>
1450 </table>
1451 </div>
1452 <?php
1453 }
1454
1455
1456 /**
1457 * Set repeater field id for multi donation form.
1458 *
1459 * @since 1.8
1460 *
1461 * @param int $field_id
1462 * @param array $field
1463 * @param array $fields
1464 * @param bool $default
1465 *
1466 * @return mixed
1467 */
1468 function _give_set_multi_level_repeater_field_id( $field_id, $field, $fields, $default ) {
1469 $row_placeholder = false !== $default ? $default : '{{row-count-placeholder}}';
1470 $field_id = "{$fields['id']}[{$row_placeholder}][{$field['id']}][level_id]";
1471
1472 return $field_id;
1473 }
1474
1475 add_filter( 'give_get_repeater_field__give_id_id', '_give_set_multi_level_repeater_field_id', 10, 4 );
1476
1477 /**
1478 * Set repeater field value for multi donation form.
1479 *
1480 * @since 1.8
1481 *
1482 * @param string $field_value
1483 * @param array $field
1484 * @param array $field_group
1485 * @param array $fields
1486 *
1487 * @return mixed
1488 */
1489 function _give_set_multi_level_repeater_field_value( $field_value, $field, $field_group, $fields ) {
1490 $field_value = $field_group[ $field['id'] ]['level_id'];
1491
1492 return $field_value;
1493 }
1494
1495 add_filter( 'give_get_repeater_field__give_id_value', '_give_set_multi_level_repeater_field_value', 10, 4 );
1496
1497 /**
1498 * Set default value for _give_id field.
1499 *
1500 * @since 1.8
1501 *
1502 * @param $field
1503 *
1504 * @return string
1505 */
1506 function _give_set_field_give_id_default_value( $field ) {
1507 return 0;
1508 }
1509
1510 add_filter( 'give_default_field_group_field__give_id_value', '_give_set_field_give_id_default_value' );
1511
1512 /**
1513 * Set default value for _give_default field.
1514 *
1515 * @since 1.8
1516 *
1517 * @param $field
1518 *
1519 * @return string
1520 */
1521 function _give_set_field_give_default_default_value( $field ) {
1522 return 'default';
1523 }
1524
1525 add_filter( 'give_default_field_group_field__give_default_value', '_give_set_field_give_default_default_value' );
1526
1527 /**
1528 * Set repeater field editor id for field type wysiwyg.
1529 *
1530 * @since 1.8
1531 *
1532 * @param $field_name
1533 * @param $field
1534 *
1535 * @return string
1536 */
1537 function give_repeater_field_set_editor_id( $field_name, $field ) {
1538 if ( isset( $field['repeatable_field_id'] ) && 'wysiwyg' == $field['type'] ) {
1539 $field_name = '_give_repeater_' . uniqid() . '_wysiwyg';
1540 }
1541
1542 return $field_name;
1543 }
1544
1545 add_filter( 'give_get_field_name', 'give_repeater_field_set_editor_id', 10, 2 );
1546
1547 /**
1548 * Output Donation form radio input box.
1549 *
1550 * @since 2.1.3
1551 *
1552 * @param array $field {
1553 * Optional. Array of radio field arguments.
1554 *
1555 * @type string $id Field ID. Default ''.
1556 * @type string $style CSS style for input field. Default ''.
1557 * @type string $wrapper_class CSS class to use for wrapper of input field. Default ''.
1558 * @type string $value Value of input field. Default ''.
1559 * @type string $name Name of input field. Default ''.
1560 * @type string $description Description of input field. Default ''.
1561 * @type array $attributes List of attributes of input field. Default array().
1562 * for example: 'attributes' => array( 'placeholder' => '*****', 'class'
1563 * => '****' )
1564 * @type array $options List of options. Default array().
1565 * for example: 'options' => array( 'enable' => 'Enable', 'disable' =>
1566 * 'Disable' )
1567 * }
1568 * @return void
1569 */
1570 function give_donation_form_goal( $field ) {
1571 global $thepostid, $post;
1572
1573 $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
1574 $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
1575 $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
1576 $field['value'] = give_get_field_value( $field, $thepostid );
1577 $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
1578
1579 printf(
1580 '<fieldset class="give-field-wrap %s_field %s">',
1581 esc_attr( $field['id'] ),
1582 esc_attr( $field['wrapper_class'] )
1583 );
1584
1585 printf(
1586 '<span class="give-field-label">%s</span>',
1587 esc_html( $field['name'] )
1588 );
1589
1590 printf(
1591 '<legend class="screen-reader-text">%s</legend>',
1592 esc_html( $field['name'] )
1593 );
1594 ?>
1595
1596 <ul class="give-radios">
1597 <?php
1598 foreach ( $field['options'] as $key => $value ) {
1599 $attributes = empty( $field['attributes'] ) ? '' : give_get_attribute_str( $field['attributes'] );
1600 printf(
1601 '<li><label><input name="%s" value="%s" type="radio" style="%s" %s %s /> %s </label></li>',
1602 give_get_field_name( $field ),
1603 esc_attr( $key ),
1604 esc_attr( $field['style'] ),
1605 checked( esc_attr( $field['value'] ), esc_attr( $key ), false ),
1606 $attributes,
1607 esc_html( $value )
1608 );
1609 }
1610 ?>
1611 </ul>
1612
1613 <?php
1614 /**
1615 * Action to add HTML after donation form radio button is display and before description.
1616 *
1617 * @since 2.1.3
1618 *
1619 * @param array $field Array of radio field arguments.
1620 */
1621 do_action( 'give_donation_form_goal_before_description', $field );
1622
1623 echo give_get_field_description( $field );
1624
1625 echo '</fieldset>';
1626 }
1627