-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.filejet-admin.php
More file actions
462 lines (360 loc) · 14.4 KB
/
class.filejet-admin.php
File metadata and controls
462 lines (360 loc) · 14.4 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
class Filejet_Admin
{
const NONCE = 'filejet-config-data';
const TAB_OVERVIEW = 'overview';
const TAB_MUTATIONS = 'mutations';
const TAB_CONFIGURATION = 'configuration';
const TAB_LAZY_LOAD = 'lazy_load';
const NAV = [
self::TAB_OVERVIEW => 'Overview',
self::TAB_MUTATIONS => 'Mutations',
self::TAB_CONFIGURATION => 'Ignore list',
self::TAB_LAZY_LOAD => 'Image attributes'
];
private static $initiated = false;
private static $notices = array();
public static function init()
{
if (!self::$initiated) {
self::init_hooks();
}
$key = array_key_exists('action', $_POST) ? $_POST['action'] : null;
switch (Filejet_Action::validate($key)) {
case Filejet_Action::ENTER_KEY:
self::enter_api_key();
break;
case Filejet_Action::ADD_MUTATION_SETTING:
self::add_mutation_setting();
break;
case Filejet_Action::ADD_IGNORE_SETTING:
self::add_ignore_setting();
break;
case Filejet_Action::ADD_LAZY_LOAD_SETTING:
self::add_lazy_load_setting();
break;
case Filejet_Action::DELETE_MUTATION_SETTING:
self::delete_setting(Filejet::CONFIG_MUTATIONS);
break;
case Filejet_Action::DELETE_IGNORE_SETTING:
self::delete_setting(Filejet::CONFIG_IGNORED);
break;
case Filejet_Action::DELETE_LAZY_LOAD_SETTING:
self::delete_setting(Filejet::CONFIG_LAZY_LOAD);
break;
}
}
public static function init_hooks()
{
self::$initiated = true;
add_action('admin_init', array('Filejet_Admin', 'admin_init'));
add_action('admin_menu', array('Filejet_Admin', 'admin_menu'));
add_action('admin_notices', array('Filejet_Admin', 'display_notice'));
add_action('admin_enqueue_scripts', array('Filejet_Admin', 'load_resources'));
add_action('after_setup_theme', 'myplugin_after_setup_theme');
add_filter('all_plugins', array('Filejet_Admin', 'modify_plugin_description'));
add_filter('plugin_action_links_' . FILEJET_PLUGIN_BASENAME, array('Filejet_Admin', 'addPluginActionLinks'));
add_action('wp_dashboard_setup', array('Filejet_Admin', 'init_filejet_dashboard_widget'));
}
public static function init_filejet_dashboard_widget()
{
wp_add_dashboard_widget(
'filejet_dashboard_widget',
'<img src="' .esc_url(plugins_url('assets/images/logo-white.svg', __FILE__)) . '" class="logo-white">',
['Filejet_Admin', 'filejet_dashboard_widget']
);
}
public static function filejet_dashboard_widget()
{
Filejet::view('widget', array('type' => 'plugin'));
}
public static function addPluginActionLinks($action_links)
{
$settings_link = '<a href="options-general.php?page=' . FILEJET_PLUGIN_BASENAME . '">' . __('Settings', FILEJET_PLUGIN_BASENAME) . '</a>';
array_unshift($action_links, $settings_link);
return $action_links;
}
public static function admin_init()
{
load_plugin_textdomain('filejet');
}
public static function display_api_key_warning()
{
Filejet::view('notice', array('type' => 'plugin'));
}
public static function display_notice()
{
global $hook_suffix;
if (in_array($hook_suffix, ['plugins.php', 'index.php', 'upload.php']) && !Filejet::get_api_key()) {
self::display_api_key_warning();
}
}
/**
* When FileJet is active, remove the "Activate Filejet" step from the plugin description.
*/
public static function modify_plugin_description($all_plugins)
{
if (isset($all_plugins['filejet/filejet.php'])) {
if (Filejet::get_api_key()) {
$all_plugins['filejet/filejet.php']['Description'] = __('FileJet Professional, your digital asseet optimization service', 'filejet');
} else {
$all_plugins['filejet/filejet.php']['Description'] = __('FileJet Professional, your digital asseet optimization service. To get started, just go to <a href="admin.php?page=filejet-keys">your FileJet Settings page</a> to set up your API key.', 'filejet');
}
}
return $all_plugins;
}
public static function admin_menu()
{
add_options_page('FileJet Pro', 'FileJet Pro', 'manage_options', FILEJET_PLUGIN_BASENAME, ['Filejet_Admin', 'display_page']);
}
public static function admin_head()
{
if (!current_user_can('manage_options')) {
return;
}
}
public static function admin_plugin_settings_link($links)
{
$settings_link = '<a href="' . esc_url(self::get_page_url()) . '">' . __('Settings', 'filejet') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
public static function get_page_url($page = FILEJET_PLUGIN_BASENAME)
{
return add_query_arg(['page' => $page], admin_url('admin.php'));
}
public static function get_page_url_with_tab($tab = self::TAB_CONFIGURATION, array $additional = [], $page = FILEJET_PLUGIN_BASENAME)
{
return add_query_arg(array_merge(['page' => $page, 'tab' => $tab], $additional), admin_url('admin.php'));
}
public static function tab_is_allowed($tab)
{
return in_array(
$tab,
[
self::TAB_CONFIGURATION,
self::TAB_MUTATIONS,
self::TAB_LAZY_LOAD,
],
true
);
}
public static function load_resources()
{
global $hook_suffix;
preg_match('/settings_page_(.*)\/filejet/', $hook_suffix, $matches);
$isSettingPage = !empty($matches);
if (in_array(
$hook_suffix, apply_filters(
'Filejet_Admin_page_hook_suffixes', array(
'index.php', // dashboard
'plugins.php',
'upload.php',
'options-general.php'
)
), true
) || $isSettingPage
) {
wp_register_style('filejet.css', plugin_dir_url(__FILE__) . 'assets/filejet.css', array(), FILEJET_VERSION);
wp_enqueue_style('filejet.css');
wp_register_script('chart.js', plugin_dir_url(__FILE__) . 'assets/js/Chart.bundle.min.js', array(), FILEJET_VERSION);
wp_enqueue_script('chart.js');
}
}
public static function enter_api_key()
{
if (!current_user_can('manage_options')) {
die(__('Cheatin’ uh?', 'Filejet'));
}
if (!wp_verify_nonce($_POST['_wpnonce'], self::NONCE)) {
return false;
}
if (Filejet::predefined_credentials()) {
return false; //should not get here, credentials from config
}
$storage_id = preg_replace('/[^a-z0-9]/i', '', $_POST['storageId']);
$new_key = preg_replace('/[^a-z0-9]/i', '', $_POST['key']);
$new_secret = preg_replace('/[^a-z0-9]/i', '', $_POST['secret']);
$old_key = Filejet::get_api_key();
if (empty($new_key)) {
if (!empty($old_key)) {
delete_option('filejet_api_key');
delete_option('filejet_storage_id');
delete_option('secret');
self::$notices[] = 'new-key-empty';
}
} elseif ($new_key !== $old_key) {
self::save_key($storage_id, $new_key, $new_secret);
}
return true;
}
public static function add_mutation_setting()
{
if (!current_user_can('manage_options')) {
die(__('Cheatin’ uh?', 'Filejet'));
}
if (!wp_verify_nonce($_POST['_wpnonce'], self::NONCE)) {
return false;
}
$mutation = preg_replace('/[^a-z0-9-_]/i', '', $_POST['mutation']);
$class = preg_replace('/[^a-z0-9-_]/i', '', $_POST['class']);
if (in_array($class, ['fj-image', 'fj-image-loaded'])) {
return false;
}
$config = [];
$config_current = Filejet::get_config();
$config = array_merge($config, array_key_exists(Filejet::CONFIG_MUTATIONS, $config_current) ? $config_current[Filejet::CONFIG_MUTATIONS] : []);
if (!empty($class) && !empty($mutation)) {
$config[$class] = $mutation;
$config_current[Filejet::CONFIG_MUTATIONS] = $config;
update_option('filejet_config', json_encode($config_current));
}
return true;
}
public static function add_ignore_setting()
{
if (!current_user_can('manage_options')) {
die(__('Cheatin’ uh?', 'Filejet'));
}
if (!wp_verify_nonce($_POST['_wpnonce'], self::NONCE)) {
return false;
}
$class = preg_replace('/[^a-z0-9-_]/i', '', $_POST['class']);
if (in_array($class, ['fj-image', 'fj-image-loaded'])) {
return false;
}
$config = [];
$config_current = Filejet::get_config();
$config = array_merge($config, array_key_exists(Filejet::CONFIG_IGNORED, $config_current) ? $config_current[Filejet::CONFIG_IGNORED] : []);
if (!empty($class)) {
$config[$class] = $class;
$config_current[Filejet::CONFIG_IGNORED] = $config;
update_option('filejet_config', json_encode($config_current));
}
return true;
}
public static function add_lazy_load_setting()
{
if (!current_user_can('manage_options')) {
die(__('Cheatin’ uh?', 'Filejet'));
}
if (!wp_verify_nonce($_POST['_wpnonce'], self::NONCE)) {
return false;
}
$attribute = preg_replace('/[^a-z0-9-_]/i', '', $_POST['attribute']);
if(!$attribute || in_array($attribute, ['src', 'srcset'])) {
return false;
}
$config = [];
$config_current = Filejet::get_config();
$config = array_merge($config, array_key_exists(Filejet::CONFIG_LAZY_LOAD, $config_current) ? $config_current[Filejet::CONFIG_LAZY_LOAD] : []);
if (!empty($attribute)) {
$config[$attribute] = $attribute;
$config_current[Filejet::CONFIG_LAZY_LOAD] = $config;
update_option('filejet_config', json_encode($config_current));
}
return true;
}
public static function delete_setting($option)
{
if (!current_user_can('manage_options')) {
die(__('Cheatin’ uh?', 'Filejet'));
}
if (false === in_array($option, Filejet::OPTIONS)) {
die(__('Invalid option', 'Filejet'));
}
if (!wp_verify_nonce($_POST['_wpnonce'], self::NONCE)) {
return false;
}
$class = preg_replace('/[^a-z0-9-_]/i', '', $_POST['class']);
$config = Filejet::get_config();
$option_config = array_key_exists($option, $config) ? $config[$option] : [];
if (!empty($class) && array_key_exists($class, $option_config)) {
unset($option_config[$class]);
$config[$option] = $option_config;
update_option('filejet_config', json_encode($config));
}
return true;
}
public static function save_key($storage_id, $api_key, $secret)
{
update_option('filejet_api_key', $api_key);
update_option('filejet_storage_id', $storage_id);
update_option('filejet_secret', $secret);
self::$notices['status'] = 'new-key-valid';
}
public static function dashboard_stats()
{
}
public static function plugin_action_links($links, $file)
{
if ($file === plugin_basename(plugin_dir_url(__FILE__) . '/filejet.php')) {
$links[] = '<a href="' . esc_url(self::get_page_url()) . '">' . esc_html__('Settings', 'filejet') . '</a>';
}
return $links;
}
public static function display_page()
{
if (!Filejet::get_api_key() || (isset($_GET['view']) && $_GET['view'] === 'welcome')) {
self::display_welcome_page();
} else {
self::display_configuration_page();
}
}
public static function display_welcome_page()
{
$filejet_user = false;
// if ( isset( $_GET['token'] ) && preg_match('/^(\d+)-[0-9a-f]{20}$/', $_GET['token'] ) )
// $filejet_user = self::verify_wpcom_key( '', '', array( 'token' => $_GET['token'] ) );
// elseif ( $jetpack_user = self::get_jetpack_user() )
// $filejet_user = self::verify_wpcom_key( $jetpack_user['api_key'], $jetpack_user['user_id'] );
// if ( isset( $_GET['action'] ) ) {
// if ( $_GET['action'] == 'save-key' ) {
// if ( is_object( $filejet_user ) ) {
// self::save_key( $filejet_user->api_key );
// self::display_configuration_page();
// return;
// }
// }
// }
Filejet::view('welcome', compact('filejet_user'));
}
public static function display_status()
{
Filejet::view('notice', array('type' => 'service-ok'));
}
public static function display_configuration_page()
{
Filejet::view('setup');
}
public static function get_statistics_data($year = null, $month = null)
{
if(!Filejet::get_storage_id() || !Filejet::get_api_key()) {
return;
}
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => sprintf(
'https://twrfq4l8y8.execute-api.eu-west-1.amazonaws.com/prod/stats?apiKey=%s&storageId=%s&year=%s&month=%s',
Filejet::get_api_key(),
Filejet::get_storage_id(),
$year ?: (new \DateTime())->format('Y'),
$month ?: (new \DateTime())->format('n')
)
]);
$resp = curl_exec($curl);
curl_close($curl);
$decoded = json_decode($resp, true);
if(json_last_error() !== JSON_ERROR_NONE) {
return [];
}
return $decoded;
}
public static function format_bytes($bytes, $decimals = 2)
{
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor];
}
}