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
@@ -58,10 +58,10 @@ public function cronjob()
$stat_array = autoptimizeCache::stats();
$cache_size = round( $stat_array[1] );
if ( ( $cache_size > $max_size ) && ( $do_cache_check ) ) {
update_option( 'autoptimize_cachesize_notice', true );
autoptimizeOption::update_option( 'autoptimize_cachesize_notice', true );
if ( apply_filters( 'autoptimize_filter_cachecheck_sendmail', true ) ) {
$home_url = esc_url( home_url() );
$ao_mailto = apply_filters( 'autoptimize_filter_cachecheck_mailto', get_option( 'admin_email', '' ) );
$ao_mailto = apply_filters( 'autoptimize_filter_cachecheck_mailto', autoptimizeOption::get_option( 'admin_email', '' ) );
$ao_mailsubject = __( 'Autoptimize cache size warning', 'autoptimize' ) . ' (' . $home_url . ')';
$ao_mailbody = __( 'Autoptimize\'s cache size is getting big, consider purging the cache. Have a look at https://wordpress.org/plugins/autoptimize/faq/ to see how you can keep the cache size under control.', 'autoptimize' ) . ' (site: ' . $home_url . ')';
@@ -87,11 +87,11 @@ public function cronjob()
public function show_admin_notice()
{
if ( (bool) get_option( 'autoptimize_cachesize_notice', false ) && current_user_can( 'manage_options' ) ) {
if ( (bool) autoptimizeOption::get_option( 'autoptimize_cachesize_notice', false ) && current_user_can( 'manage_options' ) ) {
echo '<div class="notice notice-warning"><p>';
_e( '<strong>Autoptimize\'s cache size is getting big</strong>, consider purging the cache. Have a look at <a href="https://wordpress.org/plugins/autoptimize/faq/" target="_blank" rel="noopener noreferrer">the Autoptimize FAQ</a> to see how you can keep the cache size under control.', 'autoptimize' );
echo '</p></div>';
update_option( 'autoptimize_cachesize_notice', false );
autoptimizeOption::update_option( 'autoptimize_cachesize_notice', false );
}
// Notice for image proxy usage.

Large diffs are not rendered by default.

@@ -33,7 +33,12 @@ public function __construct( $options = array() )
public function run()
{
if ( is_admin() ) {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
add_action( 'network_admin_menu', array( $this, 'admin_menu' ) );
} else {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
add_action( 'admin_init', array( $this, 'registersettings' ) );
add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_extra_tab' ) );
} else {
$this->run_on_frontend();
@@ -42,7 +47,7 @@ public function run()
public static function fetch_options()
{
$value = get_option( 'autoptimize_extra_settings' );
$value = autoptimizeOption::get_option( 'autoptimize_extra_settings' );
if ( empty( $value ) ) {
// Fallback to returning defaults when no stored option exists yet.
$value = autoptimizeConfig::get_ao_extra_default_options();
@@ -335,8 +340,11 @@ public function admin_menu()
'autoptimize_extra',
array( $this, 'options_page' )
);
register_setting( 'autoptimize_extra_settings', 'autoptimize_extra_settings' );
}
public function registersettings() {
register_setting( 'autoptimize_extra_settings', 'autoptimize_extra_settings' );
}
public function add_extra_tab( $in )
{
@@ -362,13 +370,13 @@ public function options_page()
<div class="wrap">
<h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1>
<?php echo autoptimizeConfig::ao_admin_tabs(); ?>
<?php if ( 'on' !== get_option( 'autoptimize_js' ) && 'on' !== get_option( 'autoptimize_css' ) && 'on' !== get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?>
<?php if ( 'on' !== autoptimizeOption::get_option( 'autoptimize_js' ) && 'on' !== autoptimizeOption::get_option( 'autoptimize_css' ) && 'on' !== autoptimizeOption::get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?>
<div class="notice-warning notice"><p>
<?php _e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?>
</p></div>
<?php } ?>

<form id='ao_settings_form' action='options.php' method='post'>
<form id='ao_settings_form' action='<?php echo admin_url( 'options.php' ); ?>' method='post'>
<?php settings_fields( 'autoptimize_extra_settings' ); ?>
<h2><?php _e( 'Extra Auto-Optimizations', 'autoptimize' ); ?></h2>
<span id='autoptimize_extra_descr'><?php _e( 'The following settings can improve your site\'s performance even more.', 'autoptimize' ); ?></span>
@@ -42,14 +42,14 @@ public function set_options( array $options )
public static function fetch_options()
{
$value = get_option( 'autoptimize_imgopt_settings' );
$value = autoptimizeOption::get_option( 'autoptimize_imgopt_settings' );
if ( empty( $value ) ) {
// Fallback to returning defaults when no stored option exists yet.
$value = autoptimizeConfig::get_ao_imgopt_default_options();
}
// get service availability and add it to the options-array.
$value['availabilities'] = get_option( 'autoptimize_service_availablity' );
$value['availabilities'] = autoptimizeOption::get_option( 'autoptimize_service_availablity' );
if ( empty( $value['availabilities'] ) ) {
$value['availabilities'] = autoptimizeUtils::check_service_availability( true );
@@ -66,7 +66,7 @@ public static function imgopt_active()
static $imgopt_active = null;
if ( null === $imgopt_active ) {
$opts = get_option( 'autoptimize_imgopt_settings', '' );
$opts = autoptimizeOption::get_option( 'autoptimize_imgopt_settings', '' );
if ( ! empty( $opts ) && is_array( $opts ) && array_key_exists( 'autoptimize_imgopt_checkbox_field_1', $opts ) && ! empty( $opts['autoptimize_imgopt_checkbox_field_1'] ) && '1' === $opts['autoptimize_imgopt_checkbox_field_1'] ) {
$imgopt_active = true;
} else {
@@ -97,7 +97,12 @@ public static function instance()
public function run()
{
if ( is_admin() ) {
add_action( 'admin_menu', array( $this, 'imgopt_admin_menu' ) );
if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
add_action( 'network_admin_menu', array( $this, 'imgopt_admin_menu' ) );
} else {
add_action( 'admin_menu', array( $this, 'imgopt_admin_menu' ) );
}
add_action( 'admin_init', array( $this, 'registersettings' ) );
add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_imgopt_tab' ), 9 );
} else {
$this->run_on_frontend();
@@ -302,7 +307,7 @@ private function normalize_img_url( $in )
// get CDN domain once.
static $cdn_domain = null;
if ( is_null( $cdn_domain ) ) {
$cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', get_option( 'autoptimize_cdn_url', '' ) );
$cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', autoptimizeOption::get_option( 'autoptimize_cdn_url', '' ) );
if ( ! empty( $cdn_url ) ) {
$cdn_domain = parse_url( $cdn_url, PHP_URL_HOST );
} else {
@@ -381,7 +386,7 @@ private function can_optimize_image( $url )
if ( null === $cdn_url ) {
$cdn_url = apply_filters(
'autoptimize_filter_base_cdnurl',
get_option( 'autoptimize_cdn_url', '' )
autoptimizeOption::get_option( 'autoptimize_cdn_url', '' )
);
}
@@ -868,8 +873,11 @@ public function imgopt_admin_menu()
'autoptimize_imgopt',
array( $this, 'imgopt_options_page' )
);
register_setting( 'autoptimize_imgopt_settings', 'autoptimize_imgopt_settings' );
}
public function registersettings() {
register_setting( 'autoptimize_imgopt_settings', 'autoptimize_imgopt_settings' );
}
public function add_imgopt_tab( $in )
{
@@ -919,7 +927,7 @@ public function imgopt_options_page()
?>
</p></div>
<?php } ?>
<form id='ao_settings_form' action='options.php' method='post'>
<form id='ao_settings_form' action='<?php echo admin_url( 'options.php' ); ?>' method='post'>
<?php settings_fields( 'autoptimize_imgopt_settings' ); ?>
<h2><?php _e( 'Image optimization', 'autoptimize' ); ?></h2>
<span id='autoptimize_imgopt_descr'><?php _e( 'Make your site significantly faster by just ticking a couple of checkboxes to optimize and lazy load your images, WebP support included!', 'autoptimize' ); ?></span>
@@ -1053,7 +1061,7 @@ public function imgopt_options_page()
public function get_imgopt_status_notice() {
if ( $this->imgopt_active() ) {
$_imgopt_notice = '';
$_stat = get_option( 'autoptimize_imgopt_provider_stat', '' );
$_stat = autoptimizeOption::get_option( 'autoptimize_imgopt_provider_stat', '' );
$_site_host = AUTOPTIMIZE_SITE_DOMAIN;
$_imgopt_upsell = 'https://shortpixel.com/aospai/af/GWRGFLW109483/' . $_site_host;
@@ -1069,7 +1077,7 @@ public function get_imgopt_status_notice() {
'refreshImgProvStats' => '1',
), admin_url( 'options-general.php' ) );
if ( $_stat && array_key_exists( 'timestamp', $_stat ) && ! empty( $_stat['timestamp'] ) ) {
$_imgopt_stats_last_run = __( 'based on status at ', 'autoptimize' ) . date_i18n( get_option( 'time_format' ), $_stat['timestamp'] );
$_imgopt_stats_last_run = __( 'based on status at ', 'autoptimize' ) . date_i18n( autoptimizeOption::get_option( 'time_format' ), $_stat['timestamp'] );
} else {
$_imgopt_stats_last_run = __( 'based on previously fetched data', 'autoptimize' );
}
@@ -1124,7 +1132,7 @@ public function query_img_provider_stats() {
if ( ! is_wp_error( $response ) ) {
if ( '200' == wp_remote_retrieve_response_code( $response ) ) {
$stats = json_decode( wp_remote_retrieve_body( $response ), true );
update_option( 'autoptimize_imgopt_provider_stat', $stats );
autoptimizeOption::update_option( 'autoptimize_imgopt_provider_stat', $stats );
}
}
}
@@ -1150,12 +1158,12 @@ public function launch_ok()
if ( null === $launch_status ) {
$avail_imgopt = $this->options['availabilities']['extra_imgopt'];
$magic_number = intval( substr( md5( parse_url( AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST ) ), 0, 3 ), 16 );
$has_launched = get_option( 'autoptimize_imgopt_launched', '' );
$has_launched = autoptimizeOption::get_option( 'autoptimize_imgopt_launched', '' );
$launch_status = false;
if ( $has_launched || ( is_array( $avail_imgopt ) && array_key_exists( 'launch-threshold', $avail_imgopt ) && $magic_number < $avail_imgopt['launch-threshold'] ) ) {
$launch_status = true;
if ( ! $has_launched ) {
update_option( 'autoptimize_imgopt_launched', 'on' );
autoptimizeOption::update_option( 'autoptimize_imgopt_launched', 'on' );
}
}
}
@@ -1173,7 +1181,7 @@ public function get_imgopt_provider_userstatus() {
static $_provider_userstatus = null;
if ( is_null( $_provider_userstatus ) ) {
$_stat = get_option( 'autoptimize_imgopt_provider_stat', '' );
$_stat = autoptimizeOption::get_option( 'autoptimize_imgopt_provider_stat', '' );
if ( is_array( $_stat ) ) {
if ( array_key_exists( 'Status', $_stat ) ) {
$_provider_userstatus['Status'] = $_stat['Status'];
@@ -81,7 +81,7 @@ public function load_textdomain()
public function setup()
{
// Do we gzip in php when caching or is the webserver doing it?
define( 'AUTOPTIMIZE_CACHE_NOGZIP', (bool) get_option( 'autoptimize_cache_nogzip' ) );
define( 'AUTOPTIMIZE_CACHE_NOGZIP', (bool) autoptimizeOption::get_option( 'autoptimize_cache_nogzip' ) );
// These can be overridden by specifying them in wp-config.php or such.
if ( ! defined( 'AUTOPTIMIZE_WP_CONTENT_NAME' ) ) {
@@ -311,12 +311,12 @@ public function should_buffer( $doing_tests = false )
}
// If setting says not to optimize logged in user and user is logged in...
if ( 'on' !== get_option( 'autoptimize_optimize_logged', 'on' ) && is_user_logged_in() && current_user_can( 'edit_posts' ) ) {
if ( 'on' !== autoptimizeOption::get_option( 'autoptimize_optimize_logged', 'on' ) && is_user_logged_in() && current_user_can( 'edit_posts' ) ) {
$ao_noptimize = true;
}
// If setting says not to optimize cart/checkout.
if ( 'on' !== get_option( 'autoptimize_optimize_checkout', 'on' ) ) {
if ( 'on' !== autoptimizeOption::get_option( 'autoptimize_optimize_checkout', 'on' ) ) {
// Checking for woocommerce, easy digital downloads and wp ecommerce...
foreach ( array( 'is_checkout', 'is_cart', 'edd_is_checkout', 'wpsc_is_cart', 'wpsc_is_checkout' ) as $func ) {
if ( function_exists( $func ) && $func() ) {
@@ -0,0 +1,104 @@
<?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 autoptimizeOption
{
/**
* Constructor, add filter on saving options.
*/
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.
*
* @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 )
{
// 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 {
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 )
{
// 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 {
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()
{
// 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.
return $old_value;
}
}
return $value;
}
}
new autoptimizeOption();
@@ -20,7 +20,11 @@ public function run()
if ( $this->enabled() ) {
add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_partner_tabs' ), 10, 1 );
}
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
if ( is_multisite() && is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) ) {
add_action( 'network_admin_menu', array( $this, 'add_admin_menu' ) );
} else {
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
}
}
protected function enabled()
@@ -314,7 +314,7 @@ public static function check_service_availability( $return_result = false )
if ( '200' == wp_remote_retrieve_response_code( $service_availability_resp ) ) {
$availabilities = json_decode( wp_remote_retrieve_body( $service_availability_resp ), true );
if ( is_array( $availabilities ) ) {
update_option( 'autoptimize_service_availablity', $availabilities );
autoptimizeOption::update_option( 'autoptimize_service_availablity', $availabilities );
if ( $return_result ) {
return $availabilities;
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.