-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin-page.php
116 lines (109 loc) · 3.72 KB
/
admin-page.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Created by PhpStorm.
* User: bilalunalnet
* Date: 11.03.2017
* Time: 17:01
*/
add_action( 'admin_menu', 'admin_menu' );
/**
* creates Tag-o-Master entry under posts menu
*/
function admin_menu() {
add_posts_page('Tag-o-Master settings page', 'Tag-o-Master', 'manage_options', 'tom_admin_page', 'admin_menu_content');
}
/**
* tag-o-master admin page content
*/
function admin_menu_content() {
if (!current_user_can( 'manage_options' )) {
wp_die(__( 'You do not have sufficient permissions to access this page.'));
}
?>
<div class="wrap">
<h2>Tag-o-Master Admin Page</h2>
<?php
global $wpdb;
// PAGINATION
$tag_count_sql = "SELECT COUNT(DISTINCT K.term_id)
FROM $wpdb->terms K, $wpdb->term_taxonomy Y, $wpdb->term_relationships P, $wpdb->postmeta Q
WHERE K.term_id = Y.term_id
AND Y.taxonomy = 'post_tag'
AND P.term_taxonomy_id = K.term_id
AND Q.post_id = P.object_id
AND Q.meta_key = '_likes_count'";
$tag_count = $wpdb->get_var($tag_count_sql);
$tags_per_page = 10;
$page_count = ceil($tag_count / $tags_per_page);
$page = isset($_GET['pages']) ? (int) $_GET['pages'] : 1;
if($page < 1) $page = 1;
if($page > $page_count) $page = $page_count;
$limit = ($page - 1) * $tags_per_page;
$current_url = add_query_arg( NULL, NULL );
// PAGE LINKS
for($s = 1; $s <= $page_count; $s++) {
if($page == $s) {
echo $s . ' ';
} else {
echo '<a href="'.$current_url.'&pages=' . $s . '">' . $s . '</a> ';
}
}
?>
<table class="widefat">
<thead>
<tr>
<th>Tag ID</th>
<th>Tag Name</th>
<th>Posts the tag is associated with</th>
<th>Likes count</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Tag ID</th>
<th>Tag Name</th>
<th>Posts the tag is associated with</th>
<th>Likes count</th>
</tr>
</tfoot>
<tbody>
<?php
// LISTING TAGS
$sql = "SELECT
K.term_id as tag_id,
K.name, Y.count, SUM(Q.meta_value) as like_count
FROM $wpdb->terms K, $wpdb->term_taxonomy Y, $wpdb->term_relationships P, $wpdb->postmeta Q
WHERE K.term_id = Y.term_id
AND Y.taxonomy = 'post_tag'
AND P.term_taxonomy_id = K.term_id
AND Q.post_id = P.object_id
AND Q.meta_key = '_likes_count'
GROUP BY K.term_id,K.name,Y.count
ORDER BY like_count DESC
LIMIT $limit, $tags_per_page
";
$tags = $wpdb->get_results($sql, OBJECT);
foreach ($tags as $tag) {
echo "<tr>";
echo "<td>$tag->tag_id</td>";
echo "<td><a href='".get_tag_link($tag->tag_id)."'>$tag->name</a></td>";
echo "<td>$tag->count</td>";
echo "<td>$tag->like_count</td>";
echo "</tr>";
}
?>
</tbody>
</table>
<?php
// PAGE LINKS
for($s = 1; $s <= $page_count; $s++) {
if($page == $s) {
echo $s . ' ';
} else {
echo '<a href="'.$current_url.'&pages=' . $s . '">' . $s . '</a> ';
}
}
?>
</div>
<?php
}