[44] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Simple Tags Plugin Importer
|
---|
| 4 | *
|
---|
| 5 | * @package WordPress
|
---|
| 6 | * @subpackage Importer
|
---|
| 7 | */
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * Simple Tags Plugin Tags converter class.
|
---|
| 11 | *
|
---|
| 12 | * Will convert Simple Tags Plugin tags over to the WordPress 2.3 taxonomy.
|
---|
| 13 | *
|
---|
| 14 | * @since unknown
|
---|
| 15 | */
|
---|
| 16 | class STP_Import {
|
---|
| 17 | function header() {
|
---|
| 18 | echo '<div class="wrap">';
|
---|
| 19 | screen_icon();
|
---|
| 20 | echo '<h2>'.__('Import Simple Tagging').'</h2>';
|
---|
| 21 | echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'<br /><br /></p>';
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | function footer() {
|
---|
| 25 | echo '</div>';
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | function greet() {
|
---|
| 29 | echo '<div class="narrow">';
|
---|
| 30 | echo '<p>'.__('Howdy! This imports tags from Simple Tagging 1.6.2 into WordPress tags.').'</p>';
|
---|
| 31 | echo '<p>'.__('This has not been tested on any other versions of Simple Tagging. Mileage may vary.').'</p>';
|
---|
| 32 | 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>';
|
---|
| 33 | echo '<p><strong>'.__('Don’t be stupid - backup your database before proceeding!').'</strong></p>';
|
---|
| 34 | echo '<form action="admin.php?import=stp&step=1" method="post">';
|
---|
| 35 | wp_nonce_field('import-stp');
|
---|
| 36 | echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 1').'" /></p>';
|
---|
| 37 | echo '</form>';
|
---|
| 38 | echo '</div>';
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | function dispatch () {
|
---|
| 42 | if ( empty( $_GET['step'] ) ) {
|
---|
| 43 | $step = 0;
|
---|
| 44 | } else {
|
---|
| 45 | $step = (int) $_GET['step'];
|
---|
| 46 | }
|
---|
| 47 | // load the header
|
---|
| 48 | $this->header();
|
---|
| 49 | switch ( $step ) {
|
---|
| 50 | case 0 :
|
---|
| 51 | $this->greet();
|
---|
| 52 | break;
|
---|
| 53 | case 1 :
|
---|
| 54 | check_admin_referer('import-stp');
|
---|
| 55 | $this->import_posts();
|
---|
| 56 | break;
|
---|
| 57 | case 2:
|
---|
| 58 | check_admin_referer('import-stp');
|
---|
| 59 | $this->import_t2p();
|
---|
| 60 | break;
|
---|
| 61 | case 3:
|
---|
| 62 | check_admin_referer('import-stp');
|
---|
| 63 | $this->cleanup_import();
|
---|
| 64 | break;
|
---|
| 65 | }
|
---|
| 66 | // load the footer
|
---|
| 67 | $this->footer();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | function import_posts ( ) {
|
---|
| 72 | echo '<div class="narrow">';
|
---|
| 73 | echo '<p><h3>'.__('Reading STP Post Tags…').'</h3></p>';
|
---|
| 74 |
|
---|
| 75 | // read in all the STP tag -> post settings
|
---|
| 76 | $posts = $this->get_stp_posts();
|
---|
| 77 |
|
---|
| 78 | // if we didn't get any tags back, that's all there is folks!
|
---|
| 79 | if ( !is_array($posts) ) {
|
---|
| 80 | echo '<p>' . __('No posts were found to have tags!') . '</p>';
|
---|
| 81 | return false;
|
---|
| 82 | }
|
---|
| 83 | else {
|
---|
| 84 | // if there's an existing entry, delete it
|
---|
| 85 | if ( get_option('stpimp_posts') ) {
|
---|
| 86 | delete_option('stpimp_posts');
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | add_option('stpimp_posts', $posts);
|
---|
| 90 | $count = count($posts);
|
---|
| 91 | echo '<p>' . sprintf( _n('Done! <strong>%s</strong> tag to post relationships were read.', 'Done! <strong>%s</strong> tags to post relationships were read.', $count), $count ) . '<br /></p>';
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | echo '<form action="admin.php?import=stp&step=2" method="post">';
|
---|
| 95 | wp_nonce_field('import-stp');
|
---|
| 96 | echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 2').'" /></p>';
|
---|
| 97 | echo '</form>';
|
---|
| 98 | echo '</div>';
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | function import_t2p ( ) {
|
---|
| 103 | echo '<div class="narrow">';
|
---|
| 104 | echo '<p><h3>'.__('Adding Tags to Posts…').'</h3></p>';
|
---|
| 105 |
|
---|
| 106 | // run that funky magic!
|
---|
| 107 | $tags_added = $this->tag2post();
|
---|
| 108 |
|
---|
| 109 | echo '<p>' . sprintf( _n('Done! <strong>%s</strong> tag was added!', 'Done! <strong>%s</strong> tags were added!', $tags_added), $tags_added ) . '<br /></p>';
|
---|
| 110 | echo '<form action="admin.php?import=stp&step=3" method="post">';
|
---|
| 111 | wp_nonce_field('import-stp');
|
---|
| 112 | echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 3').'" /></p>';
|
---|
| 113 | echo '</form>';
|
---|
| 114 | echo '</div>';
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | function get_stp_posts ( ) {
|
---|
| 118 | global $wpdb;
|
---|
| 119 | // read in all the posts from the STP post->tag table: should be wp_post2tag
|
---|
| 120 | $posts_query = "SELECT post_id, tag_name FROM " . $wpdb->prefix . "stp_tags";
|
---|
| 121 | $posts = $wpdb->get_results($posts_query);
|
---|
| 122 | return $posts;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | function tag2post ( ) {
|
---|
| 126 | global $wpdb;
|
---|
| 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 | $the_post = (int) $this_post->post_id;
|
---|
| 137 | $the_tag = $wpdb->escape($this_post->tag_name);
|
---|
| 138 | // try to add the tag
|
---|
| 139 | wp_add_post_tags($the_post, $the_tag);
|
---|
| 140 | $tags_added++;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | // that's it, all posts should be linked to their tags properly, pending any errors we just spit out!
|
---|
| 144 | return $tags_added;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | function cleanup_import ( ) {
|
---|
| 148 | delete_option('stpimp_posts');
|
---|
| 149 | $this->done();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | function done ( ) {
|
---|
| 153 | echo '<div class="narrow">';
|
---|
| 154 | echo '<p><h3>'.__('Import Complete!').'</h3></p>';
|
---|
| 155 | echo '<p>' . __('OK, so we lied about this being a 4-step program! You’re done!') . '</p>';
|
---|
| 156 | echo '<p>' . __('Now wasn’t that easy?') . '</p>';
|
---|
| 157 | echo '</div>';
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | function STP_Import ( ) {
|
---|
| 161 | // Nothing.
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | // create the import object
|
---|
| 166 | $stp_import = new STP_Import();
|
---|
| 167 |
|
---|
| 168 | // add it to the import page!
|
---|
| 169 | register_importer('stp', 'Simple Tagging', __('Import Simple Tagging tags into WordPress tags.'), array($stp_import, 'dispatch'));
|
---|
| 170 | ?>
|
---|