Skip to content

Commit

Permalink
Add plugin; Add README
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Knott committed Oct 18, 2017
1 parent 785941e commit 914b1cf
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# WordPress DB Cleaner #

A very simple plugin that lets you clean up tables left over from previous deleted sites on a multisite installation.
While WordPress will delete the tables it creates for a new site, tables created by plugins will remain.

This plugin adds a page under tools. In the textfield on this page, a list of site IDs can be given. The plugin will go through and delete all the tables that contain that ID.

Multiple IDs can be given my comma seperating them.

# License #

MIT License. A copy can be found in the root of this repository.
68 changes: 68 additions & 0 deletions db-cleaner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/*
Plugin Name: DB Cleaner
*/


add_action('wp_ajax_my_action', 'remove_site_tables_for_id');
function remove_site_tables_for_id() {
global $wpdb;
$raw_ids = explode(",", $_POST['id'] );
$ids = array();
foreach($raw_ids as $id) {
array_push($ids, intval(trim($id)));
}
$dbname = $wpdb->dbname;

$data = array();
foreach($ids as $id) {
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME like '%_\_" . $id . "\__%' and TABLE_SCHEMA = '$dbname'";
$table_names = $wpdb->get_results($sql);
foreach($table_names as $key => $row) {
array_push($data, $row->TABLE_NAME);
$query = "DROP TABLE IF EXISTS $row->TABLE_NAME";
$wpdb->query($query);
}
}
echo join(":", $data);
wp_die();
}

function myplugin_admin_page(){
?>
<script>
function delete_tables() {
var data = {
'action': 'my_action',
'id' : jQuery('#tableids').val() // We pass php values differently!
};
console.log(data);
jQuery.post(ajaxurl, data, function(response) {
var rows = response.split(":");
jQuery("#tables_found").empty();
for (var i = 0; i < rows.length; i++ ) {
console.log(rows[i]);
jQuery("#tables_found").append("<li>" + rows[i] + "</li>");
}
});
}

</script>
<div class="wrap">
<h2>DB Cleaner</h2>
<input id="tableids" type="text">
<button onclick="delete_tables()">Delete Tables</button>
</div>
<div class="wrap">
<ul id="tables_found">

</ul>
</div>
<?php
}

function register_pages() {
add_management_page('DB Cleaner', 'DB Cleaner', 'manage_options','db-cleaner', 'myplugin_admin_page');
}

add_action("admin_menu", "register_pages");

0 comments on commit 914b1cf

Please sign in to comment.