183 lines
6.9 KiB
PHP
183 lines
6.9 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
if (!class_exists('WP_List_Table')) {
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
|
|
}
|
|
|
|
class Sodino_Rules_List_Table extends WP_List_Table {
|
|
private $repository;
|
|
private $items_per_page = 20;
|
|
|
|
public function __construct($repository) {
|
|
parent::__construct([
|
|
'singular' => 'sodino_rule',
|
|
'plural' => 'sodino_rules',
|
|
'ajax' => false,
|
|
]);
|
|
|
|
$this->repository = $repository;
|
|
}
|
|
|
|
public function get_columns() {
|
|
return [
|
|
'cb' => '<input type="checkbox" />',
|
|
'name' => __('عنوان قانون', 'sodino'),
|
|
'conditions' => __('شرطها', 'sodino'),
|
|
'actions_summary'=> __('عملیاتها', 'sodino'),
|
|
'usage' => __('استفاده', 'sodino'),
|
|
'enabled' => __('وضعیت', 'sodino'),
|
|
'actions' => __('عملیات', 'sodino'),
|
|
];
|
|
}
|
|
|
|
protected function get_sortable_columns() {
|
|
return [
|
|
'name' => ['name', true],
|
|
];
|
|
}
|
|
|
|
protected function column_cb($item) {
|
|
return sprintf('<input type="checkbox" name="rule_ids[]" value="%d" />', $item->id);
|
|
}
|
|
|
|
public function get_bulk_actions() {
|
|
return [
|
|
'delete' => __('حذف گروهی', 'sodino'),
|
|
];
|
|
}
|
|
|
|
public function column_actions($item) {
|
|
$edit_url = admin_url('admin.php?page=sodino-add-rule&action=edit&id=' . $item->id);
|
|
$delete_url = wp_nonce_url(admin_url('admin.php?page=sodino-rules&action=delete&id=' . $item->id), 'delete_rule');
|
|
|
|
return sprintf(
|
|
'<a href="%s">%s</a> | <a href="%s" onclick="return confirm(\'%s\');">%s</a>',
|
|
esc_url($edit_url),
|
|
esc_html__('ویرایش', 'sodino'),
|
|
esc_url($delete_url),
|
|
esc_js(__('آیا از حذف این قانون مطمئن هستید؟', 'sodino')),
|
|
esc_html__('حذف', 'sodino')
|
|
);
|
|
}
|
|
|
|
public function column_name($item) {
|
|
$edit_url = admin_url('admin.php?page=sodino-add-rule&action=edit&id=' . $item->id);
|
|
$title = sprintf('<strong><a href="%s">%s</a></strong>', esc_url($edit_url), esc_html($item->name));
|
|
return $title;
|
|
}
|
|
|
|
private function conditionLabels() {
|
|
return [
|
|
'user_type' => __('نوع کاربر', 'sodino'),
|
|
'product_category' => __('دستهبندی محصول', 'sodino'),
|
|
'exclude_product_category' => __('بهجز دستهبندی', 'sodino'),
|
|
'product_tag' => __('برچسب محصول', 'sodino'),
|
|
'product_ids' => __('محصولات خاص', 'sodino'),
|
|
'exclude_product_ids' => __('بهجز محصولات', 'sodino'),
|
|
'cart_total_min' => __('حداقل مبلغ سبد', 'sodino'),
|
|
'cart_total_max' => __('حداکثر مبلغ سبد', 'sodino'),
|
|
'cart_item_count_min' => __('حداقل تعداد سبد', 'sodino'),
|
|
'cart_item_count_max' => __('حداکثر تعداد سبد', 'sodino'),
|
|
'cart_contains_product' => __('سبد شامل محصول', 'sodino'),
|
|
'cart_contains_category' => __('سبد شامل دستهبندی', 'sodino'),
|
|
'customer_order_count_min' => __('حداقل سفارش مشتری', 'sodino'),
|
|
'customer_order_count_max' => __('حداکثر سفارش مشتری', 'sodino'),
|
|
'day_of_week' => __('روز هفته', 'sodino'),
|
|
];
|
|
}
|
|
|
|
public function column_conditions($item) {
|
|
$labels = [
|
|
];
|
|
$labels = $this->conditionLabels();
|
|
$parts = [];
|
|
foreach ((array) $item->conditions as $condition) {
|
|
$type = $condition['type'] ?? '';
|
|
$value = $condition['value'] ?? '';
|
|
$parts[] = esc_html(sprintf('%s: %s', $labels[$type] ?? $type, is_array($value) ? implode(',', $value) : $value));
|
|
}
|
|
return $parts ? implode('<br>', $parts) : esc_html__('بدون شرط', 'sodino');
|
|
}
|
|
|
|
public function column_actions_summary($item) {
|
|
$labels = [
|
|
'discount_percent' => __('درصد تخفیف', 'sodino'),
|
|
'discount_fixed' => __('تخفیف ثابت', 'sodino'),
|
|
'set_price' => __('قیمت ثابت', 'sodino'),
|
|
'increase_percent' => __('افزایش درصدی', 'sodino'),
|
|
'increase_fixed' => __('افزایش ثابت', 'sodino'),
|
|
'free_shipping' => __('ارسال رایگان', 'sodino'),
|
|
];
|
|
|
|
$parts = [];
|
|
foreach ((array) $item->actions as $action) {
|
|
$type = $action['type'] ?? '';
|
|
$value = $action['value'] ?? 0;
|
|
$parts[] = esc_html(sprintf('%s: %s', $labels[$type] ?? $type, $type === 'free_shipping' ? '-' : $value));
|
|
}
|
|
return $parts ? implode('<br>', $parts) : esc_html__('بدون عملیات', 'sodino');
|
|
}
|
|
|
|
public function column_usage($item) {
|
|
$limit = (int) $item->usage_limit;
|
|
if ($limit <= 0) {
|
|
return esc_html(sprintf('%s / %s', number_format_i18n($item->usage_count), __('نامحدود', 'sodino')));
|
|
}
|
|
return esc_html(sprintf('%s / %s', number_format_i18n($item->usage_count), number_format_i18n($limit)));
|
|
}
|
|
|
|
public function column_enabled($item) {
|
|
return $item->enabled ? __('فعال', 'sodino') : __('غیرفعال', 'sodino');
|
|
}
|
|
|
|
public function column_default($item, $column_name) {
|
|
switch ($column_name) {
|
|
case 'name':
|
|
case 'conditions':
|
|
case 'actions_summary':
|
|
case 'usage':
|
|
case 'enabled':
|
|
case 'actions':
|
|
return '';
|
|
default:
|
|
return '';
|
|
}
|
|
}
|
|
|
|
public function prepare_items() {
|
|
$columns = $this->get_columns();
|
|
$hidden = [];
|
|
$sortable = $this->get_sortable_columns();
|
|
|
|
$this->_column_headers = [$columns, $hidden, $sortable];
|
|
|
|
$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()) {
|
|
$rule_ids = isset($_POST['rule_ids']) ? array_map('intval', $_POST['rule_ids']) : [];
|
|
if (!empty($rule_ids) && check_admin_referer('bulk-' . $this->_args['plural'])) {
|
|
foreach ($rule_ids as $id) {
|
|
$this->repository->delete($id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|