| 1 |
<?php |
| 2 |
/** |
| 3 |
* Settings handling. |
| 4 |
* |
| 5 |
* @package XSpeed |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace XSpeed; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
class Settings { |
| 13 |
|
| 14 |
const OPTION_KEY = 'xspeed_options'; |
| 15 |
|
| 16 |
public static function defaults() { |
| 17 |
return array( |
| 18 |
'cache_enabled' => false, |
| 19 |
'minify_html' => false, |
| 20 |
'minify_css' => false, |
| 21 |
'minify_js' => false, |
| 22 |
'gzip_enabled' => false, |
| 23 |
'cache_expiry' => 24, |
| 24 |
'excluded_urls' => array(), |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
public static function get() { |
| 29 |
$saved = get_option( self::OPTION_KEY, array() ); |
| 30 |
return wp_parse_args( $saved, self::defaults() ); |
| 31 |
} |
| 32 |
|
| 33 |
public static function update( array $input ) { |
| 34 |
$current = self::get(); |
| 35 |
$clean = $current; |
| 36 |
|
| 37 |
foreach ( array( 'cache_enabled', 'minify_html', 'minify_css', 'minify_js', 'gzip_enabled' ) as $key ) { |
| 38 |
if ( isset( $input[ $key ] ) ) { |
| 39 |
$clean[ $key ] = (bool) $input[ $key ]; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
if ( isset( $input['cache_expiry'] ) ) { |
| 44 |
$expiry = absint( $input['cache_expiry'] ); |
| 45 |
$clean['cache_expiry'] = max( 1, min( 720, $expiry ) ); |
| 46 |
} |
| 47 |
|
| 48 |
if ( isset( $input['excluded_urls'] ) ) { |
| 49 |
$clean['excluded_urls'] = self::sanitize_urls( $input['excluded_urls'] ); |
| 50 |
} |
| 51 |
|
| 52 |
update_option( self::OPTION_KEY, $clean ); |
| 53 |
return $clean; |
| 54 |
} |
| 55 |
|
| 56 |
public static function set_defaults() { |
| 57 |
if ( false === get_option( self::OPTION_KEY ) ) { |
| 58 |
add_option( self::OPTION_KEY, self::defaults() ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
private static function sanitize_urls( $value ) { |
| 63 |
if ( is_string( $value ) ) { |
| 64 |
$lines = preg_split( '/\r\n|\r|\n/', $value ); |
| 65 |
} elseif ( is_array( $value ) ) { |
| 66 |
$lines = $value; |
| 67 |
} else { |
| 68 |
return array(); |
| 69 |
} |
| 70 |
|
| 71 |
$clean = array(); |
| 72 |
foreach ( $lines as $line ) { |
| 73 |
$line = trim( sanitize_text_field( $line ) ); |
| 74 |
if ( '' === $line ) { |
| 75 |
continue; |
| 76 |
} |
| 77 |
$clean[] = $line; |
| 78 |
} |
| 79 |
return array_values( array_unique( $clean ) ); |
| 80 |
} |
| 81 |
} |
| 82 |
|