Changeset 3460518
- Timestamp:
- 02/13/2026 06:23:30 AM (7 weeks ago)
- File:
-
- 1 edited
-
zywrap/tags/1.0.3/includes/class-zywrap-ajax.php (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
zywrap/tags/1.0.3/includes/class-zywrap-ajax.php
r3460495 r3460518 96 96 check_ajax_referer('zywrap_sync_wrappers_nonce', 'nonce'); 97 97 98 // 1. SYSTEM CONFIGURATION: Increase limits for this request only 99 // This prevents the "Allowed memory size exhausted" fatal error 100 @ini_set('memory_limit', '1024M'); // Bump to 1GB for safety 101 @set_time_limit(600); // 10 minutes timeout 102 98 103 $api_key = get_option('zywrap_api_key'); 99 104 if (empty($api_key)) { … … 151 156 152 157 $response = wp_remote_get($api_url, [ 153 'timeout' => 30,158 'timeout' => 60, 154 159 'sslverify' => false, 155 160 'headers' => [ 'Authorization' => 'Bearer ' . $api_key ] … … 161 166 162 167 $body = wp_remote_retrieve_body($response); 168 $http_code = wp_remote_retrieve_response_code($response); 169 170 // MEMORY OPTIMIZATION: Decode and unset raw string immediately 163 171 $json = json_decode($body, true); 164 $http_code = wp_remote_retrieve_response_code($response);172 unset($body); 165 173 166 174 if ($http_code !== 200 || !$json) { … … 213 221 throw new Exception('Could not write temporary zip file.'); 214 222 } 223 224 // Clear memory of the zip string 225 unset($zip_data); 215 226 216 227 // 2. Unzip … … 238 249 foreach($json['metadata']['categories'] as $r) $rows[] = [$r['code'], $r['name'], $r['ordering']]; 239 250 Zywrap_Helpers::upsert_batch('categories', $rows, ['code', 'name', 'ordering']); 251 unset($json['metadata']['categories']); // Free memory 240 252 } 241 253 … … 245 257 foreach($json['metadata']['languages'] as $r) $rows[] = [$r['code'], $r['name'], $r['ordering']]; 246 258 Zywrap_Helpers::upsert_batch('languages', $rows, ['code', 'name', 'ordering']); 259 unset($json['metadata']['languages']); // Free memory 247 260 } 248 261 … … 252 265 foreach($json['metadata']['aiModels'] as $r) $rows[] = [$r['code'], $r['name'], $r['provider_id'] ?? null, $r['ordering']]; 253 266 Zywrap_Helpers::upsert_batch('ai_models', $rows, ['code', 'name', 'provider_id', 'ordering']); 267 unset($json['metadata']['aiModels']); // Free memory 254 268 } 255 269 … … 263 277 } 264 278 Zywrap_Helpers::upsert_batch('block_templates', $rows, ['type', 'code', 'name']); 265 } 266 267 // 5. Wrappers Upsert 279 unset($json['metadata']['templates']); // Free memory 280 } 281 282 // 5. Wrappers Upsert (BATCHED LOOP) 283 // MEMORY FIX: We process this loop in small chunks of 50 to avoid 284 // building one massive array that crashes PHP memory. 268 285 if (!empty($json['wrappers']['upserts'])) { 269 $rows = []; 286 $batch = []; 287 $batch_limit = 50; 288 270 289 foreach($json['wrappers']['upserts'] as $w) { 271 $ rows[] = [290 $batch[] = [ 272 291 $w['code'], $w['name'], $w['description'], $w['categoryCode'], 273 292 !empty($w['featured']) ? 1 : 0, … … 275 294 $w['ordering'] 276 295 ]; 296 297 // If batch is full, push to DB and clear memory 298 if (count($batch) >= $batch_limit) { 299 Zywrap_Helpers::upsert_batch('wrappers', $batch, ['code', 'name', 'description', 'category_code', 'featured', 'base', 'ordering']); 300 $batch = []; 301 } 277 302 } 278 Zywrap_Helpers::upsert_batch('wrappers', $rows, ['code', 'name', 'description', 'category_code', 'featured', 'base', 'ordering']); 303 304 // Process any remaining items 305 if (!empty($batch)) { 306 Zywrap_Helpers::upsert_batch('wrappers', $batch, ['code', 'name', 'description', 'category_code', 'featured', 'base', 'ordering']); 307 } 308 309 unset($json['wrappers']['upserts']); // Free memory immediately 279 310 } 280 311
Note: See TracChangeset
for help on using the changeset viewer.