| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Settings |
|---|
| 4 | * |
|---|
| 5 | * From Easy Digital Downloads, changed and added sections to tabs |
|---|
| 6 | * @package ERM |
|---|
| 7 | * @subpackage Admin |
|---|
| 8 | * @copyright Copyright (c) 2022, Alejandro Pascual |
|---|
| 9 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
|---|
| 10 | * @since 1.0 |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * Class ERM_Settings |
|---|
| 15 | * |
|---|
| 16 | * @since 1.0 |
|---|
| 17 | */ |
|---|
| 18 | class ERM_Settings { |
|---|
| 19 | |
|---|
| 20 | private $options; |
|---|
| 21 | private $set_name; |
|---|
| 22 | |
|---|
| 23 | public function __construct( $name = 'erm_settings' ) |
|---|
| 24 | { |
|---|
| 25 | $this->set_name = $name; |
|---|
| 26 | $this->options = get_option( $this->set_name, array() ); |
|---|
| 27 | add_action( 'admin_init', array( $this, 'register_settings') ); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | public function register_settings() { |
|---|
| 31 | |
|---|
| 32 | if ( false == get_option( $this->set_name ) ) { |
|---|
| 33 | add_option( $this->set_name ); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | foreach( $this->list_of_settings() as $tab => $settings ) { |
|---|
| 37 | |
|---|
| 38 | // Manage tab with several sections inside (name_) underscore at the end means sub settings |
|---|
| 39 | if (preg_match('/.+_$/', $tab)) { |
|---|
| 40 | |
|---|
| 41 | foreach($settings as $sec=>$sub_settings) { |
|---|
| 42 | $this->add_settings_section( $tab.$sec ); |
|---|
| 43 | $this->add_settings_fields( $tab.$sec, $sub_settings ); |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | else { |
|---|
| 47 | $this->add_settings_section( $tab); |
|---|
| 48 | $this->add_settings_fields( $tab, $settings ); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | register_setting( $this->set_name, $this->set_name, array( $this, 'sanitize') ); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public function add_settings_section( $tab ) { |
|---|
| 56 | |
|---|
| 57 | add_settings_section( |
|---|
| 58 | $this->set_name . '_' . $tab, |
|---|
| 59 | __return_null(), |
|---|
| 60 | '__return_false', |
|---|
| 61 | $this->set_name . '_' . $tab |
|---|
| 62 | ); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public function add_settings_fields( $tab, $settings ) { |
|---|
| 66 | |
|---|
| 67 | foreach( $settings as $key => $option ) { |
|---|
| 68 | |
|---|
| 69 | $name = isset( $option['name'] ) ? $option['name'] : ''; |
|---|
| 70 | |
|---|
| 71 | $callback = is_callable( array($this, 'show_'.$option['type']) ) ? array($this, 'show_'.$option['type']) : array($this, 'show_missing'); |
|---|
| 72 | |
|---|
| 73 | add_settings_field( |
|---|
| 74 | $this->set_name . '[' . $key . ']', |
|---|
| 75 | $name, |
|---|
| 76 | $callback, |
|---|
| 77 | $this->set_name . '_' . $tab, // Page |
|---|
| 78 | $this->set_name . '_' . $tab, // Section |
|---|
| 79 | array( |
|---|
| 80 | 'id' => $key, |
|---|
| 81 | 'desc' => ! empty( $option['desc'] ) ? $option['desc'] : '', |
|---|
| 82 | 'name' => isset( $option['name'] ) ? $option['name'] : null, |
|---|
| 83 | 'section' => $tab, |
|---|
| 84 | 'size' => isset( $option['size'] ) ? $option['size'] : null, |
|---|
| 85 | 'options' => isset( $option['options'] ) ? $option['options'] : '', |
|---|
| 86 | 'std' => isset( $option['std'] ) ? $option['std'] : '', |
|---|
| 87 | 'callback'=> isset( $option['callback'] ) ? $option['callback'] : '', |
|---|
| 88 | 'options' => isset( $option['options'] ) ? $option['options'] : '' |
|---|
| 89 | ) |
|---|
| 90 | ); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | public function list_of_settings() { |
|---|
| 95 | |
|---|
| 96 | $list = array( |
|---|
| 97 | 'general' => apply_filters( 'erm_settings_general', // Tab general |
|---|
| 98 | array( |
|---|
| 99 | /*'erm_menu_slug' => array( |
|---|
| 100 | 'name' => __( 'Menu slug', 'erm' ), |
|---|
| 101 | 'desc' => __( 'Use this for changing the slug for the menu Custom Post Type. By default is qr_menu.' , 'erm' ), |
|---|
| 102 | 'type' => 'text', |
|---|
| 103 | 'size' => 'medium', |
|---|
| 104 | 'std' => 'qr_menu' |
|---|
| 105 | ),*/ |
|---|
| 106 | 'erm_currency' => array( |
|---|
| 107 | 'name' => __( 'Currency', 'erm' ), |
|---|
| 108 | 'desc' => __( 'Add this character to the price. Leave it blank if you don\'t want to display.' , 'erm' ), |
|---|
| 109 | 'type' => 'text', |
|---|
| 110 | 'size' => 'small', |
|---|
| 111 | 'std' => '' |
|---|
| 112 | ), |
|---|
| 113 | 'erm_currency_position' => array( |
|---|
| 114 | 'name' => __( 'Currency position', 'erm' ), |
|---|
| 115 | 'desc' => __( '' , 'erm' ), |
|---|
| 116 | 'type' => 'radio', |
|---|
| 117 | 'size' => 'regular', |
|---|
| 118 | 'std' => 'before', |
|---|
| 119 | 'options' => array( |
|---|
| 120 | 'before' => __('Before price', 'erm'), |
|---|
| 121 | 'after' => __('After price', 'erm') |
|---|
| 122 | ) |
|---|
| 123 | ), |
|---|
| 124 | /*'erm_show_dashboard_menu_items' => array( |
|---|
| 125 | 'name' => __( 'Show Menu Items', 'erm' ), |
|---|
| 126 | 'desc' => __( 'Display the Dashboard MENU for post type "Menu Items"' , 'erm' ), |
|---|
| 127 | 'type' => 'checkbox', |
|---|
| 128 | 'size' => '', |
|---|
| 129 | 'std' => '' |
|---|
| 130 | ),*/ |
|---|
| 131 | 'erm_menu_thumb_size' => array( |
|---|
| 132 | 'name' => __( 'Menu items thumbnail size', 'erm' ), |
|---|
| 133 | 'desc' => __( '' , 'erm' ), |
|---|
| 134 | 'type' => 'select_size', |
|---|
| 135 | 'size' => 'regular', |
|---|
| 136 | 'std' => '' |
|---|
| 137 | ), |
|---|
| 138 | 'erm_custom_css' => array( |
|---|
| 139 | 'name' => __( 'Custom CSS', 'erm' ), |
|---|
| 140 | 'desc' => __( '' , 'erm' ), |
|---|
| 141 | 'type' => 'textarea', |
|---|
| 142 | 'size' => 'large', |
|---|
| 143 | 'std' => '' |
|---|
| 144 | ), |
|---|
| 145 | 'erm_custom_css_display' => array( |
|---|
| 146 | 'name' => __( 'Insert Custom CSS', 'erm' ), |
|---|
| 147 | 'desc' => __( 'Inject the CSS in the Front End' , 'erm' ), |
|---|
| 148 | 'type' => 'checkbox', |
|---|
| 149 | 'size' => '', |
|---|
| 150 | 'std' => '' |
|---|
| 151 | ) |
|---|
| 152 | ) |
|---|
| 153 | ) |
|---|
| 154 | ); |
|---|
| 155 | |
|---|
| 156 | return apply_filters( 'erm_registered_settings', $list ); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | public function get( $key , $default = false ) { |
|---|
| 160 | return empty( $this->options[$key] ) ? $default : $this->options[$key]; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | public function delete( $key ) |
|---|
| 164 | { |
|---|
| 165 | if (isset($this->options[$key]) ) { unset($this->options[$key]); |
|---|
| 166 | } |
|---|
| 167 | $options = get_option(ERM_SETTINGS); |
|---|
| 168 | unset($options[$key]); |
|---|
| 169 | update_option(ERM_SETTINGS, $options); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | public function get_all( $key ) { |
|---|
| 173 | return $this->options; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | public function sanitize( $input ) { |
|---|
| 177 | |
|---|
| 178 | if ( empty( $_POST['_wp_http_referer'] ) ) { |
|---|
| 179 | return $input; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | // Get tab & section |
|---|
| 183 | parse_str( sanitize_text_field($_POST['_wp_http_referer']), $referrer ); |
|---|
| 184 | |
|---|
| 185 | $saved = get_option( $this->set_name, array() ); |
|---|
| 186 | if( ! is_array( $saved ) ) { |
|---|
| 187 | $saved = array(); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | // Get list of settings |
|---|
| 191 | $settings = $this->list_of_settings(); |
|---|
| 192 | $tab = isset( $referrer['tab'] ) ? $referrer['tab'] : 'general'; // TAB, First key by default |
|---|
| 193 | $section = isset( $referrer['section'] ) ? $referrer['tab'] : ''; // SECTION |
|---|
| 194 | |
|---|
| 195 | $input = $input ? $input : array(); |
|---|
| 196 | |
|---|
| 197 | // Sanitize tab section |
|---|
| 198 | $input = apply_filters( 'erm_settings_' . $tab . $section . '_sanitize', $input ); |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | // Ensure checkbox is passed |
|---|
| 202 | if( !empty($settings[$tab]) ) { |
|---|
| 203 | |
|---|
| 204 | // Has sections inside tab |
|---|
| 205 | if ( preg_match('/.+_$/', $tab ) ) { |
|---|
| 206 | $comprobar = $settings[$tab][$section]; |
|---|
| 207 | } |
|---|
| 208 | // No sections inside tab |
|---|
| 209 | else { |
|---|
| 210 | $comprobar = $settings[ $tab ]; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | foreach ( $comprobar as $key => $setting ) { |
|---|
| 214 | // Single checkbox |
|---|
| 215 | if ( isset( $settings[ $tab ][ $key ][ 'type' ] ) && 'checkbox' == $settings[ $tab ][ $key ][ 'type' ] ) { |
|---|
| 216 | $input[ $key ] = ! empty( $input[ $key ] ); |
|---|
| 217 | } |
|---|
| 218 | // Multicheck list |
|---|
| 219 | if ( isset( $settings[ $tab ][ $key ][ 'type' ] ) && 'multicheck' == $settings[ $tab ][ $key ][ 'type' ] ) { |
|---|
| 220 | if( empty( $input[ $key ] ) ) { |
|---|
| 221 | $input[ $key ] = array(); |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | // Loop each input to be saved and sanitize |
|---|
| 229 | foreach( $input as $key => $value ) { |
|---|
| 230 | |
|---|
| 231 | // With sections inside tab |
|---|
| 232 | if ( preg_match('/.+_$/', $tab ) ) { |
|---|
| 233 | $type = isset( $settings[$tab][$section][$key]['type'] ) ? $settings[$tab][$section][$key]['type'] : false; |
|---|
| 234 | } |
|---|
| 235 | // No sections inside tab |
|---|
| 236 | else { |
|---|
| 237 | $type = isset( $settings[$tab][$key]['type'] ) ? $settings[$tab][$key]['type'] : false; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | // Specific sanitize. Ex. erm_settings_sanitize_textarea |
|---|
| 241 | $input[$key] = apply_filters( ERM_SETTINGS.'_sanitize_'.$type , $value, $key ); |
|---|
| 242 | |
|---|
| 243 | // General sanitize |
|---|
| 244 | $input[$key] = apply_filters( ERM_SETTINGS.'_sanitize' , $value, $key ); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | add_settings_error( 'erm-notices', '', __( 'Settings updated.', 'erm' ), 'updated' ); |
|---|
| 248 | |
|---|
| 249 | return array_merge( $saved, $input ); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | // Show fields, depends on type |
|---|
| 253 | //------------------------------------- |
|---|
| 254 | |
|---|
| 255 | /** |
|---|
| 256 | * Not found callback function |
|---|
| 257 | * |
|---|
| 258 | * @since 1.0 |
|---|
| 259 | * @param $args |
|---|
| 260 | */ |
|---|
| 261 | public function show_missing( $args ) { |
|---|
| 262 | |
|---|
| 263 | printf( __( 'The callback function for setting <strong>%s</strong> is missing.', 'erm' ), $args['id'] ); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | public function show_esc_label($name, $desc) |
|---|
| 267 | { |
|---|
| 268 | echo '<label for="'.esc_attr($name).'"> ' . esc_html($desc). '</label>'; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | /** |
|---|
| 272 | * Checkbox field |
|---|
| 273 | * |
|---|
| 274 | * @since 1.0 |
|---|
| 275 | * @param $args |
|---|
| 276 | */ |
|---|
| 277 | public function show_checkbox( $args ) |
|---|
| 278 | { |
|---|
| 279 | $checked = isset($this->options[$args['id']]) ? checked(1, $this->options[$args['id']], false) : ''; |
|---|
| 280 | $name = "{$this->set_name}[{$args['id']}]"; |
|---|
| 281 | $desc = $args['desc']; |
|---|
| 282 | |
|---|
| 283 | echo '<input type="checkbox" id="'.esc_attr($name).'" name="'.esc_attr($name).'" value="1" ' . esc_attr($checked) . '/>'; |
|---|
| 284 | $this->show_esc_label($name, $desc); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | /** |
|---|
| 288 | * Show text field |
|---|
| 289 | * |
|---|
| 290 | * @since 1.0 |
|---|
| 291 | * @param $args |
|---|
| 292 | */ |
|---|
| 293 | public function show_text( $args ) { |
|---|
| 294 | |
|---|
| 295 | if (isset($this->options[ $args['id'] ]) ) { |
|---|
| 296 | $value = $this->options[$args['id']]; |
|---|
| 297 | } else { |
|---|
| 298 | $value = isset($args['std']) ? $args['std'] : ''; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | $size = ( isset($args['size']) && ! is_null($args['size']) ) ? $args['size'] : 'regular'; |
|---|
| 302 | $name = "{$this->set_name}[{$args['id']}]"; |
|---|
| 303 | $desc = $args['desc']; |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | echo '<input type="text" class="' . esc_attr($size) . '-text" id="'.esc_attr($name).'" name="'.esc_attr($name).'" value="' . esc_attr(stripslashes($value)) . '"/>'; |
|---|
| 307 | $this->show_esc_label($name, $desc); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | /** |
|---|
| 311 | * Show textarea |
|---|
| 312 | * |
|---|
| 313 | * @since 1.0 |
|---|
| 314 | * @param $args |
|---|
| 315 | */ |
|---|
| 316 | public function show_textarea( $args ) { |
|---|
| 317 | |
|---|
| 318 | if (isset($this->options[ $args['id'] ]) ) { |
|---|
| 319 | $value = $this->options[$args['id']]; |
|---|
| 320 | } else { |
|---|
| 321 | $value = isset($args['std']) ? $args['std'] : ''; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | $size = ( isset($args['size']) && ! is_null($args['size']) ) ? $args['size'] : 'regular'; |
|---|
| 325 | $name = "{$this->set_name}[{$args['id']}]"; |
|---|
| 326 | $desc = $args['desc']; |
|---|
| 327 | |
|---|
| 328 | echo '<textarea class="'.esc_attr($size).'-text" cols="50" rows="10" id="'.esc_attr($name).'" name="'.esc_attr($name).'">' . |
|---|
| 329 | esc_textarea(stripslashes($value)) . |
|---|
| 330 | '</textarea>'; |
|---|
| 331 | echo '<div class="qrr-settings-desc">'.esc_html($desc).'</div>'; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | /** |
|---|
| 335 | * Radio field |
|---|
| 336 | * |
|---|
| 337 | * @since 1.0 |
|---|
| 338 | * @param $args |
|---|
| 339 | */ |
|---|
| 340 | public function show_radio( $args ) { |
|---|
| 341 | |
|---|
| 342 | foreach( $args['options'] as $key => $value ) { |
|---|
| 343 | $checked = false; |
|---|
| 344 | if (isset($this->options[ $args['id'] ]) && $this->options[ $args['id'] ] == $key ) { |
|---|
| 345 | $checked = true; |
|---|
| 346 | } else if (!isset($this->options[ $args['id'] ]) && isset($args['std']) && $args['std'] == $key ) { |
|---|
| 347 | $checked = true; |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | $name = "{$this->set_name}[{$args['id']}]"; |
|---|
| 351 | $id = "{$this->set_name}[{$args['id']}][{$key}]"; |
|---|
| 352 | $desc = $args['desc']; |
|---|
| 353 | |
|---|
| 354 | echo '<input name="'.esc_attr($name).'" id="'.esc_attr($id).'" type="radio" value="' . esc_attr($key) . '" ' . checked(true, $checked, false) . '/> '; |
|---|
| 355 | echo '<label for="'.esc_attr($id).'">' . esc_html($value) . '</label><br/>'; |
|---|
| 356 | } |
|---|
| 357 | echo '<p class="description">' . esc_html($desc) . '</p>'; |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | /** |
|---|
| 361 | * Select image size field |
|---|
| 362 | * |
|---|
| 363 | * @since 1.0 |
|---|
| 364 | * @param $args |
|---|
| 365 | */ |
|---|
| 366 | public function show_select_size( $args ) |
|---|
| 367 | { |
|---|
| 368 | |
|---|
| 369 | if (isset($this->options[ $args['id'] ]) ) { |
|---|
| 370 | $value = $this->options[$args['id']]; |
|---|
| 371 | } else { |
|---|
| 372 | $value = isset($args['std']) ? $args['std'] : ''; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | $sizes = erm_get_image_sizes(); |
|---|
| 376 | $size = ( isset($args['size']) && ! is_null($args['size']) ) ? $args['size'] : 'regular'; |
|---|
| 377 | $name = "{$this->set_name}[{$args['id']}]"; |
|---|
| 378 | $desc = $args['desc']; |
|---|
| 379 | |
|---|
| 380 | echo '<select name="'.esc_attr($name).'" id="'.esc_attr($name).'" >'; |
|---|
| 381 | foreach($sizes as $key => $dim) { |
|---|
| 382 | $selected = ($key == $value ? 'selected': ''); |
|---|
| 383 | $size = $key.'('.$dim['width'].'x'.$dim['height'].')'; |
|---|
| 384 | echo '<option value="'.esc_attr($key).'" '.esc_attr($selected).'>'.esc_html($size).'</option>'; |
|---|
| 385 | } |
|---|
| 386 | echo '</option>'; |
|---|
| 387 | echo '<p class="description">' . esc_html($desc) . '</p>'; |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | /** |
|---|
| 391 | * Show description |
|---|
| 392 | * |
|---|
| 393 | * @since 1.0 |
|---|
| 394 | * @param $args |
|---|
| 395 | */ |
|---|
| 396 | public function show_desc( $args ) |
|---|
| 397 | { |
|---|
| 398 | |
|---|
| 399 | echo'<p>'.esc_html($args['desc']).'</p>'; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | /** |
|---|
| 403 | * Call a callback function, name has to be in $args['callback'] |
|---|
| 404 | * |
|---|
| 405 | * @since 1.0 |
|---|
| 406 | * @param $args |
|---|
| 407 | */ |
|---|
| 408 | public function show_callback( $args ) |
|---|
| 409 | { |
|---|
| 410 | $func = $args['callback']; |
|---|
| 411 | if (is_callable($func)) { |
|---|
| 412 | call_user_func( $func, $args ); |
|---|
| 413 | } |
|---|
| 414 | } |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | /** |
|---|
| 418 | * Sanitize text field |
|---|
| 419 | * |
|---|
| 420 | * @since 1.0 |
|---|
| 421 | * @param $value |
|---|
| 422 | * @return mixed |
|---|
| 423 | */ |
|---|
| 424 | function erm_settings_sanitize_text( $value ) |
|---|
| 425 | { |
|---|
| 426 | return trim( $value ); |
|---|
| 427 | } |
|---|
| 428 | add_filter('erm_settings_sanitize_text','erm_settings_sanitize_text'); |
|---|
| 429 | |
|---|
| 430 | /** |
|---|
| 431 | * Sanitize textarea field |
|---|
| 432 | * |
|---|
| 433 | * @since 1.0 |
|---|
| 434 | * @param $value |
|---|
| 435 | * @return mixed |
|---|
| 436 | */ |
|---|
| 437 | function erm_settings_sanitize_textarea( $value ) |
|---|
| 438 | { |
|---|
| 439 | return $value; |
|---|
| 440 | } |
|---|
| 441 | add_filter('erm_settings_sanitize_textarea','erm_settings_sanitize_textarea'); |
|---|
| 442 | |
|---|