` met de hand op en komen * daardoor niet langs `wp_get_attachment_image_attributes`. En een blok dat in * een blokthema-template staat in plaats van in de post-content, komt niet * langs `wp_content_img_tag` — want `wp_filter_content_tags()` draait alleen op * de content zelf. * * `render_block` vangt beide gevallen. Het filter draait op statische én * dynamische blokken; de `is_dynamic`-check in WordPress staat eromheen, niet * erin. * * ── Alleen waar het ID vaststaat ──────────────────────────────────────────── * * We kijken uitsluitend naar de blokattributen, want daar staat het * attachment-ID dat de redacteur zelf heeft gekozen. Geen gok op basis van de * bestandsnaam in de `src`, geen zoekopdracht in de mediabibliotheek. Staat het * ID er niet in — `core/cover` met `useFeaturedImage`, bijvoorbeeld — dan doen * we niets en vangt route 1 het meestal alsnog op. * * @package Yinga\Focus */ declare( strict_types = 1 ); namespace Yinga\Focus\Frontend; defined( 'ABSPATH' ) || exit; final class BlockFilters { /** * De blokken die hun eigen `` schrijven, met de attribuutnaam waarin * het attachment-ID staat. * * @var array */ private const BLOCKS = array( 'core/image' => 'id', 'core/cover' => 'id', 'core/media-text' => 'mediaId', ); private ApplyPolicy $policy; /** * @param ApplyPolicy $policy De policy. */ public function __construct( ApplyPolicy $policy ) { $this->policy = $policy; } /** * Haakt aan. */ public function register(): void { add_filter( 'render_block', array( $this, 'block' ), 20, 2 ); } /** * @param mixed $content De gerenderde markup van het blok. * @param mixed $block De blokdefinitie. * @return mixed */ public function block( $content, $block = null ) { if ( ! is_string( $content ) || '' === $content || ! is_array( $block ) ) { return $content; } $name = isset( $block['blockName'] ) ? (string) $block['blockName'] : ''; // Dit draait voor élk blok op de pagina. De eerste controle moet dus // goedkoop zijn: één array-lookup, geen parsing. if ( ! isset( self::BLOCKS[ $name ] ) ) { return $content; } $key = self::BLOCKS[ $name ]; $attrs = isset( $block['attrs'] ) && is_array( $block['attrs'] ) ? $block['attrs'] : array(); $id = isset( $attrs[ $key ] ) ? (int) $attrs[ $key ] : 0; if ( $id <= 0 ) { return $content; } // `core/image` en `core/cover` bewaren het gekozen formaat als slug. // Dat is precies wat de hard-crop-controle nodig heeft. $size = isset( $attrs['sizeSlug'] ) && is_string( $attrs['sizeSlug'] ) && '' !== $attrs['sizeSlug'] ? $attrs['sizeSlug'] : null; return Markup::apply( $content, $id, $this->policy, $size ); } /** * De ondersteunde blokken. Voor tests en introspectie. * * @return array */ public static function blocks(): array { return self::BLOCKS; } }