['id' => 'ig', 'username' => 'fixture', 'token' => 'ig-fixture-token', 'saved_at' => time() - 2 * DAY_IN_SECONDS], 'facebook' => ['id' => 'fb', 'username' => '12345', 'profile' => 'example.page', 'token' => 'fb-fixture-token'], 'x' => ['id' => 'x', 'username' => 'fixture_x', 'token' => 'x%2Ffixture', 'enabled' => false], ]; $snapshots = []; foreach (HGSF_SOURCES as $source => $provider) { add_filter('pre_option_' . $provider['connection'], static function () use (&$connections, $source): array { return $connections[$source] ?? []; }); add_filter('pre_update_option_' . $provider['connection'], static function ($value, $old) use (&$connections, $source) { $connections[$source] = $value; return $old; }, 10, 2); add_filter('pre_option_' . $provider['snapshot'], static function () use (&$snapshots, $source): array { return $snapshots[$source] ?? []; }); add_filter('pre_update_option_' . $provider['snapshot'], static function ($value, $old) use (&$snapshots, $source) { $snapshots[$source] = $value; return $old; }, 10, 2); } $media = []; for ($index = 1; $index <= 10; $index++) { $media[] = [ 'media_type' => $index === 10 ? 'VIDEO' : 'IMAGE', 'media_url' => 'https://scontent-test.cdninstagram.com/image-' . $index . '.jpg', 'thumbnail_url' => 'https://scontent-test.cdninstagram.com/video.jpg', 'permalink' => 'https://www.instagram.com/p/fixture_' . $index . '/?igsh=test', 'caption' => 'Voorbeeld "bericht"', 'timestamp' => gmdate(DATE_ATOM, strtotime('2026-01-01') + $index * HOUR_IN_SECONDS), ]; } $media[] = array_replace($media[0], ['permalink' => 'https://example.org/p/fixture/']); $media[] = array_replace($media[0], ['media_url' => 'https://cdninstagram.com.example.org/image.jpg']); $mode = 'normal'; $requests = []; $mock_http = static function ($pre, array $args, string $url) use (&$mode, &$requests, $media) { $parts = wp_parse_url($url); $host = $parts['host']; $path = $parts['path']; parse_str($parts['query'], $query); $requests[] = [$host, $path]; hgsf_test(in_array($host, ['api.x.com', 'graph.instagram.com', 'graph.facebook.com'], true) && $args['redirection'] === 0, 'Unexpected outbound request.'); if ($mode === 'offline') { return new WP_Error('test_offline', 'Original transport error should stay private.'); } if ($host === 'graph.instagram.com') { if ($path === '/refresh_access_token') { hgsf_test($query['access_token'] === 'ig-fixture-token' && $query['grant_type'] === 'ig_refresh_token', 'Instagram refresh parameters.'); $data = ['access_token' => 'new-ig-fixture-token', 'expires_in' => 60 * DAY_IN_SECONDS]; } elseif (str_ends_with($path, '/me')) { hgsf_test($query['access_token'] === 'new-ig-fixture-token', 'Instagram reuses the renewed token.'); $data = ['user_id' => '789', 'username' => $mode === 'wrong_ig' ? 'someone_else' : 'fixture']; } else { hgsf_test(str_ends_with($path, '/789/media'), 'Instagram media endpoint.'); $data = ['data' => $mode === 'empty' ? [] : $media]; } } elseif ($host === 'graph.facebook.com') { hgsf_test($args['headers']['Authorization'] === 'Bearer fb-fixture-token' && !isset($query['access_token']), 'Facebook token must stay out of the URL.'); if (str_ends_with($path, '/me')) { $data = ['id' => '12345', 'is_published' => $mode !== 'private_fb']; } else { hgsf_test(str_ends_with($path, '/12345/published_posts'), 'Facebook published-posts endpoint.'); $photo = ['permalink_url' => 'https://www.facebook.com/example.page/posts/pfbid123/?tracking=test', 'message' => 'Foto van Facebook', 'full_picture' => 'https://scontent-test.fbcdn.net/image.jpg', 'created_time' => '2026-01-04T10:00:00Z']; $text = ['permalink_url' => 'https://m.facebook.com/story.php?story_fbid=888&id=12345&tracking=test', 'message' => 'Bericht zonder foto', 'created_time' => '2026-01-02T10:00:00Z']; $hidden = ['permalink_url' => 'https://www.facebook.com/example.page/posts/444', 'is_hidden' => true]; $outside = ['permalink_url' => 'https://www.facebook.com/l.php?u=https://example.org']; $data = ['data' => [$text, $photo, $hidden, $outside]]; } } else { hgsf_test($args['headers']['Authorization'] === 'Bearer x%2Ffixture' && !isset($query['access_token']), 'X token must stay out of the URL.'); if ($mode === 'credits') { return ['response' => ['code' => 402], 'body' => '{"detail":"private diagnostics"}']; } if ($path === '/2/users/by/username/fixture_x') { $data = ['data' => ['id' => '999', 'username' => 'fixture_x', 'protected' => $mode === 'private_x']]; } else { hgsf_test($path === '/2/users/999/tweets' && $query['max_results'] === '5' && $query['exclude'] === 'replies,retweets', 'Bounded X timeline request.'); $data = ['data' => [ ['id' => '991', 'created_at' => '2026-01-03T10:00:00Z'], ['id' => '990', 'created_at' => '2026-01-03T09:00:00Z', 'withheld' => ['country_codes' => ['NL']]], ]]; } } return ['response' => ['code' => 200], 'body' => wp_json_encode($data)]; }; add_filter('pre_http_request', $mock_http, 10, 3); hgsf_test(is_wp_error(hgsf_sync_source('x')) && !$requests, 'No X API use without opt-in, even with a token.'); hgsf_test(hgsf_sync_instagram() === true && count($requests) === 3, 'Legacy Instagram configuration and token renewal.'); hgsf_test(count($snapshots['instagram']['posts']) === 10 && str_ends_with($snapshots['instagram']['posts'][0]['image'], '/video.jpg'), 'Instagram host checks, order and video thumbnail.'); hgsf_test(hgsf_sync_instagram() === true && count($requests) === 5, 'No repeated token renewal.'); hgsf_test(hgsf_sync_source('facebook') === true && count($snapshots['facebook']['posts']) === 2, 'Facebook excludes hidden posts and non-post links.'); $connections['x']['enabled'] = true; hgsf_test(hgsf_sync_source('x') === true && count($snapshots['x']['posts']) === 1, 'X retrieves recent public posts.'); wp_set_current_user(0); $request_count = count($requests); $icons = do_shortcode('[hg_sociallinks]'); hgsf_test(!wp_script_is('hg-socialfeed-x', 'enqueued'), 'Icon bar does not load the X embed script.'); $html = do_shortcode('[hg_socialfeed limit="4"]'); $ig_only = do_shortcode('[hg_socialfeed sources="instagram"]'); hgsf_test(wp_script_is('hg-socialfeed-x', 'enqueued'), 'The mixed feed loads the official X embed script.'); hgsf_test(count($requests) === $request_count, 'Shortcodes must never call a provider API.'); hgsf_test(substr_count($html, 'tabindex="-1"') === 13 && str_contains($html, 'Meer berichten'), 'All three feeds and load more.'); hgsf_test(strpos($html, 'pfbid123') < strpos($html, '/status/991') && strpos($html, '/status/991') < strpos($html, 'story_fbid=888'), 'Combined order uses publication dates.'); hgsf_test(!str_contains($html, 'tracking=') && !str_contains($html, 'igsh=') && str_contains($html, '"bericht"'), 'Strip tracking and escape captions.'); hgsf_test(str_contains($html, 'Bericht zonder foto') && str_contains($html, 'data-dnt="true"'), 'Facebook text and official X embed.'); hgsf_test(!str_contains($ig_only, 'facebook.com') && !str_contains($ig_only, 'x.com'), 'Feed source selection.'); hgsf_test(substr_count($icons, 'get_error_code() === 'private' && !hgsf_cached_posts(hgsf_connection('facebook'), 'facebook'), 'Unpublished Facebook page clears cached posts.'); $mode = 'private_x'; hgsf_test(hgsf_sync_source('x')->get_error_code() === 'private' && !hgsf_cached_posts(hgsf_connection('x'), 'x'), 'Protected X account clears cached posts.'); $mode = 'credits'; hgsf_test(hgsf_sync_source('x')->get_error_code() === 'credits', 'Clear X credits error.'); $mode = 'wrong_ig'; hgsf_test(hgsf_sync_instagram()->get_error_code() === 'account' && !hgsf_cached_posts(hgsf_connection()), 'Wrong Instagram account clears cached posts.'); $mode = 'empty'; hgsf_test(hgsf_sync_instagram() === true && !hgsf_cached_posts(hgsf_connection()), 'Successful empty result remains empty.'); $mode = 'normal'; hgsf_test(hgsf_sync_instagram() === true, 'Restore Instagram fixtures for expiration check.'); $snapshots['instagram']['synced_at'] = time() - HGSF_CACHE_LIFETIME; hgsf_test(!str_contains(do_shortcode('[hg_socialfeed sources="instagram"]'), 'hgsf-grid'), 'Expired cache falls back to the profile link.'); $connections['x'] = ['username' => 'fixture_x']; hgsf_test(str_contains(do_shortcode('[hg_sociallinks sources="x"]'), 'x.com/fixture_x'), 'Icons work with only a profile and no access token.'); $die_handler = static fn () => static function ($message, $title, array $args = []) { throw new RuntimeException((string) ($args['response'] ?? 0)); }; add_filter('wp_die_handler', $die_handler); try { hgsf_handle_settings(); hgsf_test(false, 'Anonymous settings request must be rejected.'); } catch (RuntimeException $error) { hgsf_test($error->getMessage() === '403', 'Anonymous settings response code.'); } $admins = get_users(['role' => 'administrator', 'number' => 1, 'fields' => 'ID']); hgsf_test(!empty($admins), 'An administrator is required for the nonce test.'); wp_set_current_user($admins[0]); require_once ABSPATH . 'wp-admin/includes/template.php'; ob_start(); hgsf_admin_page(); $admin_html = ob_get_clean(); hgsf_test(substr_count($admin_html, 'type="password"') === 3 && !str_contains($admin_html, '-fixture-token'), 'Admin renders three source settings without revealing tokens.'); $request_count = count($requests); hgsf_test(hgsf_save_settings('facebook', ['operation' => 'save', 'username' => '12345', 'profile' => 'updated.page']) === 'saved', 'Updating the Facebook profile link succeeds.'); hgsf_test($connections['facebook']['token'] === 'fb-fixture-token' && count($requests) === $request_count, 'Updating an icon link preserves the Page token without fetching posts.'); hgsf_test(str_contains(do_shortcode('[hg_sociallinks sources="facebook"]'), 'facebook.com/updated.page'), 'Updated profile link is reflected in the icon bar.'); try { $_POST['source'] = 'facebook'; $_REQUEST['hgsf_nonce_facebook'] = wp_create_nonce('hgsf_settings_x'); hgsf_handle_settings(); hgsf_test(false, 'A different source nonce must be rejected.'); } catch (RuntimeException $error) { hgsf_test($error->getMessage() === '403', 'Cross-source nonce response code.'); } remove_filter('wp_die_handler', $die_handler); remove_filter('pre_http_request', $mock_http); if (getenv('HGSF_FIXTURE_HTML') === '1') { fwrite(STDOUT, $html . $icons); } else { WP_CLI::success('HG Social Feed integration checks passed.'); }