a rather stable TXT output version, finally

This commit is contained in:
Peter Molnar 2016-08-10 09:24:02 +00:00
parent 92a24e6d6b
commit d86764d91f
2 changed files with 295 additions and 228 deletions

View file

@ -2,7 +2,7 @@
"name": "petermolnar/wp-flatexport", "name": "petermolnar/wp-flatexport",
"description": "auto-export WordPress contents to folders and plain text + markdown files for longetivity and portability", "description": "auto-export WordPress contents to folders and plain text + markdown files for longetivity and portability",
"require": { "require": {
"php": ">=5.4.0", "php": ">=5.4.0"
}, },
"license": "GPLv3", "license": "GPLv3",
"authors": [ "authors": [

View file

@ -27,18 +27,24 @@ License: GPLv3
namespace WP_FLATEXPORTS; namespace WP_FLATEXPORTS;
define ( 'WP_FLATEXPORTS\force', true ); require (__DIR__ . '/vendor/autoload.php');
define ( 'WP_FLATEXPORTS\basedir', 'flat' ); use KzykHys\FrontMatter\FrontMatter;
define ( 'flatroot', \WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'flat' );
define ( 'WP_FLATEXPORTS\txtfile', 'index.txt' ); define ( 'WP_FLATEXPORTS\FORCE', false );
define ( 'WP_FLATEXPORTS\mdfile', 'item.md' ); define ( 'WP_FLATEXPORTS\ROOT', \WP_CONTENT_DIR . DIRECTORY_SEPARATOR
define ( 'WP_FLATEXPORTS\htmlfile', 'index.html' ); . 'flat' . DIRECTORY_SEPARATOR );
//define ( 'WP_FLATEXPORTS\pandocfile', 'content.asciidoc' ); define ( 'WP_FLATEXPORTS\FLATROOT', ROOT . 'posts'
define ( 'WP_FLATEXPORTS\maxattachments', 100 ); . DIRECTORY_SEPARATOR );
define ( 'WP_FLATEXPORTS\expire', 10 ); define ( 'WP_FLATEXPORTS\FILESROOT', ROOT . 'files'
define ( 'WP_FLATEXPORTS\wrap', 80 ); . DIRECTORY_SEPARATOR );
define ( 'WP_FLATEXPORTS\COMMENTROOT', ROOT . 'comments'
. DIRECTORY_SEPARATOR );
define ( 'WP_FLATEXPORTS\TXTFILE', 'index.txt' );
define ( 'WP_FLATEXPORTS\MDFILE', 'index.md' );
//define ( 'WP_FLATEXPORTS\HTMLFILE', 'index.html' );
\register_activation_hook( __FILE__ , '\WP_FLATEXPORTS\plugin_activate' ); \register_activation_hook( __FILE__ , '\WP_FLATEXPORTS\plugin_activate' );
\register_deactivation_hook( __FILE__ , '\WP_FLATEXPORTS\plugin_deactivate' );
// init all the things // init all the things
\add_action( 'init', '\WP_FLATEXPORTS\init' ); \add_action( 'init', '\WP_FLATEXPORTS\init' );
@ -53,28 +59,49 @@ define ( 'WP_FLATEXPORTS\wrap', 80 );
\add_action( 'template_redirect', '\WP_FLATEXPORTS\display' ); \add_action( 'template_redirect', '\WP_FLATEXPORTS\display' );
// //
\add_action( 'wp', '\WP_FLATEXPORTS\export' ); //\add_action( 'wp', '\WP_FLATEXPORTS\export' );
// this is to capture the complete, rendered HTML // this is to capture the complete, rendered HTML
// fired on post visit, sadly; for WordPress, there seems to be no other way // fired on post visit, sadly; for WordPress, there seems to be no other way
// to properly trigger this // to properly trigger this
export_html_init(); //export_html_init();
function post_filename ( &$post, $ext = TXTFILE ) {
$timestamp = \get_the_time( 'U', $post->ID );
$date = date( 'Y-m-d', $timestamp );
if ( empty( $date ) )
die ( json_encode( $post ) );
//$dir = FLATROOT . $date . '-' . $post->post_name;
////$dir = FLATROOT . $post->post_name;
//if ( ! is_dir( $dir ) )
//if ( ! mkdir( $dir ) )
//die ( "could not create {$dir} - that is bad, so we die now." );
//touch ( $dir, $timestamp );
//return $dir . DIRECTORY_SEPARATOR . $ext;
return FLATROOT . $date . '-' . $post->post_name . '.md';
}
/** /**
* *
*/ *
function export_html_init( ) { function export_html_init( ) {
ob_start( '\WP_FLATEXPORTS\export_html' ); ob_start( '\WP_FLATEXPORTS\export_html' );
} }
*/
/** /**
* *
*/ *
function export_html( $buffer ) { function export_html( $buffer ) {
$buffer = trim($buffer); $buffer = trim($buffer);
// skipping all collector pages and avoid rendering wp-admin-bar by skipping
// logged in users
if ( ! is_singular() || is_user_logged_in() ) if ( ! is_singular() || is_user_logged_in() )
return $buffer; return $buffer;
@ -83,32 +110,25 @@ function export_html( $buffer ) {
if ( $post === false ) if ( $post === false )
return $buffer; return $buffer;
// create directory structure $f = post_filename( $post, HTMLFILE );
$filename = $post->post_name;
$flatdir = flatroot . DIRECTORY_SEPARATOR . $filename;
$flatfile = $flatdir . DIRECTORY_SEPARATOR . htmlfile;
if ( ! is_dir( $flatdir ) )
return $buffer;
$post_timestamp = \get_the_modified_time( 'U', $post->ID ); $post_timestamp = \get_the_modified_time( 'U', $post->ID );
$file_timestamp = 0; $file_timestamp = 0;
if ( @file_exists($flatfile) ) { if ( @file_exists( $f ) ) {
$file_timestamp = @filemtime ( $flatfile ); $file_timestamp = @filemtime ( $f );
if ( $file_timestamp == $post_timestamp && force == false ) { if ( $file_timestamp == $post_timestamp && FORCE == false ) {
return $buffer; return $buffer;
} }
} }
//$buffer = post_content_clean_uploaddir( $buffer, $post ); //$buffer = post_content_clean_uploaddir( $buffer, $post );
file_put_contents( $flatfile, $buffer ); file_put_contents( $f, $buffer );
touch ( $flatfile, $post_timestamp ); touch ( $f, $post_timestamp );
return trim($buffer); return trim($buffer);
} }
*/
/** /**
* activate hook * activate hook
@ -118,6 +138,23 @@ function plugin_activate() {
die( 'The minimum PHP version required for this plugin is 5.3' ); die( 'The minimum PHP version required for this plugin is 5.3' );
} }
$dirs = [ FLATROOT, FILESROOT ];
foreach ( $dirs as $dir ) {
$dir = rtrim( $dir, '/' );
if ( ! is_dir( $dir ) ) {
if ( ! mkdir( $dir ) ) {
die ( "Could not create " . $dir . "directory" );
}
}
}
}
/**
*
*/
function plugin_deactivate() {
wp_unschedule_event( time(), 'wp_flatexport' );
wp_clear_scheduled_hook( 'wp_flatexport' );
} }
/** /**
@ -128,7 +165,7 @@ function init () {
$filters = array ( $filters = array (
'wp_flatexport_md' => array ( 'wp_flatexport_md' => array (
'md_insert_meta', 'md_insert_meta',
'txt_insert_excerpt', //'txt_insert_excerpt',
'txt_insert_content', 'txt_insert_content',
), ),
'wp_flatexport_txt' => array ( 'wp_flatexport_txt' => array (
@ -140,6 +177,7 @@ function init () {
'txt_insert_author', 'txt_insert_author',
'txt_insert_tags', 'txt_insert_tags',
'txt_insert_location', 'txt_insert_location',
'txt_insert_attachments',
'txt_insert_uuid', 'txt_insert_uuid',
), ),
'wp_flatexport_content' => array ( 'wp_flatexport_content' => array (
@ -155,14 +193,15 @@ function init () {
'post_content_setext_headers', 'post_content_setext_headers',
//'post_content_urls', //'post_content_urls',
), ),
'wp_flatexport_meta' => array ( //'wp_flatexport_meta' => array (
'meta_add_location', //'meta_add_location',
), //),
'wp_flatexport_comment' => array ( 'wp_flatexport_comment' => array (
'comment_insert_type', 'comment_insert_type',
'comment_insert_content', 'comment_insert_content',
'comment_insert_from',
'comment_insert_at', 'comment_insert_at',
'comment_insert_from',
'comment_insert_for',
), ),
); );
@ -171,7 +210,7 @@ function init () {
\add_filter ( \add_filter (
$for, $for,
"\\WP_FLATEXPORTS\\{$filter}", "\\WP_FLATEXPORTS\\{$filter}",
10 * ( $k + 1 ), 5 * ( $k + 1 ), // this will let other steps to be added
2 2
); );
} }
@ -181,6 +220,8 @@ function init () {
if (!wp_get_schedule( 'wp_flatexport' )) if (!wp_get_schedule( 'wp_flatexport' ))
wp_schedule_event ( time(), 'daily', 'wp_flatexport' ); wp_schedule_event ( time(), 'daily', 'wp_flatexport' );
} }
/** /**
@ -206,9 +247,9 @@ function depthmap () {
return array ( return array (
1 => "=", // asciidoc, restuctured text, and markdown compatible 1 => "=", // asciidoc, restuctured text, and markdown compatible
2 => "-", // asciidoc, restuctured text, and markdown compatible 2 => "-", // asciidoc, restuctured text, and markdown compatible
3 => "~", // asciidoc only //3 => "~", // asciidoc only
4 => "^", // asciidoc only //4 => "^", // asciidoc only
5 => "+", // asciidoc only //5 => "+", // asciidoc only
); );
} }
@ -218,14 +259,17 @@ function depthmap () {
function _insert_head ( $title, $depth = 2 ) { function _insert_head ( $title, $depth = 2 ) {
if ( $depth > 2 ) { if ( $depth > 2 ) {
$prefix = str_repeat( "#", $depth ); $prefix = str_repeat( "#", $depth );
$r = "\n\n{$prefix} {$title}\n"; $r = "\n\n{$prefix} {$title}";
} }
else { else {
$map = depthmap(); $map = depthmap();
$underline = str_repeat( $map[ $depth ], mb_strlen( $title) ); $underline = str_repeat( $map[ $depth ], mb_strlen( $title) );
$r = "\n\n{$title}\n${underline}\n"; $r = "\n\n{$title}\n${underline}";
} }
if ( $depth > 1 )
$r .= "\n";
return $r; return $r;
} }
@ -281,7 +325,46 @@ function txt_insert_uuid ( $text, $post ) {
* *
* extends the $text with * extends the $text with
* *
* \n\n (post excerpt) * Attachments
* -----------
*
*
*
*/
function txt_insert_attachments ( $text, $post ) {
// get all the attachments
$attachments = \get_children( array (
'post_parent'=>$post->ID,
'post_type'=>'attachment',
'orderby'=>'menu_order',
'order'=>'asc'
));
if ( empty( $attachments ) )
return $text;
$text .= _insert_head( "Attachments" );
$a = array();
foreach ( $attachments as $aid => $attachment ) {
$attachment_path = \get_attached_file( $aid );
if ( empty( $attachment_path ) || ! is_file( $attachment_path ) )
continue;
array_push( $a, "- " . basename( $attachment_path ) );
}
$text .= join( "\n", $a );
return $text;
}
/**
*
* extends the $text with
*
* \n (post excerpt)
*/ */
function txt_insert_excerpt ( $text, $post ) { function txt_insert_excerpt ( $text, $post ) {
@ -298,7 +381,7 @@ function txt_insert_excerpt ( $text, $post ) {
* *
* extends the $text with * extends the $text with
* *
* \n\n (post content) * \n (post content)
*/ */
function txt_insert_content ( $text, $post ) { function txt_insert_content ( $text, $post ) {
$content = apply_filters( $content = apply_filters(
@ -319,8 +402,8 @@ function txt_insert_content ( $text, $post ) {
* *
* Published * Published
* --------- * ---------
* initial - (post publish date in Y-m-d H:i:s P format) * - (post publish date in Y-m-d H:i:s P format)
* [current - (post last update date in Y-m-d H:i:s P format)] * [- (post last update date in Y-m-d H:i:s P format)]
*/ */
function txt_insert_published ( $text, $post ) { function txt_insert_published ( $text, $post ) {
@ -363,7 +446,10 @@ function txt_insert_urls ( $text, $post ) {
return $text; return $text;
} }
/**
* get all urls that are pointing to this very post, including syndications
*
*/
function list_urls ( $post ) { function list_urls ( $post ) {
// basic ones // basic ones
@ -373,7 +459,7 @@ function list_urls ( $post ) {
// eliminate revisions // eliminate revisions
foreach ( $slugs as $k => $slug ) { foreach ( $slugs as $k => $slug ) {
if ( preg_match ( '/-revision-v[0-9]+/', $slug ) ) { if ( preg_match ( '/-(revision|autosave)-v?[0-9]+/', $slug ) ) {
unset ( $slugs[ $k ] ); unset ( $slugs[ $k ] );
continue; continue;
} }
@ -424,8 +510,7 @@ function list_urls ( $post ) {
* Author * Author
* ------ * ------
* Author Display Name [<author@email>] * Author Display Name [<author@email>]
* avatar URL * author URLs
* [ author URL ]
*/ */
function txt_insert_author ( $text, $post ) { function txt_insert_author ( $text, $post ) {
@ -438,6 +523,7 @@ function txt_insert_author ( $text, $post ) {
if ( $author_email = \get_the_author_meta ( 'email' , $author_id ) ) if ( $author_email = \get_the_author_meta ( 'email' , $author_id ) )
$author .= " <{$author_email}>"; $author .= " <{$author_email}>";
/*
$thid = get_user_option ( 'metronet_image_id', $author_id ); $thid = get_user_option ( 'metronet_image_id', $author_id );
if ( $thid ) { if ( $thid ) {
$image = wp_get_attachment_image_src ( $thid, 'thumbnail' ); $image = wp_get_attachment_image_src ( $thid, 'thumbnail' );
@ -447,10 +533,12 @@ function txt_insert_author ( $text, $post ) {
$avatar = gravatar ( $author_email ); $avatar = gravatar ( $author_email );
} }
$author .= "\n${avatar}"; $author .= "\n${avatar}";
*/
if ( $author_url = \get_the_author_meta ( 'url' , $author_id ) ) if ( $author_url = \get_the_author_meta ( 'url' , $author_id ) )
$author .= " \n- {$author_url}"; $author .= "\n{$author_url}";
/*
$socials = array ( $socials = array (
'github' => 'https://github.com/%s', 'github' => 'https://github.com/%s',
'flickr' => 'https://www.flickr.com/people/%s', 'flickr' => 'https://www.flickr.com/people/%s',
@ -463,7 +551,7 @@ function txt_insert_author ( $text, $post ) {
if ( !empty($socialmeta) ) if ( !empty($socialmeta) )
$author .= "\n- " . sprintf ( $pattern, $socialmeta ); $author .= "\n- " . sprintf ( $pattern, $socialmeta );
} }
*/
$text .= _insert_head ( "Author" ); $text .= _insert_head ( "Author" );
$text .= "{$author}"; $text .= "{$author}";
@ -523,7 +611,7 @@ function txt_insert_location ( $text, $post ) {
$alt = \get_post_meta ( $post->ID, 'geo_altitude' , true ); $alt = \get_post_meta ( $post->ID, 'geo_altitude' , true );
if ( !empty( $alt ) ) if ( !empty( $alt ) )
$geo .= "@{$alt}"; $geo .= ",{$alt}";
$text .= _insert_head ( "Location" ); $text .= _insert_head ( "Location" );
@ -532,6 +620,86 @@ function txt_insert_location ( $text, $post ) {
return $text; return $text;
} }
/**
*
*
function md_insert_meta ( $text, $post ) {
$author_id = $post->post_author;
$raw_tags = \wp_get_post_terms( $post->ID, 'post_tag' );
$tags = array();
foreach ( $raw_tags as $k => $tag ) {
array_push( $tags, $tag->name );
}
array_unique( $tags );
$aliases = list_urls( $post );
$aliases_ = array();
foreach ( $aliases as $k => $alias ) {
$alias = str_replace( rtrim( site_url(), '/' ), '', $alias );
if ( trim( $alias, '/' ) != $post->post_name )
array_push( $aliases_, $alias );
}
$attachments = \get_children( array (
'post_parent'=>$post->ID,
'post_type'=>'attachment',
'orderby'=>'menu_order',
'order'=>'asc'
));
$a = array();
foreach ( $attachments as $aid => $attachment ) {
$attachment_path = \get_attached_file( $aid );
if ( ! empty( $attachment_path ) && is_file( $attachment_path ) )
array_push( $a, basename( $attachment_path ) );
}
$meta = [
'author' => [
'name' => \get_the_author_meta ( 'display_name' , $author_id ),
'email' => \get_the_author_meta ( 'email' , $author_id ),
'URL' => $author_url = \get_the_author_meta ( 'url' , $author_id ),
],
'date' => \get_the_time( 'Y-m-d H:i:s P', $post->ID ),
'tags' => $tags,
'title' => $post->post_title,
'url' => $post->post_name,
'id' => $post->ID,
'aliases' => $aliases_,
'attachments' => $a,
'uuid' => hash ( 'md5',
(int)$post->ID + (int) get_post_time('U', true, $post->ID )
),
];
$published = \get_the_time( 'U', $post->ID );
$modified = \get_the_modified_time( 'U', $post->ID );
if ( $published != $modified && $modified > $published )
$meta['modified'] = date( 'Y-m-d H:i:s P', $modified );
// geo
$lat = \get_post_meta ( $post->ID, 'geo_latitude' , true );
$lon = \get_post_meta ( $post->ID, 'geo_longitude' , true );
$alt = \get_post_meta ( $post->ID, 'geo_altitude' , true );
if ( ! empty( $lat ) && empty( $lon ) ) {
$meta['location'] = [
'latitude' => $lat,
'longitude' => $lon,
];
if ( ! empty( $alt ) ) {
$meta['location']['altitude'] = $alt;
}
}
$meta = apply_filters( 'wp_flatexport_md_meta', $meta, $post, $text );
return "\n\n" . yaml_emit( $meta ) . $text;
}
*/
/** /**
* *
* extends the $c with * extends the $c with
@ -550,13 +718,13 @@ function comment_insert_from ( $c, $comment ) {
if ( ! empty( $comment->comment_author_email ) ) if ( ! empty( $comment->comment_author_email ) )
$c .= " <{$comment->comment_author_email}>"; $c .= " <{$comment->comment_author_email}>";
if ( $avatar = \get_comment_meta ($comment->comment_ID, "avatar", true)) //if ( $avatar = \get_comment_meta ($comment->comment_ID, "avatar", true))
$c .= "\n{$avatar}"; //$c .= "\n{$avatar}";
elseif ( ! empty( $comment->comment_author_email ) ) //elseif ( ! empty( $comment->comment_author_email ) )
$c .= "\n". gravatar ( $comment->comment_author_email ); //$c .= "\n". gravatar ( $comment->comment_author_email );
if ( ! empty( $comment->comment_author_url )) if ( ! empty( $comment->comment_author_url ))
$c .= "\n{$comment->comment_author_url}"; $c .= "\n- {$comment->comment_author_url}";
return $c; return $c;
} }
@ -579,6 +747,25 @@ function comment_insert_type ( $c, $comment ) {
return $c; return $c;
} }
/**
*
* extends the $text with
*
* For
* ---
* original post URL
*
*/
function comment_insert_for ( $c, $comment ) {
$c .= _insert_head( "For" );
$postid = $comment->comment_post_ID;
$url = get_permalink( $postid );
$c .= $url;
return $c;
}
/** /**
* *
* extends the $text with * extends the $text with
@ -771,29 +958,6 @@ function post_content_url2footnote ( $content, $post ) {
return $content; return $content;
} }
/**
* export with pandoc
*
*
function post_content_pandoc ( $content, $post ) {
$flatroot = \WP_CONTENT_DIR . DIRECTORY_SEPARATOR . basedir;
$flatdir = $flatroot . DIRECTORY_SEPARATOR . $post->post_name;
$pandoc = $flatdir . DIRECTORY_SEPARATOR . pandocfile;
$tmp = tempnam ( sys_get_temp_dir() , __NAMESPACE__ );
file_put_contents( $tmp, $post->post_content );
$cmd =
"/usr/bin/pandoc -p -f markdown_phpextra -t asciidoc -o {$pandoc} {$tmp}";
//exec( $cmd, $exif, $retval);
passthru ( $cmd );
unlink ( $tmp );
return $content;
}
*/
/** /**
* find markdown links and replace them with footnote versions * find markdown links and replace them with footnote versions
* *
@ -801,8 +965,6 @@ function post_content_pandoc ( $content, $post ) {
function post_content_fix_emstrong ( $content, $post ) { function post_content_fix_emstrong ( $content, $post ) {
// these regexes are borrowed from https://github.com/erusev/parsedown // these regexes are borrowed from https://github.com/erusev/parsedown
$invalid = array ( $invalid = array (
'strong' => array( 'strong' => array(
//'**' => '/[*]{2}((?:\\\\\*|[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s', //'**' => '/[*]{2}((?:\\\\\*|[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s',
@ -904,7 +1066,6 @@ function post_content_setext_headers ( $content, $post ) {
* *
*/ */
function post_content ( &$post ) { function post_content ( &$post ) {
return trim ( return trim (
apply_filters ( apply_filters (
'wp_flatexport_content', 'wp_flatexport_content',
@ -939,13 +1100,50 @@ function export_all () {
/** /**
* *
*/ */
function export_auto ( $new_status = null , $old_status = null, $post = null ) { function export_auto ( $new_status = null , $old_status = null,
$post = null ) {
if ( null === $new_status || null === $old_status || null === $post ) if ( null === $new_status || null === $old_status || null === $post )
return; return;
export ( $post ); export ( $post );
} }
function export_attachments( $attachments, $post ) {
// hardlink all the attachments; no need for copy
// unless you're on a filesystem that does not support hardlinks, then copy
foreach ( $attachments as $aid => $attachment ) {
$attachment_path = \get_attached_file( $aid );
if ( empty( $attachment_path ) || ! is_file( $attachment_path ) )
continue;
$attachment_file = basename( $attachment_path);
$target_file = FILESROOT . $attachment_file;
//$target_file = dirname( post_filename( $post ) ) . DIRECTORY_SEPARATOR . $attachment_file;
debug ( "exporting {$attachment_file}", 6 );
if ( is_file( $target_file ) ) {
debug ( "{$target_file} already exists", 7 );
continue;
}
if ( link( $attachment_path, $target_file ) ) {
debug ( "{$attachment_path} was hardlinked to {$target_file}", 7 );
continue;
}
else {
if ( copy( $attachment_path, $target_file ) ) {
debug ( "{$attachment_path} was copied to {$target_file}", 7 );
continue;
}
else {
debug( "could not link or copy '{$attachment_path}'"
. " to '{$target_file}'; saving attachment failed!", 4);
}
}
}
}
/** /**
* *
*/ */
@ -961,31 +1159,15 @@ function export ( $post = null ) {
return false; return false;
// create directory structure // create directory structure
$filename = $post->post_name; $flatfile = post_filename( $post );
$flatdir = \flatroot . DIRECTORY_SEPARATOR . $filename; $post_timestamp = \get_the_time( 'U', $post->ID );
$flatfile = $flatdir . DIRECTORY_SEPARATOR . txtfile;
$mdfile = $flatdir . DIRECTORY_SEPARATOR . mdfile;
$post_timestamp = \get_the_modified_time( 'U', $post->ID );
$file_timestamp = 0; $file_timestamp = 0;
if ( @file_exists($flatfile) ) { if ( @file_exists($flatfile) ) {
$file_timestamp = @filemtime ( $flatfile ); $file_timestamp = @filemtime ( $flatfile );
} }
$mkdir = array ( \flatroot, $flatdir );
foreach ( $mkdir as $dir ) {
if ( !is_dir($dir)) {
if (!mkdir( $dir )) {
debug ('Failed to create ' . $dir . ', exiting export', 4);
return false;
}
}
}
touch($flatdir, $post_timestamp);
// get all the attachments // get all the attachments
$attachments = \get_children( array ( $attachments = \get_children( array (
'post_parent'=>$post->ID, 'post_parent'=>$post->ID,
@ -994,33 +1176,8 @@ function export ( $post = null ) {
'order'=>'asc' 'order'=>'asc'
)); ));
// 100 is there for sanity if ( ! empty( $attachments ) ) {
// hardlink all the attachments; no need for copy export_attachments( $attachments, $post );
// unless you're on a filesystem that does not support hardlinks
if ( !empty($attachments) && count($attachments) < maxattachments ) {
$out['attachments'] = array();
foreach ( $attachments as $aid => $attachment ) {
$attachment_path = \get_attached_file( $aid );
$attachment_file = basename( $attachment_path);
$target_file = $flatdir . DIRECTORY_SEPARATOR . $attachment_file;
debug ( "exporting {$attachment_file} for {$post->post_name}", 7 );
if ( is_file( $target_file ) )
continue;
if ( link( $attachment_path, $target_file ) )
continue;
else
debug( "could not hardlink '{$attachment_path}'"
. " to '{$target_file}'; trying to copy", 5);
if ( copy( $attachment_path, $target_file ) )
continue;
else
debug( "could not copy '{$attachment_path}'"
. " to '{$target_file}'; saving attachment failed!", 4);
}
} }
// deal with comments // deal with comments
@ -1033,23 +1190,20 @@ function export ( $post = null ) {
// in case our export is fresh or we're not forcing updates on each and // in case our export is fresh or we're not forcing updates on each and
// every time, walk away from this post // every time, walk away from this post
if ( $file_timestamp == $post_timestamp && force == false ) { if ( $file_timestamp == $post_timestamp && FORCE == false ) {
return true; return true;
} }
$txt = trim ( apply_filters ( 'wp_flatexport_txt', "", $post ) ) . "\n\n"; $txt = trim ( apply_filters ( 'wp_flatexport_txt', "", $post ) ) . "\n\n";
//$txt = trim ( apply_filters ( 'wp_flatexport_md', "", $post ) ) . "\n\n";
// write log // write log
debug ( "Exporting #{$post->ID}, {$post->post_name} to {$flatfile}", 6 ); debug ( "Exporting #{$post->ID}, {$post->post_name} to {$flatfile}", 6 );
file_put_contents ($flatfile, $txt); file_put_contents ($flatfile, $txt);
touch ( $flatfile, $post_timestamp ); touch ( $flatfile, $post_timestamp );
//$md = trim ( apply_filters ( 'wp_flatexport_md', "", $post ) ); touch ( dirname( $flatfile), $post_timestamp );
// write log
//debug ( "Exporting #{$post->ID}, {$post->post_name} to {$mdfile}", 6 );
//file_put_contents ($mdfile, $md);
//touch ( $mdfile, $post_timestamp );
return $txt; return $txt;
} }
@ -1058,21 +1212,19 @@ function export ( $post = null ) {
* *
*/ */
function export_comment ( $post, $comment ) { function export_comment ( $post, $comment ) {
$filename = $post->post_name; $flatdir = dirname( post_filename( $post ) );
$flatdir = flatroot . DIRECTORY_SEPARATOR . $filename;
$cfile = "comment_{$comment->comment_ID}.txt"; $c_timestamp = strtotime( $comment->comment_date );
$cfile = $flatdir . DIRECTORY_SEPARATOR . $cfile; $cfile = date( 'Y-m-d-H-i-s', $c_timestamp ) . '.md';
$cfile = COMMENTROOT . $cfile;
$cf_timestamp = 0; $cf_timestamp = 0;
$c_timestamp = strtotime( $comment->comment_date );
if ( @file_exists($cfile) ) { if ( @file_exists($cfile) ) {
$cf_timestamp = @filemtime ( $cfile ); $cf_timestamp = @filemtime ( $cfile );
} }
// non force mode means skip existing // non force mode means skip existing
if ( $c_timestamp == $cf_timestamp && force == false ) { if ( $c_timestamp == $cf_timestamp && FORCE == false ) {
return; return;
} }
@ -1083,58 +1235,6 @@ function export_comment ( $post, $comment ) {
touch ( $cfile, $c_timestamp ); touch ( $cfile, $c_timestamp );
} }
function meta_add_location ( $meta, $post ) {
return $meta;
}
/**
*
*/
function md_insert_meta ( $md, $post ) {
if ( ! extension_loaded( 'yaml') || ! function_exists( 'yaml_emit') )
return $md;
$meta = [];
$tags = \wp_get_post_terms( $post->ID, 'post_tag' );
foreach ( $tags as $k => $tag ) {
$tags[ $k ] = "{$tag->name}";
}
$urls = list_urls( $post );
$permalink = \get_permalink( $post );
foreach ( $urls as $k => $url ) {
if ( ! strstr( $url, site_url() ) )
unset ( $urls[ $k ] );
if ( strstr ( $permalink, $url ) )
unset ( $urls[ $k ] );
$urls[ $k ] = str_replace ( rtrim( site_url() . '/' ), '/', $url );
}
$meta = [
'title' => $post->post_title,
'description' => strip_tags($post->post_excerpt),
'publish_date' => \get_the_time( 'Y-m-d H:i:s P', $post->ID ),
'tags' => $tags,
'slug' => $post->post_name,
'url' => $permalink,
'published' => 'true',
'routes' => [
'canonical' => str_replace ( rtrim( site_url() . '/' ), '/', $permalink ),
'aliases' => $urls
]
];
$meta = apply_filters( 'wp_flatexport_meta', $meta, $post );
$meta = yaml_emit( $meta );
return "{$meta}{$md}";
}
/** /**
* generate gravatar img link * generate gravatar img link
*/ */
@ -1145,39 +1245,6 @@ function gravatar ( $email ) {
); );
} }
/**
*
*
function on_publish( $new_status, $old_status, $post ) {
$post = fix_post ( $post );
if ( false === $post )
return false;
$content = export( $post );
if ( $post->post_content_filtered == $export )
return true;
global $wpdb;
$dbname = "{$wpdb->prefix}posts";
$req = false;
debug("Updating post content for #{$post->ID}", 5);
$q = $wpdb->prepare( "UPDATE `{$dbname}` SET `post_content_filtered`='%s' WHERE `ID`='{$post->ID}'", $content );
try {
$req = $wpdb->query( $q );
}
catch (Exception $e) {
debug('Something went wrong: ' . $e->getMessage(), 4);
}
return true;
}
*/
/** /**
* do everything to get the Post object * do everything to get the Post object
*/ */