1 parent
7d64b920eb
commit
2ded7a3412
4 files changed
+74
-4
No files matched your search
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
- new format of hook events
|
- new format of hook events
|
||||||
- sidebar tracking
|
- sidebar tracking
|
||||||
- list of callbacks to hook events
|
- list of callbacks to hook events
|
||||||
|
- Server Timing output for Ajax and REST APIs
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- required PHP version to 7.1
|
- required PHP version to 7.1
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
declare( strict_types=1 );
|
||||||
|
|
||||||
|
namespace Rarst\Laps\Manager;
|
||||||
|
|
||||||
|
use Rarst\Laps\Plugin;
|
||||||
|
use Rarst\Laps\Record\Record_Interface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements Server Timing API headers.
|
||||||
|
*/
|
||||||
|
class Server_Timing_Manager {
|
||||||
|
|
||||||
|
/** @var Plugin */
|
||||||
|
private $laps;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Plugin $laps Container instance.
|
||||||
|
*/
|
||||||
|
public function __construct( Plugin $laps ) {
|
||||||
|
|
||||||
|
$this->laps = $laps;
|
||||||
|
|
||||||
|
add_action( 'admin_init', [ $this, 'send_timing_header' ], PHP_INT_MAX );
|
||||||
|
add_action( 'rest_pre_serve_request', [ $this, 'send_timing_header' ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends Server Timing API header with records data.
|
||||||
|
*
|
||||||
|
* @param mixed $input Pass-through input for attaching to a filter hook.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function send_timing_header( $input ) {
|
||||||
|
|
||||||
|
if ( headers_sent() ) {
|
||||||
|
return $input;
|
||||||
|
}
|
||||||
|
|
||||||
|
$ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
|
||||||
|
$rest = defined( 'REST_REQUEST' ) && REST_REQUEST;
|
||||||
|
|
||||||
|
if ( ! $ajax && ! $rest ) {
|
||||||
|
return $input;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This will be false in REST API w/o nonce, even with valid cookie. Might make sense to filter to true in dev.
|
||||||
|
if ( ! apply_filters( 'laps_can_see', current_user_can( 'manage_options' ) ) ) {
|
||||||
|
return $input;
|
||||||
|
}
|
||||||
|
|
||||||
|
$records = $this->laps['records'];
|
||||||
|
$header = '';
|
||||||
|
|
||||||
|
/** @var Record_Interface $record */
|
||||||
|
foreach ( $records as $record ) {
|
||||||
|
$duration = $record->get_duration() * 1000;
|
||||||
|
|
||||||
|
if ( $duration < 1 ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$header .= sprintf( '%s;dur=%d;desc="%s", ', $record->get_category(), $duration, $record->get_name() );
|
||||||
|
}
|
||||||
|
|
||||||
|
header( 'Server-Timing: ' . $header );
|
||||||
|
|
||||||
|
return $input;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -68,10 +68,6 @@ class Plugin extends Container {
|
|||||||
*/
|
*/
|
||||||
public function run(): void {
|
public function run(): void {
|
||||||
|
|
||||||
if ( \defined( 'DOING_AJAX' ) && DOING_AJAX ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ( $this->providers as $provider ) {
|
foreach ( $this->providers as $provider ) {
|
||||||
|
|
||||||
if ( $provider instanceof Bootable_Provider_Interface ) {
|
if ( $provider instanceof Bootable_Provider_Interface ) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Rarst\Laps\Provider;
|
|||||||
|
|
||||||
use Pimple\Container;
|
use Pimple\Container;
|
||||||
use Pimple\ServiceProviderInterface;
|
use Pimple\ServiceProviderInterface;
|
||||||
|
use Rarst\Laps\Manager\Server_Timing_Manager;
|
||||||
use Rarst\Laps\Plugin;
|
use Rarst\Laps\Plugin;
|
||||||
use Rarst\Laps\Manager\Asset_Manager;
|
use Rarst\Laps\Manager\Asset_Manager;
|
||||||
use Rarst\Laps\Manager\Load_Order_Manager;
|
use Rarst\Laps\Manager\Load_Order_Manager;
|
||||||
@@ -25,6 +26,7 @@ class Manager_Provider implements ServiceProviderInterface, Bootable_Provider_In
|
|||||||
new Load_Order_Manager(),
|
new Load_Order_Manager(),
|
||||||
new Asset_Manager(),
|
new Asset_Manager(),
|
||||||
new Toolbar_Manager( $laps ),
|
new Toolbar_Manager( $laps ),
|
||||||
|
new Server_Timing_Manager( $laps ),
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user