1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Disqus Helper package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Nikola Posa <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For full copyright and license information, please refer to the LICENSE file, |
8
|
|
|
* located at the package root folder. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DisqusHelper\Widget; |
12
|
|
|
|
13
|
|
|
use DisqusHelper\Code; |
14
|
|
|
use DisqusHelper\Exception\RuntimeException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Comments count widget. |
18
|
|
|
* |
19
|
|
|
* @author Nikola Posa <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class CommentsCountWidget extends BaseWidget |
22
|
|
|
{ |
23
|
|
|
const SCRIPT_NAME = 'count.js'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $defaultOptions = [ |
29
|
|
|
'url' => null, |
30
|
|
|
'label' => null, |
31
|
|
|
'as_link' => true, |
32
|
|
|
'identifier' => null, |
33
|
|
|
]; |
34
|
|
|
|
35
|
5 |
|
public function render(array $options = []) : string |
36
|
|
|
{ |
37
|
5 |
|
$options = array_merge(self::$defaultOptions, $options); |
38
|
|
|
|
39
|
5 |
|
$label = htmlspecialchars((string) $options['label'], ENT_QUOTES, 'UTF-8'); |
40
|
|
|
|
41
|
5 |
|
$attribs = []; |
42
|
|
|
|
43
|
5 |
|
if (isset($options['identifier'])) { |
44
|
2 |
|
$attribs['data-disqus-identifier'] = $options['identifier']; |
45
|
|
|
} |
46
|
|
|
|
47
|
5 |
|
if ($options['as_link']) { |
48
|
4 |
|
if (empty($options['url'])) { |
49
|
1 |
|
throw new RuntimeException("URL option is missing for the Comments count widget"); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
return $this->renderLink($options['url'] . '#disqus_thread', $label, $attribs); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
if (!empty($options['url'])) { |
56
|
1 |
|
$attribs['data-disqus-url'] = $options['url']; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return $this->renderElement($label, $attribs); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
private function renderLink(string $href, string $label, array $attribs) : string |
63
|
|
|
{ |
64
|
3 |
|
return '<a href="' . $href . '"' |
65
|
3 |
|
. ' ' . $this->htmlAttribsToString($attribs) . '>' |
66
|
3 |
|
. $label |
67
|
3 |
|
. '</a>'; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
private function renderElement(string $label, array $attribs) : string |
71
|
|
|
{ |
72
|
|
|
return '<span class="disqus-comment-count"' |
73
|
1 |
|
. ' ' . $this->htmlAttribsToString($attribs) . '>' |
74
|
1 |
|
. $label |
75
|
1 |
|
. '</span>'; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function visit(Code $code) : Code |
79
|
|
|
{ |
80
|
1 |
|
$code->addScriptFile(self::SCRIPT_NAME); |
81
|
|
|
|
82
|
1 |
|
return $code; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|