Make WordPress Core


Ignore:
Timestamp:
10/26/2009 01:57:55 PM (17 years ago)
Author:
ryan
Message:

Export and import custom taxonomies. Props chrisscott. fixes #10012

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/import/wordpress.php

    r12104 r12109  
    2828    var $tags = array ();
    2929    var $categories = array ();
     30    var $terms = array ();
    3031
    3132    var $j = -1;
     
    121122                    preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag);
    122123                    $this->tags[] = $tag[1];
     124                    continue;
     125                }
     126                if ( false !== strpos($importline, '<wp:term>') ) {
     127                    preg_match('|<wp:term>(.*?)</wp:term>|is', $importline, $term);
     128                    $this->terms[] = $term[1];
    123129                    continue;
    124130                }
     
    336342
    337343            $tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr);
     344        }
     345    }
     346   
     347    function process_terms() {
     348        global $wpdb, $wp_taxonomies;
     349       
     350        $custom_taxonomies = $wp_taxonomies;
     351        // get rid of the standard taxonomies
     352        unset( $custom_taxonomies['category'] );
     353        unset( $custom_taxonomies['post_tag'] );
     354        unset( $custom_taxonomies['link_category'] );
     355       
     356        $custom_taxonomies = array_keys( $custom_taxonomies );
     357        $current_terms = (array) get_terms( $custom_taxonomies, 'get=all' );
     358        $taxonomies = array();
     359        foreach ( $current_terms as $term ) {
     360            if ( isset( $_terms[$term->taxonomy] ) ) {
     361                $taxonomies[$term->taxonomy] = array_merge( $taxonomies[$term->taxonomy], array($term->name) );
     362            } else {
     363                $taxonomies[$term->taxonomy] = array($term->name);
     364            }
     365        }
     366
     367        while ( $c = array_shift($this->terms) ) {
     368            $term_name = trim($this->get_tag( $c, 'wp:term_name' ));
     369            $term_taxonomy = trim($this->get_tag( $c, 'wp:term_taxonomy' ));
     370
     371            // If the term exists in the taxonomy we leave it alone
     372            if ( isset($taxonomies[$term_taxonomy] ) && in_array( $term_name, $taxonomies[$term_taxonomy] ) )
     373                continue;
     374
     375            $slug = $this->get_tag( $c, 'wp:term_slug' );
     376            $description = $this->get_tag( $c, 'wp:term_description' );
     377
     378            $termarr = compact('slug', 'description');
     379
     380            $term_ID = wp_insert_term($term_name, $this->get_tag( $c, 'wp:term_taxonomy' ), $termarr);
    338381        }
    339382    }
     
    749792        $this->process_categories();
    750793        $this->process_tags();
     794        $this->process_terms();
    751795        $result = $this->process_posts();
    752796        wp_suspend_cache_invalidation(false);
Note: See TracChangeset for help on using the changeset viewer.