*/ class UsersWP_Admin_Settings { /** * Setting pages. * * @var array */ private static $settings = array(); /** * Error messages. * * @var array */ private static $errors = array(); /** * Update messages. * * @var array */ private static $messages = array(); public function __construct() { } /** * Include the settings page classes. */ public static function get_settings_pages() { if ( empty( self::$settings ) ) { $settings = array(); $settings[] = include 'class-uwp-settings-general.php'; $settings[] = include 'class-uwp-settings-redirects.php'; $settings[] = include 'class-uwp-settings-emails.php'; $settings[] = include 'class-uwp-settings-import-export.php'; $settings[] = include 'class-uwp-settings-addons.php'; $settings[] = include 'class-uwp-settings-uninstall.php'; self::$settings = apply_filters( 'uwp_get_settings_pages', $settings ); } return self::$settings; } /** * Save the settings. */ public static function save() { global $current_tab; if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'userswp-settings' ) ) { die( esc_html__( 'Action failed. Please refresh the page and retry.', 'userswp' ) ); } // Trigger actions do_action( 'uwp_settings_save_' . $current_tab ); do_action( 'uwp_update_options_' . $current_tab ); do_action( 'uwp_update_options' ); self::add_message( __( 'Your settings have been saved.', 'userswp' ) ); // Clear flush rules wp_schedule_single_event( time(), 'uwp_flush_rewrite_rules' ); do_action( 'uwp_settings_saved' ); } /** * Add a message. * @param string $text */ public static function add_message( $text ) { self::$messages[] = $text; } /** * Add an error. * @param string $text */ public static function add_error( $text ) { self::$errors[] = $text; } /** * Output messages + errors. */ public static function show_messages() { if ( sizeof( self::$errors ) > 0 ) { foreach ( self::$errors as $error ) { echo '
' . esc_html( $error ) . '
' . esc_html( $message ) . '
';
}
?>
' . wp_kses_post( $description ) . '
'; } elseif ( $description && in_array( $value['type'], array( 'checkbox' ) ) ) { $description = wp_kses_post( $description ); } elseif ( $description ) { $description = '' . wp_kses_post( $description ) . ''; } if ( $tooltip_html && in_array( $value['type'], array( 'checkbox' ) ) ) { $tooltip_html = '' . $tooltip_html . '
'; } elseif ( $tooltip_html ) { $tooltip_html = uwp_help_tip( $tooltip_html ); } return array( 'description' => $description, 'tooltip_html' => $tooltip_html, ); } public function uwp_get_settings() { $settings = get_option( 'uwp_settings' ); if ( empty( $settings ) ) { // Update old settings with new single option $general_settings = is_array( get_option( 'uwp_settings_general' ) ) ? get_option( 'uwp_settings_general' ) : array(); $ext_settings = is_array( get_option( 'uwp_settings_extensions' ) ) ? get_option( 'uwp_settings_extensions' ) : array(); $settings = array_merge( $general_settings, $ext_settings ); update_option( 'uwp_settings', $settings ); } return apply_filters( 'uwp_get_settings', $settings ); } /** * Save admin fields. * * Loops though the userswp options array and outputs each field. * * @param array $options Options array to output * @param array $data Optional. Data to use for saving. Defaults to $_POST. * @return bool */ public static function save_fields( $options, $data = null ) { if ( is_null( $data ) ) { $data = $_POST; } if ( empty( $data ) ) { return false; } // Options to update will be stored here and saved later. $update_options = array(); // Loop options and get values to save. foreach ( $options as $option ) { if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) { continue; } // Get posted value. if ( strstr( $option['id'], '[' ) ) { parse_str( $option['id'], $option_name_array ); $option_name = current( array_keys( $option_name_array ) ); $setting_name = key( $option_name_array[ $option_name ] ); $raw_value = isset( $data[ $option_name ][ $setting_name ] ) ? wp_unslash( $data[ $option_name ][ $setting_name ] ) : null; } else { $option_name = $option['id']; $setting_name = ''; $raw_value = isset( $data[ $option['id'] ] ) ? wp_unslash( $data[ $option['id'] ] ) : null; } // Format the value based on option type. switch ( $option['type'] ) { case 'checkbox': $value = '1' === $raw_value ? 1 : 0; break; case 'textarea': case 'editor': $value = wp_kses_post( trim( $raw_value ) ); break; case 'multiselect': case 'multi_select_countries': $value = array_filter( array_map( 'uwp_clean', (array) $raw_value ) ); break; case 'multicheckbox': $value = array_map( 'uwp_clean', (array) $raw_value ); break; case 'image_width': $value = array(); if ( isset( $raw_value['width'] ) ) { $value['width'] = uwp_clean( $raw_value['width'] ); $value['height'] = uwp_clean( $raw_value['height'] ); $value['crop'] = isset( $raw_value['crop'] ) ? 1 : 0; } else { $value['width'] = $option['default']['width']; $value['height'] = $option['default']['height']; $value['crop'] = $option['default']['crop']; } break; case 'select': $allowed_values = empty( $option['options'] ) ? array() : array_keys( $option['options'] ); if ( empty( $option['default'] ) && empty( $allowed_values ) ) { $value = null; break; } $default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] ); $value = in_array( $raw_value, $allowed_values ) ? $raw_value : $default; break; default: $value = uwp_clean( $raw_value ); break; } /** * Sanitize the value of an option. */ $value = apply_filters( 'uwp_admin_settings_sanitize_option', $value, $option, $raw_value ); /** * Sanitize the value of an option by option name. */ $value = apply_filters( "uwp_admin_settings_sanitize_option_$option_name", $value, $option, $raw_value ); if ( is_null( $value ) ) { continue; } // Check if option is an array and handle that differently to single values. if ( $option_name && $setting_name ) { if ( ! isset( $update_options[ $option_name ] ) ) { $update_options[ $option_name ] = self::get_option( $option_name, array() ); } if ( ! is_array( $update_options[ $option_name ] ) ) { $update_options[ $option_name ] = array(); } $update_options[ $option_name ][ $setting_name ] = $value; } else { $update_options[ $option_name ] = $value; } } // Save all options in our array. foreach ( $update_options as $name => $value ) { uwp_update_option( $name, $value ); } return true; } /** * Get a setting from the settings API. * * @param mixed $option_name * @param mixed $default * @return string */ public static function get_option( $option_name, $default = '' ) { return uwp_get_option( $option_name, $default ); } }