-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPipitTemplateFilter_externallinks.class.php
91 lines (61 loc) · 2.48 KB
/
PipitTemplateFilter_externallinks.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* Perch template filter for adding target and rel attributes to external links
*
* @package PipitTemplateFilter_externallinks
* @version v1.0.1
* @link https://github.com/Pipits/externallinks-filter
*/
class PipitTemplateFilter_externallinks extends PerchTemplateFilter {
public $returns_markup = true;
public function filterBeforeProcessing($value, $valueIsMarkup = false) {
if(defined('SITE_URL')) {
$site_url = SITE_URL;
} else {
$API = new PerchAPI(1.0, 'content');
$Settings = $API->get('Settings');
$site_url = $Settings->get('siteURL')->val();
}
// DOMDocument->loadHTML() automatically adds html/body tags.
// Using the LIBXML_HTML_NOIMPLIED has a negative impact
$value = '<html data-remove><body data-remove>' . $value . '</html></body>';
$doc = new DOMDocument('1.0', 'UTF-8');
libxml_use_internal_errors(true);
$doc->loadHTML(mb_convert_encoding($value, 'HTML-ENTITIES', 'UTF-8'));
$xpath = new DOMXPath($doc);
$result = $xpath->query('//a');
foreach($result as $node) {
$href = $node->getAttribute('href');
if($href && $this->is_external($href, $site_url)) {
$target = $node->getAttribute('target');
$rel = $node->getAttribute('rel');
if(!$target && $this->Tag->linkstarget()) {
$node->setAttribute("target", $this->Tag->linkstarget());
}
if(!$rel && $this->Tag->linksrel()) {
$node->setAttribute("rel", $this->Tag->linksrel());
}
}
}
libxml_clear_errors();
$output = $doc->saveHTML($doc->documentElement);
$output = str_replace(['<html data-remove><body data-remove>', '</body></html>'], '', $output);
return $output;
}
public function is_external($link, $site_url) {
$site_domain = $this->get_domain($site_url);
$link_domain = $this->get_domain($link);
if($link_domain && $site_domain !== $link_domain) {
return true;
}
return false;
}
public function get_domain($url) {
$parts = parse_url($url);
if(isset($parts['host'])) {
return $parts['host'];
}
return false;
}
}
PerchSystem::register_template_filter('externallinks', 'PipitTemplateFilter_externalLinks');