-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlikes-ratings.php
174 lines (149 loc) · 4.69 KB
/
likes-ratings.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
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
use Grav\Common\Utils;
use Grav\Plugin\LikesRatings\Likes;
use RocketTheme\Toolbox\Event\Event;
/**
* Class LikesRatingsPlugin
* @package Grav\Plugin
*/
class LikesRatingsPlugin extends Plugin
{
/**
* @return array
*
*/
public static function getSubscribedEvents()
{
return [
'onCliInitialize' => [
['autoload', 100000],
['register', 1000]
],
'onPluginsInitialized' => [
['autoload', 100000],
['register', 1000],
['onPluginsInitialized', 1000],
],
'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
];
}
/**
* [onPluginsInitialized:100000] Composer autoload.
*
* @return ClassLoader
*/
public function autoload()
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* Register the service
*/
public function register()
{
$this->grav['likes'] = function ($c) {
return new Likes($c['config']->get('plugins.likes-ratings'));
};
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
return;
}
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigInitialized' => ['onTwigInitialized', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onTwigLoader' => ['onTwigLoader', 0],
]);
}
public function onShortcodeHandlers()
{
$this->grav['shortcode']->registerAllShortcodes(__DIR__ . '/classes/shortcodes');
}
// Add images to twig template paths to allow inclusion of SVG files
public function onTwigLoader()
{
$theme_paths = $this->grav['locator']->findResources('plugins://likes-ratings/assets');
foreach($theme_paths as $images_path) {
$this->grav['twig']->addPath($images_path, 'likes-ratings');
}
}
public function onPageInitialized(Event $e)
{
$callback = $this->config->get('plugins.likes-ratings.callback');
$route = $this->grav['uri']->path();
// Process vote if appropriate
if ($callback === $route) {
// try to add the vote
$result = $this->addVote();
header('Content-Type: application/json');
echo json_encode(['status' => $result[0], 'error' => $result[1], 'content' => $result[2] ?? 'Error: missing content...']);
exit();
}
}
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function onTwigInitialized() {
$this->grav['twig']->twig()->addFunction(
new \Twig_SimpleFunction('likes_ratings', [$this, 'generateLikes'], ['is_safe' => ['html']])
);
}
public function onTwigSiteVariables()
{
if ($this->config->get('plugins.likes-ratings.built_in_css')) {
$this->grav['assets']
->addCss('plugin://likes-ratings/assets/likes-ratings.css');
}
$this->grav['assets']
->addJs('plugin://likes-ratings/assets/likes-ratings.js');
}
/**
* @param mixed|null $id
* @param array $options
* @return string
*/
public function generateLikes($id = null, $options = [])
{
/** @var Likes $likes */
$likes = $this->grav['likes'];
$id = $likes->getId($id);
if (null === $id) {
return '';
}
if (!empty($options)) {
$likes->saveOptions($id, $options);
}
return $this->grav['likes']->generateLikes($id);
}
protected function addVote()
{
if (!Utils::verifyNonce($this->grav['uri']->param('nonce'), 'likes-ratings')) {
return [false, 'Invalid security nonce'];
}
$rawData = file_get_contents('php://input');
$data = json_decode($rawData, true);
if (json_last_error() === JSON_ERROR_NONE) {
$id = $data['id'] ?? null;
$type = $data['type'] ?? null;
if ($id && $type) {
/** @var Likes $likes */
$likes = $this->grav['likes'];
$likes->mergeSavedOptions($id);
return $likes->add($id, $type, 1);
}
} else {
return [false, "Failed to decode JSON. Error: " . json_last_error_msg(), -1];
}
return [false, 'Missing id or type', -1];
}
}