|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class Turbo_Drive_Settings |
| 4 | + * |
| 5 | + * Manages the settings page for Turbo Drive in WordPress admin |
| 6 | + * |
| 7 | + * @package Turbo_Drive |
| 8 | + */ |
| 9 | + |
| 10 | +// Exit if accessed directly. |
| 11 | +if (! defined('ABSPATH')) { |
| 12 | + exit; |
| 13 | +} |
| 14 | + |
| 15 | +class Turbo_Drive_Settings |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Instance of the class |
| 19 | + * |
| 20 | + * @var Turbo_Drive_Settings |
| 21 | + */ |
| 22 | + private static $instance = null; |
| 23 | + |
| 24 | + /** |
| 25 | + * Option name in the database |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + private $option_name = 'turbo_drive_options'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Default options |
| 33 | + * |
| 34 | + * @var array |
| 35 | + */ |
| 36 | + private $default_options = array( |
| 37 | + 'progress_bar_color' => '#29d', |
| 38 | + ); |
| 39 | + |
| 40 | + /** |
| 41 | + * Constructor |
| 42 | + */ |
| 43 | + private function __construct() |
| 44 | + { |
| 45 | + add_action('admin_menu', array( $this, 'add_settings_page' )); |
| 46 | + add_action('admin_init', array( $this, 'register_settings' )); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get the unique instance of the class |
| 51 | + * |
| 52 | + * @return Turbo_Drive_Settings |
| 53 | + */ |
| 54 | + public static function get_instance() |
| 55 | + { |
| 56 | + if (self::$instance === null) { |
| 57 | + self::$instance = new self(); |
| 58 | + } |
| 59 | + |
| 60 | + return self::$instance; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Add settings page to admin menu |
| 65 | + */ |
| 66 | + public function add_settings_page() |
| 67 | + { |
| 68 | + add_options_page( |
| 69 | + __('Turbo Drive Settings', 'turbo-drive'), |
| 70 | + __('Turbo Drive', 'turbo-drive'), |
| 71 | + 'manage_options', |
| 72 | + 'turbo-drive-settings', |
| 73 | + array( $this, 'render_settings_page' ) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Register settings |
| 79 | + */ |
| 80 | + public function register_settings() |
| 81 | + { |
| 82 | + register_setting( |
| 83 | + 'turbo_drive_settings_group', |
| 84 | + $this->option_name, |
| 85 | + array( $this, 'sanitize_options' ) |
| 86 | + ); |
| 87 | + |
| 88 | + add_settings_section( |
| 89 | + 'turbo_drive_general_section', |
| 90 | + __('General Settings', 'turbo-drive'), |
| 91 | + array( $this, 'render_general_section' ), |
| 92 | + 'turbo-drive-settings' |
| 93 | + ); |
| 94 | + |
| 95 | + add_settings_field( |
| 96 | + 'progress_bar_color', |
| 97 | + __('Progress Bar Color', 'turbo-drive'), |
| 98 | + array( $this, 'render_progress_bar_color_field' ), |
| 99 | + 'turbo-drive-settings', |
| 100 | + 'turbo_drive_general_section' |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Sanitize options before saving |
| 106 | + * |
| 107 | + * @param array $input Options to sanitize. |
| 108 | + * @return array Sanitized options. |
| 109 | + */ |
| 110 | + public function sanitize_options($input) |
| 111 | + { |
| 112 | + $sanitized_input = array(); |
| 113 | + |
| 114 | + // Sanitize progress bar color |
| 115 | + if (isset($input['progress_bar_color'])) { |
| 116 | + $sanitized_input['progress_bar_color'] = sanitize_hex_color($input['progress_bar_color']); |
| 117 | + if (empty($sanitized_input['progress_bar_color'])) { |
| 118 | + $sanitized_input['progress_bar_color'] = $this->default_options['progress_bar_color']; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $sanitized_input; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Display general section description |
| 127 | + */ |
| 128 | + public function render_general_section() |
| 129 | + { |
| 130 | + echo '<p>' . esc_html__('Configure Turbo Drive options for your site.', 'turbo-drive') . '</p>'; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Display progress bar color field |
| 135 | + */ |
| 136 | + public function render_progress_bar_color_field() |
| 137 | + { |
| 138 | + $options = $this->get_options(); |
| 139 | + $color = isset($options['progress_bar_color']) ? $options['progress_bar_color'] : $this->default_options['progress_bar_color']; |
| 140 | + |
| 141 | + echo '<input type="color" id="turbo_drive_progress_bar_color" name="' . esc_attr($this->option_name) . '[progress_bar_color]" value="' . esc_attr($color) . '">'; |
| 142 | + echo '<p class="description">' . esc_html__('Choose the color of the progress bar that appears during page loads.', 'turbo-drive') . '</p>'; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Display settings page |
| 147 | + */ |
| 148 | + public function render_settings_page() |
| 149 | + { |
| 150 | + if (! current_user_can('manage_options')) { |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + ?> |
| 155 | + <div class="wrap"> |
| 156 | + <h1><?php echo esc_html(get_admin_page_title()); ?></h1> |
| 157 | + <form action="options.php" method="post"> |
| 158 | + <?php |
| 159 | + settings_fields('turbo_drive_settings_group'); |
| 160 | + do_settings_sections('turbo-drive-settings'); |
| 161 | + submit_button(); |
| 162 | + ?> |
| 163 | + </form> |
| 164 | + </div> |
| 165 | + <?php |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Get options with default values if necessary |
| 170 | + * |
| 171 | + * @return array |
| 172 | + */ |
| 173 | + public function get_options() |
| 174 | + { |
| 175 | + $options = get_option($this->option_name, $this->default_options); |
| 176 | + return wp_parse_args($options, $this->default_options); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Get value of a specific option |
| 181 | + * |
| 182 | + * @param string $key Option key. |
| 183 | + * @param mixed $default Default value. |
| 184 | + * @return mixed |
| 185 | + */ |
| 186 | + public function get_option($key, $default = null) |
| 187 | + { |
| 188 | + $options = $this->get_options(); |
| 189 | + |
| 190 | + if (isset($options[$key])) { |
| 191 | + return $options[$key]; |
| 192 | + } |
| 193 | + |
| 194 | + return $default !== null ? $default : $this->default_options[$key] ?? null; |
| 195 | + } |
| 196 | +} |
0 commit comments