<?php

/**
 * Plugin Name: TrackSharp – Server Side Tracking for WooCommerce
 * Description: First-party server-side tracking for WooCommerce. GA4 Measurement Protocol, Meta Conversions API (CAPI), Google Ads Conversion attribution, Enhanced Conversions, and WP Consent API integration.
 * Version:     1.3.1
 * Author:      Kaya van Vliet
 * Requires PHP: 7.4
 * License:     GPLv2 or later
 * Text Domain: tracksharp
 * Domain Path: /languages
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// ============================================================================
// Plugin Constants
// ============================================================================
define( 'TRACKSHARP_VERSION', '1.3.1' );
define( 'TRACKSHARP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'TRACKSHARP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'TRACKSHARP_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
// ============================================================================
// Load Textdomain for Translations
// ============================================================================
// Note: Since WordPress 4.6, translations are automatically loaded for
// plugins hosted on WordPress.org. Manual load_plugin_textdomain is not needed.
// ============================================================================
// Freemius SDK Initialization
// ============================================================================
if ( !function_exists( 'tracksharp_fs' ) ) {
    // Create a helper function for easy SDK access.
    function tracksharp_fs() {
        global $tracksharp_fs;
        if ( !isset( $tracksharp_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            $tracksharp_fs = fs_dynamic_init( array(
                'id'               => '21630',
                'slug'             => 'tracksharp',
                'premium_slug'     => 'tracksharp-pro',
                'type'             => 'plugin',
                'public_key'       => 'pk_d72f318c533531227bc514e26f8f7',
                'is_org_compliant' => true,
                'is_premium'       => false,
                'premium_suffix'   => 'Pro',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'trial'            => array(
                    'days'               => 14,
                    'is_require_payment' => false,
                ),
                'menu'             => array(
                    'slug'       => 'tracksharp-audit',
                    'first-path' => 'admin.php?page=tracksharp-setup',
                    'support'    => false,
                ),
                'is_live'          => true,
            ) );
        }
        return $tracksharp_fs;
    }

    // Init Freemius.
    tracksharp_fs();
    // Signal that SDK was initiated.
    do_action( 'tracksharp_fs_loaded' );
}
// ============================================================================
// Freemius Customizations
// ============================================================================
if ( function_exists( 'tracksharp_fs' ) ) {
    /**
     * Detect whether the current site/user is Dutch.
     *
     * @return bool
     */
    function tracksharp_is_dutch_locale() {
        $site_locale = (string) get_option( 'WPLANG' );
        if ( $site_locale === '' ) {
            $site_locale = (string) get_locale();
        }
        if ( $site_locale === '' ) {
            $site_locale = (string) get_bloginfo( 'language' );
        }
        $user_locale = '';
        if ( function_exists( 'get_user_locale' ) ) {
            $user_locale = (string) get_user_locale();
        }
        $locales = [$site_locale, $user_locale];
        foreach ( $locales as $locale ) {
            $locale = strtolower( trim( (string) $locale ) );
            if ( $locale !== '' && strpos( $locale, 'nl' ) === 0 ) {
                return true;
            }
        }
        return false;
    }

    // Set default currency based on locale: EUR for Dutch, USD for everyone else
    tracksharp_fs()->add_filter( 'default_currency', function ( $currency ) {
        return ( tracksharp_is_dutch_locale() ? 'eur' : 'usd' );
    } );
    // Custom plugin icon for pricing page
    tracksharp_fs()->add_filter( 'plugin_icon', function ( $icon_path ) {
        return TRACKSHARP_PLUGIN_DIR . 'assets/images/tracksharp-icon.png';
    } );
    // Hide some Freemius submenus - keep Account and Pricing visible
    tracksharp_fs()->add_filter(
        'is_submenu_visible',
        function ( $is_visible, $menu_id ) {
            // Hide these from the menu
            $hidden_menus = [
                'contact',
                'support',
                'addons',
                'affiliation'
            ];
            // Keep Account and Pricing visible
            if ( $menu_id === 'account' || $menu_id === 'pricing' ) {
                return true;
            }
            if ( in_array( $menu_id, $hidden_menus, true ) ) {
                return false;
            }
            return $is_visible;
        },
        10,
        2
    );
    // Safety net: show a "Purchase" link on the license activation screen
    // so users blocked after trial expiry can still find the checkout.
    tracksharp_fs()->add_action( 'connect/after_license_input', function () {
        $currency = ( tracksharp_is_dutch_locale() ? 'eur' : 'usd' );
        $checkout_url = 'https://checkout.freemius.com/plugin/21630/plan/36087/licenses/1/?billing_cycle_selector=list&currency=' . $currency;
        echo '<p style="text-align:center;margin-top:12px;">';
        echo '<a href="' . esc_url( $checkout_url ) . '" target="_blank" rel="noopener noreferrer" style="color:#158a5f;font-weight:600;">';
        echo esc_html__( "Don't have a license yet? Purchase TrackSharp Pro", 'tracksharp' );
        echo '</a>';
        echo '</p>';
    } );
}
// ============================================================================
// Plugin Initialization (Free + Pro)
// ============================================================================
// Load features helper first (needed for feature checks)
require_once TRACKSHARP_PLUGIN_DIR . 'includes/class-tracksharp-features.php';
// Load and initialize the plugin for ALL users (free + pro)
require_once TRACKSHARP_PLUGIN_DIR . 'includes/class-tracksharp-loader.php';
$tracksharp = new TrackSharp_Loader();
$tracksharp->init();
// Register activation hook
register_activation_hook( __FILE__, ['TrackSharp_Loader', 'activate'] );