Skip to content

Commit 25b6a6c

Browse files
committed
added delete all features
1 parent 43f4a90 commit 25b6a6c

2 files changed

Lines changed: 96 additions & 43 deletions

File tree

cbxsearchtracker.php

Lines changed: 93 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
/**
33
* Plugin Name: CBX Search Tracker
44
* Description: Tracks WordPress search keywords and shows most searched keywords in admin dashboard.
5-
* Version: 1.1.0
5+
* Version: 1.2.0
66
* Author: Codeboxr
77
* Text Domain: cbxsearchtracker
88
* Domain Path: /languages
99
*/
1010

11-
if ( ! defined( 'ABSPATH' ) ) exit;
11+
if ( ! defined( 'ABSPATH' ) ) {
12+
exit;
13+
}
1214

1315
class CBXSearchTracker {
1416

@@ -18,12 +20,13 @@ public function __construct() {
1820
global $wpdb;
1921
$this->table_name = $wpdb->prefix . 'cbxsearchtracker_keywords';
2022

21-
register_activation_hook(__FILE__, [$this, 'create_table']);
23+
register_activation_hook( __FILE__, [ $this, 'create_table' ] );
2224

23-
add_action('plugins_loaded', [$this, 'load_textdomain']);
24-
add_action('wp', [$this, 'track_search']);
25-
add_action('admin_menu', [$this, 'register_admin_menu']);
26-
add_action('admin_init', [$this, 'handle_delete']);
25+
add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] );
26+
add_action( 'wp', [ $this, 'track_search' ] );
27+
add_action( 'admin_menu', [ $this, 'register_admin_menu' ] );
28+
add_action( 'admin_init', [ $this, 'handle_delete' ] );
29+
add_action('admin_init', [$this, 'handle_delete_all']);
2730
}
2831

2932
/* ----------------------------
@@ -33,7 +36,7 @@ public function load_textdomain() {
3336
load_plugin_textdomain(
3437
'cbxsearchtracker',
3538
false,
36-
dirname(plugin_basename(__FILE__)) . '/languages'
39+
dirname( plugin_basename( __FILE__ ) ) . '/languages'
3740
);
3841
}
3942

@@ -54,41 +57,45 @@ public function create_table() {
5457
) $charset_collate;";
5558

5659
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
57-
dbDelta($sql);
60+
dbDelta( $sql );
5861
}
5962

6063
/* ----------------------------
6164
* Track Search
6265
* ---------------------------- */
6366
public function track_search() {
6467

65-
if ( is_admin() || ! is_search() ) return;
68+
if ( is_admin() || ! is_search() ) {
69+
return;
70+
}
6671

67-
$keyword = strtolower(trim(get_search_query()));
68-
if ( empty($keyword) ) return;
72+
$keyword = strtolower( trim( get_search_query() ) );
73+
if ( empty( $keyword ) ) {
74+
return;
75+
}
6976

7077
global $wpdb;
7178

7279
$existing = $wpdb->get_row(
73-
$wpdb->prepare("SELECT * FROM {$this->table_name} WHERE keyword = %s", $keyword)
80+
$wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE keyword = %s", $keyword )
7481
);
7582

7683
if ( $existing ) {
7784
$wpdb->update(
7885
$this->table_name,
7986
[
80-
'search_count' => $existing->search_count + 1,
81-
'last_searched' => current_time('mysql')
87+
'search_count' => $existing->search_count + 1,
88+
'last_searched' => current_time( 'mysql' )
8289
],
83-
['id' => $existing->id]
90+
[ 'id' => $existing->id ]
8491
);
8592
} else {
8693
$wpdb->insert(
8794
$this->table_name,
8895
[
89-
'keyword' => $keyword,
90-
'search_count' => 1,
91-
'last_searched' => current_time('mysql')
96+
'keyword' => $keyword,
97+
'search_count' => 1,
98+
'last_searched' => current_time( 'mysql' )
9299
]
93100
);
94101
}
@@ -99,11 +106,11 @@ public function track_search() {
99106
* ---------------------------- */
100107
public function register_admin_menu() {
101108
add_menu_page(
102-
__('Search Tracker', 'cbxsearchtracker'),
103-
__('Search Tracker', 'cbxsearchtracker'),
109+
__( 'Search Tracker', 'cbxsearchtracker' ),
110+
__( 'Search Tracker', 'cbxsearchtracker' ),
104111
'manage_options',
105112
'cbxsearchtracker',
106-
[$this, 'admin_page'],
113+
[ $this, 'admin_page' ],
107114
'dashicons-chart-bar',
108115
26
109116
);
@@ -114,18 +121,22 @@ public function register_admin_menu() {
114121
* ---------------------------- */
115122
public function handle_delete() {
116123

117-
if ( ! isset($_GET['cbx_delete']) ) return;
124+
if ( ! isset( $_GET['cbx_delete'] ) ) {
125+
return;
126+
}
118127

119-
if ( ! current_user_can('manage_options') ) return;
128+
if ( ! current_user_can( 'manage_options' ) ) {
129+
return;
130+
}
120131

121-
$id = intval($_GET['cbx_delete']);
132+
$id = intval( $_GET['cbx_delete'] );
122133

123-
check_admin_referer('cbx_delete_keyword_' . $id);
134+
check_admin_referer( 'cbx_delete_keyword_' . $id );
124135

125136
global $wpdb;
126-
$wpdb->delete($this->table_name, ['id' => $id]);
137+
$wpdb->delete( $this->table_name, [ 'id' => $id ] );
127138

128-
wp_redirect(admin_url('admin.php?page=cbxsearchtracker&deleted=1'));
139+
wp_redirect( admin_url( 'admin.php?page=cbxsearchtracker&deleted=1' ) );
129140
exit;
130141
}
131142

@@ -141,49 +152,89 @@ public function admin_page() {
141152
);
142153

143154
echo '<div class="wrap">';
144-
echo '<h1>' . esc_html__('Most Searched Keywords', 'cbxsearchtracker') . '</h1>';
155+
echo '<h1>' . esc_html__( 'Most Searched Keywords', 'cbxsearchtracker' ) . '</h1>';
156+
157+
if ( isset( $_GET['deleted'] ) ) {
158+
echo '<div class="notice notice-success is-dismissible">';
159+
echo '<p>' . esc_html__( 'Keyword deleted successfully.', 'cbxsearchtracker' ) . '</p>';
160+
echo '</div>';
161+
}
145162

146-
if ( isset($_GET['deleted']) ) {
163+
if ( isset($_GET['deleted_all']) ) {
147164
echo '<div class="notice notice-success is-dismissible">';
148-
echo '<p>' . esc_html__('Keyword deleted successfully.', 'cbxsearchtracker') . '</p>';
165+
echo '<p>' . esc_html__('All keywords deleted successfully.', 'cbxsearchtracker') . '</p>';
149166
echo '</div>';
150167
}
151168

169+
170+
// Delete All Button
171+
echo '<form method="post" style="margin-bottom:15px;">';
172+
wp_nonce_field('cbx_delete_all_keywords');
173+
echo '<input type="hidden" name="cbx_delete_all" value="1" />';
174+
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?\');" />';
175+
echo '</form>';
176+
177+
152178
echo '<table class="widefat striped">';
153179
echo '<thead><tr>';
154-
echo '<th>' . esc_html__('Keyword', 'cbxsearchtracker') . '</th>';
155-
echo '<th>' . esc_html__('Search Count', 'cbxsearchtracker') . '</th>';
156-
echo '<th>' . esc_html__('Last Searched', 'cbxsearchtracker') . '</th>';
157-
echo '<th>' . esc_html__('Action', 'cbxsearchtracker') . '</th>';
180+
echo '<th>' . esc_html__( 'Keyword', 'cbxsearchtracker' ) . '</th>';
181+
echo '<th>' . esc_html__( 'Search Count', 'cbxsearchtracker' ) . '</th>';
182+
echo '<th>' . esc_html__( 'Last Searched', 'cbxsearchtracker' ) . '</th>';
183+
echo '<th>' . esc_html__( 'Action', 'cbxsearchtracker' ) . '</th>';
158184
echo '</tr></thead>';
159185
echo '<tbody>';
160186

161187
if ( $results ) {
162188
foreach ( $results as $row ) {
163189

164190
$delete_url = wp_nonce_url(
165-
admin_url('admin.php?page=cbxsearchtracker&cbx_delete=' . $row->id),
191+
admin_url( 'admin.php?page=cbxsearchtracker&cbx_delete=' . $row->id ),
166192
'cbx_delete_keyword_' . $row->id
167193
);
168194

169195
echo '<tr>';
170-
echo '<td>' . esc_html($row->keyword) . '</td>';
171-
echo '<td>' . esc_html($row->search_count) . '</td>';
172-
echo '<td>' . esc_html($row->last_searched) . '</td>';
196+
echo '<td>' . esc_html( $row->keyword ) . '</td>';
197+
echo '<td>' . esc_html( $row->search_count ) . '</td>';
198+
echo '<td>' . esc_html( $row->last_searched ) . '</td>';
173199
echo '<td>';
174-
echo '<a href="' . esc_url($delete_url) . '" class="button button-small button-danger">';
175-
echo esc_html__('Delete', 'cbxsearchtracker');
200+
echo '<a href="' . esc_url( $delete_url ) . '" class="button button-small button-danger">';
201+
echo esc_html__( 'Delete', 'cbxsearchtracker' );
176202
echo '</a>';
177203
echo '</td>';
178204
echo '</tr>';
179205
}
180206
} else {
181-
echo '<tr><td colspan="4">' . esc_html__('No searches recorded yet.', 'cbxsearchtracker') . '</td></tr>';
207+
echo '<tr><td colspan="4">' . esc_html__( 'No searches recorded yet.', 'cbxsearchtracker' ) . '</td></tr>';
182208
}
183209

184210
echo '</tbody></table>';
185211
echo '</div>';
186212
}
213+
214+
/**
215+
* Handle Delete All
216+
*
217+
* @return void
218+
*/
219+
public function handle_delete_all() {
220+
221+
if ( ! isset( $_POST['cbx_delete_all'] ) ) {
222+
return;
223+
}
224+
225+
if ( ! current_user_can( 'manage_options' ) ) {
226+
return;
227+
}
228+
229+
check_admin_referer( 'cbx_delete_all_keywords' );
230+
231+
global $wpdb;
232+
$wpdb->query( "TRUNCATE TABLE {$this->table_name}" );
233+
234+
wp_redirect( admin_url( 'admin.php?page=cbxsearchtracker&deleted_all=1' ) );
235+
exit;
236+
}
237+
187238
}
188239

189240
new CBXSearchTracker();

readme.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tags: search, search analytics, keyword tracker, search tracker, admin dashboard
44
Requires at least: 5.8
55
Tested up to: 6.5
66
Requires PHP: 7.4
7-
Stable tag: 1.1.0
7+
Stable tag: 1.2.0
88
License: GPLv2 or later
99
License URI: https://www.gnu.org/licenses/gpl-2.0.html
1010
Text Domain: cbxsearchtracker
@@ -73,6 +73,8 @@ Currently it tracks all searches. Zero-result filtering can be added in a future
7373
3. Empty state when no searches recorded
7474

7575
== Changelog ==
76+
= 1.2.0 =
77+
* Added delete all feature
7678

7779
= 1.1.0 =
7880
* Added translation support

0 commit comments

Comments
 (0)