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:
142
admin/class-banner-list-table.php
Normal file
142
admin/class-banner-list-table.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
if (!class_exists('WP_List_Table')) {
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
|
||||
}
|
||||
|
||||
class Sodino_Banner_List_Table extends WP_List_Table {
|
||||
private $repository;
|
||||
private $items_per_page = 20;
|
||||
|
||||
public function __construct($repository) {
|
||||
parent::__construct([
|
||||
'singular' => 'sodino_banner',
|
||||
'plural' => 'sodino_banners',
|
||||
'ajax' => false,
|
||||
]);
|
||||
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function get_columns() {
|
||||
return [
|
||||
'cb' => '<input type="checkbox" />',
|
||||
'title' => __('عنوان', 'sodino'),
|
||||
'position' => __('محل نمایش', 'sodino'),
|
||||
'display_type' => __('نوع نمایش', 'sodino'),
|
||||
'schedule' => __('زمانبندی', 'sodino'),
|
||||
'status' => __('وضعیت', 'sodino'),
|
||||
'stats' => __('آمار', 'sodino'),
|
||||
'actions' => __('عملیات', 'sodino'),
|
||||
];
|
||||
}
|
||||
|
||||
protected function get_sortable_columns() {
|
||||
return [
|
||||
'priority' => ['priority', true],
|
||||
'title' => ['title', true],
|
||||
];
|
||||
}
|
||||
|
||||
protected function column_cb($item) {
|
||||
return sprintf('<input type="checkbox" name="banner_ids[]" value="%d" />', $item->id);
|
||||
}
|
||||
|
||||
public function get_bulk_actions() {
|
||||
return [
|
||||
'delete' => __('حذف گروهی', 'sodino'),
|
||||
];
|
||||
}
|
||||
|
||||
public function column_title($item) {
|
||||
$edit_url = admin_url('admin.php?page=sodino-add-banner&action=edit&id=' . $item->id);
|
||||
return sprintf('<strong><a href="%s">%s</a></strong>', esc_url($edit_url), esc_html($item->title));
|
||||
}
|
||||
|
||||
public function column_position($item) {
|
||||
$map = [
|
||||
'top' => __('بالای سایت', 'sodino'),
|
||||
'middle' => __('وسط محتوا', 'sodino'),
|
||||
'bottom' => __('پایین', 'sodino'),
|
||||
'product_page' => __('صفحه محصول', 'sodino'),
|
||||
'cart' => __('سبد خرید', 'sodino'),
|
||||
];
|
||||
return $map[$item->position] ?? __('نامشخص', 'sodino');
|
||||
}
|
||||
|
||||
public function column_display_type($item) {
|
||||
$map = [
|
||||
'inline' => __('درون صفحه', 'sodino'),
|
||||
'popup' => __('پاپآپ', 'sodino'),
|
||||
'floating_bar' => __('نوار شناور', 'sodino'),
|
||||
];
|
||||
return $map[$item->display_type] ?? __('نامشخص', 'sodino');
|
||||
}
|
||||
|
||||
public function column_schedule($item) {
|
||||
if (!empty($item->start_time) || !empty($item->end_time)) {
|
||||
$start = $item->start_time ? date_i18n('Y/m/d H:i', strtotime($item->start_time)) : __('بدون شروع', 'sodino');
|
||||
$end = $item->end_time ? date_i18n('Y/m/d H:i', strtotime($item->end_time)) : __('بدون پایان', 'sodino');
|
||||
return sprintf('%s<br><small class="description">%s</small>', esc_html($start), esc_html($end));
|
||||
}
|
||||
return __('بدون محدودیت زمانی', 'sodino');
|
||||
}
|
||||
|
||||
public function column_status($item) {
|
||||
return $item->status ? __('فعال', 'sodino') : __('غیرفعال', 'sodino');
|
||||
}
|
||||
|
||||
public function column_stats($item) {
|
||||
$ctr = $item->impressions > 0 ? round(($item->clicks / $item->impressions) * 100, 2) : 0;
|
||||
return sprintf('<div class="sodino-stats-grid"><p>%s: <strong>%s</strong></p><p>%s: <strong>%s</strong></p><p>CTR: <strong>%s%%</strong></p></div>',
|
||||
__('نمایش', 'sodino'), esc_html(number_format_i18n($item->impressions)),
|
||||
__('کلیک', 'sodino'), esc_html(number_format_i18n($item->clicks)),
|
||||
esc_html(number_format_i18n($ctr))
|
||||
);
|
||||
}
|
||||
|
||||
public function column_actions($item) {
|
||||
$edit_url = admin_url('admin.php?page=sodino-add-banner&action=edit&id=' . $item->id);
|
||||
$toggle_url = wp_nonce_url(admin_url('admin.php?page=sodino-banners&action=toggle_banner_status&id=' . $item->id), 'toggle_banner_status');
|
||||
$delete_url = wp_nonce_url(admin_url('admin.php?page=sodino-banners&action=delete_banner&id=' . $item->id), 'delete_banner');
|
||||
$toggle_label = $item->status ? __('غیرفعال کردن', 'sodino') : __('فعال کردن', 'sodino');
|
||||
|
||||
return sprintf(
|
||||
'<a href="%s">%s</a> | <a href="%s">%s</a> | <a href="%s" onclick="return confirm(\'%s\');">%s</a>',
|
||||
esc_url($edit_url), esc_html__('ویرایش', 'sodino'),
|
||||
esc_url($toggle_url), esc_html($toggle_label),
|
||||
esc_url($delete_url), esc_js(__('آیا از حذف این بنر مطمئن هستید؟', 'sodino')),
|
||||
esc_html__('حذف', 'sodino')
|
||||
);
|
||||
}
|
||||
|
||||
public function prepare_items() {
|
||||
$this->_column_headers = [$this->get_columns(), [], $this->get_sortable_columns()];
|
||||
$this->process_bulk_action();
|
||||
|
||||
$all_items = $this->repository->getAll();
|
||||
$current_page = $this->get_pagenum();
|
||||
$total_items = count($all_items);
|
||||
$this->items = array_slice($all_items, ($current_page - 1) * $this->items_per_page, $this->items_per_page);
|
||||
|
||||
$this->set_pagination_args([
|
||||
'total_items' => $total_items,
|
||||
'per_page' => $this->items_per_page,
|
||||
'total_pages' => ceil($total_items / $this->items_per_page),
|
||||
]);
|
||||
}
|
||||
|
||||
public function process_bulk_action() {
|
||||
if ('delete' === $this->current_action()) {
|
||||
$banner_ids = isset($_POST['banner_ids']) ? array_map('intval', $_POST['banner_ids']) : [];
|
||||
if (!empty($banner_ids) && check_admin_referer('bulk-' . $this->_args['plural'])) {
|
||||
foreach ($banner_ids as $id) {
|
||||
$this->repository->delete($id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user