2008-02-05 07:47:27 +01:00
< ? php
2007-04-07 06:58:28 +02:00
class WP_Categories_to_Tags {
var $categories_to_convert = array ();
var $all_categories = array ();
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
function header () {
2007-12-16 23:07:32 +01:00
echo '<div class="wrap">' ;
echo '<h2>' . __ ( 'Convert Categories to Tags' ) . '</h2>' ;
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
function footer () {
2007-12-16 23:07:32 +01:00
echo '</div>' ;
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
function populate_all_categories () {
2007-04-10 18:57:21 +02:00
global $wpdb ;
2007-06-14 04:25:30 +02:00
2007-09-11 21:54:19 +02:00
$categories = get_categories ( 'get=all' );
foreach ( $categories as $category ) {
if ( ! tag_exists ( $wpdb -> escape ( $category -> name )) )
2008-02-05 07:47:27 +01:00
$this -> all_categories [] = $category ;
2007-09-11 21:54:19 +02:00
}
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
function welcome () {
2007-04-12 07:46:36 +02:00
$this -> populate_all_categories ();
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '<div class="narrow">' ;
2007-06-14 04:25:30 +02:00
2007-04-12 07:46:36 +02:00
if ( count ( $this -> all_categories ) > 0 ) {
2007-12-16 23:07:32 +01:00
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>' ;
echo '<p>' . __ ( 'Keep in mind that if you convert a category with child categories, the children become top-level orphans.' ) . '</p>' ;
2007-06-14 04:25:30 +02:00
2007-04-12 07:46:36 +02:00
$this -> categories_form ();
} else {
2007-12-16 23:07:32 +01:00
echo '<p>' . __ ( 'You have no categories to convert!' ) . '</p>' ;
2007-04-12 07:46:36 +02:00
}
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '</div>' ;
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
function categories_form () {
2007-12-16 23:07:32 +01:00
?>
< script type = " text/javascript " >
<!--
var checkflag = " false " ;
function check_all_rows () {
field = document . formlist ;
if ( 'false' == checkflag ) {
for ( i = 0 ; i < field . length ; i ++ ) {
if ( 'cats_to_convert[]' == field [ i ] . name )
field [ i ] . checked = true ;
}
checkflag = 'true' ;
2008-02-05 07:47:27 +01:00
return '<?php _e(' Uncheck All ') ?>' ;
2007-12-16 23:07:32 +01:00
} else {
for ( i = 0 ; i < field . length ; i ++ ) {
if ( 'cats_to_convert[]' == field [ i ] . name )
field [ i ] . checked = false ;
}
checkflag = 'false' ;
2008-02-05 07:47:27 +01:00
return '<?php _e(' Check All ') ?>' ;
2007-12-16 23:07:32 +01:00
}
}
// -->
</ script >
< ? php
echo ' < form name = " formlist " id = " formlist " action = " admin.php?import=wp-cat2tag&step=2 " method = " post " >
2008-02-21 01:27:23 +01:00
< p >< input type = " button " class = " button-secondary " value = " ' . __('Check All') . ' " ' . ' onClick = " this.value=check_all_rows() " ></ p > ' ;
2007-08-25 19:07:10 +02:00
wp_nonce_field ( 'import-cat2tag' );
2007-12-16 23:07:32 +01:00
echo '<ul style="list-style:none">' ;
2007-06-14 04:25:30 +02:00
2007-05-26 20:54:16 +02:00
$hier = _get_term_hierarchy ( 'category' );
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
foreach ( $this -> all_categories as $category ) {
2007-09-21 17:04:13 +02:00
$category = sanitize_term ( $category , 'category' , 'display' );
2008-02-05 07:47:27 +01:00
2007-05-26 20:54:16 +02:00
if (( int ) $category -> parent == 0 ) {
2007-12-16 23:07:32 +01:00
echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval ( $category -> term_id ) . '" /> ' . $category -> name . ' (' . $category -> count . ')</label>' ;
2007-06-14 04:25:30 +02:00
2007-05-26 20:54:16 +02:00
if ( isset ( $hier [ $category -> term_id ])) {
2007-04-07 06:58:28 +02:00
$this -> _category_children ( $category , $hier );
}
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '</li>' ;
2007-04-07 06:58:28 +02:00
}
}
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '</ul>' ;
2007-06-14 04:25:30 +02:00
2008-02-21 01:27:23 +01:00
echo '<p class="submit"><input type="submit" name="submit" class="button" value="' . __ ( 'Convert Tags' ) . '" /></p>' ;
2007-12-16 23:07:32 +01:00
echo '</form>' ;
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
function _category_children ( $parent , $hier ) {
2007-12-16 23:07:32 +01:00
echo '<ul style="list-style:none">' ;
2007-06-14 04:25:30 +02:00
2007-05-26 20:54:16 +02:00
foreach ( $hier [ $parent -> term_id ] as $child_id ) {
2007-04-07 06:58:28 +02:00
$child =& get_category ( $child_id );
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval ( $child -> term_id ) . '" /> ' . $child -> name . ' (' . $child -> count . ')</label>' ;
2007-06-14 04:25:30 +02:00
2007-05-26 20:54:16 +02:00
if ( isset ( $hier [ $child -> term_id ])) {
2007-04-07 06:58:28 +02:00
$this -> _category_children ( $child , $hier );
}
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '</li>' ;
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '</ul>' ;
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-04-10 18:57:21 +02:00
function _category_exists ( $cat_id ) {
$cat_id = ( int ) $cat_id ;
2007-06-14 04:25:30 +02:00
2007-05-26 20:54:16 +02:00
$maybe_exists = category_exists ( $cat_id );
2007-06-14 04:25:30 +02:00
2007-05-26 20:54:16 +02:00
if ( $maybe_exists ) {
2007-04-10 18:57:21 +02:00
return true ;
} else {
return false ;
}
}
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
function convert_them () {
global $wpdb ;
2007-06-14 04:25:30 +02:00
2007-08-30 00:47:07 +02:00
if ( ( ! isset ( $_POST [ 'cats_to_convert' ]) || ! is_array ( $_POST [ 'cats_to_convert' ])) && empty ( $this -> categories_to_convert )) {
2007-12-16 23:07:32 +01:00
echo '<div class="narrow">' ;
echo '<p>' . sprintf ( __ ( 'Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.' ), 'admin.php?import=wp-cat2tag' ) . '</p>' ;
echo '</div>' ;
2007-08-30 00:47:07 +02:00
return ;
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-05-26 20:54:16 +02:00
if ( empty ( $this -> categories_to_convert ) )
$this -> categories_to_convert = $_POST [ 'cats_to_convert' ];
$hier = _get_term_hierarchy ( 'category' );
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '<ul>' ;
2007-06-14 04:25:30 +02:00
2007-08-30 00:47:07 +02:00
foreach ( ( array ) $this -> categories_to_convert as $cat_id ) {
2007-04-07 06:58:28 +02:00
$cat_id = ( int ) $cat_id ;
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '<li>' . sprintf ( __ ( 'Converting category #%s ... ' ), $cat_id );
2007-06-14 04:25:30 +02:00
2007-04-10 18:57:21 +02:00
if ( ! $this -> _category_exists ( $cat_id )) {
2007-04-07 06:58:28 +02:00
_e ( 'Category doesn\'t exist!' );
} else {
$category =& get_category ( $cat_id );
2007-06-14 04:25:30 +02:00
2007-08-30 00:47:07 +02:00
if ( tag_exists ( $wpdb -> escape ( $category -> name )) ) {
_e ( 'Category is already a tag.' );
2007-12-16 23:07:32 +01:00
echo '</li>' ;
2007-08-30 00:47:07 +02:00
continue ;
}
2007-09-11 21:33:35 +02:00
// If the category is the default, leave category in place and create tag.
if ( get_option ( 'default_category' ) == $category -> term_id ) {
$id = wp_insert_term ( $category -> name , 'post_tag' , array ( 'slug' => $category -> slug ));
$id = $id [ 'term_taxonomy_id' ];
$posts = get_objects_in_term ( $category -> term_id , 'category' );
foreach ( $posts as $post ) {
2008-02-05 07:47:27 +01:00
if ( ! $wpdb -> get_var ( " SELECT object_id FROM $wpdb->term_relationships WHERE object_id = ' $post ' AND term_taxonomy_id = ' $id ' " ) )
2007-09-11 21:33:35 +02:00
$wpdb -> query ( " INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES (' $post ', ' $id ') " );
2007-10-09 00:39:20 +02:00
clean_post_cache ( $post );
2007-09-11 21:33:35 +02:00
}
} else {
2007-10-09 00:39:20 +02:00
$tt_ids = $wpdb -> get_col ( " SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = ' { $category -> term_id } ' AND taxonomy = 'category' " );
if ( $tt_ids ) {
$posts = $wpdb -> get_col ( " SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ( " . join ( ',' , $tt_ids ) . " ) GROUP BY object_id " );
foreach ( ( array ) $posts as $post )
clean_post_cache ( $post );
}
2007-09-11 21:33:35 +02:00
// Change the category to a tag.
$wpdb -> query ( " UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = ' { $category -> term_id } ' AND taxonomy = 'category' " );
2007-10-09 00:39:20 +02:00
$terms = $wpdb -> get_col ( " SELECT term_id FROM $wpdb->term_taxonomy WHERE parent = ' { $category -> term_id } ' AND taxonomy = 'category' " );
foreach ( ( array ) $terms as $term )
clean_category_cache ( $term );
2007-09-11 21:33:35 +02:00
// Set all parents to 0 (root-level) if their parent was the converted tag
$wpdb -> query ( " UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = ' { $category -> term_id } ' AND taxonomy = 'category' " );
}
2007-04-10 18:57:21 +02:00
// Clean the cache
2007-05-26 20:54:16 +02:00
clean_category_cache ( $category -> term_id );
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
_e ( 'Converted successfully.' );
}
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '</li>' ;
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-12-16 23:07:32 +01:00
echo '</ul>' ;
echo '<p>' . sprintf ( __ ( 'We’re all done here, but you can always <a href="%s">convert more</a>.' ), 'admin.php?import=wp-cat2tag' ) . '</p>' ;
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
function init () {
2007-06-14 04:25:30 +02:00
2007-10-11 19:50:56 +02:00
$step = ( isset ( $_GET [ 'step' ])) ? ( int ) $_GET [ 'step' ] : 1 ;
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
$this -> header ();
2007-06-14 04:25:30 +02:00
2007-04-09 18:27:22 +02:00
if ( ! current_user_can ( 'manage_categories' )) {
2007-12-16 23:07:32 +01:00
echo '<div class="narrow">' ;
echo '<p>' . __ ( 'Cheatin’ uh?' ) . '</p>' ;
echo '</div>' ;
2007-04-07 17:43:20 +02:00
} else {
2007-08-25 19:07:10 +02:00
if ( $step > 1 )
check_admin_referer ( 'import-cat2tag' );
2007-04-07 17:43:20 +02:00
switch ( $step ) {
case 1 :
$this -> welcome ();
2007-04-07 06:58:28 +02:00
break ;
2007-06-14 04:25:30 +02:00
2007-04-07 17:43:20 +02:00
case 2 :
$this -> convert_them ();
2007-04-07 06:58:28 +02:00
break ;
2007-04-07 17:43:20 +02:00
}
2007-04-07 06:58:28 +02:00
}
2007-06-14 04:25:30 +02:00
2007-04-07 06:58:28 +02:00
$this -> footer ();
}
2007-06-14 04:25:30 +02:00
2007-04-07 21:04:57 +02:00
function WP_Categories_to_Tags () {
// Do nothing.
}
2007-04-07 06:58:28 +02:00
}
2007-04-07 21:04:57 +02:00
$wp_cat2tag_importer = new WP_Categories_to_Tags ();
2007-04-07 06:58:28 +02:00
register_importer ( 'wp-cat2tag' , __ ( 'Categories to Tags Converter' ), __ ( 'Convert existing categories to tags, selectively.' ), array ( & $wp_cat2tag_importer , 'init' ));
2007-12-16 23:07:32 +01:00
?>