From 3fc2cea52ef2e0caf92e148fb72d5ad4ae2c77b9 Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 26 Oct 2009 13:57:55 +0000 Subject: [PATCH] Export and import custom taxonomies. Props chrisscott. fixes #10012 git-svn-id: http://svn.automattic.com/wordpress/trunk@12109 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/import/wordpress.php | 44 +++++++++++++++++++++++++++++++++++ wp-admin/includes/export.php | 40 ++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/wp-admin/import/wordpress.php b/wp-admin/import/wordpress.php index 7fe782d110..77fc507b50 100644 --- a/wp-admin/import/wordpress.php +++ b/wp-admin/import/wordpress.php @@ -27,6 +27,7 @@ class WP_Import { var $author_ids = array (); var $tags = array (); var $categories = array (); + var $terms = array (); var $j = -1; var $fetch_attachments = false; @@ -122,6 +123,11 @@ class WP_Import { $this->tags[] = $tag[1]; continue; } + if ( false !== strpos($importline, '') ) { + preg_match('|(.*?)|is', $importline, $term); + $this->terms[] = $term[1]; + continue; + } if ( false !== strpos($importline, '') ) { $this->post = ''; $doing_entry = true; @@ -337,6 +343,43 @@ class WP_Import { $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) { $author = $this->get_tag( $post, 'dc:creator' ); @@ -748,6 +791,7 @@ class WP_Import { $this->get_entries(); $this->process_categories(); $this->process_tags(); + $this->process_terms(); $result = $this->process_posts(); wp_suspend_cache_invalidation(false); $this->backfill_parents(); diff --git a/wp-admin/includes/export.php b/wp-admin/includes/export.php index 94facf13f9..c9b128320a 100644 --- a/wp-admin/includes/export.php +++ b/wp-admin/includes/export.php @@ -24,7 +24,7 @@ define('WXR_VERSION', '1.0'); * @param unknown_type $author */ function export_wp($author='') { -global $wpdb, $post_ids, $post; +global $wpdb, $post_ids, $post, $wp_taxonomies; 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'); $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}} * @@ -182,6 +189,34 @@ function wxr_tag_description($t) { echo '' . wxr_cdata($t->description) . ''; } +/** + * {@internal Missing Short Description}} + * + * @since unknown + * + * @param object $t Term Object + */ +function wxr_term_name($t) { + if ( empty($t->name) ) + return; + + echo '' . wxr_cdata($t->name) . ''; +} + +/** + * {@internal Missing Short Description}} + * + * @since unknown + * + * @param object $t Term Object + */ +function wxr_term_description($t) { + if ( empty($t->description) ) + return; + + echo '' . wxr_cdata($t->description) . ''; +} + /** * {@internal Missing Short Description}} * @@ -255,6 +290,9 @@ echo '\n"; slug; ?> + + + taxonomy; ?>slug; ?>parent ? $custom_taxonomies[$t->parent]->name : ''; ?>