Files
2026-05-10 15:08:40 +02:00

189 lines
5.7 KiB
PHP

<?php
/**
* Yacht Custom Post Type
*
* @package YachtBooking
*/
namespace YachtBooking;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Yacht CPT class
*/
class Yacht {
/**
* Register custom post type
*/
public static function register() {
$labels = array(
'name' => __( 'Jachty', 'yacht-booking' ),
'singular_name' => __( 'Jacht', 'yacht-booking' ),
'menu_name' => __( 'Jachty', 'yacht-booking' ),
'name_admin_bar' => __( 'Jacht', 'yacht-booking' ),
'add_new' => __( 'Dodaj nowy', 'yacht-booking' ),
'add_new_item' => __( 'Dodaj nowy jacht', 'yacht-booking' ),
'new_item' => __( 'Nowy jacht', 'yacht-booking' ),
'edit_item' => __( 'Edytuj jacht', 'yacht-booking' ),
'view_item' => __( 'Zobacz jacht', 'yacht-booking' ),
'all_items' => __( 'Wszystkie jachty', 'yacht-booking' ),
'search_items' => __( 'Szukaj jachtów', 'yacht-booking' ),
'parent_item_colon' => __( 'Nadrzędny jacht:', 'yacht-booking' ),
'not_found' => __( 'Nie znaleziono jachtów', 'yacht-booking' ),
'not_found_in_trash' => __( 'Nie znaleziono jachtów w koszu', 'yacht-booking' ),
'featured_image' => __( 'Zdjęcie jachtu', 'yacht-booking' ),
'set_featured_image' => __( 'Ustaw zdjęcie jachtu', 'yacht-booking' ),
'remove_featured_image' => __( 'Usuń zdjęcie jachtu', 'yacht-booking' ),
'use_featured_image' => __( 'Użyj jako zdjęcie jachtu', 'yacht-booking' ),
);
$args = array(
'labels' => $labels,
'description' => __( 'Jachty dostępne do rezerwacji', '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_yachts',
'read_post' => 'yacht_booking_manage_yachts',
'delete_post' => 'yacht_booking_manage_yachts',
'edit_posts' => 'yacht_booking_manage_yachts',
'edit_others_posts' => 'yacht_booking_manage_yachts',
'publish_posts' => 'yacht_booking_manage_yachts',
'read_private_posts' => 'yacht_booking_manage_yachts',
'delete_posts' => 'yacht_booking_manage_yachts',
),
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
);
register_post_type( 'yacht', $args );
}
/**
* Get yacht capacity
*
* @param int $yacht_id Yacht post ID.
* @return int
*/
public static function get_capacity( $yacht_id ) {
return (int) get_post_meta( $yacht_id, '_yacht_capacity', true );
}
/**
* Get yacht price per day
*
* @param int $yacht_id Yacht post ID.
* @return float
*/
public static function get_price_per_day( $yacht_id ) {
return (float) get_post_meta( $yacht_id, '_yacht_price_per_day', true );
}
/**
* Get yacht features
*
* @param int $yacht_id Yacht post ID.
* @return array
*/
public static function get_features( $yacht_id ) {
$features = get_post_meta( $yacht_id, '_yacht_features', true );
return is_array( $features ) ? $features : array();
}
/**
* Update yacht capacity
*
* @param int $yacht_id Yacht post ID.
* @param int $capacity Capacity.
*/
public static function update_capacity( $yacht_id, $capacity ) {
update_post_meta( $yacht_id, '_yacht_capacity', (int) $capacity );
}
/**
* Update yacht price per day
*
* @param int $yacht_id Yacht post ID.
* @param float $price Price per day.
*/
public static function update_price_per_day( $yacht_id, $price ) {
update_post_meta( $yacht_id, '_yacht_price_per_day', (float) $price );
}
/**
* Get yacht alias for Google Calendar global sync.
*
* @param int $yacht_id Yacht post ID.
* @return string
*/
public static function get_gcal_alias( $yacht_id ) {
return (string) get_post_meta( $yacht_id, '_yacht_gcal_alias', true );
}
/**
* Update yacht alias for Google Calendar global sync.
*
* @param int $yacht_id Yacht post ID.
* @param string $alias Alias.
*/
public static function update_gcal_alias( $yacht_id, $alias ) {
update_post_meta( $yacht_id, '_yacht_gcal_alias', sanitize_text_field( $alias ) );
}
/**
* Update yacht features
*
* @param int $yacht_id Yacht post ID.
* @param array $features Features array.
*/
public static function update_features( $yacht_id, $features ) {
update_post_meta( $yacht_id, '_yacht_features', is_array( $features ) ? $features : array() );
}
/**
* Get admin-selected yacht color for the aggregated calendar.
*
* Returns sanitized hex (#rrggbb, lowercase) or '' when not set.
*
* @param int $yacht_id Yacht post ID.
* @return string
*/
public static function get_color( $yacht_id ) {
$value = (string) get_post_meta( $yacht_id, '_yacht_color', true );
if ( '' === $value ) {
return '';
}
return preg_match( '/^#[0-9a-f]{6}$/i', $value ) ? strtolower( $value ) : '';
}
/**
* Update yacht color. Empty value removes the meta (fallback to palette).
*
* @param int $yacht_id Yacht post ID.
* @param string $color Hex color (#rrggbb) or empty string.
*/
public static function update_color( $yacht_id, $color ) {
$color = trim( (string) $color );
if ( '' === $color ) {
delete_post_meta( $yacht_id, '_yacht_color' );
return;
}
if ( preg_match( '/^#[0-9a-f]{6}$/i', $color ) ) {
update_post_meta( $yacht_id, '_yacht_color', strtolower( $color ) );
}
}
}