99 lines
4.3 KiB
PHP
99 lines
4.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace KbAntoraImporter\Admin;
|
|
|
|
use KbAntoraImporter\GitLab\GitLabClient;
|
|
use KbAntoraImporter\Import\ImportLogger;
|
|
use KbAntoraImporter\Import\ImportManager;
|
|
use KbAntoraImporter\Plugin;
|
|
|
|
final class SyncPage
|
|
{
|
|
public static function render(): void
|
|
{
|
|
if (! current_user_can('sync_kb_docs')) {
|
|
wp_die(esc_html__('Insufficient permissions.', 'kb-antora-importer'));
|
|
}
|
|
|
|
self::handleActions();
|
|
$projects = self::loadProjects();
|
|
?>
|
|
<div class="wrap">
|
|
<h1><?php esc_html_e('Knowledgebase Synchronization', 'kb-antora-importer'); ?></h1>
|
|
<form method="post" class="kb-sync-actions">
|
|
<?php wp_nonce_field('kb_antora_sync'); ?>
|
|
<?php submit_button(__('Sync All', 'kb-antora-importer'), 'primary', 'kb_antora_sync_all', false); ?>
|
|
<?php submit_button(__('Dry Run', 'kb-antora-importer'), 'secondary', 'kb_antora_dry_run', false); ?>
|
|
</form>
|
|
|
|
<h2><?php esc_html_e('Projects', 'kb-antora-importer'); ?></h2>
|
|
<?php if (is_wp_error($projects)) : ?>
|
|
<div class="notice notice-error"><p><?php echo esc_html($projects->get_error_message()); ?></p></div>
|
|
<?php elseif (! $projects) : ?>
|
|
<p><?php esc_html_e('No projects loaded. Check GitLab settings first.', 'kb-antora-importer'); ?></p>
|
|
<?php else : ?>
|
|
<table class="widefat striped">
|
|
<thead><tr><th>Name</th><th>Path</th><th>Action</th></tr></thead>
|
|
<tbody>
|
|
<?php foreach ($projects as $project) : ?>
|
|
<tr>
|
|
<td><?php echo esc_html((string) ($project['name'] ?? '')); ?></td>
|
|
<td><code><?php echo esc_html((string) ($project['path_with_namespace'] ?? $project['path'] ?? '')); ?></code></td>
|
|
<td>
|
|
<form method="post">
|
|
<?php wp_nonce_field('kb_antora_sync_project'); ?>
|
|
<input type="hidden" name="project_id" value="<?php echo esc_attr((string) ($project['id'] ?? '')); ?>">
|
|
<?php submit_button(__('Sync Project', 'kb-antora-importer'), 'secondary small', 'kb_antora_sync_project', false); ?>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
|
|
<h2><?php esc_html_e('Import Logs', 'kb-antora-importer'); ?></h2>
|
|
<?php StatusPage::renderLogTable(ImportLogger::recent(100)); ?>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
private static function handleActions(): void
|
|
{
|
|
if (isset($_POST['kb_antora_sync_all']) && check_admin_referer('kb_antora_sync')) {
|
|
(new ImportManager())->syncAll(false);
|
|
echo '<div class="notice notice-success"><p>' . esc_html__('Synchronization finished.', 'kb-antora-importer') . '</p></div>';
|
|
}
|
|
|
|
if (isset($_POST['kb_antora_dry_run']) && check_admin_referer('kb_antora_sync')) {
|
|
(new ImportManager())->syncAll(true);
|
|
echo '<div class="notice notice-info"><p>' . esc_html__('Dry run finished.', 'kb-antora-importer') . '</p></div>';
|
|
}
|
|
|
|
if (isset($_POST['kb_antora_sync_project']) && check_admin_referer('kb_antora_sync_project')) {
|
|
$projectId = sanitize_text_field(wp_unslash((string) ($_POST['project_id'] ?? '')));
|
|
(new ImportManager())->syncProject($projectId, false);
|
|
echo '<div class="notice notice-success"><p>' . esc_html__('Project synchronization finished.', 'kb-antora-importer') . '</p></div>';
|
|
}
|
|
}
|
|
|
|
private static function loadProjects(): array|\WP_Error
|
|
{
|
|
$settings = Plugin::settings();
|
|
|
|
if (! $settings['gitlab_base_url'] || ! $settings['gitlab_token'] || ! $settings['gitlab_group']) {
|
|
return [];
|
|
}
|
|
|
|
$client = new GitLabClient($settings);
|
|
$group = $client->getGroup($settings['gitlab_group']);
|
|
|
|
if (is_wp_error($group)) {
|
|
return $group;
|
|
}
|
|
|
|
return $client->getProjects((string) ($group['id'] ?? $settings['gitlab_group']));
|
|
}
|
|
}
|