root/trunk/wp-admin/import/utw.php

Revision 8645, 6.8 kB (checked in by westi, 2 weeks ago)

phpdoc for wp-admin. See #7496 props santosj.

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