diff --git a/advagg.admin.inc b/advagg.admin.inc index d372c66..63ec56c 100644 --- a/advagg.admin.inc +++ b/advagg.admin.inc @@ -217,8 +217,26 @@ function advagg_admin_info_form($form, $form_state) { } } - // Output all advagg hooks implemented. + // Display as module -> hook instead of hook -> module. + ksort($advagg_hooks); + $module_hooks = array(); foreach ($advagg_hooks as $hook => $values) { + if (!empty($values)) { + foreach ($values as $module_name) { + if (!isset($module_hooks[$module_name])) { + $module_hooks[$module_name] = array(); + } + $module_hooks[$module_name][] = $hook; + } + } + else { + $module_hooks['not in use'][] = $hook; + } + } + ksort($module_hooks); + + // Output all advagg hooks implemented. + foreach ($module_hooks as $hook => $values) { if (empty($values)) { $form['hooks_implemented'][$hook] = array( '#markup' => '
' . check_plain($hook) . ': 0
', @@ -226,7 +244,7 @@ function advagg_admin_info_form($form, $form_state) { } else { $form['hooks_implemented'][$hook] = array( - '#markup' => '
' . check_plain($hook) . ': ' . count($values) . '
  ' . filter_xss(implode('
  ', $values)) . '
', + '#markup' => '
' . check_plain($hook) . ': ' . count($values) . '
  ' . filter_xss(implode('
  ', $values), array('br')) . '
', ); } } diff --git a/advagg.module b/advagg.module index 0fdc433..8e8caa2 100644 --- a/advagg.module +++ b/advagg.module @@ -185,6 +185,7 @@ function advagg_element_info_alter(&$type) { // Swap in our own aggregation callback. if (isset($type['styles']['#aggregate_callback'])) { $type['styles']['#aggregate_callback'] = '_advagg_aggregate_css'; + $type['styles']['#pre_render'][] = 'advagg_modify_css_pre_render'; } // Swap in our own aggregation callback. @@ -200,6 +201,7 @@ function advagg_element_info_alter(&$type) { '#type' => 'scripts', ); } + $type['scripts']['#pre_render'][] = 'advagg_modify_js_pre_render'; } /** @@ -250,6 +252,71 @@ function advagg_admin_menu_output_alter(&$content) { } // Core CSS/JS override functions. + +/** + * #pre_render callback so elements can be modifed before they are rendered. + * + * @param $elements + * A render array containing: + * - #items: The JavaScript items as returned by drupal_add_js() and + * altered by drupal_get_js(). + * - #group_callback: A function to call to group #items. Following + * this function, #aggregate_callback is called to aggregate items within + * the same group into a single file. + * - #aggregate_callback: A function to call to aggregate the items within + * the groups arranged by the #group_callback function. + * + * @return + * A render array that will render to a string of JavaScript tags. + */ +function advagg_modify_js_pre_render($elements) { + // Put children elements into a reference array. + $children = array(); + foreach ($elements as $key => &$value) { + if ($key !== '' && $key[0] === '#') { + continue; + } + $children[$key] = &$value; + } + + // Allow other modules to modify the children before they are rendered. + // Call hook_advagg_modify_js_pre_render_alter() + drupal_alter('advagg_modify_js_pre_render', $children); + return $elements; +} + +/** + * #pre_render callback so elements can be modifed before they are rendered. + * + * @param $elements + * A render array containing: + * - #items: The JavaScript items as returned by drupal_add_js() and + * altered by drupal_get_js(). + * - #group_callback: A function to call to group #items. Following + * this function, #aggregate_callback is called to aggregate items within + * the same group into a single file. + * - #aggregate_callback: A function to call to aggregate the items within + * the groups arranged by the #group_callback function. + * + * @return + * A render array that will render to a string of JavaScript tags. + */ +function advagg_modify_css_pre_render($elements) { + // Put children elements into a reference array. + $children = array(); + foreach ($elements as $key => &$value) { + if ($key !== '' && $key[0] === '#') { + continue; + } + $children[$key] = &$value; + } + + // Allow other modules to modify the children before they are rendered. + // Call hook_advagg_modify_css_pre_render_alter() + drupal_alter('advagg_modify_css_pre_render', $children); + return $elements; +} + /** * Default callback to aggregate CSS files and inline content. * @@ -770,8 +837,10 @@ function advagg_hooks_implemented($all = TRUE) { $hooks += array( 'advagg_build_aggregate_plans_alter' => array(), 'advagg_changed_files' => array(), - 'advagg_js_groups' => array(), - 'advagg_css_groups' => array(), + 'advagg_css_groups_alter' => array(), + 'advagg_js_groups_alter' => array(), + 'advagg_modify_css_pre_render_alter' => array(), + 'advagg_modify_js_pre_render_alter' => array(), 'js_alter' => array(), 'css_alter' => array(), ); diff --git a/advagg_js_compress/advagg_js_compress.admin.inc b/advagg_js_compress/advagg_js_compress.admin.inc index 0452a34..a69c22d 100644 --- a/advagg_js_compress/advagg_js_compress.admin.inc +++ b/advagg_js_compress/advagg_js_compress.admin.inc @@ -14,34 +14,40 @@ function advagg_js_compress_admin_settings_form($form, $form_state) { $form = array(); - $form['advagg_js_compress_agg_files'] = array( - '#type' => 'checkbox', - '#title' => t('Compress JS Files'), - '#default_value' => variable_get('advagg_js_compress_agg_files', ADVAGG_JS_COMPRESS_AGG_FILES), - ); - $description = ''; - $options = array(0 => t('JSMin+')); + $options = array( + 0 => t('Disabled'), + 1 => t('JSMin+'), +// 2 => t('Packer'), + ); if (function_exists('jsmin')) { - $options[1] = t('JSMin'); + $options[3] = t('JSMin'); $description .= t('JSMin is the C complied version and is about 25 times faster. Recommend using it.'); } else { - $description .= t('You can use the much faster C version of JSMin by installing the JSMin PHP Extension on this server.', array('@php_jsmin' => 'http://www.ypass.net/software/php_jsmin/')); + $description .= t('You can use the much faster C version of JSMin by installing the JSMin PHP Extension on this server. ', array('@php_jsmin' => 'http://www.ypass.net/software/php_jsmin/')); } $form['advagg_js_compressor'] = array( '#type' => 'radios', - '#title' => t('Select the compression program to use'), + '#title' => t('File Compression: Select a Compressor'), '#default_value' => variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR), '#options' => $options, '#description' => filter_xss($description), ); + $form['advagg_js_inline_compressor'] = array( + '#type' => 'radios', + '#title' => t('Inline Compression: Select a Compressor'), + '#default_value' => variable_get('advagg_js_inline_compressor', ADVAGG_JS_INLINE_COMPRESSOR), + '#options' => $options, + '#description' => filter_xss($description), + ); + $form['advagg_js_compress_packer'] = array( '#type' => 'checkbox', - '#title' => t('Enable Packer'), + '#title' => t('Use Packer on non GZip JS Aggregates'), '#default_value' => variable_get('advagg_js_compress_packer', ADVAGG_JS_COMPRESS_PACKER), - '#description' => t('If enabled the non gzip version of JS files will be compressed using the JS Packer. WARNING: This has a high chance of breaking your JS. Only Enable on production after testing the non gzipped version locally.'), + '#description' => t('If enabled the non gzip version of JS files will be compressed using the JS Packer. Packer works similar to gzip, thus using packer on a gzipped file does not give a big improvement in terms of bytes transfered over the wire. WARNING: This has a high chance of breaking your JS. Only Enable on production after testing the non gzipped version locally.'), ); // Clear the cache bins on submit. diff --git a/advagg_js_compress/advagg_js_compress.advagg.inc b/advagg_js_compress/advagg_js_compress.advagg.inc index f3d33d1..6ae1a30 100644 --- a/advagg_js_compress/advagg_js_compress.advagg.inc +++ b/advagg_js_compress/advagg_js_compress.advagg.inc @@ -7,10 +7,12 @@ /** * Implement hook_advagg_get_js_file_contents_alter(). + * + * Used to compress a js file. */ function advagg_js_compress_advagg_get_js_file_contents_alter(&$contents, $filename, $aggregate_settings) { - // Do nothing if js compression is disabled. - if (empty($aggregate_settings['variables']['advagg_js_compress_agg_files']) || !isset($aggregate_settings['variables']['advagg_js_compressor'])) { + // Do nothing if js file compression is disabled. + if (empty($aggregate_settings['variables']['advagg_js_compressor'])) { return; } @@ -105,7 +107,9 @@ function advagg_js_compress_advagg_save_aggregate_alter(&$files_to_save, $aggreg } /** - * Implement advagg_changed_files(). + * Implement hook_advagg_changed_files(). + * + * Used to retest js files when they change. */ function advagg_js_compress_advagg_changed_files($files, $types) { // Only care about js files. @@ -160,15 +164,15 @@ function advagg_js_compress_prep(&$contents, $filename, $aggregate_settings, $ad // Strip Byte Order Marks (BOM's), preg_* cannot parse these well. $contents = str_replace(pack("CCC", 0xef, 0xbb, 0xbf), "", $contents); // Use the compressor. - if ($compressor == 0) { + if ($compressor == 1) { advagg_js_compress_jsminplus($contents, $log_errors); } - elseif ($compressor == 1) { - $contents = jsmin($contents); - } elseif ($compressor == 2) { advagg_js_compress_jspacker($contents); } + elseif ($compressor == 3) { + $contents = jsmin($contents); + } // Save into cache. cache_set($cache_id, $contents, 'cache_advagg_info'); @@ -265,26 +269,26 @@ function advagg_js_compress_run_test($filename) { $httprl_used = FALSE; // Build list of compressors. - $compressors = array(0 => 'jsminplus', 2 => 'packer'); + $compressors = array(1 => 'jsminplus', 2 => 'packer'); if (function_exists('jsmin')) { - $compressors[1] = 'jsmin'; + $compressors[3] = 'jsmin'; ksort($compressors); } // Set to 0 if file doesn't exist. if (empty($info['content_hash'])) { $results = array( - 0 => array('code' => 0, 'ratio' => 0, 'name' => 'jsminplus'), - 1 => array('code' => 0, 'ratio' => 0, 'name' => 'jsmin'), + 1 => array('code' => 0, 'ratio' => 0, 'name' => 'jsminplus'), 2 => array('code' => 0, 'ratio' => 0, 'name' => 'packer'), + 3 => array('code' => 0, 'ratio' => 0, 'name' => 'jsmin'), ); } else { // Set to "-1" so if php bombs, the file will be marked as bad. $results = array( - 0 => array('code' => -1, 'ratio' => 0, 'name' => 'jsminplus'), - 1 => array('code' => -1, 'ratio' => 0, 'name' => 'jsmin'), + 1 => array('code' => -1, 'ratio' => 0, 'name' => 'jsminplus'), 2 => array('code' => -1, 'ratio' => 0, 'name' => 'packer'), + 3 => array('code' => -1, 'ratio' => 0, 'name' => 'jsmin'), ); // Run this via httprl if possible. if (!module_exists('httprl') || !httprl_is_background_callback_capable()) { diff --git a/advagg_js_compress/advagg_js_compress.module b/advagg_js_compress/advagg_js_compress.module index a1bb476..f9552c8 100644 --- a/advagg_js_compress/advagg_js_compress.module +++ b/advagg_js_compress/advagg_js_compress.module @@ -3,7 +3,6 @@ /** * @file * Advanced CSS/JS aggregation js compression module. - * */ /** @@ -12,24 +11,24 @@ define('ADVAGG_JS_COMPRESS_PACKER', FALSE); /** - * Default value to see what compressor to use. 0 is JSMin+. + * Default value to see what compressor to use. 0 is Disabled. */ define('ADVAGG_JS_COMPRESSOR', 0); /** - * Default value for the compression ratio test. + * Default value to see what compressor to use. 0 is Disabled. */ -define('ADVAGG_JS_COMPRESS_RATIO', 0.1); +define('ADVAGG_JS_INLINE_COMPRESSOR', 0); /** * Default value for the compression ratio test. */ -define('ADVAGG_JS_MAX_COMPRESS_RATIO', 0.90); +define('ADVAGG_JS_COMPRESS_RATIO', 0.1); /** - * Default value to see if this will compress aggregated files. + * Default value for the compression ratio test. */ -define('ADVAGG_JS_COMPRESS_AGG_FILES', TRUE); +define('ADVAGG_JS_MAX_COMPRESS_RATIO', 0.9); /** * Implementation of hook_menu @@ -58,12 +57,38 @@ function advagg_js_compress_menu() { */ function advagg_js_compress_advagg_current_hooks_hash_array_alter(&$aggregate_settings) { $aggregate_settings['variables']['advagg_js_compressor'] = variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR); - $aggregate_settings['variables']['advagg_js_compress_agg_files'] = variable_get('advagg_js_compress_agg_files', ADVAGG_CSS_COMPRESS_AGG_FILES); $aggregate_settings['variables']['advagg_js_compress_packer'] = variable_get('advagg_js_compress_packer', ADVAGG_JS_COMPRESS_PACKER); $aggregate_settings['variables']['advagg_js_max_compress_ratio'] = variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO); } /** + * Implement hook_advagg_modify_js_pre_render_alter(). + * + * Used compress inline js. + */ +function advagg_js_compress_advagg_modify_js_pre_render_alter(&$children) { + // Get variables. + $aggregate_settings['variables']['advagg_js_compressor'] = variable_get('advagg_js_inline_compressor', ADVAGG_JS_INLINE_COMPRESSOR); + $aggregate_settings['variables']['advagg_js_max_compress_ratio'] = variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO); + + // Do nothing if the compressor is disabled. + if (empty($aggregate_settings['variables']['advagg_js_compressor'])) { + return; + } + + // Compress any inline JS. + module_load_include('inc', 'advagg_js_compress', 'advagg_js_compress.advagg'); + foreach ($children as $key => &$values) { + if (!empty($values['#value'])) { + $contents = $values['#value']; + $filename = drupal_hash_base64($contents); + advagg_js_compress_prep($contents, $filename, $aggregate_settings, FALSE); + $values['#value'] = $contents; + } + } +} + +/** * Test a file, making sure it is compressable. * * @param $filename