289 lines
8.3 KiB
PHP
289 lines
8.3 KiB
PHP
<?php
|
|
// Prevent direct access
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
use Sodino\Controllers\RuleController;
|
|
use Sodino\Controllers\DashboardController;
|
|
use Sodino\Controllers\SettingsController;
|
|
use Sodino\Controllers\AdminController;
|
|
use Sodino\Repositories\BannerRepository;
|
|
use Sodino\Repositories\RuleRepository;
|
|
use Sodino\Repositories\UpsellRepository;
|
|
use Sodino\Repositories\EventRepository;
|
|
|
|
// Initialize repositories
|
|
$ruleRepository = new RuleRepository();
|
|
$upsellRepository = new UpsellRepository();
|
|
$bannerRepository = new BannerRepository();
|
|
$eventRepository = new EventRepository();
|
|
|
|
// Initialize controllers
|
|
$ruleController = new RuleController($ruleRepository);
|
|
$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
|
|
*/
|
|
add_action('admin_menu', function() use ($adminController) {
|
|
add_menu_page(
|
|
__('سودینو', 'sodino'),
|
|
__('سودینو', 'sodino'),
|
|
'manage_options',
|
|
'sodino-dashboard',
|
|
[$adminController, 'dashboardPage'],
|
|
'dashicons-money-alt',
|
|
56
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-dashboard',
|
|
__('داشبورد', 'sodino'),
|
|
__('داشبورد', 'sodino'),
|
|
'manage_options',
|
|
'sodino-dashboard',
|
|
[$adminController, 'dashboardPage']
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-dashboard',
|
|
__('قوانین قیمتگذاری', 'sodino'),
|
|
__('قوانین قیمتگذاری', 'sodino'),
|
|
'manage_options',
|
|
'sodino-rules',
|
|
[$adminController, 'rulesPage']
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-dashboard',
|
|
__('افزودن قانون', 'sodino'),
|
|
__('افزودن قانون', 'sodino'),
|
|
'manage_options',
|
|
'sodino-add-rule',
|
|
[$adminController, 'addRulePage']
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-dashboard',
|
|
__('آپسل (پیشنهاد فروش)', 'sodino'),
|
|
__('آپسل (پیشنهاد فروش)', 'sodino'),
|
|
'manage_options',
|
|
'sodino-upsells',
|
|
[$adminController, 'upsellsPage']
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-dashboard',
|
|
__('افزودن آپسل', 'sodino'),
|
|
__('افزودن آپسل', 'sodino'),
|
|
'manage_options',
|
|
'sodino-add-upsell',
|
|
[$adminController, 'addUpsellPage']
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-dashboard',
|
|
__('بنرهای هوشمند', 'sodino'),
|
|
__('بنرهای هوشمند', 'sodino'),
|
|
'manage_options',
|
|
'sodino-banners',
|
|
[$adminController, 'bannersPage']
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-dashboard',
|
|
__('افزودن بنر', 'sodino'),
|
|
__('افزودن بنر', 'sodino'),
|
|
'manage_options',
|
|
'sodino-add-banner',
|
|
[$adminController, 'addBannerPage']
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-dashboard',
|
|
__('قیمت رقبا (بهزودی)', 'sodino'),
|
|
__('قیمت رقبا (بهزودی)', 'sodino'),
|
|
'manage_options',
|
|
'sodino-competitor-price',
|
|
[$adminController, 'competitorPricePage']
|
|
);
|
|
|
|
add_submenu_page(
|
|
'sodino-dashboard',
|
|
__('تنظیمات', 'sodino'),
|
|
__('تنظیمات', 'sodino'),
|
|
'manage_options',
|
|
'sodino-settings',
|
|
[$adminController, 'settingsPage']
|
|
);
|
|
});
|
|
|
|
/**
|
|
* Admin AJAX handlers
|
|
*/
|
|
add_action('wp_ajax_sodino_search_products', [$adminController, 'searchProductsAjax']);
|
|
|
|
/**
|
|
* Enqueue admin assets
|
|
*/
|
|
add_action('admin_enqueue_scripts', function($hook) {
|
|
if (strpos($hook, 'sodino') === false) {
|
|
return;
|
|
}
|
|
|
|
// 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-dashboard-js', plugin_dir_url(__FILE__) . 'js/dashboard.js', [], SODINO_VERSION, true);
|
|
}
|
|
|
|
// Upsell specific scripts
|
|
if (strpos($hook, 'sodino-add-upsell') !== false || strpos($hook, 'sodino_page_sodino-add-upsell') !== false) {
|
|
wp_enqueue_script('sodino-upsell-admin', plugin_dir_url(__FILE__) . 'js/upsell-admin.js', ['jquery'], SODINO_VERSION, true);
|
|
wp_localize_script('sodino-upsell-admin', 'sodinoUpsellAdmin', [
|
|
'nonce' => wp_create_nonce('sodino_search_products'),
|
|
'ajaxUrl' => admin_url('admin-ajax.php')
|
|
]);
|
|
}
|
|
|
|
// Banner specific scripts
|
|
if (strpos($hook, 'sodino-add-banner') !== false || strpos($hook, 'sodino_page_sodino-add-banner') !== false) {
|
|
wp_enqueue_media();
|
|
wp_enqueue_script('sodino-banner-admin', plugin_dir_url(__FILE__) . 'js/banner-admin.js', ['jquery'], SODINO_VERSION, true);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Handle admin actions
|
|
*/
|
|
add_action('admin_init', function() use ($ruleController, $settingsController, $adminController) {
|
|
$page = $_GET['page'] ?? '';
|
|
$action = $_GET['action'] ?? '';
|
|
|
|
// Rule actions
|
|
if ($page === 'sodino-rules' && $action === 'delete') {
|
|
$ruleController->delete();
|
|
}
|
|
|
|
// Settings actions
|
|
if ($page === 'sodino-settings' && $action === 'clear_cache') {
|
|
$settingsController->clearCache();
|
|
}
|
|
|
|
// Banner actions
|
|
if (strpos($page, 'sodino') === 0 && in_array($action, ['delete_banner', 'toggle_banner_status'], true)) {
|
|
$adminController->handleBannerActions();
|
|
}
|
|
|
|
// Upsell actions
|
|
if (strpos($page, 'sodino') === 0 && in_array($action, ['delete_upsell', 'toggle_upsell_status'], true)) {
|
|
$adminController->handleUpsellActions();
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Show admin notices
|
|
*/
|
|
add_action('admin_notices', function() {
|
|
$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';
|
|
printf(
|
|
'<div class="notice %s is-dismissible"><p>%s</p></div>',
|
|
esc_attr($class),
|
|
esc_html($notice['message'])
|
|
);
|
|
}
|
|
});
|