<?php
 /**
 * Plugin Name: WTC: Sözleşmeler, Kargo, SMS, İade, Form, OTP (SMS Doğrulama), Puan, Kupon Yönetimi
 * Description: WooCommerce Türkiye Checkout, Türkiye'deki e-ticaret siteleri için geliştirilmiş kapsamlı ödeme ve sipariş form yönetimi eklentisidir. Fatura & Adres Yönetimi, Sözleşme Yönetimi (Mesafeli Satış Sözleşmesi, Ön Bilgilendirme Formu), Kargo Takip ve Yönetimi, SMS Bildirimleri, İade Yönetimi, giriş/kayıt/ödeme sırasında SMS Doğrulama (OTP) Sistemi ve Kullanım İyileştirmeleri için geliştirilmiştir.
 * Version: 1.3.4
 * Author: wooSoft
 * Author URI: https://www.woosoft.com.tr/
 * Text Domain: wtc-checkout
 * Domain Path: /languages/
 * Requires PHP: 8.2
 * WC requires at least: 9.7
 * WC tested up to: 10.8.x
 * Requires Plugins: woocommerce
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */
defined('ABSPATH') || exit;
if (!defined('WTC_VERSION')) {
    define('WTC_VERSION', '1.3.4');
}
if (!defined('WTC_PLUGIN_FILE')) {
    define('WTC_PLUGIN_FILE', __FILE__);
}
if (!defined('WTC_PLUGIN_DIR')) {
    define('WTC_PLUGIN_DIR', plugin_dir_path(__FILE__));
}
if (!defined('WTC_PLUGIN_URL')) {
    define('WTC_PLUGIN_URL', plugin_dir_url(__FILE__));
}
if (!defined('WTC_LOG_DIR')) {
    define('WTC_LOG_DIR', WTC_PLUGIN_DIR . 'logs');
}
add_action(
    'before_woocommerce_init',
    function() {
        if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
        }
    }
);
add_filter('cron_schedules', 'wtc_add_custom_cron_schedules');
function wtc_add_custom_cron_schedules($schedules) {
    $schedules['daily'] = array(
        'interval' => DAY_IN_SECONDS,
        'display'  => __('Günlük', 'wtc-checkout')
    );
    $schedules['weekly'] = array(
        'interval' => 7 * DAY_IN_SECONDS,
        'display'  => __('Haftalık', 'wtc-checkout')
    );
    $schedules['monthly'] = array(
        'interval' => 30 * DAY_IN_SECONDS,
        'display'  => __('Aylık', 'wtc-checkout')
    );
    $schedules['quarterly'] = array(
        'interval' => 90 * DAY_IN_SECONDS,
        'display'  => __('3 Aylık', 'wtc-checkout')
    );
    $schedules['yearly'] = array(
        'interval' => 365 * DAY_IN_SECONDS,
        'display'  => __('Yıllık', 'wtc-checkout')
    );
    return $schedules;
}
function wtc_check_woocommerce_active() {
    if (!class_exists('WooCommerce')) {
        if (WP_DEBUG) {
        }
        deactivate_plugins(plugin_basename(__FILE__));
        wp_die('Bu eklenti için WooCommerce gereklidir.');
    }
}
register_activation_hook(__FILE__, 'wtc_check_woocommerce_active');
$composer_autoloader = WTC_PLUGIN_DIR . 'vendor/autoload.php';
if (!file_exists($composer_autoloader)) {
    add_action('admin_notices', function() {
        ?>
        <div class="notice notice-error">
            <p><?php esc_html_e('WooCommerce Turkey Checkout: Composer bağımlılıkları eksik. Lütfen plugin dizininde "composer install" komutunu çalıştırın.', 'wtc-checkout'); ?></p>
        </div>
        <?php
    });
    return;
}
require_once $composer_autoloader;
function wtc_init() {
    if (class_exists('WooCommerce')) {
        require_once WTC_PLUGIN_DIR . 'modules/forms/frontend/CheckoutViewModel.php';
        require_once WTC_PLUGIN_DIR . 'modules/forms/frontend/MyAccountViewModel.php';
        require_once WTC_PLUGIN_DIR . 'modules/forms/services/OrderDisplayService.php';
        require_once WTC_PLUGIN_DIR . 'modules/forms/services/UserProfileService.php';
        require_once WTC_PLUGIN_DIR . 'modules/contracts/services/EmailService.php';
        new WTC_CHECKOUT\Modules\Forms\Frontend\CheckoutViewModel();
        new WTC_CHECKOUT\Modules\Forms\Frontend\MyAccountViewModel();
        new WTC_CHECKOUT\Modules\Forms\Services\OrderDisplayService();
        WTC_CHECKOUT\Modules\Forms\Services\UserProfileService::get_instance();
        new \WTC_CHECKOUT\Modules\Contracts\Services\EmailService();
    }
}
add_action('init', 'wtc_init', 1);
function wtc_init_admin() {
    if (is_admin()) {
        require_once WTC_PLUGIN_DIR . 'core/AssetManager.php';
        require_once WTC_PLUGIN_DIR . 'core/MenuManager.php';
        require_once WTC_PLUGIN_DIR . 'modules/forms/admin/FieldLayoutSettings.php';
        require_once WTC_PLUGIN_DIR . 'core/general/GeneralSettings.php';
        require_once WTC_PLUGIN_DIR . 'modules/contracts/admin/ContractsSettings.php';
        require_once WTC_PLUGIN_DIR . 'modules/contracts/admin/OrderContractsMetaBox.php';
        require_once WTC_PLUGIN_DIR . 'core/general/CurrencyService.php';
        require_once WTC_PLUGIN_DIR . 'modules/cargo/admin/CargoSettings.php';
        require_once WTC_PLUGIN_DIR . 'modules/sms/admin/SMSSettings.php';
        require_once WTC_PLUGIN_DIR . 'modules/otp/admin/OTPSettings.php';
        require_once WTC_PLUGIN_DIR . 'modules/refund/admin/RefundSettings.php';
        require_once WTC_PLUGIN_DIR . 'modules/points/admin/PointsSettings.php';
        new WTC_CHECKOUT\Core\AssetManager();
        new WTC_CHECKOUT\Core\MenuManager();
        new WTC_CHECKOUT\Modules\Forms\Admin\FieldLayoutSettings();
        new WTC_CHECKOUT\Core\General\GeneralSettings();
        new WTC_CHECKOUT\Modules\Contracts\Admin\ContractsSettings();
        new WTC_CHECKOUT\Modules\Contracts\Admin\OrderContractsMetaBox();
        new WTC_CHECKOUT\Core\General\CurrencyService();
        new WTC_CHECKOUT\Modules\Cargo\Admin\CargoSettings();
        new WTC_CHECKOUT\Modules\SMS\Admin\SMSSettings();
        new WTC_CHECKOUT\Modules\OTP\Admin\OTPSettings();
        new WTC_CHECKOUT\Modules\Refund\Admin\RefundSettings();
        new WTC_CHECKOUT\Modules\Points\Admin\PointsSettings();
    }
}
add_action('init', 'wtc_init_admin', 2);
function wtc_init_frontend_services() {
    if (!is_admin()) {
        require_once WTC_PLUGIN_DIR . 'core/AssetManager.php';
        require_once WTC_PLUGIN_DIR . 'modules/contracts/frontend/ContractDisplayService.php';
        require_once WTC_PLUGIN_DIR . 'modules/contracts/services/OrderContractService.php';
        require_once WTC_PLUGIN_DIR . 'core/general/ShippingVisibilityService.php';
        require_once WTC_PLUGIN_DIR . 'core/general/CartCustomizationService.php';
        require_once WTC_PLUGIN_DIR . 'core/general/CurrencyService.php';
        require_once WTC_PLUGIN_DIR . 'core/general/RegisterViewModel.php';
        require_once WTC_PLUGIN_DIR . 'core/general/PasswordStrengthService.php';
        new WTC_CHECKOUT\Core\AssetManager();
        new WTC_CHECKOUT\Modules\Contracts\Frontend\ContractDisplayService();
        new WTC_CHECKOUT\Modules\Contracts\Services\OrderContractService();
        new WTC_CHECKOUT\Core\General\ShippingVisibilityService();
        new WTC_CHECKOUT\Core\General\CartCustomizationService();
        new WTC_CHECKOUT\Core\General\CurrencyService();
        new WTC_CHECKOUT\Core\General\RegisterViewModel();
        new WTC_CHECKOUT\Core\General\PasswordStrengthService();
    }
}
add_action('init', 'wtc_init_frontend_services', 3);
function wtc_init_coupons_services() {
    if (class_exists('WooCommerce')) {
        require_once WTC_PLUGIN_DIR . 'modules/coupons/services/CouponManager.php';
        require_once WTC_PLUGIN_DIR . 'modules/coupons/frontend/CouponAccountHandler.php';
        require_once WTC_PLUGIN_DIR . 'modules/coupons/admin/CouponSettings.php';
        WTC_CHECKOUT\Modules\Coupons\Services\CouponManager::get_instance();
        WTC_CHECKOUT\Modules\Coupons\Frontend\CouponAccountHandler::get_instance();
        if (is_admin()) {   
            new WTC_CHECKOUT\Modules\Coupons\Admin\CouponSettings();
        }
    }
}
add_action('init', 'wtc_init_coupons_services', 4);
function wtc_init_dashboard_services() {
    if (class_exists('WooCommerce')) {
        $service_file = WTC_PLUGIN_DIR . 'modules/dashboard/services/DashboardDataService.php';
        $style_manager_file = WTC_PLUGIN_DIR . 'modules/dashboard/services/DashboardStyleManager.php';
        $handler_file = WTC_PLUGIN_DIR . 'modules/dashboard/frontend/DashboardHandler.php';
        $settings_file = WTC_PLUGIN_DIR . 'modules/dashboard/admin/DashboardSettings.php';
        if (file_exists($service_file) && file_exists($handler_file) && file_exists($settings_file)) {
            require_once WTC_PLUGIN_DIR . 'core/Helpers/SvgIconHelper.php';
            require_once $service_file;
            require_once $style_manager_file;
            require_once $handler_file;
            require_once $settings_file;
            \WTC_CHECKOUT\Modules\Dashboard\Frontend\DashboardHandler::get_instance();
            if (is_admin()) {
                new \WTC_CHECKOUT\Modules\Dashboard\Admin\DashboardSettings();
            }
        }
    }
}
add_action('init', 'wtc_init_dashboard_services', 5);
function wtc_init_contract_manager() {
    if (get_option('wtc_contracts_enabled', 'no') !== 'yes') {
        return;
    }
    require_once WTC_PLUGIN_DIR . 'modules/contracts/services/ContractManager.php';
    require_once WTC_PLUGIN_DIR . 'modules/contracts/services/ContractDownloadService.php';
    require_once WTC_PLUGIN_DIR . 'modules/contracts/services/PDFService.php';
    require_once WTC_PLUGIN_DIR . 'modules/contracts/services/OrderContractService.php';
    new WTC_CHECKOUT\Modules\Contracts\Services\ContractManager();
}
add_action('init', 'wtc_init_contract_manager', 4); 
add_filter('plugin_row_meta', function($links, $file) {
    if ($file === plugin_basename(__FILE__)) {
        $links[] = '<a href="https://www.woosoft.com.tr/" style="color:#d54e21;font-weight:bold;">Premium</a>';
    }
    return $links;
}, 10, 2);
add_filter('plugin_action_links_' . plugin_basename(__FILE__), function($links) {
    $settings_link = '<a href="' . admin_url('admin.php?page=wtc-general-settings') . '">' . __('Ayarlar', 'wtc-checkout') . '</a>';
    array_unshift($links, $settings_link);
    return $links;
});