/** * * @param type $oObj * @param type $iItemid * @param type $sCss * @return type */ public function getAdminLinks($oObj, $iItemid, $sCss = '') { if (empty($this->links)) { $hash = $iItemid . $this->params->get('pro_searchBody', 0) . $this->params->get('pro_cookielessdomain', 0); $sId = md5('getAdminLinks' . JCH_VERSION . serialize($hash)); $aFunction = array($this, 'generateAdminLinks'); $aArgs = array($oObj, $sCss); $iLifeTime = (int) $this->params->get('lifetime', '30') * 24 * 60 * 60; $this->links = JchPlatformCache::getCallbackCache($sId, $iLifeTime, $aFunction, $aArgs); } return $this->links; }
/** * * @param type $oObj * @param type $iItemid * @param type $sCss * @return type */ public function getAdminLinks($oObj, $iItemid, $sCss = '') { JCH_DEBUG ? JchPlatformProfiler::mark('beforeGetAdminLinks plgSystem (JCH Optimize)') : null; if (!defined('JCH_VERSION')) { define('JCH_VERSION', '4.2.0'); } $sId = md5('getAdminLinks' . JCH_VERSION . serialize($iItemid . $this->params->get('pro_searchBody'))); $aFunction = array($this, 'generateAdminLinks'); $aArgs = array($oObj, $sCss); $iLifeTime = (int) $this->params->get('lifetime', '30') * 24 * 60 * 60; $aLinks = JchPlatformCache::getCallbackCache($sId, $iLifeTime, $aFunction, $aArgs); JCH_DEBUG ? JchPlatformProfiler::mark('afterGetAdminLinks plgSystem (JCH Optimize)') : null; return $aLinks; }
/** * * @param type $sContent * @param type $iLifetime * @return type */ public static function getCachedFile($sContent, $iLifetime) { $sContent = preg_replace_callback('#\\[\\[JCH_([^\\]]++)\\]\\]#', function ($aM) use($iLifetime) { return JchPlatformCache::getCache($aM[1], $iLifetime); }, $sContent); return $sContent; }
/** * Create and cache aggregated file if it doesn't exists. * * @param array $aFunction Name of function used to aggregate files * @param array $aArgs Arguments used by function above * @param string $sId Generated id to identify cached file * @return boolean True on success */ public function loadCache($aFunction, $aArgs, $sId) { $iLifeTime = (int) $this->params->get('cache_lifetime', '1') * 24 * 60 * 60; $bCached = JchPlatformCache::getCallbackCache($sId, $iLifeTime, $aFunction, $aArgs); return $bCached; }
/** * Aggregate contents of CSS and JS files * * @param array $aUrlArray Array of links of files * @param string $sType CSS or js * @return string Aggregarted contents * @throws Exception */ public function combineFiles($aUrlArray, $sType, $oCssParser) { $sContents = ''; $sLifetime = (int) $this->params->get('lifetime', '30') * 24 * 60 * 60; $this->bAsync = FALSE; $this->sAsyncUrl = ''; $oFileRetriever = JchOptimizeFileRetriever::getInstance(); foreach ($aUrlArray as $aUrl) { $sUrl = $this->prepareFileUrl($aUrl, $sType); JCH_DEBUG ? JchPlatformProfiler::start('CombineFile - ' . $sUrl) : null; if ($sType == 'js') { $sJsContents = $this->handleAsyncUrls($aUrl); if ($sJsContents === FALSE) { continue; } $sContents .= $sJsContents; unset($sJsContents); } if (isset($aUrl['id']) && $aUrl['id'] != '') { $function = array($this, 'cacheContent'); $args = array($aUrl, $sType, $oFileRetriever, $oCssParser, TRUE); $sCachedContent = JchPlatformCache::getCallbackCache($aUrl['id'], $sLifetime, $function, $args); $this->{$sType} .= $sCachedContent; if (!isset($aUrl['url']) && $this->sAsyncUrl != '' && $sType == 'js') { $this->sAsyncUrl = ''; } if ($this->sAsyncUrl == '') { $sContents .= $this->addCommentedUrl($sType, $aUrl) . '[[JCH_' . $aUrl['id'] . ']]' . $this->sLnEnd . 'DELIMITER'; } } else { $sContent = $this->cacheContent($aUrl, $sType, $oFileRetriever, $oCssParser, FALSE); $sContents .= $this->addCommentedUrl($sType, $aUrl) . $sContent . '|"LINE_END"|'; } JCH_DEBUG ? JchPlatformProfiler::stop('CombineFile - ' . $sUrl, TRUE) : null; } if ($this->bAsync) { $sContents = $this->getLoadScript() . $sContents; if ($this->sAsyncUrl != '') { $sContents .= $this->addCommentedUrl('js', $this->sAsyncUrl) . 'loadScript("' . $this->sAsyncUrl . '", function(){}); DELIMITER'; $this->sAsyncUrl = ''; } } return $sContents; }
/** * * @param type $parent */ public function uninstall($parent) { jimport('joomla.filesystem.folder'); require_once JPATH_ROOT . '/plugins/system/jch_optimize/jchoptimize/loader.php'; $sprites = JPATH_ROOT . '/images/jch-optimize'; if (file_exists($sprites)) { JFolder::delete($sprites); } $oJchCache = JchPlatformCache::getCacheObject(); $oJchCache->clean('plg_jch_optimize'); $oJchCache->clean('page'); $htaccess = JPATH_ROOT . '/.htaccess'; if (file_exists($htaccess)) { $contents = file_get_contents($htaccess); $regex = '@\\n?## BEGIN EXPIRES CACHING - JCH OPTIMIZE ##.*?## END EXPIRES CACHING - JCH OPTIMIZE ##@s'; $clean_contents = preg_replace($regex, '', $contents); file_put_contents($htaccess, $clean_contents); } }
/** * Aggregate contents of CSS and JS files * * @param array $aUrlArray Array of links of files * @param string $sType CSS or js * @return string Aggregarted contents * @throws Exception */ public function combineFiles($aUrlArray, $sType, $oCssParser) { global $_PROFILER; JCH_DEBUG ? JchPlatformProfiler::mark('beforeCombineFiles - ' . $sType . ' plgSystem (JCH Optimize)') : null; $sContents = ''; $sLifetime = (int) $this->params->get('lifetime', '30') * 24 * 60 * 60; $this->bAsync = FALSE; $this->sAsyncUrl = ''; $this->{$sType} = ''; $oFileRetriever = JchOptimizeFileRetriever::getInstance(); foreach ($aUrlArray as $aUrl) { if ($sType == 'js') { $sJsContents = $this->handleAsyncUrls($aUrl); if ($sJsContents === FALSE) { continue; } $sContents .= $sJsContents; unset($sJsContents); } if (isset($aUrl['id']) && $aUrl['id'] != '') { $function = array($this, 'cacheContent'); $args = array($aUrl, $sType, $oFileRetriever, $oCssParser); $sCachedContent = JchPlatformCache::getCallbackCache($aUrl['id'], $sLifetime, $function, $args); $this->{$sType} .= $sCachedContent; if (!isset($aUrl['url']) && $this->sAsyncUrl != '' && $sType == 'js') { $this->sAsyncUrl = ''; } if ($this->sAsyncUrl == '') { $sContents .= $this->addCommentedUrl($sType, $aUrl) . '[[JCH_' . $aUrl['id'] . ']]' . 'DELIMITER'; } } else { $sContent = $this->cacheContent($aUrl, $sType, $oFileRetriever, $oCssParser); $sContents .= $this->addCommentedUrl($sType, $aUrl) . $sContent . 'DELIMITER'; } } if ($this->bAsync) { $sContents = $this->getLoadScript() . $sContents; if ($this->sAsyncUrl != '') { $sContents .= $this->addCommentedUrl('js', $this->sAsyncUrl) . 'loadScript("' . $this->sAsyncUrl . '", function(){}); DELIMITER'; $this->sAsyncUrl = ''; } } JCH_DEBUG ? JchPlatformProfiler::mark('afterCombineFiles - ' . $sType . ' plgSystem (JCH Optimize)') : null; return $sContents; }
/** * * @param type $path * @param type $filter * @param type $recurse * @param type $exclude * @return array */ public static function lsFiles($path, $filter = '.', $recurse = false, $exclude = array()) { $wp_filesystem = JchPlatformCache::getWpFileSystem(); $items = $wp_filesystem->dirlist($path, false, $recurse); $files = array(); self::filterItems($path, $filter, $items, $files); return $files; }
/** * * @param JchPlatformSettings $params */ public static function garbageCron(JchPlatformSettings $params) { $lifetime = (int) $params->get('cache_lifetime', '1') * 24 * 60 * 60; JchPlatformCache::gc($lifetime); }
/** * * @global type $wp_filesystem * @return type */ public static function getWpFileSystem() { if (!isset(self::$wp_filesystem)) { global $wp_filesystem; $wp_filesystem_cache = $wp_filesystem; if (!class_exists('WP_Filesystem_Base')) { include_once ABSPATH . 'wp-admin/includes/file.php'; } add_filter('request_filesystem_credentials', array('JchPlatformCache', 'requestFilesystemCredentials'), 10, 7); if (false === ($creds = request_filesystem_credentials(admin_url('options-general.php?page=jchoptimize-settings'), '', false, WP_CONTENT_DIR, null, true))) { $message = 'The plugin needs to access the filesystem via FTP. Please define your FTP credentials in your wp_config.php file with FTP_HOST, FTP_USER, FTP_PASS, etc.'; if (function_exists('jch_add_notices')) { jch_add_notices('error', __($message, 'jch-optimize')); return self::$wp_filesystem = false; } else { throw new Exception(__($message)); } } if (false === WP_Filesystem($creds, WP_CONTENT_DIR, true)) { $message = 'Could not connect to the filesystem. Please check your FTP credentials in wp_config.php file'; if (function_exists('jch_add_notices')) { jch_add_notices('error', __($message, 'jch-optimize')); return self::$wp_filesystem = false; } else { throw new Exception(__($message)); } } self::$wp_filesystem = $wp_filesystem; if (!defined('JCH_CACHE_DIR')) { define('JCH_CACHE_DIR', $wp_filesystem->wp_content_dir() . 'cache/jch-optimize/'); } $wp_filesystem = $wp_filesystem_cache; } return self::$wp_filesystem; }
/** * Create and cache aggregated file if it doesn't exists, file will have * lifetime set in global configurations. * * @param array $aFunction Name of function used to aggregate files * @param array $aArgs Arguments used by function above * @param string $sId Generated id to identify cached file * @return boolean True on success */ public function loadCache($aFunction, $aArgs, $sId) { JCH_DEBUG ? JchPlatformProfiler::mark('beforeLoadCache plgSystem (JCH Optimize)') : null; $iLifeTime = (int) $this->params->get('lifetime', '30') * 24 * 60 * 60; $bCached = JchPlatformCache::getCallbackCache($sId, $iLifeTime, $aFunction, $aArgs); JCH_DEBUG ? JchPlatformProfiler::mark('afterLoadCache plgSystem (JCH Optimize)') : null; return $bCached; }
function jch_optimize_uninstall() { delete_option('jch_options'); JchPlatformCache::deleteCache(); }