-
Notifications
You must be signed in to change notification settings - Fork 5
/
unitegallery.php
86 lines (72 loc) · 2.42 KB
/
unitegallery.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
<?php
namespace Grav\Plugin;
use Grav\Common\Grav;
use Grav\Common\Plugin;
class UniteGalleryPlugin extends Plugin
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
public function onPluginsInitialized()
{
// Don't proceed if we are in the admin plugin
if ($this->isAdmin()) {
return;
}
$this->enable([
'onTwigExtensions' => ['onTwigExtensions', 0],
'onPageInitialized' => ['onPageInitialized', 0],
]);
}
public function onTwigExtensions()
{
require_once(__DIR__ . '/twig/unitegallery_extension.php');
$this->grav['twig']->twig->addExtension(new \UniteGalleryTwigExtension());
}
public function onPageInitialized()
{
$page = $this->grav['page'];
$config = $this->mergeConfig($page);
if (!$config->get('assets_in_meta', true))
return;
$meta = [];
// Initialize all page content up front before Twig happens
if (isset($page->header()->content['items'])) {
foreach ($page->collection() as $item) {
$item->content();
$item_meta = $item->getContentMeta('unitegallery_assets');
if ($item_meta) {
$meta = array_merge_recursive($meta, $item_meta);
}
}
}
$page->content();
// get the meta and check for assets
$page_meta = $page->getContentMeta('unitegallery_assets');
if ($page_meta) {
$meta = array_merge_recursive($meta, $page_meta);
}
if (empty($meta)) {
$gallery_theme = $config->get('gallery_theme', "default");
$assets_path = 'plugin://unitegallery/vendor/unitegallery/';
$theme_assets_prefix = $assets_path . 'themes/' . $gallery_theme . '/ug-theme-'. $gallery_theme;
$meta['js'] = [$assets_path . 'js/unitegallery.min.js', $theme_assets_prefix . '.js'];
$meta['css'] = [$assets_path . 'css/unite-gallery.css', $theme_assets_prefix . '.css'];
$page->addContentMeta('unitegallery_assets', $meta);
}
if (!empty($meta)) {
foreach ($meta['js'] as $js) {
$this->grav['assets']->addJs($js);
}
foreach ($meta['css'] as $css) {
$this->grav['assets']->addCss($css);
}
}
}
}