Created
February 5, 2024 20:46
-
-
Save MadtownLems/fba96569183a3deaf5f1aa2a2feafe88 to your computer and use it in GitHub Desktop.
Surge Modifier Proof of Concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Surge Modifier | |
* Description: Modifies the behavior of the Surge caching plugin to aggressively invalidate a subsite's cache when content has changed | |
* Version: 3 | |
* Author: Jason LeMahieu | |
* License: GPL v2 or later | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
/* CHANGELOG | |
3: Add save_post | |
*/ | |
// NOTE: This gist is pending Surge developer feedback. Do not use yet (unless you know what you're doing more than I do) | |
/* Flag each entry with the current network and blog id. This should invalidate an entire subsite's cache on any of the impacted actions | |
Note: The actual expiring is done by this bit in Surge/includes/invalidate.php, by expiring everything with this same flag | |
$expire_flag = is_multisite() | |
? sprintf( 'network:%d:%d', get_current_network_id(), get_current_blog_id() ) | |
: '/'; | |
foreach ( $flush_actions as $action ) { | |
if ( did_action( $action ) ) { | |
expire( $expire_flag ); | |
break; | |
} | |
} | |
*/ | |
add_action( 'template_redirect', 'surge_modifier_template_redirect' ); | |
function surge_modifier_template_redirect( ) { | |
Surge\flag( sprintf( 'network:%d:%d', get_current_network_id(), get_current_blog_id() ) ); | |
} | |
/* Invalidate an entire subsite's cache on additional actions */ | |
add_filter( 'surge_flush_actions', 'ext_surge_modifier_filter_surge_flush_actions' ); | |
function ext_surge_modifier_filter_surge_flush_actions( $actions ) { | |
$actions[] = 'wp_update_nav_menu'; | |
$actions[] = 'wp_update_nav_menu_item'; | |
$actions[] = 'wp_add_nav_menu_item'; | |
$actions[] = 'delete_widget'; | |
$actions[] = 'save_post'; | |
return $actions; | |
} | |
/* This example was provided by the plugin author (https://github.com/kovshenin/surge/issues/29) | |
// Flag each entry with the current blog id. | |
add_action( 'template_redirect', function() { | |
Surge\flag( sprintf( 'blogid:%d', get_current_blog_id() ) ); | |
} ); | |
// Expire all subsite cache on certain events. | |
add_action( 'certain_event', function() { | |
Surge\expire( sprintf( 'blogid:%d', get_current_blog_id() ) ); | |
} ); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment