mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress Export Administration API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Administration
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * Version number for the export format.
mp-wp_genesis 11 *
mp-wp_genesis 12 * Bump this when something changes that might affect compatibility.
mp-wp_genesis 13 *
mp-wp_genesis 14 * @since unknown
mp-wp_genesis 15 * @var string
mp-wp_genesis 16 */
mp-wp_genesis 17 define('WXR_VERSION', '1.0');
mp-wp_genesis 18
mp-wp_genesis 19 /**
mp-wp_genesis 20 * {@internal Missing Short Description}}
mp-wp_genesis 21 *
mp-wp_genesis 22 * @since unknown
mp-wp_genesis 23 *
mp-wp_genesis 24 * @param unknown_type $author
mp-wp_genesis 25 */
mp-wp_genesis 26 function export_wp($author='') {
mp-wp_genesis 27 global $wpdb, $post_ids, $post;
mp-wp_genesis 28
mp-wp_genesis 29 do_action('export_wp');
mp-wp_genesis 30
mp-wp_genesis 31 $filename = 'wordpress.' . date('Y-m-d') . '.xml';
mp-wp_genesis 32
mp-wp_genesis 33 header('Content-Description: File Transfer');
mp-wp_genesis 34 header("Content-Disposition: attachment; filename=$filename");
mp-wp_genesis 35 header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
mp-wp_genesis 36
mp-wp_genesis 37 $where = '';
mp-wp_genesis 38 if ( $author and $author != 'all' ) {
mp-wp_genesis 39 $author_id = (int) $author;
mp-wp_genesis 40 $where = $wpdb->prepare(" WHERE post_author = %d ", $author_id);
mp-wp_genesis 41 }
mp-wp_genesis 42
mp-wp_genesis 43 // grab a snapshot of post IDs, just in case it changes during the export
mp-wp_genesis 44 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
mp-wp_genesis 45
mp-wp_genesis 46 $categories = (array) get_categories('get=all');
mp-wp_genesis 47 $tags = (array) get_tags('get=all');
mp-wp_genesis 48
mp-wp_genesis 49 /**
mp-wp_genesis 50 * {@internal Missing Short Description}}
mp-wp_genesis 51 *
mp-wp_genesis 52 * @since unknown
mp-wp_genesis 53 *
mp-wp_genesis 54 * @param unknown_type $categories
mp-wp_genesis 55 */
mp-wp_genesis 56 function wxr_missing_parents($categories) {
mp-wp_genesis 57 if ( !is_array($categories) || empty($categories) )
mp-wp_genesis 58 return array();
mp-wp_genesis 59
mp-wp_genesis 60 foreach ( $categories as $category )
mp-wp_genesis 61 $parents[$category->term_id] = $category->parent;
mp-wp_genesis 62
mp-wp_genesis 63 $parents = array_unique(array_diff($parents, array_keys($parents)));
mp-wp_genesis 64
mp-wp_genesis 65 if ( $zero = array_search('0', $parents) )
mp-wp_genesis 66 unset($parents[$zero]);
mp-wp_genesis 67
mp-wp_genesis 68 return $parents;
mp-wp_genesis 69 }
mp-wp_genesis 70
mp-wp_genesis 71 while ( $parents = wxr_missing_parents($categories) ) {
mp-wp_genesis 72 $found_parents = get_categories("include=" . join(', ', $parents));
mp-wp_genesis 73 if ( is_array($found_parents) && count($found_parents) )
mp-wp_genesis 74 $categories = array_merge($categories, $found_parents);
mp-wp_genesis 75 else
mp-wp_genesis 76 break;
mp-wp_genesis 77 }
mp-wp_genesis 78
mp-wp_genesis 79 // Put them in order to be inserted with no child going before its parent
mp-wp_genesis 80 $pass = 0;
mp-wp_genesis 81 $passes = 1000 + count($categories);
mp-wp_genesis 82 while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
mp-wp_genesis 83 if ( $cat->parent == 0 || isset($cats[$cat->parent]) ) {
mp-wp_genesis 84 $cats[$cat->term_id] = $cat;
mp-wp_genesis 85 } else {
mp-wp_genesis 86 $categories[] = $cat;
mp-wp_genesis 87 }
mp-wp_genesis 88 }
mp-wp_genesis 89 unset($categories);
mp-wp_genesis 90
mp-wp_genesis 91 /**
mp-wp_genesis 92 * Place string in CDATA tag.
mp-wp_genesis 93 *
mp-wp_genesis 94 * @since unknown
mp-wp_genesis 95 *
mp-wp_genesis 96 * @param string $str String to place in XML CDATA tag.
mp-wp_genesis 97 */
mp-wp_genesis 98 function wxr_cdata($str) {
mp-wp_genesis 99 if ( seems_utf8($str) == false )
mp-wp_genesis 100 $str = utf8_encode($str);
mp-wp_genesis 101
mp-wp_genesis 102 // $str = ent2ncr(wp_specialchars($str));
mp-wp_genesis 103
mp-wp_genesis 104 $str = "<![CDATA[$str" . ( ( substr($str, -1) == ']' ) ? ' ' : '') . "]]>";
mp-wp_genesis 105
mp-wp_genesis 106 return $str;
mp-wp_genesis 107 }
mp-wp_genesis 108
mp-wp_genesis 109 /**
mp-wp_genesis 110 * {@internal Missing Short Description}}
mp-wp_genesis 111 *
mp-wp_genesis 112 * @since unknown
mp-wp_genesis 113 *
mp-wp_genesis 114 * @return string Site URL.
mp-wp_genesis 115 */
mp-wp_genesis 116 function wxr_site_url() {
mp-wp_genesis 117 global $current_site;
mp-wp_genesis 118
mp-wp_genesis 119 // mu: the base url
mp-wp_genesis 120 if ( isset($current_site->domain) ) {
mp-wp_genesis 121 return 'http://'.$current_site->domain.$current_site->path;
mp-wp_genesis 122 }
mp-wp_genesis 123 // wp: the blog url
mp-wp_genesis 124 else {
mp-wp_genesis 125 return get_bloginfo_rss('url');
mp-wp_genesis 126 }
mp-wp_genesis 127 }
mp-wp_genesis 128
mp-wp_genesis 129 /**
mp-wp_genesis 130 * {@internal Missing Short Description}}
mp-wp_genesis 131 *
mp-wp_genesis 132 * @since unknown
mp-wp_genesis 133 *
mp-wp_genesis 134 * @param object $c Category Object
mp-wp_genesis 135 */
mp-wp_genesis 136 function wxr_cat_name($c) {
mp-wp_genesis 137 if ( empty($c->name) )
mp-wp_genesis 138 return;
mp-wp_genesis 139
mp-wp_genesis 140 echo '<wp:cat_name>' . wxr_cdata($c->name) . '</wp:cat_name>';
mp-wp_genesis 141 }
mp-wp_genesis 142
mp-wp_genesis 143 /**
mp-wp_genesis 144 * {@internal Missing Short Description}}
mp-wp_genesis 145 *
mp-wp_genesis 146 * @since unknown
mp-wp_genesis 147 *
mp-wp_genesis 148 * @param object $c Category Object
mp-wp_genesis 149 */
mp-wp_genesis 150 function wxr_category_description($c) {
mp-wp_genesis 151 if ( empty($c->description) )
mp-wp_genesis 152 return;
mp-wp_genesis 153
mp-wp_genesis 154 echo '<wp:category_description>' . wxr_cdata($c->description) . '</wp:category_description>';
mp-wp_genesis 155 }
mp-wp_genesis 156
mp-wp_genesis 157 /**
mp-wp_genesis 158 * {@internal Missing Short Description}}
mp-wp_genesis 159 *
mp-wp_genesis 160 * @since unknown
mp-wp_genesis 161 *
mp-wp_genesis 162 * @param object $t Tag Object
mp-wp_genesis 163 */
mp-wp_genesis 164 function wxr_tag_name($t) {
mp-wp_genesis 165 if ( empty($t->name) )
mp-wp_genesis 166 return;
mp-wp_genesis 167
mp-wp_genesis 168 echo '<wp:tag_name>' . wxr_cdata($t->name) . '</wp:tag_name>';
mp-wp_genesis 169 }
mp-wp_genesis 170
mp-wp_genesis 171 /**
mp-wp_genesis 172 * {@internal Missing Short Description}}
mp-wp_genesis 173 *
mp-wp_genesis 174 * @since unknown
mp-wp_genesis 175 *
mp-wp_genesis 176 * @param object $t Tag Object
mp-wp_genesis 177 */
mp-wp_genesis 178 function wxr_tag_description($t) {
mp-wp_genesis 179 if ( empty($t->description) )
mp-wp_genesis 180 return;
mp-wp_genesis 181
mp-wp_genesis 182 echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
mp-wp_genesis 183 }
mp-wp_genesis 184
mp-wp_genesis 185 /**
mp-wp_genesis 186 * {@internal Missing Short Description}}
mp-wp_genesis 187 *
mp-wp_genesis 188 * @since unknown
mp-wp_genesis 189 */
mp-wp_genesis 190 function wxr_post_taxonomy() {
mp-wp_genesis 191 $categories = get_the_category();
mp-wp_genesis 192 $tags = get_the_tags();
mp-wp_genesis 193 $the_list = '';
mp-wp_genesis 194 $filter = 'rss';
mp-wp_genesis 195
mp-wp_genesis 196 if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
mp-wp_genesis 197 $cat_name = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
mp-wp_genesis 198 // for backwards compatibility
mp-wp_genesis 199 $the_list .= "\n\t\t<category><![CDATA[$cat_name]]></category>\n";
mp-wp_genesis 200 // forwards compatibility: use a unique identifier for each cat to avoid clashes
mp-wp_genesis 201 // http://trac.wordpress.org/ticket/5447
mp-wp_genesis 202 $the_list .= "\n\t\t<category domain=\"category\" nicename=\"{$category->slug}\"><![CDATA[$cat_name]]></category>\n";
mp-wp_genesis 203 }
mp-wp_genesis 204
mp-wp_genesis 205 if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
mp-wp_genesis 206 $tag_name = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
mp-wp_genesis 207 $the_list .= "\n\t\t<category domain=\"tag\"><![CDATA[$tag_name]]></category>\n";
mp-wp_genesis 208 // forwards compatibility as above
mp-wp_genesis 209 $the_list .= "\n\t\t<category domain=\"tag\" nicename=\"{$tag->slug}\"><![CDATA[$tag_name]]></category>\n";
mp-wp_genesis 210 }
mp-wp_genesis 211
mp-wp_genesis 212 echo $the_list;
mp-wp_genesis 213 }
mp-wp_genesis 214
mp-wp_genesis 215 echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
mp-wp_genesis 216
mp-wp_genesis 217 ?>
mp-wp_genesis 218 <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
mp-wp_genesis 219 <!-- It contains information about your blog's posts, comments, and categories. -->
mp-wp_genesis 220 <!-- You may use this file to transfer that content from one site to another. -->
mp-wp_genesis 221 <!-- This file is not intended to serve as a complete backup of your blog. -->
mp-wp_genesis 222
mp-wp_genesis 223 <!-- To import this information into a WordPress blog follow these steps. -->
mp-wp_genesis 224 <!-- 1. Log into that blog as an administrator. -->
mp-wp_genesis 225 <!-- 2. Go to Tools: Import in the blog's admin panels (or Manage: Import in older versions of WordPress). -->
mp-wp_genesis 226 <!-- 3. Choose "WordPress" from the list. -->
mp-wp_genesis 227 <!-- 4. Upload this file using the form provided on that page. -->
mp-wp_genesis 228 <!-- 5. You will first be asked to map the authors in this export file to users -->
mp-wp_genesis 229 <!-- on the blog. For each author, you may choose to map to an -->
mp-wp_genesis 230 <!-- existing user on the blog or to create a new user -->
mp-wp_genesis 231 <!-- 6. WordPress will then import each of the posts, comments, and categories -->
mp-wp_genesis 232 <!-- contained in this file into your blog -->
mp-wp_genesis 233
mp-wp_genesis 234 <?php the_generator('export');?>
mp-wp_genesis 235 <rss version="2.0"
mp-wp_genesis 236 xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
mp-wp_genesis 237 xmlns:content="http://purl.org/rss/1.0/modules/content/"
mp-wp_genesis 238 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
mp-wp_genesis 239 xmlns:dc="http://purl.org/dc/elements/1.1/"
mp-wp_genesis 240 xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
mp-wp_genesis 241 >
mp-wp_genesis 242
mp-wp_genesis 243 <channel>
mp-wp_genesis 244 <title><?php bloginfo_rss('name'); ?></title>
mp-wp_genesis 245 <link><?php bloginfo_rss('url') ?></link>
mp-wp_genesis 246 <description><?php bloginfo_rss("description") ?></description>
mp-wp_genesis 247 <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></pubDate>
mp-wp_genesis 248 <generator>http://wordpress.org/?v=<?php bloginfo_rss('version'); ?></generator>
mp-wp_genesis 249 <language><?php echo get_option('rss_language'); ?></language>
mp-wp_genesis 250 <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
mp-wp_genesis 251 <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
mp-wp_genesis 252 <wp:base_blog_url><?php bloginfo_rss('url'); ?></wp:base_blog_url>
mp-wp_genesis 253 <?php if ( $cats ) : foreach ( $cats as $c ) : ?>
mp-wp_genesis 254 <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>
mp-wp_genesis 255 <?php endforeach; endif; ?>
mp-wp_genesis 256 <?php if ( $tags ) : foreach ( $tags as $t ) : ?>
mp-wp_genesis 257 <wp:tag><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name($t); ?><?php wxr_tag_description($t); ?></wp:tag>
mp-wp_genesis 258 <?php endforeach; endif; ?>
mp-wp_genesis 259 <?php do_action('rss2_head'); ?>
mp-wp_genesis 260 <?php if ($post_ids) {
mp-wp_genesis 261 global $wp_query;
mp-wp_genesis 262 $wp_query->in_the_loop = true; // Fake being in the loop.
mp-wp_genesis 263 // fetch 20 posts at a time rather than loading the entire table into memory
mp-wp_genesis 264 while ( $next_posts = array_splice($post_ids, 0, 20) ) {
mp-wp_genesis 265 $where = "WHERE ID IN (".join(',', $next_posts).")";
mp-wp_genesis 266 $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
mp-wp_genesis 267 foreach ($posts as $post) {
mp-wp_genesis 268 // Don't export revisions. They bloat the export.
mp-wp_genesis 269 if ( 'revision' == $post->post_type )
mp-wp_genesis 270 continue;
mp-wp_genesis 271 setup_postdata($post); ?>
mp-wp_genesis 272 <item>
mp-wp_genesis 273 <title><?php echo apply_filters('the_title_rss', $post->post_title); ?></title>
mp-wp_genesis 274 <link><?php the_permalink_rss() ?></link>
mp-wp_genesis 275 <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
mp-wp_genesis 276 <dc:creator><?php echo wxr_cdata(get_the_author()); ?></dc:creator>
mp-wp_genesis 277 <?php wxr_post_taxonomy() ?>
mp-wp_genesis 278
mp-wp_genesis 279 <guid isPermaLink="false"><?php the_guid(); ?></guid>
mp-wp_genesis 280 <description></description>
mp-wp_genesis 281 <content:encoded><?php echo wxr_cdata( apply_filters('the_content_export', $post->post_content) ); ?></content:encoded>
mp-wp_genesis 282 <excerpt:encoded><?php echo wxr_cdata( apply_filters('the_excerpt_export', $post->post_excerpt) ); ?></excerpt:encoded>
mp-wp_genesis 283 <wp:post_id><?php echo $post->ID; ?></wp:post_id>
mp-wp_genesis 284 <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
mp-wp_genesis 285 <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
mp-wp_genesis 286 <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
mp-wp_genesis 287 <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
mp-wp_genesis 288 <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
mp-wp_genesis 289 <wp:status><?php echo $post->post_status; ?></wp:status>
mp-wp_genesis 290 <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
mp-wp_genesis 291 <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
mp-wp_genesis 292 <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
mp-wp_genesis 293 <wp:post_password><?php echo $post->post_password; ?></wp:post_password>
mp-wp_genesis 294 <?php
mp-wp_genesis 295 if ($post->post_type == 'attachment') { ?>
mp-wp_genesis 296 <wp:attachment_url><?php echo wp_get_attachment_url($post->ID); ?></wp:attachment_url>
mp-wp_genesis 297 <?php } ?>
mp-wp_genesis 298 <?php
mp-wp_genesis 299 $postmeta = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID) );
mp-wp_genesis 300 if ( $postmeta ) {
mp-wp_genesis 301 ?>
mp-wp_genesis 302 <?php foreach( $postmeta as $meta ) { ?>
mp-wp_genesis 303 <wp:postmeta>
mp-wp_genesis 304 <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
mp-wp_genesis 305 <wp:meta_value><?Php echo $meta->meta_value; ?></wp:meta_value>
mp-wp_genesis 306 </wp:postmeta>
mp-wp_genesis 307 <?php } ?>
mp-wp_genesis 308 <?php } ?>
mp-wp_genesis 309 <?php
mp-wp_genesis 310 $comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d", $post->ID) );
mp-wp_genesis 311 if ( $comments ) { foreach ( $comments as $c ) { ?>
mp-wp_genesis 312 <wp:comment>
mp-wp_genesis 313 <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
mp-wp_genesis 314 <wp:comment_author><?php echo wxr_cdata($c->comment_author); ?></wp:comment_author>
mp-wp_genesis 315 <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
mp-wp_genesis 316 <wp:comment_author_url><?php echo $c->comment_author_url; ?></wp:comment_author_url>
mp-wp_genesis 317 <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
mp-wp_genesis 318 <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
mp-wp_genesis 319 <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
mp-wp_genesis 320 <wp:comment_content><?php echo wxr_cdata($c->comment_content) ?></wp:comment_content>
mp-wp_genesis 321 <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
mp-wp_genesis 322 <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
mp-wp_genesis 323 <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
mp-wp_genesis 324 <wp:comment_user_id><?php echo $c->user_id; ?></wp:comment_user_id>
mp-wp_genesis 325 </wp:comment>
mp-wp_genesis 326 <?php } } ?>
mp-wp_genesis 327 </item>
mp-wp_genesis 328 <?php } } } ?>
mp-wp_genesis 329 </channel>
mp-wp_genesis 330 </rss>
mp-wp_genesis 331 <?php
mp-wp_genesis 332 }
mp-wp_genesis 333
mp-wp_genesis 334 ?>