first commit

This commit is contained in:
Roman Pyrih
2026-04-21 15:48:41 +02:00
commit 7483681901
10216 changed files with 3236626 additions and 0 deletions

View File

@@ -0,0 +1,524 @@
<?php
/**
* REST API Controller
*
* @package YachtBooking
*/
namespace YachtBooking;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST Controller class
*/
class Rest_Controller extends \WP_REST_Controller {
/**
* API namespace
*
* @var string
*/
const NAMESPACE = 'yacht-booking/v1';
/**
* Constructor
*/
public function __construct() {
// Hook into booking created event for email notifications
add_action( 'yacht_booking_created', array( $this, 'send_booking_notification' ), 10, 1 );
}
/**
* Register REST API routes
*/
public function register_routes() {
// GET /yacht-booking/v1/yachts
register_rest_route(
self::NAMESPACE,
'/yachts',
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_yachts' ),
'permission_callback' => '__return_true',
)
);
// GET /yacht-booking/v1/yachts/{id}
register_rest_route(
self::NAMESPACE,
'/yachts/(?P<id>\d+)',
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_yacht' ),
'permission_callback' => '__return_true',
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function( $param ) {
return is_numeric( $param );
},
),
),
)
);
// GET /yacht-booking/v1/availability/{yacht_id}
register_rest_route(
self::NAMESPACE,
'/availability/(?P<yacht_id>\d+)',
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_availability' ),
'permission_callback' => '__return_true',
'args' => array(
'yacht_id' => array(
'required' => true,
'validate_callback' => function( $param ) {
return is_numeric( $param );
},
),
'start' => array(
'required' => true,
'validate_callback' => function( $param ) {
return strtotime( $param ) !== false;
},
),
'end' => array(
'required' => true,
'validate_callback' => function( $param ) {
return strtotime( $param ) !== false;
},
),
),
)
);
// POST /yacht-booking/v1/bookings
register_rest_route(
self::NAMESPACE,
'/bookings',
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_booking' ),
'permission_callback' => '__return_true',
'args' => array(
'yacht_id' => array(
'required' => true,
'validate_callback' => function( $param ) {
return is_numeric( $param ) && get_post_type( $param ) === 'yacht';
},
'sanitize_callback' => 'absint',
),
'start_date' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
'end_date' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
'customer_name' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
'customer_email' => array(
'required' => true,
'validate_callback' => 'is_email',
'sanitize_callback' => 'sanitize_email',
),
'customer_phone' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
),
)
);
// POST /yacht-booking/v1/inquiries
register_rest_route(
self::NAMESPACE,
'/inquiries',
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_inquiry' ),
'permission_callback' => '__return_true',
'args' => array(
'yacht_id' => array(
'required' => true,
'validate_callback' => function( $param ) {
return is_numeric( $param ) && get_post_type( $param ) === 'yacht';
},
'sanitize_callback' => 'absint',
),
'customer_name' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
'customer_email' => array(
'required' => true,
'validate_callback' => 'is_email',
'sanitize_callback' => 'sanitize_email',
),
'customer_phone' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
'preferred_dates' => array(
'required' => false,
'sanitize_callback' => 'sanitize_text_field',
),
'message' => array(
'required' => false,
'sanitize_callback' => 'sanitize_textarea_field',
),
),
)
);
// GET /yacht-booking/v1/bookings (admin only)
register_rest_route(
self::NAMESPACE,
'/bookings',
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_bookings' ),
'permission_callback' => array( $this, 'can_manage_bookings' ),
)
);
// PUT /yacht-booking/v1/bookings/{id}/status (admin only)
register_rest_route(
self::NAMESPACE,
'/bookings/(?P<id>\d+)/status',
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_booking_status' ),
'permission_callback' => array( $this, 'can_manage_bookings' ),
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => 'is_numeric',
'sanitize_callback' => 'absint',
),
'status' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
),
)
);
}
/**
* Get all yachts
*
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response
*/
public function get_yachts( $request ) {
$yachts = get_posts(
array(
'post_type' => 'yacht',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
)
);
$data = array();
foreach ( $yachts as $yacht ) {
$data[] = array(
'id' => $yacht->ID,
'title' => $yacht->post_title,
'description' => $yacht->post_content,
'capacity' => Yacht::get_capacity( $yacht->ID ),
'price_per_day' => Yacht::get_price_per_day( $yacht->ID ),
'features' => Yacht::get_features( $yacht->ID ),
'image' => get_the_post_thumbnail_url( $yacht->ID, 'large' ),
);
}
return rest_ensure_response( $data );
}
/**
* Get single yacht
*
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response|\WP_Error
*/
public function get_yacht( $request ) {
$yacht_id = $request->get_param( 'id' );
$yacht = get_post( $yacht_id );
if ( ! $yacht || $yacht->post_type !== 'yacht' ) {
return new \WP_Error( 'not_found', __( 'Jacht nie znaleziony', 'yacht-booking' ), array( 'status' => 404 ) );
}
$data = array(
'id' => $yacht->ID,
'title' => $yacht->post_title,
'description' => $yacht->post_content,
'capacity' => Yacht::get_capacity( $yacht->ID ),
'price_per_day' => Yacht::get_price_per_day( $yacht->ID ),
'features' => Yacht::get_features( $yacht->ID ),
'image' => get_the_post_thumbnail_url( $yacht->ID, 'large' ),
);
return rest_ensure_response( $data );
}
/**
* Get yacht availability
*
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response
*/
public function get_availability( $request ) {
$yacht_id = $request->get_param( 'yacht_id' );
$start = $request->get_param( 'start' );
$end = $request->get_param( 'end' );
$calendar = Availability::get_availability_calendar( $yacht_id, $start, $end );
// Convert to array format for FullCalendar
$events = array();
foreach ( $calendar as $date => $info ) {
$events[] = array(
'date' => $date,
'status' => $info['status'],
);
}
return rest_ensure_response( $events );
}
/**
* Create new booking
*
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response|\WP_Error
*/
public function create_booking( $request ) {
// Check if online booking is enabled
if ( ! Settings::is_booking_enabled() ) {
return new \WP_Error(
'booking_disabled',
__( 'Rezerwacje online są obecnie wyłączone. Skontaktuj się z nami telefonicznie lub mailowo.', 'yacht-booking' ),
array( 'status' => 403 )
);
}
// Verify nonce
if ( ! wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ) ) {
return new \WP_Error( 'invalid_nonce', __( 'Nieprawidłowy token bezpieczeństwa', 'yacht-booking' ), array( 'status' => 403 ) );
}
$yacht_id = $request->get_param( 'yacht_id' );
$start_date = $request->get_param( 'start_date' );
$end_date = $request->get_param( 'end_date' );
// Check availability
if ( ! Availability::is_available( $yacht_id, $start_date, $end_date ) ) {
return new \WP_Error(
'not_available',
__( 'Jacht niedostępny w wybranych terminach', 'yacht-booking' ),
array( 'status' => 400 )
);
}
// Calculate price
$days = Availability::count_days( $start_date, $end_date );
$price_per_day = Yacht::get_price_per_day( $yacht_id );
$total_price = $days * $price_per_day;
// Create booking
$booking_id = Booking::create(
array(
'yacht_id' => $yacht_id,
'start_date' => $start_date,
'end_date' => $end_date,
'customer_name' => $request->get_param( 'customer_name' ),
'customer_email' => $request->get_param( 'customer_email' ),
'customer_phone' => $request->get_param( 'customer_phone' ),
'total_price' => $total_price,
'status' => Settings::get_default_status(),
)
);
if ( ! $booking_id ) {
return new \WP_Error(
'booking_failed',
__( 'Nie udało się utworzyć rezerwacji', 'yacht-booking' ),
array( 'status' => 500 )
);
}
// Mark as booked in availability cache
Availability::mark_as_booked( $yacht_id, $start_date, $end_date, $booking_id );
return rest_ensure_response(
array(
'success' => true,
'message' => __( 'Rezerwacja została wysłana pomyślnie!', 'yacht-booking' ),
'booking_id' => $booking_id,
)
);
}
/**
* Create new inquiry
*
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response|\WP_Error
*/
public function create_inquiry( $request ) {
// Verify nonce
if ( ! wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ) ) {
return new \WP_Error( 'invalid_nonce', __( 'Nieprawidłowy token bezpieczeństwa', 'yacht-booking' ), array( 'status' => 403 ) );
}
$inquiry_id = Inquiry::create(
array(
'yacht_id' => $request->get_param( 'yacht_id' ),
'customer_name' => $request->get_param( 'customer_name' ),
'customer_email' => $request->get_param( 'customer_email' ),
'customer_phone' => $request->get_param( 'customer_phone' ),
'preferred_dates' => $request->get_param( 'preferred_dates' ),
'message' => $request->get_param( 'message' ),
)
);
if ( ! $inquiry_id ) {
return new \WP_Error(
'inquiry_failed',
__( 'Nie udało się wysłać zapytania', 'yacht-booking' ),
array( 'status' => 500 )
);
}
Inquiry::send_emails( $inquiry_id );
return rest_ensure_response(
array(
'success' => true,
'message' => __( 'Twoje zapytanie zostało wysłane. Sprawdź swoją skrzynkę email — wysłaliśmy potwierdzenie.', 'yacht-booking' ),
)
);
}
/**
* Check if current user can manage bookings.
*
* @return bool
*/
public function can_manage_bookings() {
return current_user_can( 'yacht_booking_manage_bookings' );
}
/**
* Get bookings list (admin only).
*
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response
*/
public function get_bookings( $request ) {
$bookings = get_posts(
array(
'post_type' => 'yacht_booking',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
)
);
$data = array();
foreach ( $bookings as $booking ) {
$booking_id = $booking->ID;
$yacht_id = Booking::get_yacht_id( $booking_id );
$yacht = get_post( $yacht_id );
$data[] = array(
'id' => $booking_id,
'yacht_id' => $yacht_id,
'yacht_name' => $yacht ? $yacht->post_title : '',
'start_date' => Booking::get_start_date( $booking_id ),
'end_date' => Booking::get_end_date( $booking_id ),
'status' => Booking::get_status( $booking_id ),
'customer_name' => Booking::get_customer_name( $booking_id ),
'customer_email' => Booking::get_customer_email( $booking_id ),
'customer_phone' => Booking::get_customer_phone( $booking_id ),
'total_price' => Booking::get_total_price( $booking_id ),
'created_at' => get_the_date( 'Y-m-d H:i:s', $booking ),
);
}
return rest_ensure_response( $data );
}
/**
* Update booking status (admin only).
*
* @param \WP_REST_Request $request Request object.
* @return \WP_REST_Response|\WP_Error
*/
public function update_booking_status( $request ) {
$booking_id = absint( $request->get_param( 'id' ) );
$status = sanitize_text_field( $request->get_param( 'status' ) );
if ( ! in_array( $status, array( 'pending', 'confirmed', 'cancelled' ), true ) ) {
return new \WP_Error( 'invalid_status', __( 'Nieprawidłowy status rezerwacji', 'yacht-booking' ), array( 'status' => 400 ) );
}
$booking = get_post( $booking_id );
if ( ! $booking || 'yacht_booking' !== $booking->post_type ) {
return new \WP_Error( 'not_found', __( 'Rezerwacja nie została znaleziona', 'yacht-booking' ), array( 'status' => 404 ) );
}
Booking::update_status( $booking_id, $status );
if ( 'cancelled' === $status ) {
Availability::clear_booking_availability( $booking_id );
}
return rest_ensure_response(
array(
'success' => true,
'status' => $status,
)
);
}
/**
* Send booking notification email to admin
*
* @param int $booking_id Booking post ID.
*/
public function send_booking_notification( $booking_id ) {
$template_data = Email_Templates::get_booking_template_data( $booking_id );
$template = Email_Templates::compile( Email_Templates::TYPE_ADMIN_NEW_BOOKING, $template_data );
$admin_email = get_option( 'admin_email' );
$customer_email = Booking::get_customer_email( $booking_id );
if ( empty( $template['subject'] ) || empty( $admin_email ) ) {
return;
}
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . Settings::get_email_from_name() . ' <' . Settings::get_email_from_address() . '>',
'Reply-To: ' . $template_data['customer_name'] . ' <' . $customer_email . '>',
);
wp_mail( $admin_email, $template['subject'], $template['body'], $headers );
do_action( 'yacht_booking_notification_sent', $booking_id, $admin_email );
}
}