Compare commits
7 Commits
v1.4
...
084c92b9e1
Author | SHA1 | Date | |
---|---|---|---|
084c92b9e1 | |||
226fe6ae43 | |||
5614f47939 | |||
41bb7f855e | |||
a30032f1ca | |||
0fbd2be3e6 | |||
af2e4cbdd6 |
@ -4,7 +4,7 @@
|
|||||||
* Plugin Name: UUID File Renamer
|
* Plugin Name: UUID File Renamer
|
||||||
* Description: Dieses Plugin benennt hochgeladene Dateien automatisch in eine UUID um und ermöglicht das Umbenennen bestehender Medien direkt in der Mediathek.
|
* Description: Dieses Plugin benennt hochgeladene Dateien automatisch in eine UUID um und ermöglicht das Umbenennen bestehender Medien direkt in der Mediathek.
|
||||||
* Plugin URI: https://gitea.gnilebein.de/gnilebein/wordpres-uuid-file-renamer
|
* Plugin URI: https://gitea.gnilebein.de/gnilebein/wordpres-uuid-file-renamer
|
||||||
* Version: 1.4
|
* Version: 1.5
|
||||||
* Author: Patrick Niebeling <patrick@niebel.ing>
|
* Author: Patrick Niebeling <patrick@niebel.ing>
|
||||||
* Author URI: https://gnilebein.de
|
* Author URI: https://gnilebein.de
|
||||||
* Text Domain: uuid-file-renamer
|
* Text Domain: uuid-file-renamer
|
||||||
@ -34,17 +34,30 @@ function add_uuid_rename_button($form_fields, $post) {
|
|||||||
return $form_fields;
|
return $form_fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Link in der Mediathek hinzufügen
|
||||||
|
add_filter('media_row_actions', 'add_uuid_rename_link', 10, 2);
|
||||||
|
function add_uuid_rename_link($actions, $post) {
|
||||||
|
if ($post->post_type === 'attachment') {
|
||||||
|
$actions['rename_to_uuid'] = '<a href="#" class="rename-to-uuid" data-id="' . $post->ID . '">' . __('In UUID umbenennen', 'uuid-file-renamer') . '</a>';
|
||||||
|
}
|
||||||
|
return $actions;
|
||||||
|
}
|
||||||
|
|
||||||
// AJAX-Handler für Einzel-Umbenennung
|
// AJAX-Handler für Einzel-Umbenennung
|
||||||
add_action('wp_ajax_rename_media_to_uuid', 'rename_media_to_uuid_ajax');
|
add_action('wp_ajax_rename_media_to_uuid', 'rename_media_to_uuid_ajax');
|
||||||
function rename_media_to_uuid_ajax() {
|
function rename_media_to_uuid_ajax() {
|
||||||
if (!current_user_can('manage_options')) {
|
if (!current_user_can('manage_options')) {
|
||||||
|
set_transient('uuid_rename_error_message', __('Keine Berechtigung.', 'uuid-file-renamer'), 5);
|
||||||
wp_send_json_error(__('Keine Berechtigung.', 'uuid-file-renamer'));
|
wp_send_json_error(__('Keine Berechtigung.', 'uuid-file-renamer'));
|
||||||
}
|
}
|
||||||
if (!isset($_POST['attachment_id'])) {
|
if (!isset($_POST['attachment_id'])) {
|
||||||
|
set_transient('uuid_rename_error_message', __('Fehlende Anhangs-ID.', 'uuid-file-renamer'), 5);
|
||||||
wp_send_json_error(__('Fehlende Anhangs-ID.', 'uuid-file-renamer'));
|
wp_send_json_error(__('Fehlende Anhangs-ID.', 'uuid-file-renamer'));
|
||||||
}
|
}
|
||||||
$attachment_id = intval($_POST['attachment_id']);
|
$attachment_id = intval($_POST['attachment_id']);
|
||||||
rename_existing_media_to_uuid($attachment_id);
|
rename_existing_media_to_uuid($attachment_id);
|
||||||
|
|
||||||
|
set_transient('uuid_rename_success_message', __('Datei erfolgreich umbenannt.', 'uuid-file-renamer'), 5);
|
||||||
wp_send_json_success(__('Datei erfolgreich umbenannt.', 'uuid-file-renamer'));
|
wp_send_json_success(__('Datei erfolgreich umbenannt.', 'uuid-file-renamer'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,10 +74,29 @@ function handle_bulk_uuid_rename($redirect_to, $doaction, $attachment_ids) {
|
|||||||
if ($doaction !== 'rename_to_uuid') {
|
if ($doaction !== 'rename_to_uuid') {
|
||||||
return $redirect_to;
|
return $redirect_to;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$success_count = 0;
|
||||||
|
$error_count = 0;
|
||||||
|
|
||||||
foreach ($attachment_ids as $attachment_id) {
|
foreach ($attachment_ids as $attachment_id) {
|
||||||
rename_existing_media_to_uuid($attachment_id);
|
$result = rename_existing_media_to_uuid($attachment_id);
|
||||||
|
if ($result) {
|
||||||
|
$success_count++;
|
||||||
|
} else {
|
||||||
|
$error_count++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$redirect_to = add_query_arg('bulk_uuid_renamed', count($attachment_ids), $redirect_to);
|
|
||||||
|
// Erfolgreiche und fehlgeschlagene Umbenennungen speichern
|
||||||
|
if ($success_count > 0) {
|
||||||
|
set_transient('uuid_rename_bulk_success_message', sprintf(__('Es wurden %d Mediendateien erfolgreich umbenannt.', 'uuid-file-renamer'), $success_count), 5);
|
||||||
|
}
|
||||||
|
if ($error_count > 0) {
|
||||||
|
set_transient('uuid_rename_bulk_error_message', sprintf(__('Fehler beim Umbenennen von %d Mediendateien.', 'uuid-file-renamer'), $error_count), 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Umleitungs-URL mit Nachricht
|
||||||
|
$redirect_to = add_query_arg('bulk_uuid_renamed', $success_count, $redirect_to);
|
||||||
return $redirect_to;
|
return $redirect_to;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +107,7 @@ function rename_existing_media_to_uuid($attachment_id) {
|
|||||||
$file_info = pathinfo($file_path);
|
$file_info = pathinfo($file_path);
|
||||||
$new_file_path = $file_info['dirname'] . '/' . $uuid . '.' . $file_info['extension'];
|
$new_file_path = $file_info['dirname'] . '/' . $uuid . '.' . $file_info['extension'];
|
||||||
|
|
||||||
// Metadaten der Bilder abrufen
|
// Metadaten abrufen
|
||||||
$metadata = wp_get_attachment_metadata($attachment_id);
|
$metadata = wp_get_attachment_metadata($attachment_id);
|
||||||
$upload_dir = wp_upload_dir();
|
$upload_dir = wp_upload_dir();
|
||||||
|
|
||||||
@ -87,39 +119,78 @@ function rename_existing_media_to_uuid($attachment_id) {
|
|||||||
'post_name' => $uuid
|
'post_name' => $uuid
|
||||||
));
|
));
|
||||||
|
|
||||||
// Thumbnails umbenennen
|
// Thumbnails umbenennen, falls vorhanden
|
||||||
if (!empty($metadata['sizes'])) {
|
if (!empty($metadata['sizes'])) {
|
||||||
foreach ($metadata['sizes'] as $size => $data) {
|
foreach ($metadata['sizes'] as $size => $data) {
|
||||||
$old_thumb_path = $upload_dir['basedir'] . '/' . dirname($metadata['file']) . '/' . $data['file'];
|
$old_thumb_path = $upload_dir['basedir'] . '/' . dirname($metadata['file']) . '/' . $data['file'];
|
||||||
$thumb_info = pathinfo($old_thumb_path);
|
|
||||||
$new_thumb_path = $thumb_info['dirname'] . '/' . $uuid . '-' . $data['width'] . 'x' . $data['height'] . '.' . $thumb_info['extension'];
|
|
||||||
if (file_exists($old_thumb_path)) {
|
if (file_exists($old_thumb_path)) {
|
||||||
|
$thumb_info = pathinfo($old_thumb_path);
|
||||||
|
$new_thumb_path = $thumb_info['dirname'] . '/' . $uuid . '-' . $data['width'] . 'x' . $data['height'] . '.' . $thumb_info['extension'];
|
||||||
rename($old_thumb_path, $new_thumb_path);
|
rename($old_thumb_path, $new_thumb_path);
|
||||||
$metadata['sizes'][$size]['file'] = basename($new_thumb_path);
|
$metadata['sizes'][$size]['file'] = basename($new_thumb_path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wp_update_attachment_metadata($attachment_id, $metadata);
|
wp_update_attachment_metadata($attachment_id, $metadata);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// JS für Button hinzufügen
|
// JS für Buttons hinzufügen
|
||||||
add_action('admin_footer', 'uuid_renamer_js');
|
add_action('admin_footer', 'uuid_renamer_js');
|
||||||
function uuid_renamer_js() {
|
function uuid_renamer_js() {
|
||||||
echo '<script>
|
echo '<script>
|
||||||
jQuery(document).ready(function($) {
|
jQuery(document).ready(function($) {
|
||||||
$(document).on("click", ".rename-to-uuid", function() {
|
$(document).on("click", ".rename-to-uuid", function(event) {
|
||||||
|
event.preventDefault();
|
||||||
var button = $(this);
|
var button = $(this);
|
||||||
var attachmentId = button.data("id");
|
var attachmentId = button.data("id");
|
||||||
$.post(ajaxurl, { action: "rename_media_to_uuid", attachment_id: attachmentId }, function(response) {
|
$.post(ajaxurl, { action: "rename_media_to_uuid", attachment_id: attachmentId }, function(response) {
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
alert(response.data);
|
location.reload(); // Die Seite wird nach erfolgreichem Umbenennen neu geladen
|
||||||
location.reload();
|
|
||||||
} else {
|
} else {
|
||||||
alert(response.data);
|
set_transient("uuid_rename_error_message", response.data, 5);
|
||||||
|
location.reload(); // Die Seite wird nach Fehler ebenfalls neu geladen
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>';
|
</script>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Erfolgs- und Fehlermeldungen im Admin-Bereich anzeigen
|
||||||
|
add_action('admin_notices', 'uuid_rename_message');
|
||||||
|
function uuid_rename_message() {
|
||||||
|
// Erfolgsnachricht anzeigen
|
||||||
|
if ($message = get_transient('uuid_rename_success_message')) {
|
||||||
|
echo '<div class="notice notice-success is-dismissible">';
|
||||||
|
echo '<p>' . esc_html($message) . '</p>';
|
||||||
|
echo '</div>';
|
||||||
|
delete_transient('uuid_rename_success_message');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fehlernachricht anzeigen
|
||||||
|
if ($error_message = get_transient('uuid_rename_error_message')) {
|
||||||
|
echo '<div class="notice notice-error is-dismissible">';
|
||||||
|
echo '<p>' . esc_html($error_message) . '</p>';
|
||||||
|
echo '</div>';
|
||||||
|
delete_transient('uuid_rename_error_message');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bulk Erfolgsnachricht anzeigen
|
||||||
|
if ($message = get_transient('uuid_rename_bulk_success_message')) {
|
||||||
|
echo '<div class="notice notice-success is-dismissible">';
|
||||||
|
echo '<p>' . esc_html($message) . '</p>';
|
||||||
|
echo '</div>';
|
||||||
|
delete_transient('uuid_rename_bulk_success_message');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bulk Fehlernachricht anzeigen
|
||||||
|
if ($error_message = get_transient('uuid_rename_bulk_error_message')) {
|
||||||
|
echo '<div class="notice notice-error is-dismissible">';
|
||||||
|
echo '<p>' . esc_html($error_message) . '</p>';
|
||||||
|
echo '</div>';
|
||||||
|
delete_transient('uuid_rename_bulk_error_message');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user