You are here

function elf_replace in External Links Filter 7.3

Same name and namespace in other branches
  1. 5.3 elf.module \elf_replace()
  2. 5 elf.module \elf_replace()
  3. 5.2 elf.module \elf_replace()
  4. 6.3 elf.module \elf_replace()
  5. 6.2 elf.module \elf_replace()

Hook_filter_info() process callback.

See also

elf_filter_info()

1 string reference to 'elf_replace'
elf_filter_info in ./elf.module
Implement hook_filter().

File

./elf.module, line 133
Adds an icon to external and mailto links.

Code

function elf_replace($text, stdClass $filter) {
  $document = filter_dom_load($text);
  $links = $document
    ->getElementsByTagName('a');
  foreach ($links as $a) {
    if ($href = $a
      ->getAttribute('href')) {

      // This is a mailto link.
      if (strpos($href, 'mailto:') === 0) {
        $a
          ->setAttribute('class', $a
          ->getAttribute('class') ? $a
          ->getAttribute('class') . ' elf-mailto elf-icon' : 'elf-mailto elf-icon');
      }
      elseif (elf_url_external($href)) {

        // Add external class.
        $a
          ->setAttribute('class', $a
          ->getAttribute('class') ? $a
          ->getAttribute('class') . ' elf-external elf-icon' : 'elf-external elf-icon');
        if ($a
          ->getElementsByTagName('img')->length > 0) {
          $a
            ->setAttribute('class', $a
            ->getAttribute('class') ? $a
            ->getAttribute('class') . ' elf-img' : 'elf-img');
        }

        // Add nofollow.
        $nofollow = $filter->settings['elf_nofollow'];
        if ($nofollow) {
          $rel = array_filter(explode(' ', $a
            ->getAttribute('rel')));
          if (!in_array('nofollow', $rel)) {
            $rel[] = 'nofollow';
            $a
              ->setAttribute('rel', implode(' ', $rel));
          }
        }

        // Add redirect.
        if (variable_get('elf_redirect', FALSE)) {
          $redirect_url = elf_get_redirect_url($a
            ->getAttribute('href'));
          $a
            ->setAttribute('href', $redirect_url);
        }
      }
    }
  }
  return filter_dom_serialize($document);
}