Skip to content

Require WP 6.3 and use core HTML and loading APIs - #1400

Draft
utkarshcloudinary wants to merge 6 commits into
developfrom
feat/wp-core-min-version-apis
Draft

utkarshcloudinary wants to merge 6 commits into
developfrom
feat/wp-core-min-version-apis

Conversation

@utkarshcloudinary

@utkarshcloudinary utkarshcloudinary commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Sets WordPress 6.3 as the minimum version (readme.txt already says 6.3) and uses the core APIs that this makes available.

 cloudinary.php
+  Requires at least: 6.3 / Requires PHP: 7.4   # WP blocks activation on older sites

 Delivery::parse_element / Utils::get_tag_attributes
-  shortcode_parse_atts( trim( $tag, '</>' ) )
+  Utils::parse_html_tag()  -> WP_HTML_Tag_Processor (6.2)

 Filter::filter_out_local (front end)
-  str_replace( '/>', ' class="wp-image-N"/>' )
-  str_replace( '/>', ' loading="lazy" />' )      # saved into post content
+  add_front_end_attributes() -> set_attribute( 'class' )

 Delivery::rebuild_tag
-  wp_image_add_srcset_and_sizes( $original )     # appends a 2nd srcset
+  wp_image_add_srcset_and_sizes( remove_tag_attribute( $original, 'srcset' ) )
+  wp_img_tag_add_auto_sizes( $rebuilt )           # keeps core sizes="auto" (6.7+)

Lazy load no longer replaces images at the top of the page. It uses the same signals as core (6.3):

is_in_initial_viewport( img )
  fetchpriority="high"                 -> eager (LCP candidate)
  loading="lazy" | fetchpriority="low" -> lazy
  otherwise                            -> eager for the first wp_omit_loading_attr_threshold() images (default 3)
  apply_filters( 'cloudinary_lazy_load_in_initial_viewport', ... )

Cleanup: removes the fallbacks for WP versions below 6.3 (function_exists() guards, the get_post_parent() fallback, the class-http.php include, the pre-5.1 render_block video fallback). filter_video_block_render_block() stays, marked @deprecated, for third-party callers. The PHP version notice uses wp_admin_notice() when it is available.

Not included: the Interactivity API (needs 6.5 and a gallery/player rewrite) and wp_admin_notice() for the plugin's own notice UI (that UI has icons and timed dismiss, which core does not support).

Approach

  • Commit 1: header and removal of the old fallbacks.
  • Commit 2: HTML API parsing and tag edits. Attribute values are decoded and not unslashed, and a > inside a value no longer cuts the tag short.
  • Commit 3: initial viewport check in Lazy_Load::bypass_lazy_load(), and sizes="auto" on rebuilt tags.
  • Commit 4: slash the replacement cache before update_post_meta(). Because the parser now keeps backslashes, a cached page rendered alt="C:\new" as alt="C:new" while the uncached page did not. A regression test covers it (fails without the fix).

Evidence

The same parser code, run against the WP 6.3 WP_HTML_Tag_Processor:

input:  <IMG SRC="https://e.com/a.jpg?a=1&amp;b=2" alt="a > b" fetchpriority=high hidden class="x y" />
output: tag=img  src=https://e.com/a.jpg?a=1&b=2  alt="a > b"  fetchpriority=high  [hidden]  class="x y"

Class insertion on a tag without a self-closing slash:

  • Before: <img src="a.jpg"> stays unchanged (there is no /> to replace)
  • After: <img class="wp-image-12" src="a.jpg">

sizes on a rebuilt lazy image (WP 7.2 RC4):

  • Before: sizes="(max-width: 300px) 100vw, 300px" (core's auto was dropped when the tag was rebuilt)
  • After: sizes="auto, (max-width: 300px) 100vw, 300px"

New unit tests are in tests/phpunit/tests/test-wp-core-apis.php and test-delivery-parse-element.php. They cover the parser, the class insertion, the srcset removal, the viewport threshold and core hints, and the new filter.

Browser check (Chrome DevTools, local wp-env, WP 7.2 RC4, lazy load on, 6 synced images, image 5 has fetchpriority="high"):

Image develop This branch
Hero (core fetchpriority="high") placeholder, lazy Cloudinary URL, eager
Images 1–2 placeholder, lazy Cloudinary URL, eager
Images 3–4 (core loading="lazy") placeholder, lazy placeholder, lazy, swaps to Cloudinary on scroll
Image 5 (fetchpriority="high") placeholder, lazy Cloudinary URL, eager
  • wp_omit_loading_attr_threshold = 1: only the hero is eager. cloudinary_lazy_load_in_initial_viewport = false: all lazy, same as develop.
  • Lazy load off with breakpoints on: develop drops core's sizes="auto" from lazy images; this branch keeps it. Each image has one srcset.
  • Video block plays from Cloudinary. Cached and uncached renders are the same. No console errors or warnings.

QA notes

  1. Turn on lazy loading. Publish a post with 5 or more images and no loading attributes. On the front end, the first 3 images (default threshold) have a Cloudinary src and no data-cloudinary="lazy". The later images have the placeholder and lazy load on scroll.
  2. Add add_filter( 'wp_omit_loading_attr_threshold', fn() => 1 );. Only the first image loads eagerly.
  3. Make sure the featured or hero image has fetchpriority="high" and is never a lazy placeholder.
  4. Add add_filter( 'cloudinary_lazy_load_in_initial_viewport', '__return_false' );. All images are lazy loaded again, as on develop.
  5. Turn off lazy loading. Lazy images keep sizes="auto, …" on WP 6.7 or higher, and each image has only one srcset.
  6. Video block with the Cloudinary player on and off: the video still renders.
  7. On a WP 6.2 site, the plugin cannot be activated (version header).

Merge Danger

Door: two-way. No data or option changes. A revert restores the old behaviour. Content saved after this change no longer contains loading="lazy", which is the core default.

Blast Radius: front end markup of every image that Delivery rewrites.

  • The first images on each page now load eagerly when lazy load is on. This is faster for LCP but uses more bytes before the page is visible. Sites can change it with wp_omit_loading_attr_threshold or cloudinary_lazy_load_in_initial_viewport.
  • Parser differences: attribute names are now lowercase and backslashes in values are kept. Code on the cloudinary_parse_element / cloudinary_pre_image_tag filters that expected the old output can see different values.
  • Sites on WP versions below 6.3 cannot activate new versions of the plugin.

readme.txt already declares WordPress 6.3 as the minimum. Add the
same "Requires at least" and "Requires PHP" headers to the plugin
file, so WordPress blocks activation on older sites.

Remove the code paths that only ran on WordPress versions below 6.3:
the function_exists() checks for wp_get_original_image_path/url,
has_block and wp_print_inline_script_tag, the get_post_parent()
fallback, the pre-5.9 class-http.php include and the pre-5.1
render_block fallback for video blocks. The fallback callback stays,
marked deprecated, for third-party callers.

Use wp_admin_notice() (WP 6.4+) for the PHP version notice when it is
available.
Replace shortcode_parse_atts() and str_replace() tag handling with
WP_HTML_Tag_Processor (WP 6.2+).

- Utils::parse_html_tag() reads the tag name and attributes with the
  HTML API. Values are decoded and not unslashed, and a ">" inside a
  value no longer cuts the tag short. Delivery::parse_element() and
  Utils::get_tag_attributes() use it.
- Filter::add_front_end_attributes() sets the missing class with
  set_attribute(), instead of inserting text before "/>", which did
  nothing on tags without a self-closing slash.
- The content filter no longer stores loading="lazy" in post content.
  The check called wp_lazy_loading_enabled with the wrong arguments,
  and a stored value stops core from finding above the fold images.
- Remove the old srcset before wp_image_add_srcset_and_sizes(), which
  appends a second srcset instead of replacing the first one.
Cloudinary lazy load replaced every image with a placeholder,
including the LCP image, which delays the largest paint.

Follow the WordPress 6.3 loading optimization decisions instead:
- An image with fetchpriority="high" is the LCP candidate and is
  never lazy loaded, so core's fetchpriority now takes effect.
- An image with loading="lazy" or fetchpriority="low" is below the
  fold and is lazy loaded.
- Other images count towards wp_omit_loading_attr_threshold (3 by
  default) in document order. The first ones load eagerly.

The new cloudinary_lazy_load_in_initial_viewport filter can change
the result per image.

Delivery rebuilds the tag after core adds sizes="auto" (WP 6.7+), so
run wp_img_tag_add_auto_sizes() on the rebuilt tag to keep it.

The breakpoints e2e fixture now marks its image loading="lazy", as it
is the only image in the post and would otherwise load eagerly.
The HTML API parser keeps backslashes in attribute values, which
shortcode_parse_atts() used to strip. update_post_meta() unslashes
the value, so the cached replacement for alt="C:\new" was saved as
alt="C:new" and a cached page rendered a different tag than an
uncached one.

Slash the replacements before they are saved, and move the write
into Delivery::update_context_cache() so it can be tested.
The breakpoints fixture image is now natively lazy loaded, and
Delivery keeps the "auto" that WordPress 6.7+ adds to the sizes of
lazy loaded images. Update the expected sizes attribute.
- Video blocks: replace the src and poster through the HTML API in
  the new Video::replace_tag_urls(). get_tag_attributes() now returns
  decoded values, so str_replace() missed URLs with "&amp;" in the
  HTML and left the local URL when the Cloudinary player is off.
- Stop loading the plugin on WordPress below 6.3 and show a notice.
  The header only blocks activation from the admin, not updates with
  Composer or a zip file.
- Clear the replacement cache on cloudinary_version_upgrade. Cached
  tags skip rebuild_tag(), so pages kept the old markup.
- Check the lazy load bypass classes before the viewport check, so
  bypassed images do not use up the eager places.
- Call _deprecated_function() in the deprecated video callback.
- Move remove_tag_attribute() to Utils.
- Tests: skip the sizes="auto" test before WP 6.7, compare attributes
  instead of exact markup, parse tags through get_media_tags(), cover
  the new code, and add an e2e case for the first image.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants