You are here

function _emogrifier_process in Emogrifier 7.2

Same name and namespace in other branches
  1. 8 emogrifier.module \_emogrifier_process()
  2. 6 emogrifier.module \_emogrifier_process()
  3. 7 emogrifier.module \_emogrifier_process()

Implements hook_filter_FILTER_process().

See also

emogrifier_filter_info()

1 string reference to '_emogrifier_process'
emogrifier_filter_info in ./emogrifier.module
Implements hook_filter_info().

File

./emogrifier.module, line 49
This module uses the emogrifier class library as an input filter to convert stylesheet rules to inline style attributes. This ensures proper display on email and mobile device readers that lack stylesheet support.

Code

function _emogrifier_process($text, $filter, $format, $langcode, $cache, $cache_id) {
  if (empty($text)) {
    return '';
  }

  // Avoid PHP fatal errors when the 'dom' extension is not loaded.
  if (!extension_loaded('dom')) {
    watchdog('emogrifier', 'The PHP <a href="!dom">%dom</a> extension required by <a href="!emogrifier">%emogrifier</a> is not loaded.', array(
      '!dom' => url('http://php.net/dom'),
      '%dom' => 'dom',
      '!emogrifier' => url('http://drupal.org/project/emogrifier'),
      '%emogrifier' => 'Emogrifier',
    ), WATCHDOG_WARNING);
    return $text;
  }
  if (!_emogrifier_available()) {
    watchdog('emogrifier', 'The <a href="!library">%emogrifier</a> class library required by the <a href="!module">%emogrifier</a> module could not be loaded.', array(
      '!library' => url('http://www.pelagodesign.com/sidecar/emogrifier/'),
      '%emogrifier' => 'emogrifier',
      '!module' => url('http://drupal.org/project/emogrifier'),
    ), WATCHDOG_WARNING);
    return $text;
  }
  $emogrifier = new \Pelago\Emogrifier($text);
  $css = '';

  // Add external css from settings, add it to $urls to be parsed.
  if (isset($filter->settings['external_css']) && !empty($filter->settings['external_css'])) {
    $stylesheets = explode('\\n', str_replace('\\r', '', $filter->settings['external_css']));
    foreach ($stylesheets as $stylesheet) {

      // Check if file exists to avoid warning.
      if (file_exists($stylesheet)) {
        $css = file_get_contents($stylesheet);
      }
    }
  }

  // Add inline css from settings, add it to $styles to be parsed.
  if (isset($filter->settings['inline_css']) && !empty($filter->settings['inline_css'])) {
    $css .= $filter->settings['inline_css'];
  }
  if (!empty($css)) {
    @$emogrifier
      ->setCss($css);
  }
  if (isset($filter->settings['disable_styles_block_parsing']) && !empty($filter->settings['disable_styles_block_parsing'])) {
    @$emogrifier
      ->disableStyleBlocksParsing();
  }
  if (isset($filter->settings['disable_inline_styles_parsing']) && !empty($filter->settings['disable_inline_styles_parsing'])) {
    @$emogrifier
      ->disableInlineStyleAttributesParsing();
  }
  if (isset($filter->settings['disable_invisible_node_removal']) && !empty($filter->settings['disable_invisible_node_removal'])) {
    @$emogrifier
      ->disableInvisibleNodeRemoval();
  }
  if (isset($filter->settings['enable_css_to_html_mapping']) && !empty($filter->settings['enable_css_to_html_mapping'])) {
    @$emogrifier
      ->enableCssToHtmlMapping();
  }
  if (isset($filter->settings['add_allowed_media_type']) && !empty($filter->settings['add_allowed_media_type'])) {
    $media_types = explode(',', $filter->settings['add_allowed_media_type']);
    foreach ($media_types as $media_type) {
      @$emogrifier
        ->addAllowedMediaType(trim($media_type));
    }
  }
  if (isset($filter->settings['remove_allowed_media_type']) && !empty($filter->settings['remove_allowed_media_type'])) {
    foreach ($filter->settings['remove_allowed_media_type'] as $media_type) {
      @$emogrifier
        ->removeAllowedMediaType($media_type);
    }
  }
  if (isset($filter->settings['add_excluded_selector']) && !empty($filter->settings['add_excluded_selector'])) {
    $excluded_selectors = explode(',', $filter->settings['add_excluded_selector']);
    foreach ($excluded_selectors as $excluded_selector) {
      @$emogrifier
        ->addExcludedSelector(trim($excluded_selector));
    }
  }
  $content = @$emogrifier
    ->emogrify();

  // Emogrifier has a problem with [ and ] inside quotes.
  // This stops tokens inside of href and src attributes from being processed.
  // See https://github.com/jjriv/emogrifier/issues/98.
  $content = str_replace('%5B', '[', $content);
  $content = str_replace('%5D', ']', $content);
  return $content;
}