Skip to content

Commit f645402

Browse files
committed
Merge branch 'trunk' into fix/wrong-image-size-picture-element
2 parents 928a872 + fad7efc commit f645402

File tree

61 files changed

+1291
-649
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1291
-649
lines changed

package-lock.json

Lines changed: 311 additions & 206 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"devDependencies": {
1414
"@octokit/rest": "^21.1.1",
1515
"@wordpress/env": "^10.18.0",
16-
"@wordpress/prettier-config": "^4.18.0",
17-
"@wordpress/scripts": "^30.11.0",
16+
"@wordpress/prettier-config": "^4.19.0",
17+
"@wordpress/scripts": "^30.12.0",
1818
"commander": "13.1.0",
19-
"copy-webpack-plugin": "^12.0.2",
19+
"copy-webpack-plugin": "^13.0.0",
2020
"css-minimizer-webpack-plugin": "^7.0.0",
2121
"fast-glob": "^3.3.3",
2222
"fs-extra": "^11.3.0",
@@ -25,7 +25,7 @@
2525
"lodash": "4.17.21",
2626
"micromatch": "^4.0.8",
2727
"npm-run-all": "^4.1.5",
28-
"typescript": "^5.7.3",
28+
"typescript": "^5.8.2",
2929
"webpackbar": "^7.0.0"
3030
},
3131
"scripts": {

plugins/embed-optimizer/detect.js

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* when it is submitted for storage.
77
*/
88

9-
const consoleLogPrefix = '[Embed Optimizer]';
9+
export const name = 'Embed Optimizer';
1010

1111
/**
1212
* @typedef {import("../optimization-detective/types.ts").URLMetric} URLMetric
@@ -16,28 +16,9 @@ const consoleLogPrefix = '[Embed Optimizer]';
1616
* @typedef {import("../optimization-detective/types.ts").FinalizeArgs} FinalizeArgs
1717
* @typedef {import("../optimization-detective/types.ts").FinalizeCallback} FinalizeCallback
1818
* @typedef {import("../optimization-detective/types.ts").ExtendedElementData} ExtendedElementData
19+
* @typedef {import("../optimization-detective/types.ts").LogFunction} LogFunction
1920
*/
2021

21-
/**
22-
* Logs a message.
23-
*
24-
* @param {...*} message
25-
*/
26-
function log( ...message ) {
27-
// eslint-disable-next-line no-console
28-
console.log( consoleLogPrefix, ...message );
29-
}
30-
31-
/**
32-
* Logs an error.
33-
*
34-
* @param {...*} message
35-
*/
36-
function error( ...message ) {
37-
// eslint-disable-next-line no-console
38-
console.error( consoleLogPrefix, ...message );
39-
}
40-
4122
/**
4223
* Embed element heights.
4324
*
@@ -51,19 +32,20 @@ const loadedElementContentRects = new Map();
5132
* @type {InitializeCallback}
5233
* @param {InitializeArgs} args Args.
5334
*/
54-
export async function initialize( { isDebug } ) {
35+
export async function initialize( { log: _log } ) {
36+
// eslint-disable-next-line no-console
37+
const log = _log || console.log; // TODO: Remove once Optimization Detective likely updated, or when strict version requirement added in od_init action.
38+
5539
/** @type NodeListOf<HTMLDivElement> */
5640
const embedWrappers = document.querySelectorAll(
5741
'.wp-block-embed > .wp-block-embed__wrapper[data-od-xpath]'
5842
);
5943

6044
for ( /** @type {HTMLElement} */ const embedWrapper of embedWrappers ) {
61-
monitorEmbedWrapperForResizes( embedWrapper, isDebug );
45+
monitorEmbedWrapperForResizes( embedWrapper, log );
6246
}
6347

64-
if ( isDebug ) {
65-
log( 'Loaded embed content rects:', loadedElementContentRects );
66-
}
48+
log( 'Loaded embed content rects:', loadedElementContentRects );
6749
}
6850

6951
/**
@@ -73,24 +55,29 @@ export async function initialize( { isDebug } ) {
7355
* @param {FinalizeArgs} args Args.
7456
*/
7557
export async function finalize( {
76-
isDebug,
58+
log: _log,
59+
error: _error,
7760
getElementData,
7861
extendElementData,
7962
} ) {
63+
/* eslint-disable no-console */
64+
// TODO: Remove once Optimization Detective likely updated, or when strict version requirement added in od_init action.
65+
const log = _log || console.log;
66+
const error = _error || console.error;
67+
/* eslint-enable no-console */
68+
8069
for ( const [ xpath, domRect ] of loadedElementContentRects.entries() ) {
8170
try {
8271
extendElementData( xpath, {
8372
resizedBoundingClientRect: domRect,
8473
} );
85-
if ( isDebug ) {
86-
const elementData = getElementData( xpath );
87-
log(
88-
`boundingClientRect for ${ xpath } resized:`,
89-
elementData.boundingClientRect,
90-
'=>',
91-
domRect
92-
);
93-
}
74+
const elementData = getElementData( xpath );
75+
log(
76+
`boundingClientRect for ${ xpath } resized:`,
77+
elementData.boundingClientRect,
78+
'=>',
79+
domRect
80+
);
9481
} catch ( err ) {
9582
error(
9683
`Failed to extend element data for ${ xpath } with resizedBoundingClientRect:`,
@@ -105,19 +92,17 @@ export async function finalize( {
10592
* Monitors embed wrapper for resizes.
10693
*
10794
* @param {HTMLDivElement} embedWrapper Embed wrapper DIV.
108-
* @param {boolean} isDebug Whether debug.
95+
* @param {LogFunction} log The function to call with log messages.
10996
*/
110-
function monitorEmbedWrapperForResizes( embedWrapper, isDebug ) {
97+
function monitorEmbedWrapperForResizes( embedWrapper, log ) {
11198
if ( ! ( 'odXpath' in embedWrapper.dataset ) ) {
11299
throw new Error( 'Embed wrapper missing data-od-xpath attribute.' );
113100
}
114101
const xpath = embedWrapper.dataset.odXpath;
115102
const observer = new ResizeObserver( ( entries ) => {
116103
const [ entry ] = entries;
117104
loadedElementContentRects.set( xpath, entry.contentRect );
118-
if ( isDebug ) {
119-
log( `Resized element ${ xpath }:`, entry.contentRect );
120-
}
105+
log( `Resized element ${ xpath }:`, entry.contentRect );
121106
} );
122107
observer.observe( embedWrapper, { box: 'content-box' } );
123108
}

plugins/embed-optimizer/hooks.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ function embed_optimizer_update_markup( WP_HTML_Tag_Processor $html_processor, b
187187
$trigger_error = static function ( string $message ) use ( $function_name ): void {
188188
wp_trigger_error( $function_name, esc_html( $message ) );
189189
};
190+
191+
// As of 1.0.0-beta3, next_tag() allows $query and is beginning to migrate to skip tag closers by default.
192+
// In versions prior to this, the method always visited closers and passing a $query actually threw an exception.
193+
$tag_query = version_compare( OPTIMIZATION_DETECTIVE_VERSION, '1.0.0-beta3', '>=' )
194+
? array( 'tag_closers' => 'visit' ) : null;
190195
try {
191196
/*
192197
* Determine how to lazy load the embed.
@@ -253,7 +258,7 @@ function embed_optimizer_update_markup( WP_HTML_Tag_Processor $html_processor, b
253258
}
254259
}
255260
}
256-
} while ( $html_processor->next_tag() );
261+
} while ( $html_processor->next_tag( $tag_query ) );
257262
// If there was only one non-inline script, make it lazy.
258263
if ( 1 === $script_count && ! $has_inline_script && $html_processor->has_bookmark( $bookmark_names['script'] ) ) {
259264
$needs_lazy_script = true;

plugins/embed-optimizer/tests/test-cases/single-twitter-embed-outside-viewport-on-mobile/set-up.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
$sample_size = od_get_url_metrics_breakpoint_sample_size();
1919
for ( $j = 0; $j < $sample_size; $j++ ) {
20-
OD_URL_Metrics_Post_Type::store_url_metric(
20+
$test_case->store_url_metric(
2121
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
2222
$test_case->get_sample_url_metric(
2323
array(

plugins/embed-optimizer/tests/test-cases/single-wordpress-tv-embed-outside-viewport-on-mobile/set-up.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
$elements[0]['isLCP'] = false;
1616
}
1717

18-
OD_URL_Metrics_Post_Type::store_url_metric(
18+
$test_case->store_url_metric(
1919
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
2020
$test_case->get_sample_url_metric(
2121
array(

plugins/embed-optimizer/tests/test-cases/single-youtube-embed-inside-viewport-with-only-mobile-url-metrics/set-up.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
return static function ( Test_Embed_Optimizer_Optimization_Detective $test_case ): void {
3-
OD_URL_Metrics_Post_Type::store_url_metric(
3+
$test_case->store_url_metric(
44
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
55
$test_case->get_sample_url_metric(
66
array(

plugins/embed-optimizer/tests/test-cases/single-youtube-embed-outside-viewport-on-mobile/set-up.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
$sample_size = od_get_url_metrics_breakpoint_sample_size();
1919
for ( $i = 0; $i < $sample_size; $i++ ) {
20-
OD_URL_Metrics_Post_Type::store_url_metric(
20+
$test_case->store_url_metric(
2121
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
2222
$test_case->get_sample_url_metric(
2323
array(

plugins/embed-optimizer/tests/test-cases/single-youtube-embed-outside-viewport-with-only-mobile-url-metrics/set-up.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
return static function ( Test_Embed_Optimizer_Optimization_Detective $test_case ): void {
3-
OD_URL_Metrics_Post_Type::store_url_metric(
3+
$test_case->store_url_metric(
44
od_get_url_metrics_slug( od_get_normalized_query_vars() ),
55
$test_case->get_sample_url_metric(
66
array(

plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ private function process_picture( OD_HTML_Tag_Processor $processor, OD_Tag_Visit
226226
$crossorigin = null;
227227

228228
// Loop through child tags until we reach the closing PICTURE tag.
229-
while ( $processor->next_tag() ) {
229+
// As of 1.0.0-beta3, next_tag() allows $query and is beginning to migrate to skip tag closers by default.
230+
// In versions prior to this, the method always visited closers and passing a $query actually threw an exception.
231+
$tag_query = version_compare( OPTIMIZATION_DETECTIVE_VERSION, '1.0.0-beta3', '>=' )
232+
? array( 'tag_closers' => 'visit' ) : null;
233+
while ( $processor->next_tag( $tag_query ) ) {
230234
$tag = $processor->get_tag();
231235

232236
// If we reached the closing PICTURE tag, break.

0 commit comments

Comments
 (0)