Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated code to include a hooks.php file
  • Loading branch information
ashwinparthasarathi committed May 7, 2024
commit 94f3c79be5522cdf8dca2abee38c10dde71e65d1
38 changes: 38 additions & 0 deletions includes/server-timing/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Hook callbacks used for Server Timing.
*
* @package dominant-color-images
*
* @since 1.0.0
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}


/**
* Adds server timing to REST API response.
*
* @param WP_REST_Response|WP_Error $response Result to send to the client. Usually a `WP_REST_Response`.
* @return WP_REST_Response|WP_Error Filtered response.
*/
function rest_post_dispatch_add_server_timing( $response ) {
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
return $response;
}

if ( ! function_exists( 'perflab_server_timing' ) || ! $response instanceof WP_REST_Response ) {
return $response;
}

$server_timing = perflab_server_timing();

do_action( 'perflab_server_timing_send_header' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound

$response->header( 'Server-Timing', $server_timing->get_header() );

return $response;
}
add_filter( 'rest_post_dispatch', 'rest_post_dispatch_add_server_timing' );
28 changes: 2 additions & 26 deletions includes/server-timing/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
define( 'PERFLAB_SERVER_TIMING_SETTING', 'perflab_server_timing_settings' );
define( 'PERFLAB_SERVER_TIMING_SCREEN', 'perflab-server-timing' );

require_once __DIR__ . '/hooks.php';

/**
* Provides access the Server-Timing API.
*
Expand Down Expand Up @@ -223,29 +225,3 @@ static function ( $hook_name ) {

return $value;
}

/**
* Adds server timing to REST API response.
*
* @param WP_REST_Response|WP_Error $response Result to send to the client. Usually a `WP_REST_Response`.
* @return WP_REST_Response|WP_Error Filtered response.
*/
function rest_post_dispatch_add_server_timing( $response ) {
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
return $response;
}

if ( ! function_exists( 'perflab_server_timing' ) || ! $response instanceof WP_REST_Response ) {
return $response;
}

$server_timing = perflab_server_timing();

do_action( 'perflab_server_timing_send_header' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound

$response->header( 'Server-Timing', $server_timing->get_header() );

return $response;
}

add_filter( 'rest_post_dispatch', 'rest_post_dispatch_add_server_timing' );