PHP Cross Reference of WordPress Subversion HEAD |
| [ Index ] [ Classes ] [ Functions ] [ Variables ] [ Constants ] |
[Summary view] [Print] [Text view]
1 <?php 2 3 class WP_Import { 4 5 var $posts = array (); 6 var $post_ids_processed = array (); 7 // Array of arrays. [[0] => XML fragment, [1] => New post ID] 8 var $file; 9 var $id; 10 var $mtnames = array (); 11 var $newauthornames = array (); 12 var $allauthornames = array (); 13 var $j = -1; 14 var $another_pass = false; 15 16 function header() { 17 echo '<div class="wrap">'; 18 echo '<h2>'.__('Import WordPress').'</h2>'; 19 } 20 21 function footer() { 22 echo '</div>'; 23 } 24 25 function unhtmlentities($string) { // From php.net for < 4.3 compat 26 $trans_tbl = get_html_translation_table(HTML_ENTITIES); 27 $trans_tbl = array_flip($trans_tbl); 28 return strtr($string, $trans_tbl); 29 } 30 31 function greet() { 32 echo '<div class="narrow">'; 33 echo '<p>'.__('Howdy! Upload your WordPress eXtended RSS (WXR) file and we’ll import the posts, comments, custom fields, and categories into this blog.').'</p>'; 34 echo '<p>'.__('Choose a WordPress WXR file to upload, then click Upload file and import.').'</p>'; 35 wp_import_upload_form("admin.php?import=wordpress&step=1"); 36 echo '</div>'; 37 } 38 39 function get_tag( $string, $tag ) { 40 global $wpdb; 41 preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return); 42 $return = preg_replace('|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1]); 43 $return = $wpdb->escape( trim( $return ) ); 44 return $return; 45 } 46 47 function users_form($n) { 48 global $wpdb, $testing; 49 $users = $wpdb->get_results("SELECT user_login FROM $wpdb->users ORDER BY user_login"); 50 ?><select name="userselect[<?php echo $n; ?>]"> 51 <option value="#NONE#">- Select -</option> 52 <?php 53 foreach ($users as $user) { 54 echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>'; 55 } 56 ?> 57 </select> 58 <?php 59 } 60 61 //function to check the authorname and do the mapping 62 function checkauthor($author) { 63 global $wpdb; 64 //mtnames is an array with the names in the mt import file 65 $pass = 'changeme'; 66 if (!(in_array($author, $this->mtnames))) { //a new mt author name is found 67 ++ $this->j; 68 $this->mtnames[$this->j] = $author; //add that new mt author name to an array 69 $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user 70 if (!$user_id) { //banging my head against the desk now. 71 if ($this->newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname 72 $user_id = wp_create_user($author, $pass); 73 $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank. 74 } else { 75 $user_id = wp_create_user($this->newauthornames[$this->j], $pass); 76 } 77 } else { 78 return $user_id; // return pre-existing wp username if it exists 79 } 80 } else { 81 $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array 82 $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames 83 } 84 85 return $user_id; 86 } 87 88 function get_entries($process_post_func=NULL) { 89 set_magic_quotes_runtime(0); 90 91 # $this->posts = array(); 92 # $this->categories = array(); 93 # $this->tags = array(); 94 # $num = 0; 95 96 for ($i=0; $i<2; $i++) { 97 $this->another_pass = false; 98 $doing_entry = false; 99 100 $fp = fopen($this->file, 'r'); 101 if ($fp) { 102 while ( !feof($fp) ) { 103 $importline = rtrim(fgets($fp)); 104 105 if ( false !== strpos($importline, '<wp:category>') ) { 106 preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category); 107 $this->categories[] = $category[1]; 108 continue; 109 } 110 if ( false !== strpos($importline, '<wp:tag>') ) { 111 preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag); 112 $this->tags[] = $tag[1]; 113 continue; 114 } 115 if ( false !== strpos($importline, '<item>') ) { 116 $this->post = ''; 117 $doing_entry = true; 118 continue; 119 } 120 if ( false !== strpos($importline, '</item>') ) { 121 $num++; 122 $doing_entry = false; 123 if ($process_post_func) 124 call_user_func($process_post_func, $this->post); 125 continue; 126 } 127 if ( $doing_entry ) { 128 $this->post .= $importline . "\n"; 129 } 130 } 131 132 fclose($fp); 133 } 134 135 // skip the second loop iteration unless it's needed 136 if ( !$this->another_pass ) 137 break; 138 } 139 140 } 141 142 function get_wp_authors() { 143 $this->get_entries(array(&$this, 'process_author')); 144 145 // We need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting. 146 $temp = $this->allauthornames; 147 $authors[0] = array_shift($temp); 148 $y = count($temp) + 1; 149 for ($x = 1; $x < $y; $x ++) { 150 $next = array_shift($temp); 151 if (!(in_array($next, $authors))) 152 array_push($authors, "$next"); 153 } 154 155 return $authors; 156 } 157 158 function get_authors_from_post() { 159 $formnames = array (); 160 $selectnames = array (); 161 162 foreach ($_POST['user'] as $key => $line) { 163 $newname = trim(stripslashes($line)); 164 if ($newname == '') 165 $newname = 'left_blank'; //passing author names from step 1 to step 2 is accomplished by using POST. left_blank denotes an empty entry in the form. 166 array_push($formnames, "$newname"); 167 } // $formnames is the array with the form entered names 168 169 foreach ($_POST['userselect'] as $user => $key) { 170 $selected = trim(stripslashes($key)); 171 array_push($selectnames, "$selected"); 172 } 173 174 $count = count($formnames); 175 for ($i = 0; $i < $count; $i ++) { 176 if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form 177 array_push($this->newauthornames, "$selectnames[$i]"); 178 } else { 179 array_push($this->newauthornames, "$formnames[$i]"); 180 } 181 } 182 183 } 184 185 function wp_authors_form() { 186 ?> 187 <h2><?php _e('Assign Authors'); ?></h2> 188 <p><?php _e('To make it easier for you to edit and save the imported posts and drafts, you may want to change the name of the author of the posts. For example, you may want to import all the entries as <code>admin</code>s entries.'); ?></p> 189 <p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p> 190 <?php 191 192 193 $authors = $this->get_wp_authors(); 194 echo '<ol id="authors">'; 195 echo '<form action="?import=wordpress&step=2&id=' . $this->id . '" method="post">'; 196 wp_nonce_field('import-wordpress'); 197 $j = -1; 198 foreach ($authors as $author) { 199 ++ $j; 200 echo '<li>'.__('Current author:').' <strong>'.$author.'</strong><br />'.sprintf(__('Create user %1$s or map to existing'), ' <input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30"> <br />'); 201 $this->users_form($j); 202 echo '</li>'; 203 } 204 205 echo '<input type="submit" value="Submit">'.'<br />'; 206 echo '</form>'; 207 echo '</ol>'; 208 209 } 210 211 function select_authors() { 212 $this->get_entries(array(&$this, 'process_author')); 213 $this->wp_authors_form(); 214 } 215 216 function process_categories() { 217 global $wpdb; 218 219 $cat_names = (array) get_terms('category', 'fields=names'); 220 221 while ( $c = array_shift($this->categories) ) { 222 $cat_name = trim($this->get_tag( $c, 'wp:cat_name' )); 223 224 // If the category exists we leave it alone 225 if ( in_array($cat_name, $cat_names) ) 226 continue; 227 228 $category_nicename = $this->get_tag( $c, 'wp:category_nicename' ); 229 $posts_private = (int) $this->get_tag( $c, 'wp:posts_private' ); 230 $links_private = (int) $this->get_tag( $c, 'wp:links_private' ); 231 232 $parent = $this->get_tag( $c, 'wp:category_parent' ); 233 234 if ( empty($parent) ) 235 $category_parent = '0'; 236 else 237 $category_parent = category_exists($parent); 238 239 $catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name'); 240 241 $cat_ID = wp_insert_category($catarr); 242 } 243 } 244 245 function process_tags() { 246 global $wpdb; 247 248 $tag_names = (array) get_terms('post_tag', 'fields=names'); 249 250 while ( $c = array_shift($this->tags) ) { 251 $tag_name = trim($this->get_tag( $c, 'wp:tag_name' )); 252 253 // If the category exists we leave it alone 254 if ( in_array($tag_name, $tag_names) ) 255 continue; 256 257 $slug = $this->get_tag( $c, 'wp:tag_slug' ); 258 $description = $this->get_tag( $c, 'wp:tag_description' ); 259 260 $tagarr = compact('slug', 'description'); 261 262 $tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr); 263 } 264 } 265 266 function process_author($post) { 267 $author = $this->get_tag( $post, 'dc:creator' ); 268 if ($author) 269 $this->allauthornames[] = $author; 270 } 271 272 function process_posts() { 273 return; //FIXME 274 $i = -1; 275 echo '<ol>'; 276 277 $this->get_entries(array(&$this, 'process_post')); 278 279 echo '</ol>'; 280 281 wp_import_cleanup($this->id); 282 283 echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>'; 284 } 285 286 function process_post($post) { 287 global $wpdb; 288 289 $post_ID = (int) $this->get_tag( $post, 'wp:post_id' ); 290 if ( $post_ID && !empty($this->post_ids_processed[$post_ID]) ) // Processed already 291 return 0; 292 293 // There are only ever one of these 294 $post_title = $this->get_tag( $post, 'title' ); 295 $post_date = $this->get_tag( $post, 'wp:post_date' ); 296 $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' ); 297 $comment_status = $this->get_tag( $post, 'wp:comment_status' ); 298 $ping_status = $this->get_tag( $post, 'wp:ping_status' ); 299 $post_status = $this->get_tag( $post, 'wp:status' ); 300 $post_name = $this->get_tag( $post, 'wp:post_name' ); 301 $post_parent = $this->get_tag( $post, 'wp:post_parent' ); 302 $menu_order = $this->get_tag( $post, 'wp:menu_order' ); 303 $post_type = $this->get_tag( $post, 'wp:post_type' ); 304 $guid = $this->get_tag( $post, 'guid' ); 305 $post_author = $this->get_tag( $post, 'dc:creator' ); 306 307 $post_content = $this->get_tag( $post, 'content:encoded' ); 308 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); 309 $post_content = str_replace('<br>', '<br />', $post_content); 310 $post_content = str_replace('<hr>', '<hr />', $post_content); 311 312 preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags); 313 $tags = $tags[1]; 314 315 $tag_index = 0; 316 foreach ($tags as $tag) { 317 $tags[$tag_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $tag))); 318 $tag_index++; 319 } 320 321 preg_match_all('|<category>(.*?)</category>|is', $post, $categories); 322 $categories = $categories[1]; 323 324 $cat_index = 0; 325 foreach ($categories as $category) { 326 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category))); 327 $cat_index++; 328 } 329 330 if ($post_id = post_exists($post_title, '', $post_date)) { 331 echo '<li>'; 332 printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); 333 } else { 334 335 // If it has parent, process parent first. 336 $post_parent = (int) $post_parent; 337 if ($post_parent) { 338 if ( $parent = $this->post_ids_processed[$post_parent] ) { 339 $post_parent = $parent; // new ID of the parent 340 } 341 else { 342 // wait until the parent has been processed 343 $this->another_pass = true; 344 return; 345 } 346 } 347 348 echo '<li>'; 349 printf(__('Importing post <i>%s</i>...'), stripslashes($post_title)); 350 351 $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor 352 353 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'post_name', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt', 'guid', 'post_parent', 'menu_order', 'post_type'); 354 $comment_post_ID = $post_id = wp_insert_post($postdata); 355 if ( is_wp_error( $post_id ) ) 356 return $post_id; 357 358 // Memorize old and new ID. 359 if ( $post_id && $post_ID ) { 360 $this->post_ids_processed[intval($post_ID)] = intval($post_id); 361 } 362 363 // Add categories. 364 if (count($categories) > 0) { 365 $post_cats = array(); 366 foreach ($categories as $category) { 367 $slug = sanitize_term_field('slug', $category, 0, 'category', 'db'); 368 $cat = get_term_by('slug', $slug, 'category'); 369 $cat_ID = 0; 370 if ( ! empty($cat) ) 371 $cat_ID = $cat->term_id; 372 if ($cat_ID == 0) { 373 $category = $wpdb->escape($category); 374 $cat_ID = wp_insert_category(array('cat_name' => $category)); 375 } 376 $post_cats[] = $cat_ID; 377 } 378 wp_set_post_categories($post_id, $post_cats); 379 } 380 381 // Add tags. 382 if (count($tags) > 0) { 383 $post_tags = array(); 384 foreach ($tags as $tag) { 385 $slug = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); 386 $tag_obj = get_term_by('slug', $slug, 'post_tag'); 387 $tag_id = 0; 388 if ( ! empty($tag_obj) ) 389 $tag_id = $tag_obj->term_id; 390 if ( $tag_id == 0 ) { 391 $tag = $wpdb->escape($tag); 392 $tag_id = wp_insert_term($tag, 'post_tag'); 393 $tag_id = $tag_id['term_id']; 394 } 395 $post_tags[] = intval($tag_id); 396 } 397 wp_set_post_tags($post_id, $post_tags); 398 } 399 } 400 401 // Now for comments 402 preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments); 403 $comments = $comments[1]; 404 $num_comments = 0; 405 if ( $comments) { foreach ($comments as $comment) { 406 $comment_author = $this->get_tag( $comment, 'wp:comment_author'); 407 $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email'); 408 $comment_author_IP = $this->get_tag( $comment, 'wp:comment_author_IP'); 409 $comment_author_url = $this->get_tag( $comment, 'wp:comment_author_url'); 410 $comment_date = $this->get_tag(