Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Advanced Export for WordPress & WordPress MU
- Plugin URI: http://wpmututorials.com/plugins/advanced-export/
- Description: Adds an Advanced Export to the Tools menu which allows selective exporting of pages, posts, specific categories and/or post statuses by date.
- Version: 2.8.3
- Author: Ron Rennick
- Author URI: http://ronandandrea.com/
- */
- /* Copyright: (C) 2009 Ron Rennick, All rights reserved.
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- /*mod jrc 300312 - modified to a) export attachments only, b) remove superfluous tag and cat post data when not exporting tags or cats - see mod marks: 'mod jrc 290312', 'mod jrc 300312' - (see also: http://wordpress.org/support/topic/plugin-advanced-export-for-wp-wpmu-how-to-export-attachments-only, http://wordpress.org/support/topic/plugin-advanced-export-for-wp-wpmu-eliminating-superfluous-cat-and-tag-records-from-posts-only-exports) - esp. useful for exporting and importing in multiple batches and for use with plugin http://wordpress.org/extend/plugins/wordpress-importer/ (modieid) (ess also: http://wordpress.org/support/topic/plugin-wordpress-importer-not-importing-any-images-at-all)*/
- if(!defined('ABSPATH')) {
- die("Don't call this file directly.");
- }
- if(isset($_GET['page']) && $_GET['page'] == 'ra_export' && isset( $_GET['download'] ) ) {
- add_action('init', 'ra_do_export');
- }
- function ra_do_export() {
- if(current_user_can('edit_files')) {
- $author = isset($_GET['author']) ? $_GET['author'] : 'all';
- $category = isset($_GET['category']) ? $_GET['category'] : 'all';
- $post_type = isset($_GET['post_type']) ? wp_specialchars($_GET['post_type']) : 'all';
- $status = isset($_GET['status']) ? wp_specialchars($_GET['status']) : 'all';
- $mm_start = isset($_GET['mm_start']) ? $_GET['mm_start'] : 'all';
- $mm_end = isset($_GET['mm_end']) ? $_GET['mm_end'] : 'all';
- $aa_start = isset($_GET['aa_start']) ? intval($_GET['aa_start']) : 0;
- $aa_end = isset($_GET['aa_end']) ? intval($_GET['aa_end']) : 0;
- $terms = isset($_GET['terms']) ? wp_specialchars($_GET['terms']) : 'all';
- if($mm_start != 'all' && $aa_start > 0) {
- $start_date = sprintf( "%04d-%02d-%02d", $aa_start, $mm_start, 1 );
- } else {
- $start_date = 'all';
- }
- if($mm_end != 'all' && $aa_end > 0) {
- if($mm_end == 12) {
- $mm_end = 1;
- $aa_end++;
- } else {
- $mm_end++;
- }
- $end_date = sprintf( "%04d-%02d-%02d", $aa_end, $mm_end, 1 );
- } else {
- $end_date = 'all';
- }
- ra_export_setup();
- ra_export_wp( $author, $category, $post_type, $status, $start_date, $end_date, $terms );
- die();
- }
- }
- function ra_export_wp($author='', $category='', $post_type='', $status='', $start_date='', $end_date='', $terms = '') {
- global $wpdb, $post_ids, $post;
- define('WXR_VERSION', '1.0');
- do_action('export_wp');
- if(strlen($start_date) > 4 && strlen($end_date) > 4) {
- $filename = 'wordpress.' . $start_date . '.' . $end_date . '.xml';
- } else {
- $filename = 'wordpress.' . date('Y-m-d') . '.xml';
- }
- header('Content-Description: File Transfer');
- header("Content-Disposition: attachment; filename=$filename");
- header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
- if ( $post_type and $post_type != 'all' ) {
- $where = $wpdb->prepare("WHERE post_type = %s ", $post_type);
- } else {
- $where = "WHERE post_type != 'revision' ";
- }
- if ( $author and $author != 'all' ) {
- $author_id = (int) $author;
- $where .= $wpdb->prepare("AND post_author = %d ", $author_id);
- }
- if ( $start_date and $start_date != 'all' ) {
- $where .= $wpdb->prepare("AND post_date >= %s ", $start_date);
- }
- if ( $end_date and $end_date != 'all' ) {
- $where .= $wpdb->prepare("AND post_date < %s ", $end_date);
- }
- if ( $category and $category != 'all' and version_compare($wpdb->db_version(), '4.1', 'ge')) {
- $taxomony_id = (int) $category;
- $where .= $wpdb->prepare("AND ID IN (SELECT object_id FROM {$wpdb->term_relationships} " .
- "WHERE term_taxonomy_id = %d) ", $taxomony_id);
- }
- if ( $status and $status != 'all' ) {
- $where .= $wpdb->prepare("AND post_status = %s ", $status);
- }
- // 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 ORDER BY post_date_gmt ASC");
- $categories = (array) get_categories('get=all');
- $tags = (array) get_tags('get=all');
- while ( $parents = wxr_missing_parents($categories) ) {
- $found_parents = get_categories("include=" . join(', ', $parents));
- if ( is_array($found_parents) && count($found_parents) )
- $categories = array_merge($categories, $found_parents);
- else
- break;
- }
- // Put them in order to be inserted with no child going before its parent
- $pass = 0;
- $passes = 1000 + count($categories);
- while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
- if ( $cat->parent == 0 || isset($cats[$cat->parent]) ) {
- $cats[$cat->term_id] = $cat;
- } else {
- $categories[] = $cat;
- }
- }
- unset($categories);
- echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
- ?>
- <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
- <!-- It contains information about your blog's posts, comments, and categories. -->
- <!-- You may use this file to transfer that content from one site to another. -->
- <!-- This file is not intended to serve as a complete backup of your blog. -->
- <!-- To import this information into a WordPress blog follow these steps. -->
- <!-- 1. Log into that blog as an administrator. -->
- <!-- 2. Go to Tools: Import in the blog's admin panels (or Manage: Import in older versions of WordPress). -->
- <!-- 3. Choose "WordPress" from the list. -->
- <!-- 4. Upload this file using the form provided on that page. -->
- <!-- 5. You will first be asked to map the authors in this export file to users -->
- <!-- on the blog. For each author, you may choose to map to an -->
- <!-- existing user on the blog or to create a new user -->
- <!-- 6. WordPress will then import each of the posts, comments, and categories -->
- <!-- contained in this file into your blog -->
- <?php the_generator('export');?>
- <rss version="2.0"
- xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
- xmlns:content="http://purl.org/rss/1.0/modules/content/"
- xmlns:wfw="http://wellformedweb.org/CommentAPI/"
- xmlns:dc="http://purl.org/dc/elements/1.1/"
- xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
- >
- <channel>
- <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>
- <generator>http://wordpress.org/?v=<?php bloginfo_rss('version'); ?></generator>
- <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>
- <wp:base_blog_url><?php bloginfo_rss('url'); ?></wp:base_blog_url>
- <?php if ( $cats && ($terms == 'all' || $terms == 'cats')) : foreach ( $cats as $c ) : ?>
- <wp:category><wp:category_nicename><?php echo $c->slug; ?></wp:category_nicename><wp:category_parent><?php echo $c->parent ? $cats[$c->parent]->name : ''; ?></wp:category_parent><?php wxr_cat_name($c); ?><?php wxr_category_description($c); ?></wp:category>
- <?php endforeach; endif; ?>
- <?php if ( $tags && ($terms == 'all' || $terms == 'tags')) : foreach ( $tags as $t ) : ?>
- <wp:tag><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name($t); ?><?php wxr_tag_description($t); ?></wp:tag>
- <?php endforeach; endif; ?>
- <?php do_action('rss2_head'); ?>
- <?php if ($post_ids) {
- global $wp_query;
- $wp_query->in_the_loop = true; // Fake being in the loop.
- // 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");
- foreach ($posts as $post) {
- setup_postdata($post); ?>
- <item>
- <title><?php echo apply_filters('the_title_rss', $post->post_title); ?></title>
- <link><?php the_permalink_rss() ?></link>
- <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
- <dc:creator><?php echo wxr_cdata(get_the_author()); ?></dc:creator>
- <?php /*mod jrc 290312 - wxr_post_taxonomy() */?>
- <?php scl_wxr_post_taxonomy($terms); ?>
- <?php /*end mod jrc 290312*/ ?>
- <guid isPermaLink="false"><?php the_guid(); ?></guid>
- <description></description>
- <content:encoded><?php echo wxr_cdata( apply_filters('the_content_export', $post->post_content) ); ?></content:encoded>
- <excerpt:encoded><?php echo wxr_cdata( apply_filters('the_excerpt_export', $post->post_excerpt) ); ?></excerpt:encoded>
- <wp:post_id><?php echo $post->ID; ?></wp:post_id>
- <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
- <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
- <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
- <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
- <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
- <wp:status><?php echo $post->post_status; ?></wp:status>
- <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
- <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
- <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
- <wp:post_password><?php echo $post->post_password; ?></wp:post_password>
- <?php
- if ($post->post_type == 'attachment') { ?>
- <wp:attachment_url><?php echo wp_get_attachment_url($post->ID); ?></wp:attachment_url>
- <?php } ?>
- <?php
- $postmeta = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID) );
- if ( $postmeta ) {
- ?>
- <?php foreach( $postmeta as $meta ) { ?>
- <wp:postmeta>
- <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
- <wp:meta_value><?Php echo $meta->meta_value; ?></wp:meta_value>
- </wp:postmeta>
- <?php } ?>
- <?php } ?>
- <?php
- $comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d", $post->ID) );
- if ( $comments ) { foreach ( $comments as $c ) { ?>
- <wp:comment>
- <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
- <wp:comment_author><?php echo wxr_cdata($c->comment_author); ?></wp:comment_author>
- <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
- <wp:comment_author_url><?php echo $c->comment_author_url; ?></wp:comment_author_url>
- <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
- <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
- <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
- <wp:comment_content><?php echo wxr_cdata($c->comment_content) ?></wp:comment_content>
- <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
- <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
- <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
- <wp:comment_user_id><?php echo $c->user_id; ?></wp:comment_user_id>
- </wp:comment>
- <?php } } ?>
- </item>
- <?php } } } ?>
- </channel>
- </rss>
- <?php
- }
- function ra_export_page() {
- global $wpdb, $wp_locale;
- if(!current_user_can('edit_files')) {
- die( 'You don\'t have permissions to use this page.' );
- }
- $months = "";
- for ( $i = 1; $i < 13; $i++ ) {
- $months .= "\t\t\t<option value=\"" . zeroise($i, 2) . '">' .
- $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . "</option>\n";
- } ?>
- <div class="wrap">
- <?php screen_icon(); ?>
- <h2><?php echo wp_specialchars( $title ); ?></h2>
- <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’ve saved the download file, you can use the Import function on another WordPress blog to import this blog.'); ?></p>
- <form action="" method="get">
- <input type="hidden" name="page" value="ra_export" />
- <h3><?php _e('Options'); ?></h3>
- <table class="form-table">
- <tr>
- <th><label for="mm_start"><?php _e('Restrict Date'); ?></label></th>
- <td><strong><?php _e('Start:'); ?></strong> <?php _e('Month'); ?>
- <select name="mm_start" id="mm_start">
- <option value="all" selected="selected"><?php _e('All Dates'); ?></option>
- <?php echo $months; ?>
- </select> <?php _e('Year'); ?>
- <input type="text" id="aa_start" name="aa_start" value="" size="4" maxlength="5" />
- </td>
- <td><strong><?php _e('End:'); ?></strong> <?php _e('Month'); ?>
- <select name="mm_end" id="mm_end">
- <option value="all" selected="selected"><?php _e('All Dates'); ?></option>
- <?php echo $months; ?>
- </select> <?php _e('Year'); ?>
- <input type="text" id="aa_end" name="aa_end" value="" size="4" maxlength="5" />
- </td>
- </tr>
- <tr>
- <th><label for="author"><?php _e('Restrict Author'); ?></label></th>
- <td>
- <select name="author" id="author">
- <option value="all" selected="selected"><?php _e('All Authors'); ?></option>
- <?php
- $authors = $wpdb->get_col( "SELECT post_author FROM $wpdb->posts GROUP BY post_author" );
- foreach ( $authors as $id ) {
- $o = get_userdata( $id );
- echo "<option value='{$o->ID}'>{$o->display_name}</option>\n";
- }
- ?>
- </select>
- </td>
- </tr>
- <?php if(version_compare($wpdb->db_version(), '4.1', 'ge')) { ?>
- <tr>
- <th><label for="category"><?php _e('Restrict Category'); ?></label></th>
- <td>
- <select name="category" id="category">
- <option value="all" selected="selected"><?php _e('All Categories'); ?></option>
- <?php
- $categories = (array) get_categories('get=all');
- if($categories) {
- foreach ( $categories as $cat ) {
- echo "<option value='{$cat->term_taxonomy_id}'>{$cat->name}</option>\n";
- }
- }
- ?>
- </select>
- </td>
- </tr>
- <?php } ?>
- <tr>
- <th><label for="post_type"><?php _e('Restrict Content'); ?></label></th>
- <td>
- <select name="post_type" id="post_type">
- <option value="all" selected="selected"><?php _e('All Content'); ?></option>
- <option value="page"><?php _e('Pages'); ?></option>
- <option value="post"><?php _e('Posts'); ?></option>
- <?php /*mod jrc 300312 - allow attachments only*/ ?>
- <option value="attachment"><?php _e('Attachments'); ?></option>
- <?php /*mod jrc 300312*/ ?>
- </select>
- </td>
- </tr>
- <tr>
- <th><label for="status"><?php _e('Restrict Status'); ?></label></th>
- <td>
- <select name="status" id="status">
- <option value="all" selected="selected"><?php _e('All Statuses'); ?></option>
- <option value="draft"><?php _e('Draft'); ?></option>
- <option value="private"><?php _e('Privately published'); ?></option>
- <option value="publish"><?php _e('Published'); ?></option>
- <option value="future"><?php _e('Scheduled'); ?></option>
- </select>
- </td>
- </tr>
- <tr>
- <th><label for="terms"><?php _e('Include Blog Tag/Category Terms'); ?></label></th>
- <td>
- <select name="terms" id="terms">
- <option value="all" selected="selected"><?php _e('All Terms'); ?></option>
- <option value="cats"><?php _e('Categories'); ?></option>
- <option value="tags"><?php _e('Tags'); ?></option>
- <option value="none"><?php _e('None'); ?></option>
- </select>
- </td>
- </tr>
- </table>
- <p class="submit"><input type="submit" name="submit" class="button" value="<?php _e('Download Export File'); ?>" />
- <input type="hidden" name="download" value="true" />
- </p>
- </form>
- </div>
- <?php
- }
- function ra_add_export_page() {
- add_management_page('Advanced Export', 'Advanced Export', 10, 'ra_export', 'ra_export_page');
- }
- add_action('admin_menu', 'ra_add_export_page');
- function ra_export_setup() {
- if(!function_exists('wxr_missing_parents')) {
- function wxr_missing_parents($categories) {
- if ( !is_array($categories) || empty($categories) )
- return array();
- foreach ( $categories as $category )
- $parents[$category->term_id] = $category->parent;
- $parents = array_unique(array_diff($parents, array_keys($parents)));
- if ( $zero = array_search('0', $parents) )
- unset($parents[$zero]);
- return $parents;
- }
- }
- if(!function_exists('wxr_cdata')) {
- function wxr_cdata($str) {
- if ( seems_utf8($str) == false )
- $str = utf8_encode($str);
- // $str = ent2ncr(wp_specialchars($str));
- $str = "<![CDATA[$str" . ( ( substr($str, -1) == ']' ) ? ' ' : '') . "]]>";
- return $str;
- }
- }
- if(!function_exists('wxr_site_url')) {
- function wxr_site_url() {
- global $current_site;
- // mu: the base url
- if ( isset($current_site->domain) ) {
- return 'http://'.$current_site->domain.$current_site->path;
- }
- // wp: the blog url
- else {
- return get_bloginfo_rss('url');
- }
- }
- }
- if(!function_exists('wxr_cat_name')) {
- function wxr_cat_name($c) {
- if ( empty($c->name) )
- return;
- echo '<wp:cat_name>' . wxr_cdata($c->name) . '</wp:cat_name>';
- }
- }
- if(!function_exists('wxr_category_description')) {
- function wxr_category_description($c) {
- if ( empty($c->description) )
- return;
- echo '<wp:category_description>' . wxr_cdata($c->description) . '</wp:category_description>';
- }
- }
- if(!function_exists('wxr_tag_name')) {
- function wxr_tag_name($t) {
- if ( empty($t->name) )
- return;
- echo '<wp:tag_name>' . wxr_cdata($t->name) . '</wp:tag_name>';
- }
- }
- if(!function_exists('wxr_tag_description')) {
- function wxr_tag_description($t) {
- if ( empty($t->description) )
- return;
- echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
- }
- }
- if(!function_exists('wxr_post_taxonomy')) {
- function wxr_post_taxonomy() {
- $categories = get_the_category();
- $tags = get_the_tags();
- $the_list = '';
- $filter = 'rss';
- if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
- $cat_name = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
- // for backwards compatibility
- $the_list .= "\n\t\t<category><![CDATA[$cat_name]]></category>\n";
- // forwards compatibility: use a unique identifier for each cat to avoid clashes
- // http://trac.wordpress.org/ticket/5447
- $the_list .= "\n\t\t<category domain=\"category\" nicename=\"{$category->slug}\"><![CDATA[$cat_name]]></category>\n";
- }
- if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
- $tag_name = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
- $the_list .= "\n\t\t<category domain=\"tag\"><![CDATA[$tag_name]]></category>\n";
- // forwards compatibility as above
- $the_list .= "\n\t\t<category domain=\"tag\" nicename=\"{$tag->slug}\"><![CDATA[$tag_name]]></category>\n";
- }
- echo $the_list;
- }
- }
- }
- /*mod jrc 290312 - new func to omit tags and cats for posts when unselected by terms option*/
- function scl_wxr_post_taxonomy($terms = 'all') {
- if (empty($terms) || $terms =='none') return;
- $categories = get_the_category();
- $tags = get_the_tags();
- $the_list = '';
- $filter = 'rss';
- if ( ($terms == 'cats' || $terms =='all' ) && !empty($categories) ) foreach ( (array) $categories as $category ) {
- $cat_name = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
- // for backwards compatibility
- $the_list .= "\n\t\t<category><![CDATA[$cat_name]]></category>\n";
- // forwards compatibility: use a unique identifier for each cat to avoid clashes
- // http://trac.wordpress.org/ticket/5447
- $the_list .= "\n\t\t<category domain=\"category\" nicename=\"{$category->slug}\"><![CDATA[$cat_name]]></category>\n";
- }
- if ( ($terms == 'tags' || $terms =='all' ) && !empty($tags) ) foreach ( (array) $tags as $tag ) {
- $tag_name = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
- $the_list .= "\n\t\t<category domain=\"tag\"><![CDATA[$tag_name]]></category>\n";
- // forwards compatibility as above
- $the_list .= "\n\t\t<category domain=\"tag\" nicename=\"{$tag->slug}\"><![CDATA[$tag_name]]></category>\n";
- }
- echo $the_list;
- }
- /*end mod jrc 290312*/
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement