update($wpdb->options, ['option_value'=>(string)time()], ['option_name'=>$attempt_key,'option_value'=>(string)$attempt]) === 1; wp_cache_delete($attempt_key,'options'); } if ($claimed) { try { $r = picsart_runtime_http('https://api.picsart.com/pricing-management/model-pricing'); picsart_runtime_require($r['status'] === 200 && ($r['body']['status'] ?? '') === 'success' && is_array($r['body']['response'] ?? null), 'CATALOG', __('Picsart model refresh is unavailable.', 'picsart-commerce'), 502); $by_model = []; foreach ($r['body']['response'] as $row) if (is_numeric($row['credits'] ?? null) && isset($row['metadata']['modelId'])) $by_model[$row['metadata']['modelId']][] = ['credits'=>0 + $row['credits'],'unit'=>$row['unit'],'quality'=>$row['metadata']['quality'] ?? null,'audio'=>$row['metadata']['audio'] ?? null,'useCase'=>$row['metadata']['useCase'] ?? null]; $manifest = picsart_runtime_manifest(); $catalog = $manifest['catalog']; $prices = []; foreach (['image','video'] as $kind) { $catalog[$kind] = array_values(array_filter($catalog[$kind], static fn($m)=>!empty($manifest['requests'][$m['id']]) && isset($by_model[$manifest['workflows'][$m['id']]['pricingId']]))); foreach ($catalog[$kind] as $model) { $tiers = $by_model[$manifest['workflows'][$model['id']]['pricingId']]; $amounts = array_column($tiers,'credits'); $prices[$model['id']] = ['min'=>min($amounts),'max'=>max($amounts),'tiers'=>$tiers]; } } picsart_runtime_require($catalog['image'] && $catalog['video'], 'CATALOG', __('Picsart returned an incomplete model catalog. Saved options remain available.', 'picsart-commerce'), 502); $catalog['syncedAt'] = time() * 1000; $saved = ['catalog'=>$catalog,'prices'=>$prices]; update_option($key,$saved,false); } catch (Picsart_Runtime_Error $error) { /* Last successful catalog or bundled SDK options remain usable. */ } } $manifest = picsart_runtime_manifest(); if (!$saved) return ['catalog'=>$manifest['catalog'],'prices'=>[]]; // An upgrade may remove a request contract while the daily cache is still fresh. // Prune stale choices locally; an upgrade does not trigger an extra API refresh. foreach (['image','video'] as $kind) { $allowed = array_column($manifest['catalog'][$kind],'id'); $saved['catalog'][$kind] = array_values(array_filter($saved['catalog'][$kind], static fn($m)=>in_array($m['id'],$allowed,true) && isset($manifest['requests'][$m['id']]))); } $saved['catalog']['schemas'] = $manifest['catalog']['schemas']; $saved['catalog']['videoOptions'] = $manifest['catalog']['videoOptions']; return $saved; }