/** * Static method to initialize the plugin * * @param type $params Plugin parameters */ public static function optimize($oParams, $sHtml) { if (version_compare(PHP_VERSION, '5.3.0', '<')) { throw new Exception('PHP Version less than 5.3.0. Exiting plugin...'); } $pcre_version = preg_replace('#(^\\d++\\.\\d++).++$#', '$1', PCRE_VERSION); if (version_compare($pcre_version, '7.2', '<')) { throw new Exception('PCRE Version less than 7.2. Exiting plugin...'); } $JchOptimize = new JchOptimize($oParams); return $JchOptimize->process($sHtml); }
/** * * @return boolean * @throws Exception */ public function onAfterRender() { $app = JFactory::getApplication(); $config = JFactory::getConfig(); $user = JFactory::getUser(); if ($app->getName() != 'site' || JFactory::getDocument()->getType() != 'html' || $app->input->get('jchbackend', '', 'int') == 1 || $config->get('offline') && $user->guest || $this->isEditorLoaded()) { return FALSE; } if ($this->params->get('log', 0)) { error_reporting(E_ALL & ~E_NOTICE); } $sHtml = $app->getBody(); if ($app->input->get('jchbackend') == '2') { echo $sHtml; while (@ob_end_flush()) { } exit; } if (!defined('JCH_VERSION')) { define('JCH_VERSION', '5.0.5'); } try { loadJchOptimizeClass('JchOptimize'); $sOptimizedHtml = JchOptimize::optimize($this->params, $sHtml); } catch (Exception $ex) { JchOptimizeLogger::log($ex->getMessage(), JchPlatformSettings::getInstance($this->params)); $sOptimizedHtml = $sHtml; } $app->setBody($sOptimizedHtml); }
function jchoptimize($sHtml) { $options = get_option('jch_options'); try { $sOptimizedHtml = JchOptimize::optimize($options, $sHtml); } catch (Exception $e) { JchOptimizeLogger::log($e->getMessage(), JchPlatformSettings::getInstance($options)); $sOptimizedHtml = $sHtml; } return $sOptimizedHtml; }
/** * * @return boolean * @throws Exception */ public function onAfterRender() { if (JDEBUG && $this->params->get('debug', 0)) { global $_PROFILER; $_PROFILER->mark('beforeOnAfterRender plgSystem (JCH Optimize)'); } if (JFactory::getApplication()->getName() != 'site' || JFactory::getDocument()->getType() != 'html' || JFactory::getApplication()->input->get('jchbackend', '', 'int') == 1 || JFactory::getConfig()->get('offline') && JFactory::getUser()->guest || $this->isEditorLoaded()) { return FALSE; } if ($this->params->get('log', 0)) { error_reporting(E_ALL & ~E_NOTICE); } if (version_compare(JVERSION, '3.2.3', '>=')) { $oApp = JFactory::getApplication(); $sHtml = $oApp->getBody(); } else { $sHtml = JResponse::getBody(); } if (JFactory::getApplication()->input->get('jchbackend') == '2') { echo $sHtml; while (@ob_end_flush()) { } exit; } try { if (version_compare(PHP_VERSION, '5.3.0', '<')) { throw new Exception(JText::_('PHP Version less than 5.3.0. Exiting plugin...')); } $sOptimizedHtml = JchOptimize::optimize($this->params, $sHtml); } catch (Exception $ex) { if ($this->params->get('log', 0)) { jimport('joomla.log.log'); JLog::addLogger(array('text_file' => 'plg_jch_optimize.errors.php'), JLog::ALL, 'jch-optimize'); JLog::add(JText::_($ex->getMessage()), JLog::WARNING, 'jch-optimize'); } $sOptimizedHtml = $sHtml; } if (version_compare(JVERSION, '3.2.3', '>=')) { $oApp->setBody($sOptimizedHtml); } else { JResponse::setBody($sOptimizedHtml); } if (JDEBUG && $this->params->get('debug', 0)) { $_PROFILER->mark('afterOnAfterRender plgSystem (JCH Optimize)'); } }
/** * * @return boolean * @throws Exception */ public function onAfterRender() { if (JDEBUG && $this->params->get('debug', 0)) { global $_PROFILER; $_PROFILER->mark('beforeOnAfterRender plgSystem (JCH Optimize)'); } $app = JFactory::getApplication(); $config = JFactory::getConfig(); $user = JFactory::getUser(); if ($app->getName() != 'site' || JFactory::getDocument()->getType() != 'html' || $app->input->get('jchbackend', '', 'int') == 1 || $config->get('offline') && $user->guest || class_exists('JEditor', FALSE)) { return FALSE; } if ($this->params->get('log', 0)) { error_reporting(E_ALL & ~E_NOTICE); } if (version_compare(JVERSION, '3.2.3', '>=')) { $sHtml = $app->getBody(); } else { $sHtml = JResponse::getBody(); } if ($app->input->get('jchbackend') == '2') { echo $sHtml; while (@ob_end_flush()) { } exit; } try { loadJchOptimizeClass('JchOptimize'); $sOptimizedHtml = JchOptimize::optimize($this->params, $sHtml); } catch (Exception $ex) { JchOptimizeLogger::log($ex->getMessage(), JchPlatformSettings::getInstance($this->params)); $sOptimizedHtml = $sHtml; } if (version_compare(JVERSION, '3.2.3', '>=')) { $app->setBody($sOptimizedHtml); } else { JResponse::setBody($sOptimizedHtml); } if (JDEBUG && $this->params->get('debug', 0)) { $_PROFILER->mark('afterOnAfterRender plgSystem (JCH Optimize)'); } }
/** * Uses regex to find css declarations containing background images to include in sprite * * @param string $sCss Aggregated css file * @return array Array of css declarations and image urls to replace with sprite */ public function processCssUrls($sCss, $bBackend = FALSE) { $params = $this->params; $aRegexStart = array(); $aRegexStart[0] = ' #(?:{ (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url\\( (?=[^)]+\\.(?:png|gif|jpe?g)) ([^)]+)\\))'; $aRegexStart[1] = ' (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?no-repeat)'; $aRegexStart[2] = ' (?(?=\\s*(?>[^};]++[;\\s]++)*?background(?:-position)?\\s*+:\\s*+(?:[^;}\\s]++[^}\\S]++)*? (?<p>(?:[tblrc](?:op|ottom|eft|ight|enter)|-?[0-9]+(?:%|[c-x]{2})?))(?:\\s+(?&p))?) (?=\\s*(?>[^};]++[;\\s]++)*?background(?:-position)?\\s*+:\\s*+(?>[^;}\\s]++[\\s]++)*? (?:left|top|0(?:%|[c-x]{2})?)\\s+(?:left|top|0(?:%|[c-x]{2})?) ) )'; $sRegexMiddle = ' [^{}]++} )'; $sRegexEnd = '#isx'; $aIncludeImages = JchOptimize::getArray($params->get('csg_include_images')); $aExcludeImages = JchOptimize::getArray($params->get('csg_exclude_images')); $sIncImagesRegex = ''; if (!empty($aIncludeImages[0])) { foreach ($aIncludeImages as &$sImage) { $sImage = preg_quote($sImage, '#'); } $sIncImagesRegex .= ' |(?:{ (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url'; $sIncImagesRegex .= ' (?=[^)]* [/(](?:' . implode('|', $aIncludeImages) . ')\\))'; $sIncImagesRegex .= '\\(([^)]+)\\) ) [^{}]++ })'; } $sExImagesRegex = ''; if (!empty($aExcludeImages[0])) { $sExImagesRegex .= '(?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url\\( [^)]++ (?<!'; foreach ($aExcludeImages as &$sImage) { $sImage = preg_quote($sImage, '#'); } $sExImagesRegex .= implode('|', $aExcludeImages) . ')\\) )'; } $sRegexStart = implode('', $aRegexStart); $sRegex = $sRegexStart . $sExImagesRegex . $sRegexMiddle . $sIncImagesRegex . $sRegexEnd; if (preg_match_all($sRegex, $sCss, $aMatches) === FALSE) { throw new Exception(JText::_('Error occurred matching for images to sprite')); } if (isset($aMatches[3])) { $aMatches[1] = array_merge(array_filter($aMatches[1]), array_filter($aMatches[3])); } if ($bBackend) { $aImages = array(); $aImagesMatches = array(); $aImgRegex = array(); $aImgRegex[0] = $aRegexStart[0]; $aImgRegex[2] = $aRegexStart[1]; $aImgRegex[4] = $sRegexMiddle; $aImgRegex[5] = $sRegexEnd; $sImgRegex = implode('', $aImgRegex); if (preg_match_all($sImgRegex, $sCss, $aImagesMatches) === FALSE) { throw new Exception(JText::_('Error occurred matching for images to include')); } $aImagesMatches[0] = array_diff($aImagesMatches[0], $aMatches[0]); $aImagesMatches[1] = array_diff($aImagesMatches[1], $aMatches[1]); $aImages['include'] = $aImagesMatches; $aImages['exclude'] = $aMatches; return $aImages; } return $aMatches; }
/** * Uses regex to find css declarations containing background images to include in sprite * * @param string $sCss Aggregated css file * @return array Array of css declarations and image urls to replace with sprite */ public function processCssUrls($sCss, $bBackend = FALSE) { //JCH_DEBUG ? JchPlatformProfiler::mark('beforeProcessCssUrls plgSystem (JCH Optimize)') : null; $params = $this->params; $aRegexStart = array(); $aRegexStart[0] = ' #(?:{ (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url\\( (?=[^)]+\\.(?:png|gif|jpe?g)) ([^)]+)\\))'; $aRegexStart[1] = ' (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?no-repeat)'; $aRegexStart[2] = ' (?(?=\\s*(?>[^};]++[;\\s]++)*?background(?:-position)?\\s*+:\\s*+(?:[^;}\\s]++[^}\\S]++)*? (?<p>(?:[tblrc](?:op|ottom|eft|ight|enter)|-?[0-9]+(?:%|[c-x]{2})?))(?:\\s+(?&p))?) (?=\\s*(?>[^};]++[;\\s]++)*?background(?:-position)?\\s*+:\\s*+(?>[^;}\\s]++[\\s]++)*? (?:left|top|0(?:%|[c-x]{2})?)\\s+(?:left|top|0(?:%|[c-x]{2})?) ) )'; $sRegexMiddle = ' [^{}]++} )'; $sRegexEnd = '#isx'; $aIncludeImages = JchOptimize::getArray($params->get('csg_include_images')); $aExcludeImages = JchOptimize::getArray($params->get('csg_exclude_images')); $sIncImagesRegex = ''; if (!empty($aIncludeImages[0])) { foreach ($aIncludeImages as &$sImage) { $sImage = preg_quote($sImage, '#'); } $sIncImagesRegex .= ' |(?:{ (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url'; $sIncImagesRegex .= ' (?=[^)]* [/(](?:' . implode('|', $aIncludeImages) . ')\\))'; $sIncImagesRegex .= '\\(([^)]+)\\) ) [^{}]++ })'; } $sExImagesRegex = ''; if (!empty($aExcludeImages[0])) { $sExImagesRegex .= '(?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url\\( [^)]++ (?<!'; foreach ($aExcludeImages as &$sImage) { $sImage = preg_quote($sImage, '#'); } $sExImagesRegex .= implode('|', $aExcludeImages) . ')\\) )'; } $sRegexStart = implode('', $aRegexStart); $sRegex = $sRegexStart . $sExImagesRegex . $sRegexMiddle . $sIncImagesRegex . $sRegexEnd; if (preg_match_all($sRegex, $sCss, $aMatches) === FALSE) { throw new Exception(JchPlatformUtility::translate('Error occurred matching for images to sprite')); } if (isset($aMatches[3])) { $total = count($aMatches[1]); for ($i = 0; $i < $total; $i++) { $aMatches[1][$i] = trim($aMatches[1][$i]) ? $aMatches[1][$i] : $aMatches[3][$i]; } } if ($bBackend) { $aImages = array(); $aImagesMatches = array(); $aImgRegex = array(); $aImgRegex[0] = $aRegexStart[0]; $aImgRegex[2] = $aRegexStart[1]; $aImgRegex[4] = $sRegexMiddle; $aImgRegex[5] = $sRegexEnd; $sImgRegex = implode('', $aImgRegex); if (preg_match_all($sImgRegex, $sCss, $aImagesMatches) === FALSE) { throw new Exception(JchPlatformUtility::translate('Error occurred matching for images to include')); } $aImagesMatches[0] = array_diff($aImagesMatches[0], $aMatches[0]); $aImagesMatches[1] = array_diff($aImagesMatches[1], $aMatches[1]); $oCssSpriteGen = new CssSpriteGen($this->getImageLibrary(), $this->params, $bBackend); $aImages['include'] = $oCssSpriteGen->CreateSprite($aImagesMatches[1]); $aImages['exclude'] = $oCssSpriteGen->CreateSprite($aMatches[1]); return $aImages; } //JCH_DEBUG ? JchPlatformProfiler::mark('afterProcessCssUrls plgSystem (JCH Optimize)') : null; return $aMatches; }
/** * Generates regex for excluding components set in plugin params * * @param string $param * @return string */ protected function getExCompRegex($sExComParam) { $aComponents = array_filter(JchOptimize::getArray($sExComParam)); $aExComp = array(); if (!empty($aComponents)) { $aExComp = array_map(function ($sValue) { return $sValue . '/'; }, $aComponents); } return $aExComp; }
/** * Static method to initialize the plugin * * @param type $params Plugin parameters */ public static function optimize($oParams, $sHtml) { $JchOptimize = new JchOptimize($oParams); return $JchOptimize->process($sHtml); }
/** * Static method to initialize the plugin * * @param type $params Plugin parameters */ public static function optimize() { $JchOptimize = new JchOptimize(); $JchOptimize->process(); }