PHP code example of pantheon-systems / pantheon-advanced-page-cache

1. Go to this page and download the library: Download pantheon-systems/pantheon-advanced-page-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?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' );

add_filter( 'pantheon_cache_default_max_age', function() {
	return 10 * DAY_IN_SECONDS;
} );

do_action( 'pantheon_cache_nonce_lifetime' );

add_filter( 'pantheon_apc_disable_admin_notices', '__return_true' );

add_filter( 'pantheon_apc_disable_admin_notices', function( $disable_notices, $callback ) {
    if ( $callback === '\\Pantheon_Advanced_Page_Cache\\Admin_Interface\\admin_notice_maybe_recommend_higher_max_age' ) {
        return true;
    }
    return $disable_notices;
}, 10, 2 );