Jake Vanderwerf
2025-09-30 e5f67ddb413a1bb4f9cc191c782058dcfef2e72b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
<?php
namespace JVBase\managers;
 
use JVBase\JVB;
use JVBase\meta\MetaManager;
use Exception;
use Imagick;
use WP_Error;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
/**
 * Handles file uploads for edmonton.ink dashboard
 * Includes image processing, validation, optimization, and SEO-friendly naming
 */
class UploadManager
{
    /**
     * @var array Default configuration
     */
    protected array $config = [
        'allowed_types' => [
            'image/jpeg',
            'image/png',
            'image/gif',
            'image/webp',
        ],
        'max_size' => 5242880, // 5MB
        'convert_to_webp' => true,
        'webp_quality' => 80,
        'optimize_images' => true,
        'create_thumbnails' => true,
        'original_retention' => 2592000, // 30 days in seconds
        'use_imagick' => null // Will be set in constructor
    ];
 
    protected string $post_type;
    protected int $user_id;
    protected int $term_id;
    protected string $upload_dir;
    protected string $upload_url;
    protected object $cache;
    protected object $notifications;
    protected int $max_retries = 3;
    protected int $retry_delay = 300; // 5 minutes
 
    public function __construct($post_type, $user_id, $config = [])
    {
        $this->post_type = $post_type;
        $this->user_id = $user_id;
        $this->config = wp_parse_args($config, $this->config);
        // Check for Imagick availability
        $this->config['use_imagick'] = extension_loaded('imagick');
 
        $this->setupUploadDirs();
 
 
        // Schedule cleanup of original files
//        if (!wp_next_scheduled('jvb_cleanup_original_uploads')) {
//            wp_schedule_event(time(), 'daily', 'jvb_cleanup_original_uploads');
//        }
//        add_action('jvb_cleanup_original_uploads', [$this, 'cleanupOriginalFiles']);
 
 
        // Track upload statistics
        add_action('jvb_upload_complete', [$this, 'trackUploadStats']);
        add_action('jvb_upload_failed', [$this, 'track_failed_upload']);
    }
 
    /**
     * @param array $file_data
     *
     * @return array
     * @throws Exception
     */
    public function secureUploadedFile(array $file_data): array
    {
        // Create a persistent temporary directory if it doesn't exist
        $temp_dir = "{$this->upload_dir}/jvb_temp_uploads/{$this->user_id}";
        wp_mkdir_p($temp_dir);
 
        // Generate a unique filename for temporary storage
        $temp_filename = uniqid() . '_' . sanitize_file_name($file_data['name']);
        $temp_path = "{$temp_dir}/{$temp_filename}";
 
        // Move the uploaded file to our temporary storage
        if (!move_uploaded_file($file_data['tmp_name'], $temp_path)) {
            // Fallback to copy if needed
            if (!copy($file_data['tmp_name'], $temp_path)) {
                throw new Exception('Failed to store uploaded file');
            }
        }
 
        // Return metadata about the stored file
        return [
            'temp_path' => $temp_path,
            'original_name' => $file_data['name'],
            'mime_type' => $file_data['type'],
            'file_size' => $file_data['size'],
            'stored_at' => current_time('mysql')
        ];
    }
 
    /**
     * @param string $temp_path
     * @param string $post_type
     * @param int $post_id
     * @param int $term_id
     * @param string $original_filename
     *
     * @return array
     * @throws Exception
     */
    public function processImageFromStorage(string $temp_path, string $post_type, int $post_id = 0, int $term_id = 0, string $original_filename = ''): array
    {
        if (!file_exists($temp_path)) {
            throw new Exception('Stored file no longer exists: ' . $temp_path);
        }
 
        $this->term_id = $term_id;
        $this->post_type = $post_type;
 
        // Get base upload directory based on content type
        $rel_path = $this->getUploadDirectory();
 
        // Ensure the directory exists
        $full_upload_path = $this->upload_dir . '/' . $rel_path;
        if (!wp_mkdir_p($full_upload_path)) {
            throw new Exception("Failed to create upload directory: {$full_upload_path}");
        }
 
        // Generate filename WITHOUT extension (generateFilename should not include extension)
        $base_filename = $this->generateFilename($original_filename, get_userdata($this->user_id));
 
        // Process the image directly from our temporary storage
        return $this->processImage($temp_path, $rel_path, $base_filename, $post_id);
    }
 
    /**
     * @return void
     * @throws Exception
     */
    protected function setupUploadDirs():void
    {
        $upload_info = wp_upload_dir();
        if ($upload_info['error']) {
            throw new Exception($upload_info['error']);
        }
 
        $this->upload_dir = $upload_info['basedir'];
        $this->upload_url = $upload_info['baseurl'];
    }
 
    /**
     * @return string
     */
    public function getUploadDirectory():string
    {
        // Default WordPress organization: year/month
        $default_path = date('Y/m');
 
        /**
         * Filter the upload directory structure
         *
         * @param string $path The default upload path
         * @param string $post_type The post type being uploaded
         * @param int $user_id The user ID
         * @param int $term_id The term ID (if applicable)
         */
        return apply_filters('jvb_upload_directory', $default_path, $this->post_type, $this->user_id, $this->term_id);
    }
 
    /**
     * Generate filename with extensible filtering
     * Default: generic SEO-friendly format
     *
     * @param string $original_name
     * @param object $user_data
     * @return string
     */
    public function generateFilename(string $original_name, object $user_data): string
    {
        // Default generic filename: {post_type}-{user_id}-{counter}
        $filename = sprintf(
            '%s-%s',
            sanitize_title($this->post_type),
            $this->user_id
        );
 
        /**
         * Filter the generated filename (without extension)
         *
         * @param string $filename The generated filename
         * @param string $original_name The original filename
         * @param object $user_data WordPress user data object
         * @param string $post_type The post type
         * @param int $user_id The user ID
         * @param int $term_id The term ID (if applicable)
         */
        return apply_filters('jvb_upload_filename', $filename, $original_name, $user_data, $this->post_type, $this->user_id, $this->term_id).'-'.$this->getNextFileCounter();
    }
 
    /**
     * @return string
     */
    protected function getNextFileCounter(): string
    {
        // Get counter key for this post type
        $counter_key = 'upload_counter_' . str_replace(['/', '\\'], '_', $this->post_type);
 
        // Get current counter value, default to 0
        $counter = (int)get_user_meta($this->user_id, $counter_key, true) ?: 0;
 
        // Increment counter
        $counter++;
 
        // Update counter in user meta
        update_user_meta($this->user_id, $counter_key, $counter);
 
        // Return formatted counter
        return sprintf('%08d', $counter);
    }
 
 
    /**
     * Generate alt text with filtering for customization
     * Default: basic or empty alt text
     *
     * @param string $file
     * @param object $user_data
     * @param int|null $post_id
     * @return string
     */
    protected function generateAltText(string $file, object $user_data, int|null $post_id = null): string
    {
        // Default: basic alt text or empty
        $alt_text = '';
 
        /**
         * Filter the generated alt text
         *
         * @param string $alt_text The generated alt text
         * @param string $file The file path
         * @param object $user_data WordPress user data object
         * @param int|null $post_id The post ID (if applicable)
         * @param string $post_type The post type
         * @param int $user_id The user ID
         * @param int $term_id The term ID (if applicable)
         */
        return apply_filters('jvb_upload_alt_text', $alt_text, $file, $user_data, $post_id, $this->post_type, $this->user_id, $this->term_id);
    }
 
    /**
     * Generate image title with filtering for customization
     * Default: WordPress default behavior (filename-based)
     *
     * @param string $file
     * @param object $user_data
     * @param int|null $post_id
     * @return string
     */
    protected function generateImageTitle(string $file, object $user_data, int|null $post_id = null): string
    {
        // Default: Use filename without extension (WordPress default behavior)
        $title = pathinfo($file, PATHINFO_FILENAME);
 
        /**
         * Filter the generated image title
         *
         * @param string $title The generated title
         * @param string $file The file path
         * @param object $user_data WordPress user data object
         * @param int|null $post_id The post ID (if applicable)
         * @param string $post_type The post type
         * @param int $user_id The user ID
         * @param int $term_id The term ID (if applicable)
         */
        return apply_filters('jvb_upload_image_title', $title, $file, $user_data, $post_id, $this->post_type, $this->user_id, $this->term_id);
    }
 
    /**
     * @param array $file_data
     * @param array $options
     *
     * @return array|WP_Error
     */
    public function handleContentUpload(array $file_data, array $options = []): array|WP_Error
    {
        try {
            if (!isset($file_data['tmp_name']) || !is_uploaded_file($file_data['tmp_name'])) {
                throw new Exception('No valid file uploaded.');
            }
 
            $user_data = get_userdata($this->user_id);
            $post_id = $options['post_id'] ?? 0;
            $this->term_id = $options['term_id'] ?? 0;
 
            // Get base upload directory based on content type
            $base_dir = $this->getUploadDirectory();
            $original_dir = 'originals';
 
            $rel_path = $base_dir;
            $this->ensureUploadDirs($rel_path, $original_dir);
 
            $filename = $this->generateFilename($file_data['name'], $user_data);
 
            // Store original file
            $original_file = $this->storeOriginalFile(
                $file_data['tmp_name'],
                $base_dir,
                $original_dir,
                $filename
            );
 
            // Process immediately
            return $this->processImage($original_file, $rel_path, $filename, $post_id);
 
        } catch (Exception $e) {
            return new WP_Error('upload_failed', $e->getMessage());
        }
    }
 
    /**
     * @param string $rel_path
     * @param string $original_dir
     *
     * @return void
     * @throws Exception
     */
    protected function ensureUploadDirs($rel_path, $original_dir): void
    {
        $dirs = [
            "{$this->upload_dir}/{$rel_path}",
            "{$this->upload_dir}/{$rel_path}/{$original_dir}"
        ];
 
        foreach ($dirs as $dir) {
            if (!wp_mkdir_p($dir)) {
                throw new Exception("Failed to create directory: {$dir}");
            }
        }
    }
 
 
    /**
     * @param string $tmp_file
     * @param string $base_dir
     * @param string $original_dir
     * @param string $filename
     * @return string
     * @throws Exception
     */
    protected function storeOriginalFile(string $tmp_file, string $base_dir, string $original_dir, string $filename): string
    {
        $ext = pathinfo($tmp_file, PATHINFO_EXTENSION);
        $original_path = "{$this->upload_dir}/{$base_dir}/{$original_dir}/{$filename}.{$ext}";
 
        if (!move_uploaded_file($tmp_file, $original_path)) {
            throw new Exception('Failed to store original file');
        }
 
        return $original_path;
    }
 
    /**
     * Process image with better error handling
     */
    protected function processImage(string $original_file, string $rel_path, string $filename, int $post_id): array
    {
        // Validate the original file still exists
        if (!file_exists($original_file)) {
            throw new Exception('Original file no longer exists: ' . $original_file);
        }
 
        // Verify file type before processing
        $mime_type = mime_content_type($original_file);
        if (!in_array($mime_type, $this->config['allowed_types'])) {
            throw new Exception('Invalid file type detected during processing: ' . $mime_type);
        }
 
        // Ensure the upload directory exists
        $full_upload_dir = $this->upload_dir . '/' . $rel_path;
        if (!wp_mkdir_p($full_upload_dir)) {
            throw new Exception("Failed to create upload directory: {$full_upload_dir}");
        }
 
        // Convert to WebP if enabled
        if ($this->config['convert_to_webp'] && $mime_type !== 'image/webp') {
            $final_path = "{$full_upload_dir}/{$filename}.webp";
 
            if ($this->config['use_imagick']) {
                $this->convertWithImagick($original_file, $final_path);
            } else {
                $this->convertWithGd($original_file, $final_path);
            }
        } else {
            // Just copy the original with its extension
            $original_ext = pathinfo($original_file, PATHINFO_EXTENSION);
            $final_path = "{$full_upload_dir}/{$filename}.{$original_ext}";
 
            if (!copy($original_file, $final_path)) {
                throw new Exception("Failed to copy file from {$original_file} to {$final_path}");
            }
        }
 
        // Verify the final file was created
        if (!file_exists($final_path)) {
            throw new Exception("Final processed file was not created: {$final_path}");
        }
 
        // Generate title text
        $title = $this->generateImageTitle(
            $final_path,
            get_userdata($this->user_id),
            $post_id
        );
 
        // Create attachment with title
        $attachment_id = $this->createAttachment($final_path, $title, $post_id);
 
        // Generate thumbnails
        if ($this->config['create_thumbnails']) {
            $this->generateThumbnails($attachment_id);
        }
 
        // Update post attachments with new file info
        $this->updatePostAttachments($attachment_id, $final_path);
 
        return [
            'success' => true,
            'attachment_id' => $attachment_id,
            'url' => wp_get_attachment_url($attachment_id),
            'file' => $final_path
        ];
    }
 
    /**
     * @param string $source
     * @param string $destination
     *
     * @return void
     * @throws Exception
     */
    protected function convertWithImagick(string $source, string $destination, string $toType = 'webp'): void
    {
        $allowed = ['webp', 'jpeg', 'jpg', 'png'];
        if (!in_array($toType, $allowed)) {
            return;
        }
        try {
            $image = new Imagick($source);
            $image->setImageFormat('webp');
            $image->setImageCompressionQuality($this->config['webp_quality']);
            $image->writeImage($destination);
            $image->clear();
        } catch (Exception $e) {
            throw new Exception('WebP conversion with Imagick failed: ' . $e->getMessage());
        }
    }
 
    /**
     * Fixed convertWithGd method with better error handling
     */
    protected function convertWithGd(string $source, string $destination, string $toType = 'webp'): void
    {
        $mime_type = mime_content_type($source);
 
        // Ensure destination directory exists
        $dest_dir = dirname($destination);
        if (!wp_mkdir_p($dest_dir)) {
            throw new Exception("Failed to create destination directory: {$dest_dir}");
        }
 
        switch ($mime_type) {
            case 'image/webp':
                $image = imagecreatefromwebp($source);
                break;
            case 'image/jpeg':
                $image = imagecreatefromjpeg($source);
                break;
            case 'image/png':
                $image = imagecreatefrompng($source);
                if ($image !== false) {
                    imagepalettetotruecolor($image);
                    imagealphablending($image, true);
                    imagesavealpha($image, true);
                }
                break;
            case 'image/gif':
                $image = imagecreatefromgif($source);
                if ($image !== false) {
                    imagepalettetotruecolor($image);
                }
                break;
            default:
                throw new Exception('Unsupported image type for GD conversion: ' . $mime_type);
        }
 
        if ($image === false) {
            throw new Exception('Failed to create image resource from source file: ' . $source);
        }
 
 
        // Convert to WebP
        $result = imagewebp($image, $destination, $this->config['webp_quality']);
 
        // Clean up memory
        imagedestroy($image);
 
        if (!$result) {
            throw new Exception('WebP conversion with GD failed - imagewebp returned false');
        }
 
        // Verify the file was actually created
        if (!file_exists($destination)) {
            throw new Exception('WebP file was not created despite imagewebp returning true');
        }
    }
 
    /**
     * @return void
     */
    public function cleanupOriginalFiles(): void
    {
        $cutoff = time() - $this->config['original_retention'];
 
        // Get upload directory and find original directories
        $pattern = "{$this->upload_dir}/**/originals";
        $original_dirs = glob($pattern, GLOB_ONLYDIR);
 
        foreach ($original_dirs as $original_dir) {
            $files = glob("{$original_dir}/*");
            foreach ($files as $file) {
                if (filemtime($file) < $cutoff) {
                    unlink($file);
                }
            }
        }
    }
 
    /**
     * @param string|null $temp_dir
     * @return void
     */
    protected function cleanupTempFiles(string|null $temp_dir = null): void
    {
        if (is_null($temp_dir)) {
            $temp_dir = $this->upload_dir . '/tmp';
        }
 
        if (is_dir($temp_dir)) {
            $files = glob($temp_dir . '/*');
            foreach ($files as $file) {
                if (is_file($file)) {
                    @unlink($file);
                }
            }
            @rmdir($temp_dir);
        }
    }
 
 
    /**
     * @param int $retention_days
     * @return void
     */
    public function cleanupUserFiles(int $retention_days = 30): void
    {
        $cutoff = time() - ($retention_days * DAY_IN_SECONDS);
        $user_dir = $this->getUploadDirectory();
 
        if (!is_dir($user_dir)) {
            return;
        }
 
        $this->cleanupDirectory($user_dir, $cutoff);
    }
 
    /**
     * @param string $dir
     * @param int $cutoff
     * @return void
     */
    protected function cleanupDirectory(string $dir, int $cutoff): void
    {
        $files = glob($dir . '/*');
        foreach ($files as $file) {
            if (is_dir($file)) {
                $this->cleanupDirectory($file, $cutoff);
            } elseif (filemtime($file) < $cutoff) {
                @unlink($file);
            }
        }
    }
 
    /**
     * @param string $file
     * @param string $title
     * @param int $post_id
     * @return int|WP_Error
     * @throws Exception
     */
    protected function createAttachment(string $file, string $title, int $post_id): int|WP_Error
    {
        $file_url = str_replace($this->upload_dir, $this->upload_url, $file);
 
        $attachment = [
            'post_mime_type' => mime_content_type($file),
            'post_title' => $title,
            'post_name' => sanitize_title($title),
            'post_content' => '',
            'post_status' => 'inherit',
            'guid' => $file_url
        ];
 
        $attach_id = wp_insert_attachment($attachment, $file, $post_id);
        if (is_wp_error($attach_id)) {
            throw new Exception($attach_id->get_error_message());
        }
 
        // Generate and set alt text
        $alt_text = $this->generateAltText($file, get_userdata($this->user_id), $post_id);
        update_post_meta($attach_id, '_wp_attachment_image_alt', $alt_text);
 
        return $attach_id;
    }
 
    /**
     * Generate thumbnails using WordPress's built-in image size system
     * This will create all registered image sizes (thumbnail, medium, large, and any custom sizes)
     * Sites can register custom image sizes using add_image_size()
     *
     * @param int $attachment_id
     * @return void
     */
    protected function generateThumbnails(int $attachment_id): void
    {
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $metadata = wp_generate_attachment_metadata($attachment_id, get_attached_file($attachment_id));
        wp_update_attachment_metadata($attachment_id, $metadata);
    }
 
    /**
     * @param $result
     * @return void
     */
    protected function trackUploadStats($result)
    {
        $stats_key = "upload_stats_{$this->user_id}";
        $stats = wp_cache_get($stats_key) ?: [
            'total_uploads' => 0,
            'successful_uploads' => 0,
            'failed_uploads' => 0,
            'total_size' => 0,
            'last_upload' => null
        ];
 
        if ($result['success']) {
            $stats['successful_uploads']++;
            $stats['total_size'] += filesize($result['file']);
        } else {
            $stats['failed_uploads']++;
        }
 
        $stats['total_uploads']++;
        $stats['last_upload'] = current_time('mysql');
 
        wp_cache_set($stats_key, $stats, '', DAY_IN_SECONDS);
    }
 
    /**
     * @param int $attachment_id
     * @param string $new_file_path
     * @return void
     */
    protected function updatePostAttachments(int $attachment_id, string $new_file_path): void
    {
        // Update attachment post
        $file_url = str_replace($this->upload_dir, $this->upload_url, $new_file_path);
        $filename = basename($new_file_path);
 
        wp_update_post([
            'ID' => $attachment_id,
            'guid' => $file_url,
            'post_mime_type' => mime_content_type($new_file_path),
            'post_title' => $filename
        ]);
 
        // Update attachment metadata
        update_post_meta($attachment_id, '_wp_attached_file', str_replace($this->upload_dir . '/', '', $new_file_path));
 
        // Update attachment metadata including sizes
        $metadata = wp_get_attachment_metadata($attachment_id);
        if ($metadata) {
            $metadata['file'] = str_replace($this->upload_dir . '/', '', $new_file_path);
 
            // Update thumbnail paths if they exist
            if (!empty($metadata['sizes'])) {
                foreach ($metadata['sizes'] as $size => $info) {
                    $old_file = $info['file'];
                    $new_file = preg_replace(
                        '/\.(jpe?g|png|gif)$/i',
                        '.webp',
                        $old_file
                    );
                    $metadata['sizes'][$size]['file'] = $new_file;
                    $metadata['sizes'][$size]['mime-type'] = 'image/webp';
                }
            }
 
            wp_update_attachment_metadata($attachment_id, $metadata);
        }
 
        // If this is a profile/featured image, update those references
        $post_id = wp_get_post_parent_id($attachment_id);
        if ($post_id) {
            $featured_image_id = get_post_thumbnail_id($post_id);
            if ($featured_image_id === $attachment_id) {
                // Re-set the featured image to trigger any necessary updates
                set_post_thumbnail($post_id, $attachment_id);
            }
        }
 
        // Clear any caches
        clean_attachment_cache($attachment_id);
        clean_post_cache($post_id);
    }
 
    /**
     * Clean up empty temporary directories
     *
     * @param int $user_id
     * @return void
     */
    public function cleanupEmptyTempDirs(int $user_id): void
    {
        $temp_dir = "{$this->upload_dir}/jvb_temp_uploads/{$user_id}";
 
        if (is_dir($temp_dir)) {
            // Check if directory is empty
            $files = scandir($temp_dir);
            $is_empty = (count($files) <= 2); // Only . and .. entries
 
            if ($is_empty) {
                // Try to remove the empty directory
                @rmdir($temp_dir);
 
                // Also check if parent temp directory is empty
                $parent_temp_dir = "{$this->upload_dir}/jvb_temp_uploads";
                $parent_files = scandir($parent_temp_dir);
                $parent_is_empty = (count($parent_files) <= 2); // Only . and .. entries
 
                if ($parent_is_empty) {
                    @rmdir($parent_temp_dir);
                }
            }
        }
    }
}