public function testSingletonProtection() { update_option('am_log', 0); $cache = new AssetsMinify\Cache(); $log = Log::getInstance($cache); $this->assertTrue($log->isActive()); }
/** * Returns footer's inclusion for JS */ public function footer() { $this->js->generate('footer'); if ($this->cache->isUpdated()) { Log::getInstance()->dumpStorage(); } }
/** * Constructor */ public function __construct() { // Cache manager $this->cache = new Cache(); if (get_option('am_dev_mode', 0)) { $this->cache->flush(); } // Assets managers for Js and Css $this->js = new Js($this); $this->css = new Css($this); // Log manager $this->log = \AssetsMinify\Log::getInstance($this->cache); $this->exclusions = preg_split('/[ ]*,[ ]*/', trim(get_option('am_files_to_exclude'))); //Detects all js and css added to WordPress and removes their inclusion if (get_option('am_compress_styles', 1)) { add_action('wp_print_styles', array($this->css, 'extract')); } if (get_option('am_compress_scripts', 1)) { add_action('wp_print_scripts', array($this->js, 'extract')); } //Inclusion of scripts in <head> and before </body> add_action('wp_head', array($this, 'header')); add_action('wp_footer', array($this, 'footer')); }
/** * Takes all the stylesheets and manages their queue to compress them */ public function generate() { $profiler = array(time()); foreach ($this->assets as $media => $assets) { foreach ($assets as $ext => $content) { $mtime = md5(json_encode($content)); $cachefile = "{$media}-{$ext}-{$mtime}.css"; if (!$this->cache->fs->has($cachefile)) { $class = "AssetsMinify\\Assets\\Css\\" . ucfirst($ext); new $class($content['files'], $cachefile, $this); } $key = "{$media}-{$ext}-am-generated"; $this->files[$media][$key] = $this->cache->getPath() . $cachefile; $this->mtimes[$media][$key] = filemtime($this->files[$media][$key]); } } if (empty($this->files)) { return false; } foreach ($this->files as $media => $files) { $mtime = md5(json_encode($this->mtimes[$media])); //Saves the asseticized stylesheets $cachedFilename = "head-{$media}-{$mtime}.css"; if (!$this->cache->fs->has($cachedFilename)) { $cssDump = $this->createAsset($files, $this->getFilters())->dump(); $cssDump = str_replace('url(/wp-', 'url(' . site_url() . '/wp-', $cssDump); $cssDump = str_replace('url("/wp-', 'url("' . site_url() . '/wp-', $cssDump); $cssDump = str_replace("url('/wp-", "url('" . site_url() . "/wp-", $cssDump); $this->cache->fs->set($cachedFilename, $cssDump); $this->cache->update(); } //Prints css inclusion in the page $this->dump($cachedFilename, $media); } $profiler[] = time(); Log::getInstance()->set('Css minification', $profiler); }
/** * Takes all the JavaScript and manages the queue to compress them * * @param string $where The page's place to dump the scripts in (header or footer) */ public function generate($where) { $profiler = array(time()); if (!isset($this->assets[$where])) { return false; } foreach ($this->assets[$where] as $ext => $content) { $mtime = md5(json_encode($content)); $cachefile = "{$where}-{$ext}-{$mtime}.js"; if (!$this->cache->fs->has($cachefile)) { $class = "AssetsMinify\\Assets\\Js\\" . ucfirst($ext); new $class($content['files'], $cachefile, $this); } $key = "{$ext}-am-generated"; $this->files[$where][$key] = $this->cache->getPath() . $cachefile; $this->mtimes[$where][$key] = filemtime($this->files[$where][$key]); } if (empty($this->files[$where])) { return false; } $mtime = md5(json_encode($this->mtimes[$where])); //Saves the asseticized header scripts if (!$this->cache->fs->has("{$where}-{$mtime}.js")) { $this->cache->fs->set("{$where}-{$mtime}.js", $this->createAsset($this->files[$where], $this->getFilters())->dump()); $this->cache->update(); } //Prints <script> inclusion in the page $this->dumpScriptData($where); $async = false; if ($where != 'header' && get_option('am_async_flag', 1)) { $async = true; } $this->dump("{$where}-{$mtime}.js", $async); $profiler[] = time(); Log::getInstance()->set('Js minification', $profiler); }