<?php
/**
 * Name: Robots.txt Manager
 * Description: Customize robots.txt file content directly from WordPress admin to control search engine crawler behavior, block directories, or add custom crawl rules easily
 * Category: Utilities
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "SEO Crawler Control",
 *     "description": "Customize robots.txt to block search engines from crawling admin, uploads, or specific directories to save crawl budget."
 *   },
 *   {
 *     "title": "Add XML Sitemap Reference",
 *     "description": "Add sitemap location to robots.txt for better search engine discovery without FTP access."
 *   },
 *   {
 *     "title": "Block Unwanted Bots",
 *     "description": "Specify custom bot rules to prevent aggressive crawlers from consuming server resources."
 *   }
 * ]
 * Added: 2025-07-01
 * Tags: robots, seo, crawlers, search-engines, txt
 * WhereToFind: {
 *   "location": "Configure in module settings",
 *   "url": "",
 *   "instructions": "Edit robots.txt content - live at /robots.txt",
 *   "newTab": false
 * }
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class SwitchboardRobotsTxt {
    
    public function __construct() {
        add_filter('robots_txt', [$this, 'custom_robots_txt'], 10, 2);
    }

    /**
     * Register data locations for this module
     */
    public function register_data_locations() {
        return [
            'options' => [
                'switchboard_robots-txt_settings'
            ],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => []
        ];
    }
    
    /**
     * Render settings panel for Robots.txt Manager
     *
     * Displays settings interface for customizing robots.txt file with:
     * - Large textarea for editing robots.txt content directly
     * - Clear labeling and helpful description about usage
     * - Simple, straightforward interface for SEO professionals
     *
     * The interface provides direct control over the site's robots.txt file without
     * requiring FTP access or server file editing. Users can customize crawler behavior,
     * add sitemap references, block specific directories, or control bot access. The
     * textarea accepts standard robots.txt syntax including User-agent, Disallow, Allow,
     * Sitemap, and Crawl-delay directives. Changes take effect immediately and override
     * WordPress's default robots.txt generation. This is essential for SEO optimization,
     * crawl budget management, and protecting sensitive areas from search engine indexing.
     * Common use cases include adding XML sitemap location, blocking admin/upload folders,
     * preventing aggressive bot crawling, and managing staging site visibility.
     *
     * @param array $settings Current module settings with 'content' key containing the robots.txt content
     * @return void Outputs HTML directly
     */
    public function render_settings($settings = []) {
        $content = isset($settings['content']) ? $settings['content'] : '';
        ?>
        <input type="hidden" name="module" value="robots-txt">

        <div class="sb-sp-group" style="margin-bottom: 24px;">
            <h3 class="sb-sp-heading"><?php esc_html_e('Robots.txt Manager', 'wpswitchboard'); ?></h3>
            <p class="sb-sp-description"><?php esc_html_e('Customize robots.txt file content directly from WordPress admin to control search engine crawler behavior, block directories, or add custom crawl rules easily.', 'wpswitchboard'); ?></p>
        </div>

        <div class="sb-sp-group" style="margin-bottom: 24px;">
            <label for="setting_content" class="sb-sp-label"><?php esc_html_e('Robots.txt Content', 'wpswitchboard'); ?></label>
            <textarea name="setting_content" id="setting_content" rows="12" class="sb-sp-textarea sb-sp-w-full" style="font-family: 'Courier New', monospace; font-size: 13px;"><?php echo esc_textarea($content); ?></textarea>
            <p class="sb-sp-description"><?php
            // translators: %s is the URL where robots.txt is accessible
            printf(esc_html__('Enter your custom robots.txt content. This will be accessible at %s', 'wpswitchboard'), '<code>' . esc_url(home_url('/robots.txt')) . '</code>'); ?></p>
        </div>

        <div class="sb-sp-info-box sb-sp-info-box-tip">
            <h4 class="sb-sp-heading-sm"><?php esc_html_e('Example Format', 'wpswitchboard'); ?></h4>
            <div style="background: #ffffff; border: 1px solid #e5e7eb; border-radius: 6px; padding: 12px; margin-top: 8px;">
                <code style="font-family: 'Courier New', monospace; font-size: 13px; color: #111827; display: block; white-space: pre-wrap; word-break: break-all;">User-agent: *
Disallow: /wp-admin/
Disallow: /wp-includes/
Allow: /wp-admin/admin-ajax.php

Sitemap: <?php echo esc_url(home_url('/sitemap.xml')); ?></code>
            </div>
        </div>

        <div class="sb-sp-info-box sb-sp-info-box-info" style="margin-top: 24px;">
            <h4 class="sb-sp-heading-sm"><?php esc_html_e('About Robots.txt', 'wpswitchboard'); ?></h4>
            <p class="sb-sp-description" style="margin: 0;">
                <?php esc_html_e('The robots.txt file tells search engines which parts of your site they can crawl. Use it to block admin areas, protect sensitive directories, add sitemap references, or control bot behavior. Changes take effect immediately and override WordPress\'s default robots.txt generation.', 'wpswitchboard'); ?>
            </p>
        </div>
        <?php
    }
    
    public function custom_robots_txt($output, $public) {
        $settings = get_option('switchboard_robots-txt_settings', []);
        $custom_content = isset($settings['content']) ? $settings['content'] : '';
        
        if (!empty($custom_content)) {
            return $custom_content;
        }
        
        return $output;
    }
}

// Initialize and register with data system
$switchboard_instance = new SwitchboardRobotsTxt();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
