| 1 |
<?php |
| 2 |
/** |
| 3 |
* Content negotiation fix for Surge. |
| 4 |
* |
| 5 |
* @see https://dominikschilling.de/notes/http-accept-header-wordpress-cache-activitypub/ |
| 6 |
* |
| 7 |
* @package Activitypub |
| 8 |
*/ |
| 9 |
|
| 10 |
$representation = 'html'; |
| 11 |
|
| 12 |
if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) { |
| 13 |
/* |
| 14 |
* For a given URL, the html and ActivityPub representations must land in different cache buckets, |
| 15 |
* and this boundary must match how the plugin negotiates that same URL, or one representation could |
| 16 |
* be replayed for the other. Reuse the plugin's single source of truth: |
| 17 |
* Activitypub\accept_prefers_activitypub(), which buckets a request as JSON when its highest-priority |
| 18 |
* (by `q`, then order) media type is ActivityPub. That keeps an ActivityPub-preferring client such |
| 19 |
* as Mastodon (which also accepts `text/html;q=0.1`) in the JSON bucket instead of the html one. |
| 20 |
* |
| 21 |
* This mirrors only the Accept-header branch of Query::is_activitypub_request(), which is the sole |
| 22 |
* branch that varies a single URL. The `?activitypub` override also forces JSON, but it changes |
| 23 |
* the URL and Surge already keys on the full URL, so those requests are segregated on their own and |
| 24 |
* never share a bucket with the negotiated ones. |
| 25 |
* |
| 26 |
* Do NOT call Activitypub\is_activitypub_request() here. Surge runs this config from its |
| 27 |
* advanced-cache.php drop-in, on the cache-serve path, before the plugin, the main $wp_query, and |
| 28 |
* the plugin's autoloader exist, so that function (which needs all three) would fatal. Only the |
| 29 |
* dependency-free accept_prefers_activitypub() is safe on this path. functions-request.php is |
| 30 |
* side-effect-free and the plugin loads it with require_once, so including it here neither runs |
| 31 |
* code nor risks a redeclare. |
| 32 |
*/ |
| 33 |
require_once __DIR__ . '/../includes/functions-request.php'; |
| 34 |
|
| 35 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 36 |
if ( \Activitypub\accept_prefers_activitypub( $_SERVER['HTTP_ACCEPT'] ) ) { |
| 37 |
$representation = 'json'; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
| 42 |
$config['variants']['representation'] = $representation; |
| 43 |
unset( $representation ); |
| 44 |
|
| 45 |
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
| 46 |
return $config; |
| 47 |
|