From 9876aa44911a17fe5cae2843a34f5eb5c15368ef Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 15 Nov 2023 12:03:32 -0800 Subject: [PATCH] Rename 'page metrics' to 'URL metrics' --- .../image-loading-optimization/detection.php | 12 +- .../detection/detect.js | 36 ++--- .../storage/data.php | 148 +++++++++--------- .../storage/lock.php | 26 +-- .../storage/post-type.php | 68 ++++---- .../storage/rest-api.php | 32 ++-- 6 files changed, 161 insertions(+), 161 deletions(-) diff --git a/modules/images/image-loading-optimization/detection.php b/modules/images/image-loading-optimization/detection.php index 90a1ffc461..c54229fc86 100644 --- a/modules/images/image-loading-optimization/detection.php +++ b/modules/images/image-loading-optimization/detection.php @@ -22,12 +22,12 @@ function ilo_print_detection_script() { } $query_vars = ilo_get_normalized_query_vars(); - $slug = ilo_get_page_metrics_slug( $query_vars ); + $slug = ilo_get_url_metrics_slug( $query_vars ); $microtime = microtime( true ); // Abort if we already have all the sample size we need for all breakpoints. $needed_minimum_viewport_widths = ilo_get_needed_minimum_viewport_widths_now_for_slug( $slug ); - if ( ! ilo_needs_page_metric_for_breakpoint( $needed_minimum_viewport_widths ) ) { + if ( ! ilo_needs_url_metric_for_breakpoint( $needed_minimum_viewport_widths ) ) { return; } @@ -50,12 +50,12 @@ function ilo_print_detection_script() { 'serveTime' => $microtime * 1000, // In milliseconds for comparison with `Date.now()` in JavaScript. 'detectionTimeWindow' => $detection_time_window, 'isDebug' => WP_DEBUG, - 'restApiEndpoint' => rest_url( ILO_REST_API_NAMESPACE . ILO_PAGE_METRICS_ROUTE ), + 'restApiEndpoint' => rest_url( ILO_REST_API_NAMESPACE . ILO_URL_METRICS_ROUTE ), 'restApiNonce' => wp_create_nonce( 'wp_rest' ), - 'pageMetricsSlug' => $slug, - 'pageMetricsNonce' => ilo_get_page_metrics_storage_nonce( $slug ), + 'urlMetricsSlug' => $slug, + 'urlMetricsNonce' => ilo_get_url_metrics_storage_nonce( $slug ), 'neededMinimumViewportWidths' => $needed_minimum_viewport_widths, - 'storageLockTTL' => ilo_get_page_metric_storage_lock_ttl(), + 'storageLockTTL' => ilo_get_url_metric_storage_lock_ttl(), ); wp_print_inline_script_tag( sprintf( diff --git a/modules/images/image-loading-optimization/detection/detect.js b/modules/images/image-loading-optimization/detection/detect.js index 96d0587a4b..512832e7f2 100644 --- a/modules/images/image-loading-optimization/detection/detect.js +++ b/modules/images/image-loading-optimization/detection/detect.js @@ -93,7 +93,7 @@ function error( ...message ) { */ /** - * @typedef {Object} PageMetrics + * @typedef {Object} URLMetrics * @property {string} url - URL of the page. * @property {Object} viewport - Viewport. * @property {number} viewport.width - Viewport width. @@ -137,11 +137,11 @@ function getBreadcrumbs( leafElement ) { } /** - * Checks whether the page metric(s) for the provided viewport width is needed. + * Checks whether the URL metric(s) for the provided viewport width is needed. * * @param {number} viewportWidth - Current viewport width. * @param {Array[]} neededMinimumViewportWidths - Needed minimum viewport widths, in ascending order. - * @return {boolean} Whether page metrics are needed. + * @return {boolean} Whether URL metrics are needed. */ function isViewportNeeded( viewportWidth, neededMinimumViewportWidths ) { let lastWasNeeded = false; @@ -176,10 +176,10 @@ function getCurrentTime() { * @param {boolean} args.isDebug Whether to show debug messages. * @param {string} args.restApiEndpoint URL for where to send the detection data. * @param {string} args.restApiNonce Nonce for writing to the REST API. - * @param {string} args.pageMetricsSlug Slug for page metrics. - * @param {string} args.pageMetricsNonce Nonce for page metrics storage. - * @param {Array[]} args.neededMinimumViewportWidths Needed minimum viewport widths for page metrics. - * @param {number} args.storageLockTTL The TTL (in seconds) for the page metric storage lock. + * @param {string} args.urlMetricsSlug Slug for URL metrics. + * @param {string} args.urlMetricsNonce Nonce for URL metrics storage. + * @param {Array[]} args.neededMinimumViewportWidths Needed minimum viewport widths for URL metrics. + * @param {number} args.storageLockTTL The TTL (in seconds) for the URL metric storage lock. */ export default async function detect( { serveTime, @@ -187,15 +187,15 @@ export default async function detect( { isDebug, restApiEndpoint, restApiNonce, - pageMetricsSlug, - pageMetricsNonce, + urlMetricsSlug, + urlMetricsNonce, neededMinimumViewportWidths, storageLockTTL, } ) { const currentTime = getCurrentTime(); // As an alternative to this, the ilo_print_detection_script() function can short-circuit if the - // ilo_is_page_metric_storage_locked() function returns true. However, the downside with that is page caching could + // ilo_is_url_metric_storage_locked() function returns true. However, the downside with that is page caching could // result in metrics being missed being gathered when a user navigates around a site and primes the page cache. if ( isStorageLocked( currentTime, storageLockTTL ) ) { if ( isDebug ) { @@ -227,7 +227,7 @@ export default async function detect( { if ( ! isViewportNeeded( win.innerWidth, neededMinimumViewportWidths ) ) { if ( isDebug ) { - log( 'No need for page metrics from the current viewport.' ); + log( 'No need for URL metrics from the current viewport.' ); } return; } @@ -354,11 +354,11 @@ export default async function detect( { log( 'Detection is stopping.' ); } - /** @type {PageMetrics} */ - const pageMetrics = { + /** @type {URLMetrics} */ + const urlMetrics = { url: win.location.href, - slug: pageMetricsSlug, - nonce: pageMetricsNonce, + slug: urlMetricsSlug, + nonce: urlMetricsNonce, viewport: { width: win.innerWidth, height: win.innerHeight, @@ -396,11 +396,11 @@ export default async function detect( { boundingClientRect: elementIntersection.boundingClientRect, }; - pageMetrics.elements.push( elementMetrics ); + urlMetrics.elements.push( elementMetrics ); } if ( isDebug ) { - log( 'Page metrics:', pageMetrics ); + log( 'URL metrics:', urlMetrics ); } // TODO: Wait until idle? Yield to main? @@ -411,7 +411,7 @@ export default async function detect( { 'Content-Type': 'application/json', 'X-WP-Nonce': restApiNonce, }, - body: JSON.stringify( pageMetrics ), + body: JSON.stringify( urlMetrics ), } ); if ( response.status === 200 ) { diff --git a/modules/images/image-loading-optimization/storage/data.php b/modules/images/image-loading-optimization/storage/data.php index 211d1210f0..731275d3ea 100644 --- a/modules/images/image-loading-optimization/storage/data.php +++ b/modules/images/image-loading-optimization/storage/data.php @@ -11,24 +11,24 @@ } /** - * Gets the freshness age (TTL) for a given page metric. + * Gets the freshness age (TTL) for a given URL metric. * - * When a page metric expires it is eligible to be replaced by a newer one if its viewport lies within the same breakpoint. + * When a URL metric expires it is eligible to be replaced by a newer one if its viewport lies within the same breakpoint. * * @since n.e.x.t * @access private * * @return int Expiration TTL in seconds. */ -function ilo_get_page_metric_freshness_ttl(): int { +function ilo_get_url_metric_freshness_ttl(): int { /** - * Filters the freshness age (TTL) for a given page metric. + * Filters the freshness age (TTL) for a given URL metric. * * @since n.e.x.t * * @param int $ttl Expiration TTL in seconds. */ - return (int) apply_filters( 'ilo_page_metric_freshness_ttl', DAY_IN_SECONDS ); + return (int) apply_filters( 'ilo_url_metric_freshness_ttl', DAY_IN_SECONDS ); } /** @@ -59,7 +59,7 @@ function ilo_can_optimize_response(): bool { /** * Gets the normalized query vars for the current request. * - * This is used as a cache key for stored page metrics. + * This is used as a cache key for stored URL metrics. * * TODO: For non-singular requests, consider adding the post IDs from The Loop to ensure publishing a new post will invalidate the cache. * @@ -86,7 +86,7 @@ function ilo_get_normalized_query_vars(): array { } /** - * Gets slug for page metrics. + * Gets slug for URL metrics. * * @since n.e.x.t * @access private @@ -96,69 +96,69 @@ function ilo_get_normalized_query_vars(): array { * @param array $query_vars Normalized query vars. * @return string Slug. */ -function ilo_get_page_metrics_slug( array $query_vars ): string { +function ilo_get_url_metrics_slug( array $query_vars ): string { return md5( wp_json_encode( $query_vars ) ); } /** - * Computes nonce for storing page metrics for a specific slug. + * Computes nonce for storing URL metrics for a specific slug. * - * This is used in the REST API to authenticate the storage of new page metrics from a given URL. + * This is used in the REST API to authenticate the storage of new URL metrics from a given URL. * * @since n.e.x.t * @access private * * @see wp_create_nonce() - * @see ilo_verify_page_metrics_storage_nonce() + * @see ilo_verify_url_metrics_storage_nonce() * - * @param string $slug Page metrics slug. + * @param string $slug URL metrics slug. * @return string Nonce. */ -function ilo_get_page_metrics_storage_nonce( string $slug ): string { - return wp_create_nonce( "store_page_metrics:{$slug}" ); +function ilo_get_url_metrics_storage_nonce( string $slug ): string { + return wp_create_nonce( "store_url_metrics:{$slug}" ); } /** - * Verifies nonce for storing page metrics for a specific slug. + * Verifies nonce for storing URL metrics for a specific slug. * * @since n.e.x.t * @access private * * @see wp_verify_nonce() - * @see ilo_get_page_metrics_storage_nonce() + * @see ilo_get_url_metrics_storage_nonce() * - * @param string $nonce Page metrics storage nonce. - * @param string $slug Page metrics slug. + * @param string $nonce URL metrics storage nonce. + * @param string $slug URL metrics slug. * @return int 1 if the nonce is valid and generated between 0-12 hours ago, * 2 if the nonce is valid and generated between 12-24 hours ago. * 0 if the nonce is invalid. */ -function ilo_verify_page_metrics_storage_nonce( string $nonce, string $slug ): int { - return (int) wp_verify_nonce( $nonce, "store_page_metrics:{$slug}" ); +function ilo_verify_url_metrics_storage_nonce( string $nonce, string $slug ): int { + return (int) wp_verify_nonce( $nonce, "store_url_metrics:{$slug}" ); } /** - * Unshifts a new page metric onto an array of page metrics. + * Unshifts a new URL metric onto an array of URL metrics. * * @since n.e.x.t * @access private * - * @param array $page_metrics Page metrics. - * @param array $validated_page_metric Validated page metric. See JSON Schema defined in ilo_register_endpoint(). - * @return array Updated page metrics. + * @param array $url_metrics URL metrics. + * @param array $validated_url_metric Validated URL metric. See JSON Schema defined in ilo_register_endpoint(). + * @return array Updated URL metrics. */ -function ilo_unshift_page_metrics( array $page_metrics, array $validated_page_metric ): array { - array_unshift( $page_metrics, $validated_page_metric ); - $breakpoints = ilo_get_breakpoint_max_widths(); - $sample_size = ilo_get_page_metrics_breakpoint_sample_size(); - $grouped_page_metrics = ilo_group_page_metrics_by_breakpoint( $page_metrics, $breakpoints ); +function ilo_unshift_url_metrics( array $url_metrics, array $validated_url_metric ): array { + array_unshift( $url_metrics, $validated_url_metric ); + $breakpoints = ilo_get_breakpoint_max_widths(); + $sample_size = ilo_get_url_metrics_breakpoint_sample_size(); + $grouped_url_metrics = ilo_group_url_metrics_by_breakpoint( $url_metrics, $breakpoints ); - foreach ( $grouped_page_metrics as &$breakpoint_page_metrics ) { - if ( count( $breakpoint_page_metrics ) > $sample_size ) { + foreach ( $grouped_url_metrics as &$breakpoint_url_metrics ) { + if ( count( $breakpoint_url_metrics ) > $sample_size ) { - // Sort page metrics in descending order by timestamp. + // Sort URL metrics in descending order by timestamp. usort( - $breakpoint_page_metrics, + $breakpoint_url_metrics, static function ( $a, $b ) { if ( ! isset( $a['timestamp'] ) || ! isset( $b['timestamp'] ) ) { return 0; @@ -167,15 +167,15 @@ static function ( $a, $b ) { } ); - $breakpoint_page_metrics = array_slice( $breakpoint_page_metrics, 0, $sample_size ); + $breakpoint_url_metrics = array_slice( $breakpoint_url_metrics, 0, $sample_size ); } } - return array_merge( ...$grouped_page_metrics ); + return array_merge( ...$grouped_url_metrics ); } /** - * Gets the breakpoint max widths to group page metrics for various viewports. + * Gets the breakpoint max widths to group URL metrics for various viewports. * * Each max with represents the maximum width (inclusive) for a given breakpoint. So if there is one number, 480, then * this means there will be two viewport groupings, one for 0<=480, and another >480. If instead there were three @@ -198,7 +198,7 @@ static function ( $breakpoint_max_width ) { return (int) $breakpoint_max_width; }, /** - * Filters the breakpoint max widths to group page metrics for various viewports. + * Filters the breakpoint max widths to group URL metrics for various viewports. * * @param int[] $breakpoint_max_widths Max widths for viewport breakpoints. */ @@ -210,42 +210,42 @@ static function ( $breakpoint_max_width ) { } /** - * Gets the sample size for a breakpoint's page metrics on a given URL. + * Gets the sample size for a breakpoint's URL metrics on a given URL. * - * A breakpoint divides page metrics for viewports which are smaller and those which are larger. Given the default + * A breakpoint divides URL metrics for viewports which are smaller and those which are larger. Given the default * sample size of 3 and there being just a single breakpoint (480) by default, for a given URL, there would be a maximum - * total of 6 page metrics stored for a given URL: 3 for mobile and 3 for desktop. + * total of 6 URL metrics stored for a given URL: 3 for mobile and 3 for desktop. * * @since n.e.x.t * @access private * * @return int Sample size. */ -function ilo_get_page_metrics_breakpoint_sample_size(): int { +function ilo_get_url_metrics_breakpoint_sample_size(): int { /** - * Filters the sample size for a breakpoint's page metrics on a given URL. + * Filters the sample size for a breakpoint's URL metrics on a given URL. * * @since n.e.x.t * * @param int $sample_size Sample size. */ - return (int) apply_filters( 'ilo_page_metrics_breakpoint_sample_size', 3 ); + return (int) apply_filters( 'ilo_url_metrics_breakpoint_sample_size', 3 ); } /** - * Groups page metrics by breakpoint. + * Groups URL metrics by breakpoint. * * @since n.e.x.t * @access private * - * @param array $page_metrics Page metrics. + * @param array $url_metrics URL metrics. * @param int[] $breakpoints Viewport breakpoint max widths, sorted in ascending order. - * @return array Page metrics grouped by breakpoint. The array keys are the minimum widths for a viewport to lie within + * @return array URL metrics grouped by breakpoint. The array keys are the minimum widths for a viewport to lie within * the breakpoint. The returned array is always one larger than the provided array of breakpoints, since * the breakpoints reflect the max inclusive boundaries whereas the return value is the groups of page * metrics with viewports on either side of the breakpoint boundaries. */ -function ilo_group_page_metrics_by_breakpoint( array $page_metrics, array $breakpoints ): array { +function ilo_group_url_metrics_by_breakpoint( array $url_metrics, array $breakpoints ): array { // Convert breakpoint max widths into viewport minimum widths. $viewport_minimum_widths = array_map( @@ -257,11 +257,11 @@ static function ( $breakpoint ) { $grouped = array_fill_keys( array_merge( array( 0 ), $viewport_minimum_widths ), array() ); - foreach ( $page_metrics as $page_metric ) { - if ( ! isset( $page_metric['viewport']['width'] ) ) { + foreach ( $url_metrics as $url_metric ) { + if ( ! isset( $url_metric['viewport']['width'] ) ) { continue; } - $viewport_width = $page_metric['viewport']['width']; + $viewport_width = $url_metric['viewport']['width']; $current_minimum_viewport = 0; foreach ( $viewport_minimum_widths as $viewport_minimum_width ) { @@ -272,7 +272,7 @@ static function ( $breakpoint ) { } } - $grouped[ $current_minimum_viewport ][] = $page_metric; + $grouped[ $current_minimum_viewport ][] = $url_metric; } return $grouped; } @@ -283,31 +283,31 @@ static function ( $breakpoint ) { * @since n.e.x.t * @access private * - * @param array $page_metrics Page metrics. + * @param array $url_metrics URL metrics. * @param float $current_time Current time as returned by microtime(true). * @param int[] $breakpoint_max_widths Breakpoint max widths. * @param int $sample_size Sample size for viewports in a breakpoint. - * @param int $freshness_ttl Freshness TTL for a page metric. - * @return array Array of tuples mapping minimum viewport width to whether page metric(s) are needed. + * @param int $freshness_ttl Freshness TTL for a URL metric. + * @return array Array of tuples mapping minimum viewport width to whether URL metric(s) are needed. */ -function ilo_get_needed_minimum_viewport_widths( array $page_metrics, float $current_time, array $breakpoint_max_widths, int $sample_size, int $freshness_ttl ): array { - $metrics_by_breakpoint = ilo_group_page_metrics_by_breakpoint( $page_metrics, $breakpoint_max_widths ); +function ilo_get_needed_minimum_viewport_widths( array $url_metrics, float $current_time, array $breakpoint_max_widths, int $sample_size, int $freshness_ttl ): array { + $metrics_by_breakpoint = ilo_group_url_metrics_by_breakpoint( $url_metrics, $breakpoint_max_widths ); $needed_minimum_viewport_widths = array(); - foreach ( $metrics_by_breakpoint as $minimum_viewport_width => $viewport_page_metrics ) { - $needs_page_metrics = false; - if ( count( $viewport_page_metrics ) < $sample_size ) { - $needs_page_metrics = true; + foreach ( $metrics_by_breakpoint as $minimum_viewport_width => $viewport_url_metrics ) { + $needs_url_metrics = false; + if ( count( $viewport_url_metrics ) < $sample_size ) { + $needs_url_metrics = true; } else { - foreach ( $viewport_page_metrics as $page_metric ) { - if ( isset( $page_metric['timestamp'] ) && $page_metric['timestamp'] + $freshness_ttl < $current_time ) { - $needs_page_metrics = true; + foreach ( $viewport_url_metrics as $url_metric ) { + if ( isset( $url_metric['timestamp'] ) && $url_metric['timestamp'] + $freshness_ttl < $current_time ) { + $needs_url_metrics = true; break; } } } $needed_minimum_viewport_widths[] = array( $minimum_viewport_width, - $needs_page_metrics, + $needs_url_metrics, ); } @@ -324,30 +324,30 @@ function ilo_get_needed_minimum_viewport_widths( array $page_metrics, float $cur * * @see ilo_get_needed_minimum_viewport_widths() * - * @param string $slug Page metrics slug. - * @return array Array of tuples mapping minimum viewport width to whether page metric(s) are needed. + * @param string $slug URL metrics slug. + * @return array Array of tuples mapping minimum viewport width to whether URL metric(s) are needed. */ function ilo_get_needed_minimum_viewport_widths_now_for_slug( string $slug ): array { - $post = ilo_get_page_metrics_post( $slug ); + $post = ilo_get_url_metrics_post( $slug ); return ilo_get_needed_minimum_viewport_widths( - $post instanceof WP_Post ? ilo_parse_stored_page_metrics( $post ) : array(), + $post instanceof WP_Post ? ilo_parse_stored_url_metrics( $post ) : array(), microtime( true ), ilo_get_breakpoint_max_widths(), - ilo_get_page_metrics_breakpoint_sample_size(), - ilo_get_page_metric_freshness_ttl() + ilo_get_url_metrics_breakpoint_sample_size(), + ilo_get_url_metric_freshness_ttl() ); } /** - * Checks whether there is a page metric needed for one of the breakpoints. + * Checks whether there is a URL metric needed for one of the breakpoints. * * @since n.e.x.t * @access private * - * @param array $needed_minimum_viewport_widths Array of tuples mapping minimum viewport width to whether page metric(s) are needed. - * @return bool Whether a page metric is needed. + * @param array $needed_minimum_viewport_widths Array of tuples mapping minimum viewport width to whether URL metric(s) are needed. + * @return bool Whether a URL metric is needed. */ -function ilo_needs_page_metric_for_breakpoint( array $needed_minimum_viewport_widths ): bool { +function ilo_needs_url_metric_for_breakpoint( array $needed_minimum_viewport_widths ): bool { foreach ( $needed_minimum_viewport_widths as list( $minimum_viewport_width, $is_needed ) ) { if ( $is_needed ) { return true; diff --git a/modules/images/image-loading-optimization/storage/lock.php b/modules/images/image-loading-optimization/storage/lock.php index 298689243c..d30fe8d75c 100644 --- a/modules/images/image-loading-optimization/storage/lock.php +++ b/modules/images/image-loading-optimization/storage/lock.php @@ -11,14 +11,14 @@ } /** - * Gets the TTL (in seconds) for the page metric storage lock. + * Gets the TTL (in seconds) for the URL metric storage lock. * * @since n.e.x.t * @access private * * @return int TTL in seconds, greater than or equal to zero. A value of zero means that the storage lock should be disabled and thus that transients must not be used. */ -function ilo_get_page_metric_storage_lock_ttl(): int { +function ilo_get_url_metric_storage_lock_ttl(): int { /** * Filters how long a given IP is locked from submitting another metric-storage REST API request. @@ -39,18 +39,18 @@ function ilo_get_page_metric_storage_lock_ttl(): int { } /** - * Gets transient key for locking page metric storage (for the current IP). + * Gets transient key for locking URL metric storage (for the current IP). * * @todo Should the URL be included in the key? Or should a user only be allowed to store one metric? * @return string Transient key. */ -function ilo_get_page_metric_storage_lock_transient_key(): string { +function ilo_get_url_metric_storage_lock_transient_key(): string { $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR']; - return 'page_metrics_storage_lock_' . wp_hash( $ip_address ); + return 'url_metrics_storage_lock_' . wp_hash( $ip_address ); } /** - * Sets page metric storage lock (for the current IP). + * Sets URL metric storage lock (for the current IP). * * If the storage lock TTL is greater than zero, then a transient is set with the current timestamp and expiring at TTL * seconds. Otherwise, if the current TTL is zero, then any transient is deleted. @@ -58,9 +58,9 @@ function ilo_get_page_metric_storage_lock_transient_key(): string { * @since n.e.x.t * @access private */ -function ilo_set_page_metric_storage_lock() { - $ttl = ilo_get_page_metric_storage_lock_ttl(); - $key = ilo_get_page_metric_storage_lock_transient_key(); +function ilo_set_url_metric_storage_lock() { + $ttl = ilo_get_url_metric_storage_lock_ttl(); + $key = ilo_get_url_metric_storage_lock_transient_key(); if ( 0 === $ttl ) { delete_transient( $key ); } else { @@ -69,19 +69,19 @@ function ilo_set_page_metric_storage_lock() { } /** - * Checks whether page metric storage is locked (for the current IP). + * Checks whether URL metric storage is locked (for the current IP). * * @since n.e.x.t * @access private * * @return bool Whether locked. */ -function ilo_is_page_metric_storage_locked(): bool { - $ttl = ilo_get_page_metric_storage_lock_ttl(); +function ilo_is_url_metric_storage_locked(): bool { + $ttl = ilo_get_url_metric_storage_lock_ttl(); if ( 0 === $ttl ) { return false; } - $locked_time = get_transient( ilo_get_page_metric_storage_lock_transient_key() ); + $locked_time = get_transient( ilo_get_url_metric_storage_lock_transient_key() ); if ( false === $locked_time ) { return false; } diff --git a/modules/images/image-loading-optimization/storage/post-type.php b/modules/images/image-loading-optimization/storage/post-type.php index bb7f4e9b75..a95a71176d 100644 --- a/modules/images/image-loading-optimization/storage/post-type.php +++ b/modules/images/image-loading-optimization/storage/post-type.php @@ -10,23 +10,23 @@ exit; // Exit if accessed directly. } -const ILO_PAGE_METRICS_POST_TYPE = 'ilo_page_metrics'; +const ILO_URL_METRICS_POST_TYPE = 'ilo_url_metrics'; /** - * Registers post type for page metrics storage. + * Registers post type for URL metrics storage. * * This the configuration for this post type is similar to the oembed_cache in core. * * @since n.e.x.t * @access private */ -function ilo_register_page_metrics_post_type() { +function ilo_register_url_metrics_post_type() { register_post_type( - ILO_PAGE_METRICS_POST_TYPE, + ILO_URL_METRICS_POST_TYPE, array( 'labels' => array( - 'name' => __( 'Page Metrics', 'performance-lab' ), - 'singular_name' => __( 'Page Metrics', 'performance-lab' ), + 'name' => __( 'URL Metrics', 'performance-lab' ), + 'singular_name' => __( 'URL Metrics', 'performance-lab' ), ), 'public' => false, 'hierarchical' => false, @@ -38,21 +38,21 @@ function ilo_register_page_metrics_post_type() { ) ); } -add_action( 'init', 'ilo_register_page_metrics_post_type' ); +add_action( 'init', 'ilo_register_url_metrics_post_type' ); /** - * Gets page metrics post. + * Gets URL metrics post. * * @since n.e.x.t * @access private * - * @param string $slug Page metrics slug. + * @param string $slug URL metrics slug. * @return WP_Post|null Post object if exists. */ -function ilo_get_page_metrics_post( string $slug ) { +function ilo_get_url_metrics_post( string $slug ) { $post_query = new WP_Query( array( - 'post_type' => ILO_PAGE_METRICS_POST_TYPE, + 'post_type' => ILO_URL_METRICS_POST_TYPE, 'post_status' => 'publish', 'name' => $slug, 'posts_per_page' => 1, @@ -73,15 +73,15 @@ function ilo_get_page_metrics_post( string $slug ) { } /** - * Parses post content in page metrics post. + * Parses post content in URL metrics post. * * @since n.e.x.t * @access private * - * @param WP_Post $post Page metrics post. - * @return array Page metrics. + * @param WP_Post $post URL metrics post. + * @return array URL metrics. */ -function ilo_parse_stored_page_metrics( WP_Post $post ): array { +function ilo_parse_stored_url_metrics( WP_Post $post ): array { $this_function = __FUNCTION__; $trigger_error = static function ( $error ) use ( $this_function ) { if ( function_exists( 'wp_trigger_error' ) ) { @@ -89,63 +89,63 @@ function ilo_parse_stored_page_metrics( WP_Post $post ): array { } }; - $page_metrics = json_decode( $post->post_content, true ); + $url_metrics = json_decode( $post->post_content, true ); if ( json_last_error() ) { $trigger_error( sprintf( /* translators: 1: Post type slug, 2: JSON error message */ __( 'Contents of %1$s post type not valid JSON: %2$s', 'performance-lab' ), - ILO_PAGE_METRICS_POST_TYPE, + ILO_URL_METRICS_POST_TYPE, json_last_error_msg() ) ); - $page_metrics = array(); - } elseif ( ! is_array( $page_metrics ) ) { + $url_metrics = array(); + } elseif ( ! is_array( $url_metrics ) ) { $trigger_error( sprintf( /* translators: %s is post type slug */ __( 'Contents of %s post type was not a JSON array.', 'performance-lab' ), - ILO_PAGE_METRICS_POST_TYPE + ILO_URL_METRICS_POST_TYPE ) ); - $page_metrics = array(); + $url_metrics = array(); } - return $page_metrics; + return $url_metrics; } /** - * Stores page metric by merging it with the other page metrics for a given URL. + * Stores URL metric by merging it with the other URL metrics for a given URL. * * @since n.e.x.t * @access private * - * @param string $url URL for the page metrics. This is used purely as metadata. - * @param string $slug Page metrics slug (computed from query vars). - * @param array $validated_page_metric Validated page metric. See JSON Schema defined in ilo_register_endpoint(). + * @param string $url URL for the URL metrics. This is used purely as metadata. + * @param string $slug URL metrics slug (computed from query vars). + * @param array $validated_url_metric Validated URL metric. See JSON Schema defined in ilo_register_endpoint(). * @return int|WP_Error Post ID or WP_Error otherwise. */ -function ilo_store_page_metric( string $url, string $slug, array $validated_page_metric ) { - $validated_page_metric['timestamp'] = microtime( true ); +function ilo_store_url_metric( string $url, string $slug, array $validated_url_metric ) { + $validated_url_metric['timestamp'] = microtime( true ); // TODO: What about storing a version identifier? $post_data = array( 'post_title' => $url, // TODO: Should we keep this? It can help with debugging. ); - $post = ilo_get_page_metrics_post( $slug ); + $post = ilo_get_url_metrics_post( $slug ); if ( $post instanceof WP_Post ) { $post_data['ID'] = $post->ID; $post_data['post_name'] = $post->post_name; - $page_metrics = ilo_parse_stored_page_metrics( $post ); + $url_metrics = ilo_parse_stored_url_metrics( $post ); } else { $post_data['post_name'] = $slug; - $page_metrics = array(); + $url_metrics = array(); } - $page_metrics = ilo_unshift_page_metrics( $page_metrics, $validated_page_metric ); + $url_metrics = ilo_unshift_url_metrics( $url_metrics, $validated_url_metric ); - $post_data['post_content'] = wp_json_encode( $page_metrics, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); // TODO: No need for pretty-printing. + $post_data['post_content'] = wp_json_encode( $url_metrics, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); // TODO: No need for pretty-printing. $has_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ); if ( $has_kses ) { @@ -153,7 +153,7 @@ function ilo_store_page_metric( string $url, string $slug, array $validated_page kses_remove_filters(); } - $post_data['post_type'] = ILO_PAGE_METRICS_POST_TYPE; + $post_data['post_type'] = ILO_URL_METRICS_POST_TYPE; $post_data['post_status'] = 'publish'; if ( isset( $post_data['ID'] ) ) { $result = wp_update_post( wp_slash( $post_data ), true ); diff --git a/modules/images/image-loading-optimization/storage/rest-api.php b/modules/images/image-loading-optimization/storage/rest-api.php index a9c68ef84d..423355f344 100644 --- a/modules/images/image-loading-optimization/storage/rest-api.php +++ b/modules/images/image-loading-optimization/storage/rest-api.php @@ -12,10 +12,10 @@ const ILO_REST_API_NAMESPACE = 'image-loading-optimization/v1'; -const ILO_PAGE_METRICS_ROUTE = '/page-metrics'; +const ILO_URL_METRICS_ROUTE = '/url-metrics'; /** - * Registers endpoint for storage of page metric. + * Registers endpoint for storage of URL metric. * * @since n.e.x.t * @access private @@ -39,7 +39,7 @@ function ilo_register_endpoint() { register_rest_route( ILO_REST_API_NAMESPACE, - ILO_PAGE_METRICS_ROUTE, + ILO_URL_METRICS_ROUTE, array( 'methods' => 'POST', 'callback' => static function ( WP_REST_Request $request ) { @@ -47,10 +47,10 @@ function ilo_register_endpoint() { }, 'permission_callback' => static function () { // Needs to be available to unauthenticated visitors. - if ( ilo_is_page_metric_storage_locked() ) { + if ( ilo_is_url_metric_storage_locked() ) { return new WP_Error( - 'page_metric_storage_locked', - __( 'Page metric storage is presently locked for the current IP.', 'performance-lab' ), + 'url_metric_storage_locked', + __( 'URL metric storage is presently locked for the current IP.', 'performance-lab' ), array( 'status' => 403 ) ); } @@ -78,8 +78,8 @@ function ilo_register_endpoint() { 'required' => true, 'pattern' => '^[0-9a-f]+$', 'validate_callback' => static function ( $nonce, WP_REST_Request $request ) { - if ( ! ilo_verify_page_metrics_storage_nonce( $nonce, $request->get_param( 'slug' ) ) ) { - return new WP_Error( 'invalid_nonce', __( 'Page metrics nonce verification failure.', 'performance-lab' ) ); + if ( ! ilo_verify_url_metrics_storage_nonce( $nonce, $request->get_param( 'slug' ) ) ) { + return new WP_Error( 'invalid_nonce', __( 'URL metrics nonce verification failure.', 'performance-lab' ) ); } return true; }, @@ -162,21 +162,21 @@ function ilo_register_endpoint() { */ function ilo_handle_rest_request( WP_REST_Request $request ) { $needed_minimum_viewport_widths = ilo_get_needed_minimum_viewport_widths_now_for_slug( $request->get_param( 'slug' ) ); - if ( ! ilo_needs_page_metric_for_breakpoint( $needed_minimum_viewport_widths ) ) { + if ( ! ilo_needs_url_metric_for_breakpoint( $needed_minimum_viewport_widths ) ) { return new WP_Error( - 'no_page_metric_needed', - __( 'No page metric needed for any of the breakpoints.', 'performance-lab' ), + 'no_url_metric_needed', + __( 'No URL metric needed for any of the breakpoints.', 'performance-lab' ), array( 'status' => 403 ) ); } - ilo_set_page_metric_storage_lock(); - $new_page_metric = wp_array_slice_assoc( $request->get_json_params(), array( 'viewport', 'elements' ) ); + ilo_set_url_metric_storage_lock(); + $new_url_metric = wp_array_slice_assoc( $request->get_json_params(), array( 'viewport', 'elements' ) ); - $result = ilo_store_page_metric( + $result = ilo_store_url_metric( $request->get_param( 'url' ), $request->get_param( 'slug' ), - $new_page_metric + $new_url_metric ); if ( $result instanceof WP_Error ) { @@ -187,7 +187,7 @@ function ilo_handle_rest_request( WP_REST_Request $request ) { array( 'success' => true, 'post_id' => $result, - 'data' => ilo_parse_stored_page_metrics( ilo_get_page_metrics_post( $request->get_param( 'slug' ) ) ), // TODO: Remove this debug data. + 'data' => ilo_parse_stored_url_metrics( ilo_get_url_metrics_post( $request->get_param( 'slug' ) ) ), // TODO: Remove this debug data. ) ); }