mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-11-02 03:16:11 +01:00
Always delete image alternatives in AdminController#taskDelmedia (#814)
* Always delete image alternatives in AdminController#taskDelmedia Previously, image alternatives would only be deleted if the base image wasn't found, and only the first three alternatives would be removed. This commit changes that, so that all image alternatives are always deleted when the base image is. * Remove use of $deleteCount in AdminController#taskDelmedia
This commit is contained in:
committed by
Flavio Copes
parent
4dbeb5ab57
commit
96ac75379f
@@ -1275,62 +1275,54 @@ class AdminController
|
||||
}
|
||||
|
||||
$filename = !empty($this->post['filename']) ? $this->post['filename'] : null;
|
||||
if ($filename) {
|
||||
$targetPath = $page->path() . '/' . $filename;
|
||||
|
||||
if (file_exists($targetPath)) {
|
||||
if (unlink($targetPath)) {
|
||||
$this->admin->json_response = [
|
||||
'status' => 'success',
|
||||
'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_DELETED') . ': ' . $filename
|
||||
];
|
||||
} else {
|
||||
$this->admin->json_response = [
|
||||
'status' => 'error',
|
||||
'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_COULD_NOT_BE_DELETED') . ': ' . $filename
|
||||
];
|
||||
}
|
||||
} else {
|
||||
//Try with responsive images @1x, @2x, @3x
|
||||
$ext = pathinfo($targetPath, PATHINFO_EXTENSION);
|
||||
$fullPathFilename = $page->path() . '/' . basename($targetPath, ".$ext");
|
||||
$responsiveTargetPath = $fullPathFilename . '@1x.' . $ext;
|
||||
|
||||
$deletedResponsiveImage = false;
|
||||
if (file_exists($responsiveTargetPath) && unlink($responsiveTargetPath)) {
|
||||
$deletedResponsiveImage = true;
|
||||
}
|
||||
|
||||
$responsiveTargetPath = $fullPathFilename . '@2x.' . $ext;
|
||||
if (file_exists($responsiveTargetPath) && unlink($responsiveTargetPath)) {
|
||||
$deletedResponsiveImage = true;
|
||||
}
|
||||
|
||||
$responsiveTargetPath = $fullPathFilename . '@3x.' . $ext;
|
||||
if (file_exists($responsiveTargetPath) && unlink($responsiveTargetPath)) {
|
||||
$deletedResponsiveImage = true;
|
||||
}
|
||||
|
||||
if ($deletedResponsiveImage) {
|
||||
$this->admin->json_response = [
|
||||
'status' => 'success',
|
||||
'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_DELETED') . ': ' . $filename
|
||||
];
|
||||
} else {
|
||||
$this->admin->json_response = [
|
||||
'status' => 'error',
|
||||
'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_NOT_FOUND') . ': ' . $filename
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
if (!$filename) {
|
||||
$this->admin->json_response = [
|
||||
'status' => 'error',
|
||||
'message' => $this->admin->translate('PLUGIN_ADMIN.NO_FILE_FOUND')
|
||||
];
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$targetPath = $page->path() . '/' . $filename;
|
||||
|
||||
if (!file_exists($targetPath)) {
|
||||
$this->admin->json_response = [
|
||||
'status' => 'error',
|
||||
'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_NOT_FOUND') . ': ' . $filename
|
||||
];
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$fileParts = pathinfo($filename);
|
||||
$result = unlink($targetPath);
|
||||
|
||||
if (!$result) {
|
||||
$this->admin->json_response = [
|
||||
'status' => 'error',
|
||||
'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_COULD_NOT_BE_DELETED') . ': ' . $filename
|
||||
];
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (scandir($page->path()) as $file) {
|
||||
if (preg_match("/{$fileParts['filename']}@\d+x\.{$fileParts['extension']}$/", $file)) {
|
||||
$result = unlink($page->path() . '/' . $file);
|
||||
|
||||
if ($result) {
|
||||
$deleteCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->admin->json_response = [
|
||||
'status' => 'success',
|
||||
'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_DELETED') . ': ' . $filename
|
||||
];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user