|
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
|
|
|
// This is always a network setting. |
|
38
|
|
|
if ( 'autoptimize_enable_site_config' === $option ) { |
|
39
|
|
|
return get_network_option( get_main_network_id(), $option ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// If the plugin is network activated and our per site setting is not on, use the network configuration. |
|
43
|
|
|
$configuration_per_site = get_network_option( get_main_network_id(), 'autoptimize_enable_site_config' ); |
|
44
|
|
|
if ( self::is_ao_active_for_network() && ( 'on' !== $configuration_per_site || is_network_admin() ) ) { |
|
45
|
|
|
return get_network_option( get_main_network_id(), $option ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return get_option( $option, $default ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Saves the option in standalone and multisite instances. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $option Option name. Expected to not be SQL-escaped. |
|
55
|
|
|
* @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. |
|
56
|
|
|
* @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options, |
|
57
|
|
|
* `$autoload` can only be updated using `update_option()` if `$value` is also changed. |
|
58
|
|
|
* Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options, |
|
59
|
|
|
* the default value is 'yes'. Default null. |
|
60
|
|
|
* @return bool False if value was not updated and true if value was updated. |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function update_option( $option, $value, $autoload = null ) { |
|
63
|
|
|
if ( self::is_ao_active_for_network() && is_network_admin() ) { |
|
64
|
|
|
return update_network_option( get_main_network_id(), $option, $value ); |
|
65
|
|
|
} elseif ( 'autoptimize_enable_site_config' !== $option ) { |
|
66
|
|
|
return update_option( $option, $value, $autoload ); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Use the pre_update_option filter to check if the option to be saved if from autoptimize and |
|
72
|
|
|
* in that case, take care of multisite case. |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function check_multisite_on_saving_options() { |
|
75
|
|
|
if ( self::is_ao_active_for_network() ) { |
|
76
|
|
|
add_filter( 'pre_update_option', 'autoptimizeOptionWrapper::update_autoptimize_option_on_network', 10, 3 ); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The actual magic to differentiate between network options and per-site options. |
|
82
|
|
|
* |
|
83
|
|
|
* @param mixed $value Option value. |
|
84
|
|
|
* @param string $option Option name. |
|
85
|
|
|
* @param string $old_value Old value. |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function update_autoptimize_option_on_network( $value, $option, $old_value ) { |
|
88
|
|
|
if ( strpos( $option, 'autoptimize_' ) === 0 && self::is_options_from_network_admin() ) { |
|
89
|
|
|
if ( self::is_ao_active_for_network() ) { |
|
90
|
|
|
update_network_option( get_main_network_id(), $option, $value ); |
|
91
|
|
|
// Return old value, to stop update_option logic. |
|
92
|
|
|
return $old_value; |
|
93
|
|
|
} |
|
94
|
|
|
if ( apply_filters( 'autoptimize_filter_optionwrapper_wp_cache_delete', false ) ) { |
|
95
|
|
|
// in some (rare) cases options seem to get stuck in WP's Object cache, this should clear it there. |
|
96
|
|
|
wp_cache_delete( $option ); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
return $value; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* As options are POST-ed to wp-admin/options.php checking is_network_admin() does not |
|
104
|
|
|
* work (yet). Instead we compare the network_admin_url with the _wp_http_referer |
|
105
|
|
|
* (which should always be available as part of a hidden form field). |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function is_options_from_network_admin() { |
|
108
|
|
|
static $_really_is_network_admin = null; |
|
109
|
|
|
|
|
110
|
|
|
if ( null === $_really_is_network_admin ) { |
|
111
|
|
|
if ( array_key_exists( '_wp_http_referer', $_POST ) && strpos( network_admin_url( 'settings.php' ), strtok( $_POST['_wp_http_referer'], '?' ) ) !== false ) { |
|
112
|
|
|
$_really_is_network_admin = true; |
|
113
|
|
|
} else { |
|
114
|
|
|
$_really_is_network_admin = false; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $_really_is_network_admin; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Function to check if AO (including beta) is active for network. |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function is_ao_active_for_network() { |
|
125
|
|
|
static $_is_ao_active_for_network = null; |
|
126
|
|
|
if ( null === $_is_ao_active_for_network || defined( 'TEST_MULTISITE_FORCE_AO_ON_NETWORK' ) ) { |
|
127
|
|
|
self::maybe_include_plugin_functions(); |
|
128
|
|
|
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' ) ) { |
|
129
|
|
|
$_is_ao_active_for_network = true; |
|
130
|
|
|
} else { |
|
131
|
|
|
$_is_ao_active_for_network = false; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
return $_is_ao_active_for_network; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
new autoptimizeOptionWrapper(); |
|
138
|
|
|
|