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); }