<?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 > Analytics
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - BLUE RAVEN ADMIN TOOL ANALYTICS
 * || 
 * =============================================================================
 */

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

/*
============================================================
    BLUE RAVEN ADMIN TOOL ANALYTICS
============================================================
*/

class Blue_Raven_Admin_Tools_Analytics {

    /**
     * Render the analytics tools page
     */
    public function render() {
        $this->load_tools_analytics_template();
    }
    
    /**
     * Load the analytics template and display data
     * 
     * @private
     */
    private static function load_tools_analytics_template() {
           
    ## =========== Fetch Valus from table =========== ##
    
        global $wpdb;
        
		$table = $wpdb->prefix . 'BlueRavenValues';
		$query = $wpdb->prepare(
			"SELECT * FROM {$table} WHERE setting LIKE %s",
			'%-site-analytics%'
		);
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Table name is safely constructed, query uses prepare()
		$analytics = $wpdb->get_results($query, ARRAY_A);
		$analytics_by_setting = array();
        
        if ( ! empty( $analytics ) ) foreach ( $analytics as $row ) { if (isset($row['setting'])) $analytics_by_setting[sanitize_key($row['setting'])] = $row;}

        $google_settings = isset($analytics_by_setting['google-site-analytics']) ? $analytics_by_setting['google-site-analytics'] : array('value' => '', 'enabled' => false);
        $meta_pixel_settings = isset($analytics_by_setting['meta-site-analytics']) ? $analytics_by_setting['meta-site-analytics'] : array('value' => '', 'enabled' => false);
        $microsoft_settings = isset($analytics_by_setting['microsoft-site-analytics']) ? $analytics_by_setting['microsoft-site-analytics'] : array('value' => '', 'enabled' => false);
        $ahref_settings = isset($analytics_by_setting['ahref-site-analytics']) ? $analytics_by_setting['ahref-site-analytics'] : array('value' => '', 'enabled' => false);
    
        ?>
        
    <!-- =========== Analytics =========== -->
    
            <div class="flex-sb">
                <h3><?php esc_html_e('Analytics', 'blue-raven'); ?></h3>
                <h4>
                    <?php esc_html_e('Need Help', 'blue-raven'); ?>
                    <a href="https://portal.enterrahost.com/knowledgebase/6/" 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('Analytics tags are small pieces of code that allow you to track visitor behavior, conversions, and site performance across different platforms like Google Analytics, Meta Pixel, Ahrefs, and Microsoft Clarity.', 'blue-raven'); ?>
                <?php esc_html_e('Adding these tags correctly ensures your site collects important marketing and SEO data.', 'blue-raven'); ?>
                <?php esc_html_e('Blue Raven makes it easy to add and manage your tracking tags without needing to manually edit your site code.', 'blue-raven'); ?>
                <?php esc_html_e('Simply paste your tag ID or verification code into the relevant field, enable tracking, and start gathering insights immediately.', 'blue-raven'); ?>
            </p>      
            
            <section>
                <div class="border-bottom-white flex-right" style="margin: 20px 0;">
                    <h4 class="no-margin-padding spacing-right text-center"><?php esc_html_e('Switch On Site Analytics', 'blue-raven'); ?></h4>
                    <label class="switch">
                        <input type="checkbox" id="enable-site-analytics"
                            <?php checked(!empty($analytics_by_setting['overall-site-analytics']['enabled']) && $analytics_by_setting['overall-site-analytics']['enabled']); ?>>
                        <span class="slider"></span>
                    </label>
                </div>
            </section>
            
    <!-- =========== Google Analytics =========== -->
    
            <section class="spacing-bottom-large">
                <h4 class="spacing-right text-center"><?php esc_html_e('Google Analytics', 'blue-raven'); ?></h4>
                <p class="indent-one">
                    <?php esc_html_e('To add your Google Analytics tracking code, go to your Google Analytics account and create a new property if you haven\'t already.', 'blue-raven'); ?>
                    <?php esc_html_e('Google will provide a Measurement ID that looks like', 'blue-raven'); ?> <strong>G-XXXXXXXXXX</strong>. 
                    <?php esc_html_e('Copy only the Measurement ID, not the entire tracking script.', 'blue-raven'); ?>
                    <?php esc_html_e('For example, if your ID is G-ABC123XYZ0, you should copy just G-ABC123XYZ0.', 'blue-raven'); ?>
                    <?php esc_html_e('Paste that code into the field below, enable "Google Analytics ID".', 'blue-raven'); ?>
                    <?php esc_html_e('Your site will then start sending data to your Google Analytics account.', 'blue-raven'); ?>
                </p>
                
                <div class="flex-right row-down-right" style="margin-top: 3rem;">
                    <div class="flex-left spacing-right" style="margin: 20px 0;">
                        <label class="text-center" style="text-decoration: underline;">
                            <h4 class="no-margin-padding spacing-right-small text-center">
                                <a href="https://analytics.google.com/analytics" target="_blank" rel="noopener noreferrer">
                                    <?php esc_html_e('Google Analytics ID', 'blue-raven'); ?>
                                </a>
                            </h4>
                        </label>
                        <input type="text" name="google-key" placeholder="<?php esc_attr_e('KEY', 'blue-raven'); ?>" class="br-input" style="width:20rem;" id="google-analytics-key"
                            value="<?php echo esc_attr($google_settings['value']); ?>">
                    </div>
                    <div class="flex-right toggle-text-width" style="margin: 20px 0;">
                        <h4 class="no-margin-padding spacing-right text-center"><?php esc_html_e('Google Analytics', 'blue-raven'); ?></h4>
                        <label class="switch">
                            <input type="checkbox" id="enable-google-analytics" <?php checked($google_settings['enabled'], true); ?>>
                            <span class="slider"></span>
                        </label>
                    </div>
                </div>  
                <div style="height:1.2rem;">
                    <p class="br-error" id="google-analytics-error-message" style="text-align:right;"></p>
                </div>
            </section>
            
    <!-- =========== Meta Analytics =========== -->
    
            <section class="spacing-bottom-large">
                <h4 class="spacing-right text-center"><?php esc_html_e('Meta Pixel Analytics', 'blue-raven'); ?></h4>
                <p class="indent-one">
                    <?php esc_html_e('To add your Meta Pixel tracking code, go to your Facebook Business Manager, open Events Manager, and create a new Pixel if you haven\'t already.', 'blue-raven'); ?>
                    <?php esc_html_e('Facebook will provide a Pixel tracking code snippet that contains an ID like', 'blue-raven'); ?> <strong>123123123123123</strong>. 
                    <?php esc_html_e('Copy only the numerical ID after "id=", not the entire script.', 'blue-raven'); ?>
                    <?php esc_html_e('For example, if you see src="https://www.facebook.com/tr?id=123123123123123&ev=PageView&noscript=1", copy just 123123123123123.', 'blue-raven'); ?>
                    <?php esc_html_e('Paste that number into the field below and enable "Meta Pixel Tracking".', 'blue-raven'); ?>
                    <?php esc_html_e('Your site will then start sending visitor events to your Meta Pixel account.', 'blue-raven'); ?>
                </p>
                
                <div class="flex-right row-down-right" style="margin-top: 3rem;">
                    <div class="flex-left spacing-right" style="margin: 20px 0;">
                        <label class="text-center" style="text-decoration: underline;">
                            <h4 class="no-margin-padding spacing-right-small text-center">
                                <a href="https://business.facebook.com" target="_blank" rel="noopener noreferrer">
                                    <?php esc_html_e('Meta Pixel ID', 'blue-raven'); ?>
                                </a>
                            </h4>
                        </label>
                        <input type="text" name="meta-key" placeholder="<?php esc_attr_e('KEY', 'blue-raven'); ?>" class="br-input" style="width:20rem;" id="meta-analytics-key"
                            value="<?php echo esc_attr($meta_pixel_settings['value']); ?>">
                    </div>
                    <div class="flex-right toggle-text-width" style="margin: 20px 0;">
                        <h4 class="no-margin-padding spacing-right text-center"><?php esc_html_e('Meta Pixel', 'blue-raven'); ?></h4>
                        <label class="switch">
                            <input type="checkbox" id="enable-meta-analytics" <?php checked($meta_pixel_settings['enabled'], true); ?>>
                            <span class="slider"></span>
                        </label>
                    </div>
                </div>  
                <div style="height:1.2rem;">
                    <p class="br-error" id="meta-pixel-error-message" style="text-align:right;"></p>
                </div>
            </section>      
            
    <!-- =========== Microsoft Clarity Analytics =========== -->            
            
            <section class='spacing-bottom-large'>
                <h4 class='spacing-right text-center'><?php esc_html_e('Microsoft Clarity Analytics', 'blue-raven'); ?></h4>
                <p class='indent-one'>
                    <?php esc_html_e('To add your Microsoft Clarity tracking code, go to your Microsoft Clarity account and create a new project if you haven\'t already. 
                    Clarity will provide you with a tracking code snippet. From that snippet, locate the part that looks like', 'blue-raven'); ?> 
                    <?php /* translators: %1$s is the example script tag, %2$s is the extracted data-key value. */ printf( esc_html__('Copy only the ID that appears as the last parameter, not the entire script. For example, if the snippet is %1$s, you should copy just %2$s.', 'blue-raven'), '<strong>(window, document, "clarity", "script", "r88bob1234")</strong>', '<strong>r88bob1234</strong>'); ?>
                    <?php esc_html_e('Paste that code into the field below, enable "Add Microsoft Clarity Tracking", and your site will then start sending data to your Clarity account.', 'blue-raven'); ?>
                </p>
                <div class='flex-right row-down-right' style='margin-top: 3rem;'>
                    <div style="margin: 20px 0;" class='flex-left spacing-right'>
                        <label class='text-center' style='text-decoration: underline;'>
                            <h4 class='no-margin-padding spacing-right-small text-center'>
                                <a href='https://www.bing.com/webmasters/' target="_blank" rel="noopener noreferrer">
                                    <?php esc_html_e('Microsoft Clarity ID', 'blue-raven'); ?>
                                </a>
                            </h4>
                        </label>
                        <input type="text" name="analytics_tracking_settings[microsoft][value]" placeholder="<?php esc_attr_e('KEY', 'blue-raven'); ?>" class="br-input" style="width:20rem;" id='microsoft-analytics-key'
                            value="<?php echo esc_attr($microsoft_settings['value']); ?>">
                    </div>
                    <div style="margin: 20px 0;" class='flex-right toggle-text-width'>
                        <h4 class='no-margin-padding spacing-right text-right'><?php esc_html_e('Microsoft Clarity', 'blue-raven'); ?></h4>
                        <label class="switch">
                            <input type="checkbox" name="analytics_tracking_settings[microsoft][enabled]" id="enable-microsoft-analytics" value="1" <?php checked($microsoft_settings['enabled'], true); ?>>
                            <span class="slider"></span>
                        </label>
                    </div>
                </div>  
                <div style='height:1.2rem;'>
                    <p class='br-error' id='microsoft-analytics-error-message' style='text-align:right;'></p>
                </div>
            </section> 
                
    <!-- =========== Ahrefs Analytics =========== -->     
            
            <section class='spacing-bottom-large'>
                <h4 class='spacing-right text-center'><?php esc_html_e('Ahrefs Analytics', 'blue-raven'); ?></h4>
                <p class='indent-one'>
                    <?php esc_html_e('To add your ahrefs Analytics tracking code, go to your ahrefs account and open the Analytics section for your project. 
                    ahrefs will provide you with a tracking script. From that script, locate the part that looks like', 'blue-raven'); ?> 
                    <?php /* translators: %1$s is the example script tag, %2$s is the extracted data-key value. */ printf( esc_html__('Copy only the code inside the quotation marks after data-key=, not the entire script. For example, if the script is %1$s, you should copy just %2$s.', 'blue-raven'), '<strong>src="https://analytics.ahrefs.com/analytics.js" data-key="234d23d23" async></strong>', '<strong>234d23d23</strong>' ); ?>
                    <?php esc_html_e('you should copy just', 'blue-raven'); ?> <strong>234d23d23</strong>. 
                    <?php esc_html_e('Paste that code into the field below, enable "Add ahrefs Analytics Tracking", and your site will start sending data to your Ahrefs Analytics account.', 'blue-raven'); ?>
                </p>
                <div class='flex-right row-down-right' style='margin-top: 3rem;'>
                    <div style="margin: 20px 0;" class='flex-left spacing-right'>
                        <label class='text-center' style='text-decoration: underline;'>
                            <h4 class='no-margin-padding spacing-right-small text-center'> <a href='https://app.ahrefs.com/dashboard' target="_blank" rel="noopener noreferrer">  <?php esc_html_e('Ahrefs Analytics ID', 'blue-raven'); ?></a></h4>
                        </label>
                        <input type="text" name="analytics_tracking_settings[ahref][value]" placeholder="<?php esc_attr_e('KEY', 'blue-raven'); ?>" class="br-input" style="width:20rem;" id='ahref-analytics-key' value="<?php echo esc_attr($ahref_settings['value']); ?>">
                    </div>
                    <div style="margin: 20px 0;" class='flex-right toggle-text-width'>
                        <h4 class='no-margin-padding spacing-right text-center'><?php esc_html_e('Ahrefs Analytics', 'blue-raven'); ?></h4>
                        <label class="switch">
                            <input type="checkbox" name="analytics_tracking_settings[ahref][enabled]" id="enable-ahref-analytics" value="1" <?php checked($ahref_settings['enabled'], true); ?>>
                            <span class="slider"></span>
                        </label>
                    </div>
                </div>  
                <div style='height:1.2rem;'>
                    <p class='br-error' id='ahref-analytics-error-message' style='text-align:right;'></p>
                </div>
            </section>
        <?php
    }
}