Skip to content

Instantly share code, notes, and snippets.

@webdevid
Created August 12, 2014 19:35
Show Gist options
  • Save webdevid/0dbfcc5f14eff3175f5f to your computer and use it in GitHub Desktop.
Save webdevid/0dbfcc5f14eff3175f5f to your computer and use it in GitHub Desktop.
<?php
/**
* Copyright (c) 2011 Roman Ožana. All rights reserved.
*
* @author Roman Ožana <[email protected]>
* @link www.omdesign.cz
*/
class WpSqlHelper {
/**
* @var \wpdb
*/
private $wpdb;
/**
* @param wpdb $wpdb
*/
public function __construct(\wpdb $wpdb) {
$this->wpdb = $wpdb;
}
/**
* Delete all post revisions
*
* @return mixed
*/
public function deleteAllPostRevisions() {
$sql = "DELETE a,b,c FROM " . $this->wpdb->posts . " a
LEFT JOIN " . $this->wpdb->term_relationships . " b ON (a.ID = b.object_id)
LEFT JOIN " . $this->wpdb->postmeta . " c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision';";
return $this->wpdb->query($sql);
}
/**
* Deactivate all plugins
*
* @return mixed
*/
private function deactivateAllPlugins() {
return $this->wpdb->query("UPDATE " . $this->wpdb->options . " SET option_value = '' WHERE option_name = 'active_plugins'");
}
/**
* Delete all coment spam
*
* @return mixed
*/
public function deleteAllCommentSpam() {
return $this->wpdb->query("DELETE FROM " . $this->wpdb->comments . " WHERE comment_approved = 'spam';");
}
/**
* Delete whole feed cache from wordpress
*
* @return \false|int
*/
public function deleteFeedCache() {
return $this->wpdb->query("DELETE FROM `" . $this->wpdb->options . "` WHERE `option_name` LIKE ('_transient%_feed_%')");
}
/**
* Optimize all tables
*
* @return mixed
*/
public function optimizeTables() {
$table[] = $this->wpdb->commentmeta;
$table[] = $this->wpdb->comments;
$table[] = $this->wpdb->links;
$table[] = $this->wpdb->options;
$table[] = $this->wpdb->postmeta;
$table[] = $this->wpdb->posts;
$table[] = $this->wpdb->term_relationships;
$table[] = $this->wpdb->term_taxonomy;
$table[] = $this->wpdb->terms;
$table[] = $this->wpdb->usermeta;
$table[] = $this->wpdb->users;
$sql = "OPTIMIZE TABLE " . implode(',', $table);
return $this->wpdb->query($sql);
}
/**
* Replace text in content
*
* @param $search
* @param $replace
* @return mixed
*/
private function replaceInContent($search, $replace) {
$query = $this->wpdb->prepare('UPDATE `' . $this->wpdb->posts . '` SET post_content = replace(post_content, %s, %s)', $search, $replace);
return $this->wpdb->query($query);
}
/**
* Make default database settings after Wordpress instalation
*/
public function defaultDatabaseSettings() {
// delete all posts
$this->wpdb->query("DELETE a,b,c
FROM " . $this->wpdb->posts . " a
LEFT JOIN " . $this->wpdb->term_relationships . " b ON (a.ID = b.object_id)
LEFT JOIN " . $this->wpdb->postmeta . " c ON (a.ID = c.post_id);");
$this->wpdb->query("DELETE FROM " . $this->wpdb->links);
$this->wpdb->query("DELETE FROM " . $this->wpdb->commentmeta);
$this->wpdb->query("DELETE FROM " . $this->wpdb->comments);
if (!function_exists('update_option'))
throw new Exception('Function update_option not exists');
update_option('blogdescription', '');
update_option('admin_email', '[email protected]');
update_option('start_of_week', '1');
update_option('date_format', 'j.n.Y');
update_option('time_format', 'H:i');
update_option('posts_per_rss', '15');
update_option('posts_per_page', '5');
update_option('default_post_edit_rows', '30');
update_option('permalink_structure', '/%postname%/');
update_option('upload_path', 'wp-content/uploads');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment