forked from matt-j-m/grav-plugin-aura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aura.php
180 lines (153 loc) · 5.24 KB
/
aura.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Flex\Types\Pages\PageObject;
use RocketTheme\Toolbox\Event\Event;
use Grav\Common\Utils;
use Grav\Plugin\Aura\Aura;
/**
* Class AuraPlugin
* @package Grav\Plugin
*/
class AuraPlugin extends Plugin
{
/**
* Gives the core a list of events the plugin wants to listen to
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
spl_autoload_register(function ($class) {
if (Utils::startsWith($class, 'Grav\Plugin\Aura\\')) {
require_once __DIR__ .'/classes/' . strtolower(basename(str_replace("\\", '/', $class))) . '.php';
}
});
// Admin only events
if ($this->isAdmin()) {
$this->enable([
'onGetPageBlueprints' => ['onGetPageBlueprints', 0],
'onAdminSave' => ['onAdminSave', 0],
]);
return;
}
// Frontend events
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0]
]);
}
/**
* Extend page blueprints with additional configuration options.
*
* @param Event $event
*/
public function onGetPageBlueprints($event)
{
$types = $event->types;
$types->scanBlueprints('plugins://' . $this->name . '/blueprints');
}
public function onAdminSave(Event $event)
{
if (!$event['object'] instanceof PageObject) {
return;
}
// Don't proceed if required params not set
$requiredParams = array(
'org-name',
'org-url',
);
foreach ($requiredParams as $param) {
$key = 'plugins.aura.' . $param;
if (!$this->grav['config']->get($key)) {
return;
}
}
$page = $event['object'];
$aura = new Aura($page);
// Meta Description
if ($aura->webpage->description) {
// Append description to page metadata
$aura->webpage->metadata['description'] = array(
'name' => 'description',
'content' => htmlentities($aura->webpage->description),
);
}
// Open Graph
if ($this->grav['config']->get('plugins.aura.output-og')) {
$aura->generateOpenGraphMeta();
}
// Twitter
if ($this->grav['config']->get('plugins.aura.output-twitter')) {
$aura->generateTwitterMeta();
}
// LinkedIn
if ($this->grav['config']->get('plugins.aura.output-linkedin')) {
$aura->generateLinkedInMeta();
}
// Generate Aura metadata
$metadata = [];
foreach ($aura->webpage->metadata as $tag) {
if (array_key_exists('property', $tag)) {
$metadata[$tag['property']] = $tag['content'];
} else if (array_key_exists('name', $tag)) {
$metadata[$tag['name']] = $tag['content'];
}
}
$original = $page->getOriginal();
if ($original && !isset($original->header()->aura) && isset($page->header()->metadata) && is_array($page->header()->metadata)) {
// Page has not been saved since installation of Aura and includes some custom metadata
$legacyMetadata = ['metadata' => []];
foreach ($page->header()->metadata as $key => $val) {
if (!array_key_exists($key, $metadata)) {
// A new value has not been supplied via Aura, salvage existing metadata
$metadata[$key] = $val;
$legacyMetadata['metadata'][$key] = $val;
}
}
$page->header()->aura = array_merge($legacyMetadata, isset($page->header()->aura) ? $page->header()->aura : []);
}
$page->header()->metadata = array_merge($metadata, isset($page->header()->aura['metadata']) ? $page->header()->aura['metadata'] : []);
}
/**
* Insert meta tags and structured data to head of each page
*
* @param Event $e
*/
public function onPageInitialized()
{
// Structured Data
if ($this->grav['config']->get('plugins.aura.output-sd')) {
// Don't proceed if required params not set
$requiredParams = array(
'org-name',
'org-url',
);
foreach ($requiredParams as $param) {
$key = 'plugins.aura.' . $param;
if (!$this->grav['config']->get($key)) {
return;
}
}
$page = $this->grav['page'];
$assets = $this->grav['assets'];
$aura = new Aura($page);
// Generate structured data block
$sd = $aura->generateStructuredData();
// Drop into JS pipeline
$type = array('type' => 'application/ld+json');
if (version_compare(GRAV_VERSION, '1.6.0', '<')) {
$type = 'application/ld+json';
}
$assets->addInlineJs($sd, null, null, $type);
}
}
}