_x( 'Buildings', 'post type general name', 'wp-bnb' ), 'singular_name' => _x( 'Building', 'post type singular name', 'wp-bnb' ), 'menu_name' => _x( 'Buildings', 'admin menu', 'wp-bnb' ), 'name_admin_bar' => _x( 'Building', 'add new on admin bar', 'wp-bnb' ), 'add_new' => _x( 'Add New', 'building', 'wp-bnb' ), 'add_new_item' => __( 'Add New Building', 'wp-bnb' ), 'new_item' => __( 'New Building', 'wp-bnb' ), 'edit_item' => __( 'Edit Building', 'wp-bnb' ), 'view_item' => __( 'View Building', 'wp-bnb' ), 'all_items' => __( 'Buildings', 'wp-bnb' ), 'search_items' => __( 'Search Buildings', 'wp-bnb' ), 'parent_item_colon' => __( 'Parent Buildings:', 'wp-bnb' ), 'not_found' => __( 'No buildings found.', 'wp-bnb' ), 'not_found_in_trash' => __( 'No buildings found in Trash.', 'wp-bnb' ), 'featured_image' => __( 'Building Image', 'wp-bnb' ), 'set_featured_image' => __( 'Set building image', 'wp-bnb' ), 'remove_featured_image' => __( 'Remove building image', 'wp-bnb' ), 'use_featured_image' => __( 'Use as building image', 'wp-bnb' ), 'archives' => __( 'Building archives', 'wp-bnb' ), 'insert_into_item' => __( 'Insert into building', 'wp-bnb' ), 'uploaded_to_this_item' => __( 'Uploaded to this building', 'wp-bnb' ), 'filter_items_list' => __( 'Filter buildings list', 'wp-bnb' ), 'items_list_navigation' => __( 'Buildings list navigation', 'wp-bnb' ), 'items_list' => __( 'Buildings 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' => 'building', 'with_front' => false, ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'menu_icon' => 'dashicons-building', 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions', ), 'show_in_rest' => true, 'rest_base' => 'buildings', 'rest_controller_class' => 'WP_REST_Posts_Controller', ); register_post_type( self::POST_TYPE, $args ); } /** * Add meta boxes. * * @return void */ public static function add_meta_boxes(): void { add_meta_box( 'bnb_building_address', __( 'Address', 'wp-bnb' ), array( self::class, 'render_address_meta_box' ), self::POST_TYPE, 'normal', 'high' ); add_meta_box( 'bnb_building_contact', __( 'Contact Information', 'wp-bnb' ), array( self::class, 'render_contact_meta_box' ), self::POST_TYPE, 'normal', 'high' ); add_meta_box( 'bnb_building_details', __( 'Building Details', 'wp-bnb' ), array( self::class, 'render_details_meta_box' ), self::POST_TYPE, 'side', 'default' ); } /** * Render address meta box. * * @param \WP_Post $post Current post object. * @return void */ public static function render_address_meta_box( \WP_Post $post ): void { wp_nonce_field( 'bnb_building_meta', 'bnb_building_meta_nonce' ); $street = get_post_meta( $post->ID, self::META_PREFIX . 'street', true ); $street2 = get_post_meta( $post->ID, self::META_PREFIX . 'street2', true ); $city = get_post_meta( $post->ID, self::META_PREFIX . 'city', true ); $state = get_post_meta( $post->ID, self::META_PREFIX . 'state', true ); $zip = get_post_meta( $post->ID, self::META_PREFIX . 'zip', true ); $country = get_post_meta( $post->ID, self::META_PREFIX . 'country', true ); ?>
$value ) { $new_columns[ $key ] = $value; if ( 'title' === $key ) { $new_columns['city'] = __( 'City', 'wp-bnb' ); $new_columns['country'] = __( 'Country', 'wp-bnb' ); $new_columns['rooms'] = __( 'Rooms', '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 'city': $city = get_post_meta( $post_id, self::META_PREFIX . 'city', true ); echo esc_html( $city ?: '—' ); break; case 'country': $country = get_post_meta( $post_id, self::META_PREFIX . 'country', true ); if ( $country ) { $countries = self::get_countries(); echo esc_html( $countries[ $country ] ?? $country ); } else { echo '—'; } break; case 'rooms': $rooms = get_posts( array( 'post_type' => 'bnb_room', 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => '_bnb_room_building_id', 'value' => $post_id, ), ), ) ); $count = count( $rooms ); if ( $count > 0 ) { printf( '%s', esc_url( admin_url( 'edit.php?post_type=bnb_room&building_id=' . $post_id ) ), esc_html( sprintf( /* translators: %d: Number of rooms */ _n( '%d room', '%d rooms', $count, 'wp-bnb' ), $count ) ) ); } else { echo '—'; } break; } } /** * Add sortable columns. * * @param array $columns Existing sortable columns. * @return array */ public static function sortable_columns( array $columns ): array { $columns['city'] = 'city'; $columns['country'] = 'country'; return $columns; } /** * 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 building name', 'wp-bnb' ); } return $placeholder; } /** * Get list of countries. * * @return array