<?php
/**
 * Plugin Name: TA Relink Pricing (MU)
 * Description: Rewrites all ThirstyAffiliates pricing/upgrade links in WP Admin to a custom URL.
 * Author: You
 * Version: 1.0
 */

// Your custom destination:
if ( ! defined( 'TA_CUSTOM_PRICING_URL' ) ) {
    define( 'TA_CUSTOM_PRICING_URL', 'https://www.masteryace.com/go/thirsty' );
}

/**
 * Replace any admin sidebar/topbar/content anchor that points to
 * thirstyaffiliates.com/pricing... with TA_CUSTOM_PRICING_URL.
 * Works for static HTML and dynamically injected elements.
 */
add_action( 'admin_print_footer_scripts', function () {
    ?>
    <script>
    (function () {
        var target = <?php echo json_encode( TA_CUSTOM_PRICING_URL ); ?>;
        var selector = 'a[href*="thirstyaffiliates.com/pricing"]';

        function swap(root) {
            try {
                root.querySelectorAll(selector).forEach(function (a) {
                    a.setAttribute('href', target);
                    a.setAttribute('target', '_blank'); // keep users on admin
                    a.setAttribute('rel', 'noopener noreferrer');
                });
            } catch (e) {}
        }

        // Initial pass
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', function () { swap(document); });
        } else {
            swap(document);
        }

        // Catch dynamic injections (sidebars, notices, SPA-like admin UIs)
        var mo = new MutationObserver(function (mutations) {
            mutations.forEach(function (m) {
                m.addedNodes && m.addedNodes.forEach(function (n) {
                    if (n.nodeType === 1) swap(n);
                });
            });
        });
        try {
            mo.observe(document.documentElement, { childList: true, subtree: true });
        } catch (e) {}
    })();
    </script>
    <?php
}, 9999);
