Changed main class to concrete instance.

This commit is contained in:
Andrey Savchenko committed 2018-02-07 22:08:41 +02:00
1 parent 9527ad1508
commit 62ea1f4246
2 files changed
+46 -44

No files matched your search

+3 -1
View File
@@ -15,4 +15,6 @@ if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php';
} }
Rarst\Laps\Laps::on_load(); $laps = new \Rarst\Laps\Laps();
$laps->on_load();
+43 -43
View File
@@ -10,33 +10,33 @@ use Symfony\Component\Stopwatch\Stopwatch;
class Laps { class Laps {
/** @var Stopwatch $stopwatch */ /** @var Stopwatch $stopwatch */
public static $stopwatch; public $stopwatch;
public static $events = array(); public $events = array();
public static $query_starts = array(); public $query_starts = array();
/** /**
* Start Stopwatch and timing plugin load immediately, then set up core events and needed hooks. * Start Stopwatch and timing plugin load immediately, then set up core events and needed hooks.
*/ */
public static function on_load() { public function on_load() {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return; return;
} }
self::$stopwatch = new Stopwatch(); $this->stopwatch = new Stopwatch();
self::$stopwatch->start( 'Plugins Load', 'plugin' ); $this->stopwatch->start( 'Plugins Load', 'plugin' );
$events = new Core_Events(); $events = new Core_Events();
self::add_events( $events->get() ); $this->add_events( $events->get() );
add_action( 'pre_update_option_active_plugins', array( __CLASS__, 'pre_update_option_active_plugins' ) ); add_action( 'pre_update_option_active_plugins', array( $this, 'pre_update_option_active_plugins' ) );
add_action( 'pre_update_site_option_active_sitewide_plugins', array( __CLASS__, 'pre_update_option_active_plugins' ) ); add_action( 'pre_update_site_option_active_sitewide_plugins', array( $this, 'pre_update_option_active_plugins' ) );
add_action( 'pre_http_request', array( __CLASS__, 'pre_http_request' ), 10, 3 ); add_action( 'pre_http_request', array( $this, 'pre_http_request' ), 10, 3 );
add_action( 'http_api_debug', array( __CLASS__, 'http_api_debug' ), 10, 5 ); add_action( 'http_api_debug', array( $this, 'http_api_debug' ), 10, 5 );
add_action( 'after_setup_theme', array( __CLASS__, 'after_setup_theme' ), 15 ); add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ), 15 );
add_action( 'init', array( __CLASS__, 'init' ) ); add_action( 'init', array( $this, 'init' ) );
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
add_filter( 'query', array( __CLASS__, 'query' ), 20 ); add_filter( 'query', array( $this, 'query' ), 20 );
} }
} }
@@ -45,15 +45,15 @@ class Laps {
* *
* @param array $stops * @param array $stops
*/ */
public static function add_events( $stops ) { public function add_events( $stops ) {
self::$events = array_merge( self::$events, $stops ); $this->events = array_merge( $this->events, $stops );
foreach ( $stops as $hook_name => $data ) { foreach ( $stops as $hook_name => $data ) {
foreach ( array_keys( $data ) as $priority ) { foreach ( array_keys( $data ) as $priority ) {
add_action( $hook_name, array( __CLASS__, 'tick' ), $priority ); add_action( $hook_name, array( $this, 'tick' ), $priority );
} }
} }
} }
@@ -65,7 +65,7 @@ class Laps {
* *
* @return array * @return array
*/ */
public static function pre_update_option_active_plugins( $plugins ) { public function pre_update_option_active_plugins( $plugins ) {
$plugin = plugin_basename( dirname( __DIR__ ) . '/laps.php' ); $plugin = plugin_basename( dirname( __DIR__ ) . '/laps.php' );
$key = array_search( $plugin, $plugins ); $key = array_search( $plugin, $plugins );
@@ -87,7 +87,7 @@ class Laps {
* *
* @return mixed * @return mixed
*/ */
public static function tick( $input = null ) { public function tick( $input = null ) {
global $wp_filter; global $wp_filter;
@@ -96,23 +96,23 @@ class Laps {
$priority = $filter_instance instanceof \WP_Hook ? $filter_instance->current_priority() : key( $filter_instance ); $priority = $filter_instance instanceof \WP_Hook ? $filter_instance->current_priority() : key( $filter_instance );
// See https://core.trac.wordpress.org/ticket/41185 on broken priority, but more general sanity check. // See https://core.trac.wordpress.org/ticket/41185 on broken priority, but more general sanity check.
if ( empty( self::$events[ $filter_name ][ $priority ] ) ) { if ( empty( $this->events[ $filter_name ][ $priority ] ) ) {
return $input; return $input;
} }
$event = wp_parse_args( $event = wp_parse_args(
self::$events[ $filter_name ][ $priority ], $this->events[ $filter_name ][ $priority ],
array( array(
'action' => 'start', 'action' => 'start',
'category' => null, 'category' => null,
) )
); );
if ( 'stop' === $event['action'] && ! self::$stopwatch->isStarted( $event['event'] ) ) { if ( 'stop' === $event['action'] && ! $this->stopwatch->isStarted( $event['event'] ) ) {
return $input; return $input;
} }
self::$stopwatch->{$event['action']}( $event['event'], $event['category'] ); $this->stopwatch->{$event['action']}( $event['event'], $event['category'] );
return $input; return $input;
} }
@@ -124,14 +124,14 @@ class Laps {
* *
* @return string * @return string
*/ */
public static function query( $query ) { public function query( $query ) {
global $wpdb; global $wpdb;
if ( empty( self::$query_starts ) && ! empty( $wpdb->queries ) ) { if ( empty( $this->query_starts ) && ! empty( $wpdb->queries ) ) {
self::$query_starts[ count( $wpdb->queries ) ] = microtime( true ) * 1000; $this->query_starts[ count( $wpdb->queries ) ] = microtime( true ) * 1000;
} else { } else {
self::$query_starts[] = microtime( true ) * 1000; $this->query_starts[] = microtime( true ) * 1000;
} }
return $query; return $query;
@@ -146,9 +146,9 @@ class Laps {
* *
* @return boolean * @return boolean
*/ */
public static function pre_http_request( $false, $args, $url ) { public function pre_http_request( $false, $args, $url ) {
self::$stopwatch->start( $url, 'http' ); $this->stopwatch->start( $url, 'http' );
return $false; return $false;
} }
@@ -164,9 +164,9 @@ class Laps {
* *
* @return mixed * @return mixed
*/ */
public static function http_api_debug( $response, $type, $class, $args, $url ) { public function http_api_debug( $response, $type, $class, $args, $url ) {
self::$stopwatch->stop( $url ); $this->stopwatch->stop( $url );
return $response; return $response;
} }
@@ -174,25 +174,25 @@ class Laps {
/** /**
* When theme is done possibly add vendor-specific events. * When theme is done possibly add vendor-specific events.
*/ */
public static function after_setup_theme() { public function after_setup_theme() {
foreach ( array( 'THA', 'Hybrid', 'Genesis', 'Thematic', 'Yoast' ) as $vendor ) { foreach ( array( 'THA', 'Hybrid', 'Genesis', 'Thematic', 'Yoast' ) as $vendor ) {
$class = "Rarst\\Laps\\{$vendor}_Events"; $class = "Rarst\\Laps\\{$vendor}_Events";
/** @var Laps_Events $events */ /** @var Laps_Events $events */
$events = new $class; $events = new $class;
self::add_events( $events->get() ); $this->add_events( $events->get() );
} }
} }
public static function init() { public function init() {
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_bar_menu', array( __CLASS__, 'admin_bar_menu' ), 100 ); add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 100 );
} }
public static function enqueue_scripts() { public function enqueue_scripts() {
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_register_script( 'laps', plugins_url( "js/laps{$suffix}.js", __DIR__ ), array( 'jquery' ), '3.3.1', true ); wp_register_script( 'laps', plugins_url( "js/laps{$suffix}.js", __DIR__ ), array( 'jquery' ), '3.3.1', true );
@@ -209,7 +209,7 @@ class Laps {
* *
* @param \WP_Admin_Bar $wp_admin_bar * @param \WP_Admin_Bar $wp_admin_bar
*/ */
public static function admin_bar_menu( $wp_admin_bar ) { public function admin_bar_menu( $wp_admin_bar ) {
if ( ! apply_filters( 'laps_can_see', current_user_can( 'manage_options' ) ) ) { if ( ! apply_filters( 'laps_can_see', current_user_can( 'manage_options' ) ) ) {
return; return;
@@ -224,11 +224,11 @@ class Laps {
) )
); );
if ( self::$stopwatch->isStarted( 'Toolbar' ) ) { if ( $this->stopwatch->isStarted( 'Toolbar' ) ) {
self::$stopwatch->stop( 'Toolbar' ); $this->stopwatch->stop( 'Toolbar' );
} }
$events = self::$stopwatch->getSectionEvents( '__root__' ); $events = $this->stopwatch->getSectionEvents( '__root__' );
$start = $timestart * 1000; $start = $timestart * 1000;
$end = microtime( true ) * 1000; $end = microtime( true ) * 1000;
$total = $end - $start; $total = $end - $start;
@@ -260,7 +260,7 @@ class Laps {
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
foreach ( $wpdb->queries as $key => $query ) { foreach ( $wpdb->queries as $key => $query ) {
$query_start = isset( self::$query_starts[ $key ] ) ? self::$query_starts[ $key ] : $last_query_end; $query_start = isset( $this->query_starts[ $key ] ) ? $this->query_starts[ $key ] : $last_query_end;
list( $sql, $duration, $trace ) = $query; list( $sql, $duration, $trace ) = $query;
$sql = trim( $sql ); $sql = trim( $sql );
$category = 'query-read'; $category = 'query-read';