| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Admin Menu Handler |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace UltimateStoreKit\Admin; |
| 8 |
|
| 9 |
use UltimateStoreKit\Base\Singleton; |
| 10 |
use UltimateStoreKit\Admin\Feeds; |
| 11 |
use UltimateStoreKit\Admin\Biggopties; |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
class Dashboard { |
| 18 |
use Singleton; |
| 19 |
|
| 20 |
private function __construct() { |
| 21 |
add_action('admin_menu', [$this, 'register_admin_menu']); |
| 22 |
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']); |
| 23 |
add_filter('plugin_action_links_' . BDTUSK_PBNAME, [$this, 'plugin_action_meta']); |
| 24 |
add_action('admin_init', [$this, 'init']); |
| 25 |
} |
| 26 |
|
| 27 |
public function init() { |
| 28 |
Feeds::get_instance(); |
| 29 |
Biggopties::get_instance(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Register admin menu. |
| 34 |
* |
| 35 |
* Add new Ultimate Store Kit Settings admin menu. |
| 36 |
*/ |
| 37 |
public function register_admin_menu() { |
| 38 |
if (! current_user_can('manage_options')) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
add_menu_page( |
| 43 |
esc_html__('Ultimate Store Kit', 'ultimate-store-kit'), |
| 44 |
esc_html__('Ultimate Store Kit', 'ultimate-store-kit'), |
| 45 |
'manage_options', |
| 46 |
'ultimate-store-kit', |
| 47 |
[$this, 'print_admin_page'], |
| 48 |
$this->ultimate_store_kit_icon(), |
| 49 |
'58.7' |
| 50 |
); |
| 51 |
|
| 52 |
add_submenu_page( |
| 53 |
'ultimate-store-kit', |
| 54 |
esc_html__('Welcome', 'ultimate-store-kit'), |
| 55 |
esc_html__('Welcome', 'ultimate-store-kit'), |
| 56 |
'manage_options', |
| 57 |
'ultimate-store-kit' |
| 58 |
); |
| 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Ni04OS44MzQtMjAwLjY0OC0yMDAuNjQ4LTIwMC42NDhzLTIwMC42NDggODkuODM0LTIwMC42NDggMjAwLjY0OGMwIDEuNzg2IDAuMDIzIDMuNTY2IDAuMDY5IDUuMzRsLTAuMDA1LTAuMjYyYzAgMCAwIDAgMCAwIDAgMjcuMTcxIDIxLjk1MiA0OS4yMTMgNDkuMDg4IDQ5LjM1OWgwLjAxNHoiPjwvcGF0aD4KPC9zdmc+Cg=='; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Print admin page. |
| 67 |
*/ |
| 68 |
public function print_admin_page() { |
| 69 |
?> |
| 70 |
<div class="ultimate-store-kit-admin-root font-sans"></div> |
| 71 |
<?php |
| 72 |
} |
| 73 |
public function enqueue_admin_scripts() { |
| 74 |
$screen = get_current_screen(); |
| 75 |
|
| 76 |
|
| 77 |
if ('toplevel_page_ultimate-store-kit' !== $screen->id) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$asset_data = $this->get_asset_file('assets/admin'); |
| 82 |
|
| 83 |
wp_enqueue_script( |
| 84 |
'ultimate-store-kit-admin', |
| 85 |
BDTUSK_ASSETS_URL . 'admin/admin.js', |
| 86 |
$asset_data['dependencies'], |
| 87 |
$asset_data['version'], |
| 88 |
true |
| 89 |
); |
| 90 |
|
| 91 |
wp_set_script_translations('ultimate-store-kit-admin', 'ultimate-store-kit'); |
| 92 |
|
| 93 |
wp_localize_script( |
| 94 |
'ultimate-store-kit-admin', |
| 95 |
'ultimateStoreKitAdminData', |
| 96 |
[ |
| 97 |
'widgets' => $this->get_widgets_data(), |
| 98 |
'savedSettings' => $this->get_saved_settings(), |
| 99 |
'version' => BDTUSK_VER, |
| 100 |
'restUrl' => esc_url_raw(rest_url('ultimate-store-kit/v1/')), |
| 101 |
'restNonce' => wp_create_nonce('wp_rest'), |
| 102 |
'isPro' => function_exists('usk_license_validation') ? usk_license_validation() : false, |
| 103 |
'isProPluginActive' => defined('BDTUSK_PRO_VER'), |
| 104 |
'adminUrl' => admin_url(), |
| 105 |
'licenseData' => apply_filters('ultimate_store_kit_license_data', []), |
| 106 |
] |
| 107 |
); |
| 108 |
|
| 109 |
wp_enqueue_style( |
| 110 |
'ultimate-store-kit-admin', |
| 111 |
BDTUSK_ASSETS_URL . 'admin/style-admin.css', |
| 112 |
[], |
| 113 |
$asset_data['version'] |
| 114 |
); |
| 115 |
|
| 116 |
wp_enqueue_style('wp-components'); |
| 117 |
} |
| 118 |
|
| 119 |
public function get_asset_file($filepath) { |
| 120 |
$asset_path = BDTUSK_PATH . $filepath . '.asset.php'; |
| 121 |
|
| 122 |
if (file_exists($asset_path)) { |
| 123 |
return include $asset_path; |
| 124 |
} |
| 125 |
|
| 126 |
return [ |
| 127 |
'dependencies' => [], |
| 128 |
'version' => BDTUSK_VER, |
| 129 |
]; |
| 130 |
} |
| 131 |
|
| 132 |
public function get_widgets_data() { |
| 133 |
if (!class_exists('\UltimateStoreKit\Admin\ModuleService')) { |
| 134 |
require_once BDTUSK_ADMIN_PATH . 'module-settings.php'; |
| 135 |
} |
| 136 |
|
| 137 |
$widget_data = \UltimateStoreKit\Admin\ModuleService::get_widget_settings(function ($settings) { |
| 138 |
return $settings['settings_fields']; |
| 139 |
}); |
| 140 |
|
| 141 |
foreach ($widget_data as $section_key => $section_widgets) { |
| 142 |
if (!is_array($section_widgets)) { |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
foreach ($section_widgets as $index => $widget) { |
| 147 |
if (!is_array($widget)) { |
| 148 |
continue; |
| 149 |
} |
| 150 |
|
| 151 |
$widget_data[$section_key][$index]['dependency'] = $this->get_widget_dependency_data($widget); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return $widget_data; |
| 156 |
} |
| 157 |
|
| 158 |
private function get_widget_dependency_data($widget) { |
| 159 |
$plugin_name = !empty($widget['plugin_name']) ? $widget['plugin_name'] : ''; |
| 160 |
$plugin_path = !empty($widget['plugin_path']) ? $widget['plugin_path'] : ''; |
| 161 |
$paid = !empty($widget['paid']) ? $widget['paid'] : ''; |
| 162 |
|
| 163 |
if (empty($plugin_name) || empty($plugin_path)) { |
| 164 |
return null; |
| 165 |
} |
| 166 |
|
| 167 |
if (!function_exists('get_plugins') || !function_exists('is_plugin_active')) { |
| 168 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 169 |
} |
| 170 |
|
| 171 |
$installed_plugins = get_plugins(); |
| 172 |
$is_installed = isset($installed_plugins[$plugin_path]); |
| 173 |
$is_active = $is_installed && is_plugin_active($plugin_path); |
| 174 |
|
| 175 |
$action_url = ''; |
| 176 |
$action_label = ''; |
| 177 |
$action_type = ''; |
| 178 |
$message = ''; |
| 179 |
|
| 180 |
if (!$is_installed) { |
| 181 |
$action_type = 'install'; |
| 182 |
$action_label = __('Install Plugin', 'ultimate-store-kit'); |
| 183 |
$message = __('Install the required plugin first, then you can activate this feature.', 'ultimate-store-kit'); |
| 184 |
|
| 185 |
if (!empty($paid)) { |
| 186 |
$action_url = $paid; |
| 187 |
$action_label = __('Download Plugin', 'ultimate-store-kit'); |
| 188 |
} else { |
| 189 |
$action_url = admin_url('update.php?action=install-plugin&plugin=' . urlencode($plugin_name) . '&_wpnonce=' . wp_create_nonce('install-plugin_' . $plugin_name)); |
| 190 |
} |
| 191 |
} elseif (!$is_active) { |
| 192 |
$action_type = 'activate'; |
| 193 |
$action_label = __('Activate Plugin', 'ultimate-store-kit'); |
| 194 |
$message = __('Activate the required plugin first, then you can activate this feature.', 'ultimate-store-kit'); |
| 195 |
$action_url = admin_url('plugins.php?action=activate&plugin=' . urlencode($plugin_path) . '&plugin_status=all&paged=1&s&_wpnonce=' . wp_create_nonce('activate-plugin_' . $plugin_path)); |
| 196 |
} |
| 197 |
|
| 198 |
return [ |
| 199 |
'pluginName' => $plugin_name, |
| 200 |
'pluginPath' => $plugin_path, |
| 201 |
'isInstalled' => $is_installed, |
| 202 |
'isActive' => $is_active, |
| 203 |
'actionUrl' => $action_url, |
| 204 |
'actionLabel' => $action_label, |
| 205 |
'actionType' => $action_type, |
| 206 |
'message' => $message, |
| 207 |
]; |
| 208 |
} |
| 209 |
|
| 210 |
public function get_saved_settings() { |
| 211 |
return [ |
| 212 |
'ultimate_store_kit_active_modules' => get_option('ultimate_store_kit_active_modules', []), |
| 213 |
'ultimate_store_kit_edd_modules' => get_option('ultimate_store_kit_edd_modules', []), |
| 214 |
'ultimate_store_kit_general_modules' => get_option('ultimate_store_kit_general_modules', []), |
| 215 |
'ultimate_store_kit_other_settings' => get_option('ultimate_store_kit_other_settings', []), |
| 216 |
]; |
| 217 |
} |
| 218 |
|
| 219 |
public function plugin_action_meta($links) { |
| 220 |
|
| 221 |
$links = array_merge([sprintf('<a href="%s">%s</a>', ultimate_store_kit_dashboard_link('#welcome'), esc_html__('Settings', 'ultimate-store-kit'))], $links); |
| 222 |
|
| 223 |
|
| 224 |
return $links; |
| 225 |
} |
| 226 |
} |
| 227 |
|