<?php

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

/**
 * REEID — Woo Inline Product Sitemap: Rank Math sitemap index injection (INSULATED)
 *
 * Goal:
 * - Ensure /sitemap_index.xml includes our REEID WC sitemap for discovery on Rank Math sites.
 *
 * Safety:
 * - Touches ONLY Rank Math sitemap index generation via Rank Math filter hooks.
 * - Does NOT touch normal pages SEO (canonical/hreflang) at all.
 * - Dedupes if already present.
 */



// Deprecated: consolidated into includes/seo-sitemaps-compat.php
return;

/**
 * Insert our sitemap entry into Rank Math sitemap index "links" list.
 * Rank Math versions vary, so we register on the most common hooks used in Generator.
 */
function reeid_rm_add_wc_sitemap_to_index_links( $links ) {

    if ( ! is_array( $links ) ) {
        return $links;
    }

    $loc = (string) home_url( '/reeid-wc-product-sitemap.xml' );
    if ( $loc === '' ) {
        return $links;
    }

    // Deduplicate: if any existing link already points to our sitemap, do nothing.
    foreach ( $links as $item ) {
        if ( is_string( $item ) && strpos( $item, $loc ) !== false ) {
            return $links;
        }
        if ( is_array( $item ) && ! empty( $item['loc'] ) && (string) $item['loc'] === $loc ) {
            return $links;
        }
    }

    // Prefer array format if RankMath uses structured entries.
    $links[] = array(
        'loc'     => $loc,
        'lastmod' => gmdate( 'c' ),
    );

    return $links;
}

// Common Rank Math hooks (safe to register multiple; only the ones used will fire).
add_filter( 'rank_math/sitemap/index/links', 'reeid_rm_add_wc_sitemap_to_index_links', 50, 1 );
add_filter( 'rank_math/sitemap/index',       'reeid_rm_add_wc_sitemap_to_index_links', 50, 1 );
add_filter( 'rank_math/sitemap/index/items', 'reeid_rm_add_wc_sitemap_to_index_links', 50, 1 );
