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