-
Notifications
You must be signed in to change notification settings - Fork 12
/
gravstrap.php
152 lines (134 loc) · 4.65 KB
/
gravstrap.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* This file is part of the Gravstrap plugin and it is distributed
* under the MIT License. To use this application you must leave
* intact this copyright notice.
*
* Copyright (c) Giansimon Diblas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* For extra documentation and help please visit http://diblas.net
*
* @license MIT License
*
*/
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Gravstrap\Twig\GravstrapTwigExtension;
use Grav\Common\Page\Page;
class GravstrapPlugin extends Plugin
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
'onTwigExtensions' => ['onTwigExtensions', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onPageInitialized' => ['onPageInitialized', 0],
];
}
/**
* Initializes the shortcodes
*/
public function onShortcodeHandlers()
{
require_once(__DIR__.'/classes/Base/GravstrapShortcode.php');
require_once(__DIR__.'/classes/Base/RegisteredShortcodes.php');
require_once(__DIR__.'/classes/Twig/GravstrapTwigExtension.php');
$this->grav["shortcode"]->registerAllShortcodes(__DIR__.'/shortcodes/Basic');
$this->grav["shortcode"]->registerAllShortcodes(__DIR__.'/shortcodes/Bootstrap');
$this->grav["shortcode"]->registerAllShortcodes(__DIR__.'/shortcodes/Footer');
$this->grav["shortcode"]->registerAllShortcodes(__DIR__.'/shortcodes/Modules');
$this->grav["shortcode"]->registerAllShortcodes(__DIR__.'/shortcodes/Misc');
$this->grav["shortcode"]->registerAllShortcodes(__DIR__.'/shortcodes/Vendor');
}
/**
* Processes the page when it has children. This allows to use shortcodes in a page who has children.
*/
public function onPageInitialized()
{
$page = $this->grav['page'];
if (isset($page->header()->content['items'])) {
$page->content();
}
}
/**
* Adds Twig extension
*/
public function onTwigExtensions()
{
require_once(__DIR__ . '/classes/Twig/GravstrapTwigExtension.php');
$this->grav['twig']->twig->addExtension(new GravstrapTwigExtension());
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates/modules';
}
/**
* Assigns not rendered shortcodes to twig variables; adds modular pages assets; parses common page
*/
public function onTwigSiteVariables()
{
$page = $this->grav['page'];
$this->pageShortcodesToTwigVariable($page);
$this->manageModularPage($page);
$page = $page->find('/common');
if (null === $page) {
return;
}
$page->content();
$this->pageShortcodesToTwigVariable($page);
$this->addAssets($page);
}
private function pageShortcodesToTwigVariable(Page $page)
{
$contentMeta = $page->getContentMeta('shortcodeMeta');
if (null === $contentMeta || ! array_key_exists("shortcode", $contentMeta) || null === $shortcodes = $contentMeta["shortcode"]) {
return;
}
foreach($shortcodes as $typedShortcodes) {
foreach($typedShortcodes as $name => $content) {
$this->grav['twig']->twig_vars[$name] = $content;
}
}
}
private function manageModularPage(Page $page)
{
$children = $page->collection();
foreach ($children as $child) {
if (!$child->modular()) {
return;
}
$this->addAssets($child);
}
}
private function addAssets(Page $page)
{
// get the meta and check for assets
$meta = $page->getContentMeta('shortcodeMeta');
if (!isset($meta['shortcodeAssets'])) {
return;
}
$assets = (array) $meta['shortcodeAssets'];
foreach($assets as $type => $asset) {
switch ($type) {
case 'css':
$this->grav["assets"]->addCss($asset);
break;
case 'js':
$this->grav["assets"]->addJs($asset);
break;
}
}
}
}