Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
litespeed_wiki:cache:lscwp:esi_nonce [2017/09/24 15:36] Hai Zheng |
litespeed_wiki:cache:lscwp:esi_nonce [2020/11/14 15:23] (current) Lisa Clarke Redirect to new Documentation Site |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Note: LiteSpeed WordPress Cache plugin needs to be v1.2.4+. | + | ~~REDIRECT>https://docs.litespeedtech.com/lscache/lscwp/cache/~~ |
- | + | ||
- | 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 <code>js_composer/include/helpers/helpers_factory.php</code> line 486, | + | |
- | <syntaxhighlight lang='php'> | + | |
- | function vc_generate_nonce( $data ) { | + | |
- | return wp_create_nonce( is_array( $data ) ? ( 'vc-nonce-' . implode( '|', $data ) ) : ( 'vc-nonce-' . $data ) ); | + | |
- | } | + | |
- | </syntaxhighlight> | + | |
- | + | ||
- | change it to | + | |
- | <syntaxhighlight lang='php'> | + | |
- | function vc_generate_nonce( $data, $from_esi = false ) { | + | |
- | if ( ! $from_esi ) { | + | |
- | 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.2.4' ) ) { | + | |
- | $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 ; | + | |
- | } | + | |
- | </syntaxhighlight> | + | |
- | + | ||
- | + | ||
- | Then, in <code>js_composer/js_composer.php</code> line 244, | + | |
- | <syntaxhighlight lang='php'> | + | |
- | public function init() { | + | |
- | </syntaxhighlight> | + | |
- | + | ||
- | Change it to | + | |
- | <syntaxhighlight lang='php'> | + | |
- | 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' ) ; | + | |
- | } | + | |
- | </syntaxhighlight> | + | |
- | + | ||
- | Now you can see WordPress nonce can be cached correctly. | + |