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_sample [2017/09/24 13:59] Hai Zheng formating |
litespeed_wiki:cache:lscwp:esi_sample [2018/04/25 21:48] (current) Hai Zheng |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | == Summary == | + | Assume the following code in one WordPress plugin is some private info in one page: |
- | For example, assume the following code in one WordPress plugin is some private info in one page. | + | |
- | <syntaxhighlight lang='php'> | + | <code php> |
function show( $id ) { | function show( $id ) { | ||
- | return "ID: $id <a href='test?nonce=$nonce'>Click Me</a>"; | + | return "ID: $id <a href='test?nonce=" . wp_create_nonce() . "'>Click Me</a>"; |
} | } | ||
echo show(35); | echo show(35); | ||
- | </syntaxhighlight> | + | </code> |
With LSCWP ESI API, it can be changed to this: | With LSCWP ESI API, it can be changed to this: | ||
- | <syntaxhighlight lang='php'> | + | |
+ | ==== Solution One ==== | ||
+ | |||
+ | <code php> | ||
function show( $id, $from_esi = false ) { | function show( $id, $from_esi = false ) { | ||
// To make sure it is the original call | // To make sure it is the original call | ||
Line 18: | Line 20: | ||
if( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) { | if( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) { | ||
// To make sure is using the compatible API version | // To make sure is using the compatible API version | ||
- | if ( method_exists( 'LiteSpeed_Cache_API', 'v' ) && LiteSpeed_Cache_API::v( '1.2.4' ) ) { | + | if ( method_exists( 'LiteSpeed_Cache_API', 'v' ) && LiteSpeed_Cache_API::v( '1.3' ) ) { |
$params = array( 'id' => $id ) ;// If you have any parameters want to pass | $params = array( 'id' => $id ) ;// If you have any parameters want to pass | ||
// Let's turn this block to ESI by returning directly | // Let's turn this block to ESI by returning directly | ||
Line 25: | Line 27: | ||
} | } | ||
} | } | ||
- | return "ID: $id <a href='test?nonce=$nonce'>Click Me</a>"; | + | return "ID: $id <a href='test?nonce=" . wp_create_nonce() . "'>Click Me</a>"; |
} | } | ||
echo show(35); | echo show(35); | ||
- | </syntaxhighlight> | + | </code> |
And then, in your main process, hook the ESI call to the real content: | And then, in your main process, hook the ESI call to the real content: | ||
- | <syntaxhighlight lang='php'> | + | <code php> |
if ( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) { | if ( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) { | ||
LiteSpeed_Cache_API::hook_tpl_esi('my_plugin_esi', 'hook_esi' ); | LiteSpeed_Cache_API::hook_tpl_esi('my_plugin_esi', 'hook_esi' ); | ||
Line 40: | Line 42: | ||
$id = $params[ 'id' ] ; | $id = $params[ 'id' ] ; | ||
echo show( $id, true ) ; | echo show( $id, true ) ; | ||
+ | exit; | ||
} | } | ||
- | </syntaxhighlight> | + | </code> |
- | == Example == | ||
- | Let's take **Caldera Forms Version 1.5.6.1** as a sample. In `caldera-forms/classes/render/nonce.php` line 86 `public static function nonce_field( $form_id )` is where the form nonce is generated. So to convert it to ESI, change | ||
- | <syntaxhighlight lang='php'> | ||
- | public static function nonce_field( $form_id ){ | ||
- | $nonce_field = '<input type="hidden" id="' . esc_attr( self::nonce_field_name( $form_id ) ) . '" name="' . esc_attr( self::nonce_field_name() ) . '" value="' . esc_attr( self::create_verify_nonce( $form_id ) ) . '" data-nonce-time="' . esc_attr( time() ) . '" />'; | ||
- | $nonce_field .= wp_referer_field( false ); | ||
- | return $nonce_field; | ||
- | } | ||
- | </syntaxhighlight> | ||
- | to | ||
- | <syntaxhighlight lang='php'> | ||
- | public static function nonce_field( $form_id, $from_esi = false ){ | ||
- | if ( ! $from_esi ) { | + | ==== Solution Two ==== |
- | 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( 'form_id' => $form_id ) ; | + | |
- | return LiteSpeed_Cache_API::esi_url( 'caldera_forms', 'Caldera Forms', $params ) ; | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | $nonce_field = '<input type="hidden" id="' . esc_attr( self::nonce_field_name( $form_id ) ) . '" name="' . esc_attr( self::nonce_field_name() ) . '" value="' . esc_attr( self::create_verify_nonce( $form_id ) ) . '" data-nonce-time="' . esc_attr( time() ) . '" />'; | + | Assuming ESI is ON and LSCWP is up-to-date |
- | $nonce_field .= wp_referer_field( false ); | + | |
- | return $nonce_field; | + | |
- | } | + | |
- | /** | + | Separated `show()` function to make it easier to understand: |
- | * Handle ESI request | + | <code php> |
- | * | + | |
- | */ | + | LiteSpeed_Cache_API::hook_tpl_esi('my_plugin_esi', 'hook_esi' ); |
- | public static function hook_esi( $params ) { | + | |
- | $form_id = $params[ 'form_id' ] ; | + | function hook_esi( $param ) { |
- | echo self::nonce_field( $form_id, true ) ; | + | $id = $params[ 'id' ] ; |
- | exit ; | + | echo "ID: $id <a href='test?nonce=" . wp_create_nonce() . "'>Click Me</a>"; |
- | } | + | exit; |
- | </syntaxhighlight> | + | } |
+ | |||
+ | function show( $id ) { | ||
+ | $params = array( 'id' => $id ) ; | ||
+ | return LiteSpeed_Cache_API::esi_url( 'my_plugin_esi', 'My Plugin Name', $params ) ; | ||
+ | } | ||
+ | |||
+ | echo show(35); | ||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
+ | ===== More Sample Codes ===== | ||
- | Then go to `caldera-forms/classes/core.php`, in `function __construct()` line 146 or other preferred position, add this: | + | [[litespeed_wiki:cache:lscwp:esi_replace|Plugin Example: How to use ESI API in plugin "Caldera Form" ?]] |
- | <syntaxhighlight lang='php'> | + | |
- | if ( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) { | + | |
- | LiteSpeed_Cache_API::hook_tpl_esi( 'caldera_forms', 'Caldera_Forms_Render_Nonce::hook_esi' ); | + | |
- | } | + | |
- | </syntaxhighlight> | + | [[litespeed_wiki:cache:lscwp:esi_nonce|Plugin Example: How to use ESI API to replace WordPress nonce in plugin "Visual Composer" ?]] |
- | Now **Caldera Forms** can be perfectly cached with ESI on. |