Exemple #1
0
 /**
  * Render conditional css link
  *
  * @param array $cond_css
  *   Conditional css list that each element contain 2 sub-elements:
  *   - URL link
  *   - Attributes of css link, it is associate array with 3 keys
  *     + mine:  link type attribute
  *     + media: link media attibute
  *     + attribs: other attributes
  *
  * @return string
  */
 function renderConditionalCSS($condCss)
 {
     $data = "\n";
     $urlPath = new T3Path();
     foreach ($condCss as $css) {
         // Get url path & link attributes
         $url = preg_replace('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', '', $css['url']);
         $paths = $urlPath->get($url, true);
         if ($paths) {
             $media = $css['media'];
             $cond = $css['condition'];
             $type = $css['type'];
             // Generate links
             $paths = array_reverse($paths);
             foreach ($paths as $path) {
                 $data .= "<!--[if {$cond}]><link href=\"{$path[1]}\" type=\"{$type}\" rel=\"stylesheet\" ";
                 if (!empty($media)) {
                     $data .= "media=\"{$media}\" ";
                 }
                 $data .= "/><![endif]--> \n";
                 // Check and add rtl file
                 $rtl_path = str_replace('.css', '-rtl.css', $path[0]);
                 if (T3Common::isRTL() && is_file($rtl_path)) {
                     $rtl_path = str_replace('.css', '-rtl.css', $path[1]);
                     $data .= "<!--[if {$cond}]><link href=\"{$rtl_path}\" type=\"{$type}\" rel=\"stylesheet\" ";
                     if (!empty($media)) {
                         $data .= "media=\"{$media}\" ";
                     }
                     $data .= "/><![endif]--> \n";
                 }
             }
         }
     }
     return $data;
 }
Exemple #2
0
 /**
  * Execute css/js optimizing base on joomla document object
  *
  * @return void
  */
 public static function proccess()
 {
     $document = JFactory::getDocument();
     $urlPath = new T3Path();
     //proccess stylesheets
     $themes = T3Common::get_active_themes();
     //$themes[] = array('core', 'default'); //default now move to template root folder
     //$themes[] = array('engine', 'default');
     $themes = array_reverse($themes);
     $scripts = array();
     $css_urls = array();
     $css_urls['site'] = array();
     foreach ($themes as $theme) {
         $css_urls[$theme[0] . '.' . $theme[1]] = array();
     }
     foreach ($themes as $theme) {
         $css_urls[$theme[0] . '.' . $theme[1] . '-browser'] = array();
     }
     if (T3Common::isRTL()) {
         foreach ($themes as $theme) {
             $css_urls[$theme[0] . '.' . $theme[1] . '-rtl'] = array();
         }
     }
     //$bname = T3Common::getBrowserSortName();
     //$bver = T3Common::getBrowserMajorVersion();
     $optimize_css = T3Parameter::_getParam('optimize_css', 2);
     $optimize_js = T3Parameter::_getParam('optimize_js', 2);
     foreach ($document->_styleSheets as $strSrc => $strAttr) {
         $path = T3Head::cleanUrl($strSrc);
         if (!$path || !preg_match('#\\.css$#', $path)) {
             //External url
             $css_urls['site'][] = array('', $strSrc);
             continue;
         }
         $intemplate = false;
         if (preg_match('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', $path)) {
             $path = preg_replace('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', '', $path);
             $intemplate = true;
         }
         $paths = array();
         //$paths[] = array ('', $path, $strSrc); //The third element is the original url
         $paths[] = array('', $path);
         // Fix when source code in subfolder
         //if ($intemplate) {
         //only load other css files if in T3v2 template
         $ext = '';
         if (preg_match('#\\.[^.]+$#', $path, $matches)) {
             $ext = $matches[0];
         }
         //if ($ext) {
         //$paths[] = array('-browser', str_replace($ext, "-$bname$ext", $path));
         //$paths[] = array('-browser', str_replace($ext, "-$bname$bver$ext", $path));
         //if (T3Common::isRTL()) {
         //$paths[] = array('-rtl', str_replace($ext, "-rtl$ext", $path));
         //$paths[] = array('-rtl', str_replace($ext, "-$bname-rtl$ext", $path));
         //$paths[] = array('-rtl', str_replace($ext, "-$bname$bver-rtl$ext", $path));
         //}
         //}
         if ($ext && T3Common::isRTL()) {
             $paths[] = array('-rtl', str_replace($ext, "-rtl{$ext}", $path));
         }
         //}
         foreach ($paths as $path) {
             //
             if ($intemplate) {
                 $urls = $urlPath->get($path[1], true);
                 if ($urls) {
                     foreach ($urls as $theme => $url) {
                         $url[] = $strAttr;
                         $css_urls[$theme . $path[0]][$url[0]] = $url;
                     }
                 }
             } else {
                 if (is_file(T3Path::path($path[1]))) {
                     $css_urls['site'][T3Path::path($path[1])] = array(T3Path::path($path[1]), count($path) > 2 ? $path[2] : T3Path::url($path[1]), $strAttr);
                     //use original url ($path[2]) if exists
                 }
             }
         }
     }
     // Remove current stylesheets
     $document->_styleSheets = array();
     foreach ($document->_scripts as $strSrc => $strType) {
         $srcurl = T3Head::cleanUrl($strSrc);
         if (!$srcurl || !preg_match('#\\.js$#', $srcurl)) {
             $scrips[] = array('', $strSrc);
             continue;
         }
         if (preg_match('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', $srcurl)) {
             //in template
             $srcurl = preg_replace('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', '', $srcurl);
             $path = str_replace('/', DS, $srcurl);
             $url = $urlPath->get($path);
             if ($url) {
                 $scrips[] = $url;
             }
         } else {
             // Don't read file content => keep original link
             if ($optimize_js < 1) {
                 $scrips[] = array('', $strSrc);
                 continue;
             }
             $path = str_replace('/', DS, $srcurl);
             $scrips[] = array(JPATH_SITE . DS . $path, JURI::base(true) . '/' . $srcurl);
         }
     }
     //remove current scripts
     $document->_scripts = array();
     $tmp_css_urls = false;
     do {
         $tmp_css_urls = T3Head::optimizecss($css_urls);
     } while ($tmp_css_urls === false);
     $css_urls = $tmp_css_urls;
     //if ($url) $css_urls = array(array(array('', $url)));
     //re-add stylesheet to head
     foreach ($css_urls as $urls) {
         foreach ($urls as $url) {
             if (count($url) > 2) {
                 $attrs = $url[2];
                 $document->addStylesheet($url[1], $attrs['mime'], $attrs['media'], $attrs['attribs']);
             } else {
                 $document->addStylesheet($url[1]);
             }
         }
     }
     $tmp_scrips = false;
     do {
         $tmp_scrips = T3Head::optimizejs($scrips);
     } while ($tmp_scrips === false);
     $scrips = $tmp_scrips;
     //re-add stylesheet to head
     foreach ($scrips as $url) {
         $document->addScript($url[1]);
     }
 }
Exemple #3
0
 function proccess()
 {
     $document =& JFactory::getDocument();
     //proccess stylesheets
     $themes = T3Common::get_active_themes();
     //$themes[] = array('core', 'default'); //default now move to template root folder
     //$themes[] = array('engine', 'default');
     $themes = array_reverse($themes);
     $scripts = array();
     $css_urls = array();
     $css_urls['site'] = array();
     foreach ($themes as $theme) {
         $css_urls[$theme[0] . '.' . $theme[1]] = array();
     }
     foreach ($themes as $theme) {
         $css_urls[$theme[0] . '.' . $theme[1] . '-browser'] = array();
     }
     if (T3Common::isRTL()) {
         foreach ($themes as $theme) {
             $css_urls[$theme[0] . '.' . $theme[1] . '-rtl'] = array();
         }
     }
     $bname = T3Common::getBrowserSortName();
     $bver = T3Common::getBrowserMajorVersion();
     $optimize_css = T3Parameter::get('optimize_css', 2);
     $optimize_js = T3Parameter::get('optimize_js', 2);
     foreach ($document->_styleSheets as $strSrc => $strAttr) {
         $path = T3Head::cleanUrl($strSrc);
         if (!$path) {
             //External url
             $css_urls['site'][] = array('', $strSrc);
             continue;
         }
         $intemplate = false;
         if (preg_match('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', $path)) {
             $path = preg_replace('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', '', $path);
             $intemplate = true;
         }
         /*
         if (!$intemplate && $optimize_css < 2) // don't read file content => keep original link
         {
         	$css_urls['site'][] = array ('', $strSrc);
         	continue;
         }
         */
         $paths = array();
         $paths[] = array('', $path, $strSrc);
         //The third element is the original url
         //if ($intemplate) {
         //only load other css files if in T3v2 template
         $ext = '';
         if (preg_match('#\\.[^.]+$#', $path, $matches)) {
             $ext = $matches[0];
         }
         //$file_info = pathinfo($path);
         //$ext = $file_info['extension'];
         if ($ext) {
             $paths[] = array('-browser', str_replace($ext, "-{$bname}{$ext}", $path));
             $paths[] = array('-browser', str_replace($ext, "-{$bname}{$bver}{$ext}", $path));
             if (T3Common::isRTL()) {
                 $paths[] = array('-rtl', str_replace($ext, "-rtl{$ext}", $path));
                 $paths[] = array('-rtl', str_replace($ext, "-{$bname}-rtl{$ext}", $path));
                 $paths[] = array('-rtl', str_replace($ext, "-{$bname}{$bver}-rtl{$ext}", $path));
             }
         }
         //}
         foreach ($paths as $path) {
             //
             if ($intemplate) {
                 if ($urls = T3Path::get($path[1], true)) {
                     foreach ($urls as $theme => $url) {
                         $css_urls[$theme . $path[0]][$url[0]] = $url;
                     }
                 }
             } else {
                 if (is_file(T3Path::path($path[1]))) {
                     $css_urls['site'][T3Path::path($path[1])] = array(T3Path::path($path[1]), count($path) > 2 ? $path[2] : T3Path::url($path[1]));
                 }
                 //use original url ($path[2]) if exists
             }
         }
     }
     //remove current stylesheets
     $document->_styleSheets = array();
     foreach ($document->_scripts as $strSrc => $strType) {
         $srcurl = T3Head::cleanUrl($strSrc);
         if (!$srcurl) {
             $scrips[] = array('', $strSrc);
             continue;
         }
         if (preg_match('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', $srcurl)) {
             //in template
             $srcurl = preg_replace('/^templates\\/' . T3_ACTIVE_TEMPLATE . '\\//', '', $srcurl);
             $path = str_replace('/', DS, $srcurl);
             if ($url = T3Path::get($path)) {
                 $scrips[] = $url;
             }
         } else {
             if ($optimize_js < 1) {
                 $scrips[] = array('', $strSrc);
                 continue;
             }
             $path = str_replace('/', DS, $srcurl);
             $scrips[] = array(JPATH_SITE . DS . $path, JURI::base(true) . '/' . $srcurl);
         }
     }
     //remove current scripts
     $document->_scripts = array();
     $css_urls = T3Head::optimizecss($css_urls);
     //if ($url) $css_urls = array(array(array('', $url)));
     //re-add stylesheet to head
     foreach ($css_urls as $urls) {
         foreach ($urls as $url) {
             $document->addStylesheet($url[1]);
         }
     }
     $scrips = T3Head::optimizejs($scrips);
     //re-add stylesheet to head
     foreach ($scrips as $url) {
         $document->addScript($url[1]);
     }
 }