Skip to content

Commit

Permalink
Add 12-hour menu TTL and menu change purge
Browse files Browse the repository at this point in the history
  • Loading branch information
robertstaddon committed Apr 17, 2020
1 parent d30d5e3 commit 35be137
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
54 changes: 40 additions & 14 deletions litespeed-esi-divi-menus.php
Expand Up @@ -2,7 +2,7 @@
/*
Plugin Name: LiteSpeed ESI Divi Menus
Description: Replace Divi primary and secondary header menus with LiteSpeed ESI blocks
Version: 1.0
Version: 1.1
Author: Abundant Designs LLC
Author URI: https://www.abundantdesigns.com
License: GPLv2 or later
Expand All @@ -16,8 +16,11 @@

class LiteSpeed_ESI_Divi_Menus
{
private static $_instance ;

private static $_instance;

private $esi_divi_primary_menu_ttl = 43200;
private $esi_divi_secondary_menu_ttl = 43200;

const DIVI_PRIMARY_MENU_ID = "top-menu";
const DIVI_SECONDARY_MENU_ID = "et-secondary-nav";
const ESI_DIVI_PRIMARY_MENU_BLOCK = "litespeed_esi_divi_menu_primary";
Expand All @@ -40,29 +43,32 @@ public function init()
if ( ! defined( 'ET_CORE' ) ) return ;

if ( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) {
LiteSpeed_Cache_API::hook_tpl_not_esi( 'LiteSpeed_ESI_Divi_Menus::is_not_esi' ) ;
LiteSpeed_Cache_API::hook_tpl_esi( self::ESI_DIVI_PRIMARY_MENU_BLOCK, 'LiteSpeed_ESI_Divi_Menus::load_primary_menu' ) ;
LiteSpeed_Cache_API::hook_tpl_esi( self::ESI_DIVI_SECONDARY_MENU_BLOCK, 'LiteSpeed_ESI_Divi_Menus::load_secondary_menu' ) ;
LiteSpeed_Cache_API::hook_tpl_not_esi( array( $this, 'is_not_esi' ) );
LiteSpeed_Cache_API::hook_tpl_esi( self::ESI_DIVI_PRIMARY_MENU_BLOCK, array( $this, 'load_primary_menu' ) );
LiteSpeed_Cache_API::hook_tpl_esi( self::ESI_DIVI_SECONDARY_MENU_BLOCK, array( $this, 'load_secondary_menu' ) );

add_action( 'wp_update_nav_menu', array( $this, 'purge_esi' ) );
}
}

/**
* Hooked to LiteSpeed_Cache_API::hook_tpl_not_esi
*
* If the request is not an ESI request, hook to the add to wishlist button filter to replace it as an esi block.
* If the request is not an esi request, hook to the replace_wp_nav_menu filter to replace it as an esi block.
*/
public static function is_not_esi()
public function is_not_esi()
{
add_filter( 'wp_nav_menu', 'LiteSpeed_ESI_Divi_Menus::replace_wp_nav_menu', 999, 2) ;
add_filter( 'wp_nav_menu', array( $this, 'replace_wp_nav_menu' ), 999, 2) ;
}


/*
* Hooked to 'wp_nav_menu' filter
*
* The Divi primary and secondary menus contain user-specific content. Replace them with ESI blocks.
* The Divi primary and secondary menus contain user-specific content. Replace them with esi blocks.
*/
public static function replace_wp_nav_menu( $nav_menu_html, $args ) {
public function replace_wp_nav_menu( $nav_menu_html, $args )
{
if ( $args->menu_id == self::DIVI_PRIMARY_MENU_ID ) {
return LiteSpeed_Cache_API::esi_url( self::ESI_DIVI_PRIMARY_MENU_BLOCK, 'LiteSpeed Divi Primary Menu' ) ;
}
Expand All @@ -76,7 +82,12 @@ public static function replace_wp_nav_menu( $nav_menu_html, $args ) {
/**
* Hooked to LiteSpeed_Cache_API::hook_tpl_esi
*/
public static function load_primary_menu( ) {
public function load_primary_menu( )
{
LiteSpeed_Cache_API::debug( "Divi ESI Menus set primary menu TTL to " . $this->esi_divi_primary_menu_ttl ) ;
LiteSpeed_Cache_API::set_ttl( $this->esi_divi_primary_menu_ttl );
LiteSpeed_Cache_API::add_private( LiteSpeed_Cache_Tag::TYPE_ESI . self::DIVI_PRIMARY_MENU_ID ) ; // Tag ESI block for easy purging if needed

wp_nav_menu( array(
'theme_location' => 'primary-menu',
'container' => '',
Expand All @@ -85,7 +96,12 @@ public static function load_primary_menu( ) {
'menu_id' => 'top-menu'
) );
}
public static function load_secondary_menu( ) {
public function load_secondary_menu( )
{
LiteSpeed_Cache_API::debug( "Divi ESI Menus set seconary TTL to " . $this->esi_divi_secondary_menu_ttl ) ;
LiteSpeed_Cache_API::set_ttl( $this->esi_divi_secondary_menu_ttl ) ;
LiteSpeed_Cache_API::add_private( LiteSpeed_Cache_Tag::TYPE_ESI . self::DIVI_SECONDARY_MENU_ID ) ; // Tag ESI block for easy purging if needed

wp_nav_menu( array(
'theme_location' => 'secondary-menu',
'container' => '',
Expand All @@ -94,13 +110,23 @@ public static function load_secondary_menu( ) {
) );
}


/**
* Purge esi private tag
*/
public function purge_esi()
{
LiteSpeed_Cache_API::debug( 'Divi ESI Menus purge ESI' ) ;
LiteSpeed_Cache_API::purge_private( LiteSpeed_Cache_Tag::TYPE_ESI . self::DIVI_PRIMARY_MENU_ID ) ;
LiteSpeed_Cache_API::purge_private( LiteSpeed_Cache_Tag::TYPE_ESI . self::DIVI_SECONDARY_MENU_ID ) ;
}

/**
* Get the current instance object.
*/
public static function get_instance()
{
if ( ! isset(self::$_instance) ) {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new self() ;
}

Expand Down

0 comments on commit 35be137

Please sign in to comment.