You are here

function word_link_convert_text in Word Link 7.2

Find and convert defined word to link.

Parameters

string $text: Input text.

array $settings: Array of filter settings.

Return value

string String with converted words.

1 call to word_link_convert_text()
_word_link_process in ./word_link.module
Helper function for filter process.

File

./word_link.module, line 280

Code

function word_link_convert_text($text, $settings) {
  global $base_url;

  // Get current path. We need this to verify
  // if word will be converted on this page.
  $current_path = current_path();
  $args = arg();

  // Check if current node belongs to content type
  // in which need to convert words.
  // @todo: Need to check when node shows on another path.
  if (isset($args[0]) && $args[0] == 'node' && isset($args[1])) {
    $type = word_link_get_node_type($args[1]);
    if (empty($settings['word_link_content_types'][$type])) {
      return $text;
    }
  }

  // Get array of words.
  $words = word_link_load_all();

  // Exit if there are no words to replace.
  if (empty($words)) {
    return $text;
  }

  // Default HTML tag used in theme.
  $tag = 'a';

  // Get disallowed html tags and convert it for Xpath.
  if (!empty($settings['word_link_tags_except'])) {
    $disallowed =& drupal_static('word_link_disallowed_tags');
    if (!isset($disallowed)) {
      $disallowed_tags = preg_split('/\\s+|<|>/', $settings['word_link_tags_except'], -1, PREG_SPLIT_NO_EMPTY);
      $disallowed = array();
      foreach ($disallowed_tags as $ancestor) {
        $disallowed[] = 'and not(ancestor::' . $ancestor . ')';
      }
      $disallowed = implode(' ', $disallowed);
    }
  }
  else {
    $disallowed = '';
  }

  // Create pattern.
  $patterns =& drupal_static('word_link_patterns');
  if (!isset($patterns)) {
    $path = drupal_strtolower(drupal_get_path_alias());
    $pattern = array();
    foreach ($words as $word) {
      $url = str_replace($base_url . '/', '', $word->url);
      $url = drupal_get_path_alias($url);
      $match = FALSE;

      // Check if current path matches word except path.
      if (!empty($word->except_list)) {
        $match = drupal_match_path($path, $word->except_list);
        if ($path != $current_path) {
          $match = $match || drupal_match_path($current_path, $word->except_list);
        }
      }

      // Get visibility status and check if need to convert word on this page.
      $visibility = empty($word->except_list) || !isset($word->visibility) ? FALSE : $word->visibility;
      if ($url != $path && !$match && !$visibility || $url != $path && $visibility && $match) {
        $text_lower = preg_replace('/\\s+/', ' ', trim($word->text_lower));
        $pattern[] = preg_replace('/ /', '\\s+', preg_quote($text_lower, '/'));
      }
    }

    // Chunk pattern string to avoid preg_match_all() limits.
    $patterns = array();
    foreach (array_chunk($pattern, 1000, TRUE) as $pattern_chunk) {
      if ($settings['word_link_boundary']) {
        $patterns[] = '/(?<=)(' . implode('|', $pattern_chunk) . ')/ui';
      }
      else {
        $patterns[] = '/((\\b)|(?<=))(' . implode('|', $pattern_chunk) . ')\\b/ui';
      }
    }
  }
  foreach ($patterns as $pattern) {
    word_link_convert_text_recursively($text, $pattern, $words, $disallowed, $settings, $tag);
  }
  return $text;
}