You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: plugins/optimization-detective/docs/hooks.md
+32-16Lines changed: 32 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@
9
9
Fires when the Optimization Detective is initializing.
10
10
11
11
This action is useful for loading extension code that depends on Optimization Detective to be running. The version
12
-
of the plugin is passed as the sole argument so that if the required version is not present, the callback can shortcircuit.
12
+
of the plugin is passed as the sole argument so that if the required version is not present, the callback can short-circuit.
13
13
14
14
Example:
15
15
@@ -115,7 +115,7 @@ It is important to note that this action fires _after_ the entire template has b
115
115
other words, it will fire after the `wp_footer` action.
116
116
117
117
This action runs before any of the registered tag visitors have been invoked in the current response. It is useful for
118
-
an extension to gather the required information from the currently-stored URL Metrics for tag visitors to later leverage.
118
+
an extension to gather the required information from the currentlystored URL Metrics for tag visitors to later leverage.
119
119
See [example](https://github.com/WordPress/performance/pull/1921) from the Image Prioritizer plugin where it can be used
120
120
to determine what the common external LCP background-image is for each viewport group up front so that this doesn't have
121
121
to be computed when a tag visitor is invoked.
@@ -187,32 +187,46 @@ The additional attribution data is made available to client-side extension scrip
187
187
Filters the breakpoint max widths to group URL Metrics for various viewports.
188
188
189
189
Each number 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 are the two breakpoints defined, 480 and 782, then this means there will be three viewport groups of URL Metrics, one for 0\<=480 (i.e. mobile), another 481\<=782 (i.e. phablet/tablet), and another \>782 (i.e. desktop).
190
-
This array may be empty in which case there are no responsive breakpoints and all URL Metrics are collected in a single group.
190
+
This array may be empty, in which case there are no responsive breakpoints, and all URL Metrics are collected in a single group.
191
191
A breakpoint must be greater than zero or else a usage warning will occur.
192
192
193
-
These default breakpoints are reused from Gutenberg which appear to be used the most in media queries that affect frontend styles.
193
+
These default breakpoints are reused from Gutenberg, which appear to be used the most in media queries that affect frontend styles.
194
194
195
195
### Filter: `od_can_optimize_response` (default: boolean condition, see below)
196
196
197
197
Filters whether the current response can be optimized. By default, detection and optimization are only performed when:
198
198
199
199
1. It’s not a search template (`is_search()`).
200
200
2. It’s not a post embed template (`is_embed()`).
201
-
3. It’s not the Customizer preview (`is_customize_preview()`)
202
-
4. It’s not the response to a `POST` request.
203
-
5. 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.
201
+
3. It’s not a preview (`is_preview()`).
202
+
4. It’s not the Customizer preview (`is_customize_preview()`).
203
+
5. It’s not the response to a `POST` request.
204
+
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.
204
205
205
-
To force every response to be optimized regardless of the conditions above, you can do:
206
+
The filter now receives an additional `$disabled_flags` parameter that contains information about which specific conditions are preventing optimization. This allows for more granular control.
207
+
208
+
For example, to enable optimization specifically for search pages:
Filters the sample size for a breakpoint's URL Metrics on a given URL.
214
228
215
-
The filtered value must be greater than zero; otherwise it will be ignored and a usage warning will result.
229
+
The filtered value must be greater than zero; otherwise it will be ignored, and a usage warning will result.
216
230
217
231
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 (along with setting the `od_url_metric_storage_lock_ttl` and `od_url_metric_freshness_ttl` filters below) so that you don't have to keep reloading the page to collect new URL Metrics to flush out stale ones during active development:
218
232
@@ -226,7 +240,7 @@ add_filter( 'od_url_metrics_breakpoint_sample_size', function (): int {
226
240
227
241
Filters how long the current IP is locked from submitting another URL metric storage REST API request.
228
242
229
-
Filtering the TTL to zero will disable any URL Metric storage locking. This is useful, for example, to disable locking when a user is logged-in with code like the following:
243
+
Filtering the TTL to zero will disable any URL Metric storage locking. This is useful, for example, to disable locking when a user is loggedin with code like the following:
230
244
231
245
```php
232
246
add_filter( 'od_metrics_storage_lock_ttl', function ( int $ttl ): int {
@@ -236,7 +250,7 @@ add_filter( 'od_metrics_storage_lock_ttl', function ( int $ttl ): int {
236
250
237
251
By default, the TTL is zero (0) for authorized users and sixty (60) for everyone else. Whether the current user is authorized is determined by whether the user has the `od_store_url_metric_now` capability. This custom capability by default maps to the `manage_options` primitive capability via the `user_has_cap` filter.
238
252
239
-
During development this is useful to set to zero so you can quickly collect new URL Metrics by reloading the page without having to wait for the storage lock to release:
253
+
During development this is useful to set to zero, so you can quickly collect new URL Metrics by reloading the page without having to wait for the storage lock to release:
240
254
241
255
```php
242
256
add_filter( 'od_metrics_storage_lock_ttl', function ( int $ttl ): int {
Filters the minimum allowed viewport aspect ratio for URL Metrics.
276
290
277
-
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:
291
+
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:
278
292
279
293
```php
280
294
add_filter( 'od_minimum_viewport_aspect_ratio', static function (): int {
@@ -298,13 +312,15 @@ add_filter( 'od_maximum_viewport_aspect_ratio', static function (): int {
298
312
299
313
### Filter: `od_template_output_buffer` (default: the HTML response)
300
314
301
-
Filters the template output buffer prior to sending to the client. This filter is added to implement [\#43258](https://core.trac.wordpress.org/ticket/43258) in WordPress core.
315
+
Filters the template output buffer before sending it to the client.
316
+
317
+
This filter is added to implement [\#43258](https://core.trac.wordpress.org/ticket/43258) in WordPress core.
Copy file name to clipboardExpand all lines: plugins/optimization-detective/helper.php
+122-5Lines changed: 122 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,122 @@ function od_generate_media_query( ?int $minimum_viewport_width, ?int $maximum_vi
61
61
}
62
62
63
63
/**
64
-
* Displays the HTML generator meta tag for the Optimization Detective plugin.
64
+
* Gets the reasons why Optimization Detective is disabled for the current response.
65
+
*
66
+
* @since n.e.x.t
67
+
* @access private
68
+
*
69
+
* @return array{
70
+
* is_search?: string,
71
+
* is_embed?: string,
72
+
* is_preview?: string,
73
+
* is_customize_preview?: string,
74
+
* non_get_request?: string,
75
+
* no_cache_purge_post_id?: string,
76
+
* filter_disabled?: string,
77
+
* rest_api_unavailable?: string,
78
+
* query_param_disabled?: string
79
+
* } Array of disabled reason codes and their messages.
80
+
*/
81
+
functionod_get_disabled_reasons(): array {
82
+
$disabled_flags = array(
83
+
'is_search' => false,
84
+
'is_embed' => false,
85
+
'is_preview' => false,
86
+
'is_customize_preview' => false,
87
+
'non_get_request' => false,
88
+
'no_cache_purge_post_id' => false,
89
+
);
90
+
91
+
// Disable the search template since there is no predictability in whether posts in the loop will have featured images assigned or not. If a
92
+
// theme template for search results doesn't even show featured images, then this wouldn't be an issue.
93
+
if ( is_search() ) {
94
+
$disabled_flags['is_search'] = true;
95
+
}
96
+
97
+
// Avoid optimizing embed responses because the Post Embed iframes include a sandbox attribute with the value of
98
+
// "allow-scripts" but without "allow-same-origin". This can result in an error in the console:
99
+
// > Access to script at '.../detect.js?ver=0.4.1' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
100
+
// So it's better to just avoid attempting to optimize Post Embed responses (which don't need optimization anyway).
101
+
if ( is_embed() ) {
102
+
$disabled_flags['is_embed'] = true;
103
+
}
104
+
105
+
// Skip posts that aren't published yet.
106
+
if ( is_preview() ) {
107
+
$disabled_flags['is_preview'] = true;
108
+
}
109
+
110
+
// Disable in Customizer preview since injection of inline-editing controls can interfere with XPath. Optimization is also not necessary in this context.
111
+
if ( is_customize_preview() ) {
112
+
$disabled_flags['is_customize_preview'] = true;
113
+
}
114
+
115
+
// Disable for POST responses since they cannot, by definition, be cached.
// Disable when there is no post ID available for cache purging. Page caching plugins can only reliably be told to invalidate a cached page when a post is available to trigger
$reasons['rest_api_unavailable'] = __( 'Page is not optimized because the REST API for storing URL Metrics is not available.', 'optimization-detective' );
169
+
}
170
+
171
+
if ( isset( $_GET['optimization_detective_disabled'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
172
+
$reasons['query_param_disabled'] = __( 'Page is not optimized because the URL has the optimization_detective_disabled query parameter.', 'optimization-detective' );
173
+
}
174
+
175
+
return$reasons;
176
+
}
177
+
178
+
/**
179
+
* Displays the HTML generator META tag for the Optimization Detective plugin.
65
180
*
66
181
* See {@see 'wp_head'}.
67
182
*
@@ -72,9 +187,11 @@ function od_render_generator_meta_tag(): void {
0 commit comments