Commit Graph

932 Commits

Author SHA1 Message Date
audrasjb
6371434821 Media: Prevent division by zero in wp_img_tag_add_width_and_height_attr().
This changesets adds a check for `$size_array` values to prevent a potential division by zero.
Follow-up to [57294].

Props jdekhtiar.
Fixes #61054.
See #59352.



Built from https://develop.svn.wordpress.org/trunk@58033


git-svn-id: http://core.svn.wordpress.org/trunk@57499 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-04-22 20:35:10 +00:00
Sergey Biryukov
dd6d4e74c1 Code Modernization: Fix implicit nullable parameter type deprecation on PHP 8.4.
In PHP 8.4, declaring function or method parameters with a default value of `null` is deprecated if the type is not nullable.

PHP applications are recommended to ''explicitly'' declare the type as nullable. All type declarations that have a default value of `null`, but without declaring `null` in the type declaration, will emit a deprecation notice:
{{{
function test( array $value = null ) {}
}}}
`Deprecated: Implicitly marking parameter $value as nullable is deprecated, the explicit nullable type must be used instead`

**Recommended Changes**

Change the implicit nullable type declaration to a nullable type declaration, available since PHP 7.1:
{{{#!diff
- function test( string $test = null ) {}
+ function test( ?string $test = null ) {}
}}}

This commit updates the affected instances in core to use a nullable type declaration.

References:
* [https://wiki.php.net/rfc/deprecate-implicitly-nullable-types PHP RFC: Deprecate implicitly nullable parameter types]
* [https://php.watch/versions/8.4/implicitly-marking-parameter-type-nullable-deprecated PHP.Watch: PHP 8.4: Implicitly nullable parameter declarations deprecated]

Follow-up to [28731], [50552], [57337], [57985].

Props ayeshrajans, jrf, audrasjb, jorbin.
Fixes #60786.
Built from https://develop.svn.wordpress.org/trunk@58009


git-svn-id: http://core.svn.wordpress.org/trunk@57480 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-04-15 20:03:09 +00:00
Sergey Biryukov
9ac3e01c91 Docs: Fix typo in wp_mediaelement_fallback filter description.
Follow-up to [23729], [28128].

Props dilipbheda, mukesh27.
Fixes #60917.
Built from https://develop.svn.wordpress.org/trunk@57921


git-svn-id: http://core.svn.wordpress.org/trunk@57422 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-04-04 11:19:14 +00:00
Sergey Biryukov
117fdc548e Coding Standards: Correct alignment in wp_get_attachment_image_src().
This resolves a WPCS warning:
{{{
Equals sign not aligned with surrounding statements
}}}
so that the output of `composer format` is clean.

Follow-up to [57687].

See #59650.
Built from https://develop.svn.wordpress.org/trunk@57694


git-svn-id: http://core.svn.wordpress.org/trunk@57195 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-22 11:56:10 +00:00
joedolson
5fd767d87d Media: Ensure wp_mine_type_icon() returns expected file type.
Add an argument to `wp_mime_type_icon()` to control the file type returned. Following [57638], there are two file formats in the media icons directory. Different systems would pull up different files by default dependent on the order loaded into the cached array, causing intermittent test failures and unpredictable behavior.

Function update allows core usages to always return the `.svg` while maintaining backwards compatibility for any extended usage that expects a `.png`. Follow up to [57638].

Also handles a missed case in media list view.

Props SergeyBiryukov, sabernhardt, joedolson, antpb.
Fixes #31352.
Built from https://develop.svn.wordpress.org/trunk@57687


git-svn-id: http://core.svn.wordpress.org/trunk@57188 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-21 19:29:04 +00:00
Pascal Birchler
10b96420a5 Shortcodes: Always return an array in shortcode_parse_atts().
Previously, `shortcode_parse_atts()` would return the input (an empty string) if a shortcode had no attributes, even though the documentation said otherwise.

Always returning an (empty) array reduces confusion and improves developer experience as the return value does not have to be manually checked in the shortcode itself.

Props: nicolefurlan, swissspidy, johnbillion, bedas.
Fixes #59249.
Built from https://develop.svn.wordpress.org/trunk@57597


git-svn-id: http://core.svn.wordpress.org/trunk@57098 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-12 16:08:10 +00:00
Adam Silverstein
f94f194552 Media: enable AVIF support.
Add support for uploading, editing and saving AVIF images when supported by the server.

Add 'image/avif' to supported mime types. Correctly identify AVIF images and sizes even when PHP doesn't support AVIF. Resize uploaded AVIF files (when supported) and use for front end markup.

Props adamsilverstein, lukefiretoss, ayeshrajans, navjotjsingh, Tyrannous, jb510, gregbenz, nickpagz, JavierCasares, mukesh27, yguyon, swissspidy.
Fixes #51228.


Built from https://develop.svn.wordpress.org/trunk@57524


git-svn-id: http://core.svn.wordpress.org/trunk@57025 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-02 17:48:15 +00:00
Felix Arntz
14263ea394 Media: Consider inline image CSS width to backfill width and height attributes.
Prior to this changeset, WordPress core would use the original image size, which in the particular case of inline images would be severely off, as they are usually very small. This could lead to incorrect application of `fetchpriority="high"` and other performance optimizations.

Props westonruter, flixos90, joemcgill, mukesh27.
Fixes #59352.

Built from https://develop.svn.wordpress.org/trunk@57294


git-svn-id: http://core.svn.wordpress.org/trunk@56800 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-16 17:03:13 +00:00
costdev
28331b7aae Docs: Correct some typos in docblocks and inline comments.
This corrects several typos in documentation, including:
- "imput" -> "input"
- "proessing" -> "processing"
- "instantating" -> "instantiating"
- "filtersing" -> "filtering"
- "officaly" -> "officially"

Follow-up to [8852], [25307], [26191], [37488], [54416].

Props benniledl, mukesh27, jayadevankbh, Presskopp.
Fixes #60069. See #59651.
Built from https://develop.svn.wordpress.org/trunk@57263


git-svn-id: http://core.svn.wordpress.org/trunk@56769 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-10 11:57:15 +00:00
Sergey Biryukov
09815e007d Docs: Fix typo in get_image_tag() DocBlock.
Follow-up to [54173].

Props kitchin.
See #55646, #59651.
Built from https://develop.svn.wordpress.org/trunk@57121


git-svn-id: http://core.svn.wordpress.org/trunk@56632 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-11-17 15:33:21 +00:00
Sergey Biryukov
d7a1816bf1 Docs: Improve some DocBlocks and inline comments per the documentation standards.
Follow-up to [56834], [56836], [56837], [56838].

Props kebbet, costdev, mukesh27, SergeyBiryukov.
See #59651.
Built from https://develop.svn.wordpress.org/trunk@57120


git-svn-id: http://core.svn.wordpress.org/trunk@56631 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-11-17 13:29:26 +00:00
Sergey Biryukov
b330629c53 Coding Standards: Correct equals sign alignment in various files.
This resolves a few WPCS warnings:
{{{
Equals sign not aligned with surrounding statements
}}}
so that the output of `composer format` is clean.

Follow-up to [56796], [56803], [56838], [56839], [56985].

See #59650.
Built from https://develop.svn.wordpress.org/trunk@57053


git-svn-id: http://core.svn.wordpress.org/trunk@56564 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-11-02 00:04:24 +00:00
Sergey Biryukov
448bde29ad Docs: Correct the type for optional parameters in a few media functions.
This affects the `$image_src` and/or `$image_meta` parameters in:
* `wp_get_attachment_image_srcset()`
* `wp_get_attachment_image_sizes()`
* `wp_calculate_image_sizes()`

Follow-up to [35412], [35419], [35481], [35498], [35569], [35672].

Props dilipbheda.
Fixes #59745.
Built from https://develop.svn.wordpress.org/trunk@57042


git-svn-id: http://core.svn.wordpress.org/trunk@56553 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-11-01 11:51:26 +00:00
audrasjb
f3ccbed586 Shortcodes: Restrict ajax handler for media shortcode.
Props tykoted, xknown, peterwilsoncc, antpb, jorbin.




Built from https://develop.svn.wordpress.org/trunk@56838


git-svn-id: http://core.svn.wordpress.org/trunk@56350 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-12 12:47:21 +00:00
Sergey Biryukov
440b8c2aba Coding Standards: Remove redundant ignore annotations, take 2.
This removes ignore annotations which are unnecessary due to the configuration in the `phpcs.xml.dist` ruleset already taking care of this.

Follow-up to [45611], [50146], [50148], [50586], [50822], [56738].

Props jrf.
See #59161.
Built from https://develop.svn.wordpress.org/trunk@56743


git-svn-id: http://core.svn.wordpress.org/trunk@56255 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-29 12:21:24 +00:00
Felix Arntz
70e0a22e7d Media: Ensure images within shortcodes are correctly considered for loading optimization attributes.
Prior to this change, images added in shortcodes would be considered separately from all other images within post content, which led to incorrect application of the loading optimization attributes `loading="lazy"` and `fetchpriority="high"`.

This changeset changes the filter priority of `wp_filter_content_tags()` from the default `10` to `12` on the various content filters it is hooked in, in order to run that function after parsing shortcodes. While this may technically be considered a backward compatibility break, substantial research and lack of any relevant usage led to the assessment that the change is acceptable given its benefits.

An additional related fix included is that now the duplicate processing of images is prevented not only for post content blobs (`the_content` filter), but also for widget content blobs (`widget_text_content` and `widget_block_content` filters).

Props joemcgill, mukesh27, costdev, spacedmonkey, flixos90.
Fixes #58853.

Built from https://develop.svn.wordpress.org/trunk@56693


git-svn-id: http://core.svn.wordpress.org/trunk@56205 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-26 00:13:12 +00:00
Felix Arntz
8964275967 Media: Rely on wp_get_loading_optimization_attributes() to add decoding="async" to images.
The `wp_get_loading_optimization_attributes()` function was introduced in 6.3, as a single centralized place to control loading optimization attributes for various tags, most importantly images.

This changeset consolidates the `decoding="async"` optimization, which was added in 6.1, to occur solely as part of `wp_get_loading_optimization_attributes()`, removing duplicate code and allowing centralized filtering based on [56651].

As part of the change, the `wp_img_tag_add_decoding_attr()` function has been deprecated. The filter of the same name continues to be maintained for backward compatibility, as before covering only images that are part of a content blob such as post content (`the_content`).

Props pereirinha, mukesh27, joemcgill, flixos90.
Fixes #58892.
See #53232.

Built from https://develop.svn.wordpress.org/trunk@56690


git-svn-id: http://core.svn.wordpress.org/trunk@56202 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-25 22:38:17 +00:00
Sergey Biryukov
28af9eafb0 Coding Standards: Fix a few newly introduced WPCS issues.
Follow-up to [56570], [56573], [56589], [56604], [56612], [56620], [56629], [56631], [56638], [56642], [56644], [56649].

Props jrf.
See #59161, #58831.
Built from https://develop.svn.wordpress.org/trunk@56680


git-svn-id: http://core.svn.wordpress.org/trunk@56192 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-25 15:36:18 +00:00
Felix Arntz
cd11ddbac1 Media: Introduce filters to customize the results from wp_get_loading_optimization_attributes().
This changeset introduces two filters that allow customizing the loading optimization attributes array returned from `wp_get_loading_optimization_attributes()` for individual HTML tags:
* The `wp_get_loading_optimization_attributes` filter can be used to modify the results from the WordPress core logic.
* The `pre_wp_get_loading_optimization_attributes` filter can be used to use entirely custom logic and effectively short-circuit the core function.

Props pereirinha, mukesh27, spacedmonkey, joemcgill.
Fixes #58893.

Built from https://develop.svn.wordpress.org/trunk@56651


git-svn-id: http://core.svn.wordpress.org/trunk@56163 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-21 16:38:15 +00:00
Felix Arntz
56b5244fdb Media: Enhance wp_get_loading_optimization_attributes() to support arbitrary context values.
The `wp_get_loading_optimization_attributes()` function, which was introduced in 6.3, based on the now deprecated `wp_get_loading_attr_default()` function introduced in 5.5, relies on a `$context` parameter based on which it may alter its behavior and the attributes returned. So far, it has only supported context values used within WordPress core.

This changeset decouples the behaviors of the function from specific contexts, allowing for more flexibility. Theme and plugin developers will be able to rely on their own context values when rendering images in non-standard ways, rather than being forced to use a core context, to get the loading optimization benefits the function provides.

As part of this change, a `wp_loading_optimization_force_header_contexts` filter is introduced, which allows filtering the map of context values and whether they should be considered header contexts, i.e. i.e. any image having one of these contexts will be assumed to appear above the fold.

Props mukesh27, costdev, flixos90.
Fixes #58894.

Built from https://develop.svn.wordpress.org/trunk@56612


git-svn-id: http://core.svn.wordpress.org/trunk@56124 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-18 14:55:18 +00:00
Sergey Biryukov
b80ce60f70 Coding Standards: Use pre-increment/decrement for stand-alone statements.
Note: This is enforced by WPCS 3.0.0:

1. There should be no space between an increment/decrement operator and the variable it applies to.
2. Pre-increment/decrement should be favoured over post-increment/decrement for stand-alone statements. “Pre” will in/decrement and then return, “post” will return and then in/decrement. Using the “pre” version is slightly more performant and can prevent future bugs when code gets moved around.

References:
* [https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#increment-decrement-operators WordPress PHP Coding Standards: Increment/decrement operators]
* [https://github.com/WordPress/WordPress-Coding-Standards/pull/2130 WPCS: PR #2130 Core: add sniffs to check formatting of increment/decrement operators]

Props jrf.
See #59161, #58831.
Built from https://develop.svn.wordpress.org/trunk@56549


git-svn-id: http://core.svn.wordpress.org/trunk@56061 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-09 09:28:26 +00:00
Pascal Birchler
e07e95e232 Media: Prevent warning if [caption] shortcode is used without attributes.
Props maltfield, aslamdoctor.
Fixes #59206.
Built from https://develop.svn.wordpress.org/trunk@56488


git-svn-id: http://core.svn.wordpress.org/trunk@56000 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-08-29 15:35:21 +00:00
John Blackbourn
46fda1912c Privacy: Improve docblocks for hooks and functions related to personal data export and erasure.
See #58833

Built from https://develop.svn.wordpress.org/trunk@56482


git-svn-id: http://core.svn.wordpress.org/trunk@55994 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-08-28 21:53:21 +00:00
audrasjb
f668df3cec Docs: Wrap inline @see tags in curly braces.
Props costdev, mukesh27.
Fixes #58858.
See #58833.



Built from https://develop.svn.wordpress.org/trunk@56434


git-svn-id: http://core.svn.wordpress.org/trunk@55946 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-08-24 09:01:16 +00:00
Sergey Biryukov
efdebb621b Docs: Correct punctuation in some instances of the $crop parameter description.
Follow-up to [56416].

See #58833.
Built from https://develop.svn.wordpress.org/trunk@56418


git-svn-id: http://core.svn.wordpress.org/trunk@55930 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-08-19 00:10:23 +00:00
John Blackbourn
b5e754ee35 Media: Standardise documentation of the $crop parameter for various media functions and methods.
See #58833

Built from https://develop.svn.wordpress.org/trunk@56416


git-svn-id: http://core.svn.wordpress.org/trunk@55928 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-08-18 17:56:17 +00:00
Sergey Biryukov
93bf4a6d6f Docs: Correct duplicate hook reference for wp_min_priority_img_pixels.
The original filter location is `wp_maybe_add_fetchpriority_high_attr()` in `wp-includes/media.php`.

This commit updates the instance in `wp_get_loading_optimization_attributes()` to point to the correct file.

Follow-up to [56037], [56143], [56347].

Props tmatsuur, rajinsharwar, khokansardar.
Fixes #59067.
Built from https://develop.svn.wordpress.org/trunk@56386


git-svn-id: http://core.svn.wordpress.org/trunk@55898 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-08-11 11:35:19 +00:00
Felix Arntz
6cd7404aa3 Media: Simplify logic in wp_get_loading_optimization_attributes().
While the `wp_get_loading_optimization_attributes()` function was only recently introduced in 6.3, its code was mostly ported over from the now deprecated `wp_get_loading_attr_default()` function introduced in 5.5.

That function started out in a simple way, but over time was expanded with more and more conditionals on when to avoid lazy-loading, which ended up making the logic extremely complex and hard to follow.

This changeset refactors the logic to simplify it, in a way that allows to follow it more sequentially, and without making any functional changes, ensuring that the extensive existing unit test coverage still passes. This will facilitate future enhancements to the function to be less error-prone and make it more accessible to new contributors.

Props flixos90, joemcgill.
Fixes #58891.

Built from https://develop.svn.wordpress.org/trunk@56347


git-svn-id: http://core.svn.wordpress.org/trunk@55859 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-08-02 17:58:26 +00:00
Sergey Biryukov
ffa1d288f3 Coding Standards: Correct alignment in wp-includes/media.php.
This resolves a WPCS warning:
{{{
Found precision alignment of 1 space.
}}}

Follow-up to [55988].

Props jrf.
See #58831.
Built from https://develop.svn.wordpress.org/trunk@56283


git-svn-id: http://core.svn.wordpress.org/trunk@55795 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-23 16:00:26 +00:00
Sergey Biryukov
3a065bfb2c Coding Standards: Correct equals sign alignment in various files.
This resolves a few WPCS warnings:
{{{
Equals sign not aligned with surrounding statements
}}}
so that the output of `composer format` is clean.

Follow-up to [55971], [56033], [56056], [56143], [56214].

Props jrf.
See #58831.
Built from https://develop.svn.wordpress.org/trunk@56273


git-svn-id: http://core.svn.wordpress.org/trunk@55785 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-20 00:06:21 +00:00
Sergey Biryukov
59f1c9c205 Code Modernization: Use str_contains() in a few more places.
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).

WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.

This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.

Follow-up to [55988], [55990], [56014], [56021], [56031], [56032], [56065], [56241].

See #58206.
Built from https://develop.svn.wordpress.org/trunk@56245


git-svn-id: http://core.svn.wordpress.org/trunk@55757 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-17 13:18:27 +00:00
Joe McGill
725f39d73f Media: Optimize images created in shortcodes.
This fixes an issue where images dynamically created during shortcode rendering (e.g., shortcode image galleries), were not getting image optimizations like `loading="lazy"` or `fetchpriority="hight"` applied. Note that even after this commit, shortcodes are processed after the main content images, which can affect the order in which optimizations are applied in content areas.

Follow-up to [56037].

Props spacedmonkey, flixos90, thekt12, swissspidy, joemcgill.
Fixes #58681.

Built from https://develop.svn.wordpress.org/trunk@56214


git-svn-id: http://core.svn.wordpress.org/trunk@55726 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-11 13:58:21 +00:00
audrasjb
321158f726 Docs: Replace multiple single line comments with multi-line comments.
This changeset updates various comments as per WordPress PHP Inline Documentation Standards.
See https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/#5-inline-comments.

Follow-up to [56174], [56175], [56176], [56177], [56178], [56179], [56180].

Props costdev, audrasjb.
See #58459.



Built from https://develop.svn.wordpress.org/trunk@56191


git-svn-id: http://core.svn.wordpress.org/trunk@55703 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-10 22:38:25 +00:00
Felix Arntz
bb8ad05161 Media: Avoid programmatically created images within post content from incorrectly receiving fetchpriority="high".
Follow-up to [56037], as that changeset accidentally did not consider the changes made in [55825]: Images that are programmatically injected into post content (e.g. through a block, or shortcode, or any hook calling a function like `wp_get_attachment_image()`) must be treated as part of the whole post content blob since otherwise the heuristics for applying `fetchpriority="high"` and `loading="lazy"` are skewed by parsing certain images before others rather than sequentially parsing the entire post content. [55825] addressed that for lazy-loading, but when [56037] introduced `fetchpriority` support, the related refactor missed making the same consideration for that attribute.

Props flixos90, spacedmonkey, thekt12, mukesh27.
Fixes #58235.
See #58089.

Built from https://develop.svn.wordpress.org/trunk@56164


git-svn-id: http://core.svn.wordpress.org/trunk@55676 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-07 18:08:24 +00:00
Felix Arntz
5b91824af0 Media: Ensure that the image widget supports loading optimization attributes.
This changeset adds support for loading optimization attributes such as `loading="lazy"` and `fetchpriority="high"` to the image widget. A new context `widget_media_image` is introduced for that purpose.

Props spacedmonkey, thekt12, mukesh27, westonruter.
Fixes #58704.
See #58235.

Built from https://develop.svn.wordpress.org/trunk@56154


git-svn-id: http://core.svn.wordpress.org/trunk@55666 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-06 16:33:27 +00:00
Felix Arntz
fb9013c9b4 Media: Ensure that large images before the main query loop are counted towards lazy-loading threshold.
Following [55318], [55847], and [56142], certain images in the header of the page have received support for potentially receiving `fetchpriority="high"` and having the `loading="lazy"` attribute omitted. However, these images being present did not affect the "content media count" which counts the images towards a certain threshold so that the first ones are not lazy-loaded.

This changeset also increases that count for such header images if they are larger than a certain threshold. The threshold is used to avoid e.g. a header with lots of small images such as icon graphics to skew the lazy-loading behavior.

Props thekt12, spacedmonkey, flixos90.
Fixes #58635.

Built from https://develop.svn.wordpress.org/trunk@56143


git-svn-id: http://core.svn.wordpress.org/trunk@55655 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-05 21:38:24 +00:00
Felix Arntz
9ecfdd8e5a Media: Ensure custom header image tag supports loading optimization attributes.
This changeset is a follow up to [56037] and ensures that the `get_header_image_tag()` function receives the benefits of `wp_get_loading_optimization_attributes()` as well. Prior to `fetchpriority` support, this was not needed since the header image should never be lazy-loaded, but the image certainly is a `fetchpriority` candidate, so therefore it is crucial to have it supported.

Props felipeelia, spacedmonkey, mukesh27, westonruter, flixos90.
Fixes #58680.

Built from https://develop.svn.wordpress.org/trunk@56142


git-svn-id: http://core.svn.wordpress.org/trunk@55654 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-05 19:30:25 +00:00
audrasjb
5933075eae Docs: Fix image_get_intermediate_size() docblock.
The description of returned values in array was incorrect for `image_get_intermediate_size()`:
- `$file` returns the filename of image, no path information
- `$path` returns path relative to the uploads directory, not absolute

Props crstauf, sccr410.
Fixes #58686.
See #57840.



Built from https://develop.svn.wordpress.org/trunk@56122


git-svn-id: http://core.svn.wordpress.org/trunk@55634 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-01 23:10:22 +00:00
Felix Arntz
a1fcd5eaa4 Media: Fix inconsistent docs for existing wp_img_tag_add_loading_attr filter and remove duplicate.
Follow-up to [56037].

Props thekt12.
See #58235.

Built from https://develop.svn.wordpress.org/trunk@56112


git-svn-id: http://core.svn.wordpress.org/trunk@55624 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-29 17:34:23 +00:00
Felix Arntz
5ca27acd20 Media: Automatically add fetchpriority="high" to hero image to improve load time performance.
This changeset adds support for the `fetchpriority` attribute, which is typically added to a single image in each HTML response with a value of "high". This enhances load time performance (also Largest Contentful Paint, or LCP) by telling the browser to prioritize this image for downloading even before the layout of the page has been computed. In lab tests, this has shown to improve LCP performance by ~10% on average.

Specifically, `fetchpriority="high"` is added to the first image that satisfies all of the following conditions:
* The image is not lazy-loaded, i.e. does not have `loading="lazy"`.
* The image does not already have a (conflicting) `fetchpriority` attribute.
* The size of of the image (i.e. width * height) is greater than 50,000 squarepixels.

While these heuristics are based on several field analyses, there will always be room for optimization. Sites can customize the squarepixel threshold using a new filter `wp_min_priority_img_pixels` which should return an integer for the value.

Since the logic for adding `fetchpriority="high"` is heavily intertwined with the logic for adding `loading="lazy"`, yet the features should work decoupled from each other, the majority of code changes in this changeset is refactoring of the existing lazy-loading logic to be reusable. For this purpose, a new function `wp_get_loading_optimization_attributes()` has been introduced which returns an associative array of performance-relevant attributes for a given HTML element. This function replaces `wp_get_loading_attr_default()`, which has been deprecated. As another result of that change, a new function `wp_img_tag_add_loading_optimization_attrs()` replaces the more specific `wp_img_tag_add_loading_attr()`, which has been deprecated as well.

See https://make.wordpress.org/core/2023/05/02/proposal-for-enhancing-lcp-image-performance-with-fetchpriority/ for the original proposal and additional context.

Props thekt12, joemcgill, spacedmonkey, mukesh27, costdev, 10upsimon.
Fixes #58235.

Built from https://develop.svn.wordpress.org/trunk@56037


git-svn-id: http://core.svn.wordpress.org/trunk@55549 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-26 16:17:23 +00:00
Sergey Biryukov
ad16732d27 Code Modernization: Use str_contains() in a few more places.
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).

WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.

This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.

Follow-up to [55988], [56021].

See #58206.
Built from https://develop.svn.wordpress.org/trunk@56031


git-svn-id: http://core.svn.wordpress.org/trunk@55543 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-26 10:17:25 +00:00
Sergey Biryukov
84e9601e5a Code Modernization: Replace usage of substr() with str_starts_with() and str_ends_with().
`str_starts_with()` and `str_ends_with()` were introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins or ends with the given substring (needle).

WordPress core includes a polyfill for these functions on PHP < 8.0 as of WordPress 5.9.

This commit uses `str_starts_with()` and `str_ends_with()` in core files where appropriate:
* `$needle === substr( $string, 0, $length )`, where `$length` is the length of `$needle`, is replaced with `str_starts_with( $haystack, $needle )`.
* `$needle === substr( $string, $offset )`, where `$offset` is negative and the absolute value of `$offset` is the length of `$needle`, is replaced with `str_ends_with( $haystack, $needle )`.

This aims to make the code more readable and consistent, as well as better aligned with modern development practices.

Follow-up to [52039], [52040], [52326], [55703], [55710], [55987], [55988].

Props Soean, spacedmonkey, Clorith, ocean90, azaozz, sabernhardt, SergeyBiryukov.
Fixes #58220.
Built from https://develop.svn.wordpress.org/trunk@55990


git-svn-id: http://core.svn.wordpress.org/trunk@55502 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-22 14:57:24 +00:00
Sergey Biryukov
1ce5dc7444 Code Modernization: Replace usage of strpos() with str_contains().
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).

WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.

This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.

Follow-up to [52039], [52040], [52326], [55703], [55710], [55987].

Props Soean, spacedmonkey, costdev, dingo_d, azaozz, mikeschroder, flixos90, peterwilsoncc, SergeyBiryukov.
Fixes #58206.
Built from https://develop.svn.wordpress.org/trunk@55988


git-svn-id: http://core.svn.wordpress.org/trunk@55500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-22 14:36:26 +00:00
Sergey Biryukov
956be813b6 Coding Standards: Improve formatting of some SQL queries for better readability.
This corrects the placement of double quotes around the query and makes sure the alignment is consistent.

Props umeshmcakadi, mukesh27, krupalpanchal, dhrumilk, SergeyBiryukov.
Fixes #58372.
Built from https://develop.svn.wordpress.org/trunk@55857


git-svn-id: http://core.svn.wordpress.org/trunk@55369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-25 12:55:19 +00:00
Felix Arntz
c6f9238b77 Media: Conditionally skip lazy-loading on images before the loop to improve LCP performance.
When the logic to exclude images that likely appear above the fold from being lazy-loaded was introduced in WordPress 5.9, initially only images that appear within the main query loop were being considered. However, there is a good chance that images above the fold are rendered before the loop starts, for example in the header template part.

It is particularly common for a theme to display the featured image for a single post in the header. Based on HTTP Archive data from February 2023, the majority of LCP images that are still being lazy-loaded on WordPress sites use the `wp-post-image` class, i.e. are featured images.

This changeset enhances the logic in `wp_get_loading_attr_default()` to not lazy-load images that appear within or after the header template part and before the query loop, using a new `WP_Query::$before_loop` property.

For block themes, this was for the most part already addressed in [55318], however this enhancement implements the solution in a more generally applicable way that brings the improvement to classic themes as well.

Props thekt12, flixos90, spacedmonkey, costdev, zunaid321, mukesh27.
Fixes #58211.
See #53675, #56930.

Built from https://develop.svn.wordpress.org/trunk@55847


git-svn-id: http://core.svn.wordpress.org/trunk@55359 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-22 19:13:21 +00:00
Sergey Biryukov
cdc9d4766f Docs: Fix a few more typos in DocBlocks.
Follow-up to [39493], [41726], [55823], [55824], [55827].

See #57840.
Built from https://develop.svn.wordpress.org/trunk@55843


git-svn-id: http://core.svn.wordpress.org/trunk@55355 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-20 10:19:23 +00:00
Sergey Biryukov
046b9afcb7 Docs: Fix a few more typos in DocBlocks and inline comments.
Follow-up to [6779], [10565], [12023], [25224], [27533], [32806], [34777], [45262], [46594], [55823], [55824].

Props Presskopp.
See #57840.
Built from https://develop.svn.wordpress.org/trunk@55827


git-svn-id: http://core.svn.wordpress.org/trunk@55339 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-19 13:27:20 +00:00
Felix Arntz
2b28fea7c9 Media: Prevent special images within post content to skew image counts and cause lazy-loading bugs.
In order to skip lazy-loading the first few images on a page, as of WordPress 5.9 there has been logic to count images that are eligible based on certain criteria. One of those groups are images that appear within the content of a post.

This changeset fixes a bug where images created via `get_the_post_thumbnail()` or `wp_get_attachment_image()` that are injected into the post content would skew the count and therefore result in all images to be lazy-loaded, potentially hurting load time performance. This is relevant for example when those functions are called in server-side rendered blocks, or any other filter callbacks hooked into `the_content`.

Props flixos90, antpb, joedolson, spacedmonkey, mukesh27, thekt12, costdev, jrf.
Fixes #58089.
See #53675.

Built from https://develop.svn.wordpress.org/trunk@55825


git-svn-id: http://core.svn.wordpress.org/trunk@55337 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-18 19:55:23 +00:00
Felix Arntz
8db8a24e46 Media: Introduce wp_get_attachment_image_context filter.
Since WordPress 5.9, a "context" value of "wp_get_attachment_image" has been used in the `wp_get_attachment_image()` function to provide context to underlying functions where that is relevant, e.g. `wp_get_loading_attr_default()`. Since that value used to be not customizable, it required a workaround in `get_the_post_thumbnail()` to avoid calling those functions in `wp_get_attachment_image()`, which resulted in unnecessary complexity and was prone to errors.

This changeset introduces a `wp_get_attachment_image_context` filter and leverages it with private filter callback functions that are leveraged by default when `get_the_post_thumbnail()` is called. This avoids the need for the previous workaround and furthermore provides flexibility for other callers of `wp_get_attachment_image()` to provide their own contexts.

Props flixos90, costdev, thekt12, westonruter, spacedmonkey.
Fixes #58212.

Built from https://develop.svn.wordpress.org/trunk@55821


git-svn-id: http://core.svn.wordpress.org/trunk@55333 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-17 18:31:24 +00:00
Felix Arntz
1aeba57f4b Media: Increase default for wp_omit_loading_attr_threshold to 3.
The previous default threshold for how many content images to skip lazy-loading on (which was just 1) has proven to be too strict: HTTP Archive data shows that >70% of sites have up to 3 equal-sized images in the initial viewport, each of which could be the LCP image and therefore should not be lazy-loaded. Lazy-loading too many images has adverse effects on load time performance, while increasing the default threshold will not negatively affect load time performance even for sites where a threshold of 1 would be the perfect choice.

The change of default value in this changeset will improve performance for more WordPress sites out of the box. The `wp_omit_loading_attr_threshold` filter can still be used to customize and fine tune the value where needed.

Props thekt12, spacedmonkey, westonruter, flixos90.
Fixes #58213.

Built from https://develop.svn.wordpress.org/trunk@55816


git-svn-id: http://core.svn.wordpress.org/trunk@55328 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-16 18:52:23 +00:00