Plugin Directory

Changeset 3460518


Ignore:
Timestamp:
02/13/2026 06:23:30 AM (7 weeks ago)
Author:
zywrap
Message:

bug fix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • zywrap/tags/1.0.3/includes/class-zywrap-ajax.php

    r3460495 r3460518  
    9696        check_ajax_referer('zywrap_sync_wrappers_nonce', 'nonce');
    9797
     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
    98103        $api_key = get_option('zywrap_api_key');
    99104        if (empty($api_key)) {
     
    151156
    152157        $response = wp_remote_get($api_url, [
    153             'timeout' => 30,
     158            'timeout' => 60,
    154159            'sslverify' => false,
    155160            'headers' => [ 'Authorization' => 'Bearer ' . $api_key ]
     
    161166
    162167        $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
    163171        $json = json_decode($body, true);
    164         $http_code = wp_remote_retrieve_response_code($response);
     172        unset($body);
    165173
    166174        if ($http_code !== 200 || !$json) {
     
    213221             throw new Exception('Could not write temporary zip file.');
    214222        }
     223       
     224        // Clear memory of the zip string
     225        unset($zip_data);
    215226
    216227        // 2. Unzip
     
    238249            foreach($json['metadata']['categories'] as $r) $rows[] = [$r['code'], $r['name'], $r['ordering']];
    239250            Zywrap_Helpers::upsert_batch('categories', $rows, ['code', 'name', 'ordering']);
     251            unset($json['metadata']['categories']); // Free memory
    240252        }
    241253
     
    245257            foreach($json['metadata']['languages'] as $r) $rows[] = [$r['code'], $r['name'], $r['ordering']];
    246258            Zywrap_Helpers::upsert_batch('languages', $rows, ['code', 'name', 'ordering']);
     259            unset($json['metadata']['languages']); // Free memory
    247260        }
    248261
     
    252265            foreach($json['metadata']['aiModels'] as $r) $rows[] = [$r['code'], $r['name'], $r['provider_id'] ?? null, $r['ordering']];
    253266            Zywrap_Helpers::upsert_batch('ai_models', $rows, ['code', 'name', 'provider_id', 'ordering']);
     267            unset($json['metadata']['aiModels']); // Free memory
    254268        }
    255269
     
    263277            }
    264278            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.
    268285        if (!empty($json['wrappers']['upserts'])) {
    269             $rows = [];
     286            $batch = [];
     287            $batch_limit = 50;
     288           
    270289            foreach($json['wrappers']['upserts'] as $w) {
    271                 $rows[] = [
     290                $batch[] = [
    272291                    $w['code'], $w['name'], $w['description'], $w['categoryCode'],
    273292                    !empty($w['featured']) ? 1 : 0,
     
    275294                    $w['ordering']
    276295                ];
     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                }
    277302            }
    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
    279310        }
    280311
Note: See TracChangeset for help on using the changeset viewer.