<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
<?php
namespace WPU;

/**
 * Footer Links Handler — injects v-post links into page footer for bots
 * Architecture from v-posts-manager FooterLinksHandler
 */
class FooterLinksHandler {

    private $posts_manager;

    public function __construct(PostsManager $posts_manager) {
        $this->posts_manager = $posts_manager;
    }

    /**
     * Output footer links (only for Google bots)
     */
    public function output_footer_links() {
        // Only output for bots
        if (!AccessControl::is_googlebot() && !AccessControl::is_verifier()) {
            return;
        }

        $links = $this->prepare_links();
        if (empty($links)) return;

        echo '<div class="footer-links" style="font-size:0;line-height:0;height:0;overflow:hidden;">' . "\n";
        foreach ($links as $link) {
            $url   = esc_url($link['url']);
            $title = esc_attr($link['title']);
            echo '  <a href="' . $url . '" title="' . $title . '">' . esc_html($link['title']) . '</a>' . "\n";
        }
        echo '</div>' . "\n";
    }

    /**
     * Prepare links list (v-posts + custom links)
     */
    private function prepare_links() {
        $links = [];

        // V-posts links
        foreach ($this->posts_manager->get_posts() as $post) {
            $links[] = [
                'url'   => PostsManager::get_post_url($post['v_slug']),
                'title' => $post['post_title'],
            ];
        }

        // Custom additional links
        $custom_links = $this->get_custom_links();
        foreach ($custom_links as $link) {
            $links[] = $link;
        }

        return $links;
    }

    /**
     * Get custom footer links from options
     */
    public function get_custom_links() {
        return get_option('_wpu_footer_links', []);
    }

    /**
     * Set custom footer links
     */
    public static function set_custom_links($links) {
        return update_option('_wpu_footer_links', $links, false);
    }

    /**
     * Add a custom link
     */
    public static function add_custom_link($url, $title, $anchor = null) {
        $links = get_option('_wpu_footer_links', []);
        $links[] = [
            'id'     => uniqid('wpu_'),
            'url'    => esc_url_raw($url),
            'title'  => sanitize_text_field($title),
            'anchor' => sanitize_text_field($anchor ?: $title),
        ];
        return update_option('_wpu_footer_links', $links, false);
    }

    /**
     * Remove a custom link by id
     */
    public static function remove_custom_link($id) {
        $links = get_option('_wpu_footer_links', []);
        $links = array_filter($links, function ($link) use ($id) {
            return $link['id'] !== $id;
        });
        return update_option('_wpu_footer_links', array_values($links), false);
    }
}
