Files
hermes-cpanel-agent/includes/core.php
T

131 lines
5.4 KiB
PHP

<?php
namespace HermesAiBridge;
defined('ABSPATH') or die();
class Core {
public static function init() {
// Load text domain
load_plugin_textdomain('hermes-ai-bridge', false, dirname(plugin_basename(HERMES_AI_BRIDGE_FILE)) . '/languages');
// Register custom post type
add_action('init', ['HermesAiBridge\Scheduler', 'register_post_type']);
// REST API
add_action('rest_api_init', [__CLASS__, 'register_routes']);
// WP-Cron hooks
add_action('hermes_hourly_tick', [__CLASS__, 'hourly_tick']);
add_action('hermes_daily_tick', [__CLASS__, 'daily_tick']);
// AJAX handlers
add_action('wp_ajax_hermes_regenerate_key', [__CLASS__, 'ajax_regenerate_key']);
// Admin
if (is_admin()) {
add_action('admin_menu', [__CLASS__, 'admin_menu']);
add_action('admin_enqueue_scripts', [__CLASS__, 'admin_assets']);
}
}
public static function register_routes() {
$controller = new RestController();
$controller->register_routes();
}
public static function hourly_tick() {
$scheduler = new Scheduler();
$scheduler->process_due_tasks();
}
public static function daily_tick() {
$ai = new AiProvider();
$ai->generate_newsletter_content();
}
public static function admin_menu() {
add_menu_page(
'Hermes AI Bridge',
'Hermes AI',
'manage_options',
'hermes-ai-bridge',
[__CLASS__, 'admin_page'],
'dashicons-rest-api',
80
);
}
public static function admin_page() {
$api_key = get_option('hermes_api_key');
?>
<div class="wrap">
<h1>Hermes AI Bridge</h1>
<hr>
<h2>API Key</h2>
<p>Use this key in the <code>X-API-Key</code> header for REST API calls.</p>
<input type="text" readonly="readonly" value="<?php echo esc_attr($api_key); ?>" style="width:600px;font-family:monospace;" onclick="this.select()" />
<p><button id="hermes-regenerate-key" class="button">Regenerate Key</button></p>
<hr>
<h2>AI Provider Settings</h2>
<form method="post" action="options.php">
<?php settings_fields('hermes_ai_settings'); ?>
<table class="form-table">
<tr>
<th>Provider</th>
<td>
<select name="hermes_ai_provider">
<option value="openai" <?php selected(get_option('hermes_ai_provider'), 'openai'); ?>>OpenAI</option>
<option value="anthropic" <?php selected(get_option('hermes_ai_provider'), 'anthropic'); ?>>Anthropic</option>
<option value="opencode" <?php selected(get_option('hermes_ai_provider'), 'opencode'); ?>>OpenClaw (OpenCode)</option>
</select>
</td>
</tr>
<tr>
<th>Model</th>
<td><input type="text" name="hermes_ai_model" value="<?php echo esc_attr(get_option('hermes_ai_model', 'gpt-4')); ?>" class="regular-text" /></td>
</tr>
<tr>
<th>API Key (for AI provider)</th>
<td><input type="password" name="hermes_ai_api_key" value="<?php echo esc_attr(get_option('hermes_ai_api_key', '')); ?>" class="regular-text" /></td>
</tr>
<tr>
<th>API Base URL</th>
<td><input type="url" name="hermes_ai_base_url" value="<?php echo esc_attr(get_option('hermes_ai_base_url', 'https://api.openai.com/v1')); ?>" class="regular-text" /></td>
</tr>
<tr>
<th>Max Tokens</th>
<td><input type="number" name="hermes_ai_max_tokens" value="<?php echo esc_attr(get_option('hermes_ai_max_tokens', 2048)); ?>" min="1" max="128000" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<script>
document.getElementById('hermes-regenerate-key')?.addEventListener('click', function() {
if (!confirm('Regenerate API key? Existing integrations will stop working.')) return;
fetch(ajaxurl, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'action=hermes_regenerate_key&_wpnonce=<?php echo wp_create_nonce('hermes_regenerate_key'); ?>'
}).then(r => r.json()).then(d => { if(d.success) location.reload(); });
});
</script>
<?php
}
public static function admin_assets($hook) {
if ($hook !== 'toplevel_page_hermes-ai-bridge') return;
wp_enqueue_style('hermes-admin', HERMES_AI_BRIDGE_URL . 'assets/admin.css', [], HERMES_AI_BRIDGE_VERSION);
}
public static function ajax_regenerate_key() {
check_ajax_referer('hermes_regenerate_key');
if (!current_user_can('manage_options')) return;
$new_key = wp_hash(wp_generate_password(64, true, true));
update_option('hermes_api_key', $new_key);
wp_send_json_success(['key' => $new_key]);
}
}