feat: Add banner management functionality

- Implemented a new Banner model to represent banner data.
- Created a BannerRepository for database interactions related to banners.
- Developed a BannerService to handle business logic for banners.
- Added admin views for listing and adding banners.
- Integrated banner hooks for frontend rendering and click tracking.
- Created frontend styles and scripts for banner display and interaction.
- Updated database migrations to include a new banners table.
- Enhanced AdminController to manage banner actions and pages.
This commit is contained in:
2026-05-05 01:03:05 +03:30
parent 5930c1ad6f
commit 32c065e4b6
15 changed files with 1350 additions and 4 deletions

219
admin/views/banner-form.php Normal file
View File

@@ -0,0 +1,219 @@
<?php
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
$current_page = sanitize_text_field($_GET['page'] ?? 'sodino-add-banner');
?>
<div id="sodino-app" class="min-h-screen bg-gray-50" dir="rtl">
<div class="bg-white border-b border-gray-200 shadow-sm">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
<div class="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
<div>
<h1 class="text-3xl font-bold text-gray-900"><?php echo $banner->id ? __('ویرایش بنر', 'sodino') : __('افزودن بنر جدید', 'sodino'); ?></h1>
<p class="mt-1 text-sm text-gray-500"><?php _e('تنظیمات تبلیغات هوشمند و زمان‌بندی نمایش بنر در سایت.', 'sodino'); ?></p>
</div>
<a href="<?php echo admin_url('admin.php?page=sodino-banners'); ?>" class="inline-flex items-center gap-2 rounded-full bg-white px-5 py-3 text-sm font-semibold text-gray-700 border border-gray-200 shadow-sm hover:bg-gray-50">
<?php _e('بازگشت به لیست بنرها', 'sodino'); ?>
</a>
</div>
</div>
</div>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div class="grid gap-8 lg:grid-cols-[280px_1fr]">
<aside class="bg-white rounded-2xl border border-gray-200 p-6 shadow-sm">
<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-xl 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-xl 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-banners'); ?>" class="block px-3 py-2 rounded-xl text-sm font-medium <?php echo $current_page === 'sodino-banners' ? '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-xl 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>
</aside>
<main class="space-y-6">
<form method="post">
<?php wp_nonce_field('sodino_save_banner', 'sodino_banner_nonce'); ?>
<div class="bg-white rounded-2xl border border-gray-200 p-6 shadow-sm space-y-6">
<div>
<h2 class="text-xl font-semibold text-gray-900"><?php _e('اطلاعات اصلی', 'sodino'); ?></h2>
<p class="mt-2 text-sm text-gray-500"><?php _e('عنوان، محتوا و لینک بنر را تعریف کنید.', 'sodino'); ?></p>
</div>
<div class="grid gap-6 lg:grid-cols-2">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2" for="title"><?php _e('عنوان بنر', 'sodino'); ?></label>
<input type="text" name="title" id="title" value="<?php echo esc_attr($banner->title); ?>" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100" required>
</div>
<div>
<span class="block text-sm font-medium text-gray-700 mb-2"><?php _e('نوع محتوا', 'sodino'); ?></span>
<div class="space-y-2">
<?php $content_types = ['image' => __('تصویر', 'sodino'), 'html' => __('HTML', 'sodino'), 'shortcode' => __('شورت‌کد', 'sodino')]; ?>
<?php foreach ($content_types as $value => $label) : ?>
<label class="flex items-center gap-3 rounded-2xl border border-gray-300 bg-white px-4 py-3 cursor-pointer hover:border-blue-300">
<input type="radio" name="content_type" value="<?php echo esc_attr($value); ?>" <?php checked($banner->content_type, $value); ?> class="h-4 w-4 text-blue-600 sodino-banner-content-type">
<span class="text-sm text-gray-700"><?php echo esc_html($label); ?></span>
</label>
<?php endforeach; ?>
</div>
</div>
</div>
<div class="grid gap-6">
<div class="sodino-banner-content-group" data-type="image">
<label class="block text-sm font-medium text-gray-700 mb-2" for="content_value_image"><?php _e('آدرس تصویر بنر', 'sodino'); ?></label>
<div class="flex gap-3">
<input type="text" name="content_value" id="content_value_image" value="<?php echo esc_attr($banner->content_type === 'image' ? $banner->content_value : ''); ?>" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100">
<button type="button" id="sodino-banner-image-upload" class="inline-flex items-center rounded-2xl border border-gray-300 bg-white px-4 py-3 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50"><?php _e('انتخاب تصویر', 'sodino'); ?></button>
</div>
</div>
<div class="sodino-banner-content-group" data-type="html">
<label class="block text-sm font-medium text-gray-700 mb-2" for="content_value_html"><?php _e('محتوای HTML بنر', 'sodino'); ?></label>
<textarea name="content_value" id="content_value_html" rows="6" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100"><?php echo esc_textarea($banner->content_type === 'html' ? $banner->content_value : ''); ?></textarea>
</div>
<div class="sodino-banner-content-group" data-type="shortcode">
<label class="block text-sm font-medium text-gray-700 mb-2" for="content_value_shortcode"><?php _e('شورت‌کد بنر', 'sodino'); ?></label>
<textarea name="content_value" id="content_value_shortcode" rows="4" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100"><?php echo esc_textarea($banner->content_type === 'shortcode' ? $banner->content_value : ''); ?></textarea>
</div>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2" for="link_url"><?php _e('لینک بنر (اختیاری)', 'sodino'); ?></label>
<input type="url" name="link_url" id="link_url" value="<?php echo esc_attr($banner->link_url); ?>" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100" placeholder="https://">
</div>
</div>
<div class="bg-white rounded-2xl border border-gray-200 p-6 shadow-sm space-y-6">
<div>
<h2 class="text-xl font-semibold text-gray-900"><?php _e('نمایش', 'sodino'); ?></h2>
<p class="mt-2 text-sm text-gray-500"><?php _e('محل و سبک نمایش بنر را مشخص کنید.', 'sodino'); ?></p>
</div>
<div class="grid gap-6 lg:grid-cols-2">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2" for="position"><?php _e('محل نمایش', 'sodino'); ?></label>
<select name="position" id="position" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100">
<option value="top" <?php selected($banner->position, 'top'); ?>><?php _e('بالای سایت', 'sodino'); ?></option>
<option value="middle" <?php selected($banner->position, 'middle'); ?>><?php _e('وسط محتوا', 'sodino'); ?></option>
<option value="bottom" <?php selected($banner->position, 'bottom'); ?>><?php _e('پایین', 'sodino'); ?></option>
<option value="product_page" <?php selected($banner->position, 'product_page'); ?>><?php _e('صفحه محصول', 'sodino'); ?></option>
<option value="cart" <?php selected($banner->position, 'cart'); ?>><?php _e('سبد خرید', 'sodino'); ?></option>
</select>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2"><?php _e('نوع نمایش', 'sodino'); ?></label>
<div class="grid gap-2">
<?php $display_types = ['inline' => __('داخل صفحه', 'sodino'), 'popup' => __('پاپ‌آپ', 'sodino'), 'floating_bar' => __('نوار شناور', 'sodino')]; ?>
<?php foreach ($display_types as $value => $label) : ?>
<label class="flex items-center gap-3 rounded-2xl border border-gray-300 bg-white px-4 py-3 cursor-pointer hover:border-blue-300">
<input type="radio" name="display_type" value="<?php echo esc_attr($value); ?>" <?php checked($banner->display_type, $value); ?> class="h-4 w-4 text-blue-600">
<span class="text-sm text-gray-700"><?php echo esc_html($label); ?></span>
</label>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<div class="bg-white rounded-2xl border border-gray-200 p-6 shadow-sm space-y-6">
<div>
<h2 class="text-xl font-semibold text-gray-900"><?php _e('زمان‌بندی', 'sodino'); ?></h2>
<p class="mt-2 text-sm text-gray-500"><?php _e('زمان شروع و پایان نمایش بنر را تنظیم کنید.', 'sodino'); ?></p>
</div>
<div class="grid gap-4 md:grid-cols-3">
<button type="button" class="sodino-banner-preset inline-flex items-center justify-center rounded-2xl border border-gray-300 bg-white px-4 py-3 text-sm font-medium text-gray-700 shadow-sm hover:border-blue-300" data-preset="today"><?php _e('فقط امروز', 'sodino'); ?></button>
<button type="button" class="sodino-banner-preset inline-flex items-center justify-center rounded-2xl border border-gray-300 bg-white px-4 py-3 text-sm font-medium text-gray-700 shadow-sm hover:border-blue-300" data-preset="weekend"><?php _e('فقط آخر هفته', 'sodino'); ?></button>
<button type="button" class="sodino-banner-preset inline-flex items-center justify-center rounded-2xl border border-gray-300 bg-white px-4 py-3 text-sm font-medium text-gray-700 shadow-sm hover:border-blue-300" data-preset="hourly"><?php _e('ساعتی (18:00 تا 23:00)', 'sodino'); ?></button>
</div>
<div class="grid gap-6 lg:grid-cols-2">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2" for="start_time"><?php _e('تاریخ شروع', 'sodino'); ?></label>
<input type="datetime-local" name="start_time" id="start_time" value="<?php echo esc_attr($banner->start_time ? date('Y-m-d\TH:i', strtotime($banner->start_time)) : ''); ?>" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2" for="end_time"><?php _e('تاریخ پایان', 'sodino'); ?></label>
<input type="datetime-local" name="end_time" id="end_time" value="<?php echo esc_attr($banner->end_time ? date('Y-m-d\TH:i', strtotime($banner->end_time)) : ''); ?>" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100">
</div>
</div>
</div>
<div class="bg-white rounded-2xl border border-gray-200 p-6 shadow-sm space-y-6">
<div>
<h2 class="text-xl font-semibold text-gray-900"><?php _e('هدف‌گذاری', 'sodino'); ?></h2>
<p class="mt-2 text-sm text-gray-500"><?php _e('نوع کاربر و دستگاه نمایش بنر را مشخص کنید.', 'sodino'); ?></p>
</div>
<div class="grid gap-6 lg:grid-cols-2">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2" for="user_target"><?php _e('نوع کاربر', 'sodino'); ?></label>
<select name="user_target" id="user_target" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100">
<option value="all" <?php selected($banner->user_target, 'all'); ?>><?php _e('همه', 'sodino'); ?></option>
<option value="new" <?php selected($banner->user_target, 'new'); ?>><?php _e('کاربر جدید', 'sodino'); ?></option>
<option value="returning" <?php selected($banner->user_target, 'returning'); ?>><?php _e('بازگشتی', 'sodino'); ?></option>
</select>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2" for="device_target"><?php _e('دستگاه', 'sodino'); ?></label>
<select name="device_target" id="device_target" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100">
<option value="all" <?php selected($banner->device_target, 'all'); ?>><?php _e('همه', 'sodino'); ?></option>
<option value="desktop" <?php selected($banner->device_target, 'desktop'); ?>><?php _e('دسکتاپ', 'sodino'); ?></option>
<option value="mobile" <?php selected($banner->device_target, 'mobile'); ?>><?php _e('موبایل', 'sodino'); ?></option>
</select>
</div>
</div>
</div>
<div class="bg-white rounded-2xl border border-gray-200 p-6 shadow-sm space-y-6">
<div>
<h2 class="text-xl font-semibold text-gray-900"><?php _e('تنظیمات پیشرفته', 'sodino'); ?></h2>
<p class="mt-2 text-sm text-gray-500"><?php _e('اولویت و وضعیت نمایش بنر را تنظیم کنید.', 'sodino'); ?></p>
</div>
<div class="grid gap-6 lg:grid-cols-3">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2" for="priority"><?php _e('اولویت', 'sodino'); ?></label>
<input type="number" name="priority" id="priority" value="<?php echo esc_attr($banner->priority); ?>" min="1" class="w-full rounded-2xl border border-gray-300 bg-white px-4 py-3 text-gray-700 shadow-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-100">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2" for="impressions"><?php _e('نمایش‌ها', 'sodino'); ?></label>
<input type="number" id="impressions" value="<?php echo esc_attr($banner->impressions); ?>" disabled class="w-full rounded-2xl border border-gray-300 bg-gray-100 px-4 py-3 text-gray-700 shadow-sm">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2" for="clicks"><?php _e('کلیک‌ها', 'sodino'); ?></label>
<input type="number" id="clicks" value="<?php echo esc_attr($banner->clicks); ?>" disabled class="w-full rounded-2xl border border-gray-300 bg-gray-100 px-4 py-3 text-gray-700 shadow-sm">
</div>
</div>
<div class="flex items-center gap-3">
<label class="inline-flex items-center gap-3 rounded-2xl border border-gray-300 bg-white px-4 py-3 cursor-pointer hover:border-blue-300">
<input type="checkbox" name="status" value="1" <?php checked($banner->status, 1); ?> class="h-4 w-4 text-blue-600">
<span class="text-sm text-gray-700"><?php _e('فعال باشد', 'sodino'); ?></span>
</label>
</div>
</div>
<div class="bg-white rounded-2xl border border-gray-200 p-6 shadow-sm text-right">
<button type="submit" class="inline-flex items-center justify-center rounded-2xl bg-blue-600 px-6 py-3 text-sm font-semibold text-white shadow-sm hover:bg-blue-700 transition-colors">
<?php echo $banner->id ? __('به‌روزرسانی بنر', 'sodino') : __('ذخیره بنر', 'sodino'); ?>
</button>
</div>
</form>
</main>
</div>
</div>
</div>

View File

@@ -0,0 +1,66 @@
<?php
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
$current_page = sanitize_text_field($_GET['page'] ?? 'sodino-banners');
?>
<div id="sodino-app" class="min-h-screen bg-gray-50" dir="rtl">
<div class="bg-white border-b border-gray-200 shadow-sm">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
<div class="flex items-center justify-between gap-4">
<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>
</div>
<a href="<?php echo admin_url('admin.php?page=sodino-add-banner'); ?>" class="inline-flex items-center gap-2 rounded-full bg-blue-600 px-5 py-3 text-sm font-semibold text-white shadow-sm hover:bg-blue-700">
<?php _e('افزودن بنر جدید', 'sodino'); ?>
</a>
</div>
</div>
</div>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div class="grid gap-8 lg:grid-cols-[280px_1fr]">
<aside class="bg-white rounded-2xl border border-gray-200 p-6 shadow-sm">
<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-xl 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-xl 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-banners'); ?>" class="block px-3 py-2 rounded-xl text-sm font-medium <?php echo $current_page === 'sodino-banners' ? '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-xl 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>
</aside>
<main class="space-y-6">
<div class="bg-white rounded-2xl border border-gray-200 p-6 shadow-sm">
<div class="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
<div>
<h2 class="text-xl font-semibold text-gray-900"><?php _e('لیست بنرها', 'sodino'); ?></h2>
<p class="mt-1 text-sm text-gray-500"><?php _e('نمایش، ویرایش، فعال/غیرفعال و حذف گروهی بنرها.', 'sodino'); ?></p>
</div>
<span class="inline-flex items-center rounded-full bg-blue-50 px-4 py-2 text-sm font-medium text-blue-700">
<?php _e('حداکثر ۲ بنر همزمان نمایش داده می‌شوند', 'sodino'); ?>
</span>
</div>
</div>
<div class="bg-white rounded-2xl border border-gray-200 p-6 shadow-sm">
<form method="post">
<?php wp_nonce_field('bulk-sodino_banners'); ?>
<?php $bannerTable->display(); ?>
</form>
</div>
</main>
</div>
</div>
</div>