<?php
/**
 * "Always HD" for the Kadence Image (Adv) block  —  kadence/image.
 *
 * THE PROBLEM
 *   Kadence saves the full-size file in the block's <img src> (e.g.
 *   `…-_a7r3583.jpg.webp`, 1152w). But at render time WordPress core injects a
 *   responsive `srcset` with every generated size PLUS a `sizes="auto, …"`
 *   attribute (the WP 6.7 "auto-sizes" feature for lazy images). `sizes=auto`
 *   measures the laid-out width of the image and, inside Brentwood's narrow
 *   ratio / "Cover (fill column)" columns (e.g. the 4-up row on /academics/),
 *   it resolves small and the browser downloads the `-300x230` candidate. The
 *   `is-style-bw-cover` object-fit:cover box then upscales that 300px file to
 *   fill a much larger area → visibly blurry photos.
 *
 * THE FIX (default ON for every Image (Adv) block — no per-block toggle)
 *   Before core adds its responsive srcset, we pin the <img> to a SINGLE,
 *   descriptor-less `srcset` candidate pointing at the exact full-size URL the
 *   block already references. Two things happen:
 *     1. A one-URL srcset (no `w`/`x` descriptor) is *always* the browser's
 *        pick, so the full-resolution file loads regardless of `sizes`.
 *     2. Core's `wp_image_add_srcset_and_sizes()` skips any <img> that already
 *        carries a `srcset`, so it never re-introduces the small candidates.
 *   `sizes="auto"` may still be added, but with a single candidate it has
 *   nothing to choose from — the HD image wins.
 *
 * Scope: only `kadence/image` blocks (the "Image (Adv)" block). Other images
 * (content <img>, other blocks) keep normal responsive behaviour. Runs on
 * `render_block`, i.e. before core's `wp_filter_content_tags()` at
 * the_content priority 12, so our srcset is in place first.
 *
 * @package Kadence-Child
 */

defined( 'ABSPATH' ) || exit;

/**
 * Pin every <img> in a kadence/image block to its full-size src via a
 * single-candidate srcset, so the browser always loads the HD file.
 *
 * @param string $block_content Rendered block HTML.
 * @param array  $block         Parsed block (name + attrs).
 * @return string
 */
function bw_image_adv_force_full( $block_content, $block ) {
	if ( empty( $block['blockName'] ) || 'kadence/image' !== $block['blockName'] ) {
		return $block_content;
	}
	if ( false === strpos( $block_content, '<img' ) ) {
		return $block_content;
	}

	return preg_replace_callback(
		'/<img\b[^>]*>/i',
		function ( $m ) {
			$img = $m[0];

			// Already has a srcset (hand-authored, or a variant) — leave it be.
			if ( preg_match( '/\ssrcset\s*=/i', $img ) ) {
				return $img;
			}
			// Need a usable src to pin to.
			if ( ! preg_match( '/\ssrc\s*=\s*(["\'])(.*?)\1/i', $img, $sm ) ) {
				return $img;
			}
			$url = trim( $sm[2] );
			// Only pin real uploaded images (skip data URIs / empty).
			if ( '' === $url || 0 === strpos( $url, 'data:' ) ) {
				return $img;
			}

			// Inject a single, descriptor-less candidate = the full-size image.
			return preg_replace(
				'/<img\b/i',
				'<img srcset="' . esc_url( $url ) . '"',
				$img,
				1
			);
		},
		$block_content
	);
}
add_filter( 'render_block', 'bw_image_adv_force_full', 10, 2 );
