Skip to content

Commit 51dc6f7

Browse files
committed
Improved stats tables
1 parent eefbe63 commit 51dc6f7

File tree

3 files changed

+232
-53
lines changed

3 files changed

+232
-53
lines changed
File renamed without changes.

admin/partials/manuel-image-stats.php

Lines changed: 113 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,121 @@
66
exit; // Exit if accessed directly.
77
}
88

9-
// Fetch statistics from the database.
10-
$manuel_settings = new Manuel_Settings( '1.0' );
11-
$removed_images = $manuel_settings->get_manuel_cron()->get_images_stats();
9+
if (!class_exists('WP_List_Table')) {
10+
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
11+
}
12+
13+
class Manuel_Image_Stats extends \WP_List_Table
14+
{
15+
public function __construct() {
16+
parent::__construct([
17+
'singular' => __('Removed Image', 'manuel'),
18+
'plural' => __('Removed Images', 'manuel'),
19+
'ajax' => false
20+
]);
21+
22+
$this->prepare_items();
23+
}
24+
25+
public function get_columns() {
26+
return [
27+
'cb' => '<input type="checkbox" />',
28+
'post_id' => __('Post ID', 'manuel'),
29+
'post_title' => __('Post Title', 'manuel'),
30+
'original_image' => __('Original Image', 'manuel'),
31+
'time_removed' => __('Time Removed', 'manuel')
32+
];
33+
}
34+
35+
public function prepare_items() {
36+
$perPage = 20;
37+
$currentPage = $this->get_pagenum();
38+
$manuel_settings = new Manuel_Settings( '1.0' );
39+
$data = $manuel_settings->get_manuel_cron()->get_images_stats();
40+
41+
$totalItems = count($data);
42+
43+
$this->set_pagination_args([
44+
'total_items' => $totalItems,
45+
'per_page' => $perPage
46+
]);
47+
48+
$this->items = array_slice($data, (($currentPage-1)*$perPage), $perPage);
49+
50+
$this->_column_headers = [$this->get_columns(), [], []];
51+
}
52+
53+
public function column_default($item, $column_name) {
54+
switch ($column_name) {
55+
case 'post_id':
56+
case 'post_title':
57+
case 'time_removed':
58+
return esc_html($item[$column_name]);
59+
case 'original_image':
60+
return '<a href="' . esc_url($item[$column_name]) . '">' . esc_url($item[$column_name]) . '</a>';
61+
default:
62+
return print_r($item, true);
63+
}
64+
}
65+
66+
public function column_title($item) {
67+
$title = '<strong>' . esc_html($item->post_title) . '</strong>';
68+
69+
$actions = [
70+
'edit' => sprintf('<a href="%s">%s</a>', get_edit_post_link($item->ID), __('Edit', 'manuel')),
71+
'trash' => sprintf('<a href="%s">%s</a>', get_delete_post_link($item->ID), __('Trash', 'manuel')),
72+
'view' => sprintf('<a href="%s">%s</a>', get_permalink($item->ID), __('View', 'manuel'))
73+
];
74+
75+
return $title . $this->row_actions($actions);
76+
}
77+
78+
public function column_cb($item) {
79+
return sprintf('<input type="checkbox" name="bulk-trash[]" value="%s" />', $item->ID);
80+
}
81+
82+
public function extra_tablenav($which) {
83+
if ($which === 'top') {
84+
?>
85+
<div class="alignleft actions bulkactions">
86+
<label for="bulk-action-selector-top" class="screen-reader-text">Select bulk action</label>
87+
<select name="action" id="bulk-action-selector-top">
88+
<option value="-1">Bulk Actions</option>
89+
<option value="trash">Move to Trash</option>
90+
</select>
91+
<input type="submit" id="doaction" class="button action" value ="Apply">
92+
</div>
93+
<div class="alignleft actions">
94+
<label class="screen-reader-text" for="post-search-input">Search Posts:</label>
95+
<input type="search" id="post-search-input" name="s" value="">
96+
<input type="submit" id="search-submit" class="button" value="Search Posts">
97+
</div>
98+
<?php
99+
}
100+
if ($which === 'bottom'){
101+
?>
102+
<div class="alignleft actions bulkactions">
103+
<label for="bulk-action-selector-bottom" class="screen-reader-text">Select bulk action</label>
104+
<select name="action2" id="bulk-action-selector-bottom">
105+
<option value="-1">Bulk Actions</option>
106+
<option value="trash">Move to Trash</option>
107+
</select>
108+
<input type="submit" id="doaction2" class="button action" value="Apply">
109+
</div>
110+
<?php
111+
}
112+
}
113+
}
114+
115+
$ImagesTable = new Manuel_Image_Stats();
12116
?>
13117

14118
<div class="wrap">
15119
<h1><?php esc_html_e( 'Removed Images List', 'manuel' ); ?></h1>
16-
17-
<table class="widefat">
18-
<thead>
19-
<tr>
20-
<th><?php esc_html_e( 'Post ID', 'manuel' ); ?></th>
21-
<th><?php esc_html_e( 'Post Title', 'manuel' ); ?></th>
22-
<th><?php esc_html_e( 'Original Image', 'manuel' ); ?></th>
23-
<th><?php esc_html_e( 'Time Removed', 'manuel' ); ?></th>
24-
</tr>
25-
</thead>
26-
<tbody>
27-
<?php foreach ( $removed_images as $stat ) : ?>
28-
<tr>
29-
<td><?php echo esc_html( $stat['post_id'] ); ?></td>
30-
<td><?php echo esc_html( $stat['post_title'] ); ?></td>
31-
<td><a href="<?php echo esc_url( $stat['original_image'] ); ?>"><?php echo esc_url( $stat['original_image'] ); ?></a></td>
32-
<td><?php echo esc_html( $stat['time_removed'] ); ?></td>
33-
</tr>
34-
<?php endforeach; ?>
35-
</tbody>
36-
</table>
120+
<form id="posts-filter" method="get">
121+
<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
122+
<?php
123+
$ImagesTable->display();
124+
?>
125+
</form>
37126
</div>

admin/partials/manuel-links-stats.php

Lines changed: 119 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,128 @@
22

33
namespace Manuel;
44

5-
if ( ! defined( 'ABSPATH' ) ) {
5+
if (!defined('ABSPATH')) {
66
exit; // Exit if accessed directly.
77
}
88

9-
// Fetch statistics from the database.
10-
$manuel_settings = new Manuel_Settings( '1.0' );
11-
$stats = $manuel_settings->get_manuel_cron()->get_stats();
9+
if (!class_exists('WP_List_Table')) {
10+
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
11+
}
12+
13+
class Manuel_Links_Stats extends \WP_List_Table {
14+
public function __construct()
15+
{
16+
parent::__construct([
17+
'singular' => __('Stat', 'manuel'),
18+
'plural' => __('Stats', 'manuel'),
19+
'ajax' => false
20+
]);
21+
}
22+
23+
public function get_columns() {
24+
return [
25+
'cb' => '<input type="checkbox" />', // Render a checkbox.
26+
'post_title' => __('Post Title', 'manuel'),
27+
'original_link' => __('Original Link', 'manuel'),
28+
'anchor_text' => __('Anchor Text', 'manuel'),
29+
'time_removed' => __('Time Removed', 'manuel')
30+
];
31+
}
32+
33+
public function column_cb($item) {
34+
return sprintf('<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['ID']);
35+
}
36+
37+
public function prepare_items() {
38+
$columns = $this->get_columns();
39+
$hidden = array();
40+
$sortable = array();
41+
42+
$this->_column_headers = array($columns, $hidden, $sortable);
43+
44+
$manuel_settings = new Manuel_Settings('1.0');
45+
$data = $manuel_settings->get_manuel_cron()->get_stats();
46+
47+
$perPage = 20;
48+
$currentPage = $this->get_pagenum();
49+
$totalItems = count($data);
50+
51+
$this->set_pagination_args([
52+
'total_items' => $totalItems,
53+
'per_page' => $perPage
54+
]);
55+
56+
$data = array_slice($data, (($currentPage-1)*$perPage), $perPage);
57+
58+
$this->items = $data;
59+
}
60+
61+
public function column_default($item, $column_name) {
62+
switch ($column_name) {
63+
case 'post_id':
64+
case 'post_title':
65+
case 'anchor_text':
66+
case 'time_removed':
67+
return esc_html($item[$column_name]);
68+
case 'original_link':
69+
return '<a href="' . esc_url($item[$column_name]) . '">' . esc_url($item[$column_name]) . '</a>';
70+
default:
71+
return print_r($item, true);
72+
}
73+
}
74+
75+
public function column_post_title($item) {
76+
$actions = [
77+
'edit' => sprintf('<a href="%s">%s</a>', get_edit_post_link($item['post_id']), __('Edit', 'manuel')),
78+
'trash' => sprintf('<a href="%s">%s</a>', get_delete_post_link($item['post_id']), __('Trash', 'manuel')),
79+
'view' => sprintf('<a href="%s">%s</a>', get_permalink($item['post_id']), __('View', 'manuel'))
80+
];
81+
82+
return sprintf('%1$s %2$s', $item['post_title'], $this->row_actions($actions));
83+
}
84+
85+
public function extra_tablenav($which) {
86+
if ($which === 'top') {
87+
?>
88+
<div class="alignleft actions bulkactions">
89+
<label for="bulk-action-selector-top" class="screen-reader-text">Select bulk action</label>
90+
<select name="action" id="bulk-action-selector-top">
91+
<option value="-1">Bulk Actions</option>
92+
<option value="trash">Move to Trash</option>
93+
</select>
94+
<input type="submit" id="doaction" class="button action" value ="Apply">
95+
</div>
96+
<div class="alignleft actions">
97+
<label class="screen-reader-text" for="post-search-input">Search Posts:</label>
98+
<input type="search" id="post-search-input" name="s" value="">
99+
<input type="submit" id="search-submit" class="button" value="Search Posts">
100+
</div>
101+
<?php
102+
}
103+
if ($which === 'bottom'){
104+
?>
105+
<div class="alignleft actions bulkactions">
106+
<label for="bulk-action-selector-bottom" class="screen-reader-text">Select bulk action</label>
107+
<select name="action2" id="bulk-action-selector-bottom">
108+
<option value="-1">Bulk Actions</option>
109+
<option value="trash">Move to Trash</option>
110+
</select>
111+
<input type="submit" id="doaction2" class="button action" value="Apply">
112+
</div>
113+
<?php
114+
}
115+
}
116+
}
117+
118+
$linksTable = new Manuel_Links_Stats();
12119
?>
13120

14121
<div class="wrap">
15-
<h1><?php esc_html_e( 'Manuel Stats', 'manuel' ); ?></h1>
16-
17-
<table class="widefat">
18-
<thead>
19-
<tr>
20-
<th><?php esc_html_e( 'Post ID', 'manuel' ); ?></th>
21-
<th><?php esc_html_e( 'Post Title', 'manuel' ); ?></th>
22-
<th><?php esc_html_e( 'Original Link', 'manuel' ); ?></th>
23-
<th><?php esc_html_e( 'Anchor Text', 'manuel' ); ?></th>
24-
<th><?php esc_html_e( 'Time Removed', 'manuel' ); ?></th>
25-
</tr>
26-
</thead>
27-
<tbody>
28-
<?php foreach ( $stats as $stat ) : ?>
29-
<tr>
30-
<td><?php echo esc_html( $stat['post_id'] ); ?></td>
31-
<td><?php echo esc_html( $stat['post_title'] ); ?></td>
32-
<td><a href="<?php echo esc_url( $stat['original_link'] ); ?>"><?php echo esc_url( $stat['original_link'] ); ?></a></td>
33-
<td><?php echo esc_html( $stat['anchor_text'] ); ?></td>
34-
<td><?php echo esc_html( $stat['time_removed'] ); ?></td>
35-
</tr>
36-
<?php endforeach; ?>
37-
</tbody>
38-
</table>
39-
</div>
122+
<h2><?php esc_html_e('Manuel Links Stats', 'manuel') ?></h2>
123+
<form method="post">
124+
<?php
125+
$linksTable->prepare_items();
126+
$linksTable->display();
127+
?>
128+
</form>
129+
</div>

0 commit comments

Comments
 (0)