187 lines
7.6 KiB
PHP
187 lines
7.6 KiB
PHP
<?php
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
final class SPP_Admin_Page
|
|
{
|
|
public function register_hooks(): void
|
|
{
|
|
add_action('admin_menu', [$this, 'register_menu']);
|
|
add_action('admin_init', [$this, 'register_settings']);
|
|
add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']);
|
|
add_action('show_user_profile', [$this, 'render_user_quota_field']);
|
|
add_action('edit_user_profile', [$this, 'render_user_quota_field']);
|
|
add_action('personal_options_update', [$this, 'save_user_quota_field']);
|
|
add_action('edit_user_profile_update', [$this, 'save_user_quota_field']);
|
|
}
|
|
|
|
public function register_menu(): void
|
|
{
|
|
add_menu_page(
|
|
'Support Provisioning',
|
|
'Support Provisioning',
|
|
'edit_posts',
|
|
'support-provisioning-portal',
|
|
[$this, 'render'],
|
|
'dashicons-cloud',
|
|
58
|
|
);
|
|
}
|
|
|
|
public function register_settings(): void
|
|
{
|
|
register_setting('spp_settings', 'spp_proxmox_mode', [
|
|
'type' => 'string',
|
|
'sanitize_callback' => static fn($value) => $value === 'http' ? 'http' : 'mock',
|
|
'default' => 'mock',
|
|
]);
|
|
register_setting('spp_settings', 'spp_proxmox_base_url', ['type' => 'string', 'sanitize_callback' => 'esc_url_raw']);
|
|
register_setting('spp_settings', 'spp_proxmox_token_id', ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);
|
|
register_setting('spp_settings', 'spp_proxmox_token_secret', ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field']);
|
|
register_setting('spp_settings', 'spp_proxmox_node', ['type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => 'pve-01']);
|
|
register_setting('spp_settings', 'spp_quota_user_memory_mb', [
|
|
'type' => 'integer',
|
|
'sanitize_callback' => static fn($value) => max(0, absint($value)),
|
|
'default' => 0,
|
|
]);
|
|
register_setting('spp_settings', 'spp_quota_global_memory_mb', [
|
|
'type' => 'integer',
|
|
'sanitize_callback' => static fn($value) => max(0, absint($value)),
|
|
'default' => 0,
|
|
]);
|
|
}
|
|
|
|
public function enqueue_assets(string $hook): void
|
|
{
|
|
if ($hook !== 'toplevel_page_support-provisioning-portal') {
|
|
return;
|
|
}
|
|
|
|
wp_enqueue_style('spp-portal', SPP_PLUGIN_URL . 'assets/portal.css', [], SPP_VERSION);
|
|
wp_enqueue_script('spp-portal', SPP_PLUGIN_URL . 'assets/portal.js', [], SPP_VERSION, true);
|
|
}
|
|
|
|
public function render(): void
|
|
{
|
|
if (!current_user_can('edit_posts')) {
|
|
wp_die(esc_html__('You do not have permission to access this page.', 'support-provisioning-portal'));
|
|
}
|
|
|
|
?>
|
|
<div class="wrap spp-admin-wrap">
|
|
<h1><?php echo esc_html__('Support Provisioning Portal', 'support-provisioning-portal'); ?></h1>
|
|
<div class="spp-admin-grid">
|
|
<?php $this->render_app_root(); ?>
|
|
<?php $this->render_settings(); ?>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
private function render_app_root(): void
|
|
{
|
|
?>
|
|
<div
|
|
id="spp-portal-root"
|
|
class="spp-portal"
|
|
data-rest-url="<?php echo esc_url_raw(rest_url('support-provisioning/v1')); ?>"
|
|
data-nonce="<?php echo esc_attr(wp_create_nonce('wp_rest')); ?>"
|
|
></div>
|
|
<?php
|
|
}
|
|
|
|
private function render_settings(): void
|
|
{
|
|
?>
|
|
<form class="spp-settings" method="post" action="options.php">
|
|
<h2><?php echo esc_html__('Proxmox Settings', 'support-provisioning-portal'); ?></h2>
|
|
<?php settings_fields('spp_settings'); ?>
|
|
<label>
|
|
<span>Mode</span>
|
|
<select name="spp_proxmox_mode">
|
|
<option value="mock" <?php selected(get_option('spp_proxmox_mode', 'mock'), 'mock'); ?>>Mock</option>
|
|
<option value="http" <?php selected(get_option('spp_proxmox_mode', 'mock'), 'http'); ?>>HTTP token auth</option>
|
|
</select>
|
|
</label>
|
|
<label>
|
|
<span>Base URL</span>
|
|
<input name="spp_proxmox_base_url" type="url" value="<?php echo esc_attr(get_option('spp_proxmox_base_url', '')); ?>" placeholder="https://proxmox.example.internal:8006">
|
|
</label>
|
|
<label>
|
|
<span>Token ID</span>
|
|
<input name="spp_proxmox_token_id" type="text" value="<?php echo esc_attr(get_option('spp_proxmox_token_id', '')); ?>" placeholder="user@realm!token-name">
|
|
</label>
|
|
<label>
|
|
<span>Token Secret</span>
|
|
<input name="spp_proxmox_token_secret" type="password" value="<?php echo esc_attr(get_option('spp_proxmox_token_secret', '')); ?>">
|
|
</label>
|
|
<label>
|
|
<span>Node</span>
|
|
<input name="spp_proxmox_node" type="text" value="<?php echo esc_attr(get_option('spp_proxmox_node', 'pve-01')); ?>">
|
|
</label>
|
|
<h2><?php echo esc_html__('RAM Contingents', 'support-provisioning-portal'); ?></h2>
|
|
<p class="description"><?php echo esc_html__('Set 0 for unlimited. Active allocations include provisioning, stopped, running, and deleting deployments.', 'support-provisioning-portal'); ?></p>
|
|
<label>
|
|
<span>Default per-user RAM limit (MB)</span>
|
|
<input name="spp_quota_user_memory_mb" type="number" min="0" step="256" value="<?php echo esc_attr((string) get_option('spp_quota_user_memory_mb', 0)); ?>">
|
|
</label>
|
|
<label>
|
|
<span>Global RAM limit (MB)</span>
|
|
<input name="spp_quota_global_memory_mb" type="number" min="0" step="256" value="<?php echo esc_attr((string) get_option('spp_quota_global_memory_mb', 0)); ?>">
|
|
</label>
|
|
<?php submit_button('Save Settings'); ?>
|
|
</form>
|
|
<?php
|
|
}
|
|
|
|
public function render_user_quota_field(WP_User $user): void
|
|
{
|
|
if (!current_user_can('edit_users')) {
|
|
return;
|
|
}
|
|
|
|
$quota = get_user_meta($user->ID, 'spp_memory_quota_mb', true);
|
|
?>
|
|
<h2><?php echo esc_html__('Support Provisioning Contingent', 'support-provisioning-portal'); ?></h2>
|
|
<table class="form-table" role="presentation">
|
|
<tr>
|
|
<th><label for="spp_memory_quota_mb">RAM limit override (MB)</label></th>
|
|
<td>
|
|
<input
|
|
id="spp_memory_quota_mb"
|
|
name="spp_memory_quota_mb"
|
|
type="number"
|
|
min="0"
|
|
step="256"
|
|
value="<?php echo esc_attr((string) $quota); ?>"
|
|
class="regular-text"
|
|
>
|
|
<p class="description"><?php echo esc_html__('Leave empty to use the default per-user limit. Set 0 for unlimited.', 'support-provisioning-portal'); ?></p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<?php
|
|
}
|
|
|
|
public function save_user_quota_field(int $user_id): void
|
|
{
|
|
if (!current_user_can('edit_user', $user_id)) {
|
|
return;
|
|
}
|
|
|
|
if (!array_key_exists('spp_memory_quota_mb', $_POST)) {
|
|
return;
|
|
}
|
|
|
|
$raw_value = sanitize_text_field(wp_unslash($_POST['spp_memory_quota_mb']));
|
|
if ($raw_value === '') {
|
|
delete_user_meta($user_id, 'spp_memory_quota_mb');
|
|
return;
|
|
}
|
|
|
|
update_user_meta($user_id, 'spp_memory_quota_mb', max(0, absint($raw_value)));
|
|
}
|
|
}
|