Skip to content

Instantly share code, notes, and snippets.

@thomasgriffin
Created August 30, 2012 02:17
Show Gist options
  • Save thomasgriffin/3521748 to your computer and use it in GitHub Desktop.
Save thomasgriffin/3521748 to your computer and use it in GitHub Desktop.
<?php
/**
* Database emtpying and file removing class.
*
* Truncates all necessary tables in the defined database and removes
* any files uploaded by the demo user.
*
* @since 1.0.0
*
* @author Thomas Griffin
*/
final class TGM_Empty_Soliloquy_Demo {
/**
* MySQL database host.
*
* @since 1.0.0
*
* @var string
*/
private $mysql_host = 'YOUR MYSQL HOST GOES HERE';
/**
* MySQL database user.
*
* @since 1.0.0
*
* @var string
*/
private $mysql_user = 'YOUR DB USER GOES HERE';
/**
* MySQL database password.
*
* @since 1.0.0
*
* @var string
*/
private $mysql_pass = 'YOUR DB USER PASSWORD GOES HERE';
/**
* MySQL database name.
*
* @since 1.0.0
*
* @var string
*/
private $mysql_db = 'YOUR DB NAME GOES HERE';
/**
* MySQL database link identifier.
*
* @since 1.0.0
*
* @var string
*/
private $mysql_link;
/**
* MySQL database instance.
*
* @since 1.0.0
*
* @var string
*/
private $mysql_instance;
/**
* Database tables to truncate.
*
* @since 1.0.0
*
* @var array
*/
private $mysql_tables = array( 'ENTER AN ARRAY OF TABLES YOU WANT TO TRUNCATE HERE' );
/**
* Server path to the uploads folder.
*
* @since 1.0.0
*
* @var string
*/
private $uploads_path = 'IF YOUR DEMO SITE STORES UPLOADS, PLACE THE REAL PATH TO THE UPLOADS FOLDER HERE';
/**
* Constructor. Loads the class.
*
* @since 1.0.0
*/
private function __construct() {
/** Load the class */
$this->load();
}
/**
* Performs all the necessary actions to empty out the demo area.
*
* @since 1.0.0
*/
private function load() {
/** Open the database connection, and if it fails, simply exit the script */
if ( ! $this->mysql_link = mysqli_connect( $this->mysql_host, $this->mysql_user, $this->mysql_pass ) )
exit;
/** Connect to our demo database, and if something fails, close the MySQL connection and exit the script */
if ( ! $this->mysql_instance = mysqli_select_db( $this->mysql_db, $this->mysql_link ) ) {
mysqli_close( $this->mysql_link );
exit;
}
/** Truncate database tables, and if something fails, close the MySQL connection and exit the script */
if ( ! $this->truncate_tables( $this->mysql_tables ) ) {
mysqli_close( $this->mysql_link );
exit;
}
/** Remove image files that have been uploaded, and if something fails, close the MySQL connection and exit the script */
if ( ! $this->remove_image_files( $this->uploads_path ) ) {
mysqli_close( $this->mysql_link );
exit;
}
/** Close the database connection */
mysqli_close( $this->mysql_link );
}
/**
* Truncates the wp_posts and wp_postmeta tables. It also adds back the home page after truncating
* the tables.
*
* @since 1.0.0
*
* @return bool True on success, false if an error
*/
private function truncate_tables( $tables ) {
/** Truncate the tables */
foreach ( $tables as $table ) {
$query = sprintf( "TRUNCATE TABLE %s", mysqli_real_escape_string( $table, $this->mysql_link ) );
if ( ! $result = mysqli_query( $query, $this->mysql_link ) )
return false;
}
/** Insert the home page into the database */
$query = sprintf( "INSERT INTO solil_demo_wp_posts (ID, post_author, post_date, post_date_gmt, post_title, post_status, comment_status, ping_status, post_name, post_parent, guid, menu_order, post_type, comment_count ) VALUES (1, 1, %d, %d, 'Home', 'publish', 'closed', 'closed', 'home', 0, '%s', 0, 'page', 0)", date( 'Y-m-d H:i:s' ), gmdate( 'Y-m-d H:i:s' ), mysqli_real_escape_string( stripslashes( 'http://demo.soliloquywp.com/?page_id=1' ), $this->mysql_link ) );
if ( ! $result = mysqli_query( $query, $this->mysql_link ) ) {
echo mysqli_error();
return false;
}
/** Set the show_on_front option to a page */
$query = "UPDATE solil_demo_wp_options SET option_value='page' WHERE option_name='show_on_front'";
if ( ! $result = mysqli_query( $query, $this->mysql_link ) ) {
echo mysqli_error();
return false;
}
/** Now we set the show_on_front option to our home page ID */
$query = "UPDATE solil_demo_wp_options SET option_value=1 WHERE option_name='page_on_front'";
if ( ! $result = mysqli_query( $query, $this->mysql_link ) ) {
echo mysqli_error();
return false;
}
return true;
}
/**
* Removes any image files added in the demo sliders.
*
* @since 1.0.0
*
* @param string $path The server path to the uploads directory
*/
private function remove_image_files( $path ) {
/** If the path is not to a directory, bail out */
if ( ! is_dir( $path ) )
return false;
/** If we can't grab any of the file contents, bail out */
if ( ! $contents = $this->get_all_dir_files( $path ) )
return false;
/** Loop through the files and delete them, and if there is an issue, bail out */
foreach ( $contents as $file )
if ( ! @unlink( $file ) )
return false;
return true;
}
/**
* Recursive function to scan through all directories in the wp-content/uploads
* folder and remove all image files.
*
* @since 1.0.0
*
* @param string $path The server path to the uploads directory
*/
private function get_all_dir_files( $path ) {
/** Scan the directory and get all the contents */
$root = scandir( $path );
$result = array();
/** Loop through the contents */
foreach ( $root as $value ) {
/** If we have a directory, let's skip over it */
if ( '.' === $value || '..' === $value )
continue;
/** If it is a file, add it to the array and continue looking for more files/directories */
if ( is_file( $path . '/' . $value ) ) {
$result[] = $path . '/' . $value;
continue;
}
/** Loop through any directories found and add any files to the array */
foreach ( $this->get_all_dir_files( $path . '/' . $value ) as $value )
$result[] = $value;
}
/** Return the array of files found when scanning the uploads directory */
return $result;
}
/**
* Helper function to get the class object. If instance is already set, return it.
* Else create the object and return it.
*
* @since 1.0.0
*
* @return object $instance Return the class instance
*/
public static function get_instance() {
if ( ! isset( self::$instance ) )
return self::$instance = new TGM_Empty_Soliloquy_Demo;
return self::$instance;
}
}
/** Instantiate the class */
$tgm_empty_soliloquy_demo = TGM_Empty_Soliloquy_Demo::get_instance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment