-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcustom_links.php
109 lines (105 loc) · 3.24 KB
/
custom_links.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* 友链
*
* @package custom
*/
?>
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php
$this->need('header.php');
class Custom_Links
{
private $count;
private $lazyLoad;
private $srcSelector;
private $gravatar;
public function __construct($content)
{
require_once 'assets/php/phpQuery.php';
$options = Helper::options();
$this->gravatar = preg_replace('/\/$/','',$options->gravatar).'/';
$this->lazyLoad = in_array('lazyload', $options->advanced) ? getLazyLoadImg() : false;
$this->srcSelector = $this->lazyLoad ? 'data-original' : 'img';
$pq = phpQuery::newDocumentHTML($content);
$pq->find('ul')->each(array($this, 'ul'));
echo $pq->html();
}
public function ul($dom)
{
$pq = pq($dom);
$this->count = null;
$pq->children()->each(array($this, 'li'));
if ($this->count > 0) {
$pq->addClass('links-group');
}
}
public function li($dom)
{
if ($this->count === null) {
$this->count = 0;
} elseif ($this->count === 0) {
return;
}
$pq = pq($dom);
$children = $pq->children();
$length = $children->length;
if ($length != 2) {
return;
}
$a = pq($children->get(0));
if ($a->length != 1) {
return;
}
$img = $a->is('img') ? $a : $a->find('img');
if ($img->length != 1) {
return;
}
$src = $img->attr('src');
if ($src && str_starts_with($src, 'mailto:')) {
$src = substr($src, 7);
}
$desc = $img->attr('alt');
$a = pq($children->get(1));
if ($a->length != 1) {
return;
}
$name = $a->text();
$url = $a->attr('href');
++$this->count;
if (!empty($desc)) {
$desc = "title=\"$desc\"";
}
if (!filter_var($src,FILTER_VALIDATE_URL)) {
if (filter_var($src,FILTER_VALIDATE_EMAIL))
$src = $this->gravatar . md5(strtolower($src)) . '?s=100';
else if (is_md5($src) || empty($src))
$src = $this->gravatar . strtolower($src) . '?s=100';
else if (preg_match('/^\d{5,}$/', $src) !== false)
$src = "https://q.qlogo.cn/g?b=qq&nk=$src&s=100";
}
if ($this->lazyLoad) {
$src = "src=\"$this->lazyLoad\" data-original=\"$src\"";
} else {
$src = "src=\"$src\"";
}
$html = <<<HTML
<a target="_blank" href="$url" $desc><div class="links-body"><img $src alt="$name"><p>$name</p></div></a>
HTML;
$pq->wrap($html);
}
}
?>
<div class="col-mb-12 col-8" id="main" role="main">
<article class="post" itemscope itemtype="http://schema.org/BlogPosting">
<h1 class="post-title" itemprop="name headline"><a itemprop="url" href="<?php $this->permalink() ?>"><?php $this->title() ?></a></h1>
<div class="post-content" itemprop="articleBody">
<div class="links">
<?php new Custom_Links($this->content); ?>
</div>
</div>
</article>
<?php $this->need('comments.php'); ?>
</div><!-- end #main-->
<?php $this->need('sidebar.php'); ?>
<?php $this->need('footer.php'); ?>