Init(Core): create and add project
This commit is contained in:
132
admin/class-rules-list-table.php
Normal file
132
admin/class-rules-list-table.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?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'),
|
||||
'condition_type' => __('نوع کاربر', 'sodino'),
|
||||
'action_value' => __('درصد تخفیف', '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;
|
||||
}
|
||||
|
||||
public function column_condition_type($item) {
|
||||
$value = __('کاربر جدید', 'sodino');
|
||||
if ($item->condition_value === 'returning') {
|
||||
$value = __('کاربر بازگشتی', 'sodino');
|
||||
}
|
||||
return esc_html($value);
|
||||
}
|
||||
|
||||
public function column_action_value($item) {
|
||||
return sprintf('%s %%', esc_html($item->action_value));
|
||||
}
|
||||
|
||||
public function column_enabled($item) {
|
||||
return $item->enabled ? __('فعال', 'sodino') : __('غیرفعال', 'sodino');
|
||||
}
|
||||
|
||||
public function column_default($item, $column_name) {
|
||||
switch ($column_name) {
|
||||
case 'name':
|
||||
case 'condition_type':
|
||||
case 'action_value':
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user