From 4b209c1a7d47647ad5235540a680163dfc1983e3 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Fri, 2 Jun 2023 12:13:41 +0100
Subject: [PATCH] Rename the cleaned up file to the original uploaded one

Should avoid failure of the is_uploaded_file() check.
---
 wp-mat.php | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/wp-mat.php b/wp-mat.php
index 694f051..9c7700a 100644
--- a/wp-mat.php
+++ b/wp-mat.php
@@ -3,7 +3,7 @@
  * Plugin Name: wp-mat
  * Plugin URI: https://git.autistici.org/noblogs/wp-mat
  * Description: Process uploaded files through MAT
- * Version: 0.0.8
+ * Version: 0.0.9
  * Author: Autistici/Inventati
  * Author URI: https://www.autistici.org/
  * License: MIT
@@ -73,11 +73,15 @@ function wp_mat_handle_upload_prefilter($file) {
     // file with the sanitized one.
     $new_tmp_file = tempnam("", "wp_mat_");
     if (wp_mat_issue_request($file['tmp_name'], $file['type'], $new_tmp_file)) {
+        // Rename the cleaned up file to the original file, or WP
+        // will not pass the is_uploaded_file() check in wp_handle_upload().
         unlink($file['tmp_name']);
+        rename($new_tmp_file, $file['tmp_name']);
+
         $new_file = array(
             "name" => $file['name'],
             "type" => $file['type'],
-            "tmp_name" => $new_tmp_file,
+            "tmp_name" => $file['tmp_name'],
             "size" => filesize($new_tmp_file),
             "error" => 0,
         );
-- 
GitLab