refactor(Core): refactor and optimize code

This commit is contained in:
2026-05-06 00:54:24 +03:30
parent 32c065e4b6
commit dec4e67b9e
20 changed files with 1787 additions and 361 deletions

View File

@@ -4,197 +4,185 @@ if (!defined('ABSPATH')) {
exit;
}
$current_page = sanitize_text_field($_GET['page'] ?? 'sodino-settings');
?>
<div id="sodino-app" class="min-h-screen bg-gray-50" dir="rtl">
<!-- Header -->
<div class="bg-white border-b border-gray-200">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="py-6">
<div class="flex items-center justify-between">
<div>
<h1 class="text-3xl font-bold text-gray-900"><?php _e('سودینو', 'sodino'); ?></h1>
<p class="mt-1 text-sm text-gray-500"><?php _e('بهینه‌سازی هوشمند فروش', 'sodino'); ?></p>
// Load components
require_once SODINO_PLUGIN_DIR . 'admin/components/layout.php';
sodino_admin_layout($current_page ?? 'sodino-settings', function() use ($settings) {
?>
<!-- Page Header -->
<?php sodino_card(
__('تنظیمات سودینو', 'sodino'),
__('تنظیمات عمومی پلاگین را مدیریت کنید.', 'sodino'),
null,
'mb-8'
); ?>
<!-- Settings Form -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<form method="post" class="space-y-8">
<?php wp_nonce_field('sodino_save_settings', 'sodino_settings_nonce'); ?>
<!-- General Settings -->
<div class="border-b border-gray-200 pb-6">
<h3 class="text-lg font-semibold text-gray-900 mb-4"><?php _e('تنظیمات عمومی', 'sodino'); ?></h3>
<div class="space-y-4">
<?php sodino_form_field([
'type' => 'checkbox',
'name' => 'plugin_enabled',
'label' => __('فعال‌سازی پلاگین', 'sodino'),
'value' => $settings['plugin_enabled'] ?? 1,
'description' => __('پلاگین سودینو را فعال یا غیرفعال کنید.', 'sodino')
]); ?>
<?php sodino_form_field([
'type' => 'checkbox',
'name' => 'pricing_enabled',
'label' => __('فعال‌سازی قیمت‌گذاری پویا', 'sodino'),
'value' => $settings['pricing_enabled'] ?? 1,
'description' => __('قیمت‌گذاری پویا بر اساس قوانین تعریف‌شده.', 'sodino')
]); ?>
<?php sodino_form_field([
'type' => 'checkbox',
'name' => 'upsell_enabled',
'label' => __('فعال‌سازی آپسل', 'sodino'),
'value' => $settings['upsell_enabled'] ?? 1,
'description' => __('نمایش پیشنهادات فروش به مشتریان.', 'sodino')
]); ?>
<?php sodino_form_field([
'type' => 'checkbox',
'name' => 'banner_enabled',
'label' => __('فعال‌سازی بنرهای هوشمند', 'sodino'),
'value' => $settings['banner_enabled'] ?? 1,
'description' => __('نمایش بنرهای هدفمند به کاربران.', 'sodino')
]); ?>
</div>
</div>
<!-- Pricing Strategy -->
<div class="border-b border-gray-200 pb-6">
<h3 class="text-lg font-semibold text-gray-900 mb-4"><?php _e('استراتژی قیمت‌گذاری', 'sodino'); ?></h3>
<div class="space-y-4">
<?php sodino_form_field([
'type' => 'checkbox',
'name' => 'allow_multiple_rules',
'label' => __('اجازه اعمال چند قانون همزمان', 'sodino'),
'value' => $settings['allow_multiple_rules'] ?? 0,
'description' => __('اگر فعال باشد، چند قانون می‌تواند روی یک محصول اعمال شود.', 'sodino')
]); ?>
<?php sodino_form_field([
'type' => 'select',
'name' => 'strategy',
'label' => __('استراتژی انتخاب قانون', 'sodino'),
'value' => $settings['strategy'] ?? 'priority',
'options' => [
'priority' => __('بر اساس اولویت', 'sodino'),
'highest_discount' => __('بیشترین تخفیف', 'sodino'),
'first_valid' => __('اولین قانون معتبر', 'sodino')
],
'description' => __('نحوه انتخاب قانون زمانی که چند قانون معتبر وجود دارد.', 'sodino')
]); ?>
<?php sodino_form_field([
'type' => 'number',
'name' => 'max_discount_percent',
'label' => __('حداکثر درصد تخفیف', 'sodino'),
'value' => $settings['max_discount_percent'] ?? 100,
'placeholder' => '100',
'description' => __('حداکثر درصد تخفیفی که می‌تواند اعمال شود (0-100).', 'sodino')
]); ?>
<?php sodino_form_field([
'type' => 'number',
'name' => 'min_product_price',
'label' => __('حداقل قیمت محصول', 'sodino'),
'value' => $settings['min_product_price'] ?? 0,
'placeholder' => '0',
'description' => __('حداقل قیمتی که یک محصول می‌تواند داشته باشد.', 'sodino')
]); ?>
<?php sodino_form_field([
'type' => 'checkbox',
'name' => 'cart_pricing_enabled',
'label' => __('قیمت‌گذاری در سبد خرید', 'sodino'),
'value' => $settings['cart_pricing_enabled'] ?? 1,
'description' => __('اعمال قیمت‌گذاری پویا در صفحه سبد خرید.', 'sodino')
]); ?>
</div>
</div>
<!-- Performance Settings -->
<div class="border-b border-gray-200 pb-6">
<h3 class="text-lg font-semibold text-gray-900 mb-4"><?php _e('تنظیمات عملکرد', 'sodino'); ?></h3>
<div class="space-y-4">
<?php sodino_form_field([
'type' => 'checkbox',
'name' => 'cache_enabled',
'label' => __('فعال‌سازی کش', 'sodino'),
'value' => $settings['cache_enabled'] ?? 1,
'description' => __('استفاده از کش برای بهبود عملکرد.', 'sodino')
]); ?>
<?php sodino_form_field([
'type' => 'number',
'name' => 'cache_duration',
'label' => __('مدت زمان کش (ثانیه)', 'sodino'),
'value' => $settings['cache_duration'] ?? 3600,
'placeholder' => '3600',
'description' => __('مدت زمان نگهداری داده‌ها در کش (پیش‌فرض: 3600 ثانیه = 1 ساعت).', 'sodino')
]); ?>
<div class="flex items-center gap-4">
<?php sodino_button(
__('پاک کردن کش', 'sodino'),
wp_nonce_url(admin_url('admin.php?page=sodino-settings&action=clear_cache'), 'clear_cache'),
'secondary'
); ?>
<span class="text-sm text-gray-500"><?php _e('تمام کش‌های سودینو را پاک می‌کند.', 'sodino'); ?></span>
</div>
</div>
</div>
</div>
</div>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div class="flex gap-8">
<!-- Sidebar -->
<aside class="w-64 flex-shrink-0">
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<h2 class="text-lg font-semibold text-gray-900 mb-4"><?php _e('منوی سودینو', 'sodino'); ?></h2>
<nav class="space-y-2">
<a href="<?php echo admin_url('admin.php?page=sodino-dashboard'); ?>" class="block px-3 py-2 rounded-md text-sm font-medium <?php echo $current_page === 'sodino-dashboard' ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?>">
<?php _e('داشبورد', 'sodino'); ?>
</a>
<a href="<?php echo admin_url('admin.php?page=sodino-rules'); ?>" class="block px-3 py-2 rounded-md text-sm font-medium <?php echo $current_page === 'sodino-rules' ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?>">
<?php _e('قوانین', 'sodino'); ?>
</a>
<a href="<?php echo admin_url('admin.php?page=sodino-add-rule'); ?>" class="block px-3 py-2 rounded-md text-sm font-medium <?php echo $current_page === 'sodino-add-rule' ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?>">
<?php _e('افزودن قانون', 'sodino'); ?>
</a>
<a href="<?php echo admin_url('admin.php?page=sodino-upsells'); ?>" class="block px-3 py-2 rounded-md text-sm font-medium <?php echo $current_page === 'sodino-upsells' ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?>">
<?php _e('آپسل (پیشنهاد فروش)', 'sodino'); ?>
</a>
<a href="<?php echo admin_url('admin.php?page=sodino-add-upsell'); ?>" class="block px-3 py-2 rounded-md text-sm font-medium <?php echo $current_page === 'sodino-add-upsell' ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?>">
<?php _e('افزودن آپسل', 'sodino'); ?>
</a>
<a href="<?php echo admin_url('admin.php?page=sodino-competitor-price'); ?>" class="block px-3 py-2 rounded-md text-sm font-medium <?php echo $current_page === 'sodino-competitor-price' ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?>">
<?php _e('قیمت رقبا (به‌زودی)', 'sodino'); ?>
</a>
<a href="<?php echo admin_url('admin.php?page=sodino-settings'); ?>" class="block px-3 py-2 rounded-md text-sm font-medium <?php echo $current_page === 'sodino-settings' ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?>">
<?php _e('تنظیمات', 'sodino'); ?>
</a>
</nav>
<!-- Advanced Settings -->
<div>
<h3 class="text-lg font-semibold text-gray-900 mb-4"><?php _e('تنظیمات پیشرفته', 'sodino'); ?></h3>
<div class="space-y-4">
<?php sodino_form_field([
'type' => 'checkbox',
'name' => 'ab_testing_enabled',
'label' => __('فعال‌سازی A/B Testing', 'sodino'),
'value' => $settings['ab_testing_enabled'] ?? 0,
'description' => __('تست A/B برای قوانین قیمت‌گذاری (قابلیت آزمایشی).', 'sodino')
]); ?>
<?php sodino_form_field([
'type' => 'checkbox',
'name' => 'scheduled_campaigns_enabled',
'label' => __('فعال‌سازی کمپین‌های زمان‌بندی شده', 'sodino'),
'value' => $settings['scheduled_campaigns_enabled'] ?? 1,
'description' => __('اجرای خودکار قوانین بر اساس تاریخ شروع و پایان.', 'sodino')
]); ?>
<?php sodino_form_field([
'type' => 'checkbox',
'name' => 'debug_mode',
'label' => __('حالت دیباگ', 'sodino'),
'value' => $settings['debug_mode'] ?? 0,
'description' => __('فعال‌سازی لاگ‌های دیباگ (فقط برای توسعه‌دهندگان).', 'sodino')
]); ?>
</div>
</aside>
</div>
<!-- Main Content -->
<main class="flex-1 min-w-0">
<!-- Page Header -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-8">
<h2 class="text-2xl font-semibold text-gray-900"><?php _e('تنظیمات سودینو', 'sodino'); ?></h2>
<p class="mt-2 text-gray-600"><?php _e('کنترل کامل تجربه قیمت‌گذاری و بهینه‌سازی درآمد را از اینجا انجام دهید.', 'sodino'); ?></p>
</div>
<?php if (isset($_GET['updated']) && $_GET['updated'] === 'true') : ?>
<div class="mb-6 bg-green-50 border border-green-200 rounded-lg p-4">
<div class="flex">
<div class="flex-shrink-0">
<svg class="h-5 w-5 text-green-400" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" />
</svg>
</div>
<div class="mr-3">
<p class="text-sm font-medium text-green-800"><?php _e('تنظیمات با موفقیت ذخیره شد.', 'sodino'); ?></p>
</div>
</div>
</div>
<?php endif; ?>
<form method="post" class="space-y-6">
<?php wp_nonce_field('sodino_save_settings', 'sodino_settings_nonce'); ?>
<!-- General Section -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<div class="mb-6">
<h3 class="text-lg font-semibold text-gray-900"><?php _e('عمومی', 'sodino'); ?></h3>
<p class="mt-1 text-sm text-gray-500"><?php _e('تنظیمات کلی فعال‌سازی پلاگین و ویژگی‌های اصلی.', 'sodino'); ?></p>
</div>
<div class="grid gap-6 md:grid-cols-2">
<div class="bg-gray-50 rounded-lg p-5 border border-gray-200">
<label class="flex items-center gap-3 text-gray-700">
<input type="checkbox" name="plugin_enabled" value="1" <?php checked($settings['plugin_enabled'], 1); ?> class="h-5 w-5 rounded border-gray-300 text-blue-600 focus:ring-blue-500">
<span><?php _e('فعال‌سازی کل پلاگین', 'sodino'); ?></span>
</label>
<p class="mt-3 text-sm text-gray-500"><?php _e('اگر غیرفعال باشد، هیچ قاعده‌ای اعمال نخواهد شد.', 'sodino'); ?></p>
</div>
<div class="bg-gray-50 rounded-lg p-5 border border-gray-200">
<label class="flex items-center gap-3 text-gray-700">
<input type="checkbox" name="pricing_enabled" value="1" <?php checked($settings['pricing_enabled'], 1); ?> class="h-5 w-5 rounded border-gray-300 text-blue-600 focus:ring-blue-500">
<span><?php _e('فعال‌سازی قیمت‌گذاری پویا', 'sodino'); ?></span>
</label>
<p class="mt-3 text-sm text-gray-500"><?php _e('این گزینه، اعمال قوانین قیمت‌گذاری را کنترل می‌کند.', 'sodino'); ?></p>
</div>
<div class="bg-gray-50 rounded-lg p-5 border border-gray-200">
<label class="flex items-center gap-3 text-gray-700">
<input type="checkbox" name="upsell_enabled" value="1" <?php checked($settings['upsell_enabled'], 1); ?> class="h-5 w-5 rounded border-gray-300 text-blue-600 focus:ring-blue-500">
<span><?php _e('فعال‌سازی سیستم آپسل', 'sodino'); ?></span>
</label>
<p class="mt-3 text-sm text-gray-500"><?php _e('پیشنهادهای درآمدی اضافه را نمایش می‌دهد.', 'sodino'); ?></p>
</div>
</div>
</div>
<!-- Pricing Behavior Section -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<div class="mb-6">
<h3 class="text-lg font-semibold text-gray-900"><?php _e('رفتار قیمت‌گذاری', 'sodino'); ?></h3>
<p class="mt-1 text-sm text-gray-500"><?php _e('چگونگی اجرای قوانین در فرآیند قیمت‌گذاری.', 'sodino'); ?></p>
</div>
<div class="grid gap-6 md:grid-cols-2">
<div class="bg-gray-50 rounded-lg p-5 border border-gray-200">
<label class="flex items-center gap-3 text-gray-700">
<input type="checkbox" name="allow_multiple_rules" value="1" <?php checked($settings['allow_multiple_rules'], 1); ?> class="h-5 w-5 rounded border-gray-300 text-blue-600 focus:ring-blue-500">
<span><?php _e('اجازه اعمال چند قانون همزمان', 'sodino'); ?></span>
</label>
<p class="mt-3 text-sm text-gray-500"><?php _e('قوانین معتبر به صورت متوالی اعمال می‌شوند.', 'sodino'); ?></p>
</div>
<div class="bg-gray-50 rounded-lg p-5 border border-gray-200">
<label class="block text-sm font-medium text-gray-700 mb-3"><?php _e('استراتژی اعمال', 'sodino'); ?></label>
<select name="strategy" class="w-full rounded-lg border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-100">
<option value="highest_discount" <?php selected($settings['strategy'], 'highest_discount'); ?>><?php _e('بالاترین تخفیف', 'sodino'); ?></option>
<option value="first_valid" <?php selected($settings['strategy'], 'first_valid'); ?>><?php _e('اولین قانون معتبر', 'sodino'); ?></option>
<option value="priority" <?php selected($settings['strategy'], 'priority'); ?>><?php _e('بر اساس اولویت', 'sodino'); ?></option>
</select>
<p class="mt-3 text-sm text-gray-500"><?php _e('استراتژی انتخاب قانون زمانی که بیش از یک قانون معتبر وجود داشته باشد.', 'sodino'); ?></p>
</div>
</div>
</div>
<!-- Limits Section -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<div class="mb-6">
<h3 class="text-lg font-semibold text-gray-900"><?php _e('محدودیت‌ها', 'sodino'); ?></h3>
<p class="mt-1 text-sm text-gray-500"><?php _e('پارامترهای محدودسازی برای حفظ حاشیه سود.', 'sodino'); ?></p>
</div>
<div class="grid gap-6 md:grid-cols-2">
<div class="bg-gray-50 rounded-lg p-5 border border-gray-200">
<label class="block text-sm font-medium text-gray-700 mb-3"><?php _e('حداکثر درصد تخفیف', 'sodino'); ?></label>
<input type="number" name="max_discount_percent" value="<?php echo esc_attr($settings['max_discount_percent']); ?>" min="0" max="100" class="w-full rounded-lg border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-100">
<p class="mt-3 text-sm text-gray-500"><?php _e('حداکثر تخفیف مجاز برای هر محصول را تعیین می‌کند.', 'sodino'); ?></p>
</div>
<div class="bg-gray-50 rounded-lg p-5 border border-gray-200">
<label class="block text-sm font-medium text-gray-700 mb-3"><?php _e('حداقل قیمت محصول', 'sodino'); ?></label>
<input type="number" name="min_product_price" value="<?php echo esc_attr($settings['min_product_price']); ?>" min="0" step="0.01" class="w-full rounded-lg border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-100">
<p class="mt-3 text-sm text-gray-500"><?php _e('از کاهش قیمت زیر این مقدار جلوگیری می‌کند.', 'sodino'); ?></p>
</div>
</div>
</div>
<!-- Features Section -->
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
<div class="mb-6">
<h3 class="text-lg font-semibold text-gray-900"><?php _e('ویژگی‌ها', 'sodino'); ?></h3>
<p class="mt-1 text-sm text-gray-500"><?php _e('گزینه‌های پیشرفته برای شخصی‌سازی رفتار درآمدی.', 'sodino'); ?></p>
</div>
<div class="grid gap-6 md:grid-cols-2">
<div class="bg-gray-50 rounded-lg p-5 border border-gray-200">
<label class="flex items-center gap-3 text-gray-700">
<input type="checkbox" name="ab_testing_enabled" value="1" <?php checked($settings['ab_testing_enabled'], 1); ?> class="h-5 w-5 rounded border-gray-300 text-blue-600 focus:ring-blue-500">
<span><?php _e('فعال‌سازی تست A/B', 'sodino'); ?></span>
</label>
<p class="mt-3 text-sm text-gray-500"><?php _e('امکان فعال‌سازی سناریوهای آزمایشی را اضافه می‌کند.', 'sodino'); ?></p>
</div>
<div class="bg-gray-50 rounded-lg p-5 border border-gray-200">
<label class="flex items-center gap-3 text-gray-700">
<input type="checkbox" name="cart_pricing_enabled" value="1" <?php checked($settings['cart_pricing_enabled'], 1); ?> class="h-5 w-5 rounded border-gray-300 text-blue-600 focus:ring-blue-500">
<span><?php _e('اعمال قیمت‌گذاری در سبد خرید', 'sodino'); ?></span>
</label>
<p class="mt-3 text-sm text-gray-500"><?php _e('در صورت خاموش بودن، قیمت‌گذاری پویا فقط در نمایش محصول انجام می‌شود.', 'sodino'); ?></p>
</div>
<div class="bg-gray-50 rounded-lg p-5 border border-gray-200">
<label class="flex items-center gap-3 text-gray-700">
<input type="checkbox" name="scheduled_campaigns_enabled" value="1" <?php checked($settings['scheduled_campaigns_enabled'], 1); ?> class="h-5 w-5 rounded border-gray-300 text-blue-600 focus:ring-blue-500">
<span><?php _e('فعال‌سازی کمپین‌های زمان‌بندی شده', 'sodino'); ?></span>
</label>
<p class="mt-3 text-sm text-gray-500"><?php _e('با فعال کردن، می‌توانید قوانین را به صورت زمان‌بندی‌شده اجرا کنید.', 'sodino'); ?></p>
</div>
</div>
</div>
<!-- Submit Button -->
<div class="flex justify-end">
<button type="submit" class="inline-flex items-center px-6 py-3 border border-transparent text-sm font-medium rounded-lg text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors duration-200">
<?php _e('ذخیره تنظیمات', 'sodino'); ?>
</button>
</div>
</form>
</main>
</div>
<!-- Submit Button -->
<div class="flex items-center justify-end gap-4 pt-6 border-t border-gray-200">
<button type="submit" class="inline-flex items-center px-6 py-3 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors duration-200">
<?php _e('ذخیره تنظیمات', 'sodino'); ?>
</button>
</div>
</form>
</div>
</div>
<?php
});
?>