_x( 'Rooms', 'post type general name', 'wp-bnb' ),
'singular_name' => _x( 'Room', 'post type singular name', 'wp-bnb' ),
'menu_name' => _x( 'Rooms', 'admin menu', 'wp-bnb' ),
'name_admin_bar' => _x( 'Room', 'add new on admin bar', 'wp-bnb' ),
'add_new' => _x( 'Add New', 'room', 'wp-bnb' ),
'add_new_item' => __( 'Add New Room', 'wp-bnb' ),
'new_item' => __( 'New Room', 'wp-bnb' ),
'edit_item' => __( 'Edit Room', 'wp-bnb' ),
'view_item' => __( 'View Room', 'wp-bnb' ),
'all_items' => __( 'Rooms', 'wp-bnb' ),
'search_items' => __( 'Search Rooms', 'wp-bnb' ),
'parent_item_colon' => __( 'Parent Rooms:', 'wp-bnb' ),
'not_found' => __( 'No rooms found.', 'wp-bnb' ),
'not_found_in_trash' => __( 'No rooms found in Trash.', 'wp-bnb' ),
'featured_image' => __( 'Room Image', 'wp-bnb' ),
'set_featured_image' => __( 'Set room image', 'wp-bnb' ),
'remove_featured_image' => __( 'Remove room image', 'wp-bnb' ),
'use_featured_image' => __( 'Use as room image', 'wp-bnb' ),
'archives' => __( 'Room archives', 'wp-bnb' ),
'insert_into_item' => __( 'Insert into room', 'wp-bnb' ),
'uploaded_to_this_item' => __( 'Uploaded to this room', 'wp-bnb' ),
'filter_items_list' => __( 'Filter rooms list', 'wp-bnb' ),
'items_list_navigation' => __( 'Rooms list navigation', 'wp-bnb' ),
'items_list' => __( 'Rooms list', 'wp-bnb' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => 'wp-bnb',
'query_var' => true,
'rewrite' => array(
'slug' => 'room',
'with_front' => false,
),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'menu_icon' => 'dashicons-admin-home',
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt',
'revisions',
),
'show_in_rest' => true,
'rest_base' => 'rooms',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'taxonomies' => array( RoomType::TAXONOMY, Amenity::TAXONOMY ),
);
register_post_type( self::POST_TYPE, $args );
}
/**
* Add meta boxes.
*
* @return void
*/
public static function add_meta_boxes(): void {
add_meta_box(
'bnb_room_building',
__( 'Building', 'wp-bnb' ),
array( self::class, 'render_building_meta_box' ),
self::POST_TYPE,
'side',
'high'
);
add_meta_box(
'bnb_room_details',
__( 'Room Details', 'wp-bnb' ),
array( self::class, 'render_details_meta_box' ),
self::POST_TYPE,
'normal',
'high'
);
add_meta_box(
'bnb_room_gallery',
__( 'Room Gallery', 'wp-bnb' ),
array( self::class, 'render_gallery_meta_box' ),
self::POST_TYPE,
'normal',
'default'
);
}
/**
* Render building selection meta box.
*
* @param \WP_Post $post Current post object.
* @return void
*/
public static function render_building_meta_box( \WP_Post $post ): void {
wp_nonce_field( 'bnb_room_meta', 'bnb_room_meta_nonce' );
$building_id = get_post_meta( $post->ID, self::META_PREFIX . 'building_id', true );
$buildings = get_posts(
array(
'post_type' => Building::POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
)
);
?>
' . esc_html__( 'Add a building', 'wp-bnb' ) . ''
);
?>
ID, self::META_PREFIX . 'room_number', true );
$floor = get_post_meta( $post->ID, self::META_PREFIX . 'floor', true );
$capacity = get_post_meta( $post->ID, self::META_PREFIX . 'capacity', true );
$max_adults = get_post_meta( $post->ID, self::META_PREFIX . 'max_adults', true );
$max_children = get_post_meta( $post->ID, self::META_PREFIX . 'max_children', true );
$size = get_post_meta( $post->ID, self::META_PREFIX . 'size', true );
$beds = get_post_meta( $post->ID, self::META_PREFIX . 'beds', true );
$bathrooms = get_post_meta( $post->ID, self::META_PREFIX . 'bathrooms', true );
$status = get_post_meta( $post->ID, self::META_PREFIX . 'status', true );
?>
ID, self::META_PREFIX . 'gallery', true );
$gallery_ids = $gallery_ids ? explode( ',', $gallery_ids ) : array();
?>
$value ) {
if ( 'taxonomy-bnb_room_type' === $key ) {
continue; // Will add after title.
}
if ( 'taxonomy-bnb_amenity' === $key ) {
continue; // Will add after room type.
}
$new_columns[ $key ] = $value;
if ( 'title' === $key ) {
$new_columns['building'] = __( 'Building', 'wp-bnb' );
$new_columns['room_number'] = __( 'Room #', 'wp-bnb' );
$new_columns['taxonomy-bnb_room_type'] = __( 'Room Type', 'wp-bnb' );
$new_columns['capacity'] = __( 'Capacity', 'wp-bnb' );
$new_columns['status'] = __( 'Status', 'wp-bnb' );
}
}
return $new_columns;
}
/**
* Render custom column content.
*
* @param string $column Column name.
* @param int $post_id Post ID.
* @return void
*/
public static function render_column( string $column, int $post_id ): void {
switch ( $column ) {
case 'building':
$building_id = get_post_meta( $post_id, self::META_PREFIX . 'building_id', true );
if ( $building_id ) {
$building = get_post( $building_id );
if ( $building ) {
printf(
'%s',
esc_url( get_edit_post_link( $building_id ) ),
esc_html( $building->post_title )
);
} else {
echo '—';
}
} else {
echo '—';
}
break;
case 'room_number':
$room_number = get_post_meta( $post_id, self::META_PREFIX . 'room_number', true );
echo esc_html( $room_number ?: '—' );
break;
case 'capacity':
$capacity = get_post_meta( $post_id, self::META_PREFIX . 'capacity', true );
if ( $capacity ) {
printf(
' %s',
esc_html( $capacity )
);
} else {
echo '—';
}
break;
case 'status':
$status = get_post_meta( $post_id, self::META_PREFIX . 'status', true ) ?: 'available';
$statuses = self::get_room_statuses();
$colors = self::get_status_colors();
?>
Building::POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
)
);
if ( empty( $buildings ) ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Filter display only.
$selected = isset( $_GET['building_id'] ) ? absint( $_GET['building_id'] ) : 0;
?>
is_main_query() ) {
return;
}
if ( self::POST_TYPE !== $query->get( 'post_type' ) ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Filter query only.
if ( ! empty( $_GET['building_id'] ) ) {
$query->set(
'meta_query',
array(
array(
'key' => self::META_PREFIX . 'building_id',
'value' => absint( $_GET['building_id'] ),
),
)
);
}
}
/**
* Change title placeholder.
*
* @param string $placeholder Default placeholder.
* @param \WP_Post $post Current post.
* @return string
*/
public static function change_title_placeholder( string $placeholder, \WP_Post $post ): string {
if ( self::POST_TYPE === $post->post_type ) {
return __( 'Enter room name', 'wp-bnb' );
}
return $placeholder;
}
/**
* Get room status options.
*
* @return array
*/
public static function get_room_statuses(): array {
return array(
'available' => __( 'Available', 'wp-bnb' ),
'occupied' => __( 'Occupied', 'wp-bnb' ),
'maintenance' => __( 'Maintenance', 'wp-bnb' ),
'blocked' => __( 'Blocked', 'wp-bnb' ),
);
}
/**
* Get status color codes.
*
* @return array
*/
public static function get_status_colors(): array {
return array(
'available' => '#00a32a',
'occupied' => '#72aee6',
'maintenance' => '#dba617',
'blocked' => '#d63638',
);
}
/**
* Get building for a room.
*
* @param int $room_id Room post ID.
* @return \WP_Post|null
*/
public static function get_building( int $room_id ): ?\WP_Post {
$building_id = get_post_meta( $room_id, self::META_PREFIX . 'building_id', true );
if ( ! $building_id ) {
return null;
}
return get_post( $building_id );
}
/**
* Get rooms for a building.
*
* @param int $building_id Building post ID.
* @return array<\WP_Post>
*/
public static function get_rooms_for_building( int $building_id ): array {
return get_posts(
array(
'post_type' => self::POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => self::META_PREFIX . 'building_id',
'value' => $building_id,
),
),
'orderby' => 'meta_value',
'meta_key' => self::META_PREFIX . 'room_number',
'order' => 'ASC',
)
);
}
}