Files
cdnPRO/wyprzedaze.pl/index.php

97 lines
2.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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/wyprzedaze.myshopify.com/feeds/view/cmi52jjvt1ibbzycu3oy19bxe.xml';
// Dokąd zapisujemy plik na serwerze (relatywnie do tego skryptu)
$outputFile = __DIR__ . '/wyprzedaze_feed.xml';
// Domena do podmiany
$oldDomain = 'https://wyprzedaze.myshopify.com';
$newDomain = 'https://wyprzedaze.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 w każdym <g:link> ---
foreach ($xml->channel->item as $item)
{
// Dzieci w przestrzeni nazw g
$gChildren = $item->children($namespaces['g']);
if (isset($gChildren->link))
{
$currentLink = (string) $gChildren->link;
// Podmiana samej domeny na nową
$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';
}
// --- 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 . '/wyprzedaze_feed.xml';
echo 'Nowy plik XML został zapisany tutaj: <a href="' . htmlspecialchars($fileUrl, ENT_QUOTES, 'UTF-8') . '">' . htmlspecialchars($fileUrl, ENT_QUOTES, 'UTF-8') . '</a>';