raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * The Ultimate Tag Warrior Importer.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Importer
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * Ultimate Tag Warrior Converter to 2.3 taxonomy.
mp-wp_genesis 11 *
mp-wp_genesis 12 * This converts the Ultimate Tag Warrior tags to the 2.3 WordPress taxonomy.
mp-wp_genesis 13 *
mp-wp_genesis 14 * @since 2.3.0
mp-wp_genesis 15 */
mp-wp_genesis 16 class UTW_Import {
mp-wp_genesis 17
mp-wp_genesis 18 function header() {
mp-wp_genesis 19 echo '<div class="wrap">';
mp-wp_genesis 20 screen_icon();
mp-wp_genesis 21 echo '<h2>'.__('Import Ultimate Tag Warrior').'</h2>';
mp-wp_genesis 22 echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'<br /><br /></p>';
mp-wp_genesis 23 }
mp-wp_genesis 24
mp-wp_genesis 25 function footer() {
mp-wp_genesis 26 echo '</div>';
mp-wp_genesis 27 }
mp-wp_genesis 28
mp-wp_genesis 29 function greet() {
mp-wp_genesis 30 echo '<div class="narrow">';
mp-wp_genesis 31 echo '<p>'.__('Howdy! This imports tags from Ultimate Tag Warrior 3 into WordPress tags.').'</p>';
mp-wp_genesis 32 echo '<p>'.__('This has not been tested on any other versions of Ultimate Tag Warrior. Mileage may vary.').'</p>';
mp-wp_genesis 33 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>';
mp-wp_genesis 34 echo '<p><strong>'.__('Don&#8217;t be stupid - backup your database before proceeding!').'</strong></p>';
mp-wp_genesis 35 echo '<form action="admin.php?import=utw&amp;step=1" method="post">';
mp-wp_genesis 36 echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.__('Step 1').'" /></p>';
mp-wp_genesis 37 echo '</form>';
mp-wp_genesis 38 echo '</div>';
mp-wp_genesis 39 }
mp-wp_genesis 40
mp-wp_genesis 41
mp-wp_genesis 42 function dispatch () {
mp-wp_genesis 43 if ( empty( $_GET['step'] ) ) {
mp-wp_genesis 44 $step = 0;
mp-wp_genesis 45 } else {
mp-wp_genesis 46 $step = (int) $_GET['step'];
mp-wp_genesis 47 }
mp-wp_genesis 48
mp-wp_genesis 49 if ( $step > 1 )
mp-wp_genesis 50 check_admin_referer('import-utw');
mp-wp_genesis 51
mp-wp_genesis 52 // load the header
mp-wp_genesis 53 $this->header();
mp-wp_genesis 54
mp-wp_genesis 55 switch ( $step ) {
mp-wp_genesis 56 case 0 :
mp-wp_genesis 57 $this->greet();
mp-wp_genesis 58 break;
mp-wp_genesis 59 case 1 :
mp-wp_genesis 60 $this->import_tags();
mp-wp_genesis 61 break;
mp-wp_genesis 62 case 2 :
mp-wp_genesis 63 $this->import_posts();
mp-wp_genesis 64 break;
mp-wp_genesis 65 case 3:
mp-wp_genesis 66 $this->import_t2p();
mp-wp_genesis 67 break;
mp-wp_genesis 68 case 4:
mp-wp_genesis 69 $this->cleanup_import();
mp-wp_genesis 70 break;
mp-wp_genesis 71 }
mp-wp_genesis 72
mp-wp_genesis 73 // load the footer
mp-wp_genesis 74 $this->footer();
mp-wp_genesis 75 }
mp-wp_genesis 76
mp-wp_genesis 77
mp-wp_genesis 78 function import_tags ( ) {
mp-wp_genesis 79 echo '<div class="narrow">';
mp-wp_genesis 80 echo '<p><h3>'.__('Reading UTW Tags&#8230;').'</h3></p>';
mp-wp_genesis 81
mp-wp_genesis 82 $tags = $this->get_utw_tags();
mp-wp_genesis 83
mp-wp_genesis 84 // if we didn't get any tags back, that's all there is folks!
mp-wp_genesis 85 if ( !is_array($tags) ) {
mp-wp_genesis 86 echo '<p>' . __('No Tags Found!') . '</p>';
mp-wp_genesis 87 return false;
mp-wp_genesis 88 }
mp-wp_genesis 89 else {
mp-wp_genesis 90
mp-wp_genesis 91 // if there's an existing entry, delete it
mp-wp_genesis 92 if ( get_option('utwimp_tags') ) {
mp-wp_genesis 93 delete_option('utwimp_tags');
mp-wp_genesis 94 }
mp-wp_genesis 95
mp-wp_genesis 96 add_option('utwimp_tags', $tags);
mp-wp_genesis 97
mp-wp_genesis 98
mp-wp_genesis 99 $count = count($tags);
mp-wp_genesis 100
mp-wp_genesis 101 echo '<p>' . sprintf( __ngettext('Done! <strong>%s</strong> tag were read.', 'Done! <strong>%s</strong> tags were read.', $count), $count ) . '<br /></p>';
mp-wp_genesis 102 echo '<p>' . __('The following tags were found:') . '</p>';
mp-wp_genesis 103
mp-wp_genesis 104 echo '<ul>';
mp-wp_genesis 105
mp-wp_genesis 106 foreach ( $tags as $tag_id => $tag_name ) {
mp-wp_genesis 107
mp-wp_genesis 108 echo '<li>' . $tag_name . '</li>';
mp-wp_genesis 109
mp-wp_genesis 110 }
mp-wp_genesis 111
mp-wp_genesis 112 echo '</ul>';
mp-wp_genesis 113
mp-wp_genesis 114 echo '<br />';
mp-wp_genesis 115
mp-wp_genesis 116 echo '<p>' . __('If you don&#8217;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>';
mp-wp_genesis 117
mp-wp_genesis 118
mp-wp_genesis 119 }
mp-wp_genesis 120
mp-wp_genesis 121 echo '<form action="admin.php?import=utw&amp;step=2" method="post">';
mp-wp_genesis 122 wp_nonce_field('import-utw');
mp-wp_genesis 123 echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.__('Step 2').'" /></p>';
mp-wp_genesis 124 echo '</form>';
mp-wp_genesis 125 echo '</div>';
mp-wp_genesis 126 }
mp-wp_genesis 127
mp-wp_genesis 128
mp-wp_genesis 129 function import_posts ( ) {
mp-wp_genesis 130 echo '<div class="narrow">';
mp-wp_genesis 131 echo '<p><h3>'.__('Reading UTW Post Tags&#8230;').'</h3></p>';
mp-wp_genesis 132
mp-wp_genesis 133 // read in all the UTW tag -> post settings
mp-wp_genesis 134 $posts = $this->get_utw_posts();
mp-wp_genesis 135
mp-wp_genesis 136 // if we didn't get any tags back, that's all there is folks!
mp-wp_genesis 137 if ( !is_array($posts) ) {
mp-wp_genesis 138 echo '<p>' . __('No posts were found to have tags!') . '</p>';
mp-wp_genesis 139 return false;
mp-wp_genesis 140 }
mp-wp_genesis 141 else {
mp-wp_genesis 142
mp-wp_genesis 143 // if there's an existing entry, delete it
mp-wp_genesis 144 if ( get_option('utwimp_posts') ) {
mp-wp_genesis 145 delete_option('utwimp_posts');
mp-wp_genesis 146 }
mp-wp_genesis 147
mp-wp_genesis 148 add_option('utwimp_posts', $posts);
mp-wp_genesis 149
mp-wp_genesis 150
mp-wp_genesis 151 $count = count($posts);
mp-wp_genesis 152
mp-wp_genesis 153 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>';
mp-wp_genesis 154
mp-wp_genesis 155 }
mp-wp_genesis 156
mp-wp_genesis 157 echo '<form action="admin.php?import=utw&amp;step=3" method="post">';
mp-wp_genesis 158 wp_nonce_field('import-utw');
mp-wp_genesis 159 echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.__('Step 3').'" /></p>';
mp-wp_genesis 160 echo '</form>';
mp-wp_genesis 161 echo '</div>';
mp-wp_genesis 162
mp-wp_genesis 163 }
mp-wp_genesis 164
mp-wp_genesis 165
mp-wp_genesis 166 function import_t2p ( ) {
mp-wp_genesis 167
mp-wp_genesis 168 echo '<div class="narrow">';
mp-wp_genesis 169 echo '<p><h3>'.__('Adding Tags to Posts&#8230;').'</h3></p>';
mp-wp_genesis 170
mp-wp_genesis 171 // run that funky magic!
mp-wp_genesis 172 $tags_added = $this->tag2post();
mp-wp_genesis 173
mp-wp_genesis 174 echo '<p>' . sprintf( __ngettext( 'Done! <strong>%s</strong> tag were added!', 'Done! <strong>%s</strong> tags were added!', $tags_added ), $tags_added ) . '<br /></p>';
mp-wp_genesis 175
mp-wp_genesis 176 echo '<form action="admin.php?import=utw&amp;step=4" method="post">';
mp-wp_genesis 177 wp_nonce_field('import-utw');
mp-wp_genesis 178 echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.__('Step 4').'" /></p>';
mp-wp_genesis 179 echo '</form>';
mp-wp_genesis 180 echo '</div>';
mp-wp_genesis 181
mp-wp_genesis 182 }
mp-wp_genesis 183
mp-wp_genesis 184
mp-wp_genesis 185 function get_utw_tags ( ) {
mp-wp_genesis 186
mp-wp_genesis 187 global $wpdb;
mp-wp_genesis 188
mp-wp_genesis 189 // read in all the tags from the UTW tags table: should be wp_tags
mp-wp_genesis 190 $tags_query = "SELECT tag_id, tag FROM " . $wpdb->prefix . "tags";
mp-wp_genesis 191
mp-wp_genesis 192 $tags = $wpdb->get_results($tags_query);
mp-wp_genesis 193
mp-wp_genesis 194 // rearrange these tags into something we can actually use
mp-wp_genesis 195 foreach ( $tags as $tag ) {
mp-wp_genesis 196
mp-wp_genesis 197 $new_tags[$tag->tag_id] = $tag->tag;
mp-wp_genesis 198
mp-wp_genesis 199 }
mp-wp_genesis 200
mp-wp_genesis 201 return $new_tags;
mp-wp_genesis 202
mp-wp_genesis 203 }
mp-wp_genesis 204
mp-wp_genesis 205 function get_utw_posts ( ) {
mp-wp_genesis 206
mp-wp_genesis 207 global $wpdb;
mp-wp_genesis 208
mp-wp_genesis 209 // read in all the posts from the UTW post->tag table: should be wp_post2tag
mp-wp_genesis 210 $posts_query = "SELECT tag_id, post_id FROM " . $wpdb->prefix . "post2tag";
mp-wp_genesis 211
mp-wp_genesis 212 $posts = $wpdb->get_results($posts_query);
mp-wp_genesis 213
mp-wp_genesis 214 return $posts;
mp-wp_genesis 215
mp-wp_genesis 216 }
mp-wp_genesis 217
mp-wp_genesis 218
mp-wp_genesis 219 function tag2post ( ) {
mp-wp_genesis 220
mp-wp_genesis 221 // get the tags and posts we imported in the last 2 steps
mp-wp_genesis 222 $tags = get_option('utwimp_tags');
mp-wp_genesis 223 $posts = get_option('utwimp_posts');
mp-wp_genesis 224
mp-wp_genesis 225 // null out our results
mp-wp_genesis 226 $tags_added = 0;
mp-wp_genesis 227
mp-wp_genesis 228 // loop through each post and add its tags to the db
mp-wp_genesis 229 foreach ( $posts as $this_post ) {
mp-wp_genesis 230
mp-wp_genesis 231 $the_post = (int) $this_post->post_id;
mp-wp_genesis 232 $the_tag = (int) $this_post->tag_id;
mp-wp_genesis 233
mp-wp_genesis 234 // what's the tag name for that id?
mp-wp_genesis 235 $the_tag = $tags[$the_tag];
mp-wp_genesis 236
mp-wp_genesis 237 // screw it, just try to add the tag
mp-wp_genesis 238 wp_add_post_tags($the_post, $the_tag);
mp-wp_genesis 239
mp-wp_genesis 240 $tags_added++;
mp-wp_genesis 241
mp-wp_genesis 242 }
mp-wp_genesis 243
mp-wp_genesis 244 // that's it, all posts should be linked to their tags properly, pending any errors we just spit out!
mp-wp_genesis 245 return $tags_added;
mp-wp_genesis 246
mp-wp_genesis 247
mp-wp_genesis 248 }
mp-wp_genesis 249
mp-wp_genesis 250
mp-wp_genesis 251 function cleanup_import ( ) {
mp-wp_genesis 252
mp-wp_genesis 253 delete_option('utwimp_tags');
mp-wp_genesis 254 delete_option('utwimp_posts');
mp-wp_genesis 255
mp-wp_genesis 256 $this->done();
mp-wp_genesis 257
mp-wp_genesis 258 }
mp-wp_genesis 259
mp-wp_genesis 260
mp-wp_genesis 261 function done ( ) {
mp-wp_genesis 262
mp-wp_genesis 263 echo '<div class="narrow">';
mp-wp_genesis 264 echo '<p><h3>'.__('Import Complete!').'</h3></p>';
mp-wp_genesis 265
mp-wp_genesis 266 echo '<p>' . __('OK, so we lied about this being a 5-step program! You&#8217;re done!') . '</p>';
mp-wp_genesis 267
mp-wp_genesis 268 echo '<p>' . __('Now wasn&#8217;t that easy?') . '</p>';
mp-wp_genesis 269
mp-wp_genesis 270 echo '</div>';
mp-wp_genesis 271
mp-wp_genesis 272 }
mp-wp_genesis 273
mp-wp_genesis 274
mp-wp_genesis 275 function UTW_Import ( ) {
mp-wp_genesis 276
mp-wp_genesis 277 // Nothing.
mp-wp_genesis 278
mp-wp_genesis 279 }
mp-wp_genesis 280
mp-wp_genesis 281 }
mp-wp_genesis 282
mp-wp_genesis 283
mp-wp_genesis 284 // create the import object
mp-wp_genesis 285 $utw_import = new UTW_Import();
mp-wp_genesis 286
mp-wp_genesis 287 // add it to the import page!
mp-wp_genesis 288 register_importer('utw', 'Ultimate Tag Warrior', __('Import Ultimate Tag Warrior tags into WordPress tags.'), array($utw_import, 'dispatch'));
mp-wp_genesis 289
mp-wp_genesis 290 ?>