-
Notifications
You must be signed in to change notification settings - Fork 16
/
bootstrapper.php
103 lines (91 loc) · 3.26 KB
/
bootstrapper.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\Grav;
use \Grav\Common\Page\Page;
class BootstrapperPlugin extends Plugin
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onThemeInitialized' => ['onThemeInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onThemeInitialized()
{
if ($this->isAdmin()) {
return;
}
$load_events = false;
// if not always_load see if the theme expects to load bootstrap plugin
if (!$this->config->get('plugins.bootstrapper.always_load')) {
$theme = $this->grav['theme'];
if (isset($theme->load_bootstrapper_plugin) && $theme->load_bootstrapper_plugin) {
$load_events = true;
}
} else {
$load_events = true;
}
if ($load_events) {
$this->enable([
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]);
}
}
/**
* if enabled on this page, load the JS + CSS and set the selectors.
*/
public function onTwigSiteVariables()
{
$config = $this->config->get('plugins.bootstrapper');
$version = $config['version'];
$mode = $config['mode'] == 'production' ? '.min' : '';
$bootstrap_bits = [];
if ($version == 'v5') {
$currentVersion = '5.0.0-beta3';
} elseif ($version == 'v4') {
$currentVersion = '4.6.0';
} else {
$currentVersion = '3.4.1';
}
$bootstrapCDN = "https://cdn.jsdelivr.net/npm/bootstrap@{$currentVersion}/dist";
if ($config['use_cdn']) {
if ($config['load_core_css']) {
$bootstrap_bits[] = "{$bootstrapCDN}/css/bootstrap{$mode}.css";
}
if ($config['load_theme_css'] && $version === 'v3') {
$bootstrap_bits[] = "{$bootstrapCDN}/css/bootstrap-theme{$mode}.css";
}
if ($config['load_popper_js'] && ($version == 'v4' || $version === 'v5')) {
$bootstrap_bits[] = "{$bootstrapCDN}/js/bootstrap.bundle{$mode}.js";
} else {
if ($config['load_core_js']) {
$bootstrap_bits[] = "{$bootstrapCDN}/js/bootstrap{$mode}.js";
}
}
} else {
if ($config['load_core_css']) {
$bootstrap_bits[] = "plugin://bootstrapper/css/{$version}/bootstrap{$mode}.css";
}
if ($config['load_theme_css'] && $version === 'v3') {
$bootstrap_bits[] = "plugin://bootstrapper/css/{$version}/bootstrap-theme{$mode}.css";
}
if ($config['load_popper_js'] && ($version === 'v4' || $version === 'v5')) {
$bootstrap_bits[] = "plugin://bootstrapper/js/{$version}/bootstrap.bundle{$mode}.js";
} else {
if ($config['load_core_js']) {
$bootstrap_bits[] = "plugin://bootstrapper/js/{$version}/bootstrap{$mode}.js";
}
}
}
$assets = $this->grav['assets'];
$assets->registerCollection('bootstrap', $bootstrap_bits);
$assets->add('bootstrap', 100);
}
}