316 lines
7.2 KiB
PHP
316 lines
7.2 KiB
PHP
<?php
|
|
/**
|
|
* Inquiry List Table
|
|
*
|
|
* @package YachtBooking
|
|
*/
|
|
|
|
namespace YachtBooking;
|
|
|
|
// Exit if accessed directly.
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
if ( ! class_exists( 'WP_List_Table' ) ) {
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
|
|
}
|
|
|
|
/**
|
|
* Inquiry List Table class
|
|
*/
|
|
class Inquiry_List_Table extends \WP_List_Table {
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct() {
|
|
parent::__construct(
|
|
array(
|
|
'singular' => 'inquiry',
|
|
'plural' => 'inquiries',
|
|
'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' => __( 'Preferowane terminy', 'yacht-booking' ),
|
|
'message' => __( 'Wiadomość', 'yacht-booking' ),
|
|
'emails' => __( 'Emaile', 'yacht-booking' ),
|
|
'date_created' => __( 'Data wysłania', 'yacht-booking' ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get sortable columns
|
|
*
|
|
* @return array
|
|
*/
|
|
public function get_sortable_columns() {
|
|
return array(
|
|
'id' => array( 'ID', true ),
|
|
'date_created' => array( 'date', true ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get bulk actions
|
|
*
|
|
* @return array
|
|
*/
|
|
public function get_bulk_actions() {
|
|
return array(
|
|
'delete' => __( 'Usu\u0144', '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_inquiry',
|
|
'posts_per_page' => $per_page,
|
|
'paged' => $current_page,
|
|
'orderby' => $orderby,
|
|
'order' => $order,
|
|
'post_status' => 'publish',
|
|
);
|
|
|
|
if ( ! empty( $search ) ) {
|
|
$args['s'] = $search;
|
|
}
|
|
|
|
if ( ! empty( $_GET['yacht_filter'] ) && 'all' !== $_GET['yacht_filter'] ) {
|
|
$args['meta_query'] = array(
|
|
array(
|
|
'key' => '_inquiry_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="inquiry[]" value="%d" />', $item->ID );
|
|
}
|
|
|
|
/**
|
|
* Column ID
|
|
*
|
|
* @param object $item Item.
|
|
* @return string
|
|
*/
|
|
public function column_id( $item ) {
|
|
$delete_url = wp_nonce_url(
|
|
admin_url( 'admin.php?page=yacht-inquiries&action=delete&inquiry=' . $item->ID ),
|
|
'delete_inquiry_' . $item->ID
|
|
);
|
|
|
|
$actions = array(
|
|
'delete' => sprintf(
|
|
'<a href="%s" class="submitdelete" onclick="return confirm(\'%s\')">%s</a>',
|
|
esc_url( $delete_url ),
|
|
esc_js( __( 'Czy na pewno chcesz usunac to zapytanie?', 'yacht-booking' ) ),
|
|
__( 'Usun', '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 = Inquiry::get_yacht_id( $item->ID );
|
|
$yacht = get_post( $yacht_id );
|
|
|
|
return $yacht ? esc_html( $yacht->post_title ) : '—';
|
|
}
|
|
|
|
/**
|
|
* Column customer
|
|
*
|
|
* @param object $item Item.
|
|
* @return string
|
|
*/
|
|
public function column_customer( $item ) {
|
|
return sprintf(
|
|
'<strong>%s</strong><br><small>%s<br>%s</small>',
|
|
esc_html( Inquiry::get_customer_name( $item->ID ) ),
|
|
esc_html( Inquiry::get_customer_email( $item->ID ) ),
|
|
esc_html( Inquiry::get_customer_phone( $item->ID ) )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Column preferred dates
|
|
*
|
|
* @param object $item Item.
|
|
* @return string
|
|
*/
|
|
public function column_dates( $item ) {
|
|
$dates = Inquiry::get_preferred_dates( $item->ID );
|
|
return $dates ? esc_html( $dates ) : '—';
|
|
}
|
|
|
|
/**
|
|
* Column message
|
|
*
|
|
* @param object $item Item.
|
|
* @return string
|
|
*/
|
|
public function column_message( $item ) {
|
|
$message = Inquiry::get_message( $item->ID );
|
|
if ( ! $message ) {
|
|
return '—';
|
|
}
|
|
|
|
$short = mb_strlen( $message ) > 80 ? mb_substr( $message, 0, 80 ) . '...' : $message;
|
|
return '<span title="' . esc_attr( $message ) . '">' . esc_html( $short ) . '</span>';
|
|
}
|
|
|
|
/**
|
|
* Column emails — links to view sent emails.
|
|
*
|
|
* @param object $item Item.
|
|
* @return string
|
|
*/
|
|
public function column_emails( $item ) {
|
|
$admin_url = wp_nonce_url(
|
|
admin_url( 'admin.php?page=yacht-inquiries&action=view_email&inquiry=' . $item->ID . '&type=admin' ),
|
|
'view_email_' . $item->ID
|
|
);
|
|
$customer_url = wp_nonce_url(
|
|
admin_url( 'admin.php?page=yacht-inquiries&action=view_email&inquiry=' . $item->ID . '&type=customer' ),
|
|
'view_email_' . $item->ID
|
|
);
|
|
|
|
return sprintf(
|
|
'<a href="%s" class="button button-small">%s</a> <a href="%s" class="button button-small">%s</a>',
|
|
esc_url( $admin_url ),
|
|
esc_html__( 'Do admina', 'yacht-booking' ),
|
|
esc_url( $customer_url ),
|
|
esc_html__( 'Do klienta', 'yacht-booking' )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Column date created
|
|
*
|
|
* @param object $item Item.
|
|
* @return string
|
|
*/
|
|
public function column_date_created( $item ) {
|
|
return sprintf(
|
|
'%s<br><small>%s</small>',
|
|
get_the_date( Settings::get_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 '';
|
|
}
|
|
|
|
/**
|
|
* Filters above table
|
|
*
|
|
* @param string $which Position.
|
|
*/
|
|
protected function extra_tablenav( $which ) {
|
|
if ( 'top' !== $which ) {
|
|
return;
|
|
}
|
|
|
|
$current_yacht = isset( $_GET['yacht_filter'] ) ? (int) $_GET['yacht_filter'] : 'all';
|
|
?>
|
|
<div class="alignleft actions">
|
|
<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
|
|
}
|
|
|
|
/**
|
|
* No items message
|
|
*/
|
|
public function no_items() {
|
|
esc_html_e( 'Brak zapytań.', 'yacht-booking' );
|
|
}
|
|
}
|