<!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-&gt;tags[] = $tag[1];
</span><span class="cx">                                         continue;
</span><span class="cx">                                 }
</span><ins>+                                if ( false !== strpos($importline, '&lt;wp:term&gt;') ) {
+                                        preg_match('|&lt;wp:term&gt;(.*?)&lt;/wp:term&gt;|is', $importline, $term);
+                                        $this-&gt;terms[] = $term[1];
+                                        continue;
+                                }
</ins><span class="cx">                                 if ( false !== strpos($importline, '&lt;item&gt;') ) {
</span><span class="cx">                                         $this-&gt;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-&gt;taxonomy] ) ) {
+                                $taxonomies[$term-&gt;taxonomy] = array_merge( $taxonomies[$term-&gt;taxonomy], array($term-&gt;name) );
+                        } else {
+                                $taxonomies[$term-&gt;taxonomy] = array($term-&gt;name);
+                        }
+                }
</ins><span class="cx"> 
</span><ins>+                while ( $c = array_shift($this-&gt;terms) ) {
+                        $term_name = trim($this-&gt;get_tag( $c, 'wp:term_name' ));
+                        $term_taxonomy = trim($this-&gt;get_tag( $c, 'wp:term_taxonomy' ));
+
+                        // If the term exists in the taxonomy we leave it alone
+                        if ( isset($taxonomies[$term_taxonomy] ) &amp;&amp; in_array( $term_name, $taxonomies[$term_taxonomy] ) )
+                                continue;
+
+                        $slug = $this-&gt;get_tag( $c, 'wp:term_slug' );
+                        $description = $this-&gt;get_tag( $c, 'wp:term_description' );
+
+                        $termarr = compact('slug', 'description');
+
+                        $term_ID = wp_insert_term($term_name, $this-&gt;get_tag( $c, 'wp:term_taxonomy' ), $termarr);
+                }
+        }
+
</ins><span class="cx">         function process_author($post) {
</span><span class="cx">                 $author = $this-&gt;get_tag( $post, 'dc:creator' );
</span><span class="cx">                 if ($author)
</span><span class="lines">@@ -748,6 +791,7 @@
</span><span class="cx">                 $this-&gt;get_entries();
</span><span class="cx">                 $this-&gt;process_categories();
</span><span class="cx">                 $this-&gt;process_tags();
</span><ins>+                $this-&gt;process_terms();
</ins><span class="cx">                 $result = $this-&gt;process_posts();
</span><span class="cx">                 wp_suspend_cache_invalidation(false);
</span><span class="cx">                 $this-&gt;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-&gt;name) )
+                return;
+
+        echo '&lt;wp:term_name&gt;' . wxr_cdata($t-&gt;name) . '&lt;/wp:term_name&gt;';
+}
+
+/**
+ * {@internal Missing Short Description}}
+ *
+ * @since unknown
+ *
+ * @param object $t Term Object
+ */
+function wxr_term_description($t) {
+        if ( empty($t-&gt;description) )
+                return;
+
+        echo '&lt;wp:term_description&gt;' . wxr_cdata($t-&gt;description) . '&lt;/wp:term_description&gt;';
+}
+
+/**
+ * {@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"> &lt;?php if ( $tags ) : foreach ( $tags as $t ) : ?&gt;
</span><span class="cx">         &lt;wp:tag&gt;&lt;wp:tag_slug&gt;&lt;?php echo $t-&gt;slug; ?&gt;&lt;/wp:tag_slug&gt;&lt;?php wxr_tag_name($t); ?&gt;&lt;?php wxr_tag_description($t); ?&gt;&lt;/wp:tag&gt;
</span><span class="cx"> &lt;?php endforeach; endif; ?&gt;
</span><ins>+&lt;?php if ( $terms ) : foreach ( $terms as $t ) : ?&gt;
+        &lt;wp:term&gt;&lt;wp:term_taxonomy&gt;&lt;?php echo $t-&gt;taxonomy; ?&gt;&lt;/wp:term_taxonomy&gt;&lt;wp:term_slug&gt;&lt;?php echo $t-&gt;slug; ?&gt;&lt;/wp:term_slug&gt;&lt;wp:term_parent&gt;&lt;?php echo $t-&gt;parent ? $custom_taxonomies[$t-&gt;parent]-&gt;name : ''; ?&gt;&lt;/wp:term_parent&gt;&lt;?php wxr_term_name($t); ?&gt;&lt;?php wxr_term_description($t); ?&gt;&lt;/wp:term&gt;
+&lt;?php endforeach; endif; ?&gt;
</ins><span class="cx">         &lt;?php do_action('rss2_head'); ?&gt;
</span><span class="cx">         &lt;?php if ($post_ids) {
</span><span class="cx">                 global $wp_query;
</span></span></pre>
</div>
</div>

</body>
</html>