function _gotwo_link in Go - url redirects 7
Process callback for the GO filter.
This function will load the text into a DOM object, search for <go> elements, saves non-existent href's and title's to the {gowto} table and replace the <go> tags with it's real <a> links. If defined in the <go> link the href is altered as requested. The full text with all links replaced is returned.
Parameters
$text: A raw text with all <go> links to be processed.
Return value
A raw text with all <go> links replaced with real <a> links.
1 string reference to '_gotwo_link'
- gotwo_filter_info in ./
gotwo.module - Implements hook_filter_info().
File
- ./
gotwo.module, line 193 - Module that provides easy to use redirection links. A redirection link would be like: http://examples.org/go/a_label http://examples.org/go/123546 http://examples.org/go/or/like/this
Code
function _gotwo_link($text, $filter) {
$html_dom = filter_dom_load($text);
$goNodesToRemove = array();
foreach ($html_dom
->getElementsByTagName('go') as $goNode) {
// Clone <go> link to <a> link and keep all attributes intact.
$linkNode = $goNode->ownerDocument
->createElement('a');
if ($goNode->attributes->length) {
foreach ($goNode->attributes as $attribute) {
$linkNode
->setAttribute($attribute->nodeName, $attribute->nodeValue);
}
}
// Clone all <go> child nodes inside the new <a> tag.
while ($goNode
->hasChildNodes()) {
$childNodes = $goNode->childNodes;
$linkNode
->appendChild($childNodes
->item(0));
}
// Verify if the url exists in the {gotwo} table. If the url is missing,
// add the url with link title to the {gotwo} table.
$href = $goNode
->getAttribute('href');
$title = $goNode
->getAttribute('title');
if (!empty($href)) {
$linkNode
->setAttribute('href', _gotwo_get_url($href, empty($title) ? NULL : $title));
}
// Insert the new $linkNode before the previous $goNode.
$goNode->parentNode
->insertBefore($linkNode, $goNode);
// Save $goNode to remove array. We cannot remove the child here or the DOM
// index will be cluttered and every second <go> link is not replacement.
$goNodesToRemove[] = $goNode;
}
// Now we are able to remove the child's without loosing the index.
foreach ($goNodesToRemove as $goNode) {
$goNode->parentNode
->removeChild($goNode);
}
$text = filter_dom_serialize($html_dom);
return trim($text);
}