Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DavidAnderson684/88a8e909f51b1d93bbb80720ef29c566 to your computer and use it in GitHub Desktop.
Save DavidAnderson684/88a8e909f51b1d93bbb80720ef29c566 to your computer and use it in GitHub Desktop.
Purge old VFB data
<?php
// Install as an mu-plugin, and use something like WP-Crontrol to add a regular repeating call of the purge_vfb_data action
if (!defined('ABSPATH')) die('No direct access');
add_action('purge_vfb_data', 'my_purge_vfb_data', 10, 1);
function my_purge_vfb_data($days = 90) {
global $wpdb;
$post_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM ".$wpdb->posts." WHERE post_type='vfb_entry' AND post_date < DATE_SUB(NOW(), INTERVAL %d DAY);", $days));
$count = 0;
foreach ($post_ids as $post_id) {
$args = array(
'post_parent' => $post_id,
'post_type' => 'attachment'
);
$posts = get_posts( $args );
if (is_array($posts) && count($posts) > 0) {
// Delete all the Children of the Parent Page
foreach($posts as $post){
wp_delete_post($post->ID, true);
}
}
$ret_del = wp_delete_post($post_id, true);
if (false !== $ret_del) {
$count++;
}
}
$count_post_ids = count($post_ids);
if ($count_post_ids > 0) {
error_log($count." out of ".$count_post_ids." vfb_entry posts are deleted successfully.");
} else {
error_log("There are no vfb_entry post entries.");
}
$older_tables = array('vfb_pro_entries', 'visual_form_builder_entries');
foreach ($older_tables as $older_table) {
$table_name = $wpdb->prefix.$older_table;
if ($table_name == $wpdb->get_var("SHOW TABLES LIKE '$table_name'")) {
$ret = $wpdb->query($wpdb->prepare("DELETE FROM ".$wpdb->prefix."vfb_pro_entries WHERE date_submitted < DATE_SUB(NOW(), INTERVAL %d DAY);", 90));
if ($ret) {
error_log("90 days old VFB entries from $older_table have been deleted successfully.");
} else {
error_log("No VFB entries from $older_table deleted.");
}
} else {
error_log("Table $older_table doen't exist (not an error).");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment