54 lines
1.9 KiB
PHP
54 lines
1.9 KiB
PHP
<?php
|
|
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
function hgsf_fetch_x(array $connection): array|WP_Error {
|
|
$token = $connection['token'];
|
|
$handle = $connection['username'];
|
|
$response = hgsf_http_get('x', '2/users/by/username/' . rawurlencode($handle), [
|
|
'user.fields' => 'protected',
|
|
], $token);
|
|
if (is_wp_error($response)) {
|
|
return $response;
|
|
}
|
|
$account = $response['data'] ?? [];
|
|
$id = $account['id'] ?? '';
|
|
$username = $account['username'] ?? '';
|
|
if (!is_string($id) || !preg_match('/^[0-9]{1,19}$/D', $id) || !is_string($username) || strcasecmp($username, $handle) !== 0) {
|
|
return new WP_Error('account', 'X heeft geen openbaar profiel gevonden voor de ingestelde gebruikersnaam.');
|
|
}
|
|
if (($account['protected'] ?? null) !== false) {
|
|
return new WP_Error('private', 'Het ingestelde X-profiel is afgeschermd.');
|
|
}
|
|
|
|
$response = hgsf_http_get('x', '2/users/' . $id . '/tweets', [
|
|
'max_results' => 5,
|
|
'exclude' => 'replies,retweets',
|
|
'post.fields' => 'created_at,withheld',
|
|
], $token);
|
|
if (is_wp_error($response)) {
|
|
return $response;
|
|
}
|
|
$data = $response['data'] ?? [];
|
|
if (!is_array($data) || (!isset($response['data']) && ($response['meta']['result_count'] ?? null) !== 0)) {
|
|
return new WP_Error('api', 'X heeft geen leesbare berichtenlijst teruggegeven.');
|
|
}
|
|
|
|
$posts = [];
|
|
foreach ($data as $post) {
|
|
$id = $post['id'] ?? '';
|
|
$created_at = $post['created_at'] ?? '';
|
|
$time = is_string($created_at) ? strtotime($created_at) : false;
|
|
if (!is_string($id) || !preg_match('/^[0-9]{1,19}$/D', $id) || !$time || !empty($post['withheld'])) {
|
|
continue;
|
|
}
|
|
$posts[$id] = [
|
|
'id' => $id,
|
|
'link' => 'https://x.com/' . rawurlencode($handle) . '/status/' . $id,
|
|
'type' => 'EMBED',
|
|
'date' => $time,
|
|
];
|
|
}
|
|
return array_values($posts);
|
|
}
|