250 lines
7.2 KiB
PHP
250 lines
7.2 KiB
PHP
<?php
|
|
/**
|
|
* Booking Custom Post Type
|
|
*
|
|
* @package YachtBooking
|
|
*/
|
|
|
|
namespace YachtBooking;
|
|
|
|
// Exit if accessed directly
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Booking CPT class
|
|
*/
|
|
class Booking {
|
|
|
|
/**
|
|
* Register custom post type
|
|
*/
|
|
public static function register() {
|
|
$labels = array(
|
|
'name' => __( 'Rezerwacje', 'yacht-booking' ),
|
|
'singular_name' => __( 'Rezerwacja', 'yacht-booking' ),
|
|
'menu_name' => __( 'Rezerwacje', 'yacht-booking' ),
|
|
'name_admin_bar' => __( 'Rezerwacja', 'yacht-booking' ),
|
|
'add_new' => __( 'Dodaj nową', 'yacht-booking' ),
|
|
'add_new_item' => __( 'Dodaj nową rezerwację', 'yacht-booking' ),
|
|
'new_item' => __( 'Nowa rezerwacja', 'yacht-booking' ),
|
|
'edit_item' => __( 'Edytuj rezerwację', 'yacht-booking' ),
|
|
'view_item' => __( 'Zobacz rezerwację', 'yacht-booking' ),
|
|
'all_items' => __( 'Wszystkie rezerwacje', 'yacht-booking' ),
|
|
'search_items' => __( 'Szukaj rezerwacji', 'yacht-booking' ),
|
|
'parent_item_colon' => __( 'Nadrzędna rezerwacja:', 'yacht-booking' ),
|
|
'not_found' => __( 'Nie znaleziono rezerwacji', 'yacht-booking' ),
|
|
'not_found_in_trash' => __( 'Nie znaleziono rezerwacji w koszu', 'yacht-booking' ),
|
|
);
|
|
|
|
$args = array(
|
|
'labels' => $labels,
|
|
'description' => __( 'Rezerwacje jachtów', 'yacht-booking' ),
|
|
'public' => false,
|
|
'publicly_queryable' => false,
|
|
'show_ui' => true,
|
|
'show_in_menu' => false, // Custom menu będzie w class-admin.php
|
|
'show_in_rest' => true,
|
|
'query_var' => true,
|
|
'rewrite' => false,
|
|
'capability_type' => 'post',
|
|
'capabilities' => array(
|
|
'edit_post' => 'yacht_booking_manage_bookings',
|
|
'read_post' => 'yacht_booking_manage_bookings',
|
|
'delete_post' => 'yacht_booking_manage_bookings',
|
|
'edit_posts' => 'yacht_booking_manage_bookings',
|
|
'edit_others_posts' => 'yacht_booking_manage_bookings',
|
|
'publish_posts' => 'yacht_booking_manage_bookings',
|
|
'read_private_posts' => 'yacht_booking_manage_bookings',
|
|
'delete_posts' => 'yacht_booking_manage_bookings',
|
|
),
|
|
'has_archive' => false,
|
|
'hierarchical' => false,
|
|
'menu_position' => null,
|
|
'supports' => array( 'title', 'custom-fields' ),
|
|
);
|
|
|
|
register_post_type( 'yacht_booking', $args );
|
|
}
|
|
|
|
/**
|
|
* Get booking yacht ID
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @return int
|
|
*/
|
|
public static function get_yacht_id( $booking_id ) {
|
|
return (int) get_post_meta( $booking_id, '_booking_yacht_id', true );
|
|
}
|
|
|
|
/**
|
|
* Get booking start date
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @return string
|
|
*/
|
|
public static function get_start_date( $booking_id ) {
|
|
return get_post_meta( $booking_id, '_booking_start_date', true );
|
|
}
|
|
|
|
/**
|
|
* Get booking end date
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @return string
|
|
*/
|
|
public static function get_end_date( $booking_id ) {
|
|
return get_post_meta( $booking_id, '_booking_end_date', true );
|
|
}
|
|
|
|
/**
|
|
* Get booking status
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @return string
|
|
*/
|
|
public static function get_status( $booking_id ) {
|
|
$status = get_post_meta( $booking_id, '_booking_status', true );
|
|
return $status ? $status : 'pending';
|
|
}
|
|
|
|
/**
|
|
* Get customer name
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @return string
|
|
*/
|
|
public static function get_customer_name( $booking_id ) {
|
|
return get_post_meta( $booking_id, '_booking_customer_name', true );
|
|
}
|
|
|
|
/**
|
|
* Get customer email
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @return string
|
|
*/
|
|
public static function get_customer_email( $booking_id ) {
|
|
return get_post_meta( $booking_id, '_booking_customer_email', true );
|
|
}
|
|
|
|
/**
|
|
* Get customer phone
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @return string
|
|
*/
|
|
public static function get_customer_phone( $booking_id ) {
|
|
return get_post_meta( $booking_id, '_booking_customer_phone', true );
|
|
}
|
|
|
|
/**
|
|
* Get total price
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @return float
|
|
*/
|
|
public static function get_total_price( $booking_id ) {
|
|
return (float) get_post_meta( $booking_id, '_booking_total_price', true );
|
|
}
|
|
|
|
/**
|
|
* Get Google Calendar Event ID
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @return string
|
|
*/
|
|
public static function get_gcal_event_id( $booking_id ) {
|
|
return get_post_meta( $booking_id, '_booking_gcal_event_id', true );
|
|
}
|
|
|
|
/**
|
|
* Get admin notes
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @return string
|
|
*/
|
|
public static function get_notes( $booking_id ) {
|
|
return get_post_meta( $booking_id, '_booking_notes', true );
|
|
}
|
|
|
|
/**
|
|
* Update booking status
|
|
*
|
|
* @param int $booking_id Booking post ID.
|
|
* @param string $status Status (pending, confirmed, cancelled).
|
|
*/
|
|
public static function update_status( $booking_id, $status ) {
|
|
$valid_statuses = array( 'pending', 'confirmed', 'cancelled' );
|
|
|
|
if ( in_array( $status, $valid_statuses, true ) ) {
|
|
update_post_meta( $booking_id, '_booking_status', $status );
|
|
|
|
// Trigger action hook
|
|
do_action( 'yacht_booking_status_changed', $booking_id, $status );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create new booking
|
|
*
|
|
* @param array $data Booking data.
|
|
* @return int|false Booking ID on success, false on failure.
|
|
*/
|
|
public static function create( $data ) {
|
|
// Validate required fields
|
|
$required = array( 'yacht_id', 'start_date', 'end_date', 'customer_name', 'customer_email', 'customer_phone' );
|
|
foreach ( $required as $field ) {
|
|
if ( empty( $data[ $field ] ) ) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Get yacht title
|
|
$yacht = get_post( $data['yacht_id'] );
|
|
if ( ! $yacht || $yacht->post_type !== 'yacht' ) {
|
|
return false;
|
|
}
|
|
|
|
// Create booking post
|
|
$booking_id = wp_insert_post(
|
|
array(
|
|
'post_type' => 'yacht_booking',
|
|
'post_title' => sprintf(
|
|
__( 'Rezerwacja #%s - %s', 'yacht-booking' ),
|
|
time(),
|
|
$yacht->post_title
|
|
),
|
|
'post_status' => 'publish',
|
|
)
|
|
);
|
|
|
|
if ( is_wp_error( $booking_id ) ) {
|
|
return false;
|
|
}
|
|
|
|
// Save booking meta
|
|
update_post_meta( $booking_id, '_booking_yacht_id', (int) $data['yacht_id'] );
|
|
update_post_meta( $booking_id, '_booking_start_date', sanitize_text_field( $data['start_date'] ) );
|
|
update_post_meta( $booking_id, '_booking_end_date', sanitize_text_field( $data['end_date'] ) );
|
|
update_post_meta( $booking_id, '_booking_status', ! empty( $data['status'] ) ? $data['status'] : 'pending' );
|
|
update_post_meta( $booking_id, '_booking_customer_name', sanitize_text_field( $data['customer_name'] ) );
|
|
update_post_meta( $booking_id, '_booking_customer_email', sanitize_email( $data['customer_email'] ) );
|
|
update_post_meta( $booking_id, '_booking_customer_phone', sanitize_text_field( $data['customer_phone'] ) );
|
|
|
|
if ( ! empty( $data['total_price'] ) ) {
|
|
update_post_meta( $booking_id, '_booking_total_price', (float) $data['total_price'] );
|
|
}
|
|
|
|
if ( ! empty( $data['notes'] ) ) {
|
|
update_post_meta( $booking_id, '_booking_notes', wp_kses_post( $data['notes'] ) );
|
|
}
|
|
|
|
// Trigger action hook
|
|
do_action( 'yacht_booking_created', $booking_id );
|
|
|
|
return $booking_id;
|
|
}
|
|
}
|