Skip to content

Instantly share code, notes, and snippets.

@tommymarshall
Created May 25, 2016 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommymarshall/d6bf0be0310e3e62bb951901377fb033 to your computer and use it in GitHub Desktop.
Save tommymarshall/d6bf0be0310e3e62bb951901377fb033 to your computer and use it in GitHub Desktop.
Resets WordPress posts to a previous revision that does not contain spam links.
<?php
function contains($str, array $arr)
{
foreach($arr as $a)
{
if (stripos($str, $a) !== false) return true;
}
return false;
}
// Find posts containing these words
$bad_words_list = [
'1800petmeds',
'atarax',
'baclofen',
'canada-drugsonline',
'dapoxetine',
'doctormedsnoprescriptionrx',
'doxycycline',
'drugstoreforyou',
'estrace',
'estradiol',
'ethinyl',
'fluoxetine',
'fucidin',
'levitra',
'medicalcareontheinternet',
'ordermedsnoprescription',
'paper-help24h',
'partnerpharmacy24',
'prednisone',
'rxshopnow',
'serotonin',
'sildenafil',
'valacyclovir',
'valtrex',
'zithromax',
];
// Database credentials
$db_host = '127.0.0.1';
$db_name = 'wordpress_db';
$db_user = 'root';
$db_pass = '';
$connection = new PDO('mysql:host='.$db_host.';dbname='.$db_name.';charset=utf8', $db_user, $db_pass);
foreach ($bad_words_list as $bad)
{
$query = 'SELECT * FROM `wp_posts` WHERE `post_content` LIKE "%'.$bad.'%" AND `post_status` = "publish"';
echo '<hr>';
$primary_query = $connection->query($query);
echo '<h2>Results for: '.$bad.' ('.$primary_query->rowCount().' found)</h2>';
foreach($primary_query as $primary)
{
$sub_query = 'SELECT * FROM `wp_posts` WHERE `post_name` = "'.$primary['ID'].'-revision-v1" ORDER BY `ID` DESC';
$sub_query_results = $connection->query($sub_query);
if ($sub_query_results->rowCount() > 0)
{
foreach($sub_query_results as $revision)
{
if ( contains($revision['post_content'], $bad_list) === false)
{
$update = 'UPDATE `wp_posts` SET
`post_status` = "publish",
`post_modified` = now(),
`post_modified_gmt` = now(),
`post_name` = "'.$primary['post_name'].'",
`ping_status` = "'.$primary['ping_status'].'",
`post_parent` = 0,
`post_type` = "'.$primary['ping_type'].'",
WHERE `ID`='.$revision['ID'];
$connection->query($update);
echo 'Updated '.$revision['ID'].' to be live Version<br>';
$delete = 'DELETE FROM `wp_posts` WHERE `ID` = '.$primary['ID'];
$connection->query($delete);
echo 'Deleted '.$primary['ID'].'<br>';
break;
}
else
{
$delete = 'DELETE FROM `wp_posts` WHERE `ID` = '.$revision['ID'];
$connection->query($delete);
echo 'Deleted Revision '.$revision['ID'].'<br>';
}
}
}
else
{
echo 'No Revisions to roll back to for '.$primary['ID'].' (Clean manually)<br>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment