Files
2026-05-07 14:57:59 +02:00

447 lines
15 KiB
PHP

<?php
/**
* Shortcode Handler
*
* @package YachtBooking
*/
namespace YachtBooking;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Shortcode class - handles [yacht_calendar] shortcode
*/
class Shortcode {
/**
* Single instance
*
* @var Shortcode
*/
private static $instance = null;
/**
* Get instance
*
* @return Shortcode
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
add_shortcode( 'yacht_calendar', array( $this, 'render_calendar' ) );
add_shortcode( 'yacht_calendar_all', array( $this, 'render_calendar_all' ) );
}
/**
* Render shortcode `[yacht_calendar_all]` — wspólny kalendarz wszystkich jachtów.
*
* Atrybuty:
* - height: wysokość w px (default 650)
* - show_legend: yes|no (default yes)
*
* @param array $atts Shortcode attributes.
* @return string HTML.
*/
public function render_calendar_all( $atts ) {
$atts = shortcode_atts(
array(
'height' => 650,
'show_legend' => 'yes',
),
$atts,
'yacht_calendar_all'
);
// Lazy-load widget class for the View helper.
if ( ! class_exists( '\YachtBooking\Calendar_All_View' ) ) {
require_once YACHT_BOOKING_PLUGIN_DIR . 'frontend/class-calendar-widget-all.php';
}
$dom_id = 'yacht-calendar-all-' . wp_rand( 1000, 9999 );
$height = (int) $atts['height'];
$show_legend = 'yes' === strtolower( (string) $atts['show_legend'] );
return Calendar_All_View::render( $dom_id, $height, $show_legend );
}
/**
* Get yachts for quick switcher.
*
* @return array<int, array<string, mixed>>
*/
private function get_yacht_switcher_items() {
$yachts = get_posts(
array(
'post_type' => 'yacht',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
)
);
$items = array();
foreach ( $yachts as $yacht ) {
$items[] = array(
'id' => (int) $yacht->ID,
'title' => get_the_title( $yacht ),
'description' => wp_kses_post( wpautop( $yacht->post_content ) ),
);
}
return $items;
}
/**
* Render quick yacht switcher.
*
* @param array<int, array<string, mixed>> $yachts Yacht items.
* @param int $active_id Active yacht ID.
* @return void
*/
private function render_yacht_switcher( $yachts, $active_id ) {
$other_yachts = array_filter(
$yachts,
static function( $yacht_item ) use ( $active_id ) {
return (int) $yacht_item['id'] !== (int) $active_id;
}
);
if ( empty( $other_yachts ) ) {
return;
}
?>
<div class="yacht-calendar-switcher" aria-label="<?php esc_attr_e( 'Szybki wybór jachtu', 'yacht-booking' ); ?>">
<span class="yacht-calendar-switcher-label"><?php esc_html_e( 'Zobacz też:', 'yacht-booking' ); ?></span>
<div class="yacht-calendar-switcher-buttons">
<?php foreach ( $other_yachts as $yacht_item ) : ?>
<button type="button"
class="yacht-calendar-switcher-button"
data-yacht-id="<?php echo esc_attr( $yacht_item['id'] ); ?>"
aria-pressed="false">
<?php echo esc_html( $yacht_item['title'] ); ?>
</button>
<?php endforeach; ?>
</div>
</div>
<?php
}
/**
* Render calendar shortcode
*
* Usage: [yacht_calendar yacht_id="1" show_form="yes" height="600"]
*
* @param array $atts Shortcode attributes.
* @return string HTML output.
*/
public function render_calendar( $atts ) {
$atts = shortcode_atts(
array(
'yacht_id' => 0,
'show_form' => 'yes',
'height' => 600,
'primary_color' => '#2271b1',
'available_color' => '#35b56a',
'booked_color' => '#e53935',
),
$atts,
'yacht_calendar'
);
$yacht_id = (int) $atts['yacht_id'];
// If no yacht_id provided, get first yacht
if ( ! $yacht_id ) {
$yachts = get_posts(
array(
'post_type' => 'yacht',
'posts_per_page' => 1,
'orderby' => 'ID',
'order' => 'ASC',
'fields' => 'ids',
)
);
if ( empty( $yachts ) ) {
return '<div class="yacht-calendar-error" style="padding: 20px; background: #f8d7da; border: 1px solid #f5c6cb; border-radius: 4px;">' .
'<p><strong>' . esc_html__( 'Błąd:', 'yacht-booking' ) . '</strong> ' .
esc_html__( 'Brak jachtów w systemie. Dodaj przynajmniej jeden jacht w panelu administratora.', 'yacht-booking' ) .
'</p></div>';
}
$yacht_id = $yachts[0];
}
$yacht = get_post( $yacht_id );
if ( ! $yacht || 'yacht' !== $yacht->post_type ) {
return '<div class="yacht-calendar-error" style="padding: 20px; background: #f8d7da; border: 1px solid #f5c6cb; border-radius: 4px;">' .
'<p><strong>' . esc_html__( 'Błąd:', 'yacht-booking' ) . '</strong> ' .
esc_html__( 'Nie znaleziono jachtu o podanym ID.', 'yacht-booking' ) .
'</p></div>';
}
$booking_enabled = Settings::is_booking_enabled();
$yacht_switcher = $this->get_yacht_switcher_items();
$show_form = $booking_enabled && 'yes' === strtolower( $atts['show_form'] );
$height = (int) $atts['height'];
$primary_color = sanitize_hex_color( $atts['primary_color'] );
$available_bg = sanitize_hex_color( $atts['available_color'] );
$booked_bg = sanitize_hex_color( $atts['booked_color'] );
$calendar_id = 'yacht-calendar-' . wp_rand( 1000, 9999 );
$terms_url = Settings::get_terms_page_url();
ob_start();
?>
<div class="yacht-calendar-wrapper<?php echo $booking_enabled ? '' : ' yacht-calendar-view-only'; ?>"
data-yacht-id="<?php echo esc_attr( $yacht_id ); ?>"
data-calendar-id="<?php echo esc_attr( $calendar_id ); ?>"
data-primary-color="<?php echo esc_attr( $primary_color ); ?>"
data-available-bg="<?php echo esc_attr( $available_bg ); ?>"
data-booked-bg="<?php echo esc_attr( $booked_bg ); ?>"
data-yachts="<?php echo esc_attr( wp_json_encode( $yacht_switcher ) ); ?>"
data-booking-enabled="<?php echo $booking_enabled ? '1' : '0'; ?>">
<?php $this->render_yacht_switcher( $yacht_switcher, $yacht_id ); ?>
<div class="yacht-calendar-header">
<h3 class="yacht-calendar-title">
<?php if ( $booking_enabled ) : ?>
<?php
/* translators: %s: yacht name */
printf( esc_html__( 'Rezerwacja: %s', 'yacht-booking' ), esc_html( $yacht->post_title ) );
?>
<?php else : ?>
<?php
/* translators: %s: yacht name */
printf( esc_html__( 'Dostępność: %s', 'yacht-booking' ), esc_html( $yacht->post_title ) );
?>
<?php endif; ?>
</h3>
<div class="yacht-calendar-description"<?php echo $yacht->post_content ? '' : ' style="display: none;"'; ?>>
<?php echo wp_kses_post( wpautop( $yacht->post_content ) ); ?>
</div>
</div>
<div class="yacht-calendar-instructions">
<p>
<?php if ( $booking_enabled ) : ?>
<strong><?php esc_html_e( 'Instrukcja:', 'yacht-booking' ); ?></strong>
<?php esc_html_e( 'Zielone dni są dostępne, czerwone są zajęte lub zablokowane. Wybierz termin, przeciągając myszą po dostępnych dniach.', 'yacht-booking' ); ?>
<?php else : ?>
<?php esc_html_e( 'Zielone dni są dostępne, czerwone są zajęte lub zablokowane. Aby zarezerwować jacht, skontaktuj się z nami telefonicznie lub mailowo.', 'yacht-booking' ); ?>
<?php endif; ?>
</p>
</div>
<div class="yacht-calendar-legend" aria-label="<?php esc_attr_e( 'Legenda kalendarza rezerwacji', 'yacht-booking' ); ?>">
<span class="yacht-legend-item">
<span class="yacht-legend-swatch" style="background-color: <?php echo esc_attr( $available_bg ); ?>;"></span>
<?php esc_html_e( 'Dostępny', 'yacht-booking' ); ?>
</span>
<span class="yacht-legend-item">
<span class="yacht-legend-swatch" style="background-color: <?php echo esc_attr( $booked_bg ); ?>;"></span>
<?php esc_html_e( 'Zajęty / zablokowany', 'yacht-booking' ); ?>
</span>
<span class="yacht-legend-item">
<span class="yacht-legend-swatch yacht-legend-swatch-past"></span>
<?php esc_html_e( 'Data przeszła', 'yacht-booking' ); ?>
</span>
</div>
<?php if ( ! $booking_enabled ) : ?>
<div class="yacht-inquiry-layout">
<div class="yacht-inquiry-calendar-col">
<div id="<?php echo esc_attr( $calendar_id ); ?>"
class="yacht-calendar"
style="height: <?php echo esc_attr( $height ); ?>px;">
</div>
</div>
<div class="yacht-inquiry-form-col">
<div class="yacht-inquiry-form-container">
<h4><?php esc_html_e( 'Zapytaj o rezerwację', 'yacht-booking' ); ?></h4>
<p class="yacht-inquiry-desc">
<?php esc_html_e( 'Wypełnij formularz, a skontaktujemy się z Tobą w sprawie rezerwacji.', 'yacht-booking' ); ?>
</p>
<form class="yacht-inquiry-form" data-yacht-id="<?php echo esc_attr( $yacht_id ); ?>">
<?php wp_nonce_field( 'yacht_inquiry_submit', 'yacht_inquiry_nonce' ); ?>
<div class="form-field">
<label for="inquiry_name_<?php echo esc_attr( $calendar_id ); ?>">
<?php esc_html_e( 'Imię i nazwisko', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="text"
id="inquiry_name_<?php echo esc_attr( $calendar_id ); ?>"
name="customer_name"
required>
</div>
<div class="form-field">
<label for="inquiry_email_<?php echo esc_attr( $calendar_id ); ?>">
<?php esc_html_e( 'Email', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="email"
id="inquiry_email_<?php echo esc_attr( $calendar_id ); ?>"
name="customer_email"
required>
</div>
<div class="form-field">
<label for="inquiry_phone_<?php echo esc_attr( $calendar_id ); ?>">
<?php esc_html_e( 'Telefon', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="tel"
id="inquiry_phone_<?php echo esc_attr( $calendar_id ); ?>"
name="customer_phone"
required>
</div>
<div class="form-field">
<label for="inquiry_dates_<?php echo esc_attr( $calendar_id ); ?>">
<?php esc_html_e( 'Preferowane terminy', 'yacht-booking' ); ?>
</label>
<input type="text"
id="inquiry_dates_<?php echo esc_attr( $calendar_id ); ?>"
name="preferred_dates"
placeholder="<?php esc_attr_e( 'np. 15-22 lipca', 'yacht-booking' ); ?>">
</div>
<div class="form-field">
<label for="inquiry_message_<?php echo esc_attr( $calendar_id ); ?>">
<?php esc_html_e( 'Wiadomość', 'yacht-booking' ); ?>
</label>
<textarea id="inquiry_message_<?php echo esc_attr( $calendar_id ); ?>"
name="message"
rows="3"
placeholder="<?php esc_attr_e( 'Dodatkowe pytania lub uwagi...', 'yacht-booking' ); ?>"></textarea>
</div>
<?php if ( $terms_url ) : ?>
<p class="booking-terms">
<?php
printf(
wp_kses_post( __( 'Wysyłając formularz akceptujesz %s.', 'yacht-booking' ) ),
'<a href="' . esc_url( $terms_url ) . '" target="_blank" rel="noopener">' . esc_html__( 'regulamin', 'yacht-booking' ) . '</a>'
);
?>
</p>
<?php endif; ?>
<div class="form-actions">
<button type="submit" class="yacht-booking-submit">
<?php esc_html_e( 'Wyślij zapytanie', 'yacht-booking' ); ?>
</button>
</div>
<div class="yacht-inquiry-response"></div>
</form>
</div>
</div>
</div>
<?php else : ?>
<div id="<?php echo esc_attr( $calendar_id ); ?>"
class="yacht-calendar"
style="height: <?php echo esc_attr( $height ); ?>px;">
</div>
<?php if ( $show_form ) : ?>
<div class="yacht-booking-form-container" style="display: none;">
<h4><?php esc_html_e( 'Formularz Rezerwacji', 'yacht-booking' ); ?></h4>
<form class="yacht-booking-form" data-yacht-id="<?php echo esc_attr( $yacht_id ); ?>">
<?php wp_nonce_field( 'yacht_booking_submit', 'yacht_booking_nonce' ); ?>
<div class="form-row">
<div class="form-field">
<label for="booking_start_date_<?php echo esc_attr( $calendar_id ); ?>">
<?php esc_html_e( 'Data rozpoczęcia', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="text"
id="booking_start_date_<?php echo esc_attr( $calendar_id ); ?>"
name="start_date"
class="booking-start-date"
readonly
required>
</div>
<div class="form-field">
<label for="booking_end_date_<?php echo esc_attr( $calendar_id ); ?>">
<?php esc_html_e( 'Data zakończenia', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="text"
id="booking_end_date_<?php echo esc_attr( $calendar_id ); ?>"
name="end_date"
class="booking-end-date"
readonly
required>
</div>
</div>
<div class="form-field">
<label for="booking_name_<?php echo esc_attr( $calendar_id ); ?>">
<?php esc_html_e( 'Imię i nazwisko', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="text"
id="booking_name_<?php echo esc_attr( $calendar_id ); ?>"
name="customer_name"
required>
</div>
<div class="form-field">
<label for="booking_email_<?php echo esc_attr( $calendar_id ); ?>">
<?php esc_html_e( 'Email', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="email"
id="booking_email_<?php echo esc_attr( $calendar_id ); ?>"
name="customer_email"
required>
</div>
<div class="form-field">
<label for="booking_phone_<?php echo esc_attr( $calendar_id ); ?>">
<?php esc_html_e( 'Telefon', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="tel"
id="booking_phone_<?php echo esc_attr( $calendar_id ); ?>"
name="customer_phone"
required>
</div>
<?php if ( $terms_url ) : ?>
<p class="booking-terms">
<?php
printf(
wp_kses_post( __( 'Wysyłając formularz akceptujesz %s.', 'yacht-booking' ) ),
'<a href="' . esc_url( $terms_url ) . '" target="_blank" rel="noopener">' . esc_html__( 'regulamin', 'yacht-booking' ) . '</a>'
);
?>
</p>
<?php endif; ?>
<div class="form-actions">
<button type="submit" class="yacht-booking-submit">
<?php esc_html_e( 'Wyślij rezerwację', 'yacht-booking' ); ?>
</button>
</div>
<div class="yacht-booking-response"></div>
</form>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
<?php
return ob_get_clean();
}
}