render.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. if ( ! empty( $_GET['preview'] ) ) {
  3. $post_id = filter_input( INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT );
  4. $slug = get_post_meta($post_id, '_slug_zrzutka', true);
  5. } else {
  6. $slug = get_post_meta(get_the_ID(), '_slug_zrzutka', true);
  7. }
  8. if (false === ($html = get_transient($slug))) {
  9. $url = "https://zrzutka.pl/" . $slug;
  10. // Używamy cURL do pobrania zawartości strony
  11. $ch = curl_init();
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  15. curl_setopt($ch, CURLOPT_VERBOSE, true);
  16. // curl_setopt($ch, CURLOPT_STDERR, $wrapper);
  17. $html = curl_exec($ch);
  18. curl_close($ch);
  19. set_transient($slug, $html, 60 * 15);
  20. }
  21. // Sprawdzamy, czy udało się pobrać zawartość
  22. if (!$html) {
  23. echo "Nie udało się pobrać zawartości ze strony.";
  24. }
  25. // Tworzymy nowy obiekt DOMDocument i wczytujemy pobraną zawartość
  26. $dom = new DOMDocument;
  27. @$dom->loadHTML($html); // @ jest używane, aby stłumić ostrzeżenia generowane przez niepoprawny HTML
  28. // Tworzymy obiekt DOMXPath do wyszukiwania elementów
  29. $xpath = new DOMXPath($dom);
  30. // Szukamy elementu z klasą 'h3' w obrębie diva o atrybucie 'data-action' równym 'chip-show-amounts'
  31. $amountNode = $xpath->query('//div[@data-action="chip-show-amounts"]//div[@class="h3 m-0"]')->item(0);
  32. // Szukamy elementu z sumą ceny
  33. $sumNode = $xpath->query('//div[@data-action="chip-show-amounts"]//span[@data-action="chip-target-sum"]')->item(0);
  34. // Wyświetlamy znalezione wartości
  35. if ($amountNode && $sumNode) {
  36. $amount = strip_tags(trim($amountNode->textContent));
  37. $sum = strip_tags(trim($sumNode->textContent));
  38. // Parsowanie kwot jako liczby całkowite
  39. $amountNumber = intval(str_replace(' ', '', $amount));
  40. $sumNumber = intval(str_replace(' ', '', $sum));
  41. // Obliczamy procentowy udział
  42. $percent = ($amountNumber / $sumNumber) * 100;
  43. // Tworzymy pasek postępu
  44. $progressBar = "<div style='width: 100%; margin-top: 0.25em; background-color: #e0e0e0; border-radius: 20px; height: 20px; overflow: hidden;'>
  45. <div style='width: $percent%; background-color: #e64164; text-align: center; color: white; border-radius: 10px; padding: 5px; height: 50%;'></div>
  46. </div>
  47. <p class='has-small-font-size'>Mamy już <strong>$amountNumber zł</strong> z <strong>$sumNumber zł</strong>!</p>
  48. ";
  49. echo $progressBar;
  50. } else {
  51. echo "";
  52. }
  53. ?>