102 lines
2.9 KiB
PHP
102 lines
2.9 KiB
PHP
<?php
|
||
function removeEmoji($text)
|
||
{
|
||
return preg_replace(
|
||
'/[\p{Cs}\p{So}\x{2600}-\x{27BF}\x{1F000}-\x{1FFFF}]/u',
|
||
'',
|
||
$text
|
||
);
|
||
}
|
||
|
||
// Adres źródłowego pliku XML
|
||
$sourceUrl = 'https://xml-feed.app.softitdigital.com/ibra-makeup.myshopify.com/feeds/view/cmi5215vf1f4czycu5150rxkp.xml';
|
||
|
||
// Dokąd zapisujemy plik na serwerze (relatywnie do tego skryptu)
|
||
$outputFile = __DIR__ . '/ibra-makeup_feed.xml';
|
||
|
||
// Domena do podmiany
|
||
$oldDomain = 'https://ibra-makeup.myshopify.com';
|
||
$newDomain = 'https://ibra-makeup.pl';
|
||
|
||
// --- 1. Pobranie XML ---
|
||
$xmlString = @file_get_contents($sourceUrl);
|
||
|
||
if ($xmlString === false)
|
||
{
|
||
die('Nie udało się pobrać pliku XML ze źródła.');
|
||
}
|
||
|
||
// --- 2. Wczytanie XML z przestrzeniami nazw ---
|
||
$xml = @simplexml_load_string($xmlString);
|
||
|
||
if ($xml === false)
|
||
{
|
||
die('Nie udało się zinterpretować pliku XML.');
|
||
}
|
||
|
||
// Pobranie listy namespace’ów (m.in. "g")
|
||
$namespaces = $xml->getNamespaces(true);
|
||
|
||
// Upewniamy się, że mamy namespace "g"
|
||
if (!isset($namespaces['g']))
|
||
{
|
||
die('W pliku XML nie znaleziono przestrzeni nazw "g".');
|
||
}
|
||
|
||
// --- 3. Podmiana domeny + zamiana price <-> sale_price ---
|
||
foreach ($xml->channel->item as $item)
|
||
{
|
||
|
||
// Dzieci w przestrzeni nazw g
|
||
$gChildren = $item->children($namespaces['g']);
|
||
|
||
// --- Podmiana linku ---
|
||
if (isset($gChildren->link))
|
||
{
|
||
$currentLink = (string) $gChildren->link;
|
||
$newLink = str_replace($oldDomain, $newDomain, $currentLink);
|
||
$gChildren->link = $newLink;
|
||
}
|
||
|
||
// --- Zamiana price <-> sale_price jeśli sale_price istnieje ---
|
||
if (isset($gChildren->sale_price) && trim((string)$gChildren->sale_price) !== '')
|
||
{
|
||
|
||
$originalPrice = (string)$gChildren->price;
|
||
$salePrice = (string)$gChildren->sale_price;
|
||
|
||
// Zamiana wartości
|
||
$gChildren->price = $salePrice;
|
||
$gChildren->sale_price = $originalPrice;
|
||
}
|
||
|
||
if (isset($gChildren->identifier_exists))
|
||
{
|
||
$gChildren->identifier_exists = 'true';
|
||
}
|
||
|
||
$gChildren->google_product_category = 'Health & Beauty > Personal Care > Cosmetics';
|
||
|
||
// --- Usuwanie emoji z opisu ---
|
||
if (isset($gChildren->description))
|
||
{
|
||
$cleanDescription = removeEmoji((string)$gChildren->description);
|
||
$gChildren->description = $cleanDescription;
|
||
}
|
||
}
|
||
|
||
// --- 4. Zapis zmodyfikowanego XML na serwerze ---
|
||
if ($xml->asXML($outputFile) === false)
|
||
{
|
||
die('Nie udało się zapisać zmodyfikowanego pliku XML na serwerze.');
|
||
}
|
||
|
||
// --- 5. Zbudowanie URL do zapisanego pliku i wyświetlenie go ---
|
||
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
|
||
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
|
||
$path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
|
||
|
||
$fileUrl = $scheme . '://' . $host . $path . '/ibra-makeup_feed.xml';
|
||
|
||
echo 'Nowy plik XML został zapisany tutaj: <a href="' . htmlspecialchars($fileUrl, ENT_QUOTES, 'UTF-8') . '">' . htmlspecialchars($fileUrl, ENT_QUOTES, 'UTF-8') . '</a>';
|