This repository has been archived by the owner on Aug 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsharer.php
114 lines (89 loc) · 3.07 KB
/
sharer.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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
class SharerPlugin extends Plugin
{
public static function getSubscribedEvents() {
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onAssetsInitialized' => ['onAssetsInitialized', 0],
'onTwigInitialized' => ['onTwigInitialized', 0]
]);
}
/**
* Add plugin templates.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function onAssetsInitialized()
{
$config = $this->config->get('plugins.sharer');
if ($config['built_in_css']) {
$this->grav['assets']->addCss('plugin://sharer/assets/css-compiled/sharer.css');
}
if($config['fontawesome_icons'] && $config['fontawesome_css']) {
if($config['fontawesome_v4']) {
$this->grav['assets']->addCss('plugin://sharer/assets/css/font-awesome-4.7.0.min.css', 99);
} else {
$this->grav['assets']->addCss('plugin://sharer/assets/css/fontawesome-5.11.2.min.css', 99);
$this->grav['assets']->addCss('plugin://sharer/assets/css/all.min.css', 99);
}
}
$this->grav['assets']->addJs('plugin://sharer/assets/js/sharer.min.js', 100);
}
/**
* Merge config and page header frontmatter values in a Twig var
*/
public function onTwigSiteVariables()
{
$config = $this->config->get('plugins.sharer');
$page = $this->grav['page'];
$header = $page->header();
if (isset($header->sharer) && is_array($header->sharer)) {
$page_frontmatter = $header->sharer;
} else {
$page_frontmatter = array();
}
$data = array_replace_recursive($config, $page_frontmatter);
$twig = $this->grav['twig'];
$twig->twig_vars['sharer_config'] = $data;
}
/**
* Add simple `sharer()` Twig function and `|sharer_sort_buttons` filter
*/
public function onTwigInitialized()
{
$this->grav['twig']->twig()->addFunction(
new \Twig_SimpleFunction('sharer', [$this, 'renderTemplate'])
);
$this->grav['twig']->twig()->addFilter(
new \Twig_SimpleFilter('sharer_sort_buttons', [$this, 'sortButtonsByPriority'])
);
}
public function renderTemplate() {
return $this->grav['twig']->processTemplate('partials/sharer.html.twig', [
'page' => $this->grav['page']
]);
}
// Sort buttons by priority parameter
public function sortButtonsByPriority($items = [])
{
uasort($items, function($a, $b) {
return $a['priority'] <=> $b['priority'];
});
return $items;
}
}