-
Notifications
You must be signed in to change notification settings - Fork 0
/
disqus.php
71 lines (62 loc) · 1.76 KB
/
disqus.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
<?php
use Herbie\DI;
use Herbie\Hook;
class DisqusPlugin
{
/** @var \Herbie\Config */
public static $config;
/**
* Initialize plugin
*/
public static function install()
{
self::$config = DI::get('Config');
if ((bool)self::$config->get('plugins.config.disqus.twig', false)) {
Hook::attach('twigInitialized', ['DisqusPlugin', 'twigInitialized']);
}
if ((bool)self::$config->get('plugins.config.disqus.shortcode', true)) {
Hook::attach('shortcodeInitialized', ['DisqusPlugin', 'shortcodeInitialized']);
}
}
/**
* @param $twig
*/
public static function twigInitialized($twig)
{
$simpleFunction = new Twig_SimpleFunction('disqus', ['DisqusPlugin', 'disqusTwig'], ['is_safe' => ['html']]);
$twig->addFunction($simpleFunction);
}
/**
* @param $shortcode
*/
public static function shortcodeInitialized($shortcode)
{
$shortcode->add('disqus', ['DisqusPlugin', 'disqusShortcode']);
}
/**
* @param string $shortname
* @return string
*/
public static function disqusTwig($shortname)
{
$template = self::$config->get(
'plugins.config.disqus.template',
'@plugin/disqus/templates/disqus.twig'
);
return DI::get('Twig')->render($template, [
'shortname' => $shortname
]);
}
/**
* @param array $options
* @return string
*/
public static function disqusShortcode($options)
{
$options = array_merge([
'shortname' => empty($options[0]) ? '' : $options[0],
], $options);
return call_user_func_array(['DisqusPlugin', 'disqusTwig'], $options);
}
}
DisqusPlugin::install();