<?php
/**
 * Per-post option to hide the title.
 *
 * Stores a boolean meta (_audace_hide_title) editable in the document sidebar.
 * On render, the core/post-title block returns empty when the meta is true.
 */

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

add_action( 'init', function () {
    foreach ( array( 'post', 'page' ) as $type ) {
        register_post_meta( $type, '_audace_hide_title', array(
            'type'              => 'boolean',
            'single'            => true,
            'default'           => false,
            'show_in_rest'      => true,
            'auth_callback'     => function () { return current_user_can( 'edit_posts' ); },
            'sanitize_callback' => 'rest_sanitize_boolean',
        ) );
    }
} );

add_filter( 'render_block_core/post-title', function ( $block_content, $block ) {
    $post_id = $block['context']['postId'] ?? get_the_ID();
    if ( $post_id && get_post_meta( $post_id, '_audace_hide_title', true ) ) {
        // Marker so any parent container holding only the title can be hidden via CSS :has().
        return '<div class="audace-hidden-title" hidden aria-hidden="true"></div>';
    }
    return $block_content;
}, 10, 2 );

add_filter( 'body_class', function ( $classes ) {
    if ( is_singular() && get_post_meta( get_the_ID(), '_audace_hide_title', true ) ) {
        $classes[] = 'audace-title-hidden';
    }
    return $classes;
} );
