Export and import custom taxonomies. Props chrisscott. fixes #10012
git-svn-id: http://svn.automattic.com/wordpress/trunk@12109 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
c79136f6d1
commit
3fc2cea52e
|
@ -27,6 +27,7 @@ class WP_Import {
|
||||||
var $author_ids = array ();
|
var $author_ids = array ();
|
||||||
var $tags = array ();
|
var $tags = array ();
|
||||||
var $categories = array ();
|
var $categories = array ();
|
||||||
|
var $terms = array ();
|
||||||
|
|
||||||
var $j = -1;
|
var $j = -1;
|
||||||
var $fetch_attachments = false;
|
var $fetch_attachments = false;
|
||||||
|
@ -122,6 +123,11 @@ class WP_Import {
|
||||||
$this->tags[] = $tag[1];
|
$this->tags[] = $tag[1];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if ( false !== strpos($importline, '<wp:term>') ) {
|
||||||
|
preg_match('|<wp:term>(.*?)</wp:term>|is', $importline, $term);
|
||||||
|
$this->terms[] = $term[1];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if ( false !== strpos($importline, '<item>') ) {
|
if ( false !== strpos($importline, '<item>') ) {
|
||||||
$this->post = '';
|
$this->post = '';
|
||||||
$doing_entry = true;
|
$doing_entry = true;
|
||||||
|
@ -337,6 +343,43 @@ class WP_Import {
|
||||||
$tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr);
|
$tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function process_terms() {
|
||||||
|
global $wpdb, $wp_taxonomies;
|
||||||
|
|
||||||
|
$custom_taxonomies = $wp_taxonomies;
|
||||||
|
// get rid of the standard taxonomies
|
||||||
|
unset( $custom_taxonomies['category'] );
|
||||||
|
unset( $custom_taxonomies['post_tag'] );
|
||||||
|
unset( $custom_taxonomies['link_category'] );
|
||||||
|
|
||||||
|
$custom_taxonomies = array_keys( $custom_taxonomies );
|
||||||
|
$current_terms = (array) get_terms( $custom_taxonomies, 'get=all' );
|
||||||
|
$taxonomies = array();
|
||||||
|
foreach ( $current_terms as $term ) {
|
||||||
|
if ( isset( $_terms[$term->taxonomy] ) ) {
|
||||||
|
$taxonomies[$term->taxonomy] = array_merge( $taxonomies[$term->taxonomy], array($term->name) );
|
||||||
|
} else {
|
||||||
|
$taxonomies[$term->taxonomy] = array($term->name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while ( $c = array_shift($this->terms) ) {
|
||||||
|
$term_name = trim($this->get_tag( $c, 'wp:term_name' ));
|
||||||
|
$term_taxonomy = trim($this->get_tag( $c, 'wp:term_taxonomy' ));
|
||||||
|
|
||||||
|
// If the term exists in the taxonomy we leave it alone
|
||||||
|
if ( isset($taxonomies[$term_taxonomy] ) && in_array( $term_name, $taxonomies[$term_taxonomy] ) )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
$slug = $this->get_tag( $c, 'wp:term_slug' );
|
||||||
|
$description = $this->get_tag( $c, 'wp:term_description' );
|
||||||
|
|
||||||
|
$termarr = compact('slug', 'description');
|
||||||
|
|
||||||
|
$term_ID = wp_insert_term($term_name, $this->get_tag( $c, 'wp:term_taxonomy' ), $termarr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function process_author($post) {
|
function process_author($post) {
|
||||||
$author = $this->get_tag( $post, 'dc:creator' );
|
$author = $this->get_tag( $post, 'dc:creator' );
|
||||||
|
@ -748,6 +791,7 @@ class WP_Import {
|
||||||
$this->get_entries();
|
$this->get_entries();
|
||||||
$this->process_categories();
|
$this->process_categories();
|
||||||
$this->process_tags();
|
$this->process_tags();
|
||||||
|
$this->process_terms();
|
||||||
$result = $this->process_posts();
|
$result = $this->process_posts();
|
||||||
wp_suspend_cache_invalidation(false);
|
wp_suspend_cache_invalidation(false);
|
||||||
$this->backfill_parents();
|
$this->backfill_parents();
|
||||||
|
|
|
@ -24,7 +24,7 @@ define('WXR_VERSION', '1.0');
|
||||||
* @param unknown_type $author
|
* @param unknown_type $author
|
||||||
*/
|
*/
|
||||||
function export_wp($author='') {
|
function export_wp($author='') {
|
||||||
global $wpdb, $post_ids, $post;
|
global $wpdb, $post_ids, $post, $wp_taxonomies;
|
||||||
|
|
||||||
do_action('export_wp');
|
do_action('export_wp');
|
||||||
|
|
||||||
|
@ -46,6 +46,13 @@ $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts $where ORDER BY post_dat
|
||||||
$categories = (array) get_categories('get=all');
|
$categories = (array) get_categories('get=all');
|
||||||
$tags = (array) get_tags('get=all');
|
$tags = (array) get_tags('get=all');
|
||||||
|
|
||||||
|
$custom_taxonomies = $wp_taxonomies;
|
||||||
|
unset($custom_taxonomies['category']);
|
||||||
|
unset($custom_taxonomies['post_tag']);
|
||||||
|
unset($custom_taxonomies['link_category']);
|
||||||
|
$custom_taxonomies = array_keys($custom_taxonomies);
|
||||||
|
$terms = (array) get_terms($custom_taxonomies, 'get=all');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@internal Missing Short Description}}
|
* {@internal Missing Short Description}}
|
||||||
*
|
*
|
||||||
|
@ -182,6 +189,34 @@ function wxr_tag_description($t) {
|
||||||
echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
|
echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@internal Missing Short Description}}
|
||||||
|
*
|
||||||
|
* @since unknown
|
||||||
|
*
|
||||||
|
* @param object $t Term Object
|
||||||
|
*/
|
||||||
|
function wxr_term_name($t) {
|
||||||
|
if ( empty($t->name) )
|
||||||
|
return;
|
||||||
|
|
||||||
|
echo '<wp:term_name>' . wxr_cdata($t->name) . '</wp:term_name>';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@internal Missing Short Description}}
|
||||||
|
*
|
||||||
|
* @since unknown
|
||||||
|
*
|
||||||
|
* @param object $t Term Object
|
||||||
|
*/
|
||||||
|
function wxr_term_description($t) {
|
||||||
|
if ( empty($t->description) )
|
||||||
|
return;
|
||||||
|
|
||||||
|
echo '<wp:term_description>' . wxr_cdata($t->description) . '</wp:term_description>';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@internal Missing Short Description}}
|
* {@internal Missing Short Description}}
|
||||||
*
|
*
|
||||||
|
@ -255,6 +290,9 @@ echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
|
||||||
<?php endforeach; endif; ?>
|
<?php endforeach; endif; ?>
|
||||||
<?php if ( $tags ) : foreach ( $tags as $t ) : ?>
|
<?php if ( $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>
|
<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 if ( $terms ) : foreach ( $terms as $t ) : ?>
|
||||||
|
<wp:term><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 ? $custom_taxonomies[$t->parent]->name : ''; ?></wp:term_parent><?php wxr_term_name($t); ?><?php wxr_term_description($t); ?></wp:term>
|
||||||
<?php endforeach; endif; ?>
|
<?php endforeach; endif; ?>
|
||||||
<?php do_action('rss2_head'); ?>
|
<?php do_action('rss2_head'); ?>
|
||||||
<?php if ($post_ids) {
|
<?php if ($post_ids) {
|
||||||
|
|
Loading…
Reference in New Issue