PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.7.5
GiveWP – Donation Plugin and Fundraising Platform v2.7.5
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 / class-admin-settings.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 5 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
class-admin-settings.php
1237 lines
1 <?php
2 /**
3 * Give Admin Settings Class
4 *
5 * @package Give
6 * @subpackage Classes/Give_Admin_Settings
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
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 if ( ! class_exists( 'Give_Admin_Settings' ) ) :
17
18 /**
19 * Give_Admin_Settings Class.
20 *
21 * @since 1.8
22 */
23 class Give_Admin_Settings {
24
25 /**
26 * Setting pages.
27 *
28 * @since 1.8
29 * @var array List of settings.
30 */
31 private static $settings = [];
32
33 /**
34 * Setting filter and action prefix.
35 *
36 * @since 1.8
37 * @var string setting filter and action name prefix.
38 */
39 private static $setting_filter_prefix = '';
40
41 /**
42 * Error messages.
43 *
44 * @since 1.8
45 * @var array List of errors.
46 */
47 private static $errors = [];
48
49 /**
50 * Update messages.
51 *
52 * @since 1.8
53 * @var array List of messages.
54 */
55 private static $messages = [];
56
57 /**
58 * Include the settings page classes.
59 *
60 * @since 1.8
61 * @return array
62 */
63 public static function get_settings_pages() {
64 /**
65 * Filter the setting page.
66 *
67 * Note: filter dynamically fire on basis of setting page slug.
68 * For example: if you register a setting page with give-settings menu slug
69 * then filter will be give-settings_get_settings_pages
70 *
71 * @since 1.8
72 *
73 * @param array $settings Array of settings class object.
74 */
75 self::$settings = apply_filters( self::$setting_filter_prefix . '_get_settings_pages', [] );
76
77 return self::$settings;
78 }
79
80 /**
81 * Verify admin setting nonce
82 *
83 * @since 1.8.14
84 * @access public
85 *
86 * @return bool
87 */
88 public static function verify_nonce() {
89 if ( empty( $_REQUEST['_give-save-settings'] ) || ! wp_verify_nonce( $_REQUEST['_give-save-settings'], 'give-save-settings' ) ) {
90 return false;
91 }
92
93 return true;
94 }
95
96 /**
97 * Save the settings.
98 *
99 * @since 1.8
100 * @return void
101 */
102 public static function save() {
103 $current_tab = give_get_current_setting_tab();
104
105 if ( ! self::verify_nonce() ) {
106 echo '<div class="notice error"><p>' . esc_attr__( 'Action failed. Please refresh the page and retry.', 'give' ) . '</p></div>';
107 die();
108 }
109
110 /**
111 * Trigger Action.
112 *
113 * Note: action dynamically fire on basis of setting page slug and current tab.
114 * For example: if you register a setting page with give-settings menu slug and general current tab name
115 * then action will be give-settings_save_general
116 *
117 * @since 1.8
118 */
119 do_action( self::$setting_filter_prefix . '_save_' . $current_tab );
120
121 self::add_message( 'give-setting-updated', __( 'Your settings have been saved.', 'give' ) );
122
123 /**
124 * Trigger Action.
125 *
126 * Note: action dynamically fire on basis of setting page slug.
127 * For example: if you register a setting page with give-settings menu slug
128 * then action will be give-settings_saved
129 *
130 * @since 1.8
131 */
132 do_action( self::$setting_filter_prefix . '_saved' );
133 }
134
135 /**
136 * Add a message.
137 *
138 * @since 1.8
139 *
140 * @param string $code Message code (Note: This should be unique).
141 * @param string $message Message text.
142 *
143 * @return void
144 */
145 public static function add_message( $code, $message ) {
146 self::$messages[ $code ] = $message;
147 }
148
149 /**
150 * Add an error.
151 *
152 * @since 1.8
153 *
154 * @param string $code Message code (Note: This should be unique).
155 * @param string $message Message text.
156 *
157 * @return void
158 */
159 public static function add_error( $code, $message ) {
160 self::$errors[ $code ] = $message;
161 }
162
163 /**
164 * Output messages + errors.
165 *
166 * @since 1.8
167 * @return void
168 */
169 public static function show_messages() {
170 $notice_html = '';
171 $classes = 'give-notice settings-error notice is-dismissible';
172
173 self::$errors = apply_filters( self::$setting_filter_prefix . '_error_notices', self::$errors );
174 self::$messages = apply_filters( self::$setting_filter_prefix . '_update_notices', self::$messages );
175
176 if ( 0 < count( self::$errors ) ) {
177 foreach ( self::$errors as $code => $message ) {
178 $notice_html .= sprintf(
179 '<div id="setting-error-%1$s" class="%2$s error" style="display: none"><p>%3$s</p></div>',
180 $code,
181 $classes,
182 $message
183 );
184 }
185 }
186
187 if ( 0 < count( self::$messages ) ) {
188 foreach ( self::$messages as $code => $message ) {
189 $notice_html .= sprintf(
190 '<div id="setting-error-%1$s" class="%2$s updated" style="display: none"><p><strong>%3$s</strong></p></div>',
191 $code,
192 $classes,
193 $message
194 );
195 }
196 }
197
198 echo $notice_html;
199 }
200
201 /**
202 * Settings page.
203 *
204 * Handles the display of the main give settings page in admin.
205 *
206 * @since 1.8
207 * @return bool
208 */
209 public static function output() {
210 // Get current setting page.
211 self::$setting_filter_prefix = give_get_current_setting_page();
212
213 // Bailout: Exit if setting page is not defined.
214 if ( empty( self::$setting_filter_prefix ) ) {
215 return false;
216 }
217
218 /**
219 * Trigger Action.
220 *
221 * Note: action dynamically fire on basis of setting page slug
222 * For example: if you register a setting page with give-settings menu slug
223 * then action will be give-settings_start
224 *
225 * @since 1.8
226 */
227 do_action( self::$setting_filter_prefix . '_start' );
228
229 $current_tab = give_get_current_setting_tab();
230 $current_section = give_get_current_setting_section();
231
232 // Include settings pages.
233 $all_setting = self::get_settings_pages();
234
235 /* @var object $current_setting_obj */
236 $current_setting_obj = new StdClass();
237
238 foreach ( $all_setting as $setting ) {
239 if (
240 method_exists( $setting, 'get_id' ) &&
241 $current_tab === $setting->get_id()
242 ) {
243 $current_setting_obj = $setting;
244 break;
245 }
246 }
247
248 // Save settings if data has been posted.
249 if ( isset( $_POST['_give-save-settings'] ) ) {
250 self::save();
251 }
252
253 /**
254 * Filter the tabs for current setting page.
255 *
256 * Note: filter dynamically fire on basis of setting page slug.
257 * For example: if you register a setting page with give-settings menu slug and general current tab name
258 * then action will be give-settings_tabs_array
259 *
260 * @since 1.8
261 */
262 $tabs = apply_filters( self::$setting_filter_prefix . '_tabs_array', [] );
263
264 include 'views/html-admin-settings.php';
265
266 return true;
267 }
268
269 /**
270 * Get a setting from the settings API.
271 *
272 * @since 1.8
273 *
274 * @param string $option_name Option Name.
275 * @param string $field_id Field ID.
276 * @param mixed $default Default.
277 *
278 * @return string|bool
279 */
280 public static function get_option( $option_name = '', $field_id = '', $default = false ) {
281 // Bailout.
282 if ( empty( $option_name ) && empty( $field_id ) ) {
283 return false;
284 }
285
286 if ( ! empty( $field_id ) && ! empty( $option_name ) ) {
287 // Get field value if any.
288 $option_value = get_option( $option_name );
289
290 $option_value = ( is_array( $option_value ) && array_key_exists( $field_id, $option_value ) )
291 ? $option_value[ $field_id ]
292 : $default;
293 } else {
294 // If option name is empty but not field name then this means, setting is direct store to option table under there field name.
295 $option_name = ! $option_name ? $field_id : $option_name;
296
297 // Get option value if any.
298 $option_value = get_option( $option_name, $default );
299 }
300
301 /**
302 * Filter the option value
303 *
304 * @since 2.2.3
305 *
306 * @param mixed $option_value
307 * @param string $option_name
308 * @param string $field_id
309 * @param mixed $default
310 */
311 return apply_filters( 'give_admin_field_get_value', $option_value, $option_name, $field_id, $default );
312 }
313
314 /**
315 * Output admin fields.
316 *
317 * Loops though the give options array and outputs each field.
318 *
319 * @todo Refactor this function
320 *
321 * @since 1.8
322 * @access public
323 *
324 * @param array $sections Opens array to output.
325 * @param string $option_name Opens array to output.
326 *
327 * @return void
328 */
329 public static function output_fields( $sections, $option_name = '' ) {
330
331 $current_page = give_get_current_setting_page();
332 $current_tab = give_get_current_setting_tab();
333 $current_section = give_get_current_setting_section();
334 $groups = give_get_settings_groups();
335
336 if ( is_array( $groups ) && count( $groups ) > 0 ) {
337 ?>
338 <div class="give-settings-section-content">
339 <div class="give-settings-section-group-menu">
340 <ul>
341 <?php
342 if ( is_array( $groups ) && count( $groups ) > 0 ) {
343 $count = 1;
344
345 foreach ( $groups as $slug => $group ) {
346 $default_group = ( 1 === $count && empty( $_GET['group'] ) ) ? $slug : '';
347 $current_group = ! empty( $_GET['group'] ) ? give_clean( $_GET['group'] ) : $default_group;
348 $active_class = ( $slug === $current_group ) ? 'active' : '';
349
350 echo sprintf(
351 '<li><a class="%1$s" href="%2$s" data-group="%3$s">%4$s</a></li>',
352 esc_html( $active_class ),
353 esc_url( admin_url( "edit.php?post_type=give_forms&page={$current_page}&tab={$current_tab}&section={$current_section}&group={$slug}" ) ),
354 esc_html( $slug ),
355 esc_html( $group )
356 );
357 $count++;
358 }
359 }
360 ?>
361 </ul>
362 </div>
363 <div class="give-settings-section-group-content">
364 <?php
365 $count = 1;
366 foreach ( $sections as $group => $fields ) {
367 if ( ! empty( $group ) ) {
368
369 $default_group = ( 1 === $count && empty( $_GET['group'] ) ) ? $group : '';
370 $current_group = ! empty( $_GET['group'] ) ? give_clean( $_GET['group'] ) : $default_group;
371 $hide_class = $group !== $current_group ? 'give-hidden' : '';
372 ?>
373 <div id="give-settings-section-group-<?php echo esc_attr( $group ); ?>" class="give-settings-section-group <?php echo esc_html( $hide_class ); ?>">
374 <?php
375 foreach ( $fields as $value ) {
376 if ( ! isset( $value['type'] ) ) {
377 continue;
378 }
379 self::prepare_settings_field( $value, $option_name );
380 }
381 ?>
382 </div>
383 <?php
384 }
385
386 $count++;
387 }
388 ?>
389 </div>
390 </div>
391 <?php
392 } else {
393
394 // Loop through each section.
395 foreach ( $sections as $value ) {
396 if ( ! isset( $value['type'] ) ) {
397 continue;
398 }
399 self::prepare_settings_field( $value, $option_name );
400 }
401 }
402
403 }
404
405 /**
406 * This function will help you prepare the admin settings field.
407 *
408 * @since 2.5.5
409 * @access public
410 *
411 * @param array $value Settings Field Array.
412 * @param string $option_name Option Name.
413 *
414 * @return mixed
415 */
416 public static function prepare_settings_field( $value, $option_name ) {
417
418 $current_tab = give_get_current_setting_tab();
419
420 // Field Default values.
421 $defaults = [
422 'id' => '',
423 'class' => '',
424 'css' => '',
425 'default' => '',
426 'desc' => '',
427 'table_html' => true,
428 'repeat' => false,
429 'repeat_btn_title' => __( 'Add Field', 'give' ),
430 ];
431
432 // Set title.
433 $defaults['title'] = isset( $value['name'] ) ? $value['name'] : '';
434
435 // Set default setting.
436 $value = wp_parse_args( $value, $defaults );
437
438 // Colorpicker field.
439 $value['class'] = ( 'colorpicker' === $value['type'] ? trim( $value['class'] ) . ' give-colorpicker' : $value['class'] );
440 $value['type'] = ( 'colorpicker' === $value['type'] ? 'text' : $value['type'] );
441
442 // Custom attribute handling.
443 $custom_attributes = [];
444
445 if ( ! empty( $value['attributes'] ) && is_array( $value['attributes'] ) ) {
446 foreach ( $value['attributes'] as $attribute => $attribute_value ) {
447 $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
448 }
449 }
450
451 // Description handling.
452 $description = self::get_field_description( $value );
453
454 // Switch based on type.
455 switch ( $value['type'] ) {
456
457 // Section Titles.
458 case 'title':
459 if ( ! empty( $value['title'] ) || ! empty( $value['desc'] ) ) {
460 ?>
461 <div class="give-setting-tab-header give-setting-tab-header-<?php echo $current_tab; ?>">
462 <?php if ( ! empty( $value['title'] ) ) : ?>
463 <h2><?php echo self::get_field_title( $value ); ?></h2>
464 <hr>
465 <?php endif; ?>
466
467 <?php if ( ! empty( $value['desc'] ) ) : ?>
468 <?php echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) ); ?>
469 <?php endif; ?>
470 </div>
471 <?php
472 }
473
474 if ( $value['table_html'] ) {
475 echo '<table class="form-table give-setting-tab-body give-setting-tab-body-' . $current_tab . '">' . "\n\n";
476 }
477
478 if ( ! empty( $value['id'] ) ) {
479
480 /**
481 * Trigger Action.
482 *
483 * Note: action dynamically fire on basis of field id.
484 *
485 * @since 1.8
486 */
487 do_action( 'give_settings_' . sanitize_title( $value['id'] ) );
488 }
489
490 break;
491
492 // Section Ends.
493 case 'sectionend':
494 if ( ! empty( $value['id'] ) ) {
495
496 /**
497 * Trigger Action.
498 *
499 * Note: action dynamically fire on basis of field id.
500 *
501 * @since 1.8
502 */
503 do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_end' );
504 }
505
506 if ( $value['table_html'] ) {
507 echo '</table>';
508 }
509
510 if ( ! empty( $value['id'] ) ) {
511
512 /**
513 * Trigger Action.
514 *
515 * Note: action dynamically fire on basis of field id.
516 *
517 * @since 1.8
518 */
519 do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_after' );
520 }
521
522 break;
523
524 // Standard text inputs and subtypes like 'number'.
525 case 'colorpicker':
526 case 'hidden':
527 $value['wrapper_class'] = empty( $value['wrapper_class'] ) ? 'give-hidden' : trim( $value['wrapper_class'] ) . ' give-hidden';
528 case 'text':
529 case 'email':
530 case 'number':
531 case 'password':
532 $type = $value['type'];
533 $option_value = self::get_option( $option_name, $value['id'], $value['default'] );
534
535 // Set default value for repeater field if not any value set yet.
536 if ( $value['repeat'] && is_string( $option_value ) ) {
537 $option_value = [ $value['default'] ];
538 }
539 ?>
540 <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : ''; ?>>
541 <th scope="row" class="titledesc">
542 <label
543 for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
544 </th>
545 <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ); ?>">
546 <?php if ( $value['repeat'] ) : ?>
547 <?php foreach ( $option_value as $index => $field_value ) : ?>
548 <p>
549 <input
550 name="<?php echo esc_attr( $value['id'] ); ?>[]"
551 type="<?php echo esc_attr( $type ); ?>"
552 style="<?php echo esc_attr( $value['css'] ); ?>"
553 value="<?php echo esc_attr( $field_value ); ?>"
554 class="give-input-field<?php echo( empty( $value['class'] ) ? '' : ' ' . esc_attr( $value['class'] ) ); ?> <?php echo esc_attr( $value['id'] ); ?>"
555 <?php echo implode( ' ', $custom_attributes ); ?>
556 />
557 <span class="give-remove-setting-field"
558 title="<?php esc_html_e( 'Remove setting field', 'give' ); ?>">-</span>
559 </p>
560 <?php endforeach; ?>
561 <a href="#" data-id="<?php echo $value['id']; ?>"
562 class="give-repeat-setting-field button-secondary"><?php echo $value['repeat_btn_title']; ?></a>
563 <?php else : ?>
564 <input
565 name="<?php echo esc_attr( $value['id'] ); ?>"
566 id="<?php echo esc_attr( $value['id'] ); ?>"
567 type="<?php echo esc_attr( $type ); ?>"
568 style="<?php echo esc_attr( $value['css'] ); ?>"
569 value="<?php echo esc_attr( $option_value ); ?>"
570 class="give-input-field<?php echo( empty( $value['class'] ) ? '' : ' ' . esc_attr( $value['class'] ) ); ?>"
571 <?php echo implode( ' ', $custom_attributes ); ?>
572 />
573 <?php endif; ?>
574 <?php echo $description; ?>
575 </td>
576 </tr>
577 <?php
578 break;
579
580 // Textarea.
581 case 'textarea':
582 $option_value = self::get_option( $option_name, $value['id'], $value['default'] );
583 $default_attributes = [
584 'rows' => 10,
585 'cols' => 60,
586 ];
587 $textarea_attributes = isset( $value['attributes'] ) ? $value['attributes'] : [];
588 ?>
589 <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : ''; ?>>
590 <th scope="row" class="titledesc">
591 <label
592 for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
593 </th>
594 <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ); ?>">
595 <textarea
596 name="<?php echo esc_attr( $value['id'] ); ?>"
597 id="<?php echo esc_attr( $value['id'] ); ?>"
598 style="<?php echo esc_attr( $value['css'] ); ?>"
599 class="<?php echo esc_attr( $value['class'] ); ?>"
600 <?php echo give_get_attribute_str( $textarea_attributes, $default_attributes ); ?>
601 ><?php echo esc_textarea( $option_value ); ?></textarea>
602 <?php echo $description; ?>
603 </td>
604 </tr>
605 <?php
606 break;
607
608 // Select boxes.
609 case 'select':
610 case 'multiselect':
611 $option_value = self::get_option( $option_name, $value['id'], $value['default'] );
612 $setting_name = esc_attr( $value['id'] ) . ( 'multiselect' === $value['type'] ? '[]' : '' );
613
614 /**
615 * Insert page in option if missing.
616 *
617 * Check success_page setting in general settings.
618 */
619 if (
620 isset( $value['attributes'] ) &&
621 false !== strpos( $value['class'], 'give-select-chosen' ) &&
622 in_array( 'data-search-type', array_keys( $value['attributes'] ) ) &&
623 'pages' === $value['attributes']['data-search-type'] &&
624 ! in_array( $option_value, array_keys( $value['options'] ) )
625 ) {
626 $value['options'][ $option_value ] = get_the_title( $option_value );
627 }
628 ?>
629 <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : ''; ?>>
630 <th scope="row" class="titledesc">
631 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
632 </th>
633 <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ); ?>">
634 <select name="<?php echo $setting_name; ?>"
635 id="<?php echo esc_attr( $value['id'] ); ?>"
636 style="<?php echo esc_attr( $value['css'] ); ?>"
637 class="<?php echo esc_attr( $value['class'] ); ?>"
638 <?php echo implode( ' ', $custom_attributes ); ?>
639 <?php echo ( 'multiselect' === $value['type'] ) ? 'multiple="multiple"' : ''; ?>
640 >
641
642 <?php
643 if ( ! empty( $value['options'] ) ) {
644 foreach ( $value['options'] as $key => $val ) {
645 ?>
646 <option value="<?php echo esc_attr( $key ); ?>"
647 <?php
648
649 if ( is_array( $option_value ) ) {
650 selected( in_array( $key, $option_value ), true );
651 } else {
652 selected( $option_value, $key );
653 }
654
655 ?>
656 ><?php echo $val; ?></option>
657 <?php
658 }
659 }
660 ?>
661
662 </select> <?php echo $description; ?>
663 </td>
664 </tr>
665 <?php
666 break;
667
668 // Radio inputs.
669 case 'radio_inline':
670 $value['class'] = empty( $value['class'] ) ? 'give-radio-inline' : $value['class'] . ' give-radio-inline';
671 case 'radio':
672 $option_value = self::get_option( $option_name, $value['id'], $value['default'] );
673 ?>
674 <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : ''; ?>>
675 <th scope="row" class="titledesc">
676 <label
677 for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
678 </th>
679 <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ); ?> <?php echo( ! empty( $value['class'] ) ? $value['class'] : '' ); ?>">
680 <fieldset>
681 <ul>
682 <?php
683 foreach ( $value['options'] as $key => $val ) {
684 ?>
685 <li>
686 <label><input
687 name="<?php echo esc_attr( $value['id'] ); ?>"
688 value="<?php echo $key; ?>"
689 type="radio"
690 style="<?php echo esc_attr( $value['css'] ); ?>"
691 <?php echo implode( ' ', $custom_attributes ); ?>
692 <?php checked( $key, $option_value ); ?>
693 /> <?php echo $val; ?></label>
694 </li>
695 <?php
696 }
697 ?>
698 <?php echo $description; ?>
699 </fieldset>
700 </td>
701 </tr>
702 <?php
703 break;
704
705 // Checkbox input.
706 case 'checkbox':
707 $option_value = self::get_option( $option_name, $value['id'], $value['default'] );
708 ?>
709 <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : ''; ?>>
710 <th scope="row" class="titledesc">
711 <label
712 for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
713 </th>
714 <td class="give-forminp">
715 <input
716 name="<?php echo esc_attr( $value['id'] ); ?>"
717 id="<?php echo esc_attr( $value['id'] ); ?>"
718 type="checkbox"
719 class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>"
720 value="1"
721 <?php checked( $option_value, 'on' ); ?>
722 <?php echo implode( ' ', $custom_attributes ); ?>
723 />
724 <?php echo $description; ?>
725 </td>
726 </tr>
727 <?php
728 break;
729
730 // Multi Checkbox input.
731 case 'multicheck':
732 $option_value = self::get_option( $option_name, $value['id'], $value['default'] );
733 $option_value = is_array( $option_value ) ? $option_value : [];
734 ?>
735 <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : ''; ?>>
736 <th scope="row" class="titledesc">
737 <label
738 for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
739 </th>
740 <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ); ?> <?php echo( ! empty( $value['class'] ) ? $value['class'] : '' ); ?>">
741 <fieldset>
742 <ul>
743 <?php
744 foreach ( $value['options'] as $key => $val ) {
745 ?>
746 <li>
747 <label>
748 <input
749 name="<?php echo esc_attr( $value['id'] ); ?>[]"
750 value="<?php echo $key; ?>"
751 type="checkbox"
752 style="<?php echo esc_attr( $value['css'] ); ?>"
753 <?php echo implode( ' ', $custom_attributes ); ?>
754 <?php
755 if ( in_array( $key, $option_value ) ) {
756 echo 'checked="checked"';
757 }
758 ?>
759 /> <?php echo $val; ?>
760 </label>
761 </li>
762 <?php
763 }
764 ?>
765 <?php echo $description; ?>
766 </fieldset>
767 </td>
768 </tr>
769 <?php
770 break;
771
772 // File input field.
773 case 'file':
774 case 'media':
775 $option_value = self::get_option( $option_name, $value['id'], $value['default'] );
776 $button_label = sprintf( __( 'Add or Upload %s', 'give' ), ( 'file' === $value['type'] ? __( 'File', 'give' ) : __( 'Image', 'give' ) ) );
777 $fvalue = empty( $value['fvalue'] ) ? 'url' : $value['fvalue'];
778
779 $allow_media_preview_tags = [ 'jpg', 'jpeg', 'png', 'gif', 'ico' ];
780 $preview_image_src = $option_value ? ( 'id' === $fvalue ? wp_get_attachment_url( $option_value ) : $option_value ) : '';
781 $preview_image_extension = $preview_image_src ? pathinfo( $preview_image_src, PATHINFO_EXTENSION ) : '';
782 $is_show_preview = in_array( $preview_image_extension, $allow_media_preview_tags );
783 ?>
784 <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : ''; ?>>
785 <th scope="row" class="titledesc">
786 <label
787 for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
788 </th>
789 <td class="give-forminp">
790 <div class="give-field-wrap">
791 <label for="<?php echo $value['id']; ?>">
792 <input
793 name="<?php echo esc_attr( $value['id'] ); ?>"
794 id="<?php echo esc_attr( $value['id'] ); ?>"
795 type="text"
796 class="give-input-field<?php echo esc_attr( isset( $value['class'] ) ? ' ' . $value['class'] : '' ); ?>"
797 value="<?php echo $option_value; ?>"
798 style="<?php echo esc_attr( $value['css'] ); ?>"
799 <?php echo implode( ' ', $custom_attributes ); ?>
800 />&nbsp;&nbsp;&nbsp;&nbsp;<input class="give-upload-button button" type="button"
801 data-fvalue="<?php echo $fvalue; ?>"
802 data-field-type="<?php echo $value['type']; ?>"
803 value="<?php echo $button_label; ?>">
804 <?php echo $description; ?>
805 <div
806 class="give-image-thumb<?php echo ! $option_value || ! $is_show_preview ? ' give-hidden' : ''; ?>">
807 <span class="give-delete-image-thumb dashicons dashicons-no-alt"></span>
808 <img src="<?php echo $preview_image_src; ?>" alt="">
809 </div>
810 </label>
811 </div>
812 </td>
813 </tr>
814 <?php
815 break;
816
817 // WordPress Editor.
818 case 'wysiwyg':
819 // Get option value.
820 $option_value = self::get_option( $option_name, $value['id'], $value['default'] );
821
822 // Get editor settings.
823 $editor_settings = ! empty( $value['options'] ) ? $value['options'] : [];
824 ?>
825 <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : ''; ?>>
826 <th scope="row" class="titledesc">
827 <label
828 for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
829 </th>
830 <td class="give-forminp">
831 <?php wp_editor( $option_value, $value['id'], $editor_settings ); ?>
832 <?php echo $description; ?>
833 </td>
834 </tr>
835 <?php
836 break;
837
838 // Custom: Email preview buttons field.
839 case 'email_preview_buttons':
840 ?>
841 <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : ''; ?>>
842 <th scope="row" class="titledesc">
843 <label
844 for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
845 </th>
846 <td class="give-forminp">
847 <?php give_email_preview_buttons_callback( $value ); ?>
848 <?php echo $description; ?>
849 </td>
850 </tr>
851 <?php
852 break;
853
854 // Custom: API field.
855 case 'api':
856 give_api_callback();
857 echo $description;
858 break;
859
860 // Custom: Gateway API key.
861 case 'api_key':
862 $option_value = self::get_option( $option_name, $value['id'], $value['default'] );
863 $type = ! empty( $option_value ) ? 'password' : 'text';
864 ?>
865 <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : ''; ?>>
866 <th scope="row" class="titledesc">
867 <label
868 for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
869 </th>
870 <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ); ?>">
871 <input
872 name="<?php echo esc_attr( $value['id'] ); ?>"
873 id="<?php echo esc_attr( $value['id'] ); ?>"
874 type="<?php echo esc_attr( $type ); ?>"
875 style="<?php echo esc_attr( $value['css'] ); ?>"
876 value="<?php echo esc_attr( trim( $option_value ) ); ?>"
877 class="give-input-field<?php echo( empty( $value['class'] ) ? '' : ' ' . esc_attr( $value['class'] ) ); ?>"
878 <?php echo implode( ' ', $custom_attributes ); ?>
879 /> <?php echo $description; ?>
880 </td>
881 </tr>
882 <?php
883 break;
884
885 // Note: only for internal use.
886 case 'chosen':
887 // Get option value.
888 $option_value = array_filter( (array) self::get_option( $option_name, $value['id'], $value['default'] ) );
889 $wrapper_class = ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '';
890 $type = '';
891 $allow_new_values = ! empty( $value['allow-custom-values'] ) && (bool) $value['allow-custom-values'] ? 'data-allows-new-values="true"' : '';
892 $name = give_get_field_name( $value );
893 $choices = $value['options'];
894 $value['style'] = isset( $value['style'] ) ? $value['style'] : '';
895
896 // Set attributes based on multiselect datatype.
897 if ( ! empty( $value['data_type'] ) && 'multiselect' === $value['data_type'] ) {
898 $type = 'multiple';
899 $name .= '[]';
900 $option_value = empty( $option_value ) ? [] : $option_value;
901 }
902
903 // Add dynamically added values to options
904 // we can add option dynamically to chosen select field. For example: "Title Prefixes"
905 if ( $allow_new_values && $option_value ) {
906 $choices = array_merge( array_combine( $option_value, $option_value ), $value['options'] );
907 }
908 ?>
909 <tr valign="top" <?php echo $wrapper_class; ?>>
910 <th scope="row" class="titledesc">
911 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_attr( self::get_field_title( $value ) ); ?></label>
912 </th>
913 <td class="give-forminp give-forminp-<?php echo esc_attr( $value['type'] ); ?>">
914 <select
915 class="give-select-chosen give-chosen-settings"
916 style="<?php echo esc_attr( $value['style'] ); ?>"
917 name="<?php echo esc_attr( $name ); ?>"
918 id="<?php echo esc_attr( $value['id'] ); ?>"
919 <?php
920 echo "{$type} {$allow_new_values}";
921 echo implode( ' ', $custom_attributes );
922 ?>
923 >
924 <?php
925 foreach ( $choices as $key => $name ) {
926 echo sprintf(
927 '<option %1$s value="%2$s">%3$s</option>',
928 in_array( $key, $option_value ) ? 'selected="selected"' : '',
929 esc_attr( $key ),
930 $name
931 );
932 }
933 ?>
934 </select>
935 <?php echo wp_kses_post( $description ); ?>
936 </td>
937 </tr>
938 <?php
939 break;
940
941 // Custom: Log field.
942 case 'logs':
943 // Get current section.
944 $current_section = $_GET['section'] = give_get_current_setting_section();
945
946 /**
947 * Fires for each tab of logs view.
948 *
949 * @since 1.0
950 */
951 do_action( "give_logs_view_{$current_section}" );
952
953 echo $description;
954 break;
955
956 // Custom: Data field.
957 case 'data':
958 include GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-data.php';
959
960 echo $description;
961 break;
962
963 // Custom: Give Docs Link field type.
964 case 'give_docs_link':
965 $wrapper_class = ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '';
966 ?>
967 <tr valign="top" <?php echo esc_html( $wrapper_class ); ?>>
968 <td class="give-docs-link" colspan="2">
969 <p class="give-docs-link">
970 <a href="<?php echo esc_url( $value['url'] ); ?>" target="_blank">
971 <?php
972 echo sprintf(
973 /* translators: %s Title */
974 esc_html__( 'Need Help? See docs on "%s"', 'give' ),
975 esc_html( $value['title'] )
976 );
977 ?>
978 <span class="dashicons dashicons-editor-help"></span>
979 </a>
980 </p>
981 </td>
982 </tr>
983 <?php
984 break;
985
986 // Default: run an action
987 // You can add or handle your custom field action.
988 default:
989 // Get option value.
990 $option_value = self::get_option( $option_name, $value['id'], $value['default'] );
991 do_action( 'give_admin_field_' . $value['type'], $value, $option_value );
992 break;
993 }
994 }
995
996 /**
997 * Helper function to get the formatted description for a given form field.
998 * Plugins can call this when implementing their own custom settings types.
999 *
1000 * @since 1.8
1001 *
1002 * @param array $value The form field value array
1003 *
1004 * @return string The HTML description of the field.
1005 */
1006 public static function get_field_description( $value ) {
1007 $description = '';
1008
1009 // Support for both 'description' and 'desc' args.
1010 $description_key = isset( $value['description'] ) ? 'description' : 'desc';
1011 $value = ( isset( $value[ $description_key ] ) && ! empty( $value[ $description_key ] ) ) ? $value[ $description_key ] : '';
1012
1013 if ( ! empty( $value ) ) {
1014 $description = '<div class="give-field-description">' . wp_kses_post( $value ) . '</div>';
1015 }
1016
1017 return $description;
1018 }
1019
1020
1021 /**
1022 * Helper function to get the formated title.
1023 * Plugins can call this when implementing their own custom settings types.
1024 *
1025 * @since 1.8
1026 *
1027 * @param array $value The form field value array
1028 *
1029 * @return array The description and tip as a 2 element array
1030 */
1031 public static function get_field_title( $value ) {
1032 $title = esc_html( $value['title'] );
1033
1034 // If html tag detected then allow them to print.
1035 if ( strip_tags( $title ) ) {
1036 $title = $value['title'];
1037 }
1038
1039 return $title;
1040 }
1041
1042 /**
1043 * Save admin fields.
1044 *
1045 * Loops though the give options array and outputs each field.
1046 *
1047 * @since 1.8
1048 *
1049 * @param array $options Options array to output
1050 * @param string $option_name Option name to save output. If empty then option will be store in there own option name i.e option id.
1051 *
1052 * @return bool
1053 */
1054 public static function save_fields( $options, $option_name = '' ) {
1055
1056 // Fetch form posted super global data.
1057 $post_data = give_clean( $_POST );
1058
1059 // Bailout, if posted data doesn't exists.
1060 if ( empty( $post_data ) ) {
1061 return false;
1062 }
1063
1064 $new_options = [];
1065 $options_keys = array_keys( $options );
1066 $is_vertical_tabs = is_array( $options_keys ) && count( $options_keys ) > 0 ? ctype_alnum( $options_keys[0] ) : false;
1067
1068 if ( $is_vertical_tabs ) {
1069
1070 // Loop through each vertical tabs related field options to destructure into single array.
1071 foreach ( $options as $option ) {
1072 $new_options = array_merge( $new_options, $option );
1073 }
1074
1075 // Assign new field options.
1076 $options = $new_options;
1077 }
1078
1079 // Options to update will be stored here and saved later.
1080 $update_options = [];
1081
1082 // Loop options and get values to save.
1083 foreach ( $options as $option ) {
1084 if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
1085 continue;
1086 }
1087
1088 // Get posted value.
1089 if ( strstr( $option['id'], '[' ) ) {
1090 parse_str( $option['id'], $option_name_array );
1091 $field_option_name = current( array_keys( $option_name_array ) );
1092 $setting_name = key( $option_name_array[ $field_option_name ] );
1093 $raw_value = isset( $_POST[ $field_option_name ][ $setting_name ] ) ? wp_unslash( $_POST[ $field_option_name ][ $setting_name ] ) : null;
1094 } else {
1095 $field_option_name = $option['id'];
1096 $setting_name = '';
1097 $raw_value = isset( $_POST[ $option['id'] ] ) ? wp_unslash( $_POST[ $option['id'] ] ) : null;
1098 }
1099
1100 // Format the value based on option type.
1101 switch ( $option['type'] ) {
1102 case 'checkbox':
1103 $value = is_null( $raw_value ) ? '' : 'on';
1104 break;
1105 case 'wysiwyg':
1106 case 'textarea':
1107 $value = wp_kses_post( trim( $raw_value ) );
1108 break;
1109 case 'multiselect':
1110 case 'chosen':
1111 $value = array_filter( array_map( 'give_clean', (array) $raw_value ) );
1112 break;
1113 default:
1114 $value = give_clean( $raw_value );
1115 break;
1116 }
1117
1118 /**
1119 * Sanitize the value of an option.
1120 *
1121 * @since 1.8
1122 */
1123 $value = apply_filters( 'give_admin_settings_sanitize_option', $value, $option, $raw_value );
1124
1125 /**
1126 * Sanitize the value of an option by option name.
1127 *
1128 * @since 1.8
1129 */
1130 $value = apply_filters( "give_admin_settings_sanitize_option_{$field_option_name}", $value, $option, $raw_value );
1131
1132 if ( is_null( $value ) ) {
1133 continue;
1134 }
1135
1136 // Check if option is an array and handle that differently to single values.
1137 if ( $field_option_name && $setting_name ) {
1138 if ( ! isset( $update_options[ $field_option_name ] ) ) {
1139 $update_options[ $field_option_name ] = get_option( $field_option_name, [] );
1140 }
1141 if ( ! is_array( $update_options[ $field_option_name ] ) ) {
1142 $update_options[ $field_option_name ] = [];
1143 }
1144 $update_options[ $field_option_name ][ $setting_name ] = $value;
1145 } else {
1146 $update_options[ $field_option_name ] = $value;
1147 }
1148 }
1149
1150 // Save all options in our array or there own option name i.e. option id.
1151 if ( empty( $option_name ) ) {
1152 foreach ( $update_options as $name => $value ) {
1153 update_option( $name, $value, false );
1154
1155 /**
1156 * Trigger action.
1157 *
1158 * Note: This is dynamically fire on basis of option name.
1159 *
1160 * @since 1.8
1161 */
1162 do_action( "give_save_option_{$name}", $value, $name );
1163 }
1164 } else {
1165 $old_options = ( $old_options = get_option( $option_name ) ) ? $old_options : [];
1166 $update_options = array_merge( $old_options, $update_options );
1167
1168 update_option( $option_name, $update_options, false );
1169
1170 /**
1171 * Trigger action.
1172 *
1173 * Note: This is dynamically fire on basis of setting name.
1174 *
1175 * @since 1.8
1176 */
1177 do_action( "give_save_settings_{$option_name}", $update_options, $option_name, $old_options );
1178 }
1179
1180 return true;
1181 }
1182
1183
1184 /**
1185 * Check if admin saving setting or not.
1186 *
1187 * @since 1.8.17
1188 *
1189 * @return bool
1190 */
1191 public static function is_saving_settings() {
1192 return self::verify_nonce();
1193 }
1194
1195 /**
1196 * Verify setting page
1197 *
1198 * @since 2.0
1199 * @access public
1200 *
1201 * @param string $tab
1202 * @param string $section
1203 *
1204 * @return bool
1205 */
1206 public static function is_setting_page( $tab = '', $section = '' ) {
1207 $is_setting_page = false;
1208
1209 // Are we accessing admin?
1210 if ( ! is_admin() ) {
1211 return $is_setting_page;
1212 }
1213
1214 // Are we accessing any give page?
1215 if (
1216 ! isset( $_GET['post_type'], $_GET['page'] )
1217 || 'give_forms' !== give_clean( $_GET['post_type'] )
1218 ) {
1219 return $is_setting_page;
1220 }
1221
1222 // Check fo setting tab.
1223 if ( ! empty( $tab ) ) {
1224 $is_setting_page = ( $tab === give_get_current_setting_tab() );
1225 }
1226
1227 // Check fo setting section.
1228 if ( ! empty( $section ) ) {
1229 $is_setting_page = ( $section === give_get_current_setting_section() );
1230 }
1231
1232 return $is_setting_page;
1233 }
1234 }
1235
1236 endif;
1237