Skip to content

Commit

Permalink
3.0.0-beta16: WCML currency supported; vary API half done
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-hai committed Mar 5, 2020
1 parent 47b91b6 commit 746bf3a
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 101 deletions.
4 changes: 2 additions & 2 deletions litespeed-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Plugin Name: LiteSpeed Cache
* Plugin URI: https://www.litespeedtech.com/products/cache-plugins/wordpress-acceleration
* Description: High-performance page caching and site optimization from LiteSpeed
* Version: 3.0.0-beta15
* Version: 3.0.0-beta16
* Author: LiteSpeed Technologies
* Author URI: https://www.litespeedtech.com
* License: GPLv3
Expand Down Expand Up @@ -45,7 +45,7 @@
return;
}

! defined( 'LSCWP_V' ) && define( 'LSCWP_V', '3.0.0-beta15' );
! defined( 'LSCWP_V' ) && define( 'LSCWP_V', '3.0.0-beta16' );

! defined( 'LSCWP_CONTENT_DIR' ) && define( 'LSCWP_CONTENT_DIR', WP_CONTENT_DIR ) ;
! defined( 'LSCWP_DIR' ) && define( 'LSCWP_DIR', dirname( __FILE__ ) . '/' ) ;// Full absolute path '/var/www/html/***/wp-content/plugins/litespeed-cache/' or MU
Expand Down
98 changes: 37 additions & 61 deletions src/api.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,63 +48,77 @@ public static function init()
/**
* Init
*/
// API::hook_init( $hook ) -> action `litespeed_init`
// API::hook_init( $hook ) -> Action `litespeed_init`

/**
* Conf
*/
add_filter( 'litespeed_conf', __NAMESPACE__ . '\Conf::val' ); // API::config($id) -> filter `litespeed_conf`
// API::conf_append( $name, $default ) -> action `litespeed_conf_append`
add_filter( 'litespeed_conf', __NAMESPACE__ . '\Conf::val' ); // API::config($id) -> Filter `litespeed_conf`
// API::conf_append( $name, $default ) -> Action `litespeed_conf_append`
add_action( 'litespeed_conf_multi_switch', __CLASS__ . '::conf_multi_switch', 10, 2 ) ;
// API::force_option( $k, $v ) -> action ``litespeed_conf_force`
// API::force_option( $k, $v ) -> Action ``litespeed_conf_force`

/**
* Cache Control Hooks
*/
// API::hook_control($tags) && action `litespeed_api_control` -> action `litespeed_control_finalize`
add_action( 'litespeed_control_set_private', __NAMESPACE__ . '\Control::set_private' ); // API::set_cache_private() -> action `litespeed_control_set_private`
add_action( 'litespeed_control_set_nocache', __NAMESPACE__ . '\Control::set_nocache' ); // API::set_nocache( $reason = false ) -> action `litespeed_control_set_nocache`
add_action( 'litespeed_control_set_cacheable', __NAMESPACE__ . '\Control::set_cacheable' ); // API::set_cacheable( $reason ) -> action `litespeed_control_set_cacheable` // Might needed if not call hook `wp`
add_action( 'litespeed_control_force_cacheable', __NAMESPACE__ . '\Control::force_cacheable' ); // API::set_force_cacheable( $reason ) -> action `litespeed_control_force_cacheable` // Set cache status to force cacheable ( Will ignore most kinds of non-cacheable conditions )
add_action( 'litespeed_control_force_public', __NAMESPACE__ . '\Control::set_public_forced' ); // API::set_force_public( $reason ) -> action `litespeed_control_force_public` // Set cache to force public cache if cacheable ( Will ignore most kinds of non-cacheable conditions )
add_filter( 'litespeed_control_cacheable', __NAMESPACE__ . '\Control::is_cacheable', 3 ); // API::not_cacheable() -> filter `litespeed_control_cacheable` // Note: Read-Only. Directly append to this filter won't work. Call actions above to set cacheable or not
add_action( 'litespeed_control_set_ttl', __NAMESPACE__ . '\Control::set_custom_ttl', 10, 2 ); // API::set_ttl( $val ) -> action `litespeed_control_set_ttl`
add_filter( 'litespeed_control_ttl', __NAMESPACE__ . '\Control::get_ttl', 3 ); // API::get_ttl() -> filter `litespeed_control_ttl`
// API::hook_control($tags) && action `litespeed_api_control` -> Action `litespeed_control_finalize`
add_action( 'litespeed_control_set_private', __NAMESPACE__ . '\Control::set_private' ); // API::set_cache_private() -> Action `litespeed_control_set_private`
add_action( 'litespeed_control_set_nocache', __NAMESPACE__ . '\Control::set_nocache' ); // API::set_nocache( $reason = false ) -> Action `litespeed_control_set_nocache`
add_action( 'litespeed_control_set_cacheable', __NAMESPACE__ . '\Control::set_cacheable' ); // API::set_cacheable( $reason ) -> Action `litespeed_control_set_cacheable` // Might needed if not call hook `wp`
add_action( 'litespeed_control_force_cacheable', __NAMESPACE__ . '\Control::force_cacheable' ); // API::set_force_cacheable( $reason ) -> Action `litespeed_control_force_cacheable` // Set cache status to force cacheable ( Will ignore most kinds of non-cacheable conditions )
add_action( 'litespeed_control_force_public', __NAMESPACE__ . '\Control::set_public_forced' ); // API::set_force_public( $reason ) -> Action `litespeed_control_force_public` // Set cache to force public cache if cacheable ( Will ignore most kinds of non-cacheable conditions )
add_filter( 'litespeed_control_cacheable', __NAMESPACE__ . '\Control::is_cacheable', 3 ); // API::not_cacheable() -> Filter `litespeed_control_cacheable` // Note: Read-Only. Directly append to this filter won't work. Call actions above to set cacheable or not
add_action( 'litespeed_control_set_ttl', __NAMESPACE__ . '\Control::set_custom_ttl', 10, 2 ); // API::set_ttl( $val ) -> Action `litespeed_control_set_ttl`
add_filter( 'litespeed_control_ttl', __NAMESPACE__ . '\Control::get_ttl', 3 ); // API::get_ttl() -> Filter `litespeed_control_ttl`

/**
* Tag Hooks
*/
// API::hook_tag( $hook ) -> action `litespeed_tag_finalize`
add_action( 'litespeed_tag_add', __NAMESPACE__ . '\Tag::add' ); // API::tag_add( $tag ) -> action `litespeed_tag_add`
// API::hook_tag( $hook ) -> Action `litespeed_tag_finalize`
add_action( 'litespeed_tag_add', __NAMESPACE__ . '\Tag::add' ); // API::tag_add( $tag ) -> Action `litespeed_tag_add`
add_action( 'litespeed_tag_add_post', __NAMESPACE__ . '\Tag::add_post' );
add_action( 'litespeed_tag_add_widget', __NAMESPACE__ . '\Tag::add_widget' );
add_action( 'litespeed_tag_add_private', __NAMESPACE__ . '\Tag::add_private' ); // API::tag_add_private( $tags ) -> action `litespeed_tag_add_private`
add_action( 'litespeed_tag_add_private', __NAMESPACE__ . '\Tag::add_private' ); // API::tag_add_private( $tags ) -> Action `litespeed_tag_add_private`
add_action( 'litespeed_tag_add_private_esi', __NAMESPACE__ . '\Tag::add_private_esi' );

/**
* Purge Hooks
*/
// API::hook_purge($tags) -> action `litespeed_purge_finalize`
add_action( 'litespeed_purge', __NAMESPACE__ . '\Purge::add' ); // API::purge($tags) -> action `litespeed_purge`
// API::hook_purge($tags) -> Action `litespeed_purge_finalize`
add_action( 'litespeed_purge', __NAMESPACE__ . '\Purge::add' ); // API::purge($tags) -> Action `litespeed_purge`
add_action( 'litespeed_purge_all', __NAMESPACE__ . '\Purge::purge_all' );
add_action( 'litespeed_purge_post', __NAMESPACE__ . '\Purge::purge_post' ); // API::purge_post( $pid ) -> action `litespeed_purge_post`
add_action( 'litespeed_purge_post', __NAMESPACE__ . '\Purge::purge_post' ); // API::purge_post( $pid ) -> Action `litespeed_purge_post`
add_action( 'litespeed_purge_posttype', __NAMESPACE__ . '\Purge::purge_posttype' );
add_action( 'litespeed_purge_url', __NAMESPACE__ . '\Purge::purge_url' );
add_action( 'litespeed_purge_widget', __NAMESPACE__ . '\Purge::purge_widget' );
add_action( 'litespeed_purge_esi', __NAMESPACE__ . '\Purge::purge_esi' );
add_action( 'litespeed_purge_private', __NAMESPACE__ . '\Purge::add_private' ); // API::purge_private( $tags ) -> action `litespeed_purge_private`
add_action( 'litespeed_purge_private', __NAMESPACE__ . '\Purge::add_private' ); // API::purge_private( $tags ) -> Action `litespeed_purge_private`
add_action( 'litespeed_purge_private_esi', __NAMESPACE__ . '\Purge::add_private_esi' );
add_action( 'litespeed_purge_private_all', __NAMESPACE__ . '\Purge::add_private_all' ); // API::purge_private_all() -> action `litespeed_purge_private_all`
add_action( 'litespeed_purge_private_all', __NAMESPACE__ . '\Purge::add_private_all' ); // API::purge_private_all() -> Action `litespeed_purge_private_all`

/**
* ESI
*/
// API::nonce_action( $action ) & API::nonce( $action = -1, $defence_for_html_filter = true ) -> action `litespeed_nonce`
// API::nonce_action( $action ) & API::nonce( $action = -1, $defence_for_html_filter = true ) -> Action `litespeed_nonce`

add_filter( 'litespeed_is_mobile', wp_is_mobile() ? '__return_true' : '__return_false' ); // API::set_mobile() -> filter `litespeed_is_mobile`

/**
* Vary
*
* To modify default vary, There are two ways: Action `litespeed_vary_append` or Filter `litespeed_vary`
*/
add_action( 'litespeed_vary_ajax_force', __NAMESPACE__ . '\Vary::can_ajax_vary' ); // API::force_vary() -> Action `litespeed_vary_ajax_force` // Force finalize vary even if its in an AJAX call
add_action( 'litespeed_vary_append', __NAMESPACE__ . '\Vary::append', 10, 2 ); // API::vary( $k, $v, $default = null ) -> Action `litespeed_vary_append // Alter default vary cookie value // Default vary cookie is an array before finalization, after that it will be combined to a string and store as default vary cookie name
// API::hook_vary_finalize( $hook ) -> Filter `litespeed_vary`

add_filter( 'litespeed_is_mobile', wp_is_mobile() ? '__return_true' : '__return_false' ); // API::set_mobile() -> Filter `litespeed_is_mobile`

/**
* Mist
*/
add_action( 'litespeed_debug', __NAMESPACE__ . '\Debug2::debug' ); // API::debug()-> action `litespeed_debug`
add_action( 'litespeed_debug2', __NAMESPACE__ . '\Debug2::debug2' ); // API::debug2()-> action `litespeed_debug2`

}

/**
Expand Down Expand Up @@ -218,44 +232,6 @@ public static function filter_vary_cookies( $hook, $priority = 10 )
add_filter( 'litespeed_vary_cookies', $hook, $priority ) ;
}

/**
* Alter default vary cookie value
*
* Default vary cookie is an array before finalization, after that it will be combined to a string and store as default vary cookie name
*
* @since 2.6
* @access public
*/
public static function vary( $k, $v, $default = null )
{
if ( $v === $default ) {
return ;
}
Vary::append( $k, $v ) ;
}

/**
* Hook vary tags to default vary finialization
*
* @since 1.7.2
* @access public
*/
public static function hook_vary_finalize( $hook )
{
add_filter( 'litespeed_vary', $hook ) ;
}

/**
* Force finalize vary even if its in an AJAX call
*
* @since 2.6
* @access public
*/
public static function force_vary()
{
Vary::can_ajax_vary() ;
}

/**
* Hook vary appending to vary
*
Expand Down
10 changes: 5 additions & 5 deletions src/control.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ protected function __construct()
* @since 1.6.2
* @access public
*/
public function vary_add_role_exclude( $varies )
public function vary_add_role_exclude( $vary )
{
if ( ! $this->in_cache_exc_roles() ) {
return $varies ;
if ( $this->in_cache_exc_roles() ) {
$vary[ 'role_exclude_cache' ] = 1;
}
$varies[ 'role_exclude_cache' ] = 1 ;
return $varies ;

return $vary;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/optimize.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function __construct()
* Add vary filter for Role Excludes
* @since 1.6
*/
add_filter( 'litespeed_vary', array( $this, 'vary_add_role_exclude' ) ) ;
add_filter( 'litespeed_vary', array( $this, 'vary_add_role_exclude' ) );

/**
* Prefetch DNS
Expand All @@ -106,13 +106,13 @@ protected function __construct()
* @since 1.6
* @access public
*/
public function vary_add_role_exclude( $varys )
public function vary_add_role_exclude( $vary )
{
if ( ! Conf::get_instance()->in_optm_exc_roles() ) {
return $varys ;
if ( Conf::get_instance()->in_optm_exc_roles() ) {
$vary[ 'role_exclude_optm' ] = 1;
}
$varys[ 'role_exclude_optm' ] = 1 ;
return $varys ;

return $vary;
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/vary.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Vary extends Instance
private static $_vary_name = '_lscache_vary' ; // this default vary cookie is used for logged in status check
private static $_vary_cookies = array() ; // vary header only!
private static $_default_vary_val = array() ;
private static $_can_change_vary = false; // Currently only AJAX used this

/**
* Adds the actions used for setting up cookies on log in/out.
Expand Down Expand Up @@ -240,8 +241,8 @@ public function remove_logged_in()
*/
public static function can_ajax_vary()
{
Debug2::debug( '[Vary] litespeed_ajax_vary -> true' ) ;
add_filter( 'litespeed_ajax_vary', '__return_true' ) ;
Debug2::debug( '[Vary] _can_change_vary -> true' ) ;
self::$_can_change_vary = true;
}

/**
Expand All @@ -253,13 +254,11 @@ public static function can_ajax_vary()
private function can_change_vary()
{
// Don't change for ajax due to ajax not sending webp header
/**
* Added `litespeed_ajax_vary` hook for 3rd party to set vary when doing ajax call ( Login With Ajax )
* @since 1.6.6
*/
if ( Router::is_ajax() && ! apply_filters( 'litespeed_ajax_vary', false ) ) {
Debug2::debug( '[Vary] can_change_vary bypassed due to ajax call' ) ;
return false ;
if ( Router::is_ajax() ) {
if ( ! self::$_can_change_vary ) {
Debug2::debug( '[Vary] can_change_vary bypassed due to ajax call' ) ;
return false ;
}
}

/**
Expand Down Expand Up @@ -372,7 +371,7 @@ public function in_vary_group( $role )
*/
public function finalize_default_vary( $uid = false )
{
$vary = self::$_default_vary_val ;
$vary = self::$_default_vary_val;

if ( ! $uid ) {
$uid = Router::get_uid() ;
Expand Down Expand Up @@ -414,8 +413,9 @@ public function finalize_default_vary( $uid = false )
* Add filter
* @since 1.6 Added for Role Excludes for optimization cls
* @since 1.6.2 Hooked to webp
* @since 3.0 Used by 3rd hooks too
*/
$vary = apply_filters( 'litespeed_vary', $vary ) ;
$vary = apply_filters( 'litespeed_vary', $vary );

if ( ! $vary ) {
return false ;
Expand Down Expand Up @@ -625,7 +625,7 @@ public static function add( $vary )
*/
public static function append( $name, $val )
{
self::$_default_vary_val[ $name ] = $val ;
self::$_default_vary_val[ $name ] = $val;
}

/**
Expand Down
1 change: 1 addition & 0 deletions thirdparty/entry.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'Facetwp',
'Theme_My_Login',
'User_Switching',
'WCML',
'WooCommerce',
'Wp_Polls',
'WP_PostRatings',
Expand Down
25 changes: 14 additions & 11 deletions thirdparty/wcml.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ class WCML

public static function detect()
{
if ( ! defined( 'WPML_PLUGIN_BASENAME' ) ) return;
if ( ! defined( 'WCML_VERSION' ) ) return;

add_filter( 'litespeed_internal_domains', __CLASS__ . '::append_domains' );
add_filter( 'wcml_client_currency', __CLASS__ . '::apply_client_currency' );
add_action( 'wcml_set_client_currency', __CLASS__ . '::set_client_currency' );
}

/**
* Take language domains as internal domains
*/
public static function append_domains( $domains )
public static function set_client_currency( $currency )
{
$wpml_domains = apply_filters( 'wpml_setting', false, 'language_domains' );
if ( $wpml_domains ) {
$domains = array_merge( $domains, array_values( $wpml_domains ) );
}
self::apply_client_currency( $currency );

return $domains;
do_action( 'litespeed_vary_ajax_force' );
}

public static function apply_client_currency( $currency )
{
if ( $currency !== wcml_get_woocommerce_currency_option() ) {
do_action( 'litespeed_vary_append', 'wcml_currency', $currency );
}

return $currency;
}
}
9 changes: 5 additions & 4 deletions thirdparty/woocommerce.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function add_hooks()
* @since 1.7.2
*/
add_action( 'template_include', array( $this, 'check_if_need_esi' ) ) ;
API::hook_vary_finalize( array( $this, 'vary_maintain' ) ) ;
add_filter( 'litespeed_vary', array( $this, 'vary_maintain' ) );

}

Expand Down Expand Up @@ -180,10 +180,11 @@ public function check_if_need_esi( $template )
public function vary_maintain( $vary )
{
if ( $this->vary_needed() ) {
do_action( 'litespeed_debug', 'API: 3rd woo added vary due to cart not empty' ) ;
$vary[ 'woo_cart' ] = 1 ;
do_action( 'litespeed_debug', 'API: 3rd woo added vary due to cart not empty' );
$vary[ 'woo_cart' ] = 1;
}
return $vary ;

return $vary;
}

/**
Expand Down

0 comments on commit 746bf3a

Please sign in to comment.