PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / full-site-import / Cleanup / PreviewDirsTask.php

PreviewDirsTask.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/full-site-import/Cleanup/PreviewDirsTask.php

74 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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