<?php

/**
 * Plugin Name: CodeWP Affiliate Top Bar
 * Plugin URI: https://codewp.ai
 * Description: Displays a custom top bar for users referred by an affiliate.
 * Version: 1.0
 * Author: CodeWP Assistant
 * Author URI: https://codewp.ai
 */

// Ensure WooCommerce and AffiliateWP are active before executing.
add_action('plugins_loaded', 'cwpai_check_required_plugins');
function cwpai_check_required_plugins() {
    if (class_exists('WooCommerce') && class_exists('Affiliate_WP')) {
        add_action('wp_head', 'cwpai_display_affiliate_top_bar');
    }
}

function cwpai_display_affiliate_top_bar() {
    $affiliate_id  = affiliate_wp()->tracking->get_affiliate_id();
  error_log($affiliate_id);
    if (!$affiliate_id) {
        return;
    }

    $affiliate_name = affwp_get_affiliate_name($affiliate_id);
    if (!$affiliate_name) {
        return;
    }

    echo '<div id="cwpai-affiliate-top-bar" style="position: fixed; top: 0; width: 100%; background: #333; color: #fff; text-align: center; padding: 10px; z-index: 9999;">You are now shopping with ' . esc_html($affiliate_name) . '</div>';

}