190 lines
5.6 KiB
PHP
190 lines
5.6 KiB
PHP
<?php
|
|
namespace Sodino\Controllers;
|
|
|
|
use Sodino\Repositories\RuleRepository;
|
|
use Sodino\Models\Rule;
|
|
|
|
/**
|
|
* Rule Controller
|
|
*/
|
|
class RuleController extends BaseController {
|
|
private $ruleRepository;
|
|
|
|
public function __construct(RuleRepository $ruleRepository) {
|
|
$this->ruleRepository = $ruleRepository;
|
|
}
|
|
|
|
/**
|
|
* List rules page
|
|
*/
|
|
public function index() {
|
|
$this->checkCapability();
|
|
|
|
require_once SODINO_PLUGIN_DIR . 'admin/class-rules-list-table.php';
|
|
$rulesTable = new \Sodino_Rules_List_Table($this->ruleRepository);
|
|
$rulesTable->prepare_items();
|
|
|
|
$this->render('rules-list', [
|
|
'rulesTable' => $rulesTable,
|
|
'current_page' => 'sodino-rules'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Create rule page
|
|
*/
|
|
public function create() {
|
|
$this->checkCapability();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
return $this->store();
|
|
}
|
|
|
|
$rule = new Rule();
|
|
$this->render('rule-form', [
|
|
'rule' => $rule,
|
|
'current_page' => 'sodino-add-rule'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Edit rule page
|
|
*/
|
|
public function edit() {
|
|
$this->checkCapability();
|
|
|
|
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
|
|
$rule = $this->ruleRepository->getById($id);
|
|
|
|
if (!$rule) {
|
|
$this->redirect(
|
|
admin_url('admin.php?page=sodino-rules'),
|
|
__('قانون یافت نشد.', 'sodino'),
|
|
'error'
|
|
);
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
return $this->update($rule);
|
|
}
|
|
|
|
$this->render('rule-form', [
|
|
'rule' => $rule,
|
|
'current_page' => 'sodino-add-rule'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store new rule
|
|
*/
|
|
private function store() {
|
|
$this->verifyNonce('sodino_rule_nonce', 'sodino_save_rule');
|
|
|
|
$validator = $this->validate($_POST);
|
|
$validator->required('name', __('نام قانون الزامی است.', 'sodino'))
|
|
->numeric('priority')
|
|
->min('priority', 1)
|
|
->numeric('usage_limit')
|
|
->min('usage_limit', 0);
|
|
|
|
if ($validator->fails()) {
|
|
$this->redirect(
|
|
admin_url('admin.php?page=sodino-add-rule'),
|
|
$validator->firstError(),
|
|
'error'
|
|
);
|
|
}
|
|
|
|
$rule = new Rule();
|
|
$this->fillRuleFromPost($rule);
|
|
$this->ruleRepository->save($rule);
|
|
|
|
$this->redirect(
|
|
admin_url('admin.php?page=sodino-rules'),
|
|
__('قانون با موفقیت ایجاد شد.', 'sodino')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Update existing rule
|
|
*/
|
|
private function update($rule) {
|
|
$this->verifyNonce('sodino_rule_nonce', 'sodino_save_rule');
|
|
|
|
$validator = $this->validate($_POST);
|
|
$validator->required('name', __('نام قانون الزامی است.', 'sodino'))
|
|
->numeric('priority')
|
|
->min('priority', 1)
|
|
->numeric('usage_limit')
|
|
->min('usage_limit', 0);
|
|
|
|
if ($validator->fails()) {
|
|
$this->redirect(
|
|
admin_url('admin.php?page=sodino-add-rule&action=edit&id=' . $rule->id),
|
|
$validator->firstError(),
|
|
'error'
|
|
);
|
|
}
|
|
|
|
$this->fillRuleFromPost($rule);
|
|
$this->ruleRepository->save($rule);
|
|
|
|
$this->redirect(
|
|
admin_url('admin.php?page=sodino-rules'),
|
|
__('قانون با موفقیت بهروزرسانی شد.', 'sodino')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Delete rule
|
|
*/
|
|
public function delete() {
|
|
$this->checkCapability();
|
|
|
|
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);
|
|
|
|
$this->redirect(
|
|
admin_url('admin.php?page=sodino-rules'),
|
|
__('قانون با موفقیت حذف شد.', 'sodino')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Fill rule from POST data
|
|
*/
|
|
private function fillRuleFromPost($rule) {
|
|
$rule->name = sanitize_text_field($_POST['name'] ?? '');
|
|
$rule->priority = max(1, intval($_POST['priority'] ?? 10));
|
|
$rule->usage_limit = max(0, intval($_POST['usage_limit'] ?? 0));
|
|
$rule->user_roles = array_map('sanitize_text_field', (array) ($_POST['user_roles'] ?? []));
|
|
$rule->start_date = !empty($_POST['start_date']) ? sanitize_text_field($_POST['start_date']) : null;
|
|
$rule->end_date = !empty($_POST['end_date']) ? sanitize_text_field($_POST['end_date']) : null;
|
|
$rule->enabled = isset($_POST['enabled']) ? 1 : 0;
|
|
|
|
// Parse conditions
|
|
if (isset($_POST['conditions']) && is_array($_POST['conditions'])) {
|
|
$rule->conditions = array_map(function($condition) {
|
|
return [
|
|
'type' => sanitize_text_field($condition['type'] ?? ''),
|
|
'value' => sanitize_text_field($condition['value'] ?? '')
|
|
];
|
|
}, $_POST['conditions']);
|
|
}
|
|
|
|
// Parse actions
|
|
if (isset($_POST['actions']) && is_array($_POST['actions'])) {
|
|
$rule->actions = array_map(function($action) {
|
|
return [
|
|
'type' => sanitize_text_field($action['type'] ?? ''),
|
|
'value' => sanitize_text_field($action['value'] ?? '')
|
|
];
|
|
}, $_POST['actions']);
|
|
}
|
|
}
|
|
}
|