diff --git a/shurly.module b/shurly.module index 9e7da16..521a134 100644 --- a/shurly.module +++ b/shurly.module @@ -984,3 +984,54 @@ function _shurly_language_stub() { } return $language; } + +/** + * Implementation of hook_filter(). + */ +function shurly_filter($op, $delta = 0, $format = -1, $text = '') { + switch ($op) { + case 'list': + return array(0 => t("Shorten all outgoing URL's")); + + case 'description': + return t('Shorten all outgoing URL\'s.'); + + case 'settings': + break; + + case 'no cache': + break; + + case 'prepare': + return $text; + + case 'process': + return _shurly_filter_process($text); + + default: + return $text; + } +} + +/** + * Process callback for shurly filter. + */ +function _shurly_filter_process($text) { + // Find all a tags containing a full URL. + preg_match_all('/]*href="(http[^"]*)"[^>]*>/i', $text, $links); + if (!empty($links)) { + $links = $links[1]; + foreach ($links as $key => $link) { + $short_url = shurly_shorten($link); + $text = str_replace('"' . $link . '"', '"' . $short_url['shortUrl'] . '"', $text); + } + } + return $text; +} + +/** + * Implements hook_filter_tips(). + */ +function shurly_filter_tips($filter, $format, $long = FALSE) { + return t('All links starting with http or https will be replaced.'); +}