| 1 |
<?php |
| 2 |
/** |
| 3 |
* Reclaim orphaned AI preview directories. |
| 4 |
* |
| 5 |
* @package Templately |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Templately\Modules\FullSiteImport\Cleanup; |
| 9 |
|
| 10 |
use Templately\Modules\FullSiteImport\Utils\AIUtils; |
| 11 |
use Templately\Modules\FullSiteImport\Utils\SessionData; |
| 12 |
|
| 13 |
/** |
| 14 |
* `uploads/templately/preview/{process_id}/` — AI-generated page output kept for |
| 15 |
* the customizer preview. Same leak as the working directories, same fix: a |
| 16 |
* directory is reclaimable when no process record owns it, or when the owning |
| 17 |
* process has passed the retention window and its linked session is not active. |
| 18 |
* |
| 19 |
* Only the ownership question differs from {@see TmpDirsTask}, so that is the |
| 20 |
* only thing overridden. |
| 21 |
*/ |
| 22 |
class PreviewDirsTask extends TmpDirsTask { |
| 23 |
|
| 24 |
const SUBDIR = 'preview'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Ownership here is a process record, not a session one, so the parent's |
| 28 |
* session snapshot would be a full-table read this task never consults. |
| 29 |
*/ |
| 30 |
protected function known_session_ids(): array { |
| 31 |
return []; |
| 32 |
} |
| 33 |
|
| 34 |
public function descriptor(): array { |
| 35 |
return [ |
| 36 |
'id' => 'fsi-preview-dirs', |
| 37 |
'label' => __( 'Abandoned AI preview files', 'templately' ), |
| 38 |
'group' => 'imports', |
| 39 |
'scope' => 'files', |
| 40 |
'retention_kind' => 'age', |
| 41 |
'destructive' => true, |
| 42 |
'schedulable' => true, |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @param string $entry A process id. |
| 48 |
* @param int $threshold Unix time before which a record is expired. |
| 49 |
* @param array $known Unused here — ownership is a PROCESS record, not a |
| 50 |
* session one, and those are read individually. |
| 51 |
*/ |
| 52 |
protected function is_reclaimable( string $entry, int $threshold, array $known = [] ): bool { |
| 53 |
$process = AIUtils::read_process( $entry ); |
| 54 |
|
| 55 |
if ( ! is_array( $process ) || empty( $process ) ) { |
| 56 |
// No record owns this directory. |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
$updated_at = isset( $process['_updated_at'] ) ? (int) $process['_updated_at'] : 0; |
| 61 |
if ( $updated_at >= $threshold && $updated_at > 0 ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
// A process whose session is still live must survive even if its own |
| 66 |
// row looks stale — the session is the thing actually running. |
| 67 |
if ( ! empty( $process['session_id'] ) && SessionData::is_active( (string) $process['session_id'], $threshold ) ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
return true; |
| 72 |
} |
| 73 |
} |
| 74 |
|