Exemplo n.º 1
0
 /**
  *
  * @param HeadModule $Head
  */
 public function HeadModule_BeforeToString_Handler($Head)
 {
     $Tags = $Head->Tags();
     // Grab all of the css.
     $CssToCache = array();
     $JsToCache = array();
     // Add the global js files
     $GlobalJS = array('jquery.js', 'jquery.livequery.js', 'jquery.form.js', 'jquery.popup.js', 'jquery.gardenhandleajaxform.js', 'global.js');
     foreach ($Tags as $Index => $Tag) {
         $IsJs = GetValue(HeadModule::TAG_KEY, $Tag) == 'script';
         $IsCss = GetValue(HeadModule::TAG_KEY, $Tag) == 'link' && GetValue('rel', $Tag) == 'stylesheet';
         if (!$IsJs && !$IsCss) {
             continue;
         }
         if ($IsCss) {
             $Href = GetValue('href', $Tag, '!');
         } else {
             $Href = GetValue('src', $Tag, '!');
         }
         if ($Href[0] != '/') {
             continue;
         }
         // Strip any querystring off the href.
         $Href = preg_replace('`\\?.*`', '', $Href);
         $Path = PATH_ROOT . $Href;
         if (!file_exists($Path)) {
             continue;
         }
         // Remove the css from the tag because minifier is taking care of it.
         unset($Tags[$Index]);
         // Add the reference to the appropriate cache collection.
         if ($IsCss) {
             $CssToCache[] = $Href;
         } elseif ($IsJs) {
             // Don't include the file if it's in the global js.
             $Filename = basename($Path);
             if (in_array($Filename, $GlobalJS)) {
                 $GlobalJsFound = TRUE;
                 continue;
             }
             $JsToCache[] = $Href;
         }
     }
     // Add minified css & js directly to the head module.
     $Url = 'plugins/Minify/min/?';
     $BasePath = Gdn::Request()->WebRoot();
     if ($BasePath != '') {
         $BasePath = 'b=' . $BasePath . '&';
     }
     $Head->Tags($Tags);
     $Head->AddCss($Url . $BasePath . 'f=' . implode(',', $CssToCache), 'screen');
     $Head->AddScript($Url . 'g=globaljs', 'text/javascript', -100);
     $Head->AddScript($Url . $BasePath . 'f=' . implode(',', $JsToCache));
 }
 public function Base_AfterBody_Handler($Sender)
 {
     if (!empty($this->DeferJs)) {
         $Head = new HeadModule();
         $SortLast = 0;
         foreach ($this->ExternalJs as $ExternalJs) {
             if (intval($ExternalJs['_sort']) > $SortLast) {
                 $SortLast = intval($ExternalJs['_sort']);
             }
         }
         foreach ($this->DeferJs as $Token) {
             $SortLast++;
             $Head->AddScript("/cache/Consolidate/{$Token}", 'text/javascript', stripos($Token, '_jquery_js') !== FALSE ? -100 : $SortLast);
         }
         $Tags = $Head->GetTags();
         $Tags = array_merge($Tags, $this->ExternalJs);
         $Tags = array_merge($Tags, $this->InlineJs);
         $Head->Tags($Tags);
         $HeadParts = split("\n", $Head->ToString());
         foreach ($HeadParts as $HeadPart) {
             if (stripos($HeadPart, '<script') !== FALSE) {
                 echo trim($HeadPart) . "\n";
             }
         }
         foreach ($this->InlineJsStrings as $InlineJsString) {
             echo trim($InlineJsString) . "\n";
         }
     }
 }
   /**
    * Remove all CSS and JS files and add minified versions.
    *
    * @param HeadModule $Head
    */
   public function HeadModule_BeforeToString_Handler($Head) {
      // Set BasePath for the plugin
      $this->BasePath = Gdn::Request()->WebRoot();
      
      // Get current tags
      $Tags = $Head->Tags();

      // Grab all of the CSS
      $CssToCache = array();
      $JsToCache = array(); // Add the global js files
      $GlobalJS = array(
         'jquery.js',
         'jquery.livequery.js',
         'jquery.form.js',
         'jquery.popup.js',
         'jquery.gardenhandleajaxform.js',
         'global.js'
      );
      
      // Process all tags, finding JS & CSS files
      foreach ($Tags as $Index => $Tag) {
         $IsJs = GetValue(HeadModule::TAG_KEY, $Tag) == 'script';
         $IsCss = (GetValue(HeadModule::TAG_KEY, $Tag) == 'link' && GetValue('rel', $Tag) == 'stylesheet');
         if (!$IsJs && !$IsCss)
            continue;

         if ($IsCss)
            $Href = GetValue('href', $Tag, '!');
         else
            $Href = GetValue('src', $Tag, '!');
         
         // Skip the rest if path doesn't start with a slash
         if ($Href[0] != '/')
            continue;

         // Strip any querystring off the href.
         $Href = preg_replace('`\?.*`', '', $Href);
         
         // Strip BasePath & extra slash from Href (Minify adds an extra slash when substituting basepath)
         if($this->BasePath != '')
            $Href = preg_replace("`^/{$this->BasePath}/`U", '', $Href);
            
         // Skip the rest if the file doesn't exist
         $FixPath = ($Href[0] != '/') ? '/' : ''; // Put that slash back to test for it in file structure
         $Path = PATH_ROOT . $FixPath . $Href;
         if (!file_exists($Path))
            continue;

         // Remove the css from the tag because minifier is taking care of it.
         unset($Tags[$Index]);

         // Add the reference to the appropriate cache collection.
         if ($IsCss) {
            $CssToCache[] = $Href;
         } elseif ($IsJs) {
            // Don't include the file if it's in the global js.
            $Filename = basename($Path);
            if (in_array($Filename, $GlobalJS)) {
               continue;
            }
            $JsToCache[] = $Href;
         }
      }
      
      // Add minified css & js directly to the head module.
      $Url = 'plugins/Minify/min/?' . ($this->BasePath != '' ? "b={$this->BasePath}&" : '');
      
      // Update HeadModule's $Tags
      $Head->Tags($Tags);
      
      // Add minified CSS to HeadModule
      $Head->AddCss($Url . 'token=' . $this->_PrepareToken($CssToCache), 'screen');
      
      // Add global minified JS separately (and first)
      $Head->AddScript($Url . 'g=globaljs', 'text/javascript', -100);
      
      // Add other minified JS to HeadModule
      $Head->AddScript($Url . 'token=' . $this->_PrepareToken($JsToCache));
   }