builder
6 years ago
plugin-updates
8 years ago
settings
6 years ago
views
6 years ago
class-evf-admin-addons.php
6 years ago
class-evf-admin-assets.php
6 years ago
class-evf-admin-builder.php
7 years ago
class-evf-admin-editor.php
6 years ago
class-evf-admin-entries-table-list.php
6 years ago
class-evf-admin-entries.php
6 years ago
class-evf-admin-forms-table-list.php
6 years ago
class-evf-admin-forms.php
6 years ago
class-evf-admin-import-export.php
6 years ago
class-evf-admin-menus.php
6 years ago
class-evf-admin-notices.php
6 years ago
class-evf-admin-settings.php
6 years ago
class-evf-admin-tools.php
6 years ago
class-evf-admin-welcome.php
6 years ago
class-evf-admin.php
6 years ago
evf-admin-functions.php
6 years ago
class-evf-admin-settings.php
786 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms Admin Settings Class |
| 4 | * |
| 5 | * @package EverestForms\Admin |
| 6 | * @version 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | if ( ! class_exists( 'EVF_Admin_Settings', false ) ) : |
| 12 | |
| 13 | /** |
| 14 | * EVF_Admin_Settings Class. |
| 15 | */ |
| 16 | class EVF_Admin_Settings { |
| 17 | |
| 18 | /** |
| 19 | * Setting pages. |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | private static $settings = array(); |
| 24 | |
| 25 | /** |
| 26 | * Error messages. |
| 27 | * |
| 28 | * @var array |
| 29 | */ |
| 30 | private static $errors = array(); |
| 31 | |
| 32 | /** |
| 33 | * Update messages. |
| 34 | * |
| 35 | * @var array |
| 36 | */ |
| 37 | private static $messages = array(); |
| 38 | |
| 39 | /** |
| 40 | * Include the settings page classes. |
| 41 | */ |
| 42 | public static function get_settings_pages() { |
| 43 | if ( empty( self::$settings ) ) { |
| 44 | $settings = array(); |
| 45 | |
| 46 | include_once dirname( __FILE__ ) . '/settings/class-evf-settings-page.php'; |
| 47 | |
| 48 | $settings[] = include 'settings/class-evf-settings-general.php'; |
| 49 | $settings[] = include 'settings/class-evf-settings-recaptcha.php'; |
| 50 | $settings[] = include 'settings/class-evf-settings-email.php'; |
| 51 | $settings[] = include 'settings/class-evf-settings-validation.php'; |
| 52 | $settings[] = include 'settings/class-evf-settings-integrations.php'; |
| 53 | |
| 54 | self::$settings = apply_filters( 'everest_forms_get_settings_pages', $settings ); |
| 55 | } |
| 56 | |
| 57 | return self::$settings; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Save the settings. |
| 62 | */ |
| 63 | public static function save() { |
| 64 | global $current_tab; |
| 65 | |
| 66 | check_admin_referer( 'everest-forms-settings' ); |
| 67 | |
| 68 | // Trigger actions. |
| 69 | do_action( 'everest_forms_settings_save_' . $current_tab ); |
| 70 | do_action( 'everest_forms_update_options_' . $current_tab ); |
| 71 | do_action( 'everest_forms_update_options' ); |
| 72 | |
| 73 | self::add_message( esc_html__( 'Your settings have been saved.', 'everest-forms' ) ); |
| 74 | |
| 75 | // Clear any unwanted data and flush rules. |
| 76 | update_option( 'everest_forms_queue_flush_rewrite_rules', 'yes' ); |
| 77 | |
| 78 | do_action( 'everest_forms_settings_saved' ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Add a message. |
| 83 | * |
| 84 | * @param string $text Message. |
| 85 | */ |
| 86 | public static function add_message( $text ) { |
| 87 | self::$messages[] = $text; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Add an error. |
| 92 | * |
| 93 | * @param string $text Message. |
| 94 | */ |
| 95 | public static function add_error( $text ) { |
| 96 | self::$errors[] = $text; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Output messages + errors. |
| 101 | */ |
| 102 | public static function show_messages() { |
| 103 | if ( count( self::$errors ) > 0 ) { |
| 104 | foreach ( self::$errors as $error ) { |
| 105 | echo '<div id="message" class="error inline"><p><strong>' . esc_html( $error ) . '</strong></p></div>'; |
| 106 | } |
| 107 | } elseif ( count( self::$messages ) > 0 ) { |
| 108 | foreach ( self::$messages as $message ) { |
| 109 | echo '<div id="message" class="updated inline"><p><strong>' . esc_html( $message ) . '</strong></p></div>'; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Settings page. |
| 116 | * |
| 117 | * Handles the display of the main everest-forms settings page in admin. |
| 118 | */ |
| 119 | public static function output() { |
| 120 | global $current_section, $current_tab; |
| 121 | |
| 122 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 123 | |
| 124 | do_action( 'everest_forms_settings_start' ); |
| 125 | |
| 126 | wp_enqueue_script( 'everest_forms_settings', evf()->plugin_url() . '/assets/js/admin/settings' . $suffix . '.js', array( 'jquery', 'jquery-confirm', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'iris', 'selectWoo' ), evf()->version, true ); |
| 127 | |
| 128 | wp_localize_script( |
| 129 | 'everest_forms_settings', |
| 130 | 'everest_forms_settings_params', |
| 131 | array( |
| 132 | 'i18n_nav_warning' => __( 'The changes you made will be lost if you navigate away from this page.', 'everest-forms' ), |
| 133 | ) |
| 134 | ); |
| 135 | |
| 136 | // Get tabs for the settings page. |
| 137 | $tabs = apply_filters( 'everest_forms_settings_tabs_array', array() ); |
| 138 | |
| 139 | include dirname( __FILE__ ) . '/views/html-admin-settings.php'; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get a setting from the settings API. |
| 144 | * |
| 145 | * @param string $option_name Option name. |
| 146 | * @param mixed $default Default value. |
| 147 | * @return mixed |
| 148 | */ |
| 149 | public static function get_option( $option_name, $default = '' ) { |
| 150 | if ( ! $option_name ) { |
| 151 | return $default; |
| 152 | } |
| 153 | |
| 154 | // Array value. |
| 155 | if ( strstr( $option_name, '[' ) ) { |
| 156 | |
| 157 | parse_str( $option_name, $option_array ); |
| 158 | |
| 159 | // Option name is first key. |
| 160 | $option_name = current( array_keys( $option_array ) ); |
| 161 | |
| 162 | // Get value. |
| 163 | $option_values = get_option( $option_name, '' ); |
| 164 | |
| 165 | $key = key( $option_array[ $option_name ] ); |
| 166 | |
| 167 | if ( isset( $option_values[ $key ] ) ) { |
| 168 | $option_value = $option_values[ $key ]; |
| 169 | } else { |
| 170 | $option_value = null; |
| 171 | } |
| 172 | } else { |
| 173 | // Single value. |
| 174 | $option_value = get_option( $option_name, null ); |
| 175 | } |
| 176 | |
| 177 | if ( is_array( $option_value ) ) { |
| 178 | $option_value = array_map( 'stripslashes', $option_value ); |
| 179 | } elseif ( ! is_null( $option_value ) ) { |
| 180 | $option_value = stripslashes( $option_value ); |
| 181 | } |
| 182 | |
| 183 | return ( null === $option_value ) ? $default : $option_value; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Output admin fields. |
| 188 | * |
| 189 | * Loops though the everest-forms options array and outputs each field. |
| 190 | * |
| 191 | * @param array[] $options Opens array to output. |
| 192 | */ |
| 193 | public static function output_fields( $options ) { |
| 194 | foreach ( $options as $value ) { |
| 195 | if ( ! isset( $value['type'] ) ) { |
| 196 | continue; |
| 197 | } |
| 198 | if ( ! isset( $value['id'] ) ) { |
| 199 | $value['id'] = ''; |
| 200 | } |
| 201 | if ( ! isset( $value['title'] ) ) { |
| 202 | $value['title'] = isset( $value['name'] ) ? $value['name'] : ''; |
| 203 | } |
| 204 | if ( ! isset( $value['class'] ) ) { |
| 205 | $value['class'] = ''; |
| 206 | } |
| 207 | if ( ! isset( $value['css'] ) ) { |
| 208 | $value['css'] = ''; |
| 209 | } |
| 210 | if ( ! isset( $value['default'] ) ) { |
| 211 | $value['default'] = ''; |
| 212 | } |
| 213 | if ( ! isset( $value['desc'] ) ) { |
| 214 | $value['desc'] = ''; |
| 215 | } |
| 216 | if ( ! isset( $value['desc_tip'] ) ) { |
| 217 | $value['desc_tip'] = false; |
| 218 | } |
| 219 | if ( ! isset( $value['placeholder'] ) ) { |
| 220 | $value['placeholder'] = ''; |
| 221 | } |
| 222 | if ( ! isset( $value['suffix'] ) ) { |
| 223 | $value['suffix'] = ''; |
| 224 | } |
| 225 | if ( ! isset( $value['value'] ) ) { |
| 226 | $value['value'] = self::get_option( $value['id'], $value['default'] ); |
| 227 | } |
| 228 | |
| 229 | // Custom attribute handling. |
| 230 | $custom_attributes = array(); |
| 231 | |
| 232 | if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) ) { |
| 233 | foreach ( $value['custom_attributes'] as $attribute => $attribute_value ) { |
| 234 | $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Description handling. |
| 239 | $field_description = self::get_field_description( $value ); |
| 240 | $description = $field_description['description']; |
| 241 | $tooltip_html = $field_description['tooltip_html']; |
| 242 | |
| 243 | // Switch based on type. |
| 244 | switch ( $value['type'] ) { |
| 245 | |
| 246 | // Section Titles. |
| 247 | case 'title': |
| 248 | if ( ! empty( $value['title'] ) ) { |
| 249 | echo '<h2>' . esc_html( $value['title'] ) . '</h2>'; |
| 250 | } |
| 251 | if ( ! empty( $value['desc'] ) ) { |
| 252 | echo wp_kses_post( wpautop( wptexturize( $value['desc'] ) ) ); |
| 253 | } |
| 254 | echo '<table class="form-table">' . "\n\n"; |
| 255 | if ( ! empty( $value['id'] ) ) { |
| 256 | do_action( 'everest_forms_settings_' . sanitize_title( $value['id'] ) ); |
| 257 | } |
| 258 | break; |
| 259 | |
| 260 | // Section Ends. |
| 261 | case 'sectionend': |
| 262 | if ( ! empty( $value['id'] ) ) { |
| 263 | do_action( 'everest_forms_settings_' . sanitize_title( $value['id'] ) . '_end' ); |
| 264 | } |
| 265 | echo '</table>'; |
| 266 | if ( ! empty( $value['id'] ) ) { |
| 267 | do_action( 'everest_forms_settings_' . sanitize_title( $value['id'] ) . '_after' ); |
| 268 | } |
| 269 | break; |
| 270 | |
| 271 | // Standard text inputs and subtypes like 'number'. |
| 272 | case 'text': |
| 273 | case 'password': |
| 274 | case 'datetime': |
| 275 | case 'datetime-local': |
| 276 | case 'date': |
| 277 | case 'date-time': |
| 278 | case 'month': |
| 279 | case 'time': |
| 280 | case 'week': |
| 281 | case 'number': |
| 282 | case 'email': |
| 283 | case 'url': |
| 284 | case 'tel': |
| 285 | $option_value = $value['value']; |
| 286 | $visibility_class = array(); |
| 287 | |
| 288 | if ( isset( $value['is_visible'] ) ) { |
| 289 | $visibility_class[] = $value['is_visible'] ? 'everest-forms-visible' : 'everest-forms-hidden'; |
| 290 | } |
| 291 | |
| 292 | ?><tr valign="top" class="<?php echo esc_attr( implode( ' ', $visibility_class ) ); ?>"> |
| 293 | <th scope="row" class="titledesc"> |
| 294 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label> |
| 295 | </th> |
| 296 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 297 | <input |
| 298 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 299 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 300 | type="<?php echo esc_attr( $value['type'] ); ?>" |
| 301 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 302 | value="<?php echo esc_attr( $option_value ); ?>" |
| 303 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 304 | placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>" |
| 305 | <?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 306 | /><?php echo esc_html( $value['suffix'] ); ?> <?php echo $description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 307 | </td> |
| 308 | </tr> |
| 309 | <?php |
| 310 | break; |
| 311 | |
| 312 | // Color picker. |
| 313 | case 'color': |
| 314 | $option_value = $value['value']; |
| 315 | |
| 316 | ?> |
| 317 | <tr valign="top"> |
| 318 | <th scope="row" class="titledesc"> |
| 319 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label> |
| 320 | </th> |
| 321 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">‎ |
| 322 | <span class="colorpickpreview" style="background: <?php echo esc_attr( $option_value ); ?>"> </span> |
| 323 | <input |
| 324 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 325 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 326 | type="text" |
| 327 | dir="ltr" |
| 328 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 329 | value="<?php echo esc_attr( $option_value ); ?>" |
| 330 | class="<?php echo esc_attr( $value['class'] ); ?>colorpick" |
| 331 | placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>" |
| 332 | <?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 333 | />‎ <?php echo $description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 334 | <div id="colorPickerDiv_<?php echo esc_attr( $value['id'] ); ?>" class="colorpickdiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"></div> |
| 335 | </td> |
| 336 | </tr> |
| 337 | <?php |
| 338 | break; |
| 339 | |
| 340 | // Textarea. |
| 341 | case 'textarea': |
| 342 | $option_value = $value['value']; |
| 343 | |
| 344 | ?> |
| 345 | <tr valign="top"> |
| 346 | <th scope="row" class="titledesc"> |
| 347 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label> |
| 348 | </th> |
| 349 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 350 | <?php echo $description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 351 | |
| 352 | <textarea |
| 353 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 354 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 355 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 356 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 357 | placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>" |
| 358 | <?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 359 | ><?php echo esc_textarea( $option_value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></textarea> |
| 360 | </td> |
| 361 | </tr> |
| 362 | <?php |
| 363 | break; |
| 364 | |
| 365 | // Select boxes. |
| 366 | case 'select': |
| 367 | case 'multiselect': |
| 368 | $option_value = $value['value']; |
| 369 | |
| 370 | ?> |
| 371 | <tr valign="top"> |
| 372 | <th scope="row" class="titledesc"> |
| 373 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label> |
| 374 | </th> |
| 375 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 376 | <select |
| 377 | name="<?php echo esc_attr( $value['id'] ); ?><?php echo ( 'multiselect' === $value['type'] ) ? '[]' : ''; ?>" |
| 378 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 379 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 380 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 381 | <?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 382 | <?php echo 'multiselect' === $value['type'] ? 'multiple="multiple"' : ''; ?> |
| 383 | > |
| 384 | <?php |
| 385 | foreach ( $value['options'] as $key => $val ) { |
| 386 | ?> |
| 387 | <option value="<?php echo esc_attr( $key ); ?>" |
| 388 | <?php |
| 389 | |
| 390 | if ( is_array( $option_value ) ) { |
| 391 | selected( in_array( (string) $key, $option_value, true ), true ); |
| 392 | } else { |
| 393 | selected( $option_value, (string) $key ); |
| 394 | } |
| 395 | |
| 396 | ?> |
| 397 | > |
| 398 | <?php echo esc_html( $val ); ?></option> |
| 399 | <?php |
| 400 | } |
| 401 | ?> |
| 402 | </select> <?php echo $description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 403 | </td> |
| 404 | </tr> |
| 405 | <?php |
| 406 | break; |
| 407 | |
| 408 | // Radio inputs. |
| 409 | case 'radio': |
| 410 | $option_value = $value['value']; |
| 411 | |
| 412 | ?> |
| 413 | <tr valign="top"> |
| 414 | <th scope="row" class="titledesc"> |
| 415 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label> |
| 416 | </th> |
| 417 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 418 | <fieldset> |
| 419 | <?php echo $description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 420 | <ul class="<?php echo esc_attr( $value['class'] ); ?>"> |
| 421 | <?php |
| 422 | foreach ( $value['options'] as $key => $val ) { |
| 423 | ?> |
| 424 | <li> |
| 425 | <label><input |
| 426 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 427 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 428 | value="<?php echo esc_attr( $key ); ?>" |
| 429 | type="radio" |
| 430 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 431 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 432 | <?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 433 | <?php checked( $key, $option_value ); ?> |
| 434 | /> <?php echo esc_html( $val ); ?></label> |
| 435 | </li> |
| 436 | <?php |
| 437 | } |
| 438 | ?> |
| 439 | </ul> |
| 440 | </fieldset> |
| 441 | </td> |
| 442 | </tr> |
| 443 | <?php |
| 444 | break; |
| 445 | |
| 446 | // Radio image inputs. |
| 447 | case 'radio-image': |
| 448 | $option_value = $value['value']; |
| 449 | |
| 450 | ?> |
| 451 | <tr valign="top"> |
| 452 | <th scope="row" class="titledesc"> |
| 453 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label> |
| 454 | </th> |
| 455 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 456 | <fieldset> |
| 457 | <ul> |
| 458 | <?php |
| 459 | foreach ( $value['options'] as $key => $val ) { |
| 460 | ?> |
| 461 | <li> |
| 462 | <label> |
| 463 | <img src="<?php echo esc_html( $val['image'] ); ?>"> |
| 464 | <input |
| 465 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 466 | value="<?php echo esc_attr( $key ); ?>" |
| 467 | type="radio" |
| 468 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 469 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 470 | <?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 471 | <?php checked( $key, $option_value ); ?> |
| 472 | /> |
| 473 | <?php echo esc_html( $val['name'] ); ?></label> |
| 474 | </li> |
| 475 | <?php |
| 476 | } |
| 477 | ?> |
| 478 | </ul> |
| 479 | <?php echo $description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 480 | </fieldset> |
| 481 | </td> |
| 482 | </tr> |
| 483 | <?php |
| 484 | break; |
| 485 | |
| 486 | // Checkbox input. |
| 487 | case 'checkbox': |
| 488 | $option_value = $value['value']; |
| 489 | $visibility_class = array(); |
| 490 | |
| 491 | if ( ! isset( $value['hide_if_checked'] ) ) { |
| 492 | $value['hide_if_checked'] = false; |
| 493 | } |
| 494 | if ( ! isset( $value['show_if_checked'] ) ) { |
| 495 | $value['show_if_checked'] = false; |
| 496 | } |
| 497 | if ( 'yes' === $value['hide_if_checked'] || 'yes' === $value['show_if_checked'] ) { |
| 498 | $visibility_class[] = 'hidden_option'; |
| 499 | } |
| 500 | if ( 'option' === $value['hide_if_checked'] ) { |
| 501 | $visibility_class[] = 'hide_options_if_checked'; |
| 502 | } |
| 503 | if ( 'option' === $value['show_if_checked'] ) { |
| 504 | $visibility_class[] = 'show_options_if_checked'; |
| 505 | } |
| 506 | if ( isset( $value['is_visible'] ) ) { |
| 507 | $visibility_class[] = $value['is_visible'] ? 'everest-forms-visible' : 'everest-forms-hidden'; |
| 508 | } |
| 509 | |
| 510 | if ( ! isset( $value['checkboxgroup'] ) || 'start' === $value['checkboxgroup'] ) { |
| 511 | ?> |
| 512 | <tr valign="top" class="<?php echo esc_attr( implode( ' ', $visibility_class ) ); ?>"> |
| 513 | <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ); ?></th> |
| 514 | <td class="forminp forminp-checkbox"> |
| 515 | <fieldset> |
| 516 | <?php |
| 517 | } else { |
| 518 | ?> |
| 519 | <fieldset class="<?php echo esc_attr( implode( ' ', $visibility_class ) ); ?>"> |
| 520 | <?php |
| 521 | } |
| 522 | |
| 523 | if ( ! empty( $value['title'] ) ) { |
| 524 | ?> |
| 525 | <legend class="screen-reader-text"><span><?php echo esc_html( $value['title'] ); ?></span></legend> |
| 526 | <?php |
| 527 | } |
| 528 | |
| 529 | ?> |
| 530 | <label for="<?php echo esc_attr( $value['id'] ); ?>"> |
| 531 | <input |
| 532 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 533 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 534 | type="checkbox" |
| 535 | class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>" |
| 536 | value="1" |
| 537 | <?php checked( $option_value, 'yes' ); ?> |
| 538 | <?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 539 | /> <?php echo $description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 540 | </label> <?php echo $tooltip_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 541 | <?php |
| 542 | |
| 543 | if ( ! isset( $value['checkboxgroup'] ) || 'end' === $value['checkboxgroup'] ) { |
| 544 | ?> |
| 545 | </fieldset> |
| 546 | </td> |
| 547 | </tr> |
| 548 | <?php |
| 549 | } else { |
| 550 | ?> |
| 551 | </fieldset> |
| 552 | <?php |
| 553 | } |
| 554 | break; |
| 555 | |
| 556 | // Single page selects. |
| 557 | case 'single_select_page': |
| 558 | $args = array( |
| 559 | 'name' => $value['id'], |
| 560 | 'id' => $value['id'], |
| 561 | 'sort_column' => 'menu_order', |
| 562 | 'sort_order' => 'ASC', |
| 563 | 'show_option_none' => ' ', |
| 564 | 'class' => $value['class'], |
| 565 | 'echo' => false, |
| 566 | 'selected' => absint( $value['value'] ), |
| 567 | 'post_status' => 'publish,private,draft', |
| 568 | ); |
| 569 | |
| 570 | if ( isset( $value['args'] ) ) { |
| 571 | $args = wp_parse_args( $value['args'], $args ); |
| 572 | } |
| 573 | |
| 574 | ?> |
| 575 | <tr valign="top" class="single_select_page"> |
| 576 | <th scope="row" class="titledesc"> |
| 577 | <label><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label> |
| 578 | </th> |
| 579 | <td class="forminp"> |
| 580 | <?php echo str_replace( ' id=', " data-placeholder='" . esc_attr__( 'Select a page…', 'everest-forms' ) . "' style='" . $value['css'] . "' class='" . $value['class'] . "' id=", wp_dropdown_pages( $args ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> <?php echo $description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 581 | </td> |
| 582 | </tr> |
| 583 | <?php |
| 584 | break; |
| 585 | |
| 586 | // Days/months/years selector. |
| 587 | case 'relative_date_selector': |
| 588 | $periods = array( |
| 589 | 'days' => __( 'Day(s)', 'everest-forms' ), |
| 590 | 'weeks' => __( 'Week(s)', 'everest-forms' ), |
| 591 | 'months' => __( 'Month(s)', 'everest-forms' ), |
| 592 | 'years' => __( 'Year(s)', 'everest-forms' ), |
| 593 | ); |
| 594 | $option_value = evf_parse_relative_date_option( $value['value'] ); |
| 595 | ?> |
| 596 | <tr valign="top"> |
| 597 | <th scope="row" class="titledesc"> |
| 598 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label> |
| 599 | </th> |
| 600 | <td class="forminp"> |
| 601 | <input |
| 602 | name="<?php echo esc_attr( $value['id'] ); ?>[number]" |
| 603 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 604 | type="number" |
| 605 | style="width: 80px;" |
| 606 | value="<?php echo esc_attr( $option_value['number'] ); ?>" |
| 607 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 608 | placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>" |
| 609 | step="1" |
| 610 | min="1" |
| 611 | <?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 612 | /> |
| 613 | <select name="<?php echo esc_attr( $value['id'] ); ?>[unit]" style="width: auto;"> |
| 614 | <?php |
| 615 | foreach ( $periods as $value => $label ) { |
| 616 | echo '<option value="' . esc_attr( $value ) . '"' . selected( $option_value['unit'], $value, false ) . '>' . esc_html( $label ) . '</option>'; |
| 617 | } |
| 618 | ?> |
| 619 | </select> <?php echo ( $description ) ? $description : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 620 | </td> |
| 621 | </tr> |
| 622 | <?php |
| 623 | break; |
| 624 | |
| 625 | // Default: run an action. |
| 626 | default: |
| 627 | do_action( 'everest_forms_admin_field_' . $value['type'], $value ); |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Helper function to get the formatted description and tip HTML for a |
| 635 | * given form field. Plugins can call this when implementing their own custom |
| 636 | * settings types. |
| 637 | * |
| 638 | * @param array $value The form field value array. |
| 639 | * @return array The description and tip as a 2 element array. |
| 640 | */ |
| 641 | public static function get_field_description( $value ) { |
| 642 | $description = ''; |
| 643 | $tooltip_html = ''; |
| 644 | |
| 645 | if ( true === $value['desc_tip'] ) { |
| 646 | $tooltip_html = $value['desc']; |
| 647 | } elseif ( ! empty( $value['desc_tip'] ) ) { |
| 648 | $description = $value['desc']; |
| 649 | $tooltip_html = $value['desc_tip']; |
| 650 | } elseif ( ! empty( $value['desc'] ) ) { |
| 651 | $description = $value['desc']; |
| 652 | } |
| 653 | |
| 654 | if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ), true ) ) { |
| 655 | $description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>'; |
| 656 | } elseif ( $description && in_array( $value['type'], array( 'checkbox' ), true ) ) { |
| 657 | $description = wp_kses_post( $description ); |
| 658 | } elseif ( $description ) { |
| 659 | $description = '<p class="description">' . wp_kses_post( $description ) . '</p>'; |
| 660 | } |
| 661 | |
| 662 | if ( $tooltip_html && in_array( $value['type'], array( 'checkbox' ), true ) ) { |
| 663 | $tooltip_html = '<p class="description">' . $tooltip_html . '</p>'; |
| 664 | } elseif ( $tooltip_html ) { |
| 665 | $tooltip_html = evf_help_tip( $tooltip_html ); |
| 666 | } |
| 667 | |
| 668 | return array( |
| 669 | 'description' => $description, |
| 670 | 'tooltip_html' => $tooltip_html, |
| 671 | ); |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Save admin fields. |
| 676 | * |
| 677 | * Loops though the everest-forms options array and outputs each field. |
| 678 | * |
| 679 | * @param array $options Options array to output. |
| 680 | * @param array $data Optional. Data to use for saving. Defaults to $_POST. |
| 681 | * @return bool |
| 682 | */ |
| 683 | public static function save_fields( $options, $data = null ) { |
| 684 | if ( is_null( $data ) ) { |
| 685 | $data = $_POST; // phpcs:ignore WordPress.Security.NonceVerification |
| 686 | } |
| 687 | if ( empty( $data ) ) { |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | // Options to update will be stored here and saved later. |
| 692 | $update_options = array(); |
| 693 | $autoload_options = array(); |
| 694 | |
| 695 | // Loop options and get values to save. |
| 696 | foreach ( $options as $option ) { |
| 697 | if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) || ( isset( $option['is_option'] ) && false === $option['is_option'] ) ) { |
| 698 | continue; |
| 699 | } |
| 700 | |
| 701 | // Get posted value. |
| 702 | if ( strstr( $option['id'], '[' ) ) { |
| 703 | parse_str( $option['id'], $option_name_array ); |
| 704 | $option_name = current( array_keys( $option_name_array ) ); |
| 705 | $setting_name = key( $option_name_array[ $option_name ] ); |
| 706 | $raw_value = isset( $data[ $option_name ][ $setting_name ] ) ? wp_unslash( $data[ $option_name ][ $setting_name ] ) : null; |
| 707 | } else { |
| 708 | $option_name = $option['id']; |
| 709 | $setting_name = ''; |
| 710 | $raw_value = isset( $data[ $option['id'] ] ) ? wp_unslash( $data[ $option['id'] ] ) : null; |
| 711 | } |
| 712 | |
| 713 | // Format the value based on option type. |
| 714 | switch ( $option['type'] ) { |
| 715 | case 'checkbox': |
| 716 | $value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no'; |
| 717 | break; |
| 718 | case 'textarea': |
| 719 | $value = wp_kses_post( trim( $raw_value ) ); |
| 720 | break; |
| 721 | case 'select': |
| 722 | $allowed_values = empty( $option['options'] ) ? array() : array_map( 'strval', array_keys( $option['options'] ) ); |
| 723 | if ( empty( $option['default'] ) && empty( $allowed_values ) ) { |
| 724 | $value = null; |
| 725 | break; |
| 726 | } |
| 727 | $default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] ); |
| 728 | $value = in_array( $raw_value, $allowed_values, true ) ? $raw_value : $default; |
| 729 | break; |
| 730 | default: |
| 731 | $value = evf_clean( $raw_value ); |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Sanitize the value of an option. |
| 737 | * |
| 738 | * @since 1.0.0 |
| 739 | */ |
| 740 | $value = apply_filters( 'everest_forms_admin_settings_sanitize_option', $value, $option, $raw_value ); |
| 741 | |
| 742 | /** |
| 743 | * Sanitize the value of an option by option name. |
| 744 | * |
| 745 | * @since 1.0.0 |
| 746 | */ |
| 747 | $value = apply_filters( "everest_forms_admin_settings_sanitize_option_$option_name", $value, $option, $raw_value ); |
| 748 | |
| 749 | if ( is_null( $value ) ) { |
| 750 | continue; |
| 751 | } |
| 752 | |
| 753 | // Check if option is an array and handle that differently to single values. |
| 754 | if ( $option_name && $setting_name ) { |
| 755 | if ( ! isset( $update_options[ $option_name ] ) ) { |
| 756 | $update_options[ $option_name ] = get_option( $option_name, array() ); |
| 757 | } |
| 758 | if ( ! is_array( $update_options[ $option_name ] ) ) { |
| 759 | $update_options[ $option_name ] = array(); |
| 760 | } |
| 761 | $update_options[ $option_name ][ $setting_name ] = $value; |
| 762 | } else { |
| 763 | $update_options[ $option_name ] = $value; |
| 764 | } |
| 765 | |
| 766 | $autoload_options[ $option_name ] = isset( $option['autoload'] ) ? (bool) $option['autoload'] : true; |
| 767 | |
| 768 | /** |
| 769 | * Fire an action before saved. |
| 770 | * |
| 771 | * @deprecated 1.2.0 - doesn't allow manipulation of values! |
| 772 | */ |
| 773 | do_action( 'everest_forms_update_option', $option ); |
| 774 | } |
| 775 | |
| 776 | // Save all options in our array. |
| 777 | foreach ( $update_options as $name => $value ) { |
| 778 | update_option( $name, $value, $autoload_options[ $name ] ? 'yes' : 'no' ); |
| 779 | } |
| 780 | |
| 781 | return true; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | endif; |
| 786 |