first commit
This commit is contained in:
@@ -0,0 +1,414 @@
|
||||
<?php
|
||||
/**
|
||||
* Booking List Table
|
||||
*
|
||||
* @package YachtBooking
|
||||
*/
|
||||
|
||||
namespace YachtBooking;
|
||||
|
||||
// Exit if accessed directly
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// Load WP_List_Table if not loaded
|
||||
if ( ! class_exists( 'WP_List_Table' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Booking List Table class
|
||||
*/
|
||||
class Booking_List_Table extends \WP_List_Table {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
array(
|
||||
'singular' => 'booking',
|
||||
'plural' => 'bookings',
|
||||
'ajax' => false,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get columns
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_columns() {
|
||||
return array(
|
||||
'cb' => '<input type="checkbox" />',
|
||||
'id' => __( 'ID', 'yacht-booking' ),
|
||||
'yacht' => __( 'Jacht', 'yacht-booking' ),
|
||||
'customer' => __( 'Klient', 'yacht-booking' ),
|
||||
'dates' => __( 'Termin', 'yacht-booking' ),
|
||||
'status' => __( 'Status', 'yacht-booking' ),
|
||||
'total_price' => __( 'Cena', 'yacht-booking' ),
|
||||
'date_created' => __( 'Data utworzenia', 'yacht-booking' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sortable columns
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_sortable_columns() {
|
||||
return array(
|
||||
'id' => array( 'ID', true ),
|
||||
'status' => array( 'status', false ),
|
||||
'total_price' => array( 'total_price', false ),
|
||||
'date_created' => array( 'date', true ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bulk actions
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_bulk_actions() {
|
||||
return array(
|
||||
'approve' => __( 'Zatwierdź', 'yacht-booking' ),
|
||||
'cancel' => __( 'Anuluj', 'yacht-booking' ),
|
||||
'delete' => __( 'Usuń', 'yacht-booking' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare items for display
|
||||
*/
|
||||
public function prepare_items() {
|
||||
$per_page = 20;
|
||||
$current_page = $this->get_pagenum();
|
||||
$orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : 'date';
|
||||
$order = isset( $_GET['order'] ) ? sanitize_text_field( wp_unslash( $_GET['order'] ) ) : 'DESC';
|
||||
$search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '';
|
||||
|
||||
$args = array(
|
||||
'post_type' => 'yacht_booking',
|
||||
'posts_per_page' => $per_page,
|
||||
'paged' => $current_page,
|
||||
'orderby' => $orderby,
|
||||
'order' => $order,
|
||||
'post_status' => 'publish',
|
||||
);
|
||||
|
||||
// Search
|
||||
if ( ! empty( $search ) ) {
|
||||
$args['s'] = $search;
|
||||
}
|
||||
|
||||
// Filter by status
|
||||
if ( ! empty( $_GET['status_filter'] ) && 'all' !== $_GET['status_filter'] ) {
|
||||
$args['meta_query'] = array(
|
||||
array(
|
||||
'key' => '_booking_status',
|
||||
'value' => sanitize_text_field( wp_unslash( $_GET['status_filter'] ) ),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Filter by yacht
|
||||
if ( ! empty( $_GET['yacht_filter'] ) && 'all' !== $_GET['yacht_filter'] ) {
|
||||
if ( ! isset( $args['meta_query'] ) ) {
|
||||
$args['meta_query'] = array();
|
||||
}
|
||||
$args['meta_query'][] = array(
|
||||
'key' => '_booking_yacht_id',
|
||||
'value' => (int) $_GET['yacht_filter'],
|
||||
);
|
||||
}
|
||||
|
||||
$query = new \WP_Query( $args );
|
||||
|
||||
$this->items = $query->posts;
|
||||
|
||||
$this->set_pagination_args(
|
||||
array(
|
||||
'total_items' => $query->found_posts,
|
||||
'per_page' => $per_page,
|
||||
'total_pages' => $query->max_num_pages,
|
||||
)
|
||||
);
|
||||
|
||||
$columns = $this->get_columns();
|
||||
$hidden = array();
|
||||
$sortable = $this->get_sortable_columns();
|
||||
|
||||
$this->_column_headers = array( $columns, $hidden, $sortable );
|
||||
}
|
||||
|
||||
/**
|
||||
* Column checkbox
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_cb( $item ) {
|
||||
return sprintf(
|
||||
'<input type="checkbox" name="booking[]" value="%d" />',
|
||||
$item->ID
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Column ID
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_id( $item ) {
|
||||
$actions = array();
|
||||
|
||||
$status = Booking::get_status( $item->ID );
|
||||
|
||||
// Approve action (for pending only)
|
||||
if ( 'pending' === $status ) {
|
||||
$approve_url = wp_nonce_url(
|
||||
admin_url( 'admin.php?page=yacht-bookings-list&action=approve&booking=' . $item->ID ),
|
||||
'approve_booking_' . $item->ID
|
||||
);
|
||||
$actions['approve-booking'] = sprintf(
|
||||
'<a href="%s" style="color: #28a745;">%s</a>',
|
||||
esc_url( $approve_url ),
|
||||
__( 'Zatwierdź', 'yacht-booking' )
|
||||
);
|
||||
}
|
||||
|
||||
// Cancel action (for pending and confirmed)
|
||||
if ( in_array( $status, array( 'pending', 'confirmed' ), true ) ) {
|
||||
$cancel_url = wp_nonce_url(
|
||||
admin_url( 'admin.php?page=yacht-bookings-list&action=cancel&booking=' . $item->ID ),
|
||||
'cancel_booking_' . $item->ID
|
||||
);
|
||||
$actions['cancel'] = sprintf(
|
||||
'<a href="%s" style="color: #856404;" onclick="return confirm(\'%s\')">%s</a>',
|
||||
esc_url( $cancel_url ),
|
||||
esc_js( __( 'Czy na pewno chcesz anulować tę rezerwację?', 'yacht-booking' ) ),
|
||||
__( 'Anuluj', 'yacht-booking' )
|
||||
);
|
||||
}
|
||||
|
||||
// Delete action (always visible)
|
||||
$delete_url = wp_nonce_url(
|
||||
admin_url( 'admin.php?page=yacht-bookings-list&action=delete&booking=' . $item->ID ),
|
||||
'delete_booking_' . $item->ID
|
||||
);
|
||||
$actions['delete'] = sprintf(
|
||||
'<a href="%s" class="submitdelete" onclick="return confirm(\'%s\')">%s</a>',
|
||||
esc_url( $delete_url ),
|
||||
esc_js( __( 'Czy na pewno chcesz usunąć tę rezerwację? Tej operacji nie można cofnąć.', 'yacht-booking' ) ),
|
||||
__( 'Usuń', 'yacht-booking' )
|
||||
);
|
||||
|
||||
return sprintf(
|
||||
'<strong>#%d</strong>%s',
|
||||
$item->ID,
|
||||
$this->row_actions( $actions )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Column yacht
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_yacht( $item ) {
|
||||
$yacht_id = Booking::get_yacht_id( $item->ID );
|
||||
$yacht = get_post( $yacht_id );
|
||||
|
||||
if ( $yacht ) {
|
||||
return sprintf(
|
||||
'<a href="%s">%s</a>',
|
||||
esc_url( admin_url( 'admin.php?page=yacht-bookings-add-yacht&yacht_id=' . $yacht_id ) ),
|
||||
esc_html( $yacht->post_title )
|
||||
);
|
||||
}
|
||||
|
||||
return '—';
|
||||
}
|
||||
|
||||
/**
|
||||
* Column customer
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_customer( $item ) {
|
||||
$customer_name = Booking::get_customer_name( $item->ID );
|
||||
$customer_email = Booking::get_customer_email( $item->ID );
|
||||
$customer_phone = Booking::get_customer_phone( $item->ID );
|
||||
|
||||
return sprintf(
|
||||
'<strong>%s</strong><br><small>%s<br>%s</small>',
|
||||
esc_html( $customer_name ),
|
||||
esc_html( $customer_email ),
|
||||
esc_html( $customer_phone )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Column dates
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_dates( $item ) {
|
||||
$start_date = Booking::get_start_date( $item->ID );
|
||||
$end_date = Booking::get_end_date( $item->ID );
|
||||
$days = Availability::count_days( $start_date, $end_date );
|
||||
|
||||
return sprintf(
|
||||
'<strong>%s</strong><br><small>do %s (%d %s)</small>',
|
||||
esc_html( Settings::format_date( $start_date ) ),
|
||||
esc_html( Settings::format_date( $end_date ) ),
|
||||
$days,
|
||||
_n( 'dzień', 'dni', $days, 'yacht-booking' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Column status
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_status( $item ) {
|
||||
$status = Booking::get_status( $item->ID );
|
||||
|
||||
$status_labels = array(
|
||||
'pending' => __( 'Oczekująca', 'yacht-booking' ),
|
||||
'confirmed' => __( 'Potwierdzona', 'yacht-booking' ),
|
||||
'cancelled' => __( 'Anulowana', 'yacht-booking' ),
|
||||
);
|
||||
|
||||
$status_label = isset( $status_labels[ $status ] ) ? $status_labels[ $status ] : $status;
|
||||
|
||||
return sprintf(
|
||||
'<span class="booking-status %s">%s</span>',
|
||||
esc_attr( $status ),
|
||||
esc_html( $status_label )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Column total price
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_total_price( $item ) {
|
||||
$total_price = Booking::get_total_price( $item->ID );
|
||||
|
||||
if ( $total_price > 0 ) {
|
||||
return sprintf(
|
||||
'<strong>%s</strong>',
|
||||
esc_html( Settings::format_price( $total_price ) )
|
||||
);
|
||||
}
|
||||
|
||||
return '—';
|
||||
}
|
||||
|
||||
/**
|
||||
* Column date created
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_date_created( $item ) {
|
||||
$date_format = Settings::get_date_format();
|
||||
|
||||
return sprintf(
|
||||
'%s<br><small>%s</small>',
|
||||
get_the_date( $date_format, $item ),
|
||||
get_the_time( 'H:i', $item )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default column
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @param string $column_name Column name.
|
||||
* @return string
|
||||
*/
|
||||
public function column_default( $item, $column_name ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Display filters above the table
|
||||
*/
|
||||
protected function extra_tablenav( $which ) {
|
||||
if ( 'top' !== $which ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$current_status = isset( $_GET['status_filter'] ) ? sanitize_text_field( wp_unslash( $_GET['status_filter'] ) ) : 'all';
|
||||
$current_yacht = isset( $_GET['yacht_filter'] ) ? (int) $_GET['yacht_filter'] : 'all';
|
||||
|
||||
?>
|
||||
<div class="alignleft actions">
|
||||
<!-- Status filter -->
|
||||
<select name="status_filter">
|
||||
<option value="all" <?php selected( $current_status, 'all' ); ?>>
|
||||
<?php esc_html_e( 'Wszystkie statusy', 'yacht-booking' ); ?>
|
||||
</option>
|
||||
<option value="pending" <?php selected( $current_status, 'pending' ); ?>>
|
||||
<?php esc_html_e( 'Oczekujące', 'yacht-booking' ); ?>
|
||||
</option>
|
||||
<option value="confirmed" <?php selected( $current_status, 'confirmed' ); ?>>
|
||||
<?php esc_html_e( 'Potwierdzone', 'yacht-booking' ); ?>
|
||||
</option>
|
||||
<option value="cancelled" <?php selected( $current_status, 'cancelled' ); ?>>
|
||||
<?php esc_html_e( 'Anulowane', 'yacht-booking' ); ?>
|
||||
</option>
|
||||
</select>
|
||||
|
||||
<!-- Yacht filter -->
|
||||
<select name="yacht_filter">
|
||||
<option value="all" <?php selected( $current_yacht, 'all' ); ?>>
|
||||
<?php esc_html_e( 'Wszystkie jachty', 'yacht-booking' ); ?>
|
||||
</option>
|
||||
<?php
|
||||
$yachts = get_posts(
|
||||
array(
|
||||
'post_type' => 'yacht',
|
||||
'posts_per_page' => -1,
|
||||
'orderby' => 'title',
|
||||
'order' => 'ASC',
|
||||
)
|
||||
);
|
||||
|
||||
foreach ( $yachts as $yacht ) {
|
||||
printf(
|
||||
'<option value="%d" %s>%s</option>',
|
||||
$yacht->ID,
|
||||
selected( $current_yacht, $yacht->ID, false ),
|
||||
esc_html( $yacht->post_title )
|
||||
);
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
|
||||
<input type="submit" class="button" value="<?php esc_attr_e( 'Filtruj', 'yacht-booking' ); ?>">
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Message when no items
|
||||
*/
|
||||
public function no_items() {
|
||||
esc_html_e( 'Brak rezerwacji.', 'yacht-booking' );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user