-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathblog-injector.php
103 lines (87 loc) · 3.1 KB
/
blog-injector.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
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
use Grav\Common\Twig\Twig;
class BlogInjectorPlugin extends Plugin
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
// don't continue if this is admin and plugin is disabled for admin
if (!$this->grav['config']->get('plugins.shortcode-core.active_admin') && $this->isAdmin()) {
return;
}
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]);
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Set needed variables to display breadcrumbs.
*/
public function onTwigSiteVariables()
{
$framework = strtolower($this->config->get('plugins.blog-injector.framework'));
if (empty($framework)){
throw new \InvalidArgumentException('The blog "framework" variable value must be defined. At the moment it is empty. If you are overriding the default configuration, please define the "framework" variable with a valid value.');
}
if (!preg_grep("/" . $framework . "/i", array(
"pure",
"bootstrap",
))){
throw new \InvalidArgumentException(sprintf('The blog "framework" variable value must be one of "pure" or "bootstrap". You gave "%s"', $framework));
}
if ($this->config->get('plugins.blog-injector.add_default_css')) {
$this->grav['assets']->add(sprintf('plugin://blog-injector/css/%s_blog.css', $framework));
}
if ($this->config->get('plugins.blog-injector.add_framework_assets')) {
$method = 'add' . ucfirst($framework);
$this->$method();
}
$twig = $this->grav['twig'];
$twig->twig_vars['framework'] = $framework;
$this->setUrls($twig);
}
private function addBootstrap()
{
$this->grav['assets']->add('plugin://blog-injector/vendor/bootstrap/css/bootstrap.min.css', 100);
$this->grav['assets']->add('plugin://blog-injector/vendor/bootstrap/js/bootstrap.min.js', 100);
}
private function addPure()
{
$this->grav['assets']->add('plugin://blog-injector/vendor/pure/grids-min.css', 100);
}
private function setUrls(Twig $twig)
{
$parent = $this->grav['page']->parent();
if (null === $parent) {
$twig->twig_vars['blog_feed_url'] = $twig->twig_vars['blog_base_url'] = '';
return;
}
$baseUrl = $parent->url();
if ($baseUrl == '/') {
$baseUrl = $this->grav['page']->url();
}
$feedUrl = $baseUrl;
$twig->twig_vars['blog_base_url'] = $baseUrl;
$twig->twig_vars['blog_feed_url'] = $feedUrl;
}
}