158 lines
4.3 KiB
PHP
158 lines
4.3 KiB
PHP
<?php
|
|
namespace Sodino\Controllers;
|
|
|
|
use Sodino\Repositories\RuleRepository;
|
|
use Sodino\Models\Rule;
|
|
|
|
/**
|
|
* Admin Controller
|
|
*/
|
|
class AdminController {
|
|
private $ruleRepository;
|
|
|
|
public function __construct(RuleRepository $ruleRepository) {
|
|
$this->ruleRepository = $ruleRepository;
|
|
}
|
|
|
|
/**
|
|
* Handle admin menu
|
|
*/
|
|
public function addMenu() {
|
|
add_menu_page(
|
|
__('قیمتیار', 'sodino'),
|
|
__('قیمتیار', 'sodino'),
|
|
'manage_options',
|
|
'sodino-rules',
|
|
[$this, 'rulesPage'],
|
|
'dashicons-money-alt',
|
|
56
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-rules',
|
|
__('قوانین قیمتگذاری', 'sodino'),
|
|
__('قوانین قیمتگذاری', 'sودino'),
|
|
'manage_options',
|
|
'sodino-rules',
|
|
[$this, 'rulesPage']
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-rules',
|
|
__('افزودن قانون', 'sودino'),
|
|
__('افزودن قانون', 'sودino'),
|
|
'manage_options',
|
|
'sodino-add-rule',
|
|
[$this, 'addRulePage']
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-rules',
|
|
__('تنظیمات', 'sodino'),
|
|
__('تنظیمات', 'sودینو'),
|
|
'manage_options',
|
|
'sodino-settings',
|
|
[$this, 'settingsPage']
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Rules admin page
|
|
*/
|
|
public function rulesPage() {
|
|
$this->listRulesPage();
|
|
}
|
|
|
|
/**
|
|
* List rules page
|
|
*/
|
|
private function listRulesPage() {
|
|
require_once SODINO_PLUGIN_DIR . 'admin/class-rules-list-table.php';
|
|
|
|
$rulesTable = new \Sodino_Rules_List_Table($this->ruleRepository);
|
|
$rulesTable->prepare_items();
|
|
|
|
include SODINO_PLUGIN_DIR . 'admin/views/rules-list.php';
|
|
}
|
|
|
|
/**
|
|
* Add or edit rule page
|
|
*/
|
|
public function addRulePage() {
|
|
if (isset($_GET['action']) && $_GET['action'] === 'edit') {
|
|
return $this->editRulePage();
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$this->saveRule();
|
|
} else {
|
|
$rule = new Rule();
|
|
include SODINO_PLUGIN_DIR . 'admin/views/rule-form.php';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Settings page
|
|
*/
|
|
public function settingsPage() {
|
|
include SODINO_PLUGIN_DIR . 'admin/views/settings.php';
|
|
}
|
|
|
|
/**
|
|
* Edit rule page
|
|
*/
|
|
private function editRulePage() {
|
|
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
|
|
$rule = $this->ruleRepository->getById($id);
|
|
|
|
if (!$rule) {
|
|
wp_die(__('Rule not found', 'sodino'));
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$this->saveRule($rule);
|
|
} else {
|
|
include SODINO_PLUGIN_DIR . 'admin/views/rule-form.php';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Save rule
|
|
*/
|
|
private function saveRule($rule = null) {
|
|
if (!isset($_POST['gheymatyar_rule_nonce']) || !wp_verify_nonce($_POST['gheymatyar_rule_nonce'], 'gheymatyar_save_rule')) {
|
|
wp_die(__('خطای امنیتی رخ داد.', 'sodino'));
|
|
}
|
|
|
|
if (!$rule) {
|
|
$rule = new Rule();
|
|
}
|
|
|
|
$rule->name = sanitize_text_field($_POST['name'] ?? '');
|
|
$rule->condition_type = sanitize_text_field($_POST['condition_type'] ?? 'user_type');
|
|
$rule->condition_value = sanitize_text_field($_POST['condition_value'] ?? 'new');
|
|
$rule->action_type = sanitize_text_field($_POST['action_type'] ?? 'discount_percent');
|
|
$rule->action_value = sanitize_text_field($_POST['action_value'] ?? '0');
|
|
$rule->enabled = isset($_POST['enabled']) ? 1 : 0;
|
|
|
|
$this->ruleRepository->save($rule);
|
|
|
|
wp_safe_redirect(admin_url('admin.php?page=sodino-rules'));
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Handle delete action
|
|
*/
|
|
public function handleDelete() {
|
|
if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'delete_rule')) {
|
|
wp_die(__('خطای امنیتی رخ داد.', 'sodino'));
|
|
}
|
|
|
|
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
|
|
$this->ruleRepository->delete($id);
|
|
|
|
wp_redirect(admin_url('admin.php?page=sodino-rules'));
|
|
exit;
|
|
}
|
|
} |