Created
January 20, 2015 23:30
-
-
Save ashfame/04f6243f0af27e3aee76 to your computer and use it in GitHub Desktop.
Initial pass at a plugin which reduces the extra number of queries triggered by plugins to look for non-existent options in options table. Use this as a starting point for what you are trying to do.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: AnattaDesign Site Optimizer | |
* Plugin URI: http://anattadesign.com/ | |
* Description: This plugin optimizes the site by reducing the extra number of queries triggered by plugins to look for non-existent options in options table | |
* Version: 0.1 | |
* Author: Anatta Design | |
* Author URI: http://anattadesign.com/ | |
*/ | |
call_user_func( array( new AnattaOptimizer(), | |
'setup' | |
) | |
); | |
class AnattaOptimizer { | |
function setup() { | |
if( is_admin() ) { | |
add_action( 'activated_plugin', array( $this, 'force_this_plugin_to_load_first' ) ); | |
} | |
$options = get_option( 'AD_empty_options' ); | |
if( !$options ) { | |
$options = array( 'erp', | |
'jetpack_id', | |
'linkssc_enable_links_manager', | |
'linkssc_default_css', | |
'wpseo_rss', | |
'wpseo_internallinks', | |
'embed_autourls', | |
'jetpack_holiday_snow_enabled', | |
'woocommerce_informal_localisation_type', | |
'woocommerce_ga_id', | |
'woocommerce_ga_standard_tracking_enabled', | |
'woocommerce_ga_ecommerce_tracking_enabled', | |
'woocommerce_google_analytics_settings', | |
'woocommerce_sharethis', | |
'woocommerce_sharethis_settings', | |
'woocommerce_shareyourcart_settings', | |
'woocommerce_sharedaddy', | |
'woocommerce_sharedaddy_settings' | |
); | |
update_option( 'AD_empty_options', $options ); | |
} | |
foreach( $options as $option ) { | |
add_filter( 'pre_option_' . $option, '__return_empty_string' ); | |
add_action( 'add_option_' . $option, array( $this, 'remove_key_from_empty_options_list' ) ); | |
add_action( 'update_option_' . $option, array( $this, 'updated_option' ) ); | |
add_action( 'deleted_option' . $option, array( $this, 'delete_option' ) ); | |
add_action( 'update_option' . $option, array( $this, 'update_option' ), 10, 3 ); | |
add_action( 'add_option' . $option, array( $this, 'add_option' ), 10, 2 ); | |
} | |
} | |
function updated_option() { | |
$option = current_filter(); | |
if( substr( $option, 0, strlen( 'update_option_' ) ) == 'update_option_' ) { | |
$option = substr( $option, strlen( 'update_option_' ) ); | |
$this->remove_key_from_empty_options_list( $option ); | |
} | |
} | |
function add_option( $option, $value ) { | |
if( '' === $value ) { | |
$this->add_key_to_empty_options_list( $option ); | |
} | |
} | |
function delete_option( $option ) { | |
if( 0 !== strpos( $option, '_' ) ) { | |
$this->add_key_to_empty_options_list( $option ); | |
} | |
} | |
function update_option( $option, $old_value, $value ) { | |
if( '' === $value ) { | |
$this->add_key_to_empty_options_list( $option ); | |
} | |
} | |
function remove_key_from_empty_options_list( $option ) { | |
$options = get_option( 'AD_empty_options' ); | |
if( !is_array( $options ) ) { | |
return; | |
} | |
$options = array_diff( $options, array( $option ) ); | |
update_option( 'AD_empty_options', $options ); | |
} | |
function add_key_to_empty_options_list( $option ) { | |
$options = get_option( 'AD_empty_options' ); | |
if( !is_array( $options ) ) { | |
$options = array(); | |
} | |
$options[] = $option; | |
update_option( 'AD_empty_options', $options ); | |
} | |
function force_this_plugin_to_load_first() { | |
$path = str_replace( WP_PLUGIN_DIR . '/', '', __FILE__ ); | |
if( $plugins = get_option( 'active_plugins' ) ) { | |
if( $key = array_search( $path, $plugins ) ) { | |
array_splice( $plugins, $key, 1 ); | |
array_unshift( $plugins, $path ); | |
update_option( 'active_plugins', $plugins ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment