<?php
/**
 * REEID — Woo Inline Product Sitemap: SEO plugin compatibility hooks (INSULATED)
 *
 * Goal:
 * - Ensure /reeid-wc-product-sitemap.xml is discoverable in popular SEO plugin sitemap indexes
 * - Always keep universal discovery via robots.txt (handled elsewhere)
 * - Never touch canonical/hreflang for normal pages
 */

defined( 'ABSPATH' ) || exit;
// Deprecated: consolidated into includes/seo-sitemaps-compat.php
return;



/** Shared helper */
if ( ! function_exists( 'reeid_wc_sitemap_loc' ) ) {
    function reeid_wc_sitemap_loc(): string {
        return (string) home_url( '/reeid-wc-product-sitemap.xml' );
    }
}

/* ------------------------------
 * Yoast SEO
 * ------------------------------ */
add_filter( 'wpseo_sitemap_index', function( $xml ) {

    if ( ! defined( 'WPSEO_VERSION' ) ) {
        return $xml;
    }

    $loc = reeid_wc_sitemap_loc();

    if ( is_string( $xml ) && strpos( $xml, $loc ) !== false ) {
        return $xml;
    }

    $xml .= "\n<sitemap>\n";
    $xml .= '  <loc>' . esc_url( $loc ) . "</loc>\n";
    $xml .= '  <lastmod>' . esc_html( gmdate( 'c' ) ) . "</lastmod>\n";
    $xml .= "</sitemap>\n";

    return $xml;
}, 50 );

/* ------------------------------
 * SEOPress
 * ------------------------------ */
add_filter( 'seopress_sitemaps_xml_index', function( $xml ) {

    // SEOPress constant (safe presence check)
    if ( ! defined( 'SEOPRESS_VERSION' ) && ! defined( 'SEOPRESS_PRO_VERSION' ) ) {
        return $xml;
    }

    $loc = reeid_wc_sitemap_loc();

    if ( is_string( $xml ) && strpos( $xml, $loc ) !== false ) {
        return $xml;
    }

    $xml .= "\n<sitemap>\n";
    $xml .= '  <loc>' . esc_url( $loc ) . "</loc>\n";
    $xml .= '  <lastmod>' . esc_html( gmdate( 'c' ) ) . "</lastmod>\n";
    $xml .= "</sitemap>\n";

    return $xml;
}, 50 );

/* ------------------------------
 * All in One SEO (AIOSEO)
 * ------------------------------ */
add_filter( 'aioseo_sitemap_indexes', function( $indexes ) {

    // AIOSEO is present only when its main class exists
    if ( ! class_exists( '\AIOSEO\Plugin\Common\Main\Main', false ) && ! defined( 'AIOSEO_VERSION' ) ) {
        return $indexes;
    }

    $loc = reeid_wc_sitemap_loc();

    foreach ( (array) $indexes as $idx ) {
        if ( is_array( $idx ) && ! empty( $idx['loc'] ) && (string) $idx['loc'] === $loc ) {
            return $indexes;
        }
    }

    $indexes[] = array(
        'loc'     => $loc,
        'lastmod' => gmdate( 'c' ),
        'count'   => 0,
    );

    return $indexes;
}, 50 );
