| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Utils; |
| 4 |
|
| 5 |
use Templately\Utils\Options; |
| 6 |
|
| 7 |
/** |
| 8 |
* AI-related utility functions for Templately |
| 9 |
* Handles AI process data management using API key-based storage with count-based cleanup |
| 10 |
*/ |
| 11 |
class AIUtils { |
| 12 |
|
| 13 |
/** |
| 14 |
* Handle SSE message wait with timeout exit condition |
| 15 |
* Static utility function for reusable timeout handling across different import contexts |
| 16 |
* |
| 17 |
* @param string $session_id Session ID for progress tracking |
| 18 |
* @param string $progress_id Progress tracking identifier (e.g., 'ai_content_time', 'finalize_time') |
| 19 |
* @param array $updated_ids Currently updated/processed pages |
| 20 |
* @param array $ai_page_ids All AI pages that need processing |
| 21 |
* @param callable $sse_message_callback Callback function for sending SSE messages |
| 22 |
* @param array $additional_sse_data Additional data to include in SSE message |
| 23 |
* @return bool True if should continue processing, false if should exit |
| 24 |
*/ |
| 25 |
public static function handle_sse_wait_with_timeout($session_id, $progress_id, $updated_ids, $ai_page_ids, $sse_message_callback, $additional_sse_data = []) { |
| 26 |
$total_pages = count($ai_page_ids); |
| 27 |
$updated_pages = count($updated_ids['pages'] ?? []); |
| 28 |
|
| 29 |
// If all pages are processed or credit cost is available, continue processing |
| 30 |
if ($total_pages <= $updated_pages || isset($updated_ids['credit_cost'])) { |
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
// Get timeout tracking data from session |
| 35 |
$session_data = Utils::get_session_data($session_id); |
| 36 |
$progress_data = $session_data['progress'][$progress_id] ?? []; |
| 37 |
$last_progress = $progress_data['last_progress'] ?? 0; |
| 38 |
$last_time = $progress_data['last_time'] ?? 0; |
| 39 |
$current_time = time(); |
| 40 |
$progress_percentage = $total_pages > 0 ? round(($updated_pages / $total_pages) * 100) : 0; |
| 41 |
|
| 42 |
// Skip timeout check if credit cost is available and time difference is > 10 seconds |
| 43 |
if (isset($updated_ids['credit_cost']) && !empty($last_time) && ($current_time - $last_time) > 10) { |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
// Check if time difference is less than 5 minutes (timeout condition) |
| 48 |
if (empty($last_time) || ($current_time - $last_time) < 5 * MINUTE_IN_SECONDS) { |
| 49 |
// Only update time if progress has changed |
| 50 |
if ($progress_percentage !== $last_progress) { |
| 51 |
$updated_progress = $session_data['progress'] ?? []; |
| 52 |
$updated_progress[$progress_id] = [ |
| 53 |
'last_progress' => $progress_percentage, |
| 54 |
'last_time' => $current_time, |
| 55 |
]; |
| 56 |
Utils::update_session_data($session_id, ['progress' => $updated_progress]); |
| 57 |
} |
| 58 |
|
| 59 |
// Prepare SSE message data |
| 60 |
$sse_data = array_merge([ |
| 61 |
'type' => 'wait', |
| 62 |
'action' => 'wait', |
| 63 |
'generated_pages' => $updated_ids, |
| 64 |
'all_pages' => $ai_page_ids, |
| 65 |
], $additional_sse_data); |
| 66 |
|
| 67 |
// Send wait message and exit |
| 68 |
call_user_func($sse_message_callback, $sse_data); |
| 69 |
exit; |
| 70 |
} |
| 71 |
|
| 72 |
// Timeout exceeded - could send error message here if needed |
| 73 |
// Currently commented out in original AIContent.php |
| 74 |
/* |
| 75 |
call_user_func($sse_message_callback, [ |
| 76 |
'action' => 'error', |
| 77 |
'status' => 'error', |
| 78 |
'type' => "error", |
| 79 |
'title' => __("Oops!", "templately"), |
| 80 |
'message' => __("Taking too long....", "templately"), |
| 81 |
]); |
| 82 |
exit; |
| 83 |
*/ |
| 84 |
|
| 85 |
// Continue processing if timeout exceeded (current behavior) |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get AI process data from WordPress options |
| 91 |
* |
| 92 |
* @return array The AI process data array |
| 93 |
*/ |
| 94 |
public static function get_ai_process_data() { |
| 95 |
$all_ai_process_data = get_option('templately_ai_process_data', []); |
| 96 |
return is_array($all_ai_process_data) ? $all_ai_process_data : []; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Update AI process data in WordPress options with process_id as key |
| 101 |
* Appends to existing data |
| 102 |
* |
| 103 |
* @param array $data The AI process data to save (process_id => process_data) |
| 104 |
* @return bool True on success, false on failure |
| 105 |
*/ |
| 106 |
public static function update_ai_process_data($data) { |
| 107 |
if (!is_array($data)) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
// Get existing data |
| 112 |
$all_ai_process_data = get_option('templately_ai_process_data', []); |
| 113 |
if (!is_array($all_ai_process_data)) { |
| 114 |
$all_ai_process_data = []; |
| 115 |
} |
| 116 |
|
| 117 |
// Append new data to existing data |
| 118 |
foreach ($data as $process_id => $process_data) { |
| 119 |
if (is_array($process_data)) { |
| 120 |
$all_ai_process_data[$process_id] = $process_data; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return update_option('templately_ai_process_data', $all_ai_process_data); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get the latest AI process data for the current API key |
| 129 |
* Used in import_info() to return the most recent AI process |
| 130 |
* |
| 131 |
* @return array|null The latest AI process data or null if not found |
| 132 |
*/ |
| 133 |
public static function get_latest_ai_process_by_api_key() { |
| 134 |
$api_key = Options::get_instance()->get('api_key'); |
| 135 |
if (empty($api_key)) { |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
$all_ai_process_data = get_option('templately_ai_process_data', []); |
| 140 |
if (!is_array($all_ai_process_data)) { |
| 141 |
return null; |
| 142 |
} |
| 143 |
|
| 144 |
$api_key_processes = []; |
| 145 |
|
| 146 |
// Collect processes for current API key |
| 147 |
foreach ($all_ai_process_data as $process_id => $process_data) { |
| 148 |
if (is_array($process_data) && isset($process_data['api_key']) && $process_data['api_key'] === $api_key) { |
| 149 |
$api_key_processes[$process_id] = $process_data; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if (empty($api_key_processes)) { |
| 154 |
return null; |
| 155 |
} |
| 156 |
|
| 157 |
// Return the last (most recent) process |
| 158 |
return end($api_key_processes); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
/** |
| 164 |
* Get AI process data by session ID |
| 165 |
* |
| 166 |
* @param string $session_id The session ID to search for |
| 167 |
* @return array|null The AI process data array or null if not found |
| 168 |
*/ |
| 169 |
public static function get_ai_process_data_by_session_id($session_id) { |
| 170 |
if (empty($session_id)) { |
| 171 |
return null; |
| 172 |
} |
| 173 |
|
| 174 |
$ai_process_data = self::get_ai_process_data(); |
| 175 |
|
| 176 |
foreach ($ai_process_data as $process) { |
| 177 |
if (is_array($process) && isset($process['session_id']) && $process['session_id'] === $session_id) { |
| 178 |
return $process; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get AI process ID by session ID |
| 187 |
* |
| 188 |
* @param string $session_id The session ID to search for |
| 189 |
* @return string|null The process ID (array key) or null if not found |
| 190 |
*/ |
| 191 |
public static function get_ai_process_id_by_session_id($session_id) { |
| 192 |
if (empty($session_id)) { |
| 193 |
return null; |
| 194 |
} |
| 195 |
|
| 196 |
$ai_process_data = self::get_ai_process_data(); |
| 197 |
|
| 198 |
foreach ($ai_process_data as $process_id => $process) { |
| 199 |
if (is_array($process) && isset($process['session_id']) && $process['session_id'] === $session_id) { |
| 200 |
return $process_id; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return null; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Get AI process data by process ID |
| 209 |
* |
| 210 |
* @param string $process_id The process ID to search for |
| 211 |
* @return array|null The AI process data array or null if not found |
| 212 |
*/ |
| 213 |
public static function get_ai_process_data_by_process_id($process_id) { |
| 214 |
if (empty($process_id)) { |
| 215 |
return null; |
| 216 |
} |
| 217 |
|
| 218 |
$ai_process_data = self::get_ai_process_data(); |
| 219 |
|
| 220 |
if (isset($ai_process_data[$process_id]) && is_array($ai_process_data[$process_id])) { |
| 221 |
return $ai_process_data[$process_id]; |
| 222 |
} |
| 223 |
|
| 224 |
return null; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Clean AI process data by pack ID, keeping only the current process |
| 229 |
* Removes all AI process entries with the same pack_id except the current process |
| 230 |
* |
| 231 |
* @param string $pack_id The pack ID to match for cleanup |
| 232 |
* @param string $current_process_id The current process ID to preserve (optional) |
| 233 |
* @return array Array of removed process IDs |
| 234 |
*/ |
| 235 |
public static function clean_ai_process_data_by_pack_id($pack_id, $current_process_id = null) { |
| 236 |
if (empty($pack_id)) { |
| 237 |
return []; |
| 238 |
} |
| 239 |
|
| 240 |
$all_ai_process_data = get_option('templately_ai_process_data', []); |
| 241 |
if (!is_array($all_ai_process_data)) { |
| 242 |
return []; |
| 243 |
} |
| 244 |
|
| 245 |
$removed_process_ids = []; |
| 246 |
|
| 247 |
foreach ($all_ai_process_data as $process_id => $process_data) { |
| 248 |
if (!is_array($process_data)) { |
| 249 |
continue; |
| 250 |
} |
| 251 |
|
| 252 |
// Skip the current process by process_id if provided |
| 253 |
if (!empty($current_process_id) && $process_id === $current_process_id) { |
| 254 |
continue; |
| 255 |
} |
| 256 |
|
| 257 |
// Remove processes that have the same pack_id |
| 258 |
if (isset($process_data['pack_id']) && $process_data['pack_id'] === $pack_id) { |
| 259 |
unset($all_ai_process_data[$process_id]); |
| 260 |
$removed_process_ids[] = $process_id; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
// Update the options with cleaned data if any processes were removed |
| 265 |
if (!empty($removed_process_ids)) { |
| 266 |
update_option('templately_ai_process_data', $all_ai_process_data); |
| 267 |
} |
| 268 |
|
| 269 |
return $removed_process_ids; |
| 270 |
} |
| 271 |
} |
| 272 |
|