/** * Register Helpers * * @return null */ public static function registerHelpers() { include_once 'helpers/MgCacheHelper.php'; include_once 'helpers/MgAssetHelper.php'; MgAssetHelper::init(); MgCacheHelper::init(); }
/** * Register Settings * * @return null */ public static function registerSettings() { // retrieve admin options $adminOptions = MgCacheHelper::getAdminOptions(); // set internal properties self::$cacheStylesheets = $adminOptions['cache_stylesheets'] ? true : false; self::$cacheScripts = $adminOptions['cache_scripts'] ? true : false; self::$concatenate = $adminOptions['concatenate_files'] ? true : false; self::$minify = $adminOptions['minify_output'] ? true : false; }
/** * * 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')); } }
/** * Print Admin Page * * @return null */ public static function printAdminPage() { // retrieve admin options $adminOptions = MgCacheHelper::getAdminOptions(); $route = str_replace('&clear_cache=true', '', $_SERVER['REQUEST_URI']); // manual cache clear if (isset($_GET['clear_cache']) && $_GET['clear_cache'] == 'true') { // delete contents of cache directory try { $flushResponse = MgCacheHelper::flush(); ?> <div class="updated"><p><strong><?php _e("Cache Cleared.", "Mg Cache"); ?> </strong></p></div> <?php } catch (\Exception $e) { ?> <div class="error"><p><strong><?php _e("Error encountered clearing cache.", "Mg Cache"); ?> </strong></p></div> <?php } } // check if form was submitted if (isset($_POST['update_MgCacheSettings'])) { // update admin options array with post values $adminOptions['cache_stylesheets'] = isset($_POST['cache_stylesheets']) ? true : false; $adminOptions['cache_scripts'] = isset($_POST['cache_scripts']) ? true : false; $adminOptions['concatenate_files'] = isset($_POST['concatenate_files']) ? true : false; $adminOptions['minify_output'] = isset($_POST['minify_output']) ? true : false; // update database update_option(MgCacheHelper::$adminOptionsName, $adminOptions); // delete contents of cache directory MgCacheHelper::flush(); // output status ?> <div class="updated"><p><strong><?php _e("Settings Updated.", "Mg Cache"); ?> </strong></p></div><?php } // output admin form include realpath(dirname(__FILE__) . '/views/admin-mg-cache.php'); }