Repository navigation
Expand file tree
/
Copy pathlearn2-git-sync.php
More file actions
139 lines (128 loc) · 4.07 KB
/
Copy pathlearn2-git-sync.php
File metadata and controls
139 lines (128 loc) · 4.07 KB
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
<?php
namespace Grav\Theme;
use Grav\Common\Grav;
use Grav\Common\Theme;
use RocketTheme\Toolbox\Event\Event;
class Learn2GitSync extends Learn2
{
/**
* Initialize plugin and subsequent events
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onTwigInitialized' => ['onTwigInitialized', 0],
'onThemeInitialized' => ['onThemeInitialized', 0],
'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
'onTNTSearchIndex' => ['onTNTSearchIndex', 0]
];
}
public function onShortcodeHandlers()
{
$this->grav['shortcode']->registerAllShortcodes('user://themes/learn2-git-sync/shortcodes');
}
public function onTNTSearchIndex(Event $e)
{
$fields = $e['fields'];
$page = $e['page'];
$taxonomy = $page->taxonomy();
if (isset($taxonomy['tag'])) {
$fields->tag = implode(",", $taxonomy['tag']);
}
}
public function onTwigInitialized() {
if (!isset($this->grav['shortcode'])) {
return;
}
$sc = $this->grav['shortcode'];
$sc->getHandlers()->addAlias('version', 'lang');
}
/**
* Register events and route with Grav
*
* @return void
*/
public function onThemeInitialized()
{
/* Check if Admin-interface */
if (!$this->isAdmin()) {
$this->enable(
[
'onPageInitialized' => ['onPageInitialized', 0]
]
);
}
}
/**
* Get default category setting
*
* @return string
*/
public static function getdefaulttaxonomycategory()
{
$config = Grav::instance()['config'];
return $config->get('themes.' . $config->get('system.pages.theme'). '.default_taxonomy_category');
}
/**
* Handle CSS
*
* @return void
*/
public function onPageInitialized()
{
$assets = $this->grav['assets'];
$config = $this->config();
if (isset($config['style'])) {
$style = $config['style'];
if ($style == 'default') {
$style = 'theme';
}
$current = self::fileFinder(
$style,
'.css',
'theme://css/styles',
'theme://css'
);
// Styles removed in 2.3.0 (or any unknown style) fall back to Classic
if (!$current) {
$style = 'theme';
$current = self::fileFinder($style, '.css', 'theme://css');
}
$assets->addCss($current, 101);
// Dark Mode: each style has a dark companion (e.g. theme-2026-dark.css), loaded after the style and before custom.css
$mode = $config['dark_mode']['mode'] ?? 'disabled';
$dark = ($mode === 'enabled' || $mode === 'auto') ? self::fileFinder($style . '-dark', '.css', 'theme://css/styles', 'theme://css') : false;
if ($dark && $mode === 'enabled') {
$assets->addCss($dark, 101);
} elseif ($dark && !$this->grav['config']->get('system.assets.css_pipeline')) {
$assets->addCss($dark, ['priority' => 101, 'media' => '(prefers-color-scheme: dark)']);
} elseif ($dark) {
// A pipelined stylesheet loses its media attribute, so the template links it separately
$this->grav['twig']->twig_vars['dark_mode_auto_css'] = $dark;
}
}
}
/**
* Search for a file in multiple locations
*
* @param string $file Filename.
* @param string $ext File extension.
* @param array ...$locations List of paths.
*
* @return string
*/
public static function fileFinder($file, $ext, ...$locations)
{
$return = false;
foreach ($locations as $location) {
if (file_exists($location . '/' . $file . $ext)) {
$return = $location . '/' . $file . $ext;
break;
}
}
return $return;
}
}
?>