<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pantheon-systems / pantheon-advanced-page-cache example snippets
/**
* Add surrogate key for the featured content sidebar rendered on the homepage.
*/
add_filter( 'pantheon_wp_main_query_surrogate_keys', function( $keys ){
if ( is_home() ) {
$keys[] = 'sidebar-home-featured';
}
return $keys;
});
/**
* Trigger a purge event for the featured content sidebar when widgets are updated.
*/
add_action( 'update_option_sidebars_widgets', function() {
pantheon_wp_clear_edge_keys( array( 'sidebar-home-featured' ) );
});
/**
* An example of pre-fetching a WP_Query to tag the
* response with queried data. You'd use `papcx_wp_query()`
* a second time within your template to use the data.
*/
add_filter( 'pantheon_wp_main_query_surrogate_keys', function( $keys ) {
if ( is_home() ) {
$query = papcx_wp_query( array(
'post_type' => 'page',
) );
foreach( $query->posts as $post ) {
$keys[] = 'post-' . $post->ID;
}
}
return $keys;
});
/**
* Register a 'papc-non-persistent' cache group to cache data
* in a non-persistent manner. We only want data in this group
* to be cached within the page request.
*/
add_action( 'init', function(){
wp_cache_add_non_persistent_groups( array( 'papc-non-persistent' ) );
});
/**
* Helper function to instantiate a WP_Query object only
* once per page request.
*
* @param array $args Arguments to pass to WP_Query.
* @return WP_Query
*/
function papcx_wp_query( $args = array() ) {
$cache_key = md5( serialize( $args ) );
// WP_Query object will be in cache the second time we use the function.
$cache_value = wp_cache_get( $cache_key, 'papc-non-persistent' );
if ( false !== $cache_value ) {
return $cache_value;
}
$query = new WP_Query( $args );
wp_cache_set( $cache_key, $query, 'papc-non-persistent' );
return $query;
}
/**
* Add a custom post type to the ignored post types.
*
* @param array $ignored_post_types The array of ignored post types.
* @return array
*/
function filter_ignored_posts( $ignored_post_types ) {
$ignored_post_types[] = 'my-post-type'; // Ignore my-post-type from cache purges.
return $ignored_post_types;
}
add_filter( 'pantheon_purge_post_type_ignored', 'filter_ignored_posts' );