refactor(Core): optimize admin panel and refactor
This commit is contained in:
103
admin/admin.php
103
admin/admin.php
@@ -25,6 +25,86 @@ $dashboardController = new DashboardController($eventRepository, $ruleRepository
|
||||
$settingsController = new SettingsController();
|
||||
$adminController = new AdminController($ruleRepository, $upsellRepository, $bannerRepository);
|
||||
|
||||
function sodino_admin_notice_key() {
|
||||
return 'sodino_admin_notice_' . get_current_user_id();
|
||||
}
|
||||
|
||||
function sodino_old_input_key() {
|
||||
return 'sodino_old_input_' . get_current_user_id();
|
||||
}
|
||||
|
||||
function sodino_get_admin_notice($delete = false) {
|
||||
$notice = get_transient(sodino_admin_notice_key());
|
||||
|
||||
if (!$notice) {
|
||||
$notice = get_transient('sodino_admin_notice');
|
||||
}
|
||||
|
||||
if ($notice && $delete) {
|
||||
delete_transient(sodino_admin_notice_key());
|
||||
delete_transient('sodino_admin_notice');
|
||||
}
|
||||
|
||||
return is_array($notice) ? $notice : null;
|
||||
}
|
||||
|
||||
function sodino_get_old_input($delete = false) {
|
||||
$old = get_transient(sodino_old_input_key());
|
||||
|
||||
if (is_array($old) && isset($old['_sodino_page'])) {
|
||||
$current_page = isset($_GET['page']) ? sanitize_key($_GET['page']) : '';
|
||||
if ($current_page !== sanitize_key($old['_sodino_page'])) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
if ($old && $delete) {
|
||||
delete_transient(sodino_old_input_key());
|
||||
}
|
||||
|
||||
return is_array($old) ? $old : [];
|
||||
}
|
||||
|
||||
function sodino_old_input($key, $default = '') {
|
||||
static $old = null;
|
||||
|
||||
if ($old === null) {
|
||||
$old = sodino_get_old_input(false);
|
||||
}
|
||||
|
||||
if (strpos($key, '.') === false) {
|
||||
return array_key_exists($key, $old) ? $old[$key] : $default;
|
||||
}
|
||||
|
||||
$value = $old;
|
||||
foreach (explode('.', $key) as $part) {
|
||||
if (!is_array($value) || !array_key_exists($part, $value)) {
|
||||
return $default;
|
||||
}
|
||||
$value = $value[$part];
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
function sodino_render_admin_notice() {
|
||||
$notice = sodino_get_admin_notice(true);
|
||||
if (!$notice) {
|
||||
return;
|
||||
}
|
||||
|
||||
$type = $notice['type'] ?? 'success';
|
||||
$class = $type === 'error' ? 'sodino-form-notice sodino-form-notice-error' : 'sodino-form-notice sodino-form-notice-success';
|
||||
$title = $type === 'error' ? __('خطای فرم', 'sodino') : __('عملیات موفق', 'sodino');
|
||||
|
||||
printf(
|
||||
'<div class="%s" role="alert"><strong>%s</strong><p>%s</p></div>',
|
||||
esc_attr($class),
|
||||
esc_html($title),
|
||||
esc_html($notice['message'] ?? '')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add admin menu
|
||||
*/
|
||||
@@ -102,6 +182,15 @@ add_action('admin_menu', function() use ($adminController) {
|
||||
[$adminController, 'addBannerPage']
|
||||
);
|
||||
|
||||
add_submenu_page(
|
||||
'sodino-dashboard',
|
||||
__('قیمت رقبا (بهزودی)', 'sodino'),
|
||||
__('قیمت رقبا (بهزودی)', 'sodino'),
|
||||
'manage_options',
|
||||
'sodino-competitor-price',
|
||||
[$adminController, 'competitorPricePage']
|
||||
);
|
||||
|
||||
add_submenu_page(
|
||||
'sodino-dashboard',
|
||||
__('تنظیمات', 'sodino'),
|
||||
@@ -125,16 +214,12 @@ add_action('admin_enqueue_scripts', function($hook) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Enqueue Tailwind via CDN
|
||||
wp_enqueue_script('sodino-tailwind', 'https://cdn.tailwindcss.com', [], SODINO_VERSION);
|
||||
|
||||
// Admin CSS
|
||||
wp_enqueue_style('sodino-admin', plugin_dir_url(__FILE__) . 'css/admin.css', [], SODINO_VERSION);
|
||||
|
||||
// Dashboard specific scripts
|
||||
if (strpos($hook, 'sodino-dashboard') !== false || strpos($hook, 'sodino_page_sodino-dashboard') !== false) {
|
||||
wp_enqueue_script('sodino-chart-js', 'https://cdn.jsdelivr.net/npm/chart.js', [], null, true);
|
||||
wp_enqueue_script('sodino-dashboard-js', plugin_dir_url(__FILE__) . 'js/dashboard.js', ['sodino-chart-js'], SODINO_VERSION, true);
|
||||
wp_enqueue_script('sodino-dashboard-js', plugin_dir_url(__FILE__) . 'js/dashboard.js', [], SODINO_VERSION, true);
|
||||
}
|
||||
|
||||
// Upsell specific scripts
|
||||
@@ -185,7 +270,12 @@ add_action('admin_init', function() use ($ruleController, $settingsController, $
|
||||
* Show admin notices
|
||||
*/
|
||||
add_action('admin_notices', function() {
|
||||
$notice = get_transient('sodino_admin_notice');
|
||||
$page = isset($_GET['page']) ? sanitize_key($_GET['page']) : '';
|
||||
if (strpos($page, 'sodino') === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$notice = sodino_get_admin_notice(true);
|
||||
|
||||
if ($notice) {
|
||||
$class = $notice['type'] === 'error' ? 'notice-error' : 'notice-success';
|
||||
@@ -194,6 +284,5 @@ add_action('admin_notices', function() {
|
||||
esc_attr($class),
|
||||
esc_html($notice['message'])
|
||||
);
|
||||
delete_transient('sodino_admin_notice');
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user