- Implemented Google Taxonomy loading via AJAX in main_view.php, allowing users to select categories for products. - Enhanced product editing modal to include fields for product title, description, and Google category selection. - Updated AJAX calls to save product data, including custom title, description, and selected Google category. - Added character count validation for product title input. - Integrated Select2 for improved category selection UI. - Created google-taxonomy.php to fetch and cache Google Taxonomy data, ensuring efficient retrieval and fallback mechanisms. - Removed outdated custom feed XML file. - Updated layout-logged.php to include necessary Select2 styles and scripts.
70 lines
1.8 KiB
PHP
70 lines
1.8 KiB
PHP
<?php
|
||
// tools/google-taxonomy.php
|
||
|
||
header('Content-Type: application/json; charset=utf-8');
|
||
|
||
const TAXONOMY_SOURCE_URL = 'https://www.google.com/basepages/producttype/taxonomy-with-ids.pl-PL.txt';
|
||
const TAXONOMY_CACHE_FILE = __DIR__ . '/cache/google-taxonomy-pl.json'; // zmień jak trzeba
|
||
const TAXONOMY_CACHE_TTL = 7 * 24 * 60 * 60; // 7 dni
|
||
|
||
// jeśli jest świeży cache – zwracamy go od razu
|
||
if (file_exists(TAXONOMY_CACHE_FILE) && (time() - filemtime(TAXONOMY_CACHE_FILE) < TAXONOMY_CACHE_TTL)) {
|
||
readfile(TAXONOMY_CACHE_FILE);
|
||
exit;
|
||
}
|
||
|
||
// próba pobrania z Google
|
||
$txt = @file_get_contents(TAXONOMY_SOURCE_URL);
|
||
|
||
if ($txt === false) {
|
||
// jeśli nie ma cache i nie udało się pobrać – błąd
|
||
if (!file_exists(TAXONOMY_CACHE_FILE)) {
|
||
http_response_code(500);
|
||
echo json_encode([
|
||
'status' => 'error',
|
||
'message' => 'Nie udało się pobrać taksonomii Google.'
|
||
], JSON_UNESCAPED_UNICODE);
|
||
exit;
|
||
}
|
||
|
||
// fallback – stary cache (lepsze to niż nic)
|
||
readfile(TAXONOMY_CACHE_FILE);
|
||
exit;
|
||
}
|
||
|
||
// parsowanie TXT -> tablica {id, text}
|
||
$list = [];
|
||
$lines = explode("\n", $txt);
|
||
|
||
foreach ($lines as $line) {
|
||
$line = trim($line);
|
||
if ($line === '') {
|
||
continue;
|
||
}
|
||
|
||
// format linii: 2271 - Health & Beauty > Personal Care > Cosmetics
|
||
$parts = explode(' - ', $line, 2);
|
||
if (count($parts) === 2) {
|
||
$list[] = [
|
||
'id' => trim($parts[0]),
|
||
'text' => trim($parts[1]),
|
||
];
|
||
}
|
||
}
|
||
|
||
$response = [
|
||
'status' => 'ok',
|
||
'categories' => $list,
|
||
];
|
||
|
||
// zapisujemy cache na serwerze
|
||
$dir = dirname(TAXONOMY_CACHE_FILE);
|
||
if (!is_dir($dir)) {
|
||
@mkdir($dir, 0775, true);
|
||
}
|
||
|
||
file_put_contents(TAXONOMY_CACHE_FILE, json_encode($response, JSON_UNESCAPED_UNICODE));
|
||
|
||
// i zwracamy do frontu
|
||
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|