| 1 |
<?php |
| 2 |
/** |
| 3 |
* Autoptimize options handler. |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* This class takes care of the set and get of option for standalone and multisite WordPress instances. |
| 12 |
*/ |
| 13 |
class autoptimizeOptionWrapper { |
| 14 |
/** |
| 15 |
* Constructor, add filter on saving options. |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Ensure that is_plugin_active_for_network function is declared. |
| 22 |
*/ |
| 23 |
public static function maybe_include_plugin_functions() { |
| 24 |
if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
| 25 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Retrieves the option in standalone and multisite instances. |
| 31 |
* |
| 32 |
* @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
| 33 |
* @param mixed $default Optional. Default value to return if the option does not exist. |
| 34 |
* @return mixed Value set for the option. |
| 35 |
*/ |
| 36 |
public static function get_option( $option, $default = false ) { |
| 37 |
if ( is_multisite() && self::is_ao_active_for_network() ) { |
| 38 |
static $configuration_per_site = null; |
| 39 |
if ( null === $configuration_per_site || defined( 'TEST_MULTISITE_FORCE_AO_ON_NETWORK' ) ) { |
| 40 |
$configuration_per_site = get_network_option( get_main_network_id(), 'autoptimize_enable_site_config', 'on' ); |
| 41 |
if ( null === $configuration_per_site ) { |
| 42 |
// Config per site is off, set as empty string to make sure the var it is not null any more so it can be cached. |
| 43 |
$configuration_per_site = ''; |
| 44 |
} |
| 45 |
} |
| 46 |
} else { |
| 47 |
// Kind of dummy value as when not on multisite or if AO not network enabled, config is always on site-level. |
| 48 |
$configuration_per_site = 'on'; |
| 49 |
} |
| 50 |
|
| 51 |
// This is always a network setting, it is on by default to ensure settings are available at site level unless explicitly turned off. |
| 52 |
if ( 'autoptimize_enable_site_config' === $option ) { |
| 53 |
return $configuration_per_site; |
| 54 |
} |
| 55 |
|
| 56 |
// If the plugin is network activated and our per site setting is not on, use the network configuration. |
| 57 |
if ( is_multisite() && self::is_ao_active_for_network() && ( 'on' !== $configuration_per_site || is_network_admin() ) ) { |
| 58 |
return get_network_option( get_main_network_id(), $option, $default ); |
| 59 |
} |
| 60 |
|
| 61 |
return get_option( $option, $default ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Saves the option in standalone and multisite instances. |
| 66 |
* |
| 67 |
* @param string $option Option name. Expected to not be SQL-escaped. |
| 68 |
* @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. |
| 69 |
* @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options, |
| 70 |
* `$autoload` can only be updated using `update_option()` if `$value` is also changed. |
| 71 |
* Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options, |
| 72 |
* the default value is 'yes'. Default null. |
| 73 |
* @return bool False if value was not updated and true if value was updated. |
| 74 |
*/ |
| 75 |
public static function update_option( $option, $value, $autoload = null ) { |
| 76 |
if ( self::is_ao_active_for_network() && is_network_admin() ) { |
| 77 |
return update_network_option( get_main_network_id(), $option, $value ); |
| 78 |
} elseif ( 'autoptimize_enable_site_config' !== $option ) { |
| 79 |
return update_option( $option, $value, $autoload ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Use the pre_update_option filter to check if the option to be saved if from autoptimize and |
| 85 |
* in that case, take care of multisite case. |
| 86 |
*/ |
| 87 |
public static function check_multisite_on_saving_options() { |
| 88 |
if ( self::is_ao_active_for_network() ) { |
| 89 |
add_filter( 'pre_update_option', 'autoptimizeOptionWrapper::update_autoptimize_option_on_network', 10, 3 ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* The actual magic to differentiate between network options and per-site options. |
| 95 |
* |
| 96 |
* @param mixed $value Option value. |
| 97 |
* @param string $option Option name. |
| 98 |
* @param string $old_value Old value. |
| 99 |
*/ |
| 100 |
public static function update_autoptimize_option_on_network( $value, $option, $old_value ) { |
| 101 |
if ( strpos( $option, 'autoptimize_' ) === 0 && self::is_options_from_network_admin() ) { |
| 102 |
if ( self::is_ao_active_for_network() ) { |
| 103 |
update_network_option( get_main_network_id(), $option, $value ); |
| 104 |
// Return old value, to stop update_option logic. |
| 105 |
return $old_value; |
| 106 |
} |
| 107 |
if ( apply_filters( 'autoptimize_filter_optionwrapper_wp_cache_delete', false ) ) { |
| 108 |
// in some (rare) cases options seem to get stuck in WP's Object cache, this should clear it there. |
| 109 |
wp_cache_delete( $option ); |
| 110 |
} |
| 111 |
} |
| 112 |
return $value; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* As options are POST-ed to wp-admin/options.php checking is_network_admin() does not |
| 117 |
* work (yet). Instead we compare the network_admin_url with the _wp_http_referer |
| 118 |
* (which should always be available as part of a hidden form field). |
| 119 |
*/ |
| 120 |
public static function is_options_from_network_admin() { |
| 121 |
static $_really_is_network_admin = null; |
| 122 |
|
| 123 |
if ( null === $_really_is_network_admin ) { |
| 124 |
if ( array_key_exists( '_wp_http_referer', $_POST ) && strpos( network_admin_url( 'settings.php' ), strtok( $_POST['_wp_http_referer'], '?' ) ) !== false ) { |
| 125 |
$_really_is_network_admin = true; |
| 126 |
} else { |
| 127 |
$_really_is_network_admin = false; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $_really_is_network_admin; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Function to check if AO (including beta) is active for network. |
| 136 |
*/ |
| 137 |
public static function is_ao_active_for_network() { |
| 138 |
static $_is_ao_active_for_network = null; |
| 139 |
if ( null === $_is_ao_active_for_network || defined( 'TEST_MULTISITE_FORCE_AO_ON_NETWORK' ) ) { |
| 140 |
self::maybe_include_plugin_functions(); |
| 141 |
if ( is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) || is_plugin_active_for_network( 'autoptimize-beta/autoptimize.php' ) || defined( 'TEST_MULTISITE_FORCE_AO_ON_NETWORK' ) ) { |
| 142 |
$_is_ao_active_for_network = true; |
| 143 |
} else { |
| 144 |
$_is_ao_active_for_network = false; |
| 145 |
} |
| 146 |
} |
| 147 |
return $_is_ao_active_for_network; |
| 148 |
} |
| 149 |
} |
| 150 |
new autoptimizeOptionWrapper(); |
| 151 |
|