<?php
/**
 * REEID — Canonical authority guard
 * Ensures ONLY seo-sync.php outputs canonical.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
add_action( 'wp', function () {

    // Remove WordPress core canonical
    remove_action( 'wp_head', 'rel_canonical' );

    // Remove any REEID canonical emitters except seo-sync
    $hooks = $GLOBALS['wp_filter']['wp_head'] ?? null;
    if ( ! $hooks ) {
        return;
    }

    foreach ( $hooks as $priority => $callbacks ) {
        foreach ( $callbacks as $cb ) {
            if (
                is_array( $cb )
                && isset( $cb['function'] )
                && is_string( $cb['function'] )
                && str_contains( $cb['function'], 'canonical' )
                && $cb['function'] !== 'reeid_output_hreflang_tags_seosync'
            ) {
                remove_action( 'wp_head', $cb['function'], $priority );
            }
        }
    }
}, 1 );
