| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Models; |
| 4 | 4 | |
| 5 | +use FluentSupport\App\Services\Helper; | |
| 6 | + | |
| 5 | 7 | class Attachment extends Model |
| 6 | 8 | { |
| 7 | 9 | protected $table = 'fs_attachments'; |
| 8 | 10 | |
| @@ -24,20 +26,64 @@ | ||
| 24 | 26 | 'full_url', |
| 25 | 27 | 'title', |
| 26 | 28 | 'driver', |
| 27 | 29 | 'file_size', |
| 28 | - 'status' | |
| 30 | + 'status', | |
| 31 | + 'settings' | |
| 29 | 32 | ]; |
| 30 | 33 | |
| 31 | 34 | public static function boot() |
| 32 | 35 | { |
| 36 | + parent::boot(); | |
| 37 | + | |
| 33 | 38 | static::creating(function ($model) { |
| 34 | 39 | $uid = wp_generate_uuid4(); |
| 35 | - $model->file_hash = md5($uid.mt_rand(0,1000)); | |
| 40 | + $model->file_hash = md5($uid . wp_rand(0, 1000)); | |
| 36 | 41 | }); |
| 37 | 42 | } |
| 38 | 43 | |
| 39 | 44 | /** |
| 45 | + * Delete a collection of attachments — physical files first, then DB records. | |
| 46 | + * Handles local files and fires the remote-driver hook for cloud-stored files. | |
| 47 | + * Optionally removes the ticket upload directory after files are purged. | |
| 48 | + * | |
| 49 | + * @param \FluentSupport\Framework\Database\Orm\Collection|array $attachments | |
| 50 | + * @param int|null $ticketId When provided, removes the ticket_{id} upload directory. | |
| 51 | + */ | |
| 52 | + public static function purgeAttachments($attachments, $ticketId = null) | |
| 53 | + { | |
| 54 | + foreach ($attachments as $attachment) { | |
| 55 | + if ($attachment->driver && $attachment->driver !== 'local') { | |
| 56 | + do_action('fluent_support/delete_remote_attachment_' . $attachment->driver, $attachment); | |
| 57 | + } elseif ($attachment->file_path && file_exists($attachment->file_path)) { | |
| 58 | + wp_delete_file($attachment->file_path); | |
| 59 | + } | |
| 60 | + } | |
| 61 | + | |
| 62 | + if ($ticketId) { | |
| 63 | + $uploadDir = wp_upload_dir(); | |
| 64 | + $ticketDir = $uploadDir['basedir'] . '/' . FLUENT_SUPPORT_UPLOAD_DIR . '/ticket_' . $ticketId; | |
| 65 | + if (is_dir($ticketDir)) { | |
| 66 | + if (!class_exists('\WP_Filesystem_Direct')) { | |
| 67 | + require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; | |
| 68 | + require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; | |
| 69 | + } | |
| 70 | + (new \WP_Filesystem_Direct(false))->rmdir($ticketDir, true); | |
| 71 | + } | |
| 72 | + } | |
| 73 | + } | |
| 74 | + | |
| 75 | + public function setSettingsAttribute($settings) | |
| 76 | + { | |
| 77 | + $this->attributes['settings'] = \maybe_serialize($settings); | |
| 78 | + } | |
| 79 | + | |
| 80 | + public function getSettingsAttribute($settings) | |
| 81 | + { | |
| 82 | + return Helper::safeUnserialize($settings); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 40 | 86 | * Accessor to get dynamic full_name attribute |
| 41 | 87 | * @return string |
| 42 | 88 | */ |
| 43 | 89 | public function getSecureUrlAttribute() |
| @@ -42,9 +88,9 @@ | ||
| 42 | 88 | */ |
| 43 | 89 | public function getSecureUrlAttribute() |
| 44 | 90 | { |
| 45 | 91 | return add_query_arg([ |
| 46 | - 'fst_file' => $this->file_hash, | |
| 47 | - 'secure_sign' => md5($this->id.date('YmdH')) | |
| 92 | + 'fst_file' => $this->file_hash, | |
| 93 | + 'secure_sign' => hash_hmac('sha256', $this->id . '|' . gmdate('YmdH'), wp_salt('secure_auth')) | |
| 48 | 94 | ], site_url('/index.php')); |
| 49 | 95 | } |
| 50 | 96 | } |