<?php

/**
 * Injection aria-hidden sur les images décoratives
 */
function custom_add_aria_hidden_to_image( $block_content, $block ) {

    if ( $block['blockName'] !== 'core/image' ) {
        return $block_content;
    }

    if ( empty( $block['attrs']['ariaHidden'] ) ) {
        return $block_content;
    }

    if ( false === strpos( $block_content, '<img' ) ) {
        return $block_content;
    }

    // Injection propre et contrôlée
    $block_content = preg_replace_callback(
        '/<img[^>]+>/',
        function( $matches ) {

            $img = $matches[0];

            // Supprime alt existant
            $img = preg_replace( '/alt="[^"]*"/', '', $img );

            // Ajoute alt="" et aria-hidden
            $img = rtrim( $img, '>' );
            $img .= ' alt="" aria-hidden="true">';

            return $img;
        },
        $block_content
    );

    return $block_content;
}

add_filter( 'render_block', 'custom_add_aria_hidden_to_image', 10, 2 );