/
detection.php
72 lines (67 loc) · 2.88 KB
/
detection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* Detection for Optimization Detective.
*
* @package optimization-detective
* @since 0.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Prints the script for detecting loaded images and the LCP element.
*
* @since 0.1.0
* @access private
*
* @param string $slug URL metrics slug.
* @param OD_URL_Metrics_Group_Collection $group_collection URL metrics group collection.
*/
function od_get_detection_script( string $slug, OD_URL_Metrics_Group_Collection $group_collection ): string {
/**
* Filters the time window between serve time and run time in which loading detection is allowed to run.
*
* This is the allowance of milliseconds between when the page was first generated (and perhaps cached) and when the
* detect function on the page is allowed to perform its detection logic and submit the request to store the results.
* This avoids situations in which there is missing URL Metrics in which case a site with page caching which
* also has a lot of traffic could result in a cache stampede.
*
* @since 0.1.0
* @todo The value should probably be something like the 99th percentile of Time To Last Byte (TTLB) for WordPress sites in CrUX.
*
* @param int $detection_time_window Detection time window in milliseconds.
*/
$detection_time_window = apply_filters( 'od_detection_time_window', 5000 );
$web_vitals_lib_data = require __DIR__ . '/detection/web-vitals.asset.php';
$web_vitals_lib_src = add_query_arg( 'ver', $web_vitals_lib_data['version'], plugin_dir_url( __FILE__ ) . '/detection/web-vitals.js' );
$current_url = od_get_current_url();
$detect_args = array(
'serveTime' => microtime( true ) * 1000, // In milliseconds for comparison with `Date.now()` in JavaScript.
'detectionTimeWindow' => $detection_time_window,
'isDebug' => WP_DEBUG,
'restApiEndpoint' => rest_url( OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE ),
'restApiNonce' => wp_create_nonce( 'wp_rest' ),
'currentUrl' => $current_url,
'urlMetricsSlug' => $slug,
'urlMetricsNonce' => od_get_url_metrics_storage_nonce( $slug, $current_url ),
'urlMetricsGroupStatuses' => array_map(
static function ( OD_URL_Metrics_Group $group ): array {
return array(
'minimumViewportWidth' => $group->get_minimum_viewport_width(),
'complete' => $group->is_complete(),
);
},
iterator_to_array( $group_collection )
),
'storageLockTTL' => OD_Storage_Lock::get_ttl(),
'webVitalsLibrarySrc' => $web_vitals_lib_src,
);
return wp_get_inline_script_tag(
sprintf(
'import detect from %s; detect( %s );',
wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . 'detection/detect.js' ) ),
wp_json_encode( $detect_args )
),
array( 'type' => 'module' )
);
}