<?php
/**
 * Audace — propagate the core/site-logo block's `width` attribute to the
 * rendered <img>.
 *
 * WP stores the user-configured logo width as a block attribute but, for
 * SVG logos, never outputs it as a width attribute or inline style on the
 * image — so the browser renders an SVG with no intrinsic dimensions as
 * 0 × 0. This filter re-injects `width` / `height="auto"` on the <img>.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_filter( 'render_block_core/site-logo', function ( $block_content, $block ) {
    if ( empty( $block['attrs']['width'] ) ) {
        return $block_content;
    }
    $w = (int) $block['attrs']['width'];
    if ( $w <= 0 ) {
        return $block_content;
    }

    // Only touch <img class="custom-logo">, and only if it doesn't already
    // carry an explicit width attribute.
    $block_content = preg_replace_callback(
        '/<img\b([^>]*\bclass="[^"]*\bcustom-logo\b[^"]*"[^>]*)>/i',
        function ( $m ) use ( $w ) {
            $attrs = $m[1];
            if ( preg_match( '/\swidth\s*=\s*"[^"]+"/i', $attrs ) ) {
                return $m[0];
            }
            return '<img' . $attrs . ' width="' . $w . '" height="auto" style="width:' . $w . 'px;height:auto">';
        },
        $block_content,
        1
    );

    return $block_content;
}, 10, 2 );
