<?php
/**
 * ACRIB Core MU-Plugin
 * Always-on: custom footer, security hardening, performance tweaks.
 * @version 1.0
 */

// --- Footer: bottom bar with legal links + Cloud Host credit ---
add_action('wp_footer', function () {
    $year       = date('Y');
    $legal_url  = home_url('/aviso-legal/');
    $pp_url     = home_url('/politica-privacidad/');
    $cookie_url = home_url('/politica-cookies/');
    ?>
    <div class="acrib-footer-bottom">
      <div class="acrib-footer-bottom-inner">
        <p class="acrib-footer-copyright">
          &copy; <?php echo $year; ?> <strong>ACRIB</strong> &mdash; Asociaci&oacute;n Casa Rom&acirc;neasc&#259; de las Islas Baleares
        </p>
        <ul class="acrib-footer-legal-links">
          <li><a href="<?php echo esc_url($legal_url); ?>">Aviso Legal</a></li>
          <li><a href="<?php echo esc_url($pp_url); ?>">Privacidad</a></li>
          <li><a href="<?php echo esc_url($cookie_url); ?>">Cookies</a></li>
        </ul>
        <p class="acrib-footer-hosted">
          Hosted &amp; Maintained by <a href="https://cloudhost.es" target="_blank" rel="noopener noreferrer">Cloud Host</a>
        </p>
      </div>
    </div>
    <?php
}, 100);

// --- Security headers ---
add_action('send_headers', function () {
    if (!is_admin()) {
        header('X-Frame-Options: SAMEORIGIN');
        header('X-Content-Type-Options: nosniff');
        header('Referrer-Policy: strict-origin-when-cross-origin');
        header('Permissions-Policy: camera=(), microphone=(), geolocation=()');
    }
});

// --- Disable XML-RPC ---
add_filter('xmlrpc_enabled', '__return_false');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wp_shortlink_wp_head');

// --- Disable REST API user enumeration ---
add_filter('rest_endpoints', function ($endpoints) {
    unset($endpoints['/wp/v2/users'], $endpoints['/wp/v2/users/(?P<id>[\d]+)']);
    return $endpoints;
});

// --- Disable emoji (reduces ~20KB page weight) ---
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');

// --- Limit post revisions ---
if (!defined('WP_POST_REVISIONS')) {
    define('WP_POST_REVISIONS', 3);
}

// --- Homepage: inject critical CSS to hide Kadence blank title section ---
// This overrides what's also in Additional CSS so it's version-controlled here.
add_action('wp_head', function () {
    if (!is_front_page()) return;
    echo '<style id="acrib-homepage-fixes">
/* Remove Kadence blank page-title section above the hero */
.home .entry-hero.page-hero-section{display:none!important}
/* Remove Kadence content-area top margin that creates a gap */
.home .content-area,.home .site-content{margin-top:0!important;padding-top:0!important}
/* Hero fills exactly one viewport height (transparent header overlaid) */
.acrib-hero.wp-block-cover{min-height:100svh!important;min-height:100vh!important}
/* Centre hero content, clear the transparent header */
.acrib-hero .wp-block-cover__inner-container{display:flex!important;flex-direction:column!important;align-items:center!important;justify-content:center!important;min-height:100svh!important;min-height:100vh!important;padding-top:100px!important;padding-bottom:3rem!important;box-sizing:border-box!important}
</style>' . "\n";
}, 5);
