Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
if ( did_action( 'muplugins_loaded' ) || ! isset( $_GET['time_plugins_load'] ) ) {
return;
}
Time_Plugins_Load::load();
/**
* Hijacks plugins load process and captures timing information.
*
* Do NOT run this unless you understand what it is doing. Probably not even then.
*/
class Time_Plugins_Load {
protected static $start;
protected static $stop;
protected static $last;
protected static $plugins_loaded;
protected static $results = array();
public static function load() {
add_filter( 'option_active_plugins', array( __CLASS__, 'option_active_plugins' ) );
add_action( 'plugins_loaded', array( __CLASS__, 'plugins_loaded' ), 20 );
add_action( 'wp_footer', array( __CLASS__, 'footer' ) );
add_action( 'admin_footer', array( __CLASS__, 'footer' ) );
}
/**
* @param array $active_plugins
*
* @return array
*/
public static function option_active_plugins( $active_plugins ) {
remove_filter( __FUNCTION__, array( __CLASS__, __FUNCTION__, ) );
$network_plugins = is_multisite() ? wp_get_active_network_plugins() : false;
self::$start = self::$last = microtime( true );
foreach ( $active_plugins as $plugin ) {
if ( ! validate_file( $plugin ) // $plugin must validate as file
&& '.php' == substr( $plugin, - 4 ) // $plugin must end with '.php'
&& file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist
// not already included as a network plugin
&& ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $plugin, $network_plugins ) )
) {
$plugin_path = WP_PLUGIN_DIR . '/' . $plugin;
wp_register_plugin_realpath( $plugin_path );
include_once( $plugin_path );
$current = microtime( true );
self::$results[ $plugin ] = ( $current - self::$last ) * 1000;
self::$last = $current;
}
}
self::$stop = microtime( true );
return array();
}
public static function plugins_loaded() {
self::$plugins_loaded = microtime( true );
}
public static function footer() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
echo '<pre>';
print_r( self::$results );
echo '<br />';
print_r( 'File load total (ms): ' . array_sum( self::$results ) );
echo '<br />';
print_r( 'Start to plugins_loaded end (ms): ' . ( self::$plugins_loaded - self::$start ) * 1000 );
echo '</pre>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.