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