75 lines
3.0 KiB
PHP
75 lines
3.0 KiB
PHP
<?php
|
|
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
const HGSF_FACEBOOK_VERSION = 'v27.0';
|
|
|
|
function hgsf_facebook_permalink($url): string {
|
|
if (!is_string($url)) {
|
|
return '';
|
|
}
|
|
$parts = wp_parse_url(esc_url_raw($url, ['https']));
|
|
$path = $parts['path'] ?? '';
|
|
if (($parts['scheme'] ?? '') !== 'https'
|
|
|| !in_array($parts['host'] ?? '', ['www.facebook.com', 'facebook.com', 'm.facebook.com'], true)
|
|
|| !preg_match('~^/(?:[a-z0-9.]+/)?(?:(?:posts|videos|reels?|photos)/[a-z0-9_.-]+|story\.php|permalink\.php|photo(?:\.php)?|watch)/?$~iD', $path)) {
|
|
return '';
|
|
}
|
|
|
|
$query = [];
|
|
parse_str($parts['query'] ?? '', $query);
|
|
$query = array_filter(
|
|
array_intersect_key($query, array_flip(['id', 'v', 'story_fbid', 'fbid'])),
|
|
static fn ($value): bool => is_string($value) && preg_match('/^[a-z0-9_-]+$/iD', $value) === 1
|
|
);
|
|
return 'https://www.facebook.com' . $path . ($query ? '?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986) : '');
|
|
}
|
|
|
|
function hgsf_fetch_facebook(array $connection): array|WP_Error {
|
|
$token = $connection['token'];
|
|
$page = hgsf_http_get('facebook', HGSF_FACEBOOK_VERSION . '/me', [
|
|
'fields' => 'id,is_published',
|
|
], $token);
|
|
if (is_wp_error($page)) {
|
|
return $page;
|
|
}
|
|
$page_id = $page['id'] ?? '';
|
|
if ((!is_string($page_id) && !is_int($page_id)) || (string) $page_id !== $connection['username']) {
|
|
return new WP_Error('account', 'Het Facebook-paginatoken hoort niet bij het ingestelde pagina-ID.');
|
|
}
|
|
if (($page['is_published'] ?? null) !== true) {
|
|
return new WP_Error('private', 'De Facebook-pagina is niet openbaar gepubliceerd.');
|
|
}
|
|
|
|
$response = hgsf_http_get('facebook', HGSF_FACEBOOK_VERSION . '/' . $connection['username'] . '/published_posts', [
|
|
'fields' => 'permalink_url,message,full_picture,created_time,is_hidden,is_published,is_expired',
|
|
'limit' => HGSF_POSTS_MAX,
|
|
], $token);
|
|
if (is_wp_error($response)) {
|
|
return $response;
|
|
}
|
|
if (!is_array($response['data'] ?? null)) {
|
|
return new WP_Error('api', 'Facebook heeft geen leesbare berichtenlijst teruggegeven.');
|
|
}
|
|
|
|
$posts = [];
|
|
foreach ($response['data'] as $post) {
|
|
$link = hgsf_facebook_permalink($post['permalink_url'] ?? null);
|
|
if (!$link || !empty($post['is_hidden']) || !empty($post['is_expired']) || ($post['is_published'] ?? true) !== true) {
|
|
continue;
|
|
}
|
|
$image = hgsf_media_url($post['full_picture'] ?? null, '/(?:^|\.)(fbcdn\.net|fbsbx\.com)$/iD');
|
|
$message = $post['message'] ?? '';
|
|
$timestamp = $post['created_time'] ?? '';
|
|
$caption = is_string($message) ? sanitize_text_field($message) : '';
|
|
$posts[$link] = [
|
|
'link' => $link,
|
|
'image' => $image,
|
|
'caption' => wp_html_excerpt($caption, 180, '…'),
|
|
'type' => $image ? 'IMAGE' : 'TEXT',
|
|
'date' => (is_string($timestamp) ? strtotime($timestamp) : false) ?: 0,
|
|
];
|
|
}
|
|
return array_values($posts);
|
|
}
|