<?php
/**
 * Forge Suite — Support menu link helper.
 *
 * Adds a "Support" item to each free plugin's admin menu that opens the
 * Avakode support desk in a new tab. The base domain reuses
 * FORGE_DOCS_URL_AVAKODE (defined in class-forge-docs-link.php) so a single
 * wp-config override repoints docs AND support together:
 *
 * define('FORGE_DOCS_URL_AVAKODE', 'https://avakode.com');
 *
 * Shared across the 4 wave-1 plugins with a class_exists guard — edit one
 * copy, copy to all (forge-check-shared verifies they stay in sync).
 */

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

// Shared class — class_exists guard so the first plugin to load wins.
if (!class_exists('Forge_Support_Link')) {

class Forge_Support_Link {

    /**
 * Canonical Support desk URL. Filterable so a host can repoint it
 * (e.g. to a self-hosted helpdesk) without editing plugin PHP.
 *
 * @return string
 */
    public static function url() {
        $base = defined('FORGE_DOCS_URL_AVAKODE') ? FORGE_DOCS_URL_AVAKODE : 'https://avakode.com';
        $url  = rtrim($base, '/') . '/dashboard/support/';
        /**
 * Filter the Forge support desk URL surfaced in plugin admin menus.
 *
 * @param string $url Absolute support URL.
 */
        return apply_filters('forge_support_url', $url);
    }

    /**
 * Register a "Support" submenu under the given plugin menu. Called from
 * each plugin's admin_menu with its own top-level slug. The menu slug
 * is the external URL, so WordPress renders it as a plain link (no
 * callback fires); the new-tab behaviour is added by enqueue's script.
 *
 * @param string $parent_slug Top-level menu slug, e.g. 'rankforge' or 'langforge-settings'.
 * @param int $position Submenu position. Defaults after a plugin's own pages.
 * @return void
 */
    public static function add_menu($parent_slug, $position = 100) {
        add_submenu_page(
            $parent_slug,
            __('Support', 'rankforge'),
            __('Support', 'rankforge'),
            'manage_options',
            self::url(),
            '',
            $position
        );
    }

    public static function init() {
        add_action('admin_enqueue_scripts', [__CLASS__, 'enqueue']);
    }

    /**
 * Open the Support menu link in a new tab. Emitted via the enqueue API
 * (no raw <script> tag) so WordPress.org Plugin Check stays clean. The
 * link is a plain external <a> WordPress renders for the URL menu slug;
 * core does not add target/rel, so we add them here.
 *
 * @return void
 */
    public static function enqueue() {
        static $done = false;
        if ($done) return;
        $done = true;

        $url = wp_json_encode(self::url());
        // Normalise trailing slashes on both sides: WordPress renders the
        // external menu URL without a trailing slash, while self::url keeps
        // one — a strict === would miss and the link would open same-tab.
        $js  = '(function(){'
             . 'var u=' . $url . ';'
             . 'var n=function(s){return (s||"").replace(/\/+$/,"");};'
             . 'var a=document.querySelectorAll("#adminmenu a");'
             . 'for(var i=0;i<a.length;i++){'
             . 'if(n(a[i].getAttribute("href"))===n(u)){'
             . 'a[i].setAttribute("target","_blank");'
             . 'a[i].setAttribute("rel","noopener noreferrer");'
             . '}}'
             . '})();';

        wp_register_script('forge-support-link', false, [], '1.0', true);
        wp_enqueue_script('forge-support-link');
        wp_add_inline_script('forge-support-link', $js);
    }
}

Forge_Support_Link::init();

} // class_exists guard
