You are here

function advagg_mod_admin_test_css_files in Advanced CSS/JS Aggregation 7.2

Test all CSS files seeing if any string translations do anything.

Return value

array An array with the filename key and the before => after translation value.

1 call to advagg_mod_admin_test_css_files()
advagg_mod_admin_settings_form in advagg_mod/advagg_mod.admin.inc
Form builder; Configure advagg settings.

File

advagg_mod/advagg_mod.admin.inc, line 719
Admin page callbacks for the advagg bundler module.

Code

function advagg_mod_admin_test_css_files() {
  $output = array();

  // Get list of files.
  $query_files = db_select('advagg_files', 'af')
    ->fields('af', array(
    'filename_hash',
    'filename',
  ))
    ->condition('filetype', 'css')
    ->orderBy('filename', 'ASC')
    ->execute()
    ->fetchAllKeyed();
  $files = array_values($query_files);

  // Exit if no files were found.
  if (empty($files)) {
    return $output;
  }
  foreach ($files as $filename) {

    // Skip missing files.
    if (!file_exists($filename)) {
      continue;
    }

    // Load CSS file.
    $file_contents = advagg_load_stylesheet_content((string) @advagg_file_get_contents($filename), TRUE);

    // Code taken from drupal_load_stylesheet_content().
    // Regexp to match double quoted strings.
    $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';

    // Regexp to match single quoted strings.
    $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";

    // Extract all content inside of quotes.
    $css_content_pattern = "/content:.*?({$double_quot}|{$single_quot}|(\\;|\\})).*?(?:\\;|\\})/";

    // Get strings inside of quotes of the content attribute.
    preg_match_all($css_content_pattern, $file_contents, $matches);

    // Skip if no matches.
    if (empty($matches[1])) {
      continue;
    }
    foreach ($matches[1] as $value) {

      // Skip if equal to ; or }.
      if ($value === ';' || $value === '}') {
        continue;
      }

      // Remove quotes for t function.
      $before = substr($value, 1, -1);

      // Only test if it contains A-Za-z.
      if (!preg_match('/[A-Za-z]/', $before)) {
        continue;
      }

      // Only test if it contains characters other than unicode.
      $css_unicode_pattern = '/\\\\[0-9a-fA-F]{1,6}(?:\\r\\n|[ \\t\\r\\n\\f])?/';
      $unicode_removed = preg_replace($css_unicode_pattern, '', $before);
      if (empty($unicode_removed)) {
        continue;
      }

      // Run t function.
      // @codingStandardsIgnoreLine
      $after = t($before);

      // Only include it if strings are different.
      if ($before !== $after) {
        if (!isset($output[$filename])) {
          $output[$filename] = '';
        }
        $output[$filename] .= $before . ' => ' . $after;
      }
    }
  }
  return $output;
}