-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-addon-media-rename.php
More file actions
145 lines (115 loc) · 5.07 KB
/
admin-addon-media-rename.php
File metadata and controls
145 lines (115 loc) · 5.07 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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
/**
* Class AdminAddonMediaRenamePlugin
* @package Grav\Plugin
*/
class AdminAddonMediaRenamePlugin extends Plugin {
const ROUTE = '/admin-addon-media-rename';
public static function getSubscribedEvents() {
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public function getPath() {
return '/' . trim($this->grav['admin']->base, '/') . '/' . trim(self::ROUTE, '/');
}
public function buildUrl() {
return rtrim($this->grav['uri']->rootUrl(true), '/') . '/' . trim($this->getPath(), '/');
}
public function onPluginsInitialized() {
if (!$this->isAdmin() || !$this->grav['user']->authenticated) {
return;
}
if ($this->grav['uri']->path() == $this->getPath()) {
$this->enable(['onPagesInitialized' => ['processRenameRequest', 0]]);
return;
}
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPagesInitialized' => ['onTwigExtensions', 0],
'onAdminTaskExecute' => ['onAdminTaskExecute', 0],
]);
}
public function onAdminTaskExecute($e) {
$method = $e['method'];
if ($method === 'taskAdminAddonMediaRenameDoRename') {
// Make sure we have all the data we need
if (!isset($_POST['file_name']) || !isset($_POST['new_file_name'])) {
$this->outputError($this->grav['language']->translate(['PLUGIN_ADMIN_ADDON_MEDIA_RENAME.ERROR.INVALID_INPUT']));
}
$fileName = $_POST['file_name'];
$newFileName = $_POST['new_file_name'];
$replaceAll = (isset($_POST['replace_all'])) ? $_POST['replace_all'] : false;
$page = $this->grav['admin']->page(true);
// Only process changes
if ($fileName != $newFileName) {
// Locate the media file
$basePath = $page->path() . DS;
$filePath = $basePath . $fileName;
if (!file_exists($filePath)) {
$this->outputError($this->grav['language']->translate(['PLUGIN_ADMIN_ADDON_MEDIA_RENAME.ERROR.FILE_NOT_FOUND', $filePath]));
}
$newFilePath = $basePath . $newFileName;
if (file_exists($newFilePath)) {
$this->outputError($this->grav['language']->translate(['PLUGIN_ADMIN_ADDON_MEDIA_RENAME.ERROR.NEW_FILE_EXISTS', $filePath, $newFilePath]));
}
if (!rename($filePath, $newFilePath)) {
$this->outputError($this->grav['language']->translate(['PLUGIN_ADMIN_ADDON_MEDIA_RENAME.ERROR.RENAME_FAILED', $filePath, $newFilePath]));
}
if ($replaceAll) {
$oldUrl = $page->url() . '/' . $fileName;
$instances = $this->grav['pages']->instances();
foreach ($instances as $page) {
$raw = $page->raw();
// Find all links
preg_match_all('/(\[[^\]]{0,}\])\(([^\)]{0,})\)/', $raw, $matches);
// Do replace
$replaces = 0;
foreach ($matches[0] as $k => $m) {
$url = $matches[2][$k];
$normalizedUrl = $this->grav['uri']->isExternal($url) ? $url : \Grav\Common\Utils::normalizePath($page->url() . '/' . $url);
if ($normalizedUrl === $oldUrl) {
$oldUrl = $matches[2][$k];
$parts = explode('/', $oldUrl);
$lastPart = count($parts) - 1;
$parts[$lastPart] = $newFileName;
$newUrl = implode('/', $parts);
$newLink = $matches[1][$k] . '(' . $newUrl . ')';
$raw = str_replace($matches[0][$k], $newLink, $raw);
$replaces++;
}
}
if ($replaces > 0) {
$page->raw($raw);
$page->save();
}
}
}
// Everything went fine
header('HTTP/1.1 200 OK');
die('{}');
} else {
$this->outputError($this->grav['language']->translate(['PLUGIN_ADMIN_ADDON_MEDIA_RENAME.ERROR.NO_CHANGES']));
}
}
return false;
}
public function onTwigTemplatePaths() {
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function onTwigExtensions() {
$modal = $this->grav['twig']->twig()->render('rename-modal.twig.html', $this->config->get('plugins.admin-addon-media-rename.modal'));
$page = $this->grav['admin']->page(true);
$jsConfig = ['PATH' => rtrim($this->grav['uri']->rootUrl(true), '/') . '/' . trim($this->getPath(), '/') . '/' . $page->route() . '/task:adminAddonMediaRenameDoRename', 'MODAL' => $modal];
$this->grav['assets']->addInlineJs('var ADMIN_ADDON_MEDIA_RENAME = ' . json_encode($jsConfig) . ';', -1000, false);
$this->grav['assets']->addCss('plugin://admin-addon-media-rename/admin-addon-media-rename.css', -1000, false);
$this->grav['assets']->addJs('plugin://admin-addon-media-rename/admin-addon-media-rename.js', -1000, false);
}
public function outputError($msg) {
header('HTTP/1.1 400 Bad Request');
die(json_encode(['error' => ['msg' => $msg]]));
}
}