<?php

 /**
 * =============================================================================
 * @package         Blue Raven (Free Version)
 * @Plugin URI      https://enterrahost.com/plugins/blue-raven
 * 
 * @Author          EnterraHost
 * @Author URI      https://enterrahost.com
 * @License         GPL-2.0-or-later
 *  
 * @Copyright       (c) 2025 EnterraHost
 * @Contents        Plugin Tools > Robots.txt
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - BLUE RAVEN ADMIN TOOL ROBOTS
 * || 
 * =============================================================================
 */

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

/*
============================================================
    BLUE RAVEN ADMIN TOOL ROBOTS
============================================================
*/

/**
 * Class Blue_Raven_Admin_Tools_Robots
 * 
 * Handles the rendering and functionality of the robots.txt tools in the Blue Raven admin panel.
 */
class Blue_Raven_Admin_Tools_Robots {

    /**
     * Render the robots tools interface
     */
    public function render() {
        self::load_tools_robots_template();
    }
    
    /**
     * Load the robots tools template
     * 
     * @access private
     */
    private static function load_tools_robots_template() {
        $button_text = __('Save', 'blue-raven');
        
  		$robots_path = rtrim(sanitize_text_field(isset($_SERVER['DOCUMENT_ROOT']) ? wp_unslash($_SERVER['DOCUMENT_ROOT']) : ABSPATH), '/') . '/robots.txt';
        $robots_content = '';
        
        if (file_exists($robots_path) && is_readable($robots_path)) {
            $robots_content = file_get_contents($robots_path);
            if (false === $robots_content) {
                $robots_content = '';
            }
        } else {
            $button_text = __('Create A New', 'blue-raven');
            
            $robots_content = "User-agent: *\n";
            $robots_content .= "Disallow: /wp-admin/\n";
            $robots_content .= "Allow: /wp-admin/admin-ajax.php\n";
            
            $robots_content .= "Disallow: /wp-includes/\n";
            $robots_content .= "Disallow: /wp-content/plugins/\n";
            $robots_content .= "Disallow: /wp-content/themes/\n";
            $robots_content .= "Disallow: /wp-content/cache/\n";
            $robots_content .= "Disallow: /cgi-bin/\n";
            
            $robots_content .= "Disallow: /?s=\n";
            $robots_content .= "Disallow: /*?attachment_id=\n";
            
            $robots_content .= "Disallow: /*add-to-cart=\n";
            $robots_content .= "Disallow: /cart/\n";
            $robots_content .= "Disallow: /checkout/\n";
            $robots_content .= "Disallow: /my-account/\n";
            $robots_content .= "Disallow: /wishlist/\n";
            $robots_content .= "Disallow: /*orderby=\n";
            $robots_content .= "Disallow: /*filter_*\n";
            
            $robots_content .= "Allow: /wp-content/uploads/\n";
            $robots_content .= "Sitemap: " . esc_url(get_site_url()) . "/br-sitemap.xml\n";
        }

        ?>
            <div class="flex-sb">
                <h3><?php esc_html_e('Robots', 'blue-raven'); ?></h3>
                <h4>
                    <?php esc_html_e('Need Help', 'blue-raven'); ?>
                    <a href="<?php echo esc_url('https://portal.enterrahost.com/knowledgebase/2/'); ?>" 
                    target="_blank" 
                    rel="noopener noreferrer" 
                    style="margin-left: 8px;" 
                    title="<?php esc_attr_e('Learn more', 'blue-raven'); ?>">
                        <i class="fas fa-question-circle"></i>
                    </a>
                </h4>
            </div>
            
            <p class="enterra-highlight" style="padding: 0rem 1rem;">
                <?php esc_html_e('The robots.txt file is a simple text file placed at the root of your website (e.g., yourwebsite.com/robots.txt) that tells search engine crawlers (like Googlebot) which pages or sections of your site they\'re allowed to crawl or ignore.', 'blue-raven'); ?>
            </p>
            
            <div style="margin: 20px 0;">
                <h3><?php esc_html_e('robots.txt Content', 'blue-raven'); ?></h3>
                <?php if (!file_exists($robots_path)): ?> <p style="margin: 1rem;"><?php esc_html_e('It seems you do not have a robots.txt file, this is our template:', 'blue-raven'); ?></p><?php endif; ?>
                <textarea class="br-text-area" id="robots-text-area" style="width: 100%; height: 600px;"><?php  echo esc_textarea($robots_content);  ?></textarea>
            </div>
            
            <div style="text-align: right;"><button class="enterra-button" data-tool="robots" id="save-robots-txt"><?php echo esc_html($button_text); ?> robots.txt </button></div>
        <?php
    }
}


