View source
<?php
function javascript_aggregator_help($path, $arg) {
switch ($path) {
case 'admin/help#javascript_aggregator':
$output = '<p>' . t('Uses the <a href="@jsmin">JSMin</a> library to minify the aggregated JavaScript file when <em>JavaScript optimization</em> has been enabled in the <a href="@performance">Performance settings</a>.', array(
'@performance' => url('admin/settings/performance'),
'@jsmin' => 'http://code.google.com/p/jsmin-php/',
)) . '</p>';
return $output;
}
}
function javascript_aggregator_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'system_performance_settings') {
$form['bandwidth_optimizations']['preprocess_js']['#title'] = t('Optimize and Minify JavaScript files');
$form['bandwidth_optimizations']['preprocess_js']['#description'] .= t(' Once the JavaScript files have been aggregated, they will be minified.');
$form['bandwidth_optimizations']['preprocess_js']['#weight'] = 2;
$form['bandwidth_optimizations']['javascript_aggregator_gzip'] = array(
'#type' => 'checkbox',
'#title' => t('GZip JavaScript'),
'#description' => t('Once minified, optionally <a href="@gzip">GZip</a> the aggregated JavaScript file to dramatically decrease its size.', array(
'@gzip' => 'http://en.wikipedia.org/wiki/Gzip',
)),
'#default_value' => variable_get('javascript_aggregator_gzip', FALSE),
'#weight' => 3,
);
$form['bandwidth_optimizations']['javascript_aggregator_no_htaccess'] = array(
'#type' => 'checkbox',
'#title' => t('Do not auto generate .htaccess file (experts only)'),
'#description' => t("If you want to use GZipping and your host doesn't like multiple .htaccess files check this option, together with the option above to bypass htaccess file generation and follow directions in the README.txt."),
'#default_value' => variable_get('javascript_aggregator_no_htaccess', FALSE),
'#weight' => 5,
);
$form['bandwidth_optimizations']['javascript_aggregator_jsminplus'] = array(
'#type' => 'checkbox',
'#title' => t('Use JSMin+ instead of JSMin'),
'#description' => t('Check this option to use <a href="@jsminplus">JSMin+</a> instead of JSMin.', array(
'@jsminplus' => 'http://crisp.tweakblogs.net/blog/1665/a-new-javascript-minifier-jsmin+.html',
)),
'#default_value' => variable_get('javascript_aggregator_jsminplus', FALSE),
'#weight' => 4,
);
}
}
function javascript_aggregator_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['page'])) {
if (is_array($theme_registry['page']['preprocess functions']) && ($key = array_search('javascript_aggregator_preprocess_page', $theme_registry['page']['preprocess functions']))) {
unset($theme_registry['page']['preprocess functions'][$key]);
}
$theme_registry['page']['preprocess functions'][] = 'javascript_aggregator_preprocess_page';
}
}
function javascript_aggregator_preprocess_page(&$variables) {
if (!empty($variables['scripts'])) {
$variables['scripts'] = _javascript_aggregator_minify($variables['scripts']);
}
}
function phptemplate_closure($main = 0) {
$footer = module_invoke_all('footer', $main);
$js_footer = drupal_get_js('footer');
if (!empty($js_footer)) {
$js_footer = _javascript_aggregator_minify($js_footer);
}
return implode("\n", $footer) . $js_footer;
}
function _javascript_aggregator_minify($scripts) {
if (variable_get('preprocess_js', 0)) {
$path_to_files_directory = base_path() . file_directory_path();
$pattern = "!(<script type=\"text\\/javascript\" src=\"(.*?){$path_to_files_directory})(.*?)(\"(.*?)><\\/script>)!";
if (preg_match_all($pattern, $scripts, $matches) > 0) {
$aggregated_file_name = $matches[3][0];
$jsmin_file_name = $aggregated_file_name . 'min.js';
$jsmin_file_path = file_directory_path() . $jsmin_file_name;
if (!file_exists($jsmin_file_path)) {
if (variable_get('javascript_aggregator_jsminplus', FALSE)) {
require_once drupal_get_path('module', 'javascript_aggregator') . '/jsminplus.php';
$file = str_replace(pack("CCC", 0xef, 0xbb, 0xbf), "", file_get_contents(file_directory_path() . $aggregated_file_name));
$contents = JSMinPlus::minify($file);
}
else {
require_once drupal_get_path('module', 'javascript_aggregator') . '/jsmin.php';
$contents = JSMin::minify(file_get_contents(file_directory_path() . $aggregated_file_name));
}
$contents = '// Minified using Javascript Aggregator - see ' . $path_to_files_directory . $aggregated_file_name . " for original source including licensing information.\n" . $contents;
$htaccess = file_directory_path() . '/js/.htaccess';
if (variable_get('javascript_aggregator_gzip', FALSE)) {
if (!file_exists($jsmin_file_path . '.gz')) {
file_save_data(gzencode($contents, 9), $jsmin_file_path . '.gz', FILE_EXISTS_REPLACE);
}
if (!variable_get('javascript_aggregator_no_htaccess', FALSE) && !file_exists($htaccess)) {
$rewrite_base = base_path() . file_directory_path() . '/js/';
$htaccess_contents = <<<EOT
<Files *.js.gz>
AddEncoding x-gzip .gz
ForceType text/javascript
</Files>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase {<span class="php-variable">$rewrite_base</span>}
RewriteCond %{HTTP_USER_AGENT} !".*Safari.*"
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)\\.js \$1.js.gz [L,QSA]
</IfModule>
EOT;
file_save_data($htaccess_contents, $htaccess, FILE_EXISTS_REPLACE);
}
}
else {
if (file_exists($htaccess)) {
file_delete($htaccess);
}
}
file_save_data($contents, $jsmin_file_path, FILE_EXISTS_REPLACE);
}
$scripts = str_replace($aggregated_file_name, $jsmin_file_name, $scripts);
}
}
return $scripts;
}