code Copy code
// Pobierz pola ACF
$header = get_field('modal_header', 14187) ?: $atts['header'];
$text = wp_kses_post(get_field('modal_text', 14187) ?: $atts['text']);
// Pobierz obraz tła (dostosuj nazwę pola i format zwracany przez ACF)
$background_image = get_field('modal_background_image', 14187) ?: $atts['background_image'];
// Jeśli pole ACF zwraca tablicę (Image Array), pobierz URL
if (is_array($background_image)) {
$background_image = $background_image['url'];
}
// Jeśli pole ACF zwraca ID (Image ID), pobierz URL
elseif (is_numeric($background_image)) {
$background_image = wp_get_attachment_image_url($background_image, 'full');
}
$cta_text = get_field('cta_text', 14187) ?: $atts['cta_text'];
$cta_link = get_field('cta_link', 14187) ?: $atts['cta_link'];
// Zbuduj HTML modala
$modal_html = '
function modal_shortcode($atts) {
// Pobierz atrybuty shortcode
$atts = shortcode_atts(array(
'header' => 'Domyślny Nagłówek',
'text' => 'Domyślny tekst modala',
'background_image' => '',
'cta_text' => 'Dowiedz się więcej',
'cta_link' => '#'
), $atts, 'modal');
// Pobierz pola ACF
$header = get_field('modal_header', 14187) ?: $atts['header'];
$text = wp_kses_post(get_field('modal_text', 14187) ?: $atts['text']);
// Obraz tła
$background_image = get_field('modal_background_image', 14187) ?: $atts['background_image'];
if (is_array($background_image)) {
$background_image = $background_image['url'];
} elseif (is_numeric($background_image)) {
$background_image = wp_get_attachment_image_url($background_image, 'full');
}
$cta_text = get_field('cta_text', 14187) ?: $atts['cta_text'];
$cta_link = get_field('cta_link', 14187) ?: $atts['cta_link'];
// Pobierz datę końca odliczania
$countdown_end = get_field('modal_countdown_end', 14187);
$countdown_timestamp = '';
if ($countdown_end) {
$countdown_timestamp = strtotime($countdown_end) * 1000; // JS uses milliseconds
}
// Zbuduj HTML modala
$modal_html = '
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content position-relative">
<!-- Overlay z tłem -->
<div class="modal-background" style="background-image: url(' . esc_url($background_image) . ');"></div>
<div class="modal-header">
<h2 class="card-title fw-bold" id="exampleModalLabel">' . esc_html($header) . '</h2>
<button type="button" class="btn btn-close material-symbols-outlined"
data-bs-dismiss="modal" aria-label="Close"
style="color: rgba(200,115,40,1); font-size: 1.6rem; background: none; border: none;">
close
</button>
</div>
<div class="modal-body" style="min-height: 300px;">
' . $text;
if ($countdown_timestamp) {
$modal_html .= '
<div id="modal-countdown" class="mt-4 text-center"
data-end-time="' . esc_attr($countdown_timestamp) . '"
style="font-size: 1.2rem; font-weight: bold; color: #e35f21;">
Odliczanie...
</div>';
}
$modal_html .= '
</div>
<div class="modal-footer">
<a href="' . esc_url($cta_link) . '" class="btn btn-secondary text-decoration-none px-3">' . esc_html($cta_text) . '</a>
</div>
</div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
var countdownElem = document.getElementById("modal-countdown");
if (!countdownElem) return;
var endTime = parseInt(countdownElem.dataset.endTime);
function updateCountdown() {
var now = new Date().getTime();
var distance = endTime - now;
if (distance < 0) {
countdownElem.innerHTML = "Czas minął!";
return;
}
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
countdownElem.innerHTML =
days + " dni " + hours + "h " + minutes + "m " + seconds + "s";
}
updateCountdown();
setInterval(updateCountdown, 1000);
});
</script>';
return $modal_html;
}
add_shortcode('modal', 'modal_shortcode');
Jeżeli chcesz, żeby licznik był stylizowany inaczej lub działał też np. w kilku językach (PL/EN), mogę go rozbudować.