143 lines
No EOL
5.3 KiB
PHP
143 lines
No EOL
5.3 KiB
PHP
<?php
|
|
class PostTypeList {
|
|
private string $post_type;
|
|
private array $labels;
|
|
private int $items_per_page;
|
|
private int $paged;
|
|
private array $additional_query_args;
|
|
|
|
public function __construct(string $post_type, array $labels = [], array $additional_query_args = []) {
|
|
$this->post_type = $post_type;
|
|
$this->items_per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 10;
|
|
$this->paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
|
|
|
|
// Base query args
|
|
$base_args = [
|
|
'orderby' => 'title',
|
|
'order' => 'ASC',
|
|
'post_type' => $this->post_type,
|
|
'posts_per_page' => $this->items_per_page,
|
|
'paged' => $this->paged,
|
|
];
|
|
|
|
// Add owner_id filter only for non-administrators
|
|
if (!current_user_can('administrator')) {
|
|
$base_args['meta_query'] = array(
|
|
array(
|
|
'key' => 'owner_id',
|
|
'value' => get_current_user_id(),
|
|
'compare' => '='
|
|
)
|
|
);
|
|
}
|
|
|
|
// Merge with any additional query args
|
|
$this->additional_query_args = array_merge($base_args, $additional_query_args);
|
|
|
|
// Default labels that can be overridden
|
|
$this->labels = array_merge([
|
|
'title' => ucfirst($post_type) . 's',
|
|
'no_items' => 'No ' . strtolower($post_type) . 's found.',
|
|
'delete_confirm' => 'Are you sure you want to delete this ' . strtolower($post_type) . '?'
|
|
], $labels);
|
|
}
|
|
|
|
public function render(): void {
|
|
$query = new WP_Query($this->additional_query_args);
|
|
$edit_url_base = "/dashboard/" . $this->post_type . "s/edit-" . $this->post_type . "?edit=";
|
|
?>
|
|
<div class="wrap">
|
|
<!-- <h1><?php //echo esc_html($this->labels['title']); ?></h1> -->
|
|
|
|
|
|
|
|
<!-- Items list -->
|
|
<div class="post-type-list">
|
|
<?php if ($query->have_posts()) : ?>
|
|
<?php
|
|
while ($query->have_posts()) : $query->the_post();
|
|
$post_id = get_the_ID();
|
|
?>
|
|
<div class="post-type-item">
|
|
<div class="post-type-content">
|
|
<a href="<?php //echo get_permalink(); ?>" class="post-type-name">
|
|
<?php the_title(); ?>
|
|
</a>
|
|
<div class="row-actions">
|
|
<span class="edit">
|
|
<a href="<?php //echo $edit_url_base . $post_id; ?>">Edit</a> |
|
|
</span>
|
|
<span class="trash">
|
|
<?php //echo $this->get_delete_link($post_id); ?>
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endwhile; ?>
|
|
<?php else : ?>
|
|
<p><?php echo esc_html($this->labels['no_items']); ?></p>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div class="page-nav">
|
|
<div class="tablenav-pages">
|
|
<?php
|
|
echo paginate_links(array(
|
|
'base' => add_query_arg('paged', '%#%'),
|
|
'format' => '',
|
|
'prev_text' => __('«'),
|
|
'next_text' => __('»'),
|
|
'total' => $query->max_num_pages,
|
|
'current' => $this->paged
|
|
));
|
|
?>
|
|
</div>
|
|
</div>
|
|
|
|
<?php wp_reset_postdata(); ?>
|
|
</div>
|
|
|
|
|
|
<script>
|
|
function handlePerPageChange(select) {
|
|
// Remove the paged parameter if it exists in the URL
|
|
var form = document.getElementById('items-per-page-form');
|
|
var currentPath = window.location.pathname;
|
|
|
|
// Remove any existing page/X/ from the path
|
|
currentPath = currentPath.replace(/page\/\d+\/?/, '');
|
|
|
|
// Update form action to use the cleaned path
|
|
form.action = currentPath;
|
|
// console.log(currentPath);
|
|
|
|
// Submit the form
|
|
form.submit();
|
|
}
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
protected function get_delete_link($post_id) {
|
|
if (!can_delete_post($post_id)) {
|
|
return '';
|
|
}
|
|
|
|
$current_url = add_query_arg(array()); // Gets current URL with existing parameters
|
|
$delete_url = wp_nonce_url(
|
|
add_query_arg(
|
|
array(
|
|
'action' => 'delete',
|
|
'post' => $post_id,
|
|
'redirect_to' => urlencode($current_url)
|
|
),
|
|
$current_url // Use current page URL instead of admin URL
|
|
),
|
|
'delete-post_' . $post_id
|
|
);
|
|
|
|
return sprintf(
|
|
'<a href="%s" onclick="return confirm(\'Are you sure you want to delete this item?\');" class="delete">Delete</a>',
|
|
esc_url($delete_url)
|
|
);
|
|
}
|
|
}
|