Note: LiteSpeed WordPress Cache plugin needs to be v1.3+.

Let's take WPBakery Page Builder Version 5.3 as an example. It used WordPress nonce in frontend. So once it got cached, the nonce will fail to pass validation once expired. With ESI, it can be made to a private/public cache separate from full page.

This is how to do it manually:

In

js_composer/include/helpers/helpers_factory.php

line 486,

function vc_generate_nonce( $data ) {
	return wp_create_nonce( is_array( $data ) ? ( 'vc-nonce-' . implode( '|', $data ) ) : ( 'vc-nonce-' . $data ) );
}

change it to

function vc_generate_nonce( $data, $from_esi = false ) {
	if ( ! $from_esi && ! vc_is_frontend_editor()  ) {
		if ( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) {
			if ( method_exists( 'LiteSpeed_Cache_API', 'v' ) && LiteSpeed_Cache_API::v( '1.3' ) ) {
				$params = array( 'data' => $data ) ;
				return LiteSpeed_Cache_API::esi_url( 'js_composer', 'WPBakery Page Builder', $params, 'default', true ) ;// The last parameter is to remove ESI comment wrapper
			}
		}
	}
	return wp_create_nonce( is_array( $data ) ? ( 'vc-nonce-' . implode( '|', $data ) ) : ( 'vc-nonce-' . $data ) );
}
 
 
/**
 * @param $params
 *
 * @return string
 */
function hook_esi( $params ) {
	$data = $params[ 'data' ] ;
	echo vc_generate_nonce( $data, true ) ;
	exit ;
}

Then, in

js_composer/js_composer.php

line 244,

	public function init() {

Change it to

	public function init() {
		if ( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) {
			LiteSpeed_Cache_API::hook_tpl_esi( 'js_composer', 'hook_esi' ) ;
		}

Now you can see WordPress nonce can be cached correctly.

  • Admin
  • Last modified: 2017/10/02 14:34
  • by Hai Zheng