-
Notifications
You must be signed in to change notification settings - Fork 3
/
aura-authors.php
112 lines (95 loc) · 2.94 KB
/
aura-authors.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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
/**
* Class AuraAuthorsPlugin
* @package Grav\Plugin
*/
class AuraAuthorsPlugin extends Plugin
{
/** @var array */
static protected $authors = [];
public static function getAuthors()
{
return self::$authors;
}
public function onPluginsInitialized()
{
// Admin only events
if ($this->isAdmin()) {
$this->enable([
'onGetPageBlueprints' => ['onGetPageBlueprints', 0],
'onAdminSave' => ['onAdminSave', 0],
]);
return;
}
// Frontend events
$this->enable([
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
]);
}
/**
* Add plugin directory to twig lookup paths
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Extend page blueprints with additional configuration options.
*
* @param Event $event
*/
public function onGetPageBlueprints($event)
{
self::$authors = $this->grav['config']->get('plugins.aura-authors.authors');
$types = $event->types;
$types->scanBlueprints('plugins://' . $this->name . '/blueprints');
}
public function onAdminSave(Event $event)
{
// Don't proceed if Admin is not saving a Page
if (!$event['object'] instanceof Page) {
return;
}
$page = $event['object'];
if (isset($page->header()->aura['author'])) {
// TODO: tidy this section
// Also consider how to remove an author (currently need to go to expert mode)
// Also what if someone wants to set multiple author tags? should proably allow but only consider the aura one for meta output
if (isset($page->header()->taxonomy)) {
$taxonomy = $page->header()->taxonomy;
} else {
$taxonomy = [];
}
$taxonomy['author'] = array($page->header()->aura['author']);
$page->header()->taxonomy = $taxonomy;
}
}
public static function listAuthors() {
$authorList = [];
$authors = self::getAuthors();
foreach ($authors as $author) {
$authorList[$author['label']] = $author['name'];
}
asort($authorList);
return $authorList;
}
/**
* Create structured authors array and expose to Twig
*/
public function onTwigSiteVariables()
{
$authors = array();
$raw = $this->grav['config']->get('plugins.aura-authors.authors');
if ($raw) {
foreach ($raw as $author) {
$authors[$author['label']] = $author;
}
}
$this->grav['twig']->twig_vars['authors'] = $authors;
}
}