Repository navigation
Require WP 6.3 and use core HTML and loading APIs #1400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
f2c58e2
6b09827
4b6ef86
cd53de4
a83a8ba
72331ec
024fd9f
b6f8c3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -144,11 +144,9 @@ public static function is_webstory_post_type( $post_type ) { | |
| * @return array | ||
| */ | ||
| public static function get_tag_attributes( $tag ) { | ||
| $tag = strstr( $tag, ' ', false ); | ||
| $tag = trim( $tag, '> ' ); | ||
| $args = shortcode_parse_atts( $tag ); | ||
| $parsed = self::parse_html_tag( (string) $tag ); | ||
| $return = array(); | ||
| foreach ( $args as $key => $value ) { | ||
| foreach ( $parsed['atts'] as $key => $value ) { | ||
| if ( is_int( $key ) ) { | ||
| $return[ $value ] = 'true'; | ||
| continue; | ||
|
|
@@ -159,6 +157,90 @@ public static function get_tag_attributes( $tag ) { | |
| return $return; | ||
| } | ||
|
|
||
| /** | ||
| * Parse the first HTML tag in a string with the WordPress HTML API. | ||
| * | ||
| * Attribute names are lowercase and values are decoded. Boolean attributes use numeric keys, | ||
| * the same as shortcode_parse_atts(), so Component::build_attributes() writes them back as is. | ||
| * | ||
| * @param string $html The HTML that starts with the tag. | ||
| * | ||
| * @return array{tag: string, atts: array<int|string, string>} | ||
| */ | ||
| public static function parse_html_tag( $html ) { | ||
| $parsed = array( | ||
| 'tag' => '', | ||
| 'atts' => array(), | ||
| ); | ||
| $processor = new \WP_HTML_Tag_Processor( (string) $html ); | ||
| if ( ! $processor->next_tag() ) { | ||
| return $parsed; | ||
| } | ||
|
|
||
| $parsed['tag'] = strtolower( $processor->get_tag() ); | ||
| $names = $processor->get_attribute_names_with_prefix( '' ); | ||
| foreach ( (array) $names as $name ) { | ||
| $value = $processor->get_attribute( $name ); | ||
| if ( true === $value ) { | ||
| $parsed['atts'][] = $name; | ||
| continue; | ||
| } | ||
| $parsed['atts'][ $name ] = (string) $value; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit / edge case: values are now decoded here, and It's a rare case and I wouldn't block on it, but since
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, fixed in b6f8c3f. There are 6 new unit tests across the three fixes. On the previous commit, 5 fail; the sixth ( |
||
| } | ||
|
|
||
| return $parsed; | ||
| } | ||
|
|
||
| /** | ||
| * Escape the attribute values that are unchanged from an original tag, so they render as before. | ||
| * | ||
| * The values from parse_html_tag() are decoded. Component::build_attributes() escapes them again with | ||
| * esc_attr(), which does not encode the entities it finds, so a value that shows entity text | ||
| * (alt="Use &lt;img&gt;", shown as "Use <img>") would be decoded one level too | ||
| * far and render as "Use <img>". Escaping it here with double encoding on restores it. | ||
| * | ||
| * Values that differ from the original tag are left as they are, as other code may pass them | ||
| * already escaped. A value with no "&" is left as is too, as esc_attr() handles it the same way. | ||
| * | ||
| * @param array $atts The attributes to build the tag from. | ||
| * @param string $original The original tag the attributes were parsed from. | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function escape_original_attributes( $atts, $original ) { | ||
| $original_atts = self::parse_html_tag( (string) $original )['atts']; | ||
| foreach ( $atts as $name => $value ) { | ||
| if ( | ||
| is_string( $name ) | ||
| && is_string( $value ) | ||
| && false !== strpos( $value, '&' ) | ||
| && isset( $original_atts[ $name ] ) | ||
| && $original_atts[ $name ] === $value | ||
| ) { | ||
| $atts[ $name ] = htmlspecialchars( $value, ENT_QUOTES, get_bloginfo( 'charset' ), true ); | ||
| } | ||
| } | ||
|
|
||
| return $atts; | ||
| } | ||
|
|
||
| /** | ||
| * Remove an attribute from the first tag in an HTML string. | ||
| * | ||
| * @param string $html The HTML. | ||
| * @param string $attribute The attribute name. | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function remove_tag_attribute( $html, $attribute ) { | ||
| $processor = new \WP_HTML_Tag_Processor( (string) $html ); | ||
| if ( $processor->next_tag() ) { | ||
| $processor->remove_attribute( $attribute ); | ||
| } | ||
|
|
||
| return $processor->get_updated_html(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the depth of an array. | ||
| * | ||
|
|
@@ -545,15 +627,7 @@ public static function get_support_link( $args = array() ) { | |
| * @param string $javascript Inline JavaScript code. | ||
| */ | ||
| public static function print_inline_tag( $javascript ) { | ||
| if ( function_exists( 'wp_print_inline_script_tag' ) ) { | ||
| wp_print_inline_script_tag( $javascript ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $javascript = "\n" . trim( $javascript, "\n\r " ) . "\n"; | ||
|
|
||
| printf( "<script type='text/javascript'>%s</script>\n", $javascript ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | ||
| wp_print_inline_script_tag( $javascript ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -768,13 +842,7 @@ public static function svg_encoded( $width = '600px', $height = '400px', $color | |
| * @return WP_Post|null | ||
| */ | ||
| public static function get_post_parent( $post = null ) { | ||
| // get_post_parent() was added in WP 6.0; keep a fallback for older supported versions. | ||
| if ( is_callable( 'get_post_parent' ) ) { // @phpstan-ignore function.alreadyNarrowedType | ||
| return get_post_parent( $post ); | ||
| } | ||
|
|
||
| $wp_post = get_post( $post ); // @phpstan-ignore deadCode.unreachable | ||
| return ! empty( $wp_post->post_parent ) ? get_post( $wp_post->post_parent ) : null; | ||
| return get_post_parent( $post ); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above-the-fold decision depends on where the image sits on the page, but the result ends up in this cache, keyed only by the original tag.
get_context_cache()then merges the caches of every post that went throughthe_post, so a decision made on one page gets reused on another:rebuild_tag(), so they never incrementviewport_count. An uncached image further down the page starts counting from 0 and can be made eager too.Before this PR the lazy/eager result didn't depend on position, so caching the rebuilt tag was safe. Now it isn't.
A few options:
imgtags that went through the viewport check (simple, but we lose some of the cache benefit).is_singular()and the current queried object).I don't mind which. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, you're right. I had checked this on the blog home before, but only for images core marks
loading="lazy"on the archive, where the tag differs and so does the cache key. Your case, where core leaves the tag the same and only our count differs, does leak.Fixed in b6f8c3f with a mix of your options 1 and 2:
fetchpriority,loading, or core'sdecoding="async"with noloading). The tag is the cache key, so that decision is safe to cache: when core decides differently on another page, the tag differs too.cloudinary_lazy_load_in_initial_viewportfilter changed, depend on the page. Those are now left out of_cld_replacements, so they are rebuilt on every page. That should also cover the mixed cached/uncached case, as a counted tag is never read from the cache, so it is always counted (I haven't tested that case separately).Checked on the local site, with the post's cache cleared, then written by a visit to the single post: the blog home listing that post rendered exactly the same eager/lazy result as before the cache existed. Note this post's content images all come through core, so none of them is counted; the "counted tags are left out" part is covered by
test_replacement_cache_skips_position_dependent_tags, not by this check.