'CSS Compression', 'description' => 'Adjust CSS Compression settings.', 'page callback' => 'advagg_css_compress_admin_page', 'type' => MENU_LOCAL_TASK, 'access arguments' => array('administer site configuration'), 'file path' => $file_path, 'file' => 'advagg_css_compress.admin.inc', 'weight' => 10, ); return $items; } /** * Implement hook_advagg_css_alter. */ function advagg_css_compress_advagg_css_alter(&$contents, $files, $bundle_md5) { if (!variable_get('advagg_css_compress_agg_files', ADVAGG_CSS_COMPRESS_AGG_FILES)) { return; } $compressor = variable_get('advagg_css_compressor', ADVAGG_CSS_COMPRESSOR); if ($compressor == 0) { advagg_css_compress_css_tidy($contents); } elseif ($compressor == 1) { advagg_css_compress_css_compressor($contents); } elseif ($compressor == 2) { advagg_css_compress_yui_cssmin($contents); } } /** * Implement hook_advagg_css_inline_alter. */ function advagg_css_compress_advagg_css_inline_alter(&$contents) { if (!variable_get('advagg_css_compress_inline', ADVAGG_CSS_COMPRESS_INLINE)) { return; } $compressor = variable_get('advagg_css_compressor', ADVAGG_CSS_COMPRESSOR); // If using a cache, try to get the contents of it. if (variable_get('advagg_css_compress_inline_cache', ADVAGG_CSS_COMPRESS_INLINE_CACHE)) { $key = md5($contents) . $compressor; $table = 'cache_advagg_css_compress_inline'; $data = cache_get($key, $table); if (!empty($data->data)) { $contents = $data->data; return; } } if ($compressor == 0) { advagg_css_compress_css_tidy($contents); } if ($compressor == 1) { advagg_css_compress_css_compressor($contents); } // If using a cache set it. if (isset($key)) { cache_set($key, $contents, $table, CACHE_TEMPORARY); } } /** * Use the CSS Tidy library to compress the CSS. * * TODO have set_cfg be fully configurable from GUI. */ function advagg_css_compress_css_tidy(&$contents) { // Initialize CSSTidy. $filename = drupal_get_path('module', 'advagg_css_compress') . '/csstidy/class.csstidy.inc'; include_once($filename); $css = new csstidy(); // Try to allocate enough time to run CSSTidy. if (function_exists('set_time_limit')) { @set_time_limit(variable_get('advagg_set_time_limit', ADVAGG_SET_TIME_LIMIT)); } // Set configuration. $css->set_cfg('preserve_css', variable_get('advagg_css_compress_preserve_css', ADVAGG_CSS_COMPRESS_PRESERVE_CSS)); $css->set_cfg('remove_last_;', TRUE); $css->set_cfg('merge_selectors', TRUE); $css->set_cfg('optimise_shorthands', TRUE); $css->set_cfg('silent', TRUE); $css->set_cfg('compress_colors', TRUE); $css->set_cfg('sort_selectors', FALSE); $css->set_cfg('sort_properties', FALSE); $css->set_cfg('discard_invalid_properties', FALSE); $css->set_cfg('timestamp', FALSE); $css->load_template("highest_compression"); // Compress CSS. $css->parse($contents); $contents = $css->print->plain(); } /** * Use the CSS Compressor library to compress the CSS. * * TODO have compression level be selectable from GUI. */ function advagg_css_compress_css_compressor(&$contents) { // Initialize CSSTidy. $filename = drupal_get_path('module', 'advagg_css_compress') . '/css-compressor-3.x/src/CSSCompression.inc'; include_once($filename); $CSSC = new CSSCompression(variable_get('advagg_css_compress_compressor_level', ADVAGG_CSS_COMPRESS_COMPRESSOR_LEVEL)); $contents = $CSSC->compress($contents); } /** * Use the CSSmin library from YUI to compress the CSS. */ function advagg_css_compress_yui_cssmin(&$contents) { // Include CSSmin from YUI. $filename = drupal_get_path('module', 'advagg_css_compress') . '/yui/CSSMin.inc'; include_once($filename); $cssmin = new CSSmin(); // Compress the CSS splitting lines after 4k of text $contents = $cssmin->run($contents, 4096); } /** * Implementation of hook_flush_caches(). */ function advagg_css_compress_flush_caches() { return array('cache_advagg_css_compress_inline'); }