Files
wp-bnb/src/Taxonomies/RoomType.php

225 lines
6.8 KiB
PHP
Raw Normal View History

<?php
/**
* Room Type taxonomy.
*
* Hierarchical taxonomy for room types like Standard, Suite, Family, etc.
*
* @package Magdev\WpBnb\Taxonomies
*/
declare( strict_types=1 );
namespace Magdev\WpBnb\Taxonomies;
/**
* Room Type taxonomy class.
*/
final class RoomType {
/**
* Taxonomy slug.
*
* @var string
*/
public const TAXONOMY = 'bnb_room_type';
/**
* Initialize the taxonomy.
*
* @return void
*/
public static function init(): void {
add_action( 'init', array( self::class, 'register' ) );
add_action( 'bnb_room_type_add_form_fields', array( self::class, 'add_form_fields' ) );
add_action( 'bnb_room_type_edit_form_fields', array( self::class, 'edit_form_fields' ), 10, 2 );
add_action( 'created_bnb_room_type', array( self::class, 'save_term_meta' ), 10, 2 );
add_action( 'edited_bnb_room_type', array( self::class, 'save_term_meta' ), 10, 2 );
}
/**
* Register the taxonomy.
*
* @return void
*/
public static function register(): void {
$labels = array(
'name' => _x( 'Room Types', 'taxonomy general name', 'wp-bnb' ),
'singular_name' => _x( 'Room Type', 'taxonomy singular name', 'wp-bnb' ),
'search_items' => __( 'Search Room Types', 'wp-bnb' ),
'all_items' => __( 'All Room Types', 'wp-bnb' ),
'parent_item' => __( 'Parent Room Type', 'wp-bnb' ),
'parent_item_colon' => __( 'Parent Room Type:', 'wp-bnb' ),
'edit_item' => __( 'Edit Room Type', 'wp-bnb' ),
'update_item' => __( 'Update Room Type', 'wp-bnb' ),
'add_new_item' => __( 'Add New Room Type', 'wp-bnb' ),
'new_item_name' => __( 'New Room Type Name', 'wp-bnb' ),
'menu_name' => __( 'Room Types', 'wp-bnb' ),
'back_to_items' => __( '&larr; Back to Room Types', 'wp-bnb' ),
'not_found' => __( 'No room types found.', 'wp-bnb' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true, // Hierarchical (like categories).
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'show_in_quick_edit' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'room-type',
'with_front' => false,
'hierarchical' => true,
),
'query_var' => true,
'capabilities' => array(
'manage_terms' => 'manage_options',
'edit_terms' => 'manage_options',
'delete_terms' => 'manage_options',
'assign_terms' => 'edit_posts',
),
);
register_taxonomy( self::TAXONOMY, array( 'bnb_room' ), $args );
}
/**
* Add custom fields to the add term form.
*
* @return void
*/
public static function add_form_fields(): void {
?>
<div class="form-field term-base-capacity-wrap">
<label for="room-type-base-capacity"><?php esc_html_e( 'Base Capacity', 'wp-bnb' ); ?></label>
<input type="number" name="room_type_base_capacity" id="room-type-base-capacity" value="2" min="1" max="20">
<p><?php esc_html_e( 'Default number of guests this room type typically accommodates.', 'wp-bnb' ); ?></p>
</div>
<div class="form-field term-sort-order-wrap">
<label for="room-type-sort-order"><?php esc_html_e( 'Sort Order', 'wp-bnb' ); ?></label>
<input type="number" name="room_type_sort_order" id="room-type-sort-order" value="0" min="0">
<p><?php esc_html_e( 'Display order for this room type (lower numbers appear first).', 'wp-bnb' ); ?></p>
</div>
<?php
}
/**
* Add custom fields to the edit term form.
*
* @param \WP_Term $term Current term object.
* @param string $taxonomy Current taxonomy slug.
* @return void
*/
public static function edit_form_fields( \WP_Term $term, string $taxonomy ): void {
$base_capacity = get_term_meta( $term->term_id, 'room_type_base_capacity', true );
$sort_order = get_term_meta( $term->term_id, 'room_type_sort_order', true );
?>
<tr class="form-field term-base-capacity-wrap">
<th scope="row">
<label for="room-type-base-capacity"><?php esc_html_e( 'Base Capacity', 'wp-bnb' ); ?></label>
</th>
<td>
<input type="number" name="room_type_base_capacity" id="room-type-base-capacity"
value="<?php echo esc_attr( $base_capacity ?: '2' ); ?>" min="1" max="20">
<p class="description"><?php esc_html_e( 'Default number of guests this room type typically accommodates.', 'wp-bnb' ); ?></p>
</td>
</tr>
<tr class="form-field term-sort-order-wrap">
<th scope="row">
<label for="room-type-sort-order"><?php esc_html_e( 'Sort Order', 'wp-bnb' ); ?></label>
</th>
<td>
<input type="number" name="room_type_sort_order" id="room-type-sort-order"
value="<?php echo esc_attr( $sort_order ?: '0' ); ?>" min="0">
<p class="description"><?php esc_html_e( 'Display order for this room type (lower numbers appear first).', 'wp-bnb' ); ?></p>
</td>
</tr>
<?php
}
/**
* Save term meta data.
*
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @return void
*/
public static function save_term_meta( int $term_id, int $tt_id ): void {
if ( isset( $_POST['room_type_base_capacity'] ) ) {
update_term_meta(
$term_id,
'room_type_base_capacity',
absint( $_POST['room_type_base_capacity'] )
);
}
if ( isset( $_POST['room_type_sort_order'] ) ) {
update_term_meta(
$term_id,
'room_type_sort_order',
absint( $_POST['room_type_sort_order'] )
);
}
}
/**
* Get default room types to seed on activation.
*
* @return array<string, array{capacity: int, order: int, children?: array<string, array{capacity: int, order: int}>}>
*/
public static function get_default_terms(): array {
return array(
__( 'Standard', 'wp-bnb' ) => array(
'capacity' => 2,
'order' => 10,
'children' => array(
__( 'Single', 'wp-bnb' ) => array(
'capacity' => 1,
'order' => 11,
),
__( 'Double', 'wp-bnb' ) => array(
'capacity' => 2,
'order' => 12,
),
__( 'Twin', 'wp-bnb' ) => array(
'capacity' => 2,
'order' => 13,
),
),
),
__( 'Superior', 'wp-bnb' ) => array(
'capacity' => 2,
'order' => 20,
),
__( 'Suite', 'wp-bnb' ) => array(
'capacity' => 2,
'order' => 30,
'children' => array(
__( 'Junior Suite', 'wp-bnb' ) => array(
'capacity' => 2,
'order' => 31,
),
__( 'Executive Suite', 'wp-bnb' ) => array(
'capacity' => 2,
'order' => 32,
),
),
),
__( 'Family', 'wp-bnb' ) => array(
'capacity' => 4,
'order' => 40,
),
__( 'Accessible', 'wp-bnb' ) => array(
'capacity' => 2,
'order' => 50,
),
__( 'Apartment', 'wp-bnb' ) => array(
'capacity' => 4,
'order' => 60,
),
);
}
}