API / Minn\Blocks / ImageTags

ImageTags final readonly class

Minn\Blocks\ImageTags · public/minn/src/Minn/Blocks/ImageTags.php · 446 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 for the context named (probe content-tags): whether iframes may load lazily asked first; each tag fitted out once, however often it appears. An attachment's image (a wp-image class) is given the width and height it lacks and its srcset and sizes; every image the loading, decoding and priority its context earns (wp_get_loading_optimization_attributes), "auto" sizes when lazy, then wp_content_img_tag. An iframe that names no loading of its own is given its loading, while iframes may load lazily.

staticoptimized(string $image, mixed $context): string#

An <img> given what it lacks of decoding, loading and fetchpriority, as the context earns them (wp_img_tag_add_loading_optimization_attrs): an image with no source given none of them; decoding through wp_img_tag_add_decoding_attr; for an image that writes a width and a height, lazy loading asked, then a loading through wp_img_tag_add_loading_attr (lazy or eager as given, any other answer keeping the loading the context earned, a false one none); then a high priority. Each is placed straight after "<img", so the last added reads first.

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 20

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

privatecontentImage(string $tag, mixed $context): string#

One content image fitted out as wp_filter_content_tags fits it, then through wp_content_img_tag.

privateframe(string $iframe, mixed $context): string#

An iframe given the loading its context earns (wp_iframe_tag_add_loading_attr), when it has a width and a height.

privatedimensioned(string $tag, int $id): string#

An attachment's image given the width and height its source names, when it is one of the attachment's files.

privatelazySizes(string $tag): string#

A lazy image's sizes led by "auto", unless they already are.

privateattributeOf(string $tag, string $name): ?string#

An attribute's value as written after a space, or null: its name in lower case (any case for src), as the reference reads it.

privatesizeOf(string $tag): array#

An image's width and height, each as a whole number when it is written as one. @return array{width: ?int, height: ?int}

@return array{width: ?int, height: ?int}
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).

privateautoSizedWhenLazy(string $tag): string#

An image a plugin made lazy after it was fitted (Elementor's image loading does) gets "auto" sizes on the next pass, as on the reference.

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. A plugin that turns lazy loading off (wp_lazy_loading_enabled; Elementor's image loading does) leaves every image without it.

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}
privatelazyAllowed(string $tagName): bool#

Whether content images or iframes may load lazily (wp_lazy_loading_enabled, asked for the_content).

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).