-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlugin.php
215 lines (188 loc) · 7.61 KB
/
Plugin.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
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
215
<?php
namespace ABWebDevelopers\ImageResize;
use ABWebDevelopers\ImageResize\Classes\Resizer;
use ABWebDevelopers\ImageResize\Commands\ImageResizeClear;
use ABWebDevelopers\ImageResize\Commands\ImageResizeGc;
use ABWebDevelopers\ImageResize\Commands\ImageResizeResetPermalinks;
use ABWebDevelopers\ImageResize\Models\ImagePermalink;
use ABWebDevelopers\ImageResize\Models\Settings;
use ABWebDevelopers\ImageResize\ReportWidgets\ImageResizeClearWidget;
use App;
use Artisan;
use DB;
use Event;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\QueryException;
use Symfony\Component\Console\Output\ConsoleOutput;
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
/**
* @inheritDoc
*/
public function pluginDetails()
{
return [
'name' => 'abwebdevelopers.imageresize::lang.plugin.name',
'description' => 'abwebdevelopers.imageresize::lang.plugin.description',
'author' => 'AB Web Developers',
'icon' => 'icon-file-image-o',
'homepage' => 'https://abweb.com.au'
];
}
/**
* @inheritDoc
*/
public function registerMarkupTags()
{
return [
'filters' => [
'resize' => function ($image, $width, $height = null, $options = []) {
$resizer = new Resizer();
$width = ($width !== null) ? (int) $width : null;
$height = ($height !== null) ? (int) $height : null;
$options = ($options instanceof Arrayable) ? $options->toArray() : (array) $options;
// If the given configuration has a permalink identifier then resize using it
if (isset($options['permalink']) && strlen($options['permalink'])) {
$resizer->preventDefaultImage();
$resizer->setImage((string) $image);
return $resizer->resizePermalink($options['permalink'], $width, $height, $options)->permalink_url;
}
$resizer->setImage((string) $image);
return $resizer->resize($width, $height, $options);
},
'modify' => function ($image, $options = []) {
$resizer = new Resizer();
$width = null;
$height = null;
$options = ($options instanceof Arrayable) ? $options->toArray() : (array) $options;
// If the given configuration has a permalink identifier then resize using it
if (isset($options['permalink']) && strlen($options['permalink'])) {
$resizer->preventDefaultImage();
$resizer->setImage((string) $image);
return $resizer->resizePermalink($options['permalink'], $width, $height, $options)->permalink_url;
}
$resizer->setImage((string) $image);
return $resizer->resize($width, $height, $options);
},
'filterHtmlImageResize' => function ($html, $width, $height = null, $options = []) {
$html = (string) $html;
$width = ($width !== null) ? (int) $width : null;
$height = ($height !== null) ? (int) $height : null;
$options = ($options instanceof Arrayable) ? $options->toArray() : (array) $options;
return Resizer::parseFindReplaceImages($html, $width, $height, $options);
},
'filterHtmlImageModify' => function ($html, $options = []) {
$html = (string) $html;
$width = null;
$height = null;
$options = ($options instanceof Arrayable) ? $options->toArray() : (array) $options;
return Resizer::parseFindReplaceImages($html, $width, $height, $options);
}
]
];
}
/**
* @inheritDoc
*/
public function registerSettings()
{
return [
'settings' => [
'label' => 'abwebdevelopers.imageresize::lang.plugin.name',
'description' => 'Manage default settings for the Image Resizer plugin',
'category' => 'Content',
'icon' => 'icon-image',
'class' => 'ABWebDevelopers\ImageResize\Models\Settings',
'permissions' => ['abwebdevelopers.imageresize.access_settings'],
'order' => 500,
'keywords' => 'image resize resizing modify photo modifier'
]
];
}
/**
* @inheritDoc
*/
public function registerPermissions()
{
return [
'abwebdevelopers.imageresize.access_settings' => ['tab' => 'abwebdevelopers.imageresize::lang.permissions.tab', 'label' => 'abwebdevelopers.imageresize::lang.permissions.access_settings'],
];
}
/**
* @inheritDoc
*/
public function boot()
{
Event::listen('backend.page.beforeDisplay', function ($controller, $action, $params) {
if ($controller instanceof \System\Controllers\Settings) {
// Check this is the settings page for this plugin:
if ($params === ['abwebdevelopers', 'imageresize', 'settings']) {
// Add CSS (minor patch)
$controller->addCss('/plugins/abwebdevelopers/imageresize/assets/settings-patch.css');
}
}
});
Event::listen('cache:cleared', function () {
$this->ifDatabaseExists(function () {
if (Settings::cleanupOnCacheClear()) {
if (App::runningInConsole()) {
$output = new ConsoleOutput();
$output->writeln('<info>Imagesizer: Deleting cached resized images...</info>');
}
Artisan::call('imageresize:clear');
if (App::runningInConsole()) {
$output->writeln('<info>Imagesizer: ' . Artisan::output() . '</info>');
}
}
});
});
}
/**
* @inheritDoc
*/
public function register()
{
$this->registerConsoleCommand('imageresize:gc', ImageResizeGc::class);
$this->registerConsoleCommand('imageresize:clear', ImageResizeClear::class);
$this->registerConsoleCommand('imageresize:reset-permalink', ImageResizeResetPermalinks::class);
}
/**
* @inheritDoc
*/
public function registerSchedule($schedule)
{
// This is throttled by your settings, it won't necessarily clear all images every 5 minutes
$schedule->command('imageresize:gc')->everyFiveMinutes();
}
/**
* @inheritDoc
*/
public function registerReportWidgets()
{
return [
ImageResizeClearWidget::class => [
'label' => 'Clear Image Resizer Cache',
'context' => 'dashboard',
],
];
}
/**
* Run the callback only if/when the database exists (and system_settings table exists).
*
* @param \Closure $callback
* @return mixed
*/
public function ifDatabaseExists(\Closure $callback)
{
$canConnectToDatabase = false;
try {
// Test database connection (throw exception if no DB is configured yet)
$canConnectToDatabase = DB::table('system_settings')->exists();
} catch (QueryException $e) {
}
if ($canConnectToDatabase) {
return $callback();
}
}
}