<?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 > Main Meta
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - BLUE RAVEN ADMIN TOOL MAIN META
 * || 
 * =============================================================================
 */

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

/*
============================================================
    BLUE RAVEN ADMIN TOOL MAIN META
============================================================
*/

/**
 * Class Blue_Raven_Admin_Tools_Main_Meta
 * 
 * Handles the rendering and functionality of the main meta tools in the Blue Raven admin panel.
 */
class Blue_Raven_Admin_Tools_Main_Meta {

    /**
     * Render the main meta tools interface
     */
    public function render() {
        $this->load_tools_main_meta_template();
    }
    
    /**
     * Load the main meta tools template
     * 
     * @access private
     */
    private function load_tools_main_meta_template() {
        
    ## =========== get_pages =========== ##    
        
        $pages = get_pages(array(
            'post_status' => 'publish',
            'hierarchical' => false
        ));
        
        $pages_data = array();
        
        foreach ($pages as $page) {
            
            ///-- Get meta descriptions from various SEO plugins with fallbacks
            
            $meta_description = get_post_meta($page->ID, '_blue_raven_meta_description', true);
            $meta_keywords = get_post_meta($page->ID, '_blue_raven_meta_tags', true);
            $rank_math_desc = get_post_meta($page->ID, '_rank_math_description', true);
            $yoast_desc = get_post_meta($page->ID, '_yoast_wpseo_metadesc', true);
            
            ///-- Determine which description to use with proper fallback
            
            $final_meta_description = '';
            if (!empty($meta_description)) {
                $final_meta_description = $meta_description;
            } elseif (!empty($rank_math_desc)) {
                $final_meta_description = $rank_math_desc;
            } elseif (!empty($yoast_desc)) {
                $final_meta_description = $yoast_desc;
            }
            
            $pages_data[] = array(
                'ID' => $page->ID,
                'title' => esc_html($page->post_title),
                'url' => esc_url(get_permalink($page->ID)),
                'has_meta_desc' => !empty($final_meta_description),
                'meta_description' => esc_html($final_meta_description),
                'meta_keywords' => esc_html($meta_keywords)
            );
        }
        
    ## =========== Get Values from the table =========== ##    
    
        global $wpdb;
        
        $main_meta_enabled = $wpdb->get_var( $wpdb->prepare( "SELECT enabled FROM {$wpdb->prefix}BlueRavenValues  WHERE setting LIKE %s",'main-site-meta%') );
        
    ## =========== HTML =========== ##    
    
        ?>
        <div class="flex-sb">
            <h3><?php esc_html_e('Main Meta', 'blue-raven'); ?></h3>
            <h4>
                <?php esc_html_e('Need Help', 'blue-raven'); ?>
                <a href="<?php echo esc_url('https://portal.enterrahost.com/knowledgebase/8/'); ?>" 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 indent-one"><?php esc_html_e('Meta descriptions are short summaries of a page\'s content that appear under your page title in search engine results, helping to improve click-through rates and giving users (and search engines) a clear idea of what the page is about.', 'blue-raven'); ?></p>
        
        <section>
            <div style="margin: 20px 0;" class="border-bottom-white flex-right">
                <h4 class="no-margin-padding spacing-right text-center"><?php esc_html_e('Switch On Site Meta', 'blue-raven'); ?></h4>
                <label class="switch"><input type="checkbox" id="enable-site-meta" <?php checked(!empty($main_meta_enabled)); ?>><span class="slider"></span></label>
            </div>
        </section>
            
        <section class="spacing-bottom-large">
            <?php foreach ($pages_data as $page): ?>
                <div class="page-meta border-bottom-white" style="margin-top: 2rem;" data-page-id="<?php echo absint($page['ID']); ?>">
                    <div class="flex-sb spacing-bottom">
                        <h4 style="margin: 0px;"><?php echo esc_html($page['title']); ?></h4>
                        <p><a href="<?php echo esc_url($page['url']); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e('View', 'blue-raven'); ?></a></p>
                    </div>
                    <div class="flex-sb spacing-bottom">
                        <?php if ($page['has_meta_desc']): ?>
                            <span class="br-badge success">✓ <?php esc_html_e('Set', 'blue-raven'); ?></span>
                        <?php else: ?>
                            <span class="br-badge warning">✗ <?php esc_html_e('Missing', 'blue-raven'); ?></span>
                        <?php endif; ?>
                    </div>
                    <div>
                        <div class="char-count" style="text-align: right; margin: 4px; background:">
                            <span><?php echo absint(strlen($page['meta_description'])); ?></span>/160
                        </div>
                        <div class="flex-sb spacing-bottom">
                            <label class="text-center">
                                <h4 class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Meta Description', 'blue-raven'); ?></h4>
                            </label>
                            <input type="text" name="meta-description" placeholder="<?php esc_attr_e('Page meta description', 'blue-raven'); ?>" class="br-input meta-description"  style="max-width:45rem; min-width:80%;" value="<?php echo isset($page['meta_description']) ? esc_attr($page['meta_description']) : ''; ?>">
                        </div>
                        <div class="flex-sb spacing-bottom">
                            <label class="text-center">
                                <h4 class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Meta Tags', 'blue-raven'); ?></h4>
                            </label>
                            <input type="text" name="meta-tags" placeholder="<?php esc_attr_e('tag 1, tag 2, tag 3', 'blue-raven'); ?>" class="br-input meta-tags"  style="max-width:45rem; min-width: 80%" value="<?php echo isset($page['meta_keywords']) ? esc_attr($page['meta_keywords']) : ''; ?>">
                        </div>
                    </div>
                    <div class="flex-sb spacing-bottom">
                        <div></div>
                        <button class="enterra-button save-meta" style="margin-left: 1rem;"><?php esc_html_e('Save', 'blue-raven'); ?></button>
                    </div>
                </div>
            <?php endforeach; ?>
        </section>
        
        <div class="br-notice" style="margin-top: 20px;">
            <h4><?php esc_html_e('Best Practices:', 'blue-raven'); ?></h4>
            <ul class="indent-one">
                <li><strong><?php esc_html_e('Meta descriptions:', 'blue-raven'); ?></strong> <?php esc_html_e('Keep between 150-160 characters', 'blue-raven'); ?></li>
                <li><strong><?php esc_html_e('Keywords:', 'blue-raven'); ?></strong> <?php esc_html_e('Use 3-5 relevant, comma-separated phrases', 'blue-raven'); ?></li>
                <li><?php esc_html_e('Make each page\'s meta description unique', 'blue-raven'); ?></li>
                <li><?php esc_html_e('Include primary keywords naturally', 'blue-raven'); ?></li>
                <li><?php esc_html_e('Focus on compelling, action-oriented language', 'blue-raven'); ?></li>
            </ul>
        </div> 
        <?php
    }
}
