@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin Name: UUID File Renamer
|
* Plugin Name: UUID File Renamer
|
||||||
* Description: Dieses Plugin benennt hochgeladene Dateien automatisch in eine UUID um.
|
* 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.1
|
* Version: 1.4
|
||||||
* 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
|
||||||
@ -16,15 +16,110 @@ add_filter('wp_handle_upload_prefilter', 'rename_file_to_uuid');
|
|||||||
|
|
||||||
function rename_file_to_uuid($file)
|
function rename_file_to_uuid($file)
|
||||||
{
|
{
|
||||||
// Generiere eine UUID
|
|
||||||
$uuid = wp_generate_uuid4();
|
$uuid = wp_generate_uuid4();
|
||||||
|
|
||||||
// Dateiendung beibehalten
|
|
||||||
$file_info = pathinfo($file['name']);
|
$file_info = pathinfo($file['name']);
|
||||||
$extension = isset($file_info['extension']) ? '.' . $file_info['extension'] : '';
|
$extension = isset($file_info['extension']) ? '.' . $file_info['extension'] : '';
|
||||||
|
|
||||||
// Neuen Dateinamen setzen
|
|
||||||
$file['name'] = $uuid . $extension;
|
$file['name'] = $uuid . $extension;
|
||||||
|
|
||||||
return $file;
|
return $file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Medienbearbeitungsseite erweitern
|
||||||
|
add_filter('attachment_fields_to_edit', 'add_uuid_rename_button', 10, 2);
|
||||||
|
function add_uuid_rename_button($form_fields, $post) {
|
||||||
|
$form_fields['uuid_rename'] = array(
|
||||||
|
'label' => __('UUID umbenennen', 'uuid-file-renamer'),
|
||||||
|
'input' => 'html',
|
||||||
|
'html' => '<button type="button" class="button rename-to-uuid" data-id="' . $post->ID . '">' . __('In UUID umbenennen', 'uuid-file-renamer') . '</button>'
|
||||||
|
);
|
||||||
|
return $form_fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
// AJAX-Handler für Einzel-Umbenennung
|
||||||
|
add_action('wp_ajax_rename_media_to_uuid', 'rename_media_to_uuid_ajax');
|
||||||
|
function rename_media_to_uuid_ajax() {
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
wp_send_json_error(__('Keine Berechtigung.', 'uuid-file-renamer'));
|
||||||
|
}
|
||||||
|
if (!isset($_POST['attachment_id'])) {
|
||||||
|
wp_send_json_error(__('Fehlende Anhangs-ID.', 'uuid-file-renamer'));
|
||||||
|
}
|
||||||
|
$attachment_id = intval($_POST['attachment_id']);
|
||||||
|
rename_existing_media_to_uuid($attachment_id);
|
||||||
|
wp_send_json_success(__('Datei erfolgreich umbenannt.', 'uuid-file-renamer'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Medienübersicht erweitern
|
||||||
|
add_filter('bulk_actions-upload', 'register_bulk_uuid_rename');
|
||||||
|
function register_bulk_uuid_rename($bulk_actions) {
|
||||||
|
$bulk_actions['rename_to_uuid'] = __('In UUID umbenennen', 'uuid-file-renamer');
|
||||||
|
return $bulk_actions;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Batch-Umbenennung verarbeiten
|
||||||
|
add_filter('handle_bulk_actions-upload', 'handle_bulk_uuid_rename', 10, 3);
|
||||||
|
function handle_bulk_uuid_rename($redirect_to, $doaction, $attachment_ids) {
|
||||||
|
if ($doaction !== 'rename_to_uuid') {
|
||||||
|
return $redirect_to;
|
||||||
|
}
|
||||||
|
foreach ($attachment_ids as $attachment_id) {
|
||||||
|
rename_existing_media_to_uuid($attachment_id);
|
||||||
|
}
|
||||||
|
$redirect_to = add_query_arg('bulk_uuid_renamed', count($attachment_ids), $redirect_to);
|
||||||
|
return $redirect_to;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funktion zum Umbenennen bestehender Medien inkl. Thumbnails
|
||||||
|
function rename_existing_media_to_uuid($attachment_id) {
|
||||||
|
$uuid = wp_generate_uuid4();
|
||||||
|
$file_path = get_attached_file($attachment_id);
|
||||||
|
$file_info = pathinfo($file_path);
|
||||||
|
$new_file_path = $file_info['dirname'] . '/' . $uuid . '.' . $file_info['extension'];
|
||||||
|
|
||||||
|
// Metadaten der Bilder abrufen
|
||||||
|
$metadata = wp_get_attachment_metadata($attachment_id);
|
||||||
|
$upload_dir = wp_upload_dir();
|
||||||
|
|
||||||
|
if (rename($file_path, $new_file_path)) {
|
||||||
|
update_attached_file($attachment_id, $new_file_path);
|
||||||
|
wp_update_post(array(
|
||||||
|
'ID' => $attachment_id,
|
||||||
|
'post_title' => $uuid,
|
||||||
|
'post_name' => $uuid
|
||||||
|
));
|
||||||
|
|
||||||
|
// Thumbnails umbenennen
|
||||||
|
if (!empty($metadata['sizes'])) {
|
||||||
|
foreach ($metadata['sizes'] as $size => $data) {
|
||||||
|
$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)) {
|
||||||
|
rename($old_thumb_path, $new_thumb_path);
|
||||||
|
$metadata['sizes'][$size]['file'] = basename($new_thumb_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wp_update_attachment_metadata($attachment_id, $metadata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// JS für Button hinzufügen
|
||||||
|
add_action('admin_footer', 'uuid_renamer_js');
|
||||||
|
function uuid_renamer_js() {
|
||||||
|
echo '<script>
|
||||||
|
jQuery(document).ready(function($) {
|
||||||
|
$(document).on("click", ".rename-to-uuid", function() {
|
||||||
|
var button = $(this);
|
||||||
|
var attachmentId = button.data("id");
|
||||||
|
$.post(ajaxurl, { action: "rename_media_to_uuid", attachment_id: attachmentId }, function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
alert(response.data);
|
||||||
|
location.reload();
|
||||||
|
} else {
|
||||||
|
alert(response.data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>';
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user