Skip to content

Commit 4298d32

Browse files
committed
Update od_register_tag_visitors docs for track_tag method
1 parent d225e8e commit 4298d32

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

plugins/optimization-detective/optimization.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -259,30 +259,37 @@ function od_optimize_template_output_buffer( string $buffer ): string {
259259
* or even a class with an `__invoke` method defined). The tag visitor callback is invoked by passing an instance
260260
* of the `OD_Tag_Visitor_Context` object which includes the following read-only properties:
261261
*
262-
* - `$processor` (`OD_HTML_Tag_Processor`): The processor with the cursor at the current tag.
262+
* - `$processor` (`OD_HTML_Tag_Processor`): The processor with the cursor at the current open tag.
263263
* - `$url_metric_group_collection` (`OD_URL_Metric_Group_Collection`): The URL Metrics which may include information about the current tag to inform what optimizations the callback performs.
264264
* - `$link_collection` (`OD_Link_Collection`): Collection of links which will be added to the `HEAD` when the page is served. This allows you to add preload links and preconnect links as needed.
265+
* - `$url_metrics_id` (`positive-int|null`): The post ID for the `od_url_metrics` post from which the URL Metrics were loaded (if any). For advanced usage.
265266
*
266-
* Note that you are free to call `next_tag()` in the callback (such as to walk over any child elements) since the
267-
* cursor will be reset to the tag after the callback finishes.
267+
* Note that you are free to call `$processor->next_tag()` in the callback (such as to walk over any child elements)
268+
* since the tag processor's cursor will be reset to the tag after the callback finishes.
268269
*
269-
* A tag visitor callback returns a boolean value. When it returns `true`, then Optimization Detective will mark the
270-
* tag as needing to be included among the elements stored in URL Metrics. The element data includes properties such
271-
* as intersectionRatio, intersectionRect, and boundingClientRect as well as whether it is the LCP element. If the
272-
* current tag is not relevant for the tag visitor or if the tag visitor callback does not need to query the provided
273-
* `OD_URL_Metric_Group_Collection` instance to apply the desired optimizations, it can just return `false`. An
274-
* element will be tracked in URL Metrics if _any_ tag visitor callback returns `true` when visiting the tag.
270+
* When a tag visitor sees it is at a relevant open tag (e.g. by checking `$processor->get_tag()`), it can call the
271+
* `$context->track_tag()` method to indicate that the tag should be measured during detection. This will cause the
272+
* tag to be included among the `elements` in the stored URL Metrics. The element data includes properties such
273+
* as `intersectionRatio`, `intersectionRect`, and `boundingClientRect` (provided by an `IntersectionObserver`) as
274+
* well as whether the tag is the LCP element (`isLCP`) or LCP element candidate (`isLCPCandidate`). This method
275+
* should not be called if the current tag is not relevant for the tag visitor or if the tag visitor callback does
276+
* not need to query the provided `OD_URL_Metric_Group_Collection` instance to apply the desired optimizations. (In
277+
* addition to calling the `$context->track_tag()`, a callback may also return `true` to indicate the tag should be
278+
* tracked.)
275279
*
276280
* Here's an example tag visitor that depends on URL Metrics data:
277281
*
278282
* $tag_visitor_registry->register(
279283
* 'lcp-img-fetchpriority-high',
280-
* static function ( OD_Tag_Visitor_Context $context ): bool {
284+
* static function ( OD_Tag_Visitor_Context $context ): void {
281285
* if ( $context->processor->get_tag() !== 'IMG' ) {
282-
* return false; // Tag is not relevant for this tag visitor.
286+
* return; // Tag is not relevant for this tag visitor.
283287
* }
284288
*
285-
* // Make sure fetchpriority=high is added to LCP IMG elements.
289+
* // Mark the tag for measurement during detection so it is included among the elements stored in URL Metrics.
290+
* $context->track_tag();
291+
*
292+
* // Make sure fetchpriority=high is added to LCP IMG elements based on the captured URL Metrics.
286293
* $common_lcp_element = $context->url_metric_group_collection->get_common_lcp_element();
287294
* if (
288295
* null !== $common_lcp_element
@@ -291,9 +298,6 @@ function od_optimize_template_output_buffer( string $buffer ): string {
291298
* ) {
292299
* $context->processor->set_attribute( 'fetchpriority', 'high' );
293300
* }
294-
*
295-
* // Must return true so that the tag is included among the elements stored in URL Metrics.
296-
* return true;
297301
* }
298302
* );
299303
*
@@ -306,17 +310,13 @@ function od_optimize_template_output_buffer( string $buffer ): string {
306310
* 'img-decoding-async',
307311
* static function ( OD_Tag_Visitor_Context $context ): bool {
308312
* if ( $context->processor->get_tag() !== 'IMG' ) {
309-
* return false; // Tag is not relevant for this tag visitor.
313+
* return; // Tag is not relevant for this tag visitor.
310314
* }
311315
*
312316
* // Set the decoding attribute if it is absent.
313317
* if ( null === $context->processor->get_attribute( 'decoding' ) ) {
314318
* $context->processor->set_attribute( 'decoding', 'async' );
315319
* }
316-
*
317-
* // There's no need to query OD_URL_Metric_Group_Collection, so this element
318-
* // doesn't need to be tracked in URL Metrics and the callback can return false.
319-
* return false;
320320
* }
321321
* );
322322
*

0 commit comments

Comments
 (0)