From f289066a8a4ba0a614b42b5b5e821af25fa9f1c2 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Sat, 23 Nov 2024 12:50:32 +0530 Subject: [PATCH 1/9] Set development mode to 'plugin' in dev env --- .wp-env.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.wp-env.json b/.wp-env.json index 2c652e636f..3887c95a09 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -12,6 +12,11 @@ "./plugins/webp-uploads" ], "env": { + "development": { + "config": { + "WP_DEVELOPMENT_MODE": "plugin" + } + }, "tests": { "config": { "FS_METHOD": "direct" From de58a12cbbef2a4bc65ec7a7e59946b5a42a16d5 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Sat, 23 Nov 2024 12:16:02 +0530 Subject: [PATCH 2/9] Add configuration for OD for plugin development mode --- plugins/optimization-detective/optimization.php | 13 ++++++++----- .../storage/class-od-storage-lock.php | 3 ++- plugins/optimization-detective/storage/data.php | 6 ++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/plugins/optimization-detective/optimization.php b/plugins/optimization-detective/optimization.php index 11f4df10a1..9fd32e1ee0 100644 --- a/plugins/optimization-detective/optimization.php +++ b/plugins/optimization-detective/optimization.php @@ -98,6 +98,8 @@ function_exists( 'perflab_server_timing_use_output_buffer' ) * Determines whether the current response can be optimized. * * @since 0.1.0 + * @since n.e.x.t Response is optimized for admin users as well when in 'plugin' development mode. + * * @access private * * @return bool Whether response can be optimized. @@ -116,11 +118,12 @@ function od_can_optimize_response(): bool { is_customize_preview() || // Since the images detected in the response body of a POST request cannot, by definition, be cached. ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] ) || - // The aim is to optimize pages for the majority of site visitors, not those who administer the site. For admin - // users, additional elements will be present like the script from wp_customize_support_script() which will - // interfere with the XPath indices. Note that od_get_normalized_query_vars() is varied by is_user_logged_in() - // so membership sites and e-commerce sites will still be able to be optimized for their normal visitors. - current_user_can( 'customize' ) || + // The aim is to optimize pages for the majority of site visitors, not for those who administer the site, unless + // in 'plugin' development mode. For admin users, additional elements will be present, like the script from + // wp_customize_support_script(), which will interfere with the XPath indices. Note that + // od_get_normalized_query_vars() is varied by is_user_logged_in(), so membership sites and e-commerce sites + // will still be able to be optimized for their normal visitors. + ( current_user_can( 'customize' ) && ! wp_is_development_mode( 'plugin' ) ) || // Page caching plugins can only reliably be told to invalidate a cached page when a post is available to trigger // the relevant actions on. null === od_get_cache_purge_post_id() diff --git a/plugins/optimization-detective/storage/class-od-storage-lock.php b/plugins/optimization-detective/storage/class-od-storage-lock.php index d9c0d85dc8..7344329ffc 100644 --- a/plugins/optimization-detective/storage/class-od-storage-lock.php +++ b/plugins/optimization-detective/storage/class-od-storage-lock.php @@ -23,6 +23,7 @@ final class OD_Storage_Lock { * Gets the TTL (in seconds) for the URL Metric storage lock. * * @since 0.1.0 + * @since n.e.x.t The TTL is zero when in 'plugin' development mode. * @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. @@ -43,7 +44,7 @@ public static function get_ttl(): int { * * @param int $ttl TTL. */ - $ttl = (int) apply_filters( 'od_url_metric_storage_lock_ttl', MINUTE_IN_SECONDS ); + $ttl = (int) apply_filters( 'od_url_metric_storage_lock_ttl', wp_is_development_mode( 'plugin' ) ? 0 : MINUTE_IN_SECONDS ); return max( 0, $ttl ); } diff --git a/plugins/optimization-detective/storage/data.php b/plugins/optimization-detective/storage/data.php index a53569301b..aec3c4a5ad 100644 --- a/plugins/optimization-detective/storage/data.php +++ b/plugins/optimization-detective/storage/data.php @@ -16,6 +16,7 @@ * When a URL Metric expires it is eligible to be replaced by a newer one if its viewport lies within the same breakpoint. * * @since 0.1.0 + * @since n.e.x.t The freshness TTL is set to zero when in 'plugin' development mode. * @access private * * @return int Expiration TTL in seconds. @@ -31,7 +32,7 @@ function od_get_url_metric_freshness_ttl(): int { * * @param int $ttl Expiration TTL in seconds. Defaults to 1 day. */ - $freshness_ttl = (int) apply_filters( 'od_url_metric_freshness_ttl', DAY_IN_SECONDS ); + $freshness_ttl = (int) apply_filters( 'od_url_metric_freshness_ttl', wp_is_development_mode( 'plugin' ) ? 0 : DAY_IN_SECONDS ); if ( $freshness_ttl < 0 ) { _doing_it_wrong( @@ -315,6 +316,7 @@ static function ( $original_breakpoint ) use ( $function_name ): int { * total of 6 URL Metrics stored for a given URL: 3 for mobile and 3 for desktop. * * @since 0.1.0 + * @since n.e.x.t The sample size is set to 1 when in 'plugin' development mode. * @access private * * @return int Sample size. @@ -329,7 +331,7 @@ function od_get_url_metrics_breakpoint_sample_size(): int { * * @param int $sample_size Sample size. Defaults to 3. */ - $sample_size = (int) apply_filters( 'od_url_metrics_breakpoint_sample_size', 3 ); + $sample_size = (int) apply_filters( 'od_url_metrics_breakpoint_sample_size', wp_is_development_mode( 'plugin' ) ? 1 : 3 ); if ( $sample_size <= 0 ) { _doing_it_wrong( From 480553129807d3a2201aec350043f192f978310c Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 25 Nov 2024 16:37:51 -0800 Subject: [PATCH 3/9] Disable min/max aspect ratio when in plugin development mode --- plugins/optimization-detective/storage/data.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/plugins/optimization-detective/storage/data.php b/plugins/optimization-detective/storage/data.php index aec3c4a5ad..8dbea4ffd0 100644 --- a/plugins/optimization-detective/storage/data.php +++ b/plugins/optimization-detective/storage/data.php @@ -186,6 +186,7 @@ function od_verify_url_metrics_storage_hmac( string $hmac, string $slug, string * Gets the minimum allowed viewport aspect ratio for URL Metrics. * * @since 0.6.0 + * @since n.e.x.t The default of 0 is used when in plugin development mode. * @access private * * @return float Minimum viewport aspect ratio for URL Metrics. @@ -194,20 +195,22 @@ function od_get_minimum_viewport_aspect_ratio(): float { /** * Filters the minimum allowed viewport aspect ratio for URL Metrics. * - * The 0.4 default value is intended to accommodate the phone with the greatest known aspect - * ratio at 21:9 when rotated 90 degrees to 9:21 (0.429). + * The 0.4 default value is intended to accommodate the phone with the greatest known aspect ratio at 21:9 when + * rotated 90 degrees to 9:21 (0.429). When in plugin development mode, the default value is 0; this is to account + * for Dev Tools likely being open. * * @since 0.6.0 * * @param float $minimum_viewport_aspect_ratio Minimum viewport aspect ratio. */ - return (float) apply_filters( 'od_minimum_viewport_aspect_ratio', 0.4 ); + return (float) apply_filters( 'od_minimum_viewport_aspect_ratio', wp_is_development_mode( 'plugin' ) ? 0 : 0.4 ); } /** * Gets the maximum allowed viewport aspect ratio for URL Metrics. * * @since 0.6.0 + * @since n.e.x.t The default of PHP_INT_MAX is used when in plugin development mode. * @access private * * @return float Maximum viewport aspect ratio for URL Metrics. @@ -216,14 +219,14 @@ function od_get_maximum_viewport_aspect_ratio(): float { /** * Filters the maximum allowed viewport aspect ratio for URL Metrics. * - * The 2.5 default value is intended to accommodate the phone with the greatest known aspect - * ratio at 21:9 (2.333). + * The 2.5 default value is intended to accommodate the phone with the greatest known aspect ratio at 21:9 (2.333). + * When in plugin development mode, the default value is PHP_INT_MAX; this is to account for Dev Tools likely being open. * * @since 0.6.0 * * @param float $maximum_viewport_aspect_ratio Maximum viewport aspect ratio. */ - return (float) apply_filters( 'od_maximum_viewport_aspect_ratio', 2.5 ); + return (float) apply_filters( 'od_maximum_viewport_aspect_ratio', wp_is_development_mode( 'plugin' ) ? PHP_INT_MAX : 2.5 ); } /** From 5f808bbb77803df47fe96ca0a41263a2312b8a1e Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 25 Nov 2024 16:56:29 -0800 Subject: [PATCH 4/9] Update readme with changes --- plugins/optimization-detective/readme.txt | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/plugins/optimization-detective/readme.txt b/plugins/optimization-detective/readme.txt index a57d968435..cd684b2175 100644 --- a/plugins/optimization-detective/readme.txt +++ b/plugins/optimization-detective/readme.txt @@ -92,9 +92,10 @@ Filters whether the current response can be optimized. By default, detection and 2. It’s not a post embed template (`is_embed()`). 3. It’s not the Customizer preview (`is_customize_preview()`) 4. It’s not the response to a `POST` request. -5. The user is not an administrator (`current_user_can( 'customize' )`). +5. The user is not an administrator (`current_user_can( 'customize' )`), unless you're in plugin development mode (`wp_is_development_mode( 'plugin' )`). +6. There is at least one queried post on the page. This is used to facilitate the purging of page caches after a new URL Metric is stored. -During development, you may want to force this to always be enabled: +To force every response to be optimized regardless of the conditions above, you can do: ` Date: Mon, 25 Nov 2024 17:42:21 -0800 Subject: [PATCH 5/9] Fix spelling mistake --- plugins/optimization-detective/readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/optimization-detective/readme.txt b/plugins/optimization-detective/readme.txt index cd684b2175..757dd55868 100644 --- a/plugins/optimization-detective/readme.txt +++ b/plugins/optimization-detective/readme.txt @@ -104,7 +104,7 @@ add_filter( 'od_can_optimize_response', '__return_true' ); **Filter:** `od_url_metrics_breakpoint_sample_size` (default: 3) -Filters the sample size for a breakpoint's URL Metrics on a given URL. The sample size must be greater than zero. In plugin development mode, the default is 1. You can increase the sample size if you want better guarnatees that the applied optimizations will be accurate: +Filters the sample size for a breakpoint's URL Metrics on a given URL. The sample size must be greater than zero. In plugin development mode, the default is 1. You can increase the sample size if you want better guarantees that the applied optimizations will be accurate: ` Date: Mon, 25 Nov 2024 17:59:35 -0800 Subject: [PATCH 6/9] Remove development mode for all except od_can_optimize_response() --- plugins/optimization-detective/readme.txt | 26 +++++++++++++++---- .../storage/class-od-storage-lock.php | 3 +-- .../optimization-detective/storage/data.php | 21 ++++++--------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/plugins/optimization-detective/readme.txt b/plugins/optimization-detective/readme.txt index 757dd55868..a18cc0d218 100644 --- a/plugins/optimization-detective/readme.txt +++ b/plugins/optimization-detective/readme.txt @@ -104,12 +104,12 @@ add_filter( 'od_can_optimize_response', '__return_true' ); **Filter:** `od_url_metrics_breakpoint_sample_size` (default: 3) -Filters the sample size for a breakpoint's URL Metrics on a given URL. The sample size must be greater than zero. In plugin development mode, the default is 1. You can increase the sample size if you want better guarantees that the applied optimizations will be accurate: +Filters the sample size for a breakpoint's URL Metrics on a given URL. The sample size must be greater than zero. You can increase the sample size if you want better guarantees that the applied optimizations will be accurate. During development, it may be helpful to reduce the sample size to 1: ` Date: Mon, 25 Nov 2024 20:15:20 -0800 Subject: [PATCH 7/9] Use static function in docs Co-authored-by: Mukesh Panchal --- plugins/optimization-detective/readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/optimization-detective/readme.txt b/plugins/optimization-detective/readme.txt index a18cc0d218..5a4a4937e5 100644 --- a/plugins/optimization-detective/readme.txt +++ b/plugins/optimization-detective/readme.txt @@ -167,7 +167,7 @@ During development when you have the DevTools console open on the bottom, for ex ` Date: Tue, 26 Nov 2024 07:58:46 -0800 Subject: [PATCH 8/9] Improve docs Co-authored-by: Felix Arntz --- plugins/optimization-detective/readme.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/optimization-detective/readme.txt b/plugins/optimization-detective/readme.txt index 5a4a4937e5..85a7ff5904 100644 --- a/plugins/optimization-detective/readme.txt +++ b/plugins/optimization-detective/readme.txt @@ -148,7 +148,7 @@ add_filter( 'od_url_metric_freshness_ttl', static function (): int { Filters the minimum allowed viewport aspect ratio for URL Metrics. -The 0.4 value is intended to accommodate the phone with the greatest known aspect ratio at 21:9 when rotated 90 degrees to 9:21 (0.429). During development you may want to set this to 0 to account Dev Tools likely being open and docked to the right: +The 0.4 value is intended to accommodate the phone with the greatest known aspect ratio at 21:9 when rotated 90 degrees to 9:21 (0.429). During development when you have the DevTools console open on the right, the viewport aspect ratio will be smaller than normal. In this case, you may want to set this to 0: ` Date: Tue, 26 Nov 2024 12:31:56 -0800 Subject: [PATCH 9/9] Improve docs for od_url_metric_freshness_ttl --- plugins/optimization-detective/readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/optimization-detective/readme.txt b/plugins/optimization-detective/readme.txt index 85a7ff5904..0d5e0cd246 100644 --- a/plugins/optimization-detective/readme.txt +++ b/plugins/optimization-detective/readme.txt @@ -135,7 +135,7 @@ add_filter( 'od_url_metric_freshness_ttl', static function (): int { } ); ` -During development, this can be useful to set to zero: +During development, this can be useful to set to zero so that you don't have to wait for new URL Metrics to be requested when engineering a new optimization: `