initial COmmit: Add KB Antora Importer plugin files
This commit is contained in:
171
kb-antora-importer/includes/Admin/SettingsPage.php
Normal file
171
kb-antora-importer/includes/Admin/SettingsPage.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KbAntoraImporter\Admin;
|
||||
|
||||
use KbAntoraImporter\GitLab\GitLabClient;
|
||||
use KbAntoraImporter\Import\ImportLogger;
|
||||
use KbAntoraImporter\Plugin;
|
||||
use KbAntoraImporter\Settings;
|
||||
|
||||
final class SettingsPage
|
||||
{
|
||||
public static function registerSettings(): void
|
||||
{
|
||||
register_setting('kb_antora_importer_settings', 'kb_antora_importer_settings', [
|
||||
'type' => 'array',
|
||||
'sanitize_callback' => [self::class, 'sanitize'],
|
||||
'default' => Settings::defaults(),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function sanitize(array $input): array
|
||||
{
|
||||
$old = Plugin::settings();
|
||||
$settings = Settings::defaults();
|
||||
|
||||
$settings['gitlab_base_url'] = esc_url_raw(GitLabClient::normalizeBaseUrl((string) ($input['gitlab_base_url'] ?? '')));
|
||||
$settings['gitlab_token'] = trim((string) ($input['gitlab_token'] ?? '')) ?: (string) $old['gitlab_token'];
|
||||
$settings['gitlab_group'] = sanitize_text_field((string) ($input['gitlab_group'] ?? 'knowledgebase'));
|
||||
$settings['branch_pattern'] = sanitize_text_field((string) ($input['branch_pattern'] ?? '^v.*'));
|
||||
$settings['docs_base_slug'] = sanitize_title((string) ($input['docs_base_slug'] ?? 'docs')) ?: 'docs';
|
||||
$settings['renderer_mode'] = in_array(($input['renderer_mode'] ?? 'php'), ['php', 'asciidoctor'], true) ? (string) $input['renderer_mode'] : 'php';
|
||||
$settings['asciidoctor_path'] = sanitize_text_field((string) ($input['asciidoctor_path'] ?? 'asciidoctor'));
|
||||
$settings['image_lightbox'] = ! empty($input['image_lightbox']) ? '1' : '0';
|
||||
$settings['public_docs'] = ! empty($input['public_docs']) ? '1' : '0';
|
||||
$settings['allow_svg'] = ! empty($input['allow_svg']) ? '1' : '0';
|
||||
$settings['cron_interval'] = in_array(($input['cron_interval'] ?? 'disabled'), ['disabled', 'hourly', 'daily', 'weekly'], true) ? (string) $input['cron_interval'] : 'disabled';
|
||||
|
||||
Plugin::syncCronSchedule($settings);
|
||||
if (($old['docs_base_slug'] ?? 'docs') !== $settings['docs_base_slug']) {
|
||||
flush_rewrite_rules(false);
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
public static function render(): void
|
||||
{
|
||||
if (! current_user_can('manage_kb_docs')) {
|
||||
wp_die(esc_html__('Insufficient permissions.', 'kb-antora-importer'));
|
||||
}
|
||||
|
||||
if (isset($_POST['kb_antora_test_connection']) && check_admin_referer('kb_antora_test_connection')) {
|
||||
self::handleConnectionTest();
|
||||
}
|
||||
|
||||
$settings = Plugin::settings();
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1><?php esc_html_e('Knowledgebase Settings', 'kb-antora-importer'); ?></h1>
|
||||
<form method="post" action="options.php">
|
||||
<?php settings_fields('kb_antora_importer_settings'); ?>
|
||||
<table class="form-table" role="presentation">
|
||||
<tr>
|
||||
<th scope="row"><label for="gitlab_base_url">GitLab Base URL</label></th>
|
||||
<td>
|
||||
<input class="regular-text" id="gitlab_base_url" name="kb_antora_importer_settings[gitlab_base_url]" type="url" value="<?php echo esc_attr($settings['gitlab_base_url']); ?>" placeholder="https://git.example.de">
|
||||
<p class="description"><?php esc_html_e('Use the GitLab root URL, for example https://git.example.de. Do not include /api/v4.', 'kb-antora-importer'); ?></p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="gitlab_token">GitLab API Token</label></th>
|
||||
<td><input class="regular-text" id="gitlab_token" name="kb_antora_importer_settings[gitlab_token]" type="password" value="" placeholder="<?php echo $settings['gitlab_token'] ? esc_attr__('Token is stored; leave blank to keep it', 'kb-antora-importer') : ''; ?>"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="gitlab_group">GitLab Group Path / ID</label></th>
|
||||
<td><input class="regular-text" id="gitlab_group" name="kb_antora_importer_settings[gitlab_group]" type="text" value="<?php echo esc_attr($settings['gitlab_group']); ?>"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="branch_pattern">Branch Pattern</label></th>
|
||||
<td><input class="regular-text" id="branch_pattern" name="kb_antora_importer_settings[branch_pattern]" type="text" value="<?php echo esc_attr($settings['branch_pattern']); ?>"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="docs_base_slug">Frontend Base Slug</label></th>
|
||||
<td><input class="regular-text" id="docs_base_slug" name="kb_antora_importer_settings[docs_base_slug]" type="text" value="<?php echo esc_attr($settings['docs_base_slug']); ?>"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="renderer_mode">Renderer Mode</label></th>
|
||||
<td>
|
||||
<select id="renderer_mode" name="kb_antora_importer_settings[renderer_mode]">
|
||||
<option value="php" <?php selected($settings['renderer_mode'], 'php'); ?>>PHP Renderer</option>
|
||||
<option value="asciidoctor" <?php selected($settings['renderer_mode'], 'asciidoctor'); ?>>Asciidoctor CLI</option>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="asciidoctor_path">Asciidoctor Path</label></th>
|
||||
<td><input class="regular-text" id="asciidoctor_path" name="kb_antora_importer_settings[asciidoctor_path]" type="text" value="<?php echo esc_attr($settings['asciidoctor_path']); ?>"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">Options</th>
|
||||
<td>
|
||||
<label><input type="checkbox" name="kb_antora_importer_settings[image_lightbox]" value="1" <?php checked($settings['image_lightbox'], '1'); ?>> Link images with lightbox class</label><br>
|
||||
<label><input type="checkbox" name="kb_antora_importer_settings[public_docs]" value="1" <?php checked($settings['public_docs'], '1'); ?>> Show documentation publicly</label><br>
|
||||
<label><input type="checkbox" name="kb_antora_importer_settings[allow_svg]" value="1" <?php checked($settings['allow_svg'], '1'); ?>> Allow SVG image import</label>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="cron_interval">Automatic Sync</label></th>
|
||||
<td>
|
||||
<select id="cron_interval" name="kb_antora_importer_settings[cron_interval]">
|
||||
<option value="disabled" <?php selected($settings['cron_interval'], 'disabled'); ?>>Disabled</option>
|
||||
<option value="hourly" <?php selected($settings['cron_interval'], 'hourly'); ?>>Hourly</option>
|
||||
<option value="daily" <?php selected($settings['cron_interval'], 'daily'); ?>>Daily</option>
|
||||
<option value="weekly" <?php selected($settings['cron_interval'], 'weekly'); ?>>Weekly</option>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<?php submit_button(); ?>
|
||||
</form>
|
||||
<form method="post">
|
||||
<?php wp_nonce_field('kb_antora_test_connection'); ?>
|
||||
<?php submit_button(__('Test GitLab Connection', 'kb-antora-importer'), 'secondary', 'kb_antora_test_connection'); ?>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
private static function handleConnectionTest(): void
|
||||
{
|
||||
$client = new GitLabClient(Plugin::settings());
|
||||
$result = $client->getGroup(Plugin::settings()['gitlab_group']);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
$message = self::formatConnectionError($result);
|
||||
ImportLogger::error('GitLab connection failed: ' . $message);
|
||||
add_settings_error('kb_antora_importer', 'connection_failed', esc_html($message), 'error');
|
||||
settings_errors('kb_antora_importer');
|
||||
return;
|
||||
}
|
||||
|
||||
ImportLogger::info('GitLab connection successful.');
|
||||
add_settings_error('kb_antora_importer', 'connection_ok', esc_html__('GitLab connection successful.', 'kb-antora-importer'), 'success');
|
||||
settings_errors('kb_antora_importer');
|
||||
}
|
||||
|
||||
private static function formatConnectionError(\WP_Error $error): string
|
||||
{
|
||||
$message = $error->get_error_message();
|
||||
$data = $error->get_error_data();
|
||||
|
||||
if (! is_array($data)) {
|
||||
return $message;
|
||||
}
|
||||
|
||||
if (! empty($data['url'])) {
|
||||
$message .= ' Target: ' . $data['url'];
|
||||
}
|
||||
|
||||
if (! empty($data['retry_after'])) {
|
||||
$message .= ' Retry-After: ' . $data['retry_after'];
|
||||
}
|
||||
|
||||
if (! empty($data['response_excerpt'])) {
|
||||
$message .= ' Response: ' . $data['response_excerpt'];
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user