Changeset 7897

Show
Ignore:
Timestamp:
05/05/08 17:15:35 (7 months ago)
Author:
ryan
Message:

Show when a term is both a category and a tag, also when a child category is converted to a tag and then converted back, restore the child status, if the parent still exists. Props azaozz. fixes #6909

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/import/wp-cat2tag.php

    r7645 r7897  
    44    var $categories_to_convert = array(); 
    55    var $all_categories = array(); 
     6    var $tags_to_convert = array(); 
     7    var $all_tags = array(); 
     8    var $hybrids_ids = array(); 
    69 
    710    function header() { 
    811        echo '<div class="wrap">'; 
    9         echo '<h2>' . __('Convert Categories to Tags') . '</h2>'; 
     12        if ( ! current_user_can('manage_categories') ) { 
     13            echo '<div class="narrow">'; 
     14            echo '<p>' . __('Cheatin&#8217; uh?') . '</p>'; 
     15            echo '</div>'; 
     16        } else { ?> 
     17            <div class="tablenav"><p style="margin:4px"><a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag">Categories to Tags</a> 
     18            <a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag&amp;step=3">Tags to Categories</a></p></div> 
     19<?php   } 
    1020    } 
    1121 
     
    1424    } 
    1525 
    16     function populate_all_categories() { 
     26    function populate_cats() { 
    1727        global $wpdb; 
    1828 
    1929        $categories = get_categories('get=all'); 
    2030        foreach ( $categories as $category ) { 
    21             if ( !tag_exists($wpdb->escape($category->name)) ) 
    22                 $this->all_categories[] = $category; 
    23         } 
    24     } 
    25  
    26     function welcome() { 
    27         $this->populate_all_categories(); 
    28  
    29         echo '<div class="narrow">'; 
    30  
    31         if (count($this->all_categories) > 0) { 
     31            $this->all_categories[] = $category; 
     32            if ( tag_exists( $wpdb->escape($category->name) ) ) 
     33                $this->hybrids_ids[] = $category->term_id; 
     34        } 
     35    } 
     36 
     37    function populate_tags() { 
     38        global $wpdb; 
     39 
     40        $tags = get_terms( array('post_tag'), 'get=all' ); 
     41        foreach ( $tags as $tag ) { 
     42            $this->all_tags[] = $tag; 
     43            if ( $this->_category_exists($tag->term_id) ) 
     44                $this->hybrids_ids[] = $tag->term_id; 
     45        } 
     46    } 
     47 
     48    function categories_tab() { 
     49        $this->populate_cats(); 
     50        $cat_num = count($this->all_categories); 
     51 
     52        echo '<br class="clear" />'; 
     53 
     54        if ( $cat_num > 0 ) { 
     55            echo '<h2>Convert Categories (' . $cat_num . ') to Tags.</h2>'; 
     56            echo '<div class="narrow">'; 
    3257            echo '<p>' . __('Hey there. Here you can selectively converts existing categories to tags. To get started, check the categories you wish to be converted, then click the Convert button.') . '</p>'; 
    33             echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p>'; 
     58            echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p></div>'; 
    3459 
    3560            $this->categories_form(); 
     61        } elseif ( $hyb_num > 0 ) { 
     62             echo '<p>' . __('You have no categories that can be converted. However some of your categories are both a tag and a category.') . '</p>'; 
    3663        } else { 
    3764            echo '<p>'.__('You have no categories to convert!').'</p>'; 
    3865        } 
    39  
    40         echo '</div>'; 
    41     } 
    42  
    43     function categories_form() { 
    44 ?> 
     66    } 
     67 
     68    function categories_form() { ?> 
     69 
    4570<script type="text/javascript"> 
    46 <!-- 
     71/* <![CDATA[ */ 
    4772var checkflag = "false"; 
    4873function check_all_rows() { 
    49     field = document.formlist; 
     74    field = document.catlist; 
    5075    if ( 'false' == checkflag ) { 
    5176        for ( i = 0; i < field.length; i++ ) { 
     
    6489    } 
    6590} 
    66  
    67 //  --> 
     91/* ]]> */ 
    6892</script> 
    69 <?php 
    70        echo '<form name="formlist" id="formlist" action="admin.php?import=wp-cat2tag&amp;step=2" method="post"> 
    71         <p><input type="button" class="button-secondary" value="' . __('Check All') . '"' . ' onClick="this.value=check_all_rows()"></p>'; 
    72         wp_nonce_field('import-cat2tag'); 
    73         echo '<ul style="list-style:none">'; 
    74  
     93 
     94<form name="catlist" id="catlist" action="admin.php?import=wp-cat2tag&amp;step=2" method="post"> 
     95<p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_rows()"> 
     96<?php wp_nonce_field('import-cat2tag'); ?></p> 
     97<ul style="list-style:none"> 
     98<?php 
    7599        $hier = _get_term_hierarchy('category'); 
    76100 
     
    78102            $category = sanitize_term( $category, 'category', 'display' ); 
    79103 
    80             if ((int) $category->parent == 0) { 
    81                 echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->term_id) . '" /> ' . $category->name . ' (' . $category->count . ')</label>'; 
    82  
    83                 if (isset($hier[$category->term_id])) { 
    84                     $this->_category_children($category, $hier); 
     104            if ( (int) $category->parent == 0 ) { 
     105                if ( in_array( intval($category->term_id),  $this->hybrids_ids ) ) { 
     106?> 
     107    <li style="color:#777;padding-left:1.3em;"><?php echo $category->name . ' (' . $category->count . ') *'; ?></li> 
     108<?php 
     109                } else { 
     110?> 
     111    <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php 
     112 
     113                    if ( isset($hier[$category->term_id]) ) 
     114                        $this->_category_children($category, $hier); 
     115?></li> 
     116<?php 
    85117                } 
    86  
    87                 echo '</li>'; 
    88             } 
    89         } 
    90  
    91         echo '</ul>'; 
    92  
    93         echo '<p class="submit"><input type="submit" name="submit" class="button" value="' . __('Convert Tags') . '" /></p>'; 
    94  
    95         echo '</form>'; 
     118            } 
     119        } 
     120?> 
     121</ul> 
     122 
     123<?php 
     124        if ( ! empty($this->hybrids_ids) ) { 
     125            echo '<p>' . __('* This category is also a tag. It cannot be convert again.') . '</p>'; 
     126        } 
     127?> 
     128<p class="submit"><input type="submit" name="submit" class="button" value="<?php _e('Convert Categories to Tags'); ?>" /></p> 
     129</form> 
     130 
     131<?php 
     132    } 
     133 
     134    function tags_tab() { 
     135        $this->populate_tags(); 
     136        $tags_num = count($this->all_tags); 
     137 
     138        echo '<br class="clear" />'; 
     139 
     140        if ( $tags_num > 0 ) { 
     141            echo '<h2>Convert Tags (' . $tags_num . ') to Categories.</h2>'; 
     142            echo '<div class="narrow">'; 
     143            echo '<p>' . __('Here you can selectively converts existing tags to categories. To get started, check the tags you wish to be converted, then click the Convert button.') . '</p>'; 
     144            echo '<p>' . __('The newly created categories will still be associated with the same posts.') . '</p></div>'; 
     145             
     146             
     147            $this->tags_form(); 
     148        } elseif ( $hyb_num > 0 ) { 
     149             echo '<p>' . __('You have no tags that can be converted. However some of your tags are both a tag and a category.') . '</p>'; 
     150        } else { 
     151            echo '<p>'.__('You have no tags to convert!').'</p>'; 
     152        } 
     153    } 
     154 
     155    function tags_form() { ?> 
     156 
     157<script type="text/javascript"> 
     158/* <![CDATA[ */ 
     159var checktags = "false"; 
     160function check_all_tagrows() { 
     161    field = document.taglist; 
     162    if ( 'false' == checktags ) { 
     163        for ( i = 0; i < field.length; i++ ) { 
     164            if ( 'tags_to_convert[]' == field[i].name ) 
     165                field[i].checked = true; 
     166        } 
     167        checktags = 'true'; 
     168        return '<?php _e('Uncheck All') ?>'; 
     169    } else { 
     170        for ( i = 0; i < field.length; i++ ) { 
     171            if ( 'tags_to_convert[]' == field[i].name ) 
     172                field[i].checked = false; 
     173        } 
     174        checktags = 'false'; 
     175        return '<?php _e('Check All') ?>'; 
     176    } 
     177
     178/* ]]> */ 
     179</script> 
     180 
     181<form name="taglist" id="taglist" action="admin.php?import=wp-cat2tag&amp;step=4" method="post"> 
     182<p><input type="button" class="button-secondary" value="<?php _e('Check All'); ?>" onclick="this.value=check_all_tagrows()"> 
     183<?php wp_nonce_field('import-cat2tag'); ?></p> 
     184<ul style="list-style:none"> 
     185<?php 
     186        foreach ( $this->all_tags as $tag ) { 
     187            if ( in_array( intval($tag->term_id),  $this->hybrids_ids ) ) { 
     188?> 
     189    <li style="color:#777;padding-left:1.3em;"><?php echo attribute_escape($tag->name) . ' (' . $tag->count . ') *'; ?></li> 
     190<?php 
     191            } else { 
     192?> 
     193    <li><label><input type="checkbox" name="tags_to_convert[]" value="<?php echo intval($tag->term_id); ?>" /> <?php echo attribute_escape($tag->name) . ' (' . $tag->count . ')'; ?></label></li> 
     194 
     195<?php 
     196            } 
     197        } 
     198?> 
     199</ul> 
     200 
     201<?php    
     202        if ( ! empty($this->hybrids_ids) ) 
     203            echo '<p>' . __('* This tag is also a category. It cannot be converted again.') . '</p>'; 
     204?> 
     205 
     206<p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php _e('Convert Tags to Categories'); ?>" /></p> 
     207</form> 
     208 
     209<?php 
    96210    } 
    97211 
    98212    function _category_children($parent, $hier) { 
    99         echo '<ul style="list-style:none">'; 
    100  
     213?> 
     214 
     215        <ul style="list-style:none"> 
     216<?php 
    101217        foreach ($hier[$parent->term_id] as $child_id) { 
    102218            $child =& get_category($child_id); 
    103  
    104             echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->term_id) . '" /> ' . $child->name . ' (' . $child->count . ')</label>'; 
    105  
    106             if (isset($hier[$child->term_id])) { 
     219             
     220            if ( in_array( intval($child->term_id), $this->hybrids_ids ) ) { 
     221?> 
     222        <li style="color:#777;padding-left:1.3em;"><?php echo $child->name . ' (' . $child->count . ') *'; ?></li> 
     223<?php 
     224            } else { 
     225?> 
     226        <li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label> 
     227<?php 
     228 
     229            if ( isset($hier[$child->term_id]) ) 
    107230                $this->_category_children($child, $hier); 
    108             } 
    109  
    110             echo '</li>'; 
    111         } 
    112  
    113         echo '</ul>'; 
     231?>      </li> 
     232<?php 
     233            } 
     234        } 
     235?> 
     236        </ul> 
     237<?php 
    114238    } 
    115239 
     
    126250    } 
    127251 
    128     function convert_them() { 
     252    function convert_categories() { 
    129253        global $wpdb; 
    130254 
     
    135259            return; 
    136260        } 
    137  
    138261 
    139262        if ( empty($this->categories_to_convert) ) 
     
    200323    } 
    201324 
     325    function convert_tags() { 
     326        global $wpdb; 
     327 
     328        if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) { 
     329            echo '<div class="narrow">'; 
     330            echo '<p>' . sprintf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&amp;step=3') . '</p>'; 
     331            echo '</div>'; 
     332            return; 
     333        } 
     334 
     335        if ( empty($this->categories_to_convert) ) 
     336            $this->tags_to_convert = $_POST['tags_to_convert']; 
     337 
     338        $clean_cache = array(); 
     339        echo '<ul>'; 
     340 
     341        foreach ( (array) $this->tags_to_convert as $tag_id) { 
     342            $tag_id = (int) $tag_id; 
     343 
     344            echo '<li>' . sprintf(__('Converting tag #%s ... '),  $tag_id); 
     345 
     346            if ( ! is_term($tag_id, 'post_tag') ) { 
     347                _e('Tag doesn\'t exist!'); 
     348            } else { 
     349 
     350                if ( is_term($tag_id, 'category') ) { 
     351                    _e('This Tag is already a Category.'); 
     352                    echo '</li>'; 
     353                    continue; 
     354                } 
     355 
     356                $tt_ids = $wpdb->get_col( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag_id) ); 
     357                if ( $tt_ids ) { 
     358                        $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id"); 
     359                    foreach ( (array) $posts as $post ) 
     360                        clean_post_cache($post); 
     361                } 
     362 
     363                // Change the tag to a category. 
     364                $parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag_id) ); 
     365                if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) 
     366                    $reset_parent = ''; 
     367                else $reset_parent = ", parent = '0'"; 
     368 
     369                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'category' $reset_parent WHERE term_id = %d AND taxonomy = 'post_tag'", $tag_id) ); 
     370 
     371                // Clean the cache 
     372                $clean_cache[] = $tag_id; 
     373 
     374                _e('Converted successfully.'); 
     375            } 
     376 
     377            echo '</li>'; 
     378        } 
     379 
     380        clean_term_cache( $clean_cache, 'post_tag' ); 
     381        delete_option('category_children'); 
     382         
     383        echo '</ul>'; 
     384        echo '<p>' . sprintf( __('We&#8217;re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag&amp;step=3' ) . '</p>'; 
     385    } 
     386 
    202387    function init() { 
    203388 
     
    206391        $this->header(); 
    207392 
    208         if (!current_user_can('manage_categories')) { 
    209             echo '<div class="narrow">'; 
    210             echo '<p>' . __('Cheatin&#8217; uh?') . '</p>'; 
    211             echo '</div>'; 
    212         } else { 
    213             if ( $step > 1 ) 
    214                 check_admin_referer('import-cat2tag'); 
     393        if ( current_user_can('manage_categories') ) { 
    215394 
    216395            switch ($step) { 
    217396                case 1 : 
    218                     $this->welcome(); 
     397                    $this->categories_tab(); 
    219398                break; 
    220399 
    221400                case 2 : 
    222                     $this->convert_them(); 
     401                    check_admin_referer('import-cat2tag'); 
     402                    $this->convert_categories(); 
     403                break; 
     404                 
     405                case 3 : 
     406                    $this->tags_tab(); 
     407                break; 
     408                 
     409                case 4 : 
     410                    check_admin_referer('import-cat2tag'); 
     411                    $this->convert_tags(); 
    223412                break; 
    224413            } 
     
    235424$wp_cat2tag_importer = new WP_Categories_to_Tags(); 
    236425 
    237 register_importer('wp-cat2tag', __('Categories to Tags Converter'), __('Convert existing categories to tags, selectively.'), array(&$wp_cat2tag_importer, 'init')); 
    238  
    239 ?> 
     426register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init')); 
     427 
     428?>