-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbxsearchtracker.php
More file actions
265 lines (212 loc) · 6.91 KB
/
cbxsearchtracker.php
File metadata and controls
265 lines (212 loc) · 6.91 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/**
* Plugin Name: CBX Search Tracker
* Description: Tracks WordPress search keywords and shows most searched keywords in admin dashboard.
* Plugin URI: https://github.com/codeboxrcodehub/cbxsearchtracker
* Version: 1.2.1
* Author: Codeboxr
* Author URI: http://codeboxr.com
* Text Domain: cbxsearchtracker
* Domain Path: /languages
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class CBXSearchTracker {
private $table_name;
public function __construct() {
global $wpdb;
$this->table_name = $wpdb->prefix . 'cbxsearchtracker_keywords';
register_activation_hook( __FILE__, [ $this, 'create_table' ] );
add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] );
add_action( 'wp', [ $this, 'track_search' ] );
add_action( 'admin_menu', [ $this, 'register_admin_menu' ] );
add_action( 'admin_init', [ $this, 'handle_delete' ] );
add_action('admin_init', [$this, 'handle_delete_all']);
}
/* ----------------------------
* Load Translation
* ---------------------------- */
public function load_textdomain() {
load_plugin_textdomain(
'cbxsearchtracker',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages'
);
}
/* ----------------------------
* Create DB Table
* ---------------------------- */
public function create_table() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$this->table_name} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
keyword VARCHAR(255) NOT NULL,
search_count BIGINT(20) UNSIGNED DEFAULT 1,
last_searched DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY keyword (keyword)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/* ----------------------------
* Track Search
* ---------------------------- */
public function track_search() {
if ( is_admin() || ! is_search() ) {
return;
}
$keyword = strtolower( trim( get_search_query() ) );
if ( empty( $keyword ) ) {
return;
}
global $wpdb;
$existing = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE keyword = %s", $keyword )
);
if ( $existing ) {
$wpdb->update(
$this->table_name,
[
'search_count' => $existing->search_count + 1,
'last_searched' => current_time( 'mysql' )
],
[ 'id' => $existing->id ]
);
} else {
$wpdb->insert(
$this->table_name,
[
'keyword' => $keyword,
'search_count' => 1,
'last_searched' => current_time( 'mysql' )
]
);
}
}
/* ----------------------------
* Admin Menu
* ---------------------------- */
public function register_admin_menu() {
add_menu_page(
__( 'Search Tracker', 'cbxsearchtracker' ),
__( 'Search Tracker', 'cbxsearchtracker' ),
'manage_options',
'cbxsearchtracker',
[ $this, 'admin_page' ],
'dashicons-chart-bar',
26
);
}
/* ----------------------------
* Handle Delete
* ---------------------------- */
public function handle_delete() {
if ( ! isset( $_GET['cbx_delete'] ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$id = intval( $_GET['cbx_delete'] );
check_admin_referer( 'cbx_delete_keyword_' . $id );
global $wpdb;
$wpdb->delete( $this->table_name, [ 'id' => $id ] );
wp_redirect( admin_url( 'admin.php?page=cbxsearchtracker&deleted=1' ) );
exit;
}
/* ----------------------------
* Admin Page
* ---------------------------- */
public function admin_page() {
global $wpdb;
$results = $wpdb->get_results(
"SELECT * FROM {$this->table_name} ORDER BY search_count DESC LIMIT 100"
);
$total_keywords = (int) $wpdb->get_var(
"SELECT COUNT(*) FROM {$this->table_name}"
);
$total_searches = (int) $wpdb->get_var(
"SELECT SUM(search_count) FROM {$this->table_name}"
);
echo '<div class="wrap">';
echo '<h1>' . esc_html__( 'Most Searched Keywords', 'cbxsearchtracker' ) . '</h1>';
if ( isset( $_GET['deleted'] ) ) {
echo '<div class="notice notice-success is-dismissible">';
echo '<p>' . esc_html__( 'Keyword deleted successfully.', 'cbxsearchtracker' ) . '</p>';
echo '</div>';
}
if ( isset($_GET['deleted_all']) ) {
echo '<div class="notice notice-success is-dismissible">';
echo '<p>' . esc_html__('All keywords deleted successfully.', 'cbxsearchtracker') . '</p>';
echo '</div>';
}
echo '<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;">';
// LEFT SIDE — Delete All Button
echo '<form method="post">';
wp_nonce_field('cbx_delete_all_keywords');
echo '<input type="hidden" name="cbx_delete_all" value="1" />';
echo '<input type="submit" class="button button-primary" value="' . esc_attr__('Delete All Keywords', 'cbxsearchtracker') . '" onclick="return confirm(\'Are you sure you want to delete all keywords?\');" />';
echo '</form>';
// RIGHT SIDE — Total Stats
echo '<div style="font-weight:600;">';
echo esc_html__('Total Keywords:', 'cbxsearchtracker') . ' ' . esc_html($total_keywords);
echo ' | ';
echo esc_html__('Total Searches:', 'cbxsearchtracker') . ' ' . esc_html($total_searches);
echo '</div>';
echo '</div>';
echo '<table class="widefat striped">';
echo '<thead><tr>';
echo '<th>' . esc_html__( 'Keyword', 'cbxsearchtracker' ) . '</th>';
echo '<th>' . esc_html__( 'Search Count', 'cbxsearchtracker' ) . '</th>';
echo '<th>' . esc_html__( 'Last Searched', 'cbxsearchtracker' ) . '</th>';
echo '<th>' . esc_html__( 'Action', 'cbxsearchtracker' ) . '</th>';
echo '</tr></thead>';
echo '<tbody>';
if ( $results ) {
foreach ( $results as $row ) {
$delete_url = wp_nonce_url(
admin_url( 'admin.php?page=cbxsearchtracker&cbx_delete=' . $row->id ),
'cbx_delete_keyword_' . $row->id
);
echo '<tr>';
echo '<td>' . esc_html( $row->keyword ) . '</td>';
echo '<td>' . esc_html( $row->search_count ) . '</td>';
echo '<td>' . esc_html( $row->last_searched ) . '</td>';
echo '<td>';
echo '<a href="' . esc_url( $delete_url ) . '" class="button button-small button-danger">';
echo esc_html__( 'Delete', 'cbxsearchtracker' );
echo '</a>';
echo '</td>';
echo '</tr>';
}
} else {
echo '<tr><td colspan="4">' . esc_html__( 'No searches recorded yet.', 'cbxsearchtracker' ) . '</td></tr>';
}
echo '</tbody></table>';
echo '</div>';
}
/**
* Handle Delete All
*
* @return void
*/
public function handle_delete_all() {
if ( ! isset( $_POST['cbx_delete_all'] ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
check_admin_referer( 'cbx_delete_all_keywords' );
global $wpdb;
$wpdb->query( "TRUNCATE TABLE {$this->table_name}" );
wp_redirect( admin_url( 'admin.php?page=cbxsearchtracker&deleted_all=1' ) );
exit;
}
}
new CBXSearchTracker();