| | 1 | <?php |
| | 2 | |
| | 3 | class STP_Import { |
| | 4 | |
| | 5 | function header() { |
| | 6 | echo '<div class="wrap">'; |
| | 7 | echo '<h2>'.__('Import Simple Tagging').'</h2>'; |
| | 8 | echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'<br /><br /></p>'; |
| | 9 | } |
| | 10 | |
| | 11 | function footer() { |
| | 12 | echo '</div>'; |
| | 13 | } |
| | 14 | |
| | 15 | function greet() { |
| | 16 | echo '<div class="narrow">'; |
| | 17 | echo '<p>'.__('Howdy! This imports tags from an existing Simple Tagging 1.6.2 installation into this blog using the new WordPress native tagging structure.').'</p>'; |
| | 18 | echo '<p>'.__('This has not been tested on any other versions of Simple Tagging. Mileage may vary.').'</p>'; |
| | 19 | echo '<p>'.__('To accommodate larger databases for those tag-crazy authors out there, we have made this into an easy 4-step program to help you kick that nasty Simple Tagging habit. Just keep clicking along and we will let you know when you are in the clear!').'</p>'; |
| | 20 | echo '<p><strong>'.__('Don’t be stupid - backup your database before proceeding!').'</strong></p>'; |
| | 21 | echo '<form action="admin.php?import=stp&step=1" method="post">'; |
| | 22 | echo '<p class="submit"><input type="submit" name="submit" value="'.__('Step 1 »').'" /></p>'; |
| | 23 | echo '</form>'; |
| | 24 | echo '</div>'; |
| | 25 | } |
| | 26 | |
| | 27 | |
| | 28 | function dispatch () { |
| | 29 | if ( empty( $_GET['step'] ) ) { |
| | 30 | $step = 0; |
| | 31 | } else { |
| | 32 | $step = (int) $_GET['step']; |
| | 33 | } |
| | 34 | |
| | 35 | // load the header |
| | 36 | $this->header(); |
| | 37 | |
| | 38 | switch ( $step ) { |
| | 39 | case 0 : |
| | 40 | $this->greet(); |
| | 41 | break; |
| | 42 | case 1 : |
| | 43 | $this->import_posts(); |
| | 44 | break; |
| | 45 | case 2: |
| | 46 | $this->import_t2p(); |
| | 47 | break; |
| | 48 | case 3: |
| | 49 | $this->cleanup_import(); |
| | 50 | break; |
| | 51 | } |
| | 52 | |
| | 53 | // load the footer |
| | 54 | $this->footer(); |
| | 55 | } |
| | 56 | |
| | 57 | |
| | 58 | function import_posts ( ) { |
| | 59 | echo '<div class="narrow">'; |
| | 60 | echo '<p><h3>'.__('Reading STP Post Tags…').'</h3></p>'; |
| | 61 | |
| | 62 | // read in all the STP tag -> post settings |
| | 63 | $posts = $this->get_stp_posts(); |
| | 64 | |
| | 65 | // if we didn't get any tags back, that's all there is folks! |
| | 66 | if ( !is_array($posts) ) { |
| | 67 | echo '<p>' . __('No posts were found to have tags!') . '</p>'; |
| | 68 | return false; |
| | 69 | } |
| | 70 | else { |
| | 71 | |
| | 72 | // if there's an existing entry, delete it |
| | 73 | if ( get_option('stpimp_posts') ) { |
| | 74 | delete_option('stpimp_posts'); |
| | 75 | } |
| | 76 | |
| | 77 | add_option('stpimp_posts', $posts); |
| | 78 | |
| | 79 | |
| | 80 | $count = count($posts); |
| | 81 | |
| | 82 | echo '<p>' . sprintf( __('Done! <strong>%s</strong> tag to post relationships were read.'), $count ) . '<br /></p>'; |
| | 83 | |
| | 84 | } |
| | 85 | |
| | 86 | echo '<form action="admin.php?import=stp&step=2" method="post">'; |
| | 87 | echo '<p class="submit"><input type="submit" name="submit" value="'.__('Step 2 »').'" /></p>'; |
| | 88 | echo '</form>'; |
| | 89 | echo '</div>'; |
| | 90 | |
| | 91 | } |
| | 92 | |
| | 93 | |
| | 94 | function import_t2p ( ) { |
| | 95 | |
| | 96 | echo '<div class="narrow">'; |
| | 97 | echo '<p><h3>'.__('Adding Tags to Posts…').'</h3></p>'; |
| | 98 | |
| | 99 | // run that funky magic! |
| | 100 | $tags_added = $this->tag2post(); |
| | 101 | |
| | 102 | echo '<p>' . sprintf( __('Done! <strong>%s</strong> tags where added!'), $tags_added ) . '<br /></p>'; |
| | 103 | |
| | 104 | echo '<form action="admin.php?import=stp&step=3" method="post">'; |
| | 105 | echo '<p class="submit"><input type="submit" name="submit" value="'.__('Step 3 »').'" /></p>'; |
| | 106 | echo '</form>'; |
| | 107 | echo '</div>'; |
| | 108 | |
| | 109 | } |
| | 110 | |
| | 111 | |
| | 112 | function get_stp_posts ( ) { |
| | 113 | |
| | 114 | global $wpdb; |
| | 115 | |
| | 116 | // read in all the posts from the STP post->tag table: should be wp_post2tag |
| | 117 | $posts_query = "SELECT post_id, tag_name FROM " . $wpdb->prefix . "stp_tags"; |
| | 118 | |
| | 119 | $posts = $wpdb->get_results($posts_query); |
| | 120 | |
| | 121 | return $posts; |
| | 122 | |
| | 123 | } |
| | 124 | |
| | 125 | |
| | 126 | function tag2post ( ) { |
| | 127 | |
| | 128 | // get the tags and posts we imported in the last 2 steps |
| | 129 | $posts = get_option('stpimp_posts'); |
| | 130 | |
| | 131 | // null out our results |
| | 132 | $tags_added = 0; |
| | 133 | |
| | 134 | // loop through each post and add its tags to the db |
| | 135 | foreach ( $posts as $this_post ) { |
| | 136 | |
| | 137 | $the_post = (int) $this_post->post_id; |
| | 138 | $the_tag = $this_post->tag_name; |
| | 139 | |
| | 140 | // try to add the tag |
| | 141 | wp_add_post_tags($the_post, $the_tag); |
| | 142 | |
| | 143 | $tags_added++; |
| | 144 | |
| | 145 | } |
| | 146 | |
| | 147 | // that's it, all posts should be linked to their tags properly, pending any errors we just spit out! |
| | 148 | return $tags_added; |
| | 149 | |
| | 150 | |
| | 151 | } |
| | 152 | |
| | 153 | |
| | 154 | function cleanup_import ( ) { |
| | 155 | |
| | 156 | delete_option('stpimp_posts'); |
| | 157 | |
| | 158 | $this->done(); |
| | 159 | |
| | 160 | } |
| | 161 | |
| | 162 | |
| | 163 | function done ( ) { |
| | 164 | |
| | 165 | echo '<div class="narrow">'; |
| | 166 | echo '<p><h3>'.__('Import Complete!').'</h3></p>'; |
| | 167 | |
| | 168 | echo '<p>' . __('OK, so we lied about this being a 4-step program! You’re done!') . '</p>'; |
| | 169 | |
| | 170 | echo '<p>' . __('Now wasn’t that easy?') . '</p>'; |
| | 171 | |
| | 172 | echo '</div>'; |
| | 173 | |
| | 174 | } |
| | 175 | |
| | 176 | |
| | 177 | function STP_Import ( ) { |
| | 178 | |
| | 179 | // Nothing. |
| | 180 | |
| | 181 | } |
| | 182 | |
| | 183 | } |
| | 184 | |
| | 185 | |
| | 186 | // create the import object |
| | 187 | $stp_import = new STP_Import(); |
| | 188 | |
| | 189 | // add it to the import page! |
| | 190 | register_importer('stp', 'Simple Tagging', __('Import Simple Tagging tags into the new native tagging structure.'), array($stp_import, 'dispatch')); |
| | 191 | |
| | 192 | ?> |