refactor(Core): refactor and optimize code
This commit is contained in:
94
app/Controllers/BaseController.php
Normal file
94
app/Controllers/BaseController.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
namespace Sodino\Controllers;
|
||||
|
||||
use Sodino\Core\Validator;
|
||||
|
||||
/**
|
||||
* Base Controller
|
||||
*/
|
||||
abstract class BaseController {
|
||||
|
||||
/**
|
||||
* Verify nonce
|
||||
*/
|
||||
protected function verifyNonce($nonce_field, $nonce_action) {
|
||||
if (!isset($_POST[$nonce_field]) || !wp_verify_nonce($_POST[$nonce_field], $nonce_action)) {
|
||||
wp_die(__('خطای امنیتی رخ داد.', 'sodino'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect with message
|
||||
*/
|
||||
protected function redirect($url, $message = '', $type = 'success') {
|
||||
if ($message) {
|
||||
set_transient('sodino_admin_notice', [
|
||||
'message' => $message,
|
||||
'type' => $type
|
||||
], 30);
|
||||
}
|
||||
wp_safe_redirect($url);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sanitized POST data
|
||||
*/
|
||||
protected function getPostData($key, $default = '') {
|
||||
return isset($_POST[$key]) ? sanitize_text_field($_POST[$key]) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sanitized GET data
|
||||
*/
|
||||
protected function getQueryData($key, $default = '') {
|
||||
return isset($_GET[$key]) ? sanitize_text_field($_GET[$key]) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate data
|
||||
*/
|
||||
protected function validate(array $data) {
|
||||
return Validator::make($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render view
|
||||
*/
|
||||
protected function render($view, $data = []) {
|
||||
extract($data);
|
||||
$view_file = SODINO_PLUGIN_DIR . 'admin/views/' . $view . '.php';
|
||||
|
||||
if (file_exists($view_file)) {
|
||||
include $view_file;
|
||||
} else {
|
||||
wp_die(sprintf(__('View file not found: %s', 'sodino'), $view));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check user capability
|
||||
*/
|
||||
protected function checkCapability($capability = 'manage_options') {
|
||||
if (!current_user_can($capability)) {
|
||||
wp_die(__('شما دسترسی لازم برای این عملیات را ندارید.', 'sodino'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show admin notice
|
||||
*/
|
||||
public function showAdminNotice() {
|
||||
$notice = get_transient('sodino_admin_notice');
|
||||
|
||||
if ($notice) {
|
||||
$class = $notice['type'] === 'error' ? 'notice-error' : 'notice-success';
|
||||
printf(
|
||||
'<div class="notice %s is-dismissible"><p>%s</p></div>',
|
||||
esc_attr($class),
|
||||
esc_html($notice['message'])
|
||||
);
|
||||
delete_transient('sodino_admin_notice');
|
||||
}
|
||||
}
|
||||
}
|
||||
48
app/Controllers/DashboardController.php
Normal file
48
app/Controllers/DashboardController.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace Sodino\Controllers;
|
||||
|
||||
use Sodino\Repositories\EventRepository;
|
||||
use Sodino\Repositories\RuleRepository;
|
||||
use Sodino\Services\AnalyticsService;
|
||||
|
||||
/**
|
||||
* Dashboard Controller
|
||||
*/
|
||||
class DashboardController extends BaseController {
|
||||
private $analyticsService;
|
||||
|
||||
public function __construct(EventRepository $eventRepository, RuleRepository $ruleRepository) {
|
||||
$this->analyticsService = new AnalyticsService($eventRepository, $ruleRepository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dashboard page
|
||||
*/
|
||||
public function index() {
|
||||
$this->checkCapability();
|
||||
|
||||
$filters = [
|
||||
'range' => $this->getQueryData('range', '7d'),
|
||||
'start_date' => $this->getQueryData('start_date', ''),
|
||||
'end_date' => $this->getQueryData('end_date', ''),
|
||||
'product_id' => intval($this->getQueryData('product_id', 0)),
|
||||
'category_id' => intval($this->getQueryData('category_id', 0)),
|
||||
];
|
||||
|
||||
if (!empty($filters['product_id'])) {
|
||||
$filters['product_ids'] = [$filters['product_id']];
|
||||
}
|
||||
|
||||
$dashboardData = $this->analyticsService->getDashboardData($filters);
|
||||
$productOptions = $this->analyticsService->getProductOptions();
|
||||
$categoryOptions = $this->analyticsService->getCategoryOptions();
|
||||
|
||||
$this->render('dashboard', [
|
||||
'dashboardData' => $dashboardData,
|
||||
'productOptions' => $productOptions,
|
||||
'categoryOptions' => $categoryOptions,
|
||||
'filters' => $filters,
|
||||
'current_page' => 'sodino-dashboard'
|
||||
]);
|
||||
}
|
||||
}
|
||||
189
app/Controllers/RuleController.php
Normal file
189
app/Controllers/RuleController.php
Normal file
@@ -0,0 +1,189 @@
|
||||
<?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']);
|
||||
}
|
||||
}
|
||||
}
|
||||
87
app/Controllers/SettingsController.php
Normal file
87
app/Controllers/SettingsController.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
namespace Sodino\Controllers;
|
||||
|
||||
use Sodino\Core\Settings;
|
||||
use Sodino\Core\Cache;
|
||||
|
||||
/**
|
||||
* Settings Controller
|
||||
*/
|
||||
class SettingsController extends BaseController {
|
||||
private $settings;
|
||||
private $cache;
|
||||
|
||||
public function __construct() {
|
||||
$this->settings = Settings::getInstance();
|
||||
$this->cache = Cache::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings page
|
||||
*/
|
||||
public function index() {
|
||||
$this->checkCapability();
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
return $this->save();
|
||||
}
|
||||
|
||||
$settings = $this->settings->all();
|
||||
$this->render('settings', [
|
||||
'settings' => $settings,
|
||||
'current_page' => 'sodino-settings'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save settings
|
||||
*/
|
||||
private function save() {
|
||||
$this->verifyNonce('sodino_settings_nonce', 'sodino_save_settings');
|
||||
|
||||
$settings = [
|
||||
'plugin_enabled' => isset($_POST['plugin_enabled']) ? 1 : 0,
|
||||
'pricing_enabled' => isset($_POST['pricing_enabled']) ? 1 : 0,
|
||||
'upsell_enabled' => isset($_POST['upsell_enabled']) ? 1 : 0,
|
||||
'banner_enabled' => isset($_POST['banner_enabled']) ? 1 : 0,
|
||||
'allow_multiple_rules' => isset($_POST['allow_multiple_rules']) ? 1 : 0,
|
||||
'strategy' => sanitize_text_field($_POST['strategy'] ?? 'priority'),
|
||||
'max_discount_percent' => max(0, min(100, floatval($_POST['max_discount_percent'] ?? 100))),
|
||||
'min_product_price' => max(0, floatval($_POST['min_product_price'] ?? 0)),
|
||||
'ab_testing_enabled' => isset($_POST['ab_testing_enabled']) ? 1 : 0,
|
||||
'cart_pricing_enabled' => isset($_POST['cart_pricing_enabled']) ? 1 : 0,
|
||||
'scheduled_campaigns_enabled' => isset($_POST['scheduled_campaigns_enabled']) ? 1 : 0,
|
||||
'cache_enabled' => isset($_POST['cache_enabled']) ? 1 : 0,
|
||||
'cache_duration' => max(60, intval($_POST['cache_duration'] ?? 3600)),
|
||||
'debug_mode' => isset($_POST['debug_mode']) ? 1 : 0,
|
||||
];
|
||||
|
||||
$this->settings->update($settings);
|
||||
|
||||
// Clear cache when settings change
|
||||
$this->cache->clearAll();
|
||||
|
||||
$this->redirect(
|
||||
admin_url('admin.php?page=sodino-settings'),
|
||||
__('تنظیمات با موفقیت ذخیره شد.', 'sodino')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear cache action
|
||||
*/
|
||||
public function clearCache() {
|
||||
$this->checkCapability();
|
||||
|
||||
if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'clear_cache')) {
|
||||
wp_die(__('خطای امنیتی رخ داد.', 'sodino'));
|
||||
}
|
||||
|
||||
$this->cache->clearAll();
|
||||
|
||||
$this->redirect(
|
||||
admin_url('admin.php?page=sodino-settings'),
|
||||
__('کش با موفقیت پاک شد.', 'sodino')
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user