Hệ Thống Quản Trị
Hệ Thống Quản Trị của Jankx cung cấp các công cụ toàn diện để quản lý theme, xử lý options và form.
Form Handler
Class FormHandler xử lý các form submission trong admin với kiểm tra bảo mật tích hợp.
use Jankx\Admin\Handlers\FormHandler;
use Jankx\Foundation\Application;
$app = Application::getInstance();
$formHandler = new FormHandler($app);
// Xử lý requests (thường gọi trên admin_init)
add_action('admin_init', [$formHandler, 'handleRequests']);
Các Action Có Sẵn
| Action | Mô Tả | Capability |
|---|---|---|
save_image_sizes | Lưu cài đặt kích thước ảnh | manage_options |
clear_debug_log | Xóa file debug log | manage_options |
activate_license | Kích hoạt license PRO | manage_options |
deactivate_license | Hủy kích hoạt license PRO | manage_options |
Action Tùy Chỉnh
// Đăng ký action handler tùy chỉnh
add_action('jankx/form/handle/{action}', function($data, $formHandler) {
// Xử lý action tùy chỉnh
update_option('my_custom_option', $data['value']);
// Thêm thông báo thành công
$formHandler->addAdminNotice('Đã lưu cài đặt!', 'success');
}, 10, 2);
Thông Báo Admin
Hiển thị thông báo phản hồi cho người dùng admin.
// Thêm thông báo
$formHandler->addAdminNotice('Đã lưu cài đặt!', 'success');
$formHandler->addAdminNotice('Vui lòng kiểm tra input', 'warning');
$formHandler->addAdminNotice('Đã xảy ra lỗi', 'error');
$formHandler->addAdminNotice('Thông tin tham khảo', 'info');
Các Loại Thông Báo
| Loại | Màu | Dùng Khi |
|---|---|---|
success | Xanh lá | Thao tác thành công |
warning | Vàng | Cần chú ý |
error | Đỏ | Thao tác thất bại |
info | Xanh dương | Thông tin |
Theme Options
Options Service
class ThemeOptionsService
{
protected $options = [];
public function get(string $key, $default = null)
{
return $this->options[$key] ?? $default;
}
public function set(string $key, $value): void
{
$this->options[$key] = $value;
update_option('jankx_options', $this->options);
}
}
Font Manager PRO
use Jankx\Facades\Fonts;
// Thêm font tùy chỉnh
Fonts::add([
'name' => 'Custom Font',
'family' => 'Custom, sans-serif',
'category' => 'custom',
]);
// Lấy fonts đang active
$fonts = Fonts::getActive();
// Áp dụng font cho element
Fonts::apply('#header', 'Custom Font');
Icon Manager PRO
use Jankx\Facades\Icons;
// Lấy icons theo loại
$icons = Icons::get('fontawesome');
// Lấy tất cả icon sets
$sets = Icons::getSets();
// Render icon
Icons::render('star', ['class' => 'text-primary']);
Bảo Mật
Xác Thực Nonce
// Tạo nonce
$nonce = wp_create_nonce('my_action');
// Xác thực trong handler
if (!wp_verify_nonce($_POST['_wpnonce'] ?? '', 'my_action')) {
wp_die('Kiểm tra bảo mật thất bại');
}
Kiểm Tra Capability
// Kiểm tra quyền user
if (!current_user_can('manage_options')) {
wp_die('Bạn không có quyền');
}
Làm Sạch Dữ Liệu
// FormHandler cung cấp sanitization tích hợp
$data = $this->sanitizeRequestData($_POST);
// Sanitization thủ công
$clean = [
'text' => sanitize_text_field($input['text']),
'email' => sanitize_email($input['email']),
'url' => esc_url_raw($input['url']),
'html' => wp_kses_post($input['html']),
];
Các Nguyên Tắc Tốt
- Luôn Xác Thực Nonces - Dùng
wp_verify_nonce()cho form submissions - Kiểm Tra Capability - Xác minh user có quyền phù hợp
- Xác Thực Input - Làm sạch tất cả user input
- Escape Output - Dùng
esc_html(),esc_url(),esc_attr() - Dùng Thông Báo - Cung cấp phản hồi cho user actions