You are here

function _autofloat_filter in AutoFloat 7.2

Same name and namespace in other branches
  1. 8 autofloat.module \_autofloat_filter()
  2. 7 autofloat.module \_autofloat_filter()

Find a rejected 'div', a selected 'span', a link with image or an image.

1 string reference to '_autofloat_filter'
autofloat_filter_info in ./autofloat.module
Implements hook_filter_info().

File

./autofloat.module, line 84
Autofloat module: A filter that floats images left and right automatically.

Code

function _autofloat_filter($text) {
  $selector = variable_get('autofloat_span', 'flickr-wrap');

  // Divide the variable into two selectors if a comma is found.
  $selector = array_map('trim', explode(',', $selector));

  // Make sure both array keys exist.
  if (isset($selector[0]) == FALSE) {
    $selector[0] = 'non-existing-class-value';
  }
  if (isset($selector[1]) == FALSE) {
    $selector[1] = 'non-existing-class-value';
  }
  $rejector = variable_get('autofloat_div', 'flickr-photoset');

  // Divide the variable into two rejectors if a comma is found.
  $rejector = array_map('trim', explode(',', $rejector));

  // Make sure both array keys exist, if not the regex breaks.
  if (isset($rejector[0]) == FALSE) {
    $rejector[0] = 'non-existing-class-value';
  }
  if (isset($rejector[1]) == FALSE) {
    $rejector[1] = 'non-existing-class-value';
  }
  $count = 0;

  // Load string to DOMDocument.
  $doc = filter_dom_load($text);
  $xpath = new DOMXPath($doc);

  // Iterate through the string neglecting elements nested inside those already
  // processed or those to reject.
  foreach ($xpath
    ->query('//span[not(ancestor-or-self::*[contains(@class, "nofloat")] or ancestor-or-self::*[contains(@class, "' . $rejector[0] . '")] or ancestor-or-self::*[contains(@class, "' . $rejector[1] . '")] or ancestor::span[contains(@class, "float") or contains(@class, "' . $selector[0] . '") or contains(@class, "' . $selector[1] . '")])] | //img[not(ancestor-or-self::*[contains(@class, "nofloat")] or ancestor-or-self::*[contains(@class, "' . $rejector[0] . '")] or ancestor-or-self::*[contains(@class, "' . $rejector[1] . '")] or ancestor::span[contains(@class, "float") or contains(@class, "' . $selector[0] . '") or contains(@class, "' . $selector[1] . '")])]') as $tag) {

    // Start on the left or right depending on the settings.
    if (variable_get('autofloat_start', 0) == 0) {
      $zebra = $count % 2 ? 'even' : 'odd';
    }
    else {
      $zebra = $count % 2 ? 'odd' : 'even';
    }
    $count++;

    // Add an alternating class value but maintain the existing ones.
    $tag
      ->setAttribute('class', 'autofloat-' . $zebra . ' ' . $tag
      ->getAttribute('class'));
  }

  // Convert a DOM object back to an HTML snippet.
  $subject = filter_dom_serialize($doc);
  return $subject;
}