WordPress.org

Plugin Directory

Changeset 324364 for wpcat2tag-importer


Ignore:
Timestamp:
12/19/10 20:28:26 (3 years ago)
Author:
duck_
Message:

First pass at post format support and cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wpcat2tag-importer/trunk/wpcat2tag-importer.php

    r253977 r324364  
    66Author: wordpressdotorg 
    77Author URI: http://wordpress.org/ 
    8 Version: 0.5 
     8Version: 0.5.1 
    99License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
    1010*/ 
     11 
     12/* == Todo == 
     13 * - ensure following expected behaviour in all cases... what is expected behaviour? remove+delete the old cat/tag/format? 
     14 *          ^^^ what to do on default category? (should probably leave it in place) 
     15 * - cache cleaning (think wp_delete_term does most, if not all) 
     16 * - more UI cleanup (indent for child cats, what should convert to selectors look like?, ...) 
     17 * - re-introduce select all option (old button was ugly) 
     18 * - somehow use list tables? for: probably looks better, against: poss. bulky code 
     19 */ 
    1120 
    1221if ( !defined('WP_LOAD_IMPORTERS') ) 
     
    3039if ( class_exists( 'WP_Importer' ) ) { 
    3140class WP_Categories_to_Tags extends WP_Importer { 
    32     var $categories_to_convert = array(); 
    3341    var $all_categories = array(); 
    34     var $tags_to_convert = array(); 
    3542    var $all_tags = array(); 
    3643    var $hybrids_ids = array(); 
    3744 
    38     function header() { 
    39         echo '<div class="wrap">'; 
    40         if ( ! current_user_can('manage_categories') ) { 
    41             echo '<div class="narrow">'; 
    42             echo '<p>' . __('Cheatin&#8217; uh?', 'wpcat2tag-importer') . '</p>'; 
    43             echo '</div>'; 
    44         } else { ?> 
    45             <div class="tablenav"><p style="margin:4px"><a style="display:inline;" class="button-secondary" href="admin.php?import=wpcat2tag"><?php _e( "Categories to Tags" , 'wpcat2tag-importer'); ?></a> 
    46             <a style="display:inline;" class="button-secondary" href="admin.php?import=wpcat2tag&amp;step=3"><?php _e( "Tags to Categories" , 'wpcat2tag-importer'); ?></a></p></div> 
    47 <?php   } 
     45    function header( $current_tab ) { 
     46        if ( ! current_user_can('manage_categories') ) 
     47            wp_die( __( 'Cheatin&#8217; uh?', 'wpcat2tag-importer' ) ); 
     48 
     49        $tabs = array( 
     50            'cats' => array( 'label' => __( 'Categories', 'wpcat2tag-importer' ), 'url' => admin_url( 'admin.php?import=wpcat2tag&tab=cats' ) ), 
     51            'tags' => array( 'label' => __( 'Tags', 'wpcat2tag-importer' ), 'url' => admin_url( 'admin.php?import=wpcat2tag&tab=tags' ) ) 
     52        ); 
     53 
     54        if ( function_exists( 'set_post_format' ) ) 
     55            $tabs['formats'] = array( 'label' => __( 'Formats', 'wpcat2tag-importer' ), 'url' => admin_url( 'admin.php?import=wpcat2tag&tab=formats' ) ); ?> 
     56 
     57        <div class="wrap"> 
     58        <?php screen_icon(); ?> 
     59        <h2><?php _e( 'Categories, Tags and Formats Converter', 'wpcat2tag-importer' ); ?></h2> 
     60        <h3 class="nav-tab-wrapper"> 
     61        <?php foreach ( $tabs as $tab => $info ) : 
     62            $class = ($tab == $current_tab) ? ' nav-tab-active' : ''; ?> 
     63            <a href="<?php echo $info['url']; ?>" class="nav-tab<?php echo $class; ?>"><?php echo esc_html( $info['label'] ); ?></a> 
     64        <?php endforeach; ?> 
     65        </h3> 
     66<?php 
    4867    } 
    4968 
     
    5372 
    5473    function populate_cats() { 
    55  
    56         $categories = get_categories(array('get' => 'all')); 
     74        $categories = get_categories( array('get' => 'all') ); 
    5775        foreach ( $categories as $category ) { 
    5876            $this->all_categories[] = $category; 
     
    6381 
    6482    function populate_tags() { 
    65  
    6683        $tags = get_terms( array('post_tag'), array('get' => 'all') ); 
    6784        foreach ( $tags as $tag ) { 
     
    7390 
    7491    function categories_tab() { 
     92        if ( isset($_POST['cats_to_convert']) ) { 
     93            $this->convert_categories(); 
     94            return; 
     95        } 
     96 
    7597        $this->populate_cats(); 
    76         $cat_num = count($this->all_categories); 
    77  
    78         echo '<br class="clear" />'; 
     98        $cat_num = count( $this->all_categories ); 
    7999 
    80100        if ( $cat_num > 0 ) { 
    81             screen_icon(); 
    82             echo '<h2>' . sprintf( _n( 'Convert Category to Tag.', 'Convert Categories (%d) to Tags.', $cat_num , 'wpcat2tag-importer'), $cat_num ) . '</h2>'; 
    83101            echo '<div class="narrow">'; 
    84102            echo '<p>' . __('Hey there. Here you can selectively convert existing categories to tags. To get started, check the categories you wish to be converted, then click the Convert button.', 'wpcat2tag-importer') . '</p>'; 
    85             echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.', 'wpcat2tag-importer') . '</p></div>'; 
    86  
     103            echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.', 'wpcat2tag-importer') . '</p>'; 
     104            echo '</div>'; 
    87105            $this->categories_form(); 
    88106        } else { 
     
    91109    } 
    92110 
    93     function categories_form() { ?> 
    94  
    95 <script type="text/javascript"> 
    96 /* <![CDATA[ */ 
    97 var checkflag = "false"; 
    98 function check_all_rows() { 
    99     field = document.catlist; 
    100     if ( 'false' == checkflag ) { 
    101         for ( i = 0; i < field.length; i++ ) { 
    102             if ( 'cats_to_convert[]' == field[i].name ) 
    103                 field[i].checked = true; 
    104         } 
    105         checkflag = 'true'; 
    106         return '<?php _e('Uncheck All', 'wpcat2tag-importer') ?>'; 
    107     } else { 
    108         for ( i = 0; i < field.length; i++ ) { 
    109             if ( 'cats_to_convert[]' == field[i].name ) 
    110                 field[i].checked = false; 
    111         } 
    112         checkflag = 'false'; 
    113         return '<?php _e('Check All', 'wpcat2tag-importer') ?>'; 
    114     } 
    115 } 
    116 /* ]]> */ 
    117 </script> 
    118  
    119 <form name="catlist" id="catlist" action="admin.php?import=wpcat2tag&amp;step=2" method="post"> 
    120 <p><input type="button" class="button-secondary" value="<?php esc_attr_e('Check All', 'wpcat2tag-importer'); ?>" onclick="this.value=check_all_rows()" /> 
    121 <?php wp_nonce_field('import-cat2tag'); ?></p> 
    122 <ul style="list-style:none"> 
    123  
    124 <?php   $hier = _get_term_hierarchy('category'); 
    125  
    126         foreach ($this->all_categories as $category) { 
     111    function categories_form() { 
     112        $hier = _get_term_hierarchy( 'category' ); 
     113        echo '<form name="catlist" id="catlist" action="admin.php?import=wpcat2tag&amp;tab=cats" method="post">'; 
     114        wp_nonce_field( 'import-cat2tag' ); 
     115        echo '<ul>'; 
     116 
     117        foreach ( $this->all_categories as $category ) { 
    127118            $category = sanitize_term( $category, 'category', 'display' ); 
    128119 
    129             if ( (int) $category->parent == 0 ) { ?> 
    130  
    131     <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php 
    132  
    133                  if ( in_array( intval($category->term_id),  $this->hybrids_ids ) ) 
    134                     echo ' <a href="#note"> * </a>'; 
     120            if ( (int) $category->parent == 0 ) { 
     121                echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->term_id) . '" /> ' . esc_html($category->name) . " ({$category->count})</label>"; 
     122 
     123                if ( in_array( intval($category->term_id),  $this->hybrids_ids ) ) 
     124                    echo ' <a href="#note"> * </a>'; 
    135125 
    136126                if ( isset($hier[$category->term_id]) ) 
    137                     $this->_category_children($category, $hier); ?></li> 
    138 <?php       } 
    139         } ?> 
    140 </ul> 
    141  
    142 <?php   if ( ! empty($this->hybrids_ids) ) 
    143             echo '<p><a name="note"></a>' . __('* This category is also a tag. Converting it will add that tag to all posts that are currently in the category.', 'wpcat2tag-importer') . '</p>'; ?> 
    144  
    145 <p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e('Convert Categories to Tags', 'wpcat2tag-importer'); ?>" /></p> 
    146 </form> 
    147  
    148 <?php } 
     127                    $this->_category_children($category, $hier); 
     128 
     129                echo '</li>'; 
     130            } 
     131        } 
     132 
     133        echo '</ul>'; 
     134 
     135        if ( ! empty($this->hybrids_ids) ) 
     136            echo '<p><a name="note"></a>' . __('* This category is also a tag. Converting it will add that tag to all posts that are currently in the category.', 'wpcat2tag-importer') . '</p>'; 
     137 
     138        if ( current_theme_supports( 'post-formats' ) ) : 
     139            $post_formats = get_theme_support( 'post-formats' ); 
     140            if ( is_array( $post_formats[0] ) ) : ?> 
     141<p><?php _e( 'Convert categories to:', 'wpcat2tag-importer' ); ?><br /> 
     142<label><input type="radio" name="convert_to" value="tags" checked="checked" /> <?php _e( 'Tags', 'wpcat2tag-importer' ); ?></label><br /> 
     143<label><input type="radio" name="convert_to" value="format" /> <?php _e( 'Post Format', 'wpcat2tag-importer' ); ?></label> 
     144    <select name="post_format"> 
     145    <?php foreach ( $post_formats[0] as $format ) : ?> 
     146        <option value="<?php echo esc_attr( $format ); ?>"><?php echo esc_html( get_post_format_string( $format ) ); ?></option> 
     147    <?php endforeach; ?> 
     148    </select> 
     149</p> 
     150<?php endif; endif; ?> 
     151 
     152        <p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e( 'Convert Categories', 'wpcat2tag-importer' ); ?>" /></p> 
     153        </form><?php 
     154    } 
    149155 
    150156    function tags_tab() { 
     157        if ( isset($_POST['tags_to_convert']) ) { 
     158            $this->convert_tags(); 
     159            return; 
     160        } 
     161 
    151162        $this->populate_tags(); 
    152         $tags_num = count($this->all_tags); 
    153  
    154         echo '<br class="clear" />'; 
     163        $tags_num = count( $this->all_tags ); 
    155164 
    156165        if ( $tags_num > 0 ) { 
    157             screen_icon(); 
    158             echo '<h2>' . sprintf( _n( 'Convert Tag to Category.', 'Convert Tags (%d) to Categories.', $tags_num , 'wpcat2tag-importer'), $tags_num ) . '</h2>'; 
    159166            echo '<div class="narrow">'; 
    160167            echo '<p>' . __('Here you can selectively convert existing tags to categories. To get started, check the tags you wish to be converted, then click the Convert button.', 'wpcat2tag-importer') . '</p>'; 
    161             echo '<p>' . __('The newly created categories will still be associated with the same posts.', 'wpcat2tag-importer') . '</p></div>'; 
    162  
     168            echo '<p>' . __('The newly created categories will still be associated with the same posts.', 'wpcat2tag-importer') . '</p>'; 
     169            echo '</div>'; 
    163170            $this->tags_form(); 
    164171        } else { 
     
    168175 
    169176    function tags_form() { ?> 
    170  
    171 <script type="text/javascript"> 
    172 /* <![CDATA[ */ 
    173 var checktags = "false"; 
    174 function check_all_tagrows() { 
    175     field = document.taglist; 
    176     if ( 'false' == checktags ) { 
    177         for ( i = 0; i < field.length; i++ ) { 
    178             if ( 'tags_to_convert[]' == field[i].name ) 
    179                 field[i].checked = true; 
    180         } 
    181         checktags = 'true'; 
    182         return '<?php _e('Uncheck All', 'wpcat2tag-importer') ?>'; 
    183     } else { 
    184         for ( i = 0; i < field.length; i++ ) { 
    185             if ( 'tags_to_convert[]' == field[i].name ) 
    186                 field[i].checked = false; 
    187         } 
    188         checktags = 'false'; 
    189         return '<?php _e('Check All', 'wpcat2tag-importer') ?>'; 
    190     } 
    191 } 
    192 /* ]]> */ 
    193 </script> 
    194  
    195 <form name="taglist" id="taglist" action="admin.php?import=wpcat2tag&amp;step=4" method="post"> 
    196 <p><input type="button" class="button-secondary" value="<?php esc_attr_e('Check All', 'wpcat2tag-importer'); ?>" onclick="this.value=check_all_tagrows()" /> 
    197 <?php wp_nonce_field('import-cat2tag'); ?></p> 
    198 <ul style="list-style:none"> 
     177<form name="taglist" id="taglist" action="admin.php?import=wpcat2tag&amp;tab=tags" method="post"> 
     178<?php wp_nonce_field( 'import-cat2tag' ); ?> 
     179<ul> 
    199180 
    200181<?php   foreach ( $this->all_tags as $tag ) { ?> 
    201     <li><label><input type="checkbox" name="tags_to_convert[]" value="<?php echo intval($tag->term_id); ?>" /> <?php echo esc_attr($tag->name) . ' (' . $tag->count . ')'; ?></label><?php if ( in_array( intval($tag->term_id),  $this->hybrids_ids ) ) echo ' <a href="#note"> * </a>'; ?></li> 
    202  
     182    <li><label><input type="checkbox" name="tags_to_convert[]" value="<?php echo intval($tag->term_id); ?>" /> <?php echo esc_html($tag->name) . ' (' . $tag->count . ')'; ?></label><?php if ( in_array( intval($tag->term_id),  $this->hybrids_ids ) ) echo ' <a href="#note"> * </a>'; ?></li> 
    203183<?php   } ?> 
    204184</ul> 
     
    207187            echo '<p><a name="note"></a>' . __('* This tag is also a category. When converted, all posts associated with the tag will also be in the category.', 'wpcat2tag-importer') . '</p>'; ?> 
    208188 
    209 <p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php esc_attr_e('Convert Tags to Categories', 'wpcat2tag-importer'); ?>" /></p> 
     189<?php if ( current_theme_supports( 'post-formats' ) ) : 
     190    $post_formats = get_theme_support( 'post-formats' ); 
     191    if ( is_array( $post_formats[0] ) ) : ?> 
     192<p><?php _e( 'Convert tags to:', 'wpcat2tag-importer' ); ?><br /> 
     193<label><input type="radio" name="convert_to" value="cats" checked="checked" /> <?php _e( 'Categories', 'wpcat2tag-importer' ); ?></label><br /> 
     194<label><input type="radio" name="convert_to" value="format" /> <?php _e( 'Post Format', 'wpcat2tag-importer' ); ?></label> 
     195    <select name="post_format"> 
     196    <?php foreach ( $post_formats[0] as $format ) : ?> 
     197        <option value="<?php echo esc_attr( $format ); ?>"><?php echo esc_html( get_post_format_string( $format ) ); ?></option> 
     198    <?php endforeach; ?> 
     199    </select> 
     200</p> 
     201<?php endif; endif; ?> 
     202 
     203<p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e( 'Convert Tags', 'wpcat2tag-importer' ); ?>" /></p> 
    210204</form> 
    211205 
    212206<?php } 
    213207 
    214     function _category_children($parent, $hier) { ?> 
    215  
    216         <ul style="list-style:none"> 
    217 <?php   foreach ($hier[$parent->term_id] as $child_id) { 
    218             $child =& get_category($child_id); ?> 
    219         <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label><?php 
     208    function formats_tab() { 
     209        if ( isset($_POST['post_formats']) ) { 
     210            $this->convert_formats(); 
     211            return; 
     212        } 
     213 
     214        $formats = get_terms( array('post_format'), array('get'=>'all') ); 
     215        $format_count = count( $formats ); 
     216 
     217        if ( $format_count > 0 ) { ?> 
     218            <form action="admin.php?import=wpcat2tag&amp;tab=formats" method="post"> 
     219            <?php wp_nonce_field( 'import-cat2tag' ); ?> 
     220 
     221            <ul> 
     222            <?php foreach ( $formats as $format ) : 
     223                $slug = substr( $format->slug, 12 ); ?> 
     224                <li><label><input type="checkbox" name="post_formats[]" value="<?php echo intval($format->term_id); ?>" /> <?php echo esc_html( get_post_format_string($slug) ) . " ({$format->count})"; ?></label></li> 
     225            <?php endforeach; ?> 
     226            </ul> 
     227 
     228            <p><?php _e( 'Convert formats to:', 'wpcat2tag-importer' ); ?><br /> 
     229            <label><input type="radio" name="convert_to" value="cat" checked="checked" /> <?php _e( 'Category', 'wpcat2tag-importer' ); ?></label><br /> 
     230            <label><input type="radio" name="convert_to" value="tag" /> <?php _e( 'Tag', 'wpcat2tag-importer' ); ?></label></p> 
     231            <input type="text" name="convert_to_slug" value="" /> 
     232            <p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e( 'Convert Formats', 'wpcat2tag-importer' ); ?>" /></p> 
     233            </form><?php 
     234        } else { 
     235            echo '<p>' . __( 'You have no posts set to a specific format.', 'wpcat2tag-importer' ) . '</p>'; 
     236        } 
     237    } 
     238 
     239    function convert_formats() { 
     240        check_admin_referer( 'import-cat2tag' ); 
     241 
     242        if ( ! is_array($_POST['post_formats']) || empty($_POST['convert_to_slug']) || ('cat' != $_POST['convert_to'] && 'tag' != $_POST['convert_to']) ) { 
     243            echo '<div class="narrow">'; 
     244            echo '<p>' . sprintf( __('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.', 'wpcat2tag-importer'), admin_url('admin.php?import=wpcat2tag&amp;tab=formats') ) . '</p>'; 
     245            echo '</div>'; 
     246            return; 
     247        } 
     248 
     249        $convert_to = 'tag' == $_POST['convert_to'] ? 'post_tag' : 'category'; 
     250        if ( ! $term_info = term_exists( $_POST['convert_to_slug'], $convert_to ) ) 
     251            $term_info = wp_insert_term( $_POST['convert_to_slug'], $convert_to ); 
     252 
     253        echo '<ul>'; 
     254 
     255        foreach ( $_POST['post_formats'] as $format_id ) { 
     256            $format_id = (int) $format_id; 
     257            $format = get_term( $format_id, 'post_format' ); 
     258            if ( ! $format ) { 
     259                echo '<li>' . sprintf( __( 'Post format #%d doesn&#8217;t exist!', 'wpcat2tag-importer' ), $format_id ) . "</li>\n"; 
     260            } else { 
     261                $slug = substr( $format->slug, 12 ); 
     262                echo '<li>' . sprintf( __( 'Converting format <strong>%s</strong> ... ', 'wpcat2tag-importer' ), get_post_format_string( $slug ) ); 
     263 
     264                $this->_convert_term( array( 'term_id' => $format->term_id, 'taxonomy' => 'post_format', 'term_taxonomy_id' => $format->term_taxonomy_id ), $term_info['term_taxonomy_id'] ); 
     265 
     266                echo __( 'Converted successfully.', 'wpcat2tag-importer' ) . "</li>\n"; 
     267            } 
     268        } 
     269 
     270        echo '</ul>'; 
     271        echo '<p>' . sprintf( __( 'We&#8217;re all done here, but you can always <a href="%s">convert more</a>.', 'wpcat2tag-importer' ), admin_url( 'admin.php?import=wpcat2tag&amp;tab=formats' ) ) . '</p>'; 
     272    } 
     273 
     274    function _category_children( $parent, $hier ) { 
     275        echo '<ul>'; 
     276 
     277        foreach ( $hier[$parent->term_id] as $child_id ) { 
     278            $child =& get_category($child_id); 
     279 
     280            echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="'. intval($child->term_id) .'" /> ' . esc_html($child->name) . " ({$child->count})</label>"; 
    220281 
    221282            if ( in_array( intval($child->term_id), $this->hybrids_ids ) ) 
     
    223284 
    224285            if ( isset($hier[$child->term_id]) ) 
    225                 $this->_category_children($child, $hier); ?></li> 
    226 <?php   } ?> 
    227         </ul><?php 
    228     } 
    229  
    230     function _category_exists($cat_id) { 
    231         $cat_id = (int) $cat_id; 
    232  
    233         $maybe_exists = category_exists($cat_id); 
    234  
    235         if ( $maybe_exists ) { 
    236             return true; 
    237         } else { 
    238             return false; 
    239         } 
     286                $this->_category_children($child, $hier); 
     287 
     288            echo '</li>'; 
     289        } 
     290 
     291        echo '</ul>'; 
    240292    } 
    241293 
     
    243295        global $wpdb; 
    244296 
    245         if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?> 
    246             <div class="narrow"> 
    247             <p><?php printf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag'); ?></p> 
    248             </div> 
    249 <?php       return; 
    250         } 
    251  
    252         if ( empty($this->categories_to_convert) ) 
    253             $this->categories_to_convert = $_POST['cats_to_convert']; 
    254  
    255         $hier = _get_term_hierarchy('category'); 
    256         $hybrid_cats = $clear_parents = $parents = false; 
    257         $clean_term_cache = $clean_cat_cache = array(); 
    258         $default_cat = get_option('default_category'); 
     297        check_admin_referer( 'import-cat2tag' ); 
     298 
     299        if ( ! is_array($_POST['cats_to_convert']) ) { 
     300            echo '<div class="narrow">'; 
     301            echo '<p>' . sprintf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag&amp;tab=cats') . '</p>'; 
     302            echo '</div>'; 
     303            return; 
     304        } 
     305 
     306        if ( ! isset($_POST['convert_to']) || 'format' != $_POST['convert_to'] ) { 
     307            $convert_to = 'post_tag'; 
     308        } else { 
     309            $convert_to = 'post_format'; 
     310            $term_info = $this->_get_format_info( sanitize_key($_POST['post_format']) ); 
     311            if ( is_wp_error($term_info) ) { 
     312                echo '<div class="narrow"><p>'; 
     313                echo $term_info->get_error_message() . ' '; 
     314                printf( __( 'Please <a href="%s">try again</a>.', 'wpcat2tag-importer' ), 'admin.php?import=wpcat2tag&amp;tab=cats' ); 
     315                echo '</p></div>'; 
     316                return; 
     317            } 
     318        } 
    259319 
    260320        echo '<ul>'; 
    261321 
    262         foreach ( (array) $this->categories_to_convert as $cat_id) { 
     322        foreach ( $_POST['cats_to_convert'] as $cat_id ) { 
    263323            $cat_id = (int) $cat_id; 
    264  
    265             if ( ! $this->_category_exists($cat_id) ) { 
    266                 echo '<li>' . sprintf( __('Category %s doesn&#8217;t exist!', 'wpcat2tag-importer'), $cat_id ) . "</li>\n"; 
     324            $category = get_term( $cat_id, 'category' ); 
     325            if ( ! $category ) { 
     326                echo '<li>' . sprintf( __( 'Category #%d doesn&#8217;t exist!', 'wpcat2tag-importer' ), $cat_id ) . "</li>\n"; 
    267327            } else { 
    268                 $category =& get_category($cat_id); 
    269                 echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... ', 'wpcat2tag-importer'),  $category->name); 
    270  
    271                 // If the category is the default, leave category in place and create tag. 
    272                 if ( $default_cat == $category->term_id ) { 
    273  
    274                     if ( ! ($id = term_exists( $category->slug, 'post_tag' ) ) ) 
    275                         $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug)); 
    276  
    277                     if ( is_wp_error($id) ) { 
    278                         echo $id->get_error_message() . "</li>\n"; 
    279                         continue; 
    280                     } 
    281  
    282                     $id = $id['term_taxonomy_id']; 
    283                     $posts = get_objects_in_term($category->term_id, 'category'); 
    284                     $term_order = 0; 
    285  
    286                     foreach ( $posts as $post ) { 
    287                         $values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order); 
    288                         clean_post_cache($post); 
    289                     } 
    290  
    291                     if ( $values ) { 
    292                         $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); 
    293  
    294                         $wpdb->update($wpdb->term_taxonomy, array('count' => $category->count), array('term_id' => $category->term_id, 'taxonomy' => 'post_tag') ); 
    295                     } 
    296  
    297                     echo __('Converted successfully.', 'wpcat2tag-importer') . "</li>\n"; 
    298                     continue; 
     328                echo '<li>' . sprintf( __( 'Converting category <strong>%s</strong> ... ', 'wpcat2tag-importer' ), esc_html($category->name) ); 
     329 
     330                if ( 'post_tag' == $convert_to ) { 
     331                    if ( ! $term_info = term_exists( $category->slug, 'post_tag' ) ) 
     332                        $term_info = wp_insert_term( $category->name, 'post_tag', array( 'description' => $category->description ) ); 
    299333                } 
    300334 
    301                 // if tag already exists, add it to all posts in the category 
    302                 if ( $tag_ttid = $wpdb->get_var( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $category->term_id) ) ) { 
    303                     $objects_ids = get_objects_in_term($category->term_id, 'category'); 
    304                     $tag_ttid = (int) $tag_ttid; 
    305                     $term_order = 0; 
    306  
    307                     foreach ( $objects_ids as $object_id ) 
    308                         $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tag_ttid, $term_order); 
    309  
    310                     if ( $values ) { 
    311                         $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); 
    312  
    313                         $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag_ttid) ); 
    314                         $wpdb->update($wpdb->term_taxonomy, array('count' => $count), array('term_id' => $category->term_id, 'taxonomy' => 'post_tag') ); 
    315                     } 
    316                     echo __('Tag added to all posts in this category.', 'wpcat2tag-importer') . " *</li>\n"; 
    317  
    318                     $hybrid_cats = true; 
    319                     $clean_term_cache[] = $category->term_id; 
    320                     $clean_cat_cache[] = $category->term_id; 
    321  
    322                     continue; 
     335                $this->_convert_term( array( 'term_id' => $category->term_id, 'taxonomy' => 'category', 'term_taxonomy_id' => $category->term_taxonomy_id ), $term_info['term_taxonomy_id'] ); 
     336 
     337                echo __( 'Converted successfully.', 'wpcat2tag-importer' ) . "</li>\n"; 
     338            } 
     339        } 
     340 
     341        echo '</ul>'; 
     342        echo '<p>' . sprintf( __( 'We&#8217;re all done here, but you can always <a href="%s">convert more</a>.', 'wpcat2tag-importer' ), admin_url( 'admin.php?import=wpcat2tag&amp;tab=cats' ) ) . '</p>'; 
     343    } 
     344 
     345    function convert_tags() { 
     346        check_admin_referer( 'import-cat2tag' ); 
     347 
     348        if ( ! is_array($_POST['tags_to_convert']) ) { 
     349            echo '<div class="narrow">'; 
     350            echo '<p>' . sprintf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag&amp;tab=tags') . '</p>'; 
     351            echo '</div>'; 
     352            return; 
     353        } 
     354 
     355        if ( ! isset($_POST['convert_to']) || 'format' != $_POST['convert_to'] ) { 
     356            $convert_to = 'category'; 
     357        } else { 
     358            $convert_to = 'post_format'; 
     359            $term_info = $this->_get_format_info( sanitize_key($_POST['post_format']) ); 
     360            if ( is_wp_error($term_info) ) { 
     361                echo '<div class="narrow"><p>'; 
     362                echo $term_info->get_error_message() . ' '; 
     363                printf( __( 'Please <a href="%s">try again</a>.', 'wpcat2tag-importer' ), 'admin.php?import=wpcat2tag&amp;tab=tags' ); 
     364                echo '</p></div>'; 
     365                return; 
     366            } 
     367        } 
     368 
     369        echo '<ul>'; 
     370 
     371        foreach ( $_POST['tags_to_convert'] as $tag_id ) { 
     372            $tag_id = (int) $tag_id; 
     373            $tag = get_term( $tag_id, 'post_tag' ); 
     374            if ( ! $tag ) { 
     375                echo '<li>' . sprintf( __( 'Tag #%d doesn&#8217;t exist!', 'wpcat2tag-importer' ), $tag_id ) . "</li>\n"; 
     376            } else { 
     377                echo '<li>' . sprintf( __( 'Converting tag <strong>%s</strong> ... ', 'wpcat2tag-importer' ), esc_html($tag->name) ); 
     378 
     379                if ( 'category' == $convert_to ) { 
     380                    if ( ! $term_info = term_exists( $tag->slug, 'category' ) ) 
     381                        $term_info = wp_insert_term( $tag->name, 'category', array( 'description' => $tag->description ) ); 
    323382                } 
    324383 
    325                 $tt_ids = $wpdb->get_col( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) ); 
    326                 if ( $tt_ids ) { 
    327                     $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id"); 
    328                     foreach ( (array) $posts as $post ) 
    329                         clean_post_cache($post); 
    330                 } 
    331  
    332                 // Change the category to a tag. 
    333                 $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => 'post_tag'), array('term_id' => $category->term_id, 'taxonomy' => 'category') ); 
    334  
    335                 // Set all parents to 0 (root-level) if their parent was the converted tag 
    336                 $wpdb->update($wpdb->term_taxonomy, array('parent' => 0), array('parent' => $category->term_id, 'taxonomy' => 'category') ); 
    337  
    338                 if ( $parents ) $clear_parents = true; 
    339                 $clean_cat_cache[] = $category->term_id; 
    340                 echo __('Converted successfully.', 'wpcat2tag-importer') . "</li>\n"; 
    341             } 
    342         } 
     384                $this->_convert_term( array( 'term_id' => $tag->term_id, 'taxonomy' => 'post_tag', 'term_taxonomy_id' => $tag->term_taxonomy_id ), $term_info['term_taxonomy_id'] ); 
     385 
     386                echo __( 'Converted successfully.', 'wpcat2tag-importer' ) . "</li>\n"; 
     387            } 
     388        } 
     389 
    343390        echo '</ul>'; 
    344  
    345         if ( ! empty($clean_term_cache) ) { 
    346             $clean_term_cache = array_unique(array_values($clean_term_cache)); 
    347             clean_term_cache($clean_term_cache, 'post_tag'); 
    348         } 
    349  
    350         if ( ! empty($clean_cat_cache) ) { 
    351             $clean_cat_cache = array_unique(array_values($clean_cat_cache)); 
    352             clean_term_cache($clean_cat_cache, 'category'); 
    353         } 
    354  
    355         if ( $clear_parents ) delete_option('category_children'); 
    356  
    357         if ( $hybrid_cats ) 
    358             echo '<p>' . sprintf( __('* This category is also a tag. The converter has added that tag to all posts currently in the category. If you want to remove it, please confirm that all tags were added successfully, then delete it from the <a href="%s">Manage Categories</a> page.', 'wpcat2tag-importer'), 'categories.php') . '</p>'; 
    359         echo '<p>' . sprintf( __('We&#8217;re all done here, but you can always <a href="%s">convert more</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag' ) . '</p>'; 
    360     } 
    361  
    362     function convert_tags() { 
     391        echo '<p>' . sprintf( __( 'We&#8217;re all done here, but you can always <a href="%s">convert more</a>.', 'wpcat2tag-importer' ), admin_url( 'admin.php?import=wpcat2tag&amp;tab=tags' ) ) . '</p>'; 
     392    } 
     393 
     394    /** 
     395     * Convert all term relationships to a new term, delete the old term if possible. 
     396     * 
     397     * The old term will not be deleted if it's the default category or if it's a part 
     398     * of any other taxonomies. 
     399     * 
     400     * @param array $from term_id, taxonomy and term_taxonomy_id of the term+taxonomy pair converting from 
     401     * @param int $to_ttid The term_taxonomy_id of the term+taxonomy pair we are converting to 
     402     */ 
     403    function _convert_term( $from, $to_ttid ) { 
    363404        global $wpdb; 
    364405 
    365         if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) { 
    366             echo '<div class="narrow">'; 
    367             echo '<p>' . sprintf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag&amp;step=3') . '</p>'; 
    368             echo '</div>'; 
    369             return; 
    370         } 
    371  
    372         if ( empty($this->tags_to_convert) ) 
    373             $this->tags_to_convert = $_POST['tags_to_convert']; 
    374  
    375         $hybrid_tags = $clear_parents = false; 
    376         $clean_cat_cache = $clean_term_cache = array(); 
    377         $default_cat = get_option('default_category'); 
    378         echo '<ul>'; 
    379  
    380         foreach ( (array) $this->tags_to_convert as $tag_id) { 
    381             $tag_id = (int) $tag_id; 
    382  
    383             if ( $tag = get_term( $tag_id, 'post_tag' ) ) { 
    384                 printf('<li>' . __('Converting tag <strong>%s</strong> ... ', 'wpcat2tag-importer'),  $tag->name); 
    385  
    386                 if ( $cat_ttid = $wpdb->get_var( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'category'", $tag->term_id) ) ) { 
    387                     $objects_ids = get_objects_in_term($tag->term_id, 'post_tag'); 
    388                     $cat_ttid = (int) $cat_ttid; 
    389                     $term_order = 0; 
    390  
    391                     foreach ( $objects_ids as $object_id ) { 
    392                         $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $cat_ttid, $term_order); 
    393                         clean_post_cache($object_id); 
    394                     } 
    395  
    396                     if ( $values ) { 
    397                         $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); 
    398  
    399                         if ( $default_cat != $tag->term_id ) { 
    400                             $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag->term_id) ); 
    401                             $wpdb->update($wpdb->term_taxonomy, array('count' => $count), array('term_id' => $tag->term_id, 'taxonomy' => 'category') ); 
    402                         } 
    403                     } 
    404  
    405                     $hybrid_tags = true; 
    406                     $clean_term_cache[] = $tag->term_id; 
    407                     $clean_cat_cache[] = $tag->term_id; 
    408                     echo __('All posts were added to the category with the same name.', 'wpcat2tag-importer') . " *</li>\n"; 
    409  
    410                     continue; 
    411                 } 
    412  
    413                 // Change the tag to a category. 
    414                 $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) ); 
    415                 if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) { 
    416                     $new_fields = array('taxonomy' => 'category'); 
    417                     $clear_parents = true; 
    418                 } else { 
    419                     $new_fields = array('taxonomy' => 'category', 'parent' => 0); 
    420                 } 
    421  
    422                 $wpdb->update($wpdb->term_taxonomy, $new_fields, array('term_id' => $tag->term_id, 'taxonomy' => 'post_tag') ); 
    423  
    424                 $clean_term_cache[] = $tag->term_id; 
    425                 $clean_cat_cache[] = $cat['term_id']; 
    426                 echo __('Converted successfully.', 'wpcat2tag-importer') . "</li>\n"; 
    427  
    428             } else { 
    429                 printf( '<li>' . __('Tag #%s doesn&#8217;t exist!', 'wpcat2tag-importer') . "</li>\n",  $tag_id ); 
    430             } 
    431         } 
    432  
    433         if ( ! empty($clean_term_cache) ) { 
    434             $clean_term_cache = array_unique(array_values($clean_term_cache)); 
    435             clean_term_cache($clean_term_cache, 'post_tag'); 
    436         } 
    437  
    438         if ( ! empty($clean_cat_cache) ) { 
    439             $clean_cat_cache = array_unique(array_values($clean_cat_cache)); 
    440             clean_term_cache($clean_term_cache, 'category'); 
    441         } 
    442  
    443         if ( $clear_parents ) delete_option('category_children'); 
    444  
    445         echo '</ul>'; 
    446         if ( $hybrid_tags ) 
    447             echo '<p>' . sprintf( __('* This tag is also a category. The converter has added all posts from it to the category. If you want to remove it, please confirm that all posts were added successfully, then delete it from the <a href="%s">Manage Tags</a> page.', 'wpcat2tag-importer'), 'edit-tags.php') . '</p>'; 
    448         echo '<p>' . sprintf( __('We&#8217;re all done here, but you can always <a href="%s">convert more</a>.', 'wpcat2tag-importer'), 'admin.php?import=wpcat2tag&amp;step=3' ) . '</p>'; 
     406        // transfer all the term relationships 
     407        $wpdb->update( $wpdb->term_relationships, array( 'term_taxonomy_id' => $to_ttid ), array( 'term_taxonomy_id' => $from['term_taxonomy_id'] ) ); 
     408 
     409        // remove the old term 
     410        wp_delete_term( $from['term_id'], $from['taxonomy'] ); 
     411    } 
     412 
     413    function _get_format_info( $format ) { 
     414        if ( current_theme_supports( 'post-formats' ) && ! empty( $format ) ) { 
     415            $post_formats = get_theme_support( 'post-formats' ); 
     416            if ( is_array( $post_formats ) ) { 
     417                $post_formats = $post_formats[0]; 
     418                if ( ! in_array( $format, $post_formats ) ) 
     419                    return new WP_Error( 'invalid_format', sprintf( __( 'Bad post format %s.', 'wpcat2tag-importer' ), esc_html($format) ) ); 
     420            } 
     421        } else { 
     422            return new WP_Error( 'invalid_format', __( 'Either your theme does not support post formats or you supplied an invalid format.', 'wpcat2tag-importer' ) ); 
     423        } 
     424 
     425        $format = 'post-format-' . $format; 
     426        if ( ! $term_info = term_exists( $format, 'post_format' ) ) 
     427            $term_info = wp_insert_term( $format, 'post_format' ); 
     428 
     429        return $term_info; 
    449430    } 
    450431 
    451432    function init() { 
    452  
    453         $step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1; 
    454  
    455         $this->header(); 
    456  
    457         if ( current_user_can('manage_categories') ) { 
    458  
    459             switch ($step) { 
    460                 case 1 : 
    461                     $this->categories_tab(); 
    462                 break; 
    463  
    464                 case 2 : 
    465                     check_admin_referer('import-cat2tag'); 
    466                     $this->convert_categories(); 
    467                 break; 
    468  
    469                 case 3 : 
    470                     $this->tags_tab(); 
    471                 break; 
    472  
    473                 case 4 : 
    474                     check_admin_referer('import-cat2tag'); 
    475                     $this->convert_tags(); 
    476                 break; 
    477             } 
    478         } 
     433        if ( ! current_user_can( 'manage_categories' ) ) 
     434            wp_die( __( 'Cheatin&#8217; uh?', 'wpcat2tag-importer' ) ); 
     435 
     436        $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'cats'; 
     437        $this->header( $tab ); 
     438 
     439        if ( 'cats' == $tab ) 
     440            $this->categories_tab(); 
     441        else if ( 'tags' == $tab ) 
     442            $this->tags_tab(); 
     443        else if ( 'formats' == $tab && function_exists( 'set_post_format' ) ) 
     444            $this->formats_tab(); 
    479445 
    480446        $this->footer(); 
    481447    } 
    482448 
    483     function WP_Categories_to_Tags() { 
    484         // Do nothing. 
    485     } 
     449    function WP_Categories_to_Tags() { /* Do nothing. */ } 
    486450} 
    487451 
    488452$wp_cat2tag_importer = new WP_Categories_to_Tags(); 
    489  
    490453register_importer('wpcat2tag', __('Categories and Tags Converter', 'wpcat2tag-importer'), __('Convert existing categories to tags or tags to categories, selectively.', 'wpcat2tag-importer'), array(&$wp_cat2tag_importer, 'init')); 
    491454 
Note: See TracChangeset for help on using the changeset viewer.