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 / Concerns / CleansUp.php

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

205 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Modules\FullSiteImport\Concerns;
4
5 use Elementor\Plugin;
6 use Error;
7 use Exception;
8 use Templately\Modules\FullSiteImport\Exception\FatalErrorException;
9 use Templately\Modules\FullSiteImport\Exception\RetryableErrorException;
10 use Templately\Modules\FullSiteImport\Exception\UnknownErrorException;
11 use Templately\Modules\FullSiteImport\Runners\Finalizer;
12 use Templately\Modules\FullSiteImport\Utils\LogHandler;
13 use Templately\Modules\FullSiteImport\Utils\Utils;
14 use Templately\Modules\FullSiteImport\Utils\SessionData;
15 use Templately\Modules\FullSiteImport\Utils\AIUtils;
16 use Templately\Utils\Helper;
17 use Templately\Modules\Utilities\Cleanup\Scheduler;
18 use Templately\Utils\Options;
19
20 /**
21 * CleansUp — extracted verbatim from FullSiteImport (behavior-preserving).
22 * Composed back into FullSiteImport via `use`; $this and method resolution unchanged.
23 */
24 trait CleansUp {
25 /*
26 * schedule_cleanup_cron() and run_scheduled_cleanup() lived here until spec
27 * 052. This module no longer owns a cleanup schedule: `modules/utilities/`
28 * runs the ONE sweep and this module contributes tasks to it (see
29 * Cleanup/CleanupTasks.php). The old `templately_fsi_daily_cleanup` event is
30 * unscheduled by that module's Scheduler — an orphaned recurring event keeps
31 * firing forever on sites that already stored it, even once the code that
32 * registered it is gone.
33 *
34 * The callback was one line, `SessionData::cleanup_expired(7)`, and pruned
35 * OPTIONS ONLY. It never touched the filesystem, which is why abandoned
36 * imports leaked their extracted packs indefinitely.
37 */
38
39 private function clear_data_file($request_params){
40 if(defined('TEMPLATELY_DEV') && TEMPLATELY_DEV){
41 return;
42 }
43
44 // Handle directory cleanup
45 Utils::cleanup_directory($this->dir_path);
46
47 // Always save to preview directory for AI content workflow
48 $session_id = $request_params['session_id'] ?? '';
49 $pack_id = $request_params['id'] ?? '';
50
51 // Set up directory paths for cleanup
52 $root_dir = $request_params['root_dir'] ?? untrailingslashit(Helper::upload_dir('tmp'));
53 $prv_dir = $request_params['prv_dir'] ?? untrailingslashit(Helper::upload_dir('preview'));
54
55 $processed_data = AIUtils::get_ai_process_data_by_session_id($session_id);
56
57 // Pack-scoped session cleanup is handled solely by the 7-day retention cron
58 // (034 FR-005) — the previously-disabled clean_by_pack_id() has been removed.
59
60 // Keep only the CURRENT pack's superseded AI process rows — cheap, and
61 // scoped to the import that just finished.
62 $current_process_id = !empty($processed_data['process_id']) ? $processed_data['process_id'] : null;
63 AIUtils::clean_ai_process_data_by_pack_id($pack_id, $current_process_id);
64
65 // The orphan sweep itself is no longer a lottery. It used to sit behind
66 // `mt_rand(1, 100) <= 5` and require a live pack id and session id, so
67 // four out of five finished imports swept nothing and an import that
68 // never finished swept nothing ever. Nudging the shared cleanup service
69 // runs it every time, asynchronously, off the critical path — and it
70 // works from the retention window alone, with no in-flight identifiers.
71 Scheduler::kick();
72 }
73
74 /**
75 * Directory-based cleanup for session data directories
76 * Scans the actual filesystem directories and removes directories that match cleanup criteria
77 *
78 * @param string $root_dir The root directory containing session directories
79 * @param string $pack_id The pack ID to match for cleanup
80 * @param string $current_session_id The current session ID to preserve
81 */
82 private function cleanup_session_directories($root_dir, $pack_id, $current_session_id) {
83 if (empty($root_dir) || !is_dir($root_dir) || empty($pack_id) || empty($current_session_id)) {
84 return;
85 }
86
87 try {
88 // Get all session data to check pack_id associations
89 $all_session_data = SessionData::get_all_data();
90
91 // Scan the actual directories in the filesystem
92 $directories = scandir($root_dir);
93 if ($directories === false) {
94 return;
95 }
96
97 foreach ($directories as $dir_name) {
98 // Skip current directory, parent directory, and current session
99 if ($dir_name === '.' || $dir_name === '..' || $dir_name === $current_session_id) {
100 continue;
101 }
102
103 $dir_path = trailingslashit($root_dir) . $dir_name;
104
105 // Only process actual directories
106 if (!is_dir($dir_path)) {
107 continue;
108 }
109
110 // Check if this directory should be cleaned up
111 $should_cleanup = false;
112
113 // If we have session data for this directory, check if it matches the pack_id
114 if (isset($all_session_data[$dir_name]) &&
115 isset($all_session_data[$dir_name]['id']) &&
116 $all_session_data[$dir_name]['id'] === $pack_id) {
117 $should_cleanup = true;
118 } else if (!isset($all_session_data[$dir_name])) {
119 // This is an orphaned directory with no corresponding session data
120 $should_cleanup = true;
121 }
122
123 if ($should_cleanup) {
124 Utils::cleanup_directory($dir_path);
125
126 if (defined('TEMPLATELY_DEV') && TEMPLATELY_DEV) {
127 Helper::log('Cleaned up session directory: ' . $dir_name, 'fsi_cleanup');
128 }
129 }
130 }
131 } catch (Exception $e) {
132 if (defined('TEMPLATELY_DEV') && TEMPLATELY_DEV) {
133 Helper::log('Error during session directory cleanup: ' . $e->getMessage(), 'fsi_cleanup', 'error');
134 }
135 }
136 }
137
138 /**
139 * Directory-based cleanup for AI process data directories
140 * Scans the actual filesystem directories and removes directories that match cleanup criteria
141 *
142 * @param string $prv_dir The preview directory containing process directories
143 * @param string $pack_id The pack ID to match for cleanup
144 * @param string $current_process_id The current process ID to preserve (optional)
145 */
146 private function cleanup_ai_process_directories($prv_dir, $pack_id, $current_process_id = null) {
147 if (empty($prv_dir) || !is_dir($prv_dir) || empty($pack_id)) {
148 return;
149 }
150
151 try {
152 // Get all AI process data to check pack_id associations
153 $ai_process_data = AIUtils::get_ai_process_data();
154
155 // Scan the actual directories in the filesystem
156 $directories = scandir($prv_dir);
157 if ($directories === false) {
158 return;
159 }
160
161 foreach ($directories as $dir_name) {
162 // Skip current directory, parent directory, and current process
163 if ($dir_name === '.' || $dir_name === '..' ||
164 (!empty($current_process_id) && $dir_name === $current_process_id)) {
165 continue;
166 }
167
168 $dir_path = trailingslashit($prv_dir) . $dir_name;
169
170 // Only process actual directories
171 if (!is_dir($dir_path)) {
172 continue;
173 }
174
175 // Check if this directory should be cleaned up
176 $should_cleanup = false;
177
178 // If we have process data for this directory, check if it matches the pack_id
179 if (isset($ai_process_data[$dir_name]) &&
180 is_array($ai_process_data[$dir_name]) &&
181 isset($ai_process_data[$dir_name]['pack_id']) &&
182 $ai_process_data[$dir_name]['pack_id'] === $pack_id) {
183 $should_cleanup = true;
184 } else if (!isset($ai_process_data[$dir_name])) {
185 // This is an orphaned directory with no corresponding process data
186 $should_cleanup = true;
187 }
188
189 if ($should_cleanup) {
190 Utils::cleanup_directory($dir_path);
191
192 if (defined('TEMPLATELY_DEV') && TEMPLATELY_DEV) {
193 Helper::log('Cleaned up AI process directory: ' . $dir_name, 'fsi_cleanup');
194 }
195 }
196 }
197 } catch (Exception $e) {
198 if (defined('TEMPLATELY_DEV') && TEMPLATELY_DEV) {
199 Helper::log('Error during AI process directory cleanup: ' . $e->getMessage(), 'fsi_cleanup', 'error');
200 }
201 }
202 }
203
204 }
205