refactor(Core): refactor and optimize code

This commit is contained in:
2026-05-06 00:54:24 +03:30
parent 32c065e4b6
commit dec4e67b9e
20 changed files with 1787 additions and 361 deletions

View 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');
}
}
}