Ejemplo n.º 1
0
 /**
  * Register Helpers
  *
  * @return null
  */
 public static function registerHelpers()
 {
     include_once 'helpers/MgCacheHelper.php';
     include_once 'helpers/MgAssetHelper.php';
     MgAssetHelper::init();
     MgCacheHelper::init();
 }
Ejemplo n.º 2
0
 /**
  *
  * METHODS
  *
  */
 public static function init()
 {
     if (defined('MG_CACHE_DIR')) {
         self::$cacheDir = WP_CONTENT_DIR . '/' . MG_CACHE_DIR . '/' . self::$cacheSubdir;
     } else {
         self::$cacheDir = WP_CONTENT_DIR . '/cache/' . self::$cacheSubdir;
     }
     $wpContentUrl = preg_replace('/(http|https):\\/\\/' . $_SERVER['HTTP_HOST'] . '\\/(.*)\\/wp-content/', '/wp-content', WP_CONTENT_URL);
     if (defined('MG_CACHE_PATH')) {
         self::$cachePath = $wpContentUrl . '/' . MG_CACHE_PATH . '/' . self::$cacheSubdir;
     } else {
         self::$cachePath = $wpContentUrl . '/cache/' . self::$cacheSubdir;
     }
     if (defined('MG_CACHE_EXTENSION')) {
         self::$fileExtension = MG_CACHE_EXTENSION;
     } else {
         self::$fileExtension = 'cache';
     }
     if (defined('MG_CACHE_DRIVER')) {
         self::$cacheDriver = CacheProviderFactory::build(MG_CACHE_DRIVER);
     }
     // ABSPATH won't work reliably if WP is installed in a subdirectory
     self::$webRoot = realpath(dirname($_SERVER['SCRIPT_FILENAME'])) . '/';
     if (empty(self::$webRoot)) {
         self::$webRoot = ABSPATH;
     }
     if (defined('MG_CACHE_DRIVER')) {
         self::$cacheDriver = CacheProviderFactory::build(MG_CACHE_DRIVER);
     } else {
         self::$cacheDriver = CacheProviderFactory::build('file');
     }
     // test that cache directory is writable (for windows and vagrant... which fail is_writable)
     try {
         $testFile = self::$cacheDir . '/__cache.txt';
         $fp = fopen($testFile, 'w');
         fwrite($fp, 'hello cache!');
         if (!file_exists($testFile)) {
             throw new \Exception();
         }
         MgAssetHelper::registerSettings();
         MgAssetHelper::registerFilters();
     } catch (\Exception $e) {
         add_action('admin_notices', array('MgAssetHelper', 'adminErrorNoticeCacheDirectoryWritable'));
     }
 }
Ejemplo n.º 3
0
 /**
  * Asset Stylesheet Action
  *
  * @return null
  */
 public static function assetStylesheetAction()
 {
     $pagename = get_query_var('pagename');
     $files = get_query_var('files');
     $fingerprint = get_query_var('fingerprint');
     // check if on mg_asset_css page and have 'files' parameter
     if ($pagename == 'mg_asset_css' && $files) {
         $content = '';
         $lastModified = new \DateTime('-1 year');
         // initialize cached resource file location
         $cachedResourceFile = MgCacheHelper::$cacheDir . '/' . $fingerprint . '.css';
         // create new cached resource, save to cache directory
         if (!file_exists($cachedResourceFile)) {
             // turn files parameter into array
             $fileArray = explode(',', $files);
             if (is_array($fileArray) && count($fileArray)) {
                 // loop over array of files
                 foreach ($fileArray as $file) {
                     if ($stylesheetPath = realpath(MgCacheHelper::$webRoot . $file)) {
                         // get timestamp for most recently modified file
                         $localLastModified = new \DateTime(date("F d Y H:i:s", filemtime($stylesheetPath)));
                         if ($localLastModified > $lastModified) {
                             $lastModified = $localLastModified;
                         }
                         // get contents of each file while fixing asset paths
                         $content .= MgAssetHelper::assetContents($file);
                     }
                 }
                 // strip whitespace and comments from $content
                 if (MgAssetHelper::$minify) {
                     $content = preg_replace(array('/\\/\\*.*?\\*\\//s', '/\\s+/'), array('', ' '), $content);
                 }
             }
             // save contents
             if (!empty($content)) {
                 if (!is_writable(dirname($cachedResourceFile))) {
                     mkdir(dirname($cachedResourceFile), 0775, true);
                 }
                 if (is_writable(dirname($cachedResourceFile))) {
                     file_put_contents($cachedResourceFile, $content);
                 }
             }
         }
         // read cached resource to buffer
         if (file_exists($cachedResourceFile)) {
             ob_start();
             readfile($cachedResourceFile);
             $content = ob_get_contents();
             ob_end_clean();
             $lastModified = new \DateTime(date("F d Y H:i:s", filemtime($cachedResourceFile)));
         }
         // output
         if (empty($content)) {
             header('HTTP/1.1 404 Not Found');
         } else {
             header('HTTP/1.1 200 OK');
             header('Content-Type: text/css');
             header('Content-Length: ' . strlen($content));
             header('Expires: ' . date('D, d M Y H:i:s e', strtotime('+1 year')));
             header('Cache-Control: max-age=31556926');
             header('Last-Modified: ' . $lastModified->format('D, d M Y H:i:s e'));
             header('Date: ' . $lastModified->format('D, d M Y H:i:s e'));
             header('Pragma: cache');
             header('Vary: Accept-Encoding');
             header('X-Content-Type-Options: nosniff');
             header('Accept-Ranges: bytes');
             echo $content;
         }
         exit;
     }
 }
Ejemplo n.º 4
0
 /**
  * Asset Contents
  *
  * @param string $file
  * @return string
  */
 public static function assetContents($file)
 {
     $return = '';
     if ($stylesheetPath = realpath(MgCacheHelper::$webRoot . $file)) {
         // get file info
         $pathinfo = pathinfo($file);
         // set local var for temp use in callback
         self::$assetDirectory = $pathinfo['dirname'];
         // string replacement callback
         $return = preg_replace_callback('/(url\\(\\s*[\'\\"]?)(.*?)([\'\\"]?\\s*\\))/', array('MgAssetHelper', 'assetContentHandler'), file_get_contents($stylesheetPath));
     }
     // reset local temp var
     self::$assetDirectory = null;
     return $return;
 }