@@ -196,24 +196,21 @@ public function create_diagnostic_zip() {
196196 );
197197 }
198198
199- $ upload_dir = wp_upload_dir ();
200- $ temp_dir = trailingslashit ( $ upload_dir ['basedir ' ] ) . 'tiny-compress-temp ' ;
199+ $ temp_dir = trailingslashit ( get_temp_dir () ) . 'tiny-compress-temp ' ;
201200 if ( ! file_exists ( $ temp_dir ) ) {
202201 wp_mkdir_p ( $ temp_dir );
203202 }
204203
205- $ zip_filename = 'tiny-compress-diagnostics- ' . gmdate ( 'Y-m-d-His ' ) . '.zip ' ;
206- $ zip_path = trailingslashit ( $ temp_dir ) . $ zip_filename ;
204+ $ temp_path = tempnam ($ temp_dir , 'tiny-compress-diagnostics- ' . gmdate ( 'Y-m-d-His ' ) );
207205
208206 $ zip = new ZipArchive ();
209- if ( true !== $ zip ->open ( $ zip_path , ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) {
207+ if ( true !== $ zip ->open ( $ temp_path , ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) {
210208 return new WP_Error ( 'zip_create_failed ' ,
211209 __ ( 'Failed to create zip file. ' ,
212210 'tiny-compress-images ' )
213211 );
214212 }
215213
216- // Add diagnostic info.
217214 $ info = self ::collect_info ();
218215 $ zip ->addFromString ( 'tiny-diagnostics.json ' , wp_json_encode ( $ info , JSON_PRETTY_PRINT ) );
219216
@@ -227,7 +224,7 @@ public function create_diagnostic_zip() {
227224 }
228225
229226 $ zip ->close ();
230- return $ zip_path ;
227+ return $ temp_path ;
231228 }
232229
233230 /**
@@ -243,7 +240,7 @@ public static function download_zip( $zip_path ) {
243240 }
244241
245242 header ( 'Content-Type: application/zip ' );
246- header ( 'Content-Disposition: attachment; filename=" ' . basename ( $ zip_path ) . ' " ' );
243+ header ( 'Content-Disposition: attachment; filename="tiny-compress-diagnostics.zip " ' );
247244 header ( 'Content-Length: ' . filesize ( $ zip_path ) );
248245 header ( 'Pragma: no-cache ' );
249246 header ( 'Expires: 0 ' );
@@ -257,28 +254,4 @@ public static function download_zip( $zip_path ) {
257254
258255 exit ;
259256 }
260-
261- /**
262- * Cleans up old diagnostic zip files.
263- *
264- * @since 3.7.0
265- */
266- public static function cleanup_old_diagnostics () {
267- $ upload_dir = wp_upload_dir ();
268- $ temp_dir = trailingslashit ( $ upload_dir ['basedir ' ] ) . 'tiny-compress-temp ' ;
269-
270- if ( ! file_exists ( $ temp_dir ) ) {
271- return ;
272- }
273-
274- $ files = glob ( trailingslashit ( $ temp_dir ) . 'tiny-compress-diagnostics-*.zip ' );
275- $ max_age = DAY_IN_SECONDS ; // 1 day.
276-
277- foreach ( $ files as $ file ) {
278- if ( file_exists ( $ file ) && (time () - filemtime ( $ file )) > $ max_age ) {
279- // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
280- unlink ( $ file );
281- }
282- }
283- }
284257}
0 commit comments