<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[12109] trunk/wp-admin: Export and import custom taxonomies.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12109">12109</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2009-10-26 13:57:55 +0000 (Mon, 26 Oct 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>Export and import custom taxonomies. Props chrisscott. fixes <a href="http://trac.wordpress.org/ticket/10012">#10012</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminimportwordpressphp">trunk/wp-admin/import/wordpress.php</a></li>
<li><a href="#trunkwpadminincludesexportphp">trunk/wp-admin/includes/export.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminimportwordpressphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/import/wordpress.php (12108 => 12109)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/import/wordpress.php        2009-10-26 09:04:21 UTC (rev 12108)
+++ trunk/wp-admin/import/wordpress.php        2009-10-26 13:57:55 UTC (rev 12109)
</span><span class="lines">@@ -27,6 +27,7 @@
</span><span class="cx">         var $author_ids = array ();
</span><span class="cx">         var $tags = array ();
</span><span class="cx">         var $categories = array ();
</span><ins>+        var $terms = array ();
</ins><span class="cx">
</span><span class="cx">         var $j = -1;
</span><span class="cx">         var $fetch_attachments = false;
</span><span class="lines">@@ -122,6 +123,11 @@
</span><span class="cx">                                         $this->tags[] = $tag[1];
</span><span class="cx">                                         continue;
</span><span class="cx">                                 }
</span><ins>+                                if ( false !== strpos($importline, '<wp:term>') ) {
+                                        preg_match('|<wp:term>(.*?)</wp:term>|is', $importline, $term);
+                                        $this->terms[] = $term[1];
+                                        continue;
+                                }
</ins><span class="cx">                                 if ( false !== strpos($importline, '<item>') ) {
</span><span class="cx">                                         $this->post = '';
</span><span class="cx">                                         $doing_entry = true;
</span><span class="lines">@@ -337,7 +343,44 @@
</span><span class="cx">                         $tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr);
</span><span class="cx">                 }
</span><span class="cx">         }
</span><ins>+        
+        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);
+                        }
+                }
</ins><span class="cx">
</span><ins>+                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);
+                }
+        }
+
</ins><span class="cx">         function process_author($post) {
</span><span class="cx">                 $author = $this->get_tag( $post, 'dc:creator' );
</span><span class="cx">                 if ($author)
</span><span class="lines">@@ -748,6 +791,7 @@
</span><span class="cx">                 $this->get_entries();
</span><span class="cx">                 $this->process_categories();
</span><span class="cx">                 $this->process_tags();
</span><ins>+                $this->process_terms();
</ins><span class="cx">                 $result = $this->process_posts();
</span><span class="cx">                 wp_suspend_cache_invalidation(false);
</span><span class="cx">                 $this->backfill_parents();
</span></span></pre></div>
<a id="trunkwpadminincludesexportphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/export.php (12108 => 12109)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/export.php        2009-10-26 09:04:21 UTC (rev 12108)
+++ trunk/wp-admin/includes/export.php        2009-10-26 13:57:55 UTC (rev 12109)
</span><span class="lines">@@ -24,7 +24,7 @@
</span><span class="cx"> * @param unknown_type $author
</span><span class="cx"> */
</span><span class="cx"> function export_wp($author='') {
</span><del>-global $wpdb, $post_ids, $post;
</del><ins>+global $wpdb, $post_ids, $post, $wp_taxonomies;
</ins><span class="cx">
</span><span class="cx"> do_action('export_wp');
</span><span class="cx">
</span><span class="lines">@@ -46,6 +46,13 @@
</span><span class="cx"> $categories = (array) get_categories('get=all');
</span><span class="cx"> $tags = (array) get_tags('get=all');
</span><span class="cx">
</span><ins>+$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');
+
</ins><span class="cx"> /**
</span><span class="cx"> * {@internal Missing Short Description}}
</span><span class="cx"> *
</span><span class="lines">@@ -186,7 +193,35 @@
</span><span class="cx"> * {@internal Missing Short Description}}
</span><span class="cx"> *
</span><span class="cx"> * @since unknown
</span><ins>+ *
+ * @param object $t Term Object
</ins><span class="cx"> */
</span><ins>+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}}
+ *
+ * @since unknown
+ */
</ins><span class="cx"> function wxr_post_taxonomy() {
</span><span class="cx">         $categories = get_the_category();
</span><span class="cx">         $tags = get_the_tags();
</span><span class="lines">@@ -256,6 +291,9 @@
</span><span class="cx"> <?php if ( $tags ) : foreach ( $tags as $t ) : ?>
</span><span class="cx">         <wp:tag><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name($t); ?><?php wxr_tag_description($t); ?></wp:tag>
</span><span class="cx"> <?php endforeach; endif; ?>
</span><ins>+<?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; ?>
</ins><span class="cx">         <?php do_action('rss2_head'); ?>
</span><span class="cx">         <?php if ($post_ids) {
</span><span class="cx">                 global $wp_query;
</span></span></pre>
</div>
</div>
</body>
</html>