Export and import of post tags. fixes #4682

git-svn-id: http://svn.automattic.com/wordpress/trunk@6149 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-09-21 18:34:28 +00:00
parent 81482db30b
commit a366ee01ef
2 changed files with 50 additions and 1 deletions

View File

@ -141,6 +141,27 @@ function wxr_tag_description($t) {
echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
}
function wxr_post_taxonomy() {
$categories = get_the_category();
$tags = get_the_tags();
$cat_names = array();
$tag_names = array();
$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);
$the_list .= "\n\t\t<category><![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";
}
echo $the_list;
}
print '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
?>
@ -201,7 +222,7 @@ print '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
<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 the_author() ?></dc:creator>
<?php the_category_rss() ?>
<?php wxr_post_taxonomy() ?>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<description></description>

View File

@ -316,6 +316,15 @@ class WP_Import {
$post_content = str_replace('<br>', '<br />', $post_content);
$post_content = str_replace('<hr>', '<hr />', $post_content);
preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags);
$tags = $tags[1];
$tag_index = 0;
foreach ($tags as $tag) {
$tags[$tag_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $tag)));
$tag_index++;
}
preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
$categories = $categories[1];
@ -372,6 +381,25 @@ class WP_Import {
}
wp_set_post_categories($post_id, $post_cats);
}
// Add tags.
if (count($tags) > 0) {
$post_tags = array();
foreach ($tags as $tag) {
$slug = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
$tag_obj = get_term_by('slug', $slug, 'post_tag');
$tag_id = 0;
if ( ! empty($tag_obj) )
$tag_id = $tag_obj->term_id;
if ( $tag_id == 0 ) {
$tag = $wpdb->escape($tag);
$tag_id = wp_insert_term($tag, 'post_tag');
$tag_id = $tag_id['term_id'];
}
$post_tags[] = $tag_id;
}
wp_set_post_tags($post_id, $post_tags);
}
}
// Now for comments