root/tags/1.3/wp-admin/import/stp.php

Revision 1069, 4.7 kB (checked in by donncha, 1 year ago)

Merge with WP 2.3 - testing use only!
Move pluggable functions out of wpmu-functions and into pluggable.php, fixes #439

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