_x( 'Services', 'post type general name', 'wp-bnb' ), 'singular_name' => _x( 'Service', 'post type singular name', 'wp-bnb' ), 'menu_name' => _x( 'Services', 'admin menu', 'wp-bnb' ), 'name_admin_bar' => _x( 'Service', 'add new on admin bar', 'wp-bnb' ), 'add_new' => _x( 'Add New', 'service', 'wp-bnb' ), 'add_new_item' => __( 'Add New Service', 'wp-bnb' ), 'new_item' => __( 'New Service', 'wp-bnb' ), 'edit_item' => __( 'Edit Service', 'wp-bnb' ), 'view_item' => __( 'View Service', 'wp-bnb' ), 'all_items' => __( 'Services', 'wp-bnb' ), 'search_items' => __( 'Search Services', 'wp-bnb' ), 'parent_item_colon' => __( 'Parent Services:', 'wp-bnb' ), 'not_found' => __( 'No services found.', 'wp-bnb' ), 'not_found_in_trash' => __( 'No services found in Trash.', 'wp-bnb' ), 'archives' => __( 'Service archives', 'wp-bnb' ), 'insert_into_item' => __( 'Insert into service', 'wp-bnb' ), 'uploaded_to_this_item' => __( 'Uploaded to this service', 'wp-bnb' ), 'filter_items_list' => __( 'Filter services list', 'wp-bnb' ), 'items_list_navigation' => __( 'Services list navigation', 'wp-bnb' ), 'items_list' => __( 'Services list', 'wp-bnb' ), ); $args = array( 'labels' => $labels, 'public' => false, 'publicly_queryable' => false, 'show_ui' => true, 'show_in_menu' => 'wp-bnb', 'query_var' => false, 'capability_type' => 'post', 'has_archive' => false, 'hierarchical' => false, 'menu_position' => null, 'menu_icon' => 'dashicons-plus-alt', 'supports' => array( 'title', 'editor', 'thumbnail' ), 'show_in_rest' => true, 'rest_base' => 'services', '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_service_pricing', __( 'Pricing', 'wp-bnb' ), array( self::class, 'render_pricing_meta_box' ), self::POST_TYPE, 'normal', 'high' ); add_meta_box( 'bnb_service_settings', __( 'Service Settings', 'wp-bnb' ), array( self::class, 'render_settings_meta_box' ), self::POST_TYPE, 'side', 'default' ); } /** * Render pricing meta box. * * @param \WP_Post $post Current post object. * @return void */ public static function render_pricing_meta_box( \WP_Post $post ): void { wp_nonce_field( 'bnb_service_meta', 'bnb_service_meta_nonce' ); $pricing_type = get_post_meta( $post->ID, self::META_PREFIX . 'pricing_type', true ) ?: 'per_booking'; $price = get_post_meta( $post->ID, self::META_PREFIX . 'price', true ); $currency = get_option( 'wp_bnb_currency', 'CHF' ); ?>
|
|
$value ) { $new_columns[ $key ] = $value; if ( 'title' === $key ) { $new_columns['pricing_type'] = __( 'Pricing Type', 'wp-bnb' ); $new_columns['price'] = __( 'Price', 'wp-bnb' ); $new_columns['service_status'] = __( 'Status', 'wp-bnb' ); } } // Remove date column. unset( $new_columns['date'] ); 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 'pricing_type': $pricing_type = get_post_meta( $post_id, self::META_PREFIX . 'pricing_type', true ) ?: 'per_booking'; $labels = self::get_pricing_type_labels(); $icons = array( 'included' => 'yes-alt', 'per_booking' => 'tag', 'per_night' => 'calendar-alt', ); $colors = array( 'included' => '#00a32a', 'per_booking' => '#135e96', 'per_night' => '#dba617', ); echo ''; echo esc_html( $labels[ $pricing_type ] ?? $pricing_type ); break; case 'price': $pricing_type = get_post_meta( $post_id, self::META_PREFIX . 'pricing_type', true ) ?: 'per_booking'; if ( 'included' === $pricing_type ) { echo '' . esc_html__( 'Included', 'wp-bnb' ) . ''; } else { $price = get_post_meta( $post_id, self::META_PREFIX . 'price', true ); if ( $price ) { echo esc_html( Calculator::formatPrice( (float) $price ) ); if ( 'per_night' === $pricing_type ) { echo ' ' . esc_html__( '/ night', 'wp-bnb' ) . ''; } } else { echo '' . esc_html__( 'Not set', 'wp-bnb' ) . ''; } } break; case 'service_status': $status = get_post_meta( $post_id, self::META_PREFIX . 'status', true ) ?: 'active'; $classes = array( 'active' => 'bnb-service-status-active', 'inactive' => 'bnb-service-status-inactive', ); $labels = array( 'active' => __( 'Active', 'wp-bnb' ), 'inactive' => __( 'Inactive', 'wp-bnb' ), ); echo ''; echo esc_html( $labels[ $status ] ?? $status ); echo ''; break; } } /** * Add sortable columns. * * @param array $columns Existing sortable columns. * @return array */ public static function sortable_columns( array $columns ): array { $columns['price'] = 'price'; $columns['service_status'] = 'status'; return $columns; } /** * Add filter dropdowns to admin list. * * @param string $post_type Current post type. * @return void */ public static function add_filters( string $post_type ): void { if ( self::POST_TYPE !== $post_type ) { return; } // Status filter. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Filter display only. $selected_status = isset( $_GET['service_status'] ) ? sanitize_text_field( wp_unslash( $_GET['service_status'] ) ) : ''; ?> is_main_query() ) { return; } if ( self::POST_TYPE !== $query->get( 'post_type' ) ) { return; } $meta_query = array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Filter query only. if ( ! empty( $_GET['service_status'] ) ) { $meta_query[] = array( 'key' => self::META_PREFIX . 'status', 'value' => sanitize_text_field( wp_unslash( $_GET['service_status'] ) ), ); } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Filter query only. if ( ! empty( $_GET['pricing_type'] ) ) { $meta_query[] = array( 'key' => self::META_PREFIX . 'pricing_type', 'value' => sanitize_text_field( wp_unslash( $_GET['pricing_type'] ) ), ); } if ( ! empty( $meta_query ) ) { $meta_query['relation'] = 'AND'; $query->set( 'meta_query', $meta_query ); } // Handle sorting. $orderby = $query->get( 'orderby' ); if ( 'price' === $orderby ) { $query->set( 'meta_key', self::META_PREFIX . 'price' ); $query->set( 'orderby', 'meta_value_num' ); } elseif ( 'status' === $orderby ) { $query->set( 'meta_key', self::META_PREFIX . 'status' ); $query->set( 'orderby', 'meta_value' ); } } /** * 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 __( 'Service name', 'wp-bnb' ); } return $placeholder; } /** * Get pricing type labels. * * @return array