-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebp.php
More file actions
214 lines (187 loc) · 6.46 KB
/
Copy pathwebp.php
File metadata and controls
214 lines (187 loc) · 6.46 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* @author Greenrivers
* @copyright Copyright (c) 2022 Greenrivers
* @package Grav\Plugin\Webp
*/
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Data\Data;
use Grav\Common\Plugin;
use Grav\Common\Twig\Twig;
use Grav\Common\Uri;
use Grav\Framework\Session\SessionInterface;
use Grav\Plugin\Webp\Helper\Config;
use Grav\Plugin\Webp\Utils\Response;
use Grav\Plugin\Webp\Webp;
use RocketTheme\Toolbox\Event\Event;
class WebpPlugin extends Plugin
{
/** @var Webp */
private $webp;
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
'onPluginsInitialized' => [
['autoload', 100000],
['onPluginsInitialized', 0],
['onPagesInitialized', 0]
]
];
}
/**
* @return ClassLoader
*/
public function autoload(): ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* @return void
*/
public function onPluginsInitialized(): void
{
$this->enable([
'onAssetsInitialized' => ['onAssetsInitialized', 0],
'onAdminSave' => ['onAdminSave', 0],
'onAdminTwigTemplatePaths' => ['onAdminTwigTemplatePaths', 0],
'onTwigInitialized' => ['onTwigInitialized', 0]
]);
$this->webp = new Webp();
}
/**
* @return void
*/
public function onPagesInitialized(): void
{
/** @var SessionInterface $session */
$session = $this->grav['session'];
/** @var Uri $uri */
$uri = $this->grav['uri'];
$paths = $uri->paths();
if (isset($paths[2], $paths[3]) && $paths[2] === 'webp') {
switch ($paths[3]) {
case 'set_config':
$originalPath = $uri->post('original_path');
$quality = $uri->post('quality');
$session->__set('original_path', $originalPath);
$session->__set('quality', $quality);
Response::sendJsonResponse([
'original_path' => $session->__get('original_path'),
'quality' => $session->__get('quality')
]);
break;
case 'convert':
$totalImages = $session->getFlashObject('total_images');
$convertedImagesCount = $this->webp->process(
$totalImages,
$session->getFlashObject('converted_images_count'),
$session->__get('original_path'),
$session->__get('quality'),
);
$session->setFlashObject('total_images', $totalImages);
$session->setFlashObject('converted_images_count', $convertedImagesCount);
Response::sendJsonResponse(['converted_images' => $convertedImagesCount]);
break;
case 'clear_all':
$webpImages = $session->getFlashObject('webp_images');
$removedImagesCount = $this->webp->clearAll(
$webpImages,
$session->getFlashObject('removed_images_count')
);
$session->setFlashObject('webp_images', $webpImages);
$session->setFlashObject('removed_images_count', $removedImagesCount);
Response::sendJsonResponse(['removed_images' => $removedImagesCount]);
break;
case 'webp_images':
$webpImages = $this->webp->getImagesToRemove();
$session->setFlashObject('webp_images', $webpImages);
$session->setFlashObject('removed_images_count', 0);
Response::sendJsonResponse(['webp_images' => count($webpImages)]);
break;
case 'images':
$totalImages = $this->webp->getImagesToConversion($session->__get('original_path'));
$session->setFlashObject('total_images', $totalImages);
$session->setFlashObject('converted_images_count', 0);
Response::sendJsonResponse(['total_images' => count($totalImages)]);
break;
default:
Response::sendJsonResponse(['error' => true]);
break;
}
}
}
/**
* @param Event $event
* @return void
*/
public function onAdminSave(Event $event): void
{
/** @var Data $object */
$object = $event['object'];
$blueprints = $object->blueprints();
/** @var SessionInterface $session */
$session = $this->grav['session'];
if ($object instanceof Data && $blueprints->getFilename() === 'webp/blueprints') {
$object->set('original_path', $session->__get('original_path'));
$object->set('quality', $session->__get('quality'));
}
}
/**
* @return void
*/
public function onAssetsInitialized(): void
{
if ($this->isAdmin()) {
$this->grav['assets']->addCss('plugin://webp/assets/css/admin.min.css');
$this->grav['assets']->addJs('plugin://webp/assets/js/admin.min.js');
}
}
/**
* @param Event $event
* @return Event
*/
public function onAdminTwigTemplatePaths(Event $event): Event
{
$event['paths'] = array_merge(
$event['paths'],
[__DIR__ . '/admin/themes/grav/templates']
);
return $event;
}
/**
* @param Event $event
*/
public function onTwigInitialized(Event $event): void
{
if (Config::isEnabled()) {
/** @var Twig $gravTwig */
$gravTwig = $this->grav['twig'];
$gravTwig->twig()->addFilter(
new \Twig_SimpleFilter('webp', [$this, 'webp'])
);
$gravTwig->twig()->addFilter(
new \Twig_SimpleFilter('extension', [$this, 'extension'])
);
}
}
/**
* @param string $imagePath
* @return string
*/
public function webp(string $imagePath): string
{
return $this->webp->changeImagePath($imagePath);
}
/**
* @param string $imagePath
* @return string
*/
public function extension(string $imagePath): string
{
return $this->webp->getImageExtension($imagePath);
}
}