MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //home/cafsindia/www/wp-content/plugins/imageseo/src/Actions/Admin/BackupFilename.php
<?php

namespace ImageSeoWP\Actions\Admin;

if (!defined('ABSPATH')) {
    exit;
}

class BackupFilename
{
    public function hooks()
    {
        add_action('admin_post_imageseo_backup_rename_file', [$this, 'process']);
    }

    /**
     * @return int
     */
    protected function getAttachmentId()
    {
        if ('GET' === $_SERVER['REQUEST_METHOD']) {
            return (int) $_GET['attachment_id'];
        } elseif ('POST' === $_SERVER['REQUEST_METHOD']) {
            return (int) $_POST['attachment_id'];
        }
    }

    /**
     * @since 2.0
     *
     * @return void
     */
    public function process()
    {
        $redirectUrl = admin_url('post.php?post=' . $this->getAttachmentId() . '&action=edit');

        if (!wp_verify_nonce($_GET['_wpnonce'], 'imageseo_backup_rename_file')) {
            wp_redirect($redirectUrl);
            exit;
        }

        if (!current_user_can('manage_options')) {
            wp_redirect($redirectUrl);
            exit;
        }

        $attachmentId = $this->getAttachmentId();

        $oldMetadata = get_post_meta($attachmentId, '_old_wp_attachment_metadata', true);
        $filename = '';
        if (isset($oldMetadata['original_image'])) {
            $filename = $oldMetadata['original_image'];
        } else {
            $oldFilename = explode('/', $oldMetadata['file']);
            $filename = $oldFilename[count($oldFilename) - 1];
        }

        if (!empty($filename)) {
            imageseo_get_service('UpdateFile')->updateFilename($attachmentId, $filename);
        }

        wp_redirect($redirectUrl);
    }
}