| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
/** |
| 6 |
* Handles cache configuration and related I/O |
| 7 |
*/ |
| 8 |
|
| 9 |
if (!class_exists('WP_Optimize_Minify_Config')) : |
| 10 |
|
| 11 |
class WP_Optimize_Minify_Config { |
| 12 |
|
| 13 |
static protected $_instance = null; |
| 14 |
|
| 15 |
private $_options = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Construct |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
private function __construct() { |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Getter to see if Minify is enabled |
| 27 |
* |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
public function is_enabled() { |
| 31 |
return $this->get('enabled'); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Removes all Minify settings from the Database if 'preserve_settings_on_uninstall' is false |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function purge() { |
| 40 |
if (!$this->get('preserve_settings_on_uninstall')) { |
| 41 |
if (is_multisite()) { |
| 42 |
delete_site_option('wpo_minify_config'); |
| 43 |
} else { |
| 44 |
delete_option('wpo_minify_config'); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get config from file or cache |
| 51 |
* |
| 52 |
* @param string $option - An option name |
| 53 |
* @param mixed $default - A default for the option |
| 54 |
* @return array|string |
| 55 |
*/ |
| 56 |
public function get($option = null, $default = false) { |
| 57 |
if (empty($this->_options)) { |
| 58 |
if (is_multisite()) { |
| 59 |
$config = get_site_option('wpo_minify_config', array()); |
| 60 |
} else { |
| 61 |
$config = get_option('wpo_minify_config', array()); |
| 62 |
} |
| 63 |
$this->_options = wp_parse_args($config, $this->get_defaults()); |
| 64 |
} |
| 65 |
|
| 66 |
if ($option && isset($this->_options[$option])) { |
| 67 |
return $this->_options[$option]; |
| 68 |
} elseif ($option) { |
| 69 |
return $default; |
| 70 |
} |
| 71 |
return $this->_options; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Update the config |
| 76 |
* |
| 77 |
* @param array $config - The new configuration array |
| 78 |
* @return boolean |
| 79 |
*/ |
| 80 |
public function update($config) { |
| 81 |
$prev_settings = $this->get(); |
| 82 |
$prev_settings = wp_parse_args($prev_settings, $this->get_defaults()); |
| 83 |
$new_settings = wp_parse_args($config, $prev_settings); |
| 84 |
$this->_options = $new_settings; |
| 85 |
|
| 86 |
if (is_multisite()) { |
| 87 |
update_site_option('wpo_minify_config', $new_settings); |
| 88 |
} else { |
| 89 |
update_option('wpo_minify_config', $new_settings); |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Return defaults |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public function get_defaults() { |
| 101 |
$defaults = array( |
| 102 |
// dev tab checkboxes |
| 103 |
'debug' => false, |
| 104 |
'enabled_css_preload' => false, |
| 105 |
'enabled_js_preload' => false, |
| 106 |
'hpreconnect' => '', |
| 107 |
'hpreload' => '', |
| 108 |
'loadcss' => false, |
| 109 |
'remove_css' => false, |
| 110 |
'critical_path_css' => '', |
| 111 |
'critical_path_css_is_front_page' => '', |
| 112 |
// settings tab checkboxes |
| 113 |
'preserve_settings_on_uninstall' => true, |
| 114 |
'disable_when_logged_in' => false, |
| 115 |
'default_protocol' => 'dynamic', // dynamic, http, https |
| 116 |
'html_minification' => true, |
| 117 |
'clean_header_one' => false, |
| 118 |
'emoji_removal' => true, |
| 119 |
'merge_google_fonts' => true, |
| 120 |
'enable_display_swap' => true, |
| 121 |
'remove_googlefonts' => false, |
| 122 |
'gfonts_method' => 'inherit', // inline, async, exclude |
| 123 |
'fawesome_method' => 'inherit', // inline, async, exclude |
| 124 |
'enable_css' => true, |
| 125 |
'enable_css_minification' => true, |
| 126 |
'enable_merging_of_css' => true, |
| 127 |
'remove_print_mediatypes' => false, |
| 128 |
'inline_css' => false, |
| 129 |
'enable_js' => true, |
| 130 |
'enable_js_minification' => true, |
| 131 |
'enable_merging_of_js' => true, |
| 132 |
'enable_defer_js' => 'individual', |
| 133 |
'defer_js_type' => 'defer', |
| 134 |
'defer_jquery' => true, |
| 135 |
'enable_js_trycatch' => false, |
| 136 |
'exclude_defer_login' => true, |
| 137 |
'cdn_url' => '', |
| 138 |
'cdn_force' => false, |
| 139 |
|
| 140 |
'async_css' => '', |
| 141 |
'async_js' => '', |
| 142 |
'disable_css_inline_merge' => true, |
| 143 |
'ualist' => array('x11.*fox\/54', 'oid\s4.*xus.*ome\/62', 'x11.*ome\/62', 'oobot', 'ighth', 'tmetr', 'eadles', 'ingdo'), |
| 144 |
'exclude_js_from_page_speed_tools' => false, |
| 145 |
'exclude_css_from_page_speed_tools' => false, |
| 146 |
'blacklist' => array(), |
| 147 |
'ignore_list' => array(), |
| 148 |
'exclude_js' => '', |
| 149 |
'exclude_css' => '', |
| 150 |
'edit_default_exclutions' => false, |
| 151 |
|
| 152 |
'merge_allowed_urls' => '', |
| 153 |
|
| 154 |
// internal |
| 155 |
'enabled' => false, |
| 156 |
'last-cache-update' => 0, |
| 157 |
'plugin_version' => '0.0.0', |
| 158 |
'cache_lifespan' => 30, |
| 159 |
'merge_inline_extra_css_js' => true, |
| 160 |
); |
| 161 |
return apply_filters('wpo_minify_defaults', $defaults); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Check whether everything should be purged instead of just incremented |
| 166 |
* |
| 167 |
* @return boolean |
| 168 |
*/ |
| 169 |
public function always_purge_everything() { |
| 170 |
/** |
| 171 |
* Filters the result of always_purge_everything |
| 172 |
* |
| 173 |
* @return boolean |
| 174 |
*/ |
| 175 |
return apply_filters('wpo_minify_always_purge_everything', 0 === intval($this->get('cache_lifespan')) || (defined('WPO_ADVANCED_CACHE') && defined('WP_CACHE') && WP_CACHE)); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Get the instance |
| 180 |
* |
| 181 |
* @return WP_Optimize_Minify_Config |
| 182 |
*/ |
| 183 |
public static function get_instance() { |
| 184 |
if (!self::$_instance) { |
| 185 |
self::$_instance = new self(); |
| 186 |
} |
| 187 |
return self::$_instance; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Get WPO Minify instance |
| 193 |
* |
| 194 |
* @return WP_Optimize_Minify_Config |
| 195 |
*/ |
| 196 |
function wp_optimize_minify_config() { |
| 197 |
return WP_Optimize_Minify_Config::get_instance(); |
| 198 |
} |
| 199 |
endif; |
| 200 |
|