210 lines
10 KiB
PHP
210 lines
10 KiB
PHP
<?php
|
|
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
HG_Plugin_Admin::register([
|
|
'slug' => 'hg-socialfeed',
|
|
'label' => 'Social Feed',
|
|
'title' => 'HG Social Feed',
|
|
'description' => 'Toon berichten en profiellinks van Instagram, Facebook en X.',
|
|
'version' => HGSF_VERSION,
|
|
'callback' => 'hgsf_admin_page',
|
|
]);
|
|
|
|
function hgsf_admin_page(): void {
|
|
if (!current_user_can('manage_options')) {
|
|
wp_die('Geen toegang.', '', ['response' => 403]);
|
|
}
|
|
$notice = match ($_GET['hgsf_notice'] ?? '') {
|
|
'saved' => ['success', 'Instellingen opgeslagen.'],
|
|
'synced' => ['success', 'Berichten opgehaald.'],
|
|
'failed' => ['error', 'Het ophalen is mislukt. Bekijk de status van het platform.'],
|
|
'invalid' => ['error', 'Controleer het profiel en het toegangstoken van het platform.'],
|
|
'busy' => ['error', 'Er loopt al een synchronisatie voor dit platform. Probeer het zo opnieuw.'],
|
|
'disconnected' => ['success', 'Toegangstoken en opgeslagen berichten verwijderd. De profiellink blijft beschikbaar.'],
|
|
default => null,
|
|
};
|
|
?>
|
|
<div class="wrap hg-suite">
|
|
<header class="hg-suite-header">
|
|
<span class="hg-suite-mark" aria-hidden="true">HG</span>
|
|
<div><h1>HG Social Feed</h1><p>Beheer de bronnen voor berichten en profieliconen.</p></div>
|
|
</header>
|
|
<?php if ($notice) : ?>
|
|
<div class="notice notice-<?php echo esc_attr($notice[0]); ?>"><p><?php echo esc_html($notice[1]); ?></p></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="hg-suite-grid">
|
|
<?php foreach (HGSF_SOURCES as $source => $provider) :
|
|
hgsf_admin_source($source, $provider['label']);
|
|
endforeach; ?>
|
|
</div>
|
|
|
|
<h2>Plaatsen op de website</h2>
|
|
<p><code>[hg_socialfeed]</code> toont de recentste berichten en links naar de profielen.</p>
|
|
<p><code>[hg_sociallinks]</code> toont alleen de iconenbalk met links naar de ingestelde profielen.</p>
|
|
<p>Beperk desgewenst de bronnen met <code>sources="instagram,facebook"</code>.</p>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
function hgsf_admin_source(string $source, string $label): void {
|
|
$connection = hgsf_connection($source);
|
|
$snapshot = hgsf_snapshot($connection, $source);
|
|
$field = $source === 'facebook' ? 'Pagina-ID' : 'Gebruikersnaam';
|
|
$help = match ($source) {
|
|
'instagram' => [
|
|
'Gebruik een langlevend Instagram-toegangstoken uit het Meta App Dashboard.',
|
|
'https://developers.facebook.com/documentation/instagram-platform/instagram-api-with-instagram-login/get-started',
|
|
],
|
|
'facebook' => [
|
|
'Gebruik een paginatoegangstoken voor een Facebook-pagina die je beheert.',
|
|
'https://developers.facebook.com/docs/graph-api/reference/page/published_posts',
|
|
],
|
|
'x' => [
|
|
'Gebruik het app-only Bearer Token uit de X Developer Console.',
|
|
'https://docs.x.com/x-api/getting-started/pricing',
|
|
],
|
|
};
|
|
$can_sync = $connection['token'] && ($source !== 'x' || $connection['enabled']);
|
|
?>
|
|
<section class="hg-suite-card">
|
|
<h2><?php echo esc_html($label); ?></h2>
|
|
<form action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="post">
|
|
<input type="hidden" name="action" value="hgsf_settings">
|
|
<input type="hidden" name="source" value="<?php echo esc_attr($source); ?>">
|
|
<?php wp_nonce_field('hgsf_settings_' . $source, 'hgsf_nonce_' . $source); ?>
|
|
<p>
|
|
<label for="hgsf-<?php echo esc_attr($source); ?>-username"><?php echo esc_html($field); ?></label><br>
|
|
<input type="text" id="hgsf-<?php echo esc_attr($source); ?>-username" name="username" maxlength="31"
|
|
value="<?php echo esc_attr($connection['username']); ?>" autocapitalize="none" spellcheck="false">
|
|
</p>
|
|
<?php if ($source === 'facebook') : ?>
|
|
<p>
|
|
<label for="hgsf-facebook-profile">Paginanaam in de URL</label><br>
|
|
<input type="text" id="hgsf-facebook-profile" name="profile" maxlength="75"
|
|
value="<?php echo esc_attr($connection['profile']); ?>" autocapitalize="none" spellcheck="false">
|
|
<br><span class="description">Optioneel voor de profiellink. Bijvoorbeeld mijnbedrijf uit facebook.com/mijnbedrijf.</span>
|
|
</p>
|
|
<?php endif; ?>
|
|
<p>
|
|
<label for="hgsf-<?php echo esc_attr($source); ?>-token">Toegangstoken</label><br>
|
|
<input type="password" id="hgsf-<?php echo esc_attr($source); ?>-token" name="token" autocomplete="new-password"
|
|
maxlength="2048" aria-describedby="hgsf-<?php echo esc_attr($source); ?>-token-help">
|
|
<br><span class="description" id="hgsf-<?php echo esc_attr($source); ?>-token-help">
|
|
<?php echo esc_html($connection['token'] ? 'Laat leeg om het huidige token voor dit account te behouden.' : $help[0]); ?>
|
|
<a href="<?php echo esc_url($help[1]); ?>">Documentatie</a>
|
|
</span>
|
|
</p>
|
|
<?php if ($source === 'x') : ?>
|
|
<p><label><input type="checkbox" name="enabled" value="1" <?php checked($connection['enabled']); ?>>
|
|
X-berichten automatisch ophalen. X rekent per API-gebruik.</label></p>
|
|
<?php endif; ?>
|
|
<p>
|
|
<button type="submit" name="operation" value="save" class="button button-primary">Opslaan</button>
|
|
<?php if ($can_sync) : ?>
|
|
<button type="submit" name="operation" value="sync" class="button">Nu synchroniseren</button>
|
|
<?php endif; ?>
|
|
<?php if ($connection['token']) : ?>
|
|
<button type="submit" name="operation" value="disconnect" class="button">Koppeling verwijderen</button>
|
|
<?php endif; ?>
|
|
</p>
|
|
</form>
|
|
<?php if (!$can_sync) : ?>
|
|
<p>Automatisch ophalen staat uit.</p>
|
|
<?php elseif (!empty($snapshot['error'])) : ?>
|
|
<p><?php echo esc_html($snapshot['error']); ?></p>
|
|
<?php elseif (!empty($snapshot['synced_at'])) : ?>
|
|
<p><?php echo esc_html(sprintf('Laatste geslaagde synchronisatie op %s. Opgehaalde berichten: %d.', wp_date('j F Y H:i', $snapshot['synced_at']), count($snapshot['posts']))); ?></p>
|
|
<?php else : ?>
|
|
<p>De eerste synchronisatie staat nog open.</p>
|
|
<?php endif; ?>
|
|
<?php if (!empty($snapshot['refresh_failed'])) : ?>
|
|
<p>Het token kon nog niet automatisch worden verlengd. WordPress probeert het bij de volgende synchronisatie opnieuw.</p>
|
|
<?php endif; ?>
|
|
</section>
|
|
<?php
|
|
}
|
|
|
|
function hgsf_handle_settings(): void {
|
|
if (!current_user_can('manage_options')) {
|
|
wp_die('Geen toegang.', '', ['response' => 403]);
|
|
}
|
|
$source = $_POST['source'] ?? '';
|
|
if (!is_string($source) || !isset(HGSF_SOURCES[$source])) {
|
|
wp_die('Ongeldig platform.', '', ['response' => 400]);
|
|
}
|
|
check_admin_referer('hgsf_settings_' . $source, 'hgsf_nonce_' . $source);
|
|
$result = hgsf_with_connection_lock(static fn () => hgsf_save_settings($source, $_POST), $source);
|
|
$notice = is_wp_error($result) ? 'busy' : $result;
|
|
wp_safe_redirect(add_query_arg(['page' => 'hg-socialfeed', 'hgsf_notice' => $notice], admin_url('admin.php')));
|
|
exit;
|
|
}
|
|
|
|
function hgsf_save_settings(string $source, array $input): string {
|
|
$connection = hgsf_connection($source);
|
|
$provider = HGSF_SOURCES[$source];
|
|
$operation = $input['operation'] ?? '';
|
|
if ($operation === 'disconnect') {
|
|
$connection['id'] = wp_generate_uuid4();
|
|
$connection['token'] = '';
|
|
$connection['saved_at'] = 0;
|
|
$connection['enabled'] = false;
|
|
update_option($provider['connection'], $connection, false);
|
|
delete_option($provider['snapshot']);
|
|
return 'disconnected';
|
|
}
|
|
if ($operation === 'sync') {
|
|
return is_wp_error(hgsf_update_snapshot($connection, $source)) ? 'failed' : 'synced';
|
|
}
|
|
if ($operation !== 'save') {
|
|
return 'invalid';
|
|
}
|
|
|
|
$username = $input['username'] ?? '';
|
|
$token = $input['token'] ?? '';
|
|
$profile = $source === 'facebook' ? ($input['profile'] ?? '') : '';
|
|
if (!is_string($username) || !is_string($token) || !is_string($profile)) {
|
|
return 'invalid';
|
|
}
|
|
$username = strtolower(ltrim(trim(wp_unslash($username)), '@'));
|
|
$token = trim(wp_unslash($token));
|
|
$profile = trim(wp_unslash($profile));
|
|
$enabled = $source === 'x' && ($input['enabled'] ?? '') === '1';
|
|
$pattern = match ($source) {
|
|
'instagram' => '/^[a-z0-9._]{1,30}$/D',
|
|
'facebook' => '/^[0-9]{1,30}$/D',
|
|
'x' => '/^[a-z0-9_]{1,15}$/D',
|
|
};
|
|
if (($username !== '' && !preg_match($pattern, $username))
|
|
|| ($profile !== '' && !preg_match('/^[a-z0-9.]{1,75}$/iD', $profile))
|
|
|| ($token !== '' && ($username === '' || !hgsf_valid_token($token, $source)))) {
|
|
return 'invalid';
|
|
}
|
|
|
|
$credentials_changed = $username !== $connection['username'] || $token !== '';
|
|
$enable_changed = $enabled !== $connection['enabled'];
|
|
if ($enabled && ($username === '' || ($token === '' && ($credentials_changed || !$connection['token'])))) {
|
|
return 'invalid';
|
|
}
|
|
|
|
if ($credentials_changed) {
|
|
$connection['id'] = wp_generate_uuid4();
|
|
$connection['username'] = $username;
|
|
$connection['token'] = $token;
|
|
$connection['saved_at'] = time();
|
|
}
|
|
$connection['profile'] = $profile;
|
|
$connection['enabled'] = $enabled;
|
|
update_option($provider['connection'], $connection, false);
|
|
if ($credentials_changed || $enable_changed) {
|
|
delete_option($provider['snapshot']);
|
|
if ($connection['token'] && ($source !== 'x' || $enabled)) {
|
|
return is_wp_error(hgsf_update_snapshot($connection, $source)) ? 'failed' : 'synced';
|
|
}
|
|
}
|
|
return 'saved';
|
|
}
|
|
|
|
add_action('admin_post_hgsf_settings', 'hgsf_handle_settings');
|