2007-05-04 17:28:50 -04:00
< ? php
2008-08-14 02:30:38 -04:00
/**
* The Ultimate Tag Warrior Importer .
*
* @ package WordPress
* @ subpackage Importer
*/
2007-05-04 17:28:50 -04:00
2008-08-14 02:30:38 -04:00
/**
* Ultimate Tag Warrior Converter to 2.3 taxonomy .
*
* This converts the Ultimate Tag Warrior tags to the 2.3 WordPress taxonomy .
*
* @ since 2.3 . 0
*/
2007-05-04 17:28:50 -04:00
class UTW_Import {
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function header () {
echo '<div class="wrap">' ;
echo '<h2>' . __ ( 'Import Ultimate Tag Warrior' ) . '</h2>' ;
echo '<p>' . __ ( 'Steps may take a few minutes depending on the size of your database. Please be patient.' ) . '<br /><br /></p>' ;
}
function footer () {
echo '</div>' ;
}
function greet () {
echo '<div class="narrow">' ;
2008-02-27 16:47:52 -05:00
echo '<p>' . __ ( 'Howdy! This imports tags from Ultimate Tag Warrior 3 into WordPress tags.' ) . '</p>' ;
2007-05-04 17:28:50 -04:00
echo '<p>' . __ ( 'This has not been tested on any other versions of Ultimate Tag Warrior. Mileage may vary.' ) . '</p>' ;
echo '<p>' . __ ( 'To accommodate larger databases for those tag-crazy authors out there, we have made this into an easy 5-step program to help you kick that nasty UTW habit. Just keep clicking along and we will let you know when you are in the clear!' ) . '</p>' ;
echo '<p><strong>' . __ ( 'Don’t be stupid - backup your database before proceeding!' ) . '</strong></p>' ;
echo '<form action="admin.php?import=utw&step=1" method="post">' ;
2008-10-27 16:41:05 -04:00
echo '<p class="submit"><input type="submit" name="submit" class="button" value="' . __ ( 'Step 1' ) . '" /></p>' ;
2007-05-04 17:28:50 -04:00
echo '</form>' ;
echo '</div>' ;
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function dispatch () {
if ( empty ( $_GET [ 'step' ] ) ) {
$step = 0 ;
} else {
$step = ( int ) $_GET [ 'step' ];
}
2007-09-03 19:19:20 -04:00
2007-08-25 13:07:10 -04:00
if ( $step > 1 )
check_admin_referer ( 'import-utw' );
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// load the header
$this -> header ();
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
switch ( $step ) {
case 0 :
$this -> greet ();
break ;
case 1 :
$this -> import_tags ();
break ;
case 2 :
$this -> import_posts ();
break ;
case 3 :
$this -> import_t2p ();
break ;
case 4 :
$this -> cleanup_import ();
break ;
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// load the footer
$this -> footer ();
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function import_tags ( ) {
echo '<div class="narrow">' ;
echo '<p><h3>' . __ ( 'Reading UTW Tags…' ) . '</h3></p>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
$tags = $this -> get_utw_tags ();
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// if we didn't get any tags back, that's all there is folks!
if ( ! is_array ( $tags ) ) {
echo '<p>' . __ ( 'No Tags Found!' ) . '</p>' ;
return false ;
}
else {
// if there's an existing entry, delete it
if ( get_option ( 'utwimp_tags' ) ) {
delete_option ( 'utwimp_tags' );
}
add_option ( 'utwimp_tags' , $tags );
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
$count = count ( $tags );
2007-06-13 22:25:30 -04:00
2007-12-20 12:05:06 -05:00
echo '<p>' . sprintf ( __ngettext ( 'Done! <strong>%s</strong> tag were read.' , 'Done! <strong>%s</strong> tags were read.' , $count ), $count ) . '<br /></p>' ;
2007-05-04 17:28:50 -04:00
echo '<p>' . __ ( 'The following tags were found:' ) . '</p>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
echo '<ul>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
foreach ( $tags as $tag_id => $tag_name ) {
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
echo '<li>' . $tag_name . '</li>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
echo '</ul>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
echo '<br />' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
echo '<p>' . __ ( 'If you don’t want to import any of these tags, you should delete them from the UTW tag management page and then re-run this import.' ) . '</p>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
echo '<form action="admin.php?import=utw&step=2" method="post">' ;
2007-08-25 13:07:10 -04:00
wp_nonce_field ( 'import-utw' );
2008-10-27 16:41:05 -04:00
echo '<p class="submit"><input type="submit" name="submit" class="button" value="' . __ ( 'Step 2' ) . '" /></p>' ;
2007-05-04 17:28:50 -04:00
echo '</form>' ;
echo '</div>' ;
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function import_posts ( ) {
echo '<div class="narrow">' ;
echo '<p><h3>' . __ ( 'Reading UTW Post Tags…' ) . '</h3></p>' ;
// read in all the UTW tag -> post settings
$posts = $this -> get_utw_posts ();
// if we didn't get any tags back, that's all there is folks!
if ( ! is_array ( $posts ) ) {
echo '<p>' . __ ( 'No posts were found to have tags!' ) . '</p>' ;
return false ;
}
else {
// if there's an existing entry, delete it
if ( get_option ( 'utwimp_posts' ) ) {
delete_option ( 'utwimp_posts' );
}
add_option ( 'utwimp_posts' , $posts );
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
$count = count ( $posts );
2007-06-13 22:25:30 -04:00
2007-12-20 12:05:06 -05:00
echo '<p>' . sprintf ( __ngettext ( 'Done! <strong>%s</strong> tag to post relationships were read.' , 'Done! <strong>%s</strong> tags to post relationships were read.' , $count ), $count ) . '<br /></p>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
echo '<form action="admin.php?import=utw&step=3" method="post">' ;
2007-08-25 13:07:10 -04:00
wp_nonce_field ( 'import-utw' );
2008-10-27 16:41:05 -04:00
echo '<p class="submit"><input type="submit" name="submit" class="button" value="' . __ ( 'Step 3' ) . '" /></p>' ;
2007-05-04 17:28:50 -04:00
echo '</form>' ;
echo '</div>' ;
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function import_t2p ( ) {
echo '<div class="narrow">' ;
echo '<p><h3>' . __ ( 'Adding Tags to Posts…' ) . '</h3></p>' ;
// run that funky magic!
$tags_added = $this -> tag2post ();
2007-06-13 22:25:30 -04:00
2007-12-20 12:05:06 -05:00
echo '<p>' . sprintf ( __ngettext ( 'Done! <strong>%s</strong> tag were added!' , 'Done! <strong>%s</strong> tags were added!' , $tags_added ), $tags_added ) . '<br /></p>' ;
2007-05-04 17:28:50 -04:00
echo '<form action="admin.php?import=utw&step=4" method="post">' ;
2007-08-25 13:07:10 -04:00
wp_nonce_field ( 'import-utw' );
2008-10-27 16:41:05 -04:00
echo '<p class="submit"><input type="submit" name="submit" class="button" value="' . __ ( 'Step 4' ) . '" /></p>' ;
2007-05-04 17:28:50 -04:00
echo '</form>' ;
echo '</div>' ;
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function get_utw_tags ( ) {
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
global $wpdb ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// read in all the tags from the UTW tags table: should be wp_tags
$tags_query = " SELECT tag_id, tag FROM " . $wpdb -> prefix . " tags " ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
$tags = $wpdb -> get_results ( $tags_query );
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// rearrange these tags into something we can actually use
foreach ( $tags as $tag ) {
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
$new_tags [ $tag -> tag_id ] = $tag -> tag ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
return $new_tags ;
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function get_utw_posts ( ) {
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
global $wpdb ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// read in all the posts from the UTW post->tag table: should be wp_post2tag
$posts_query = " SELECT tag_id, post_id FROM " . $wpdb -> prefix . " post2tag " ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
$posts = $wpdb -> get_results ( $posts_query );
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
return $posts ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function tag2post ( ) {
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// get the tags and posts we imported in the last 2 steps
$tags = get_option ( 'utwimp_tags' );
$posts = get_option ( 'utwimp_posts' );
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// null out our results
$tags_added = 0 ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// loop through each post and add its tags to the db
foreach ( $posts as $this_post ) {
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
$the_post = ( int ) $this_post -> post_id ;
$the_tag = ( int ) $this_post -> tag_id ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// what's the tag name for that id?
$the_tag = $tags [ $the_tag ];
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// screw it, just try to add the tag
wp_add_post_tags ( $the_post , $the_tag );
$tags_added ++ ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// that's it, all posts should be linked to their tags properly, pending any errors we just spit out!
return $tags_added ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function cleanup_import ( ) {
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
delete_option ( 'utwimp_tags' );
delete_option ( 'utwimp_posts' );
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
$this -> done ();
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function done ( ) {
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
echo '<div class="narrow">' ;
echo '<p><h3>' . __ ( 'Import Complete!' ) . '</h3></p>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
echo '<p>' . __ ( 'OK, so we lied about this being a 5-step program! You’re done!' ) . '</p>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
echo '<p>' . __ ( 'Now wasn’t that easy?' ) . '</p>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
echo '</div>' ;
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
function UTW_Import ( ) {
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
// Nothing.
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
2007-06-13 22:25:30 -04:00
2007-05-04 17:28:50 -04:00
}
// create the import object
$utw_import = new UTW_Import ();
// add it to the import page!
2008-02-27 16:47:52 -05:00
register_importer ( 'utw' , 'Ultimate Tag Warrior' , __ ( 'Import Ultimate Tag Warrior tags into WordPress tags.' ), array ( $utw_import , 'dispatch' ));
2007-05-04 17:28:50 -04:00
2007-09-28 13:37:16 -04:00
?>