You are here

advagg_css_compress.module in Advanced CSS/JS Aggregation 7.2

Advanced aggregation css compression module.

File

advagg_css_compress/advagg_css_compress.module
View source
<?php

/**
 * @file
 * Advanced aggregation css compression module.
 */

/**
 * @addtogroup default_variables
 * @{
 */

/**
 * Default value for which css compression library to use. 0 is Disabled.
 */
define('ADVAGG_CSS_COMPRESSOR', 2);

/**
 * Default value to see what compressor to use. 0 is Disabled.
 */
define('ADVAGG_CSS_COMPRESS_INLINE', 0);

/**
 * Default value to if inline compression is used if page is not cacheable.
 */
define('ADVAGG_CSS_COMPRESS_INLINE_IF_NOT_CACHEABLE', FALSE);

/**
 * Default value for per file compression settings.
 */
define('ADVAGG_CSS_COMPRESSOR_FILE_SETTINGS', -10);

/**
 * @} End of "addtogroup default_variables".
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Implements hook_menu().
 */
function advagg_css_compress_menu() {
  $file_path = drupal_get_path('module', 'advagg_css_compress');
  $config_path = advagg_admin_config_root_path();
  $items[$config_path . '/advagg/css-compress'] = array(
    'title' => 'CSS Compression',
    'description' => 'Adjust CSS Compression settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'advagg_css_compress_admin_settings_form',
    ),
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array(
      'administer site configuration',
    ),
    'file path' => $file_path,
    'file' => 'advagg_css_compress.admin.inc',
    'weight' => 10,
  );
  return $items;
}

/**
 * @} End of "addtogroup hooks".
 */

/**
 * @addtogroup advagg_hooks
 * @{
 */

/**
 * Implements hook_advagg_current_hooks_hash_array_alter().
 */
function advagg_css_compress_advagg_current_hooks_hash_array_alter(&$aggregate_settings) {
  $aggregate_settings['variables']['advagg_css_compressor'] = variable_get('advagg_css_compressor', ADVAGG_CSS_COMPRESSOR);
  $aggregate_settings['variables']['advagg_css_compressor_file_settings'] = variable_get('advagg_css_compressor_file_settings', array());
}

/**
 * Implements hook_advagg_modify_css_pre_render_alter().
 *
 * Used to compress inline css.
 */
function advagg_css_compress_advagg_modify_css_pre_render_alter(&$children, &$elements) {

  // Get variables.
  $compressor = variable_get('advagg_css_compress_inline', ADVAGG_CSS_COMPRESS_INLINE);

  // Do nothing if the compressor is disabled.
  if (empty($compressor)) {
    return;
  }

  // Do nothing if the page is not cacheable and inline compress if not
  // cacheable is not checked.
  if (!variable_get('advagg_css_compress_inline_if_not_cacheable', ADVAGG_CSS_COMPRESS_INLINE_IF_NOT_CACHEABLE) && !drupal_page_is_cacheable()) {
    return;
  }
  module_load_include('inc', 'advagg_css_compress', 'advagg_css_compress.advagg');
  if ($compressor == 2) {

    // Compress any inline CSS with YUI.
    foreach ($children as &$values) {
      if (!empty($values['#value'])) {
        advagg_css_compress_yui_cssmin($values['#value']);
      }
    }
    unset($values);
  }
}

/**
 * @} End of "addtogroup advagg_hooks".
 */

/**
 * @addtogroup 3rd_party_hooks
 * @{
 */

/**
 * Implements hook_libraries_info().
 */
function advagg_css_compress_libraries_info() {
  $libraries['YUI-CSS-compressor-PHP-port'] = array(
    // Only used in administrative UI of Libraries API.
    'name' => 'YUI CSS compressor PHP port',
    'vendor url' => 'https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port',
    'download url' => 'https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port/archive/master.zip',
    'version callback' => 'advagg_css_compress_libraries_get_version',
    'version arguments' => array(
      'file' => 'README.md',
      'pattern' => '/###\\s+v([0-9a-zA-Z\\.-]+)/',
      'lines' => 1000,
      'cols' => 20,
      'default_version' => '2.4.8',
    ),
    'local version' => '2.4.8-p10',
    'remote' => array(
      'callback' => 'advagg_get_github_version_txt',
      'url' => 'https://cdn.jsdelivr.net/gh/tubalmartin/YUI-CSS-compressor-PHP-port@master/README.md',
    ),
    'versions' => array(
      '2' => array(
        'files' => array(
          'php' => array(
            'cssmin.php',
            'data/hex-to-named-color-map.php',
            'data/named-to-hex-color-map.php',
          ),
        ),
      ),
      '3' => array(
        'files' => array(
          'php' => array(
            'src/Minifier.php',
            'src/Utils.php',
            'src/Colors.php',
            'src/data/hex-to-named-color-map.php',
            'src/data/named-to-hex-color-map.php',
          ),
        ),
      ),
      '4' => array(
        'files' => array(
          'php' => array(
            'src/Minifier.php',
            'src/Utils.php',
            'src/Colors.php',
          ),
        ),
      ),
    ),
  );
  return $libraries;
}

/**
 * @} End of "addtogroup 3rd_party_hooks".
 */

/**
 * Try libraries_get_version(), on failure use the passed in default_version.
 *
 * @param array $library
 *   An associative array containing all information about the library.
 * @param array $options
 *   An associative array containing options for the version parser.
 *
 * @return string
 *   Version number.
 */
function advagg_css_compress_libraries_get_version(array $library, array $options) {
  $return = libraries_get_version($library, $options);
  if (empty($return) && !empty($options['default_version'])) {
    $file = DRUPAL_ROOT . '/' . $library['library path'] . '/' . $options['file'];
    if (is_readable($file)) {
      return $options['default_version'];
    }
  }
  return $return;
}

/**
 * Generate the js compress configuration.
 *
 * @return array
 *   Array($options, $description, $compressors, $functions).
 */
function advagg_css_compress_configuration() {
  $description = '';
  $options = array(
    -1 => t('Disable Core'),
    0 => t('Core'),
    2 => t('YUI'),
  );
  $compressors = array();
  $functions = array(
    2 => 'advagg_css_compress_yui_cssmin',
  );

  // Allow for other modules to alter this list.
  $options_desc = array(
    $options,
    $description,
  );
  drupal_alter('advagg_css_compress_configuration', $options_desc, $compressors, $functions);
  list($options, $description) = $options_desc;
  return array(
    $options,
    $description,
    $compressors,
    $functions,
  );
}

Functions

Constants

Namesort descending Description
ADVAGG_CSS_COMPRESSOR Default value for which css compression library to use. 0 is Disabled.
ADVAGG_CSS_COMPRESSOR_FILE_SETTINGS Default value for per file compression settings.
ADVAGG_CSS_COMPRESS_INLINE Default value to see what compressor to use. 0 is Disabled.
ADVAGG_CSS_COMPRESS_INLINE_IF_NOT_CACHEABLE Default value to if inline compression is used if page is not cacheable.