'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,
};
?>
$provider) :
hgsf_admin_source($source, $provider['label']);
endforeach; ?>
Plaatsen op de website
[hg_socialfeed] toont de recentste berichten en links naar de profielen.
[hg_sociallinks] toont alleen de iconenbalk met links naar de ingestelde profielen.
Beperk desgewenst de bronnen met sources="instagram,facebook".
[
'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']);
?>
Automatisch ophalen staat uit.
De eerste synchronisatie staat nog open.
Het token kon nog niet automatisch worden verlengd. WordPress probeert het bij de volgende synchronisatie opnieuw.
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');