264 lines
13 KiB
PHP
264 lines
13 KiB
PHP
<?php
|
||
/**
|
||
* Plugin Name: Carei Reservation
|
||
* Description: Formularz rezerwacji samochodu zintegrowany z Softra Rent API, jako widget Elementor.
|
||
* Version: 1.0.0
|
||
* Author: Carei
|
||
* Text Domain: carei-reservation
|
||
* Requires PHP: 7.4
|
||
*/
|
||
|
||
if ( ! defined( 'ABSPATH' ) ) {
|
||
exit;
|
||
}
|
||
|
||
define( 'CAREI_RESERVATION_VERSION', '1.0.0' );
|
||
define( 'CAREI_RESERVATION_PATH', plugin_dir_path( __FILE__ ) );
|
||
define( 'CAREI_RESERVATION_URL', plugin_dir_url( __FILE__ ) );
|
||
|
||
/**
|
||
* Parse .env file (format: "key: value")
|
||
*/
|
||
function carei_parse_env() {
|
||
$env_path = ABSPATH . '.env';
|
||
if ( ! file_exists( $env_path ) ) {
|
||
return array();
|
||
}
|
||
|
||
$lines = file( $env_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
|
||
$env = array();
|
||
|
||
foreach ( $lines as $line ) {
|
||
$line = trim( $line );
|
||
if ( '' === $line || '#' === $line[0] ) {
|
||
continue;
|
||
}
|
||
$pos = strpos( $line, ':' );
|
||
if ( false === $pos ) {
|
||
continue;
|
||
}
|
||
$key = trim( substr( $line, 0, $pos ) );
|
||
$value = trim( substr( $line, $pos + 1 ) );
|
||
$env[ $key ] = $value;
|
||
}
|
||
|
||
return $env;
|
||
}
|
||
|
||
/**
|
||
* Load includes
|
||
*/
|
||
require_once CAREI_RESERVATION_PATH . 'includes/class-softra-api.php';
|
||
require_once CAREI_RESERVATION_PATH . 'includes/class-rest-proxy.php';
|
||
require_once CAREI_RESERVATION_PATH . 'includes/class-admin-panel.php';
|
||
|
||
/**
|
||
* Load plugin textdomain
|
||
*/
|
||
add_action( 'plugins_loaded', function () {
|
||
load_plugin_textdomain( 'carei-reservation', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
|
||
} );
|
||
|
||
/**
|
||
* Initialize plugin on plugins_loaded
|
||
*/
|
||
add_action( 'plugins_loaded', function () {
|
||
$env = carei_parse_env();
|
||
|
||
$api_url = isset( $env['url'] ) ? $env['url'] : '';
|
||
$username = isset( $env['username'] ) ? $env['username'] : '';
|
||
$password = isset( $env['password'] ) ? $env['password'] : '';
|
||
|
||
if ( empty( $api_url ) || empty( $username ) || empty( $password ) ) {
|
||
add_action( 'admin_notices', function () {
|
||
echo '<div class="notice notice-error"><p><strong>Carei Reservation:</strong> ' . esc_html__( 'Brak konfiguracji API w pliku .env (url, username, password).', 'carei-reservation' ) . '</p></div>';
|
||
} );
|
||
return;
|
||
}
|
||
|
||
// Initialize Softra API singleton
|
||
Carei_Softra_API::init( $api_url, $username, $password );
|
||
|
||
// Initialize REST proxy
|
||
new Carei_REST_Proxy();
|
||
|
||
// Initialize admin panel
|
||
new Carei_Admin_Panel();
|
||
} );
|
||
|
||
/**
|
||
* Register Elementor widget
|
||
*/
|
||
add_action( 'elementor/widgets/register', function ( $widgets_manager ) {
|
||
require_once CAREI_RESERVATION_PATH . 'includes/class-elementor-widget.php';
|
||
$widgets_manager->register( new Carei_Reservation_Widget() );
|
||
|
||
require_once CAREI_RESERVATION_PATH . 'includes/class-search-widget.php';
|
||
$widgets_manager->register( new Carei_Search_Widget() );
|
||
|
||
require_once CAREI_RESERVATION_PATH . 'includes/class-map-widget.php';
|
||
$widgets_manager->register( new Carei_Map_Widget() );
|
||
|
||
require_once CAREI_RESERVATION_PATH . 'includes/class-cities-widget.php';
|
||
$widgets_manager->register( new Carei_Cities_Widget() );
|
||
|
||
require_once CAREI_RESERVATION_PATH . 'includes/class-branches-widget.php';
|
||
$widgets_manager->register( new Carei_Branches_Widget() );
|
||
} );
|
||
|
||
/**
|
||
* Enqueue frontend assets
|
||
*/
|
||
add_action( 'wp_enqueue_scripts', function () {
|
||
// Flatpickr — cross-browser, locale-aware date/time picker (CDN)
|
||
wp_register_style(
|
||
'carei-flatpickr-css',
|
||
'https://cdn.jsdelivr.net/npm/flatpickr@4.6.13/dist/flatpickr.min.css',
|
||
array(),
|
||
'4.6.13'
|
||
);
|
||
wp_register_script(
|
||
'carei-flatpickr-js',
|
||
'https://cdn.jsdelivr.net/npm/flatpickr@4.6.13/dist/flatpickr.min.js',
|
||
array(),
|
||
'4.6.13',
|
||
true
|
||
);
|
||
wp_register_script(
|
||
'carei-flatpickr-pl',
|
||
'https://cdn.jsdelivr.net/npm/flatpickr@4.6.13/dist/l10n/pl.js',
|
||
array( 'carei-flatpickr-js' ),
|
||
'4.6.13',
|
||
true
|
||
);
|
||
|
||
wp_register_style(
|
||
'carei-reservation-css',
|
||
CAREI_RESERVATION_URL . 'assets/css/carei-reservation.css',
|
||
array( 'carei-flatpickr-css' ),
|
||
CAREI_RESERVATION_VERSION
|
||
);
|
||
wp_register_script(
|
||
'carei-reservation-js',
|
||
CAREI_RESERVATION_URL . 'assets/js/carei-reservation.js',
|
||
array( 'carei-flatpickr-js', 'carei-flatpickr-pl' ),
|
||
CAREI_RESERVATION_VERSION,
|
||
true
|
||
);
|
||
wp_localize_script( 'carei-reservation-js', 'careiReservation', array(
|
||
'restUrl' => esc_url_raw( rest_url( 'carei/v1/' ) ),
|
||
'nonce' => wp_create_nonce( 'wp_rest' ),
|
||
) );
|
||
|
||
wp_localize_script( 'carei-reservation-js', 'careiI18n', array(
|
||
// API / sieć
|
||
'errorApiHttp' => esc_html__( 'Błąd API: HTTP %status%', 'carei-reservation' ),
|
||
'errorTimeout' => esc_html__( 'Przekroczono czas oczekiwania. Spróbuj ponownie.', 'carei-reservation' ),
|
||
'errorNetwork' => esc_html__( 'Brak połączenia z serwerem. Sprawdź internet i spróbuj ponownie.', 'carei-reservation' ),
|
||
'errorLoading' => esc_html__( 'Błąd ładowania', 'carei-reservation' ),
|
||
|
||
// Selecty / placeholdery
|
||
'selectPlaceholder' => esc_html__( 'Wybierz...', 'carei-reservation' ),
|
||
'selectSegment' => esc_html__( 'Wybierz segment pojazdu', 'carei-reservation' ),
|
||
'selectSegmentShort' => esc_html__( 'Wybierz segment', 'carei-reservation' ),
|
||
'selectPickup' => esc_html__( 'Wybierz miejsce odbioru', 'carei-reservation' ),
|
||
'selectReturn' => esc_html__( 'Wybierz miejsce zwrotu', 'carei-reservation' ),
|
||
'segmentLabel' => esc_html__( 'Segment %name%', 'carei-reservation' ),
|
||
'noSegments' => esc_html__( 'Brak segmentów', 'carei-reservation' ),
|
||
'pickupPlaceholder' => esc_html__( 'Najpierw wybierz segment', 'carei-reservation' ),
|
||
'pickupLabel' => esc_html__( 'Miejsce odbioru', 'carei-reservation' ),
|
||
'returnLabel' => esc_html__( 'Miejsce zwrotu', 'carei-reservation' ),
|
||
'noPickupForSegment' => esc_html__( 'Brak lokalizacji dla tego segmentu', 'carei-reservation' ),
|
||
'noLocations' => esc_html__( 'Brak lokalizacji', 'carei-reservation' ),
|
||
|
||
// Daty
|
||
'dateStart' => esc_html__( 'Rozpoczęcie', 'carei-reservation' ),
|
||
'dateEnd' => esc_html__( 'Zakończenie', 'carei-reservation' ),
|
||
'warnPastDate' => esc_html__( '%label% — data lub godzina już minęły', 'carei-reservation' ),
|
||
'dateStartPast' => esc_html__( 'Data lub godzina rozpoczęcia już minęły', 'carei-reservation' ),
|
||
'dateEndPast' => esc_html__( 'Data lub godzina zakończenia już minęły', 'carei-reservation' ),
|
||
'dateEndAfterStart' => esc_html__( 'Data zakończenia musi być po dacie rozpoczęcia', 'carei-reservation' ),
|
||
'enterDateFrom' => esc_html__( 'Podaj datę rozpoczęcia', 'carei-reservation' ),
|
||
'enterDateTo' => esc_html__( 'Podaj datę zakończenia', 'carei-reservation' ),
|
||
|
||
// Dni/Doby (pluralizacja PL)
|
||
'daysCount' => wp_kses( __( 'Wybrano: <strong>%count% %unit%</strong>', 'carei-reservation' ), array( 'strong' => array() ) ),
|
||
'dayWord' => esc_html__( 'dzień', 'carei-reservation' ),
|
||
'daysWord' => esc_html__( 'dni', 'carei-reservation' ),
|
||
'dayOne' => esc_html__( 'doba', 'carei-reservation' ),
|
||
'dayFew' => esc_html__( 'doby', 'carei-reservation' ),
|
||
'dayMany' => esc_html__( 'dób', 'carei-reservation' ),
|
||
|
||
// Ceny
|
||
'pricePerDay' => esc_html__( '%price% zł/doba', 'carei-reservation' ),
|
||
'priceSimple' => esc_html__( '%price% zł', 'carei-reservation' ),
|
||
'priceSimpleFmt' => esc_html__( '%price% zł', 'carei-reservation' ),
|
||
'priceRange' => esc_html__( 'od %min% do %max% zł', 'carei-reservation' ),
|
||
'priceLineDaily' => esc_html__( '%perDay% zł/doba × %days% = %total% zł', 'carei-reservation' ),
|
||
'free' => esc_html__( 'Gratis', 'carei-reservation' ),
|
||
|
||
// Zagranica / akcje
|
||
'btnRemove' => esc_html__( 'Usuń', 'carei-reservation' ),
|
||
'btnAdd' => esc_html__( 'Dodaj', 'carei-reservation' ),
|
||
|
||
// Walidacja formularza
|
||
'enterFirstName' => esc_html__( 'Podaj imię', 'carei-reservation' ),
|
||
'enterLastName' => esc_html__( 'Podaj nazwisko', 'carei-reservation' ),
|
||
'enterEmail' => esc_html__( 'Podaj poprawny adres e-mail', 'carei-reservation' ),
|
||
'enterPhone' => esc_html__( 'Podaj numer telefonu (min. 9 cyfr)', 'carei-reservation' ),
|
||
'privacyRequired' => esc_html__( 'Wymagana zgoda na Politykę Prywatności', 'carei-reservation' ),
|
||
|
||
// Przyciski
|
||
'btnSubmit' => esc_html__( 'Wyślij', 'carei-reservation' ),
|
||
'btnProcessing' => esc_html__( 'Przetwarzanie...', 'carei-reservation' ),
|
||
'btnBookingInProgress' => esc_html__( 'Rezerwuję...', 'carei-reservation' ),
|
||
'btnConfirmBooking' => esc_html__( 'Potwierdź rezerwację', 'carei-reservation' ),
|
||
|
||
// Flow klienta / rezerwacji
|
||
'loadingSummary' => esc_html__( 'Ładowanie podsumowania...', 'carei-reservation' ),
|
||
'errorCustomerCreate' => esc_html__( 'Nie udało się utworzyć klienta', 'carei-reservation' ),
|
||
'errorCustomerCreatePrefix' => esc_html__( 'Błąd tworzenia klienta: %msg%', 'carei-reservation' ),
|
||
'errorSummaryPrefix' => esc_html__( 'Błąd pobierania podsumowania: %msg%', 'carei-reservation' ),
|
||
'errorBookingFailed' => esc_html__( 'Rezerwacja nie powiodła się', 'carei-reservation' ),
|
||
|
||
// Pakiet ochronny (komentarz booking)
|
||
'protectionComment' => esc_html__( 'Pakiet ochronny: %name% — %perDay% zł/doba × %days% = %total% zł (do doliczenia poza systemem)', 'carei-reservation' ),
|
||
|
||
// Podsumowanie — etykiety
|
||
'labelSegment' => esc_html__( 'Segment', 'carei-reservation' ),
|
||
'labelFrom' => esc_html__( 'Od', 'carei-reservation' ),
|
||
'labelTo' => esc_html__( 'Do', 'carei-reservation' ),
|
||
'labelPickup' => esc_html__( 'Miejsce odbioru', 'carei-reservation' ),
|
||
'labelReturn' => esc_html__( 'Miejsce zwrotu', 'carei-reservation' ),
|
||
'labelRenter' => esc_html__( 'Najemca', 'carei-reservation' ),
|
||
'labelEmail' => esc_html__( 'Email', 'carei-reservation' ),
|
||
'labelPhone' => esc_html__( 'Telefon', 'carei-reservation' ),
|
||
'labelSelectedOptions' => esc_html__( 'Wybrane opcje', 'carei-reservation' ),
|
||
'labelMessage' => esc_html__( 'Wiadomość', 'carei-reservation' ),
|
||
'labelAuto' => esc_html__( 'auto', 'carei-reservation' ),
|
||
'labelExtraCharge' => esc_html__( 'do doliczenia', 'carei-reservation' ),
|
||
'labelVat' => esc_html__( 'VAT', 'carei-reservation' ),
|
||
'labelProtectionPackage' => esc_html__( 'Pakiet ochronny', 'carei-reservation' ),
|
||
'labelToPay' => esc_html__( 'Do zapłaty', 'carei-reservation' ),
|
||
|
||
// Tabela
|
||
'thName' => esc_html__( 'Nazwa', 'carei-reservation' ),
|
||
'thQuantity' => esc_html__( 'Ilość', 'carei-reservation' ),
|
||
'thNet' => esc_html__( 'Netto', 'carei-reservation' ),
|
||
'thGross' => esc_html__( 'Brutto', 'carei-reservation' ),
|
||
|
||
// Reject reasons (Softra → PL fallback)
|
||
'rejectCarNotFound' => esc_html__( 'Brak dostępnego pojazdu w wybranym terminie. Zmień daty lub segment.', 'carei-reservation' ),
|
||
'rejectInvalidDateRange' => esc_html__( 'Nieprawidłowy zakres dat', 'carei-reservation' ),
|
||
'rejectBranchNotFound' => esc_html__( 'Nie znaleziono oddziału', 'carei-reservation' ),
|
||
'rejectCustomerExists' => esc_html__( 'Klient o tych danych już istnieje w systemie', 'carei-reservation' ),
|
||
'rejectInvalidPesel' => esc_html__( 'Nieprawidłowy numer PESEL', 'carei-reservation' ),
|
||
'rejectPriceListExpired' => esc_html__( 'Cennik wygasł. Odśwież formularz i spróbuj ponownie.', 'carei-reservation' ),
|
||
|
||
// Success / aria
|
||
'orderNumber' => esc_html__( 'Nr zamówienia: %no%', 'carei-reservation' ),
|
||
'announceBookingConfirmed' => esc_html__( 'Rezerwacja potwierdzona', 'carei-reservation' ),
|
||
'announceSummary' => esc_html__( 'Podsumowanie rezerwacji', 'carei-reservation' ),
|
||
) );
|
||
} );
|