Compare commits
2 Commits
5614f47939
...
v1.6
Author | SHA1 | Date | |
---|---|---|---|
084c92b9e1 | |||
226fe6ae43 |
@ -47,13 +47,17 @@ function add_uuid_rename_link($actions, $post) {
|
|||||||
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'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +132,9 @@ function rename_existing_media_to_uuid($attachment_id) {
|
|||||||
}
|
}
|
||||||
wp_update_attachment_metadata($attachment_id, $metadata);
|
wp_update_attachment_metadata($attachment_id, $metadata);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// JS für Buttons hinzufügen
|
// JS für Buttons hinzufügen
|
||||||
@ -123,13 +148,49 @@ function uuid_renamer_js() {
|
|||||||
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