Skip to content

Instantly share code, notes, and snippets.

@goldenapples
Created August 24, 2012 01:22
Show Gist options
  • Save goldenapples/3444456 to your computer and use it in GitHub Desktop.
Save goldenapples/3444456 to your computer and use it in GitHub Desktop.
Flat file export, first pass...
<?php
/*
Plugin Name: Flat File Database
Description: Exports WP data into a series of flat files. Hence the name.
Author: goldenapples
Author URI: http://goldenapplesdesign.com
Version: 0.1
License: GPLv2
*/
// add the admin options page
add_action('admin_menu', 'flat_file_controls');
function flat_file_controls() {
add_management_page( 'Flat File Export', 'Export all posts as flat files', 'edit_theme_options', 'ffe', 'flat_file_controls_page');
}
function flat_file_controls_page() {
if ( flat_file_export() ) return;
?>
<div>
<h2>Export site to flat files</h2>
<form action="" method="post">
<?php wp_nonce_field('flat-file-export'); ?>
<input name="save" type="submit" value="Backup entire database to flat files" />
</form></div>
<?php
}
/**
* function flat_file_export
*
* Checks to get valid filesystem credentials (this section is all copypasta from Otto's post
* at http://ottopress.com/2011/tutorial-using-the-wp_filesystem/); then if successful, begins
* to go about walking through posts and saving them to text files.
*
* @return void
* @author goldenapples
**/
function flat_file_export( ) {
if (empty($_POST)) return false;
check_admin_referer('flat-file-export');
$form_fields = array( 'save' );
$url = wp_nonce_url('tools.php?page=ffe','flat-file-export');
if ( false === ( $creds = request_filesystem_credentials($url, '', false, false, $form_fields) ) ) {
// if we get here, then we don't have credentials yet,
// but have just produced a form for the user to fill in,
// so stop processing for now
return true; // stop the normal page form from displaying
}
// now we have some credentials, try to get the wp_filesystem running
if ( ! WP_Filesystem($creds) ) {
// our credentials were no good, ask the user for them again
request_filesystem_credentials($url, '', true, false, $form_fields);
return true;
}
global $wp_filesystem;
$upload_dir = wp_upload_dir();
// $e_root; the export root, so to speak.
global $e_root;
$e_root = $upload_dir['basedir'].'/export' ;
// Make sure root folder exists
if ( !$wp_filesystem->exists( $e_root ) )
$wp_filesystem->mkdir( $e_root );
// Also a folder for content, and one for taxonomies
if ( !$wp_filesystem->exists( $e_root . '/content' ) )
$wp_filesystem->mkdir( $e_root . '/content' );
if ( !$wp_filesystem->exists( $e_root . '/taxonomy' ) )
$wp_filesystem->mkdir( $e_root . '/taxonomy' );
foreach ( get_post_types( null, 'names', 'public=true' ) as $post_type ) {
$local_dir = post_type_dir( $post_type );
$taxonomies = get_object_taxonomies( $post_type );
// Make folders now, for each of the taxonomies attached to this post type
foreach ( $taxonomies as $tax )
if ( !$wp_filesystem->exists( $e_root . '/taxonomy/' . $tax ) )
$wp_filesystem->mkdir( $e_root . '/taxonomy/' . $tax );
$posts_in_type = get_posts(
array(
'post_type' => $post_type,
'post_status' => 'publish',
'nopaging' => true
)
);
foreach ( $posts_in_type as $post ) {
setup_postdata( $post );
$object_terms = array();
$post_tax_output = '';
foreach ( $taxonomies as $tax ) {
$object_terms[$tax] = wp_get_object_terms( $post->ID, $tax, array( 'fields' => 'names' ) );
$post_tax_output .= $tax . ': ' . implode( ',', $object_terms[$tax] ) . "\r\n";
}
// This is the template the post content will be stored in
$template = <<<FORMAT
Title: {$post->post_title}
Author: {$post->post_author}
Posted: {$post->post_date}
Slug: {$post->post_name}
{$post_tax_output}
==============================
{$post->post_content}
==============================
FORMAT;
/*
* Handle all public meta values attached to a post.
*
* Meta values will be output in the format:
*
* [Meta:{$meta_key}]
* {$meta_value}
*
* ==============================
*
* (30 or more equals signs are used as a delimiter here, as elsewhere)
*/
foreach ( get_post_custom( $post->ID ) as $key => $val ) {
if ( strpos( $key, '_' ) === 0 )
continue;
if ( is_array( $val ) && count( $val ) === 1 )
$val = $val[0];
else $val = print_r( $val, true );
$template .= "[Meta:$key]" . "\r\n" . $val
. "\r\n==============================\r\n\r\n";
}
$post_loc = $local_dir . $post->post_name . '.txt';
$wp_filesystem->put_contents( $post_loc, $template, FS_CHMOD_FILE );
// Now, go back through and create symlinks for this post in all the taxonomy folders
foreach ( $taxonomies as $tax )
foreach ( $object_terms[$tax] as $term ) {
if ( !$wp_filesystem->exists( $e_root . '/taxonomy/'. $tax . '/' . $term ) )
$wp_filesystem->mkdir( $e_root . '/taxonomy/'. $tax . '/' . $term );
symlink( $post_loc, "$e_root/taxonomy/$tax/$term/{$post->post_name}.txt" );
}
}
}
}
/**
* Return the path to a post type directory
*
* @return str
* @author goldenapples
**/
function post_type_dir( $post_type ) {
global $e_root, $wp_filesystem;
$dir = trailingslashit( $e_root . '/content/' . $post_type );
if ( !$wp_filesystem->exists( $dir ) )
$wp_filesystem->mkdir( $dir );
return $dir;
}
/**
* undocumented function
*
* @return void
* @author goldenapples
**/
function flat_file_headers( ) {
return array( 'Title', 'Author', 'Posted', 'Slug', 'Catgories', 'Tags' );
}
add_filter( 'extra_flatfile_headers', 'flat_file_headers' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment