Export filtering. Props duck_. fixes #15197

git-svn-id: http://svn.automattic.com/wordpress/trunk@16652 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2010-12-01 17:21:58 +00:00
parent 68f7ed2701
commit ce490df051
5 changed files with 211 additions and 49 deletions

File diff suppressed because one or more lines are too long

View File

@ -2517,6 +2517,7 @@ div.tabs-panel-inactive {
#front-page-warning,
#front-static-pages ul,
ul.export-filters,
.inline-editor ul.cat-checklist ul,
.categorydiv ul.categorychecklist ul,
.customlinkdiv ul.categorychecklist ul,

View File

@ -16,7 +16,7 @@ if ( !current_user_can('export') )
require_once('./includes/export.php');
$title = __('Export');
add_contextual_help($current_screen,
add_contextual_help( $current_screen,
'<p>' . __('You can export a file of your site&#8217;s content in order to import it into another installation or platform. The export file will be an XML file format called WXR. Posts, pages, comments, custom fields, categories, and tags can be included. You can set filters to have the WXR file only include a certain date, author, category, tag, all posts or all pages, certain publishing statuses.') . '</p>' .
'<p>' . __('Once generated, your WXR file can be imported by another WordPress site or by another blogging platform able to access this format.') . '</p>' .
'<p><strong>' . __('For more information:') . '</strong></p>' .
@ -25,24 +25,71 @@ add_contextual_help($current_screen,
);
if ( isset( $_GET['download'] ) ) {
export_wp();
$args = array();
if ( ! isset( $_GET['content'] ) || 'all' == $_GET['content'] ) {
$args['content'] = 'all';
} else if ( 'posts' == $_GET['content'] ) {
$args['content'] = 'post';
if ( $_GET['cat'] )
$args['category'] = (int) $_GET['cat'];
if ( $_GET['post_author'] )
$args['author'] = (int) $_GET['post_author'];
if ( $_GET['post_start_date'] || $_GET['post_end_date'] ) {
$args['start_date'] = $_GET['post_start_date'];
$args['end_date'] = $_GET['post_end_date'];
}
if ( $_GET['post_status'] )
$args['status'] = $_GET['post_status'];
} else if ( 'pages' == $_GET['content'] ) {
$args['content'] = 'page';
if ( $_GET['page_author'] )
$args['author'] = (int) $_GET['page_author'];
if ( $_GET['page_start_date'] || $_GET['page_end_date'] ) {
$args['start_date'] = $_GET['page_start_date'];
$args['end_date'] = $_GET['page_end_date'];
}
if ( $_GET['page_status'] )
$args['status'] = $_GET['page_status'];
} else {
$args['content'] = $_GET['content'];
}
export_wp( $args );
die();
}
require_once ('admin-header.php');
$dateoptions = $edateoptions = '';
$types = "'" . implode("', '", get_post_types( array( 'public' => true, 'can_export' => true ), 'names' )) . "'";
$stati = "'" . implode("', '", get_post_stati( array( 'internal' => false ), 'names' )) . "'";
if ( $monthyears = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, YEAR(DATE_ADD(post_date, INTERVAL 1 MONTH)) AS `eyear`, MONTH(DATE_ADD(post_date, INTERVAL 1 MONTH)) AS `emonth` FROM $wpdb->posts WHERE post_type IN ($types) AND post_status IN ($stati) ORDER BY post_date ASC ") ) {
foreach ( $monthyears as $k => $monthyear )
$monthyears[$k]->lmonth = $wp_locale->get_month( $monthyear->month, 2 );
for( $s = 0, $e = count( $monthyears ) - 1; $e >= 0; $s++, $e-- ) {
$dateoptions .= "\t<option value=\"" . $monthyears[$s]->year . '-' . zeroise( $monthyears[$s]->month, 2 ) . '">' . $monthyears[$s]->lmonth . ' ' . $monthyears[$s]->year . "</option>\n";
$edateoptions .= "\t<option value=\"" . $monthyears[$e]->eyear . '-' . zeroise( $monthyears[$e]->emonth, 2 ) . '">' . $monthyears[$e]->lmonth . ' ' . $monthyears[$e]->year . "</option>\n";
function export_date_options() {
global $wpdb, $wp_locale;
$months = $wpdb->get_results( "
SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
FROM $wpdb->posts
WHERE post_type = 'post' AND post_status != 'auto-draft'
ORDER BY post_date DESC
" );
$month_count = count( $months );
if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
return;
foreach ( $months as $date ) {
if ( 0 == $date->year )
continue;
$month = zeroise( $date->month, 2 );
echo '<option value="' . $date->year . '-' . $month . '" />' . $wp_locale->get_month( $month ) . ' ' . $date->year . '</option>';
}
}
?>
<div class="wrap">
@ -51,16 +98,81 @@ if ( $monthyears = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`
<p><?php _e('When you click the button below WordPress will create an XML file for you to save to your computer.'); ?></p>
<p><?php _e('This format, which we call WordPress eXtended RSS or WXR, will contain your posts, pages, comments, custom fields, categories, and tags.'); ?></p>
<p><?php _e('Once you&#8217;ve saved the download file, you can use the Import function on another WordPress site to import this site.'); ?></p>
<form action="" method="get">
<?php submit_button( __('Download Export File'), 'button' ); ?>
<p><?php _e('Once you&#8217;ve saved the download file, you can use the Import function in another WordPress installation to import this site.'); ?></p>
<h3><?php _e( 'Choose what to export' ); ?></h3>
<form action="" method="get" id="export-filters">
<input type="hidden" name="download" value="true" />
</p>
<p><label><input type="radio" name="content" value="all" checked="checked" /> <?php _e( 'All content' ); ?></label>
<span class="description"><?php _e( 'This will contain all of your posts, pages, comments, custom fields, terms, navigation menus and custom posts.' ); ?></span></p>
<p><label><input type="radio" name="content" value="posts" /> <?php _e( 'Posts' ); ?></label></p>
<ul id="post-filters" class="export-filters">
<li>
<label><?php _e( 'Categories:' ); ?></label>
<?php wp_dropdown_categories( array( 'show_option_all' => __('All') ) ); ?>
</li>
<li>
<label><?php _e( 'Authors:' ); ?></label>
<?php wp_dropdown_users( array( 'name' => 'post_author', 'multi' => true, 'show_option_all' => __('All') ) ); ?>
</li>
<li>
<label><?php _e( 'Date range:' ); ?></label>
<select name="post_start_date">
<option value="0"><?php _e( 'Start Date' ); ?></option>
<?php export_date_options(); ?>
</select>
<select name="post_end_date">
<option value="0"><?php _e( 'End Date' ); ?></option>
<?php export_date_options(); ?>
</select>
</li>
<li>
<label><?php _e( 'Status:' ); ?></label>
<select name="post_status">
<option value="0"><?php _e( 'All' ); ?></option>
<?php $post_stati = get_post_stati( array( 'internal' => false ), 'objects' );
foreach ( $post_stati as $status ) : ?>
<option value="<?php echo esc_attr( $status->name ); ?>"><?php echo esc_html( $status->label ); ?></option>
<?php endforeach; ?>
</select>
</li>
</ul>
<p><label><input type="radio" name="content" value="pages" /> <?php _e( 'Pages' ); ?></label></p>
<ul id="page-filters" class="export-filters">
<li>
<label><?php _e( 'Author:' ); ?></label>
<?php wp_dropdown_users( array( 'name' => 'page_author', 'multi' => true, 'show_option_all' => __('All') ) ); ?>
</li>
<li>
<label><?php _e( 'Date range:' ); ?></label>
<select name="page_start_date">
<option value="0"><?php _e( 'Start Date' ); ?></option>
<?php export_date_options(); ?>
</select>
<select name="page_end_date">
<option value="0"><?php _e( 'End Date' ); ?></option>
<?php export_date_options(); ?>
</select>
</li>
<li>
<label><?php _e( 'Status:' ); ?></label>
<select name="page_status">
<option value="0"><?php _e( 'All' ); ?></option>
<?php foreach ( $post_stati as $status ) : ?>
<option value="<?php echo esc_attr( $status->name ); ?>"><?php echo esc_html( $status->label ); ?></option>
<?php endforeach; ?>
</select>
</li>
</ul>
<?php foreach ( get_post_types( array( '_builtin' => false, 'can_export' => true ), 'objects' ) as $post_type ) : ?>
<p><label><input type="radio" name="content" value="<?php echo esc_attr( $post_type->name ); ?>" /> <?php echo esc_html( $post_type->label ); ?></label></p>
<?php endforeach; ?>
<?php submit_button( __('Download Export File'), 'secondary' ); ?>
</form>
</div>
<?php
include ('admin-footer.php');
?>
<?php include('admin-footer.php'); ?>

View File

@ -25,6 +25,11 @@ define( 'WXR_VERSION', '1.1' );
function export_wp( $args = array() ) {
global $wpdb, $post;
$defaults = array( 'content' => 'all', 'author' => false, 'category' => false,
'start_date' => false, 'end_date' => false, 'status' => false,
);
$args = wp_parse_args( $args, $defaults );
do_action( 'export_wp' );
$sitename = sanitize_key( get_bloginfo( 'name' ) );
@ -35,31 +40,75 @@ function export_wp( $args = array() ) {
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
// grab a snapshot of post IDs, just in case it changes during the export
$post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type != 'revision' AND post_status != 'auto-draft' ORDER BY post_date_gmt ASC" );
if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) {
$ptype = get_post_type_object( $args['content'] );
if ( ! $ptype->can_export )
$args['content'] = 'post';
$categories = (array) get_categories( array( 'get' => 'all' ) );
$tags = (array) get_tags( array( 'get' => 'all' ) );
$custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
$taxonomy_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
// put categories in order with no child going before its parent
$cats = array();
while ( $cat = array_shift( $categories ) ) {
if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
$cats[$cat->term_id] = $cat;
else
$categories[] = $cat;
$where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
} else {
$post_types = get_post_types( array( 'can_export' => true ) );
$esses = array_fill( 0, count($post_types), '%s' );
$where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (". implode(',',$esses) .")", $post_types );
}
// put terms in order with no child going before its parent
$terms = array();
while ( $t = array_shift( $taxonomy_terms ) ) {
if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
$terms[$t->term_id] = $t;
else
$taxonomy_terms[] = $t;
if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) )
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
else
$where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
$join = '';
if ( $args['category'] && 'post' == $args['content'] ) {
if ( $term = term_exists( $args['category'], 'category' ) ) {
$join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
$where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_id'] );
}
}
if ( 'post' == $args['content'] || 'page' == $args['content'] ) {
if ( $args['author'] )
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
if ( $args['start_date'] )
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) );
if ( $args['end_date'] )
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime('+1 month', strtotime($args['end_date'])) ) );
}
// grab a snapshot of post IDs, just in case it changes during the export
$post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
// get the requested terms ready, empty unless posts filtered by category or all content
$cats = $tags = $terms = array();
if ( isset( $term ) && $term ) {
$cat = get_term( $term['term_id'], 'category' );
$cats = array( $cat->term_id => $cat );
unset( $term, $cat );
} else if ( 'all' == $args['content'] ) {
$categories = (array) get_categories( array( 'get' => 'all' ) );
$tags = (array) get_tags( array( 'get' => 'all' ) );
$custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
$custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
// put categories in order with no child going before its parent
while ( $cat = array_shift( $categories ) ) {
if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
$cats[$cat->term_id] = $cat;
else
$categories[] = $cat;
}
// put terms in order with no child going before its parent
while ( $t = array_shift( $custom_terms ) ) {
if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
$terms[$t->term_id] = $t;
else
$custom_terms[] = $t;
}
unset( $categories, $custom_taxonomies, $custom_terms );
}
/**
@ -271,7 +320,7 @@ function export_wp( $args = array() ) {
<title><?php bloginfo_rss( 'name' ); ?></title>
<link><?php bloginfo_rss( 'url' ); ?></link>
<description><?php bloginfo_rss( 'description' ); ?></description>
<pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_lastpostmodified( 'GMT' ), false ); ?></pubDate>
<pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate>
<language><?php echo get_option( 'rss_language' ); ?></language>
<wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
<wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
@ -288,7 +337,7 @@ function export_wp( $args = array() ) {
<?php foreach ( $terms as $t ) : ?>
<wp:term><wp:term_id><?php echo $t->term_id ?></wp:term_id><wp:term_taxonomy><?php echo $t->taxonomy; ?></wp:term_taxonomy><wp:term_slug><?php echo $t->slug; ?></wp:term_slug><wp:term_parent><?php echo $t->parent ? $terms[$t->parent]->slug : ''; ?></wp:term_parent><?php wxr_term_name( $t ); ?><?php wxr_term_description( $t ); ?></wp:term>
<?php endforeach; ?>
<?php wxr_nav_menu_terms(); ?>
<?php if ( 'all' == $args['content'] ) wxr_nav_menu_terms(); ?>
<?php do_action( 'rss2_head' ); ?>
@ -299,7 +348,7 @@ function export_wp( $args = array() ) {
// fetch 20 posts at a time rather than loading the entire table into memory
while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
$where = "WHERE ID IN (" . join( ',', $next_posts ) . ")";
$posts = $wpdb->get_results( "SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC" );
$posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
// Begin Loop
foreach ( $posts as $post ) {
@ -332,7 +381,7 @@ function export_wp( $args = array() ) {
<?php endif; ?>
<?php wxr_post_taxonomy(); ?>
<?php $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
if ( $postmeta ) : foreach( $postmeta as $meta ) : if ( $meta->meta_key != '_edit_lock' && $meta->meta_key != '_edit_last' ) : ?>
if ( $postmeta ) : foreach( $postmeta as $meta ) : if ( $meta->meta_key != '_edit_lock' ) : ?>
<wp:postmeta>
<wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
<wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>

View File

@ -54,7 +54,6 @@ function create_initial_post_types() {
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'can_export' => false,
'show_in_nav_menus' => false,
) );
@ -71,6 +70,7 @@ function create_initial_post_types() {
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'can_export' => false,
) );
register_post_type( 'nav_menu_item', array(