API / Minn\Blocks / ImageTags

ImageTags final readonly class

Minn\Blocks\ImageTags · public/minn/src/Minn/Blocks/ImageTags.php · 312 lines

The attributes the reference adds to an <img> that carries a wp-image-{id} class: loading, decoding, width and height (prepended, in that order), and srcset plus sizes (appended). The srcset lists the displayed size first, then every same-ratio size in stored order, then the original.

Used by 1

Constants and properties

privatereadonly Minn\Content\Posts $posts

Promoted from the constructor.

privatereadonly Minn\Media\Uploads $uploads

Promoted from the constructor.

privatereadonly Closure $front

Promoted from the constructor.

Public

new__construct(Minn\Content\Posts $posts, Minn\Media\Uploads $uploads, Closure $front)#
$frontClosure(): boolwhether the images are on a front-end page, where the loading budget applies
methodenrich(string $html): string#

Rewrites every wp-image-* <img> in a fragment; other images pass through.

methodenrichGallery(string $html): string#

The same for a gallery: every image also carries its data-id.

methodenrichPlugin(string $html): string#

The same for a plugin block's output, where the reference adds no sizes="auto".

methodcontent(string $html, mixed $context = NULL): string#

Every <img> and <iframe> of a content fragment as the reference's wp_filter_content_tags leaves it: an attachment's image is enriched as above (a loading it already has is kept, and an eager large one is fetched first); any other image is decoded asynchronously and, when its size is known, loaded lazily or by the page's budget; an iframe with a size is loaded lazily.

methodfinished(string $html, string $context): string#

Content the engine fitted out itself, with what the_content's other callbacks added on the way (an attachment's own link, a plugin's image): an image still without its loading attributes is fitted as the rest were, then each is offered to wp_content_img_tag, as wp_filter_content_tags would.

methodfeatured(int $attachmentId, string $alt, string $style): string#

A post's featured image at full size, in the reference's attribute order (dimensions, source, class, alt, style, then the loading attributes and the srcset). Empty when the attachment has no file.

Internals 13

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privatesized(string $tag): string#

A template's image: an attachment's gets its srcset and sizes in place, nothing more; any other is left as it is.

privatefit(string $tag): string#

One content image fitted out: an attachment's (by its wp-image class) in full, any other with its loading attributes.

privateattachmentOf(string $tag): int#

The attachment an image names in its wp-image-{id} class, or 0.

privateplainImage(string $tag): string#

An image that is not an attachment's: decoding always, loading when its size is known and it has none.

privateautoSized(string $tag): string#

A lazy image's sizes led by "auto" (wp_img_tag_add_auto_sizes), unless they already are or a plugin says not to (probe rest-attachments-controller).

privatelazyFrame(string $tag): string#

An iframe with a width and a height, and no loading of its own, loads lazily.

privaterewrite(string $html, bool $withDataId, bool $autoSizes): string#
privateloadingPrefix(int $width = 0, int $height = 0): array#

On a page the first big-enough content image is fetched eagerly with high priority, the next few are eager, and the rest lazy; only lazy images carry the "auto" sizes hint. In a REST response every image is lazy.

privategivenLoading(string $loading, int $width, int $height): array#

An image that names its own loading keeps it: an eager one large enough is fetched first, every one is decoded asynchronously. @return array{0: string, 1: bool}

@return array{0: string, 1: bool}
privateminimumPriorityPixels(): int#

The area an image must cover before it is worth fetching first.

privateenrichTag(string $tag, int $attachmentId, bool $withDataId, bool $autoSizes): string#
privatecandidates(string $tag, int $attachmentId): ?array#

The srcset candidates for an attachment's image, keyed by width: the shown size first, then same-ratio sizes in stored order, then the original; with the shown width and height, the metadata and the src. Null when the image is not one of the attachment's files.

@return array{0: array<int, string>, 1: int, 2: int, 3: array<string, mixed>, 4: string}|null
privatesrcsetAttributes(array $candidates, int $width, bool $auto, int $attachmentId = 0, array $meta = array ( ), string $src = '', int $height = 0): string#

A srcset needs a choice: one candidate yields no srcset and no sizes. With the runtime up, plugin code filters the candidates and the sizes hint the way the reference lets it (wp_calculate_image_srcset, wp_calculate_image_sizes).