<?php
/**
 * SEO Optimizations and Rank Math Compatibility
 *
 * This file contains SEO-related theme functions that work
 * seamlessly with Rank Math SEO plugin without conflicts.
 *
 * @package ROI_Theme
 * @since 1.0.0
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Remove WordPress version from header and feeds
 *
 * Prevents disclosure of WordPress version number which could
 * expose potential vulnerabilities. This is a common SEO best practice.
 *
 * @since 1.0.0
 */
function roi_remove_generator() {
    return '';
}
remove_action('wp_head', 'wp_generator');
add_filter('the_generator', 'roi_remove_generator');

/**
 * Remove RSD (Really Simple Discovery) link
 *
 * Removes unnecessary header link that's rarely needed.
 *
 * @since 1.0.0
 */
remove_action('wp_head', 'rsd_link');

/**
 * Remove Windows Live Writer manifest
 *
 * Removes deprecated Microsoft Windows Live Writer support link.
 *
 * @since 1.0.0
 */
remove_action('wp_head', 'wlwmanifest_link');

/**
 * Remove REST API link from header
 *
 * Note: Rank Math handles REST API headers, so we keep REST API
 * itself enabled but remove the link tag from the header.
 * This prevents exposing API endpoints unnecessarily.
 *
 * @since 1.0.0
 */
remove_action('wp_head', 'rest_output_link_wp_head');

/**
 * Optimize robots.txt headers
 *
 * Ensures proper cache headers for robots.txt
 *
 * @since 1.0.0
 */
function roi_robots_header() {
    if (is_robots()) {
        header('Cache-Control: public, max-age=86400');
        header('Expires: ' . gmdate('r', time() + 86400));
    }
}
add_action('pre_handle_robots_txt', 'roi_robots_header');

/**
 * Improve comment feed performance
 *
 * Disables post comments feed if not needed (can be re-enabled
 * in theme options if required for client websites).
 *
 * @since 1.0.0
 */
// Note: Keep this commented out unless client specifically needs comment feeds
// remove_action('wp_head', 'feed_links', 2);

/**
 * Clean up empty image alt attributes
 *
 * Encourages proper image SEO by highlighting missing alt text in admin
 *
 * @since 1.0.0
 */
function roi_admin_notice_missing_alt() {
    if (!current_user_can('upload_files')) {
        return;
    }

    // This is informational - actual alt text enforcement is better
    // handled by Rank Math's image optimization features
}

/**
 * Ensure wp_head() is properly closed before body
 *
 * This is called in header.php to ensure all SEO meta tags
 * (from Rank Math and theme) are properly placed.
 *
 * @since 1.0.0
 */
function roi_seo_head_hooks() {
    // This ensures proper hook execution order for Rank Math compatibility
    do_action('roi_head_close');
}

/**
 * ELIMINADO: roi_prefetch_external()
 *
 * Motivo: Fuentes Poppins ahora son self-hosted (Assets/fonts/)
 * Ya no se necesita preconnect a Google Fonts
 *
 * @since 1.0.0 - Creado
 * @since 1.1.0 - Eliminado (self-hosted fonts)
 */

/**
 * Open Graph support for Rank Math compatibility
 *
 * Ensures theme doesn't output conflicting OG tags when Rank Math is active.
 * Rank Math handles all Open Graph implementation.
 *
 * @since 1.0.0
 */
function roi_check_rank_math_active() {
    return defined('RANK_MATH_VERSION');
}

/**
 * Schema.org compatibility layer
 *
 * Provides basic schema support if Rank Math is not active.
 * When Rank Math is active, it takes over all schema implementation.
 *
 * @since 1.0.0
 */
function roi_schema_fallback() {
    // Only output schema if Rank Math is NOT active
    if (roi_check_rank_math_active()) {
        return;
    }

    // Basic organization schema fallback
    $schema = array(
        '@context' => 'https://schema.org',
        '@type' => 'WebSite',
        'name' => get_bloginfo('name'),
        'url' => get_home_url(),
    );

    if (get_bloginfo('description')) {
        $schema['description'] = get_bloginfo('description');
    }

    echo "\n" . '<!-- ROI Theme Basic Schema (Rank Math not active) -->' . "\n";
    echo '<script type="application/ld+json">' . "\n";
    echo wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
    echo '</script>' . "\n";
}
add_action('wp_head', 'roi_schema_fallback', 20);

/**
 * Security headers configuration
 *
 * Adds recommended security headers that also improve SEO
 * (by indicating secure, well-maintained site)
 *
 * @since 1.0.0
 */
function roi_security_headers() {
    // These headers improve trust signals for search engines
    header('X-Content-Type-Options: nosniff');
    header('X-Frame-Options: SAMEORIGIN');
    header('X-XSS-Protection: 1; mode=block');
}
add_action('send_headers', 'roi_security_headers');

/**
 * Ensure title tag support is active
 *
 * This is set in functions.php with add_theme_support('title-tag')
 * but we verify it here to log any issues for debugging.
 *
 * @since 1.0.0
 */
function roi_verify_title_tag_support() {
    if (!current_theme_supports('title-tag')) {
        // Log warning if title-tag support is somehow disabled
        error_log('Warning: ROI Theme title-tag support not properly initialized');
    }
}
add_action('after_setup_theme', 'roi_verify_title_tag_support', 20);
