switched to filter-based content inserting for the end output; this way it's possible to add, remove, reorder, and insert in between the predefined output parts

This commit is contained in:
Peter Molnar 2016-07-14 15:21:26 +00:00
parent 8f403a4954
commit ce59ca1929
2 changed files with 181 additions and 62 deletions

View file

@ -1,9 +1,9 @@
=== wp-flatexport === === wp-flatexport ===
Contributors: cadeyrn Contributors: cadeyrn
Tags: plain text, export, backup Tags: plain text, export, backup
Requires at least: 4.2 Requires at least: 3.0
Tested up to: 4.5.3 Tested up to: 4.5.3
Stable tag: 0.2 Stable tag: 0.3
License: GPLv3 License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html License URI: http://www.gnu.org/licenses/gpl-3.0.html
@ -42,6 +42,11 @@ Version numbering logic:
* every .B version indicates new features. * every .B version indicates new features.
* every ..C indicates bugfixes for A.B version. * every ..C indicates bugfixes for A.B version.
= 0.3 =
*2016-07-14*
* switched to filter-based content inserting for the end output; this way it's possible to add, remove, reorder, and insert in between the predefined output parts
= 0.2 = = 0.2 =
*2016-06-27* *2016-06-27*

View file

@ -3,11 +3,10 @@
Plugin Name: WP Flat Exports Plugin Name: WP Flat Exports
Plugin URI: https://github.com/petermolnar/wp-flatexport Plugin URI: https://github.com/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
Version: 0.2 Version: 0.3
Author: Peter Molnar <hello@petermolnar.eu> Author: Peter Molnar <hello@petermolnar.eu>
Author URI: http://petermolnar.eu/ Author URI: http://petermolnar.eu/
License: GPLv3 License: GPLv3
Required minimum PHP version: 5.4
*/ */
/* Copyright 2015 Peter Molnar ( hello@petermolnar.eu ) /* Copyright 2015 Peter Molnar ( hello@petermolnar.eu )
@ -32,9 +31,171 @@ define ( 'force', true );
define ( 'basedir', 'flat' ); define ( 'basedir', 'flat' );
define ( 'basefile', 'item.md' ); define ( 'basefile', 'item.md' );
define ( 'maxattachments', 100 ); define ( 'maxattachments', 100 );
define ( 'expire', 10 );
\register_activation_hook( __FILE__ , '\WP_FLATEXPORTS\plugin_activate' ); \register_activation_hook( __FILE__ , '\WP_FLATEXPORTS\plugin_activate' );
\add_action( 'wp_footer', '\WP_FLATEXPORTS\export' ); \add_action( 'wp_footer', '\WP_FLATEXPORTS\export' );
\add_action ( 'init', '\WP_FLATEXPORTS\init' );
/**
*
*/
function init () {
add_filter ( 'wp_flatexport_post', '\WP_FLATEXPORTS\insert_title', 10, 2 );
add_filter ( 'wp_flatexport_post', '\WP_FLATEXPORTS\insert_excerpt', 20, 2 );
add_filter ( 'wp_flatexport_post', '\WP_FLATEXPORTS\insert_content', 30, 2 );
add_filter ( 'wp_flatexport_post', '\WP_FLATEXPORTS\insert_published', 40, 2 );
add_filter ( 'wp_flatexport_post', '\WP_FLATEXPORTS\insert_urls', 50, 2 );
add_filter ( 'wp_flatexport_post', '\WP_FLATEXPORTS\insert_author', 60, 2 );
add_filter ( 'wp_flatexport_post', '\WP_FLATEXPORTS\insert_tags', 80, 2 );
add_filter ( 'wp_flatexport_post', '\WP_FLATEXPORTS\insert_location', 70, 2 );
}
function check_insert ( $post ) {
$post = fix_post( $post );
if ( false === $post )
return false;
$postdata = raw_post_data( $post );
if ( empty( $postdata ) )
return false;
return $postdata;
}
function insert_title ( $text, $post ) {
$postdata = check_insert ( $post );
if ( false === $postdata )
return $text;
if ( ! isset( $postdata['title'] ) || empty( $postdata['title'] ) )
return $text;
$text .= "\n\n{$postdata['title']}\n";
$text .= str_repeat( "=", strlen( $postdata['title'] ) );
return $text;
}
function insert_excerpt ( $text, $post ) {
$postdata = check_insert ( $post );
if ( false === $postdata )
return $text;
if ( ! isset( $postdata['excerpt'] ) || empty( $postdata['excerpt'] ) )
return $text;
$text .= "\n\n" . $postdata['excerpt'];
return $text;
}
function insert_content ( $text, $post ) {
$postdata = check_insert ( $post );
if ( false === $postdata )
return $text;
$text .= "\n\n" . $postdata['content'];
return $text;
}
function insert_published ( $text, $post ) {
$postdata = check_insert ( $post );
if ( false === $postdata )
return $text;
$text .= "\n\nPublished\n";
$text .= "---------\n";
$text .= "{$postdata['published']}\n\n";
if ( $postdata['published'] != $postdata['modified'] ) {
$text .= "Updated\n";
$text .= "-------\n";
$text .= "{$postdata['modified']}";
}
return $text;
}
function insert_urls ( $text, $post ) {
$postdata = check_insert ( $post );
if ( false === $postdata )
return $text;
$text .= "\n\nURLs\n";
$text .= "----\n";
$text .= "- <" . join ( ">\n- <", $postdata['urls'] ) . ">";
return $text;
}
function insert_author ( $text, $post ) {
$postdata = check_insert ( $post );
if ( false === $postdata )
return $text;
if ( ! isset( $postdata['author'] ) || empty( $postdata['author'] ) )
return $text;
$text .= "\n\nAuthor\n";
$text .= "------\n";
$text .= "{$postdata['author']}";
return $text;
}
function insert_tags ( $text, $post ) {
$postdata = check_insert ( $post );
if ( false === $postdata )
return $text;
if ( empty( $postdata['tags'] ) )
return $text;
$tags = array();
foreach ( $postdata['tags'] as $k => $tag ) {
array_push( $tags, "#{$tag->name}" );
}
$tags = join (', ', $tags);
$text .= "\n\nTags\n";
$text .= "----\n";
// these are hashtags, so escape the first one to avoid converting it into
// a header
$text .= "\\" . $tags;
return $text;
}
function insert_location ( $text, $post ) {
$postdata = check_insert ( $post );
if ( false === $postdata )
return $text;
if ( ! isset( $postdata['geo'] ) || empty( $postdata['geo'] ) )
return $text;
$text .= "\n\nLocation\n";
$text .= "--------\n";
$text .= "{$postdata['geo']}";
return $text;
}
/** /**
* activate hook * activate hook
@ -188,67 +349,13 @@ function plain_text_post ( $postid = false ) {
if ( false === $post ) if ( false === $post )
return false; return false;
$postdata = raw_post_data( $post );
if ( empty( $postdata ) )
return false;
$out = ""; $out = "";
if ( isset( $postdata['title'] ) && ! empty( $postdata['title'] ) ) { return trim ( apply_filters ( 'wp_flatexport_post', $out, $post ) );
$out .= "{$postdata['title']}\n";
// first level header
$out .= str_repeat( "=", strlen( $postdata['title'] ) ) . "\n\n";
}
if (isset($postdata['excerpt']) && !empty($postdata['excerpt']))
$out .= $postdata['excerpt'] . "\n\n";
$out .= $postdata['content'] . "\n\n";
$out .= "Published\n";
$out .= "---------\n";
$out .= "{$postdata['published']}\n\n";
if ( $postdata['published'] != $postdata['modified'] ) {
$out .= "Updated\n";
$out .= "-------\n";
$out .= "{$postdata['modified']}\n\n";
}
$out .= "URLs\n";
$out .= "----\n";
$out .= "- <" . join ( ">\n- <", $postdata['urls'] ) . ">\n\n";
if ( isset( $postdata['author'] ) && ! empty( $postdata['author'] ) ) {
$out .= "Author\n";
$out .= "------\n";
$out .= "{$postdata['author']}\n\n";
}
if ( isset( $postdata['geo'] ) && ! empty( $postdata['geo'] ) ) {
$out .= "Location\n";
$out .= "--------\n";
$out .= "{$postdata['geo']}\n\n";
} }
if ( ! empty( $postdata['tags'] ) ) {
$tags = array();
foreach ( $postdata['tags'] as $k => $tag ) {
array_push( $tags, "#{$tag->name}" );
}
$tags = join (', ', $tags);
$out .= "Tags\n";
$out .= "----\n";
// these are hashtags, so escape the first one to avoid converting it into
// a header
$out .= "\\" . $tags;
}
return apply_filters ( 'wp_flatexport_post', $out, $post );
}
/** /**
* *
@ -396,6 +503,10 @@ function raw_post_data ( &$post = null ) {
if ($post === false) if ($post === false)
return false; return false;
if ( $cached = wp_cache_get ( $post->ID, __NAMESPACE__ . __FUNCTION__ ) )
return $cached;
$content = post_content ( $post ); $content = post_content ( $post );
// excerpt // excerpt
@ -473,6 +584,9 @@ function raw_post_data ( &$post = null ) {
//'reactions' => meta_reaction( $post ), //'reactions' => meta_reaction( $post ),
); );
wp_cache_set ( $post->ID, $out, __NAMESPACE__ . __FUNCTION__, expire );
return $out; return $out;
} }