Skip to content

Commit

Permalink
[IMPROVEMENT] Move optimizer data from wp_options to separate table t…
Browse files Browse the repository at this point in the history
…o improve backup plugin's compatibility
  • Loading branch information
hi-hai committed Oct 5, 2017
1 parent ca1efe2 commit 5f26e4b
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 15 deletions.
177 changes: 177 additions & 0 deletions litespeed-cache/inc/data.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

/**
* The class to store and manage litespeed db data.
*
* @since 1.3.1
* @package LiteSpeed_Cache
* @subpackage LiteSpeed_Cache/includes
* @author LiteSpeed Technologies <[email protected]>
*/

class LiteSpeed_Cache_Data
{
private static $_instance ;

const TB_OPTIMIZER = 'litespeed_optimizer' ;

private $_charset_collate ;
private $_tb_optm ;

/**
* Init
*
* @since 1.3.1
* @access private
*/
private function __construct()
{
LiteSpeed_Cache_Log::debug2( 'Data init' ) ;
global $wpdb ;

$this->_charset_collate = $wpdb->get_charset_collate() ;

$this->_tb_optm = $wpdb->base_prefix . self::TB_OPTIMIZER ;

$this->_optm_sync() ;
}

/**
* save optimizer src to db
*
* @since 1.3.1
* @access public
*/
public static function optm_save_src( $filename, $src )
{
$instance = self::get_instance() ;
return $instance->_optm_save_src( $filename, $src ) ;
}
private function _optm_save_src( $filename, $src )
{
global $wpdb ;

$src = serialize( $src ) ;
$f = array(
'hash_name' => $filename,
'src' => $src,
'dateline' => time(),
'refer' => ! empty( $SERVER[ 'SCRIPT_URI' ] ) ? $SERVER[ 'SCRIPT_URI' ] : '',
) ;

$res = $wpdb->replace( $this->_tb_optm, $f ) ;

return $res ;
}

/**
* Get src set from hash in optimizer
*
* @since 1.3.1
* @access public
*/
public static function optm_hash2src( $filename )
{
$instance = self::get_instance() ;
return $instance->_optm_hash2src( $filename ) ;
}
private function _optm_hash2src( $filename )
{
global $wpdb ;

$s = $wpdb->prepare( 'SELECT src FROM `' . $this->_tb_optm . '` WHERE `hash_name` = %s', $filename ) ;

$res = $wpdb->get_var( $s ) ;

LiteSpeed_Cache_Log::debug2( 'Data: Loaded hash2src ' . $res ) ;

$res = unserialize( $res ) ;

return $res ;
}

/**
* Create optimizer table
*
* @since 1.3.1
* @access private
*/
private function _optm_sync()
{
if ( defined( 'LITESPEED_DID_' . __FUNCTION__ ) ) {
return ;
}
define( 'LITESPEED_DID_' . __FUNCTION__, true ) ;

// if ( get_option( $this->_tb_optm ) ) {
// return ;
// }

global $wpdb ;

LiteSpeed_Cache_Log::debug2( 'Data: Checking optm table' ) ;

// Check if table exists first
$res = $wpdb->get_var( "SHOW TABLES LIKE '$this->_tb_optm'" ) ;
if ( $res ) {
LiteSpeed_Cache_Log::debug2( 'Data: Existed' ) ;

return ;
}

LiteSpeed_Cache_Log::debug( 'Data: Creating optm table' ) ;

$s = sprintf(
'CREATE TABLE IF NOT EXISTS `%1$s` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`hash_name` varchar(255) NOT NULL COMMENT "hash.filetype",
`src` text NOT NULL COMMENT "full url array set",
`dateline` int(11) NOT NULL,
`refer` varchar(255) NOT NULL COMMENT "The container page url",
PRIMARY KEY (`id`),
UNIQUE KEY `hash_name` (`hash_name`),
KEY `dateline` (`dateline`)
) %2$s;',
$this->_tb_optm,
$this->_charset_collate
) ;

$wpdb->query( $s ) ;

// Move data from wp_options to here
$hashes = get_option( 'litespeed-cache-optimized' ) ;
if ( $hashes ) {
foreach ( $hashes as $k => $v ) {
$f = array(
'hash_name' => $k,
'src' => serialize( $v ),
'dateline' => time(),
'refer' => '',
) ;
$wpdb->replace( $this->_tb_optm, $f ) ;
}
}
delete_option( 'litespeed-cache-optimized' ) ;

// Record tb version
update_option( $this->_tb_optm, LiteSpeed_Cache::PLUGIN_VERSION ) ;

}

/**
* Get the current instance object.
*
* @since 1.3.1
* @access public
* @return Current class instance.
*/
public static function get_instance()
{
if ( ! isset( self::$_instance ) ) {
self::$_instance = new self() ;
}

return self::$_instance ;
}

}
2 changes: 1 addition & 1 deletion litespeed-cache/includes/litespeed-cache-gui.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function frontend_shortcut()
*/
public static function get_instance()
{
if ( ! isset(self::$_instance) ) {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new self() ;
}

Expand Down
20 changes: 6 additions & 14 deletions litespeed-cache/includes/litespeed-cache-optimize.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class LiteSpeed_Cache_Optimize
{
private static $_instance ;

const OPTION_OPTIMIZED = 'litespeed-cache-optimized' ;
const DIR_MIN = '/min' ;
const CSS_ASYNC_LIB = '/min/css_async.js' ;

Expand Down Expand Up @@ -673,12 +672,11 @@ private function _is_file_url( $url )
private function _minify( $filename )
{
// Search filename in db for src URLs
$hashes = get_option( self::OPTION_OPTIMIZED ) ;
if ( ! $hashes || ! is_array( $hashes ) || empty( $hashes[ $filename ] ) ) {
$urls = LiteSpeed_Cache_Data::optm_hash2src( $filename ) ;
if ( ! $urls || ! is_array( $urls ) ) {
return false;
}

$urls = $hashes[ $filename ] ;
$file_type = substr( $filename, strrpos( $filename, '.' ) + 1 ) ;

// Parse real file path
Expand Down Expand Up @@ -733,23 +731,17 @@ private function _build_hash_url( $src, $file_type = 'css' )
$filename = $short ;

// Need to check conflicts
$hashes = get_option( self::OPTION_OPTIMIZED ) ;
if ( ! is_array( $hashes ) ) {
$hashes = array() ;
}
// If short hash exists
if ( $hashes && ! empty( $hashes[ $short . '.' . $file_type ] ) ) {
if ( $urls = LiteSpeed_Cache_Data::optm_hash2src( $short . '.' . $file_type ) ) {
// If conflicts
if ( $hashes[ $short . '.' . $file_type ] !== $src ) {
$hashes[ $hash . '.' . $file_type ] = $src ;
update_option( self::OPTION_OPTIMIZED, $hashes ) ;
if ( $urls !== $src ) {
LiteSpeed_Cache_Data::optm_save_src( $hash . '.' . $file_type, $src ) ;
$filename = $hash ;
}
}
else {
// Short hash is safe now
$hashes[ $short . '.' . $file_type ] = $src ;
update_option( self::OPTION_OPTIMIZED, $hashes ) ;
LiteSpeed_Cache_Data::optm_save_src( $short . '.' . $file_type, $src ) ;
}

$file_to_save = self::DIR_MIN . '/' . $filename . '.' . $file_type ;
Expand Down
1 change: 1 addition & 0 deletions litespeed-cache/includes/litespeed.autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function _litespeed_autoload($cls)
'LiteSpeed_Cache_Control' => 'includes/litespeed-cache-control.class.php',
'LiteSpeed_Cache_Crawler' => 'includes/litespeed-cache-crawler.class.php',
'LiteSpeed_Cache_Crawler_Sitemap' => 'includes/litespeed-cache-crawler-sitemap.class.php',
'LiteSpeed_Cache_Data' => 'inc/data.class.php',
'LiteSpeed_Cache_ESI' => 'includes/litespeed-cache-esi.class.php',
'LiteSpeed_Cache_GUI' => 'includes/litespeed-cache-gui.class.php',
'LiteSpeed_Cache_Log' => 'includes/litespeed-cache-log.class.php',
Expand Down

0 comments on commit 5f26e4b

Please sign in to comment.