Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions integration/class-w3-total-cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* W3 Total Cache extension for ActivityPub requests.
*
* @package activitypub
*/

namespace Activitypub\Integration;

use function Activitypub\is_activitypub_request;

/**
* W3 Total Cache extension for ActivityPub requests.
*/
class W3_Total_Cache {

/**
* Initialize the W3 Total Cache integration.
*/
public static function init() {
\w3tc_add_action( 'pagecache_page_key', array( self::class, 'pagecache_page_key' ) );
\add_filter( 'w3tc_pgcache_rules_apache_last', array( self::class, 'w3tc_pgcache_rules_apache_last' ) );
}

/**
* Filter: Allow adding additional rules at the end of the PGCACHE_CORE block, before the last rule.
*
* @since 2.7.1
*
* @param string $key The page key.
*
* @return string The modified page key.
*/
public static function pagecache_page_key( $key ) {
// Check if this is an ActivityPub request.
if ( is_activitypub_request() ) {
$key['key'][1] .= '_activitypub';
}

return $key;
}

/**
* Modify the Apache caching rules for ActivityPub requests.
*
* @param array $rules The existing Apache rules.
*
* @return array The modified Apache rules.
*/
public static function w3tc_pgcache_rules_apache_last( $rules ) {
// Add check for ActivityPub Accept header (Content Negotiation).
$rules .= 'RewriteCond %{HTTP:Accept} (application/(ld\+json|activity\+json|json)) [NC]' . PHP_EOL;
$rules .= 'RewriteRule .* - [E=W3TC_ACTIVITYPUB:_activitypub]' . PHP_EOL;

// Also set the environment variable if activitypub parameter is explicitly set.
$rules .= 'RewriteCond %{QUERY_STRING} (?:^|&)activitypub(?:=|&|$) [NC]' . PHP_EOL;
$rules .= 'RewriteRule .* - [E=W3TC_ACTIVITYPUB:_activitypub]' . PHP_EOL;

return $rules;
}
}
9 changes: 9 additions & 0 deletions integration/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,12 @@ function ( $post_types ) {
* @see https://buddypress.org/
*/
add_action( 'bp_include', array( __NAMESPACE__ . '\Buddypress', 'init' ), 0 );

/**
* Load the W3 Total Cache integration.
*
* @see https://w3-total-cache.com/
*/
if ( function_exists( 'w3tc_add_action' ) ) {
\w3tc_add_action( 'wp_loaded', array( __NAMESPACE__ . '\W3_Total_Cache', 'init' ) );
}
Loading