Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Autoptimize compatibility with Multisite #254

Open
wants to merge 6 commits into
base: master
from
Prev

Check if is_plugin_active_for_network exists.

  • Loading branch information...
Juanfran Granados
Juanfran Granados committed May 17, 2019
commit 6faa9a8313b6a4a970e7628b3f1ff3e4b1f6a9a2
@@ -19,6 +19,15 @@ public function __construct()
{
add_action('init', [$this, 'check_multisite_on_saving_options']);
}
/**
* 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.
@@ -29,6 +38,9 @@ public function __construct()
*/
public static function get_option( $option, $default = false )
{
// Ensure that is_plugin_active_for_network function is declared.
self::maybe_include_plugin_functions();
if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
return get_network_option( get_main_network_id(), $option );
} else {
@@ -49,6 +61,10 @@ public static function get_option( $option, $default = false )
*/
public static function update_option( $option, $value, $autoload = null )
{
// Ensure that is_plugin_active_for_network function is declared.
self::maybe_include_plugin_functions();
if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
return update_network_option( get_main_network_id(), $option, $value );
} else {
@@ -62,13 +78,20 @@ public static function update_option( $option, $value, $autoload = null )
*/
public static function check_multisite_on_saving_options()
{
// Ensure that is_plugin_active_for_network function is declared.
self::maybe_include_plugin_functions();
if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
add_filter( 'pre_update_option', [$this, 'update_autoptimize_option_on_network'], 10, 3 );
}
}
public static function update_autoptimize_option_on_network( $value, $option, $old_value ) {
if( strpos( $option, 'autoptimize_' ) === 0 ) {
// Ensure that is_plugin_active_for_network function is declared.
self::maybe_include_plugin_functions();
if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
update_network_option( get_main_network_id(), $option, $value );
// Return old value, to stop update_option logic.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.