1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Disqus |
4
|
|
|
* @category modules |
5
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2013-2016, Nazar Mokrynskyi |
7
|
|
|
* @license MIT License, see license.txt |
8
|
|
|
*/ |
9
|
|
|
namespace cs\modules\Disqus; |
10
|
|
|
use |
11
|
|
|
h, |
12
|
|
|
cs\Config, |
13
|
|
|
cs\Page, |
14
|
|
|
cs\Request, |
15
|
|
|
cs\Singleton; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @method static $this instance($check = false) |
19
|
|
|
*/ |
20
|
|
|
class Comments { |
21
|
|
|
use Singleton; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $shortname; |
27
|
|
|
|
28
|
|
|
protected function construct () { |
29
|
|
|
$this->shortname = Config::instance()->module('Disqus')->shortname; |
30
|
|
|
} |
31
|
|
|
/** |
32
|
|
|
* Count of comments for specified item |
33
|
|
|
* |
34
|
|
|
* @param int $item Item id |
35
|
|
|
* @param string $module Module name |
36
|
|
|
* |
37
|
|
|
* @return string HTML snipped that will be replaced with actual count on frontend |
38
|
|
|
*/ |
39
|
|
|
function count ($item, $module) { |
40
|
|
|
if (!$this->shortname) { |
41
|
|
|
return ''; |
42
|
|
|
} |
43
|
|
|
$this->count_js(); |
44
|
|
|
Page::instance()->js( |
45
|
|
|
"disqus_count_items.push('".str_replace("'", "'", "$module/$item")."');", |
46
|
|
|
'code' |
47
|
|
|
); |
48
|
|
|
return h::{'span.cs-disqus-comments-count'}([ |
49
|
|
|
'data-identifier'=> "$module/$item" |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
/** |
53
|
|
|
* Get comments block with comments tree and comments sending form |
54
|
|
|
* |
55
|
|
|
* @param int $item Item id |
56
|
|
|
* @param string $module Module name |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
function block ($item, $module) { |
61
|
|
|
if (!$this->shortname) { |
62
|
|
|
return ''; |
63
|
|
|
} |
64
|
|
|
$this->block_js($item, $module); |
65
|
|
|
return '<div id="disqus_thread"></div>'; |
66
|
|
|
} |
67
|
|
|
protected function count_js () { |
68
|
|
|
static $added = false; |
69
|
|
|
if ($added) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
$added = true; |
73
|
|
|
Page::instance()->js( |
74
|
|
|
"var disqus_shortname = '$this->shortname'; |
75
|
|
|
if (!window.disqus_count_items) { window.disqus_count_items = []; }", |
76
|
|
|
'code' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
protected function block_js ($item, $module) { |
80
|
|
|
static $added = false; |
81
|
|
|
if ($added) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
$added = true; |
85
|
|
|
Page::instance()->js( |
86
|
|
|
"var disqus_shortname = '$this->shortname', disqus_identifier = '".str_replace("'", "'", "$module/$item")."';", |
87
|
|
|
'code' |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|