first commit
This commit is contained in:
@@ -0,0 +1,259 @@
|
||||
<?php
|
||||
/**
|
||||
* Yacht 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';
|
||||
}
|
||||
|
||||
/**
|
||||
* Yacht List Table class
|
||||
*/
|
||||
class Yacht_List_Table extends \WP_List_Table {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
array(
|
||||
'singular' => 'yacht',
|
||||
'plural' => 'yachts',
|
||||
'ajax' => false,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get columns
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_columns() {
|
||||
return array(
|
||||
'cb' => '<input type="checkbox" />',
|
||||
'title' => __( 'Nazwa jachtu', 'yacht-booking' ),
|
||||
'gcal' => __( 'Google Calendar', 'yacht-booking' ),
|
||||
'bookings' => __( 'Rezerwacje', 'yacht-booking' ),
|
||||
'date' => __( 'Data utworzenia', 'yacht-booking' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sortable columns
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_sortable_columns() {
|
||||
return array(
|
||||
'title' => array( 'title', false ),
|
||||
'date' => array( 'date', true ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bulk actions
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_bulk_actions() {
|
||||
return array(
|
||||
'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'] ) ) : 'title';
|
||||
$order = isset( $_GET['order'] ) ? sanitize_text_field( wp_unslash( $_GET['order'] ) ) : 'ASC';
|
||||
$search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '';
|
||||
|
||||
$args = array(
|
||||
'post_type' => 'yacht',
|
||||
'posts_per_page' => $per_page,
|
||||
'paged' => $current_page,
|
||||
'orderby' => $orderby,
|
||||
'order' => $order,
|
||||
'post_status' => 'publish',
|
||||
);
|
||||
|
||||
if ( ! empty( $search ) ) {
|
||||
$args['s'] = $search;
|
||||
}
|
||||
|
||||
$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="yacht[]" value="%d" />',
|
||||
$item->ID
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Column title
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_title( $item ) {
|
||||
$edit_url = admin_url( 'admin.php?page=yacht-bookings-add-yacht&yacht_id=' . $item->ID );
|
||||
$delete_url = wp_nonce_url(
|
||||
admin_url( 'admin.php?page=yacht-bookings&action=delete&yacht=' . $item->ID ),
|
||||
'delete_yacht_' . $item->ID
|
||||
);
|
||||
|
||||
$actions = array(
|
||||
'edit' => sprintf(
|
||||
'<a href="%s">%s</a>',
|
||||
esc_url( $edit_url ),
|
||||
__( 'Edytuj', 'yacht-booking' )
|
||||
),
|
||||
'delete' => sprintf(
|
||||
'<a href="%s" class="submitdelete" onclick="return confirm(\'%s\')">%s</a>',
|
||||
esc_url( $delete_url ),
|
||||
esc_js( __( 'Czy na pewno chcesz usunąć ten jacht? Spowoduje to również usunięcie wszystkich powiązanych rezerwacji.', 'yacht-booking' ) ),
|
||||
__( 'Usuń', 'yacht-booking' )
|
||||
),
|
||||
);
|
||||
|
||||
return sprintf(
|
||||
'<strong><a href="%s" class="row-title">%s</a></strong>%s',
|
||||
esc_url( $edit_url ),
|
||||
esc_html( $item->post_title ),
|
||||
$this->row_actions( $actions )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Column Google Calendar
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_gcal( $item ) {
|
||||
$gcal_id = Yacht::get_gcal_id( $item->ID );
|
||||
|
||||
if ( $gcal_id ) {
|
||||
return sprintf(
|
||||
'<span class="gcal-status connected"><span class="dashicons dashicons-yes-alt"></span> %s</span>',
|
||||
esc_html__( 'Połączony', 'yacht-booking' )
|
||||
);
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'<span class="gcal-status disconnected"><span class="dashicons dashicons-dismiss"></span> %s</span>',
|
||||
esc_html__( 'Niepołączony', 'yacht-booking' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Column bookings count
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_bookings( $item ) {
|
||||
$count = $this->count_bookings( $item->ID );
|
||||
|
||||
if ( $count > 0 ) {
|
||||
return sprintf(
|
||||
'<a href="%s">%d</a>',
|
||||
esc_url( admin_url( 'admin.php?page=yacht-bookings-list&yacht_filter=' . $item->ID ) ),
|
||||
$count
|
||||
);
|
||||
}
|
||||
|
||||
return '0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Column date
|
||||
*
|
||||
* @param object $item Item.
|
||||
* @return string
|
||||
*/
|
||||
public function column_date( $item ) {
|
||||
return get_the_date( Settings::get_date_format() . ' 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 '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Count bookings for yacht
|
||||
*
|
||||
* @param int $yacht_id Yacht ID.
|
||||
* @return int
|
||||
*/
|
||||
private function count_bookings( $yacht_id ) {
|
||||
$query = new \WP_Query(
|
||||
array(
|
||||
'post_type' => 'yacht_booking',
|
||||
'posts_per_page' => -1,
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => '_booking_yacht_id',
|
||||
'value' => $yacht_id,
|
||||
),
|
||||
),
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
return $query->found_posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Message when no items
|
||||
*/
|
||||
public function no_items() {
|
||||
esc_html_e( 'Brak jachtów. Dodaj pierwszy jacht!', 'yacht-booking' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user