Completed
Push — master ( 6e2144...8f0765 )
by Nazar
04:26
created

Comments::block()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
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
	 * TODO: Count also render in WebComponent
34
	 *
35
	 * @param int    $item   Item id
36
	 * @param string $module Module name
37
	 *
38
	 * @return string HTML snipped that will be replaced with actual count on frontend
39
	 */
40
	function count ($item, $module) {
41
		if (!$this->shortname) {
42
			return '';
43
		}
44
		$this->count_js();
45
		Page::instance()->js(
46
			"disqus_count_items.push('".str_replace("'", "'", "$module/$item")."');",
47
			'code'
48
		);
49
		return h::{'span.cs-disqus-comments-count'}([
50
			'data-identifier'=> "$module/$item"
51
		]);
52
	}
53
	protected function count_js () {
54
		static	$added	= false;
55
		if ($added) {
56
			return;
57
		}
58
		$added		= true;
59
		Page::instance()->js(
60
			"var disqus_shortname = '$this->shortname';
61
if (!window.disqus_count_items) { window.disqus_count_items = []; }",
62
			'code'
63
		);
64
	}
65
}
66