Changes from branches/2.2/wp-admin/import/wordpress.php at r5426 to trunk/wp-admin/import/wordpress.php at r4742
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/import/wordpress.php
r5426 r4742 4 4 5 5 var $posts = array (); 6 var $posts_processed = array ();7 // Array of arrays. [[0] => XML fragment, [1] => New post ID]8 6 var $file; 9 7 var $id; … … 36 34 37 35 function get_tag( $string, $tag ) { 38 global $wpdb;39 36 preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return); 40 $return = $wpdb->escape( trim( $return[1] ) );37 $return = addslashes( trim( $return[1] ) ); 41 38 return $return; 42 39 } … … 66 63 $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user 67 64 if (!$user_id) { //banging my head against the desk now. 68 if ($ this->newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname65 if ($newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname 69 66 $user_id = wp_create_user($author, $pass); 70 67 $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank. … … 85 82 function get_entries() { 86 83 set_magic_quotes_runtime(0); 87 $importdata = array_map('rtrim', file($this->file)); // Read the file into an array 88 89 $this->posts = array(); 90 $this->categories = array(); 91 $num = 0; 92 $doing_entry = false; 93 foreach ($importdata as $importline) { 94 if ( false !== strpos($importline, '<wp:category>') ) { 95 preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category); 96 $this->categories[] = $category[1]; 97 continue; 98 } 99 if ( false !== strpos($importline, '<item>') ) { 100 $this->posts[$num] = ''; 101 $doing_entry = true; 102 continue; 103 } 104 if ( false !== strpos($importline, '</item>') ) { 105 $num++; 106 $doing_entry = false; 107 continue; 108 } 109 if ( $doing_entry ) { 110 $this->posts[$num] .= $importline . "\n"; 111 } 112 } 113 114 foreach ($this->posts as $post) { 115 $post_ID = (int) $this->get_tag( $post, 'wp:post_id' ); 116 if ($post_ID) { 117 $this->posts_processed[$post_ID][0] = &$post; 118 $this->posts_processed[$post_ID][1] = 0; 119 } 120 } 84 $importdata = file($this->file); // Read the file into an array 85 $importdata = implode('', $importdata); // squish it 86 $importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata); 87 preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts); 88 $this->posts = $this->posts[1]; 89 preg_match_all('|<wp:category>(.*?)</wp:category>|is', $importdata, $this->categories); 90 $this->categories = $this->categories[1]; 121 91 } 122 92 … … 181 151 echo '<ol id="authors">'; 182 152 echo '<form action="?import=wordpress&step=2&id=' . $this->id . '" method="post">'; 183 wp_nonce_field('import-wordpress');184 153 $j = -1; 185 154 foreach ($authors as $author) { … … 199 168 $file = wp_import_handle_upload(); 200 169 if ( isset($file['error']) ) { 170 $this->header(); 201 171 echo '<p>'.__('Sorry, there has been an error.').'</p>'; 202 172 echo '<p><strong>' . $file['error'] . '</strong></p>'; 173 $this->footer(); 203 174 return; 204 175 } 205 176 $this->file = $file['file']; 206 $this->id = (int)$file['id'];177 $this->id = $file['id']; 207 178 208 179 $this->get_entries(); … … 231 202 $category_parent = '0'; 232 203 else 233 $category_parent = category_exists($parent);204 $category_parent = (int) category_exists($parent); 234 205 235 206 $catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name'); … … 240 211 241 212 function process_posts() { 213 global $wpdb; 242 214 $i = -1; 243 215 echo '<ol>'; 244 245 foreach ($this->posts as $post) 246 $this->process_post($post); 216 foreach ($this->posts as $post) { 217 218 // There are only ever one of these 219 $post_title = $this->get_tag( $post, 'title' ); 220 $post_date = $this->get_tag( $post, 'wp:post_date' ); 221 $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' ); 222 $comment_status = $this->get_tag( $post, 'wp:comment_status' ); 223 $ping_status = $this->get_tag( $post, 'wp:ping_status' ); 224 $post_status = $this->get_tag( $post, 'wp:status' ); 225 $post_parent = $this->get_tag( $post, 'wp:post_parent' ); 226 $post_type = $this->get_tag( $post, 'wp:post_type' ); 227 $guid = $this->get_tag( $post, 'guid' ); 228 $post_author = $this->get_tag( $post, 'dc:creator' ); 229 230 $post_content = $this->get_tag( $post, 'content:encoded' ); 231 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $post_content); 232 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); 233 $post_content = str_replace('<br>', '<br />', $post_content); 234 $post_content = str_replace('<hr>', '<hr />', $post_content); 235 236 preg_match_all('|<category>(.*?)</category>|is', $post, $categories); 237 $categories = $categories[1]; 238 239 $cat_index = 0; 240 foreach ($categories as $category) { 241 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category))); 242 $cat_index++; 243 } 244 245 if ($post_id = post_exists($post_title, '', $post_date)) { 246 echo '<li>'; 247 printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); 248 } else { 249 echo '<li>'; 250 printf(__('Importing post <i>%s</i>...'), stripslashes($post_title)); 251 252 $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor 253 254 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt', 'guid', 'post_parent', 'post_type'); 255 $comment_post_ID = $post_id = wp_insert_post($postdata); 256 // Add categories. 257 if (0 != count($categories)) { 258 wp_create_categories($categories, $post_id); 259 } 260 } 261 262 // Now for comments 263 preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments); 264 $comments = $comments[1]; 265 $num_comments = 0; 266 if ( $comments) { foreach ($comments as $comment) { 267 $comment_author = $this->get_tag( $comment, 'wp:comment_author'); 268 $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email'); 269 $comment_author_IP = $this->get_tag( $comment, 'wp:comment_author_IP'); 270 $comment_author_url = $this->get_tag( $comment, 'wp:comment_author_url'); 271 $comment_date = $this->get_tag( $comment, 'wp:comment_date'); 272 $comment_date_gmt = $this->get_tag( $comment, 'wp:comment_date_gmt'); 273 $comment_content = $this->get_tag( $comment, 'wp:comment_content'); 274 $comment_approved = $this->get_tag( $comment, 'wp:comment_approved'); 275 $comment_type = $this->get_tag( $comment, 'wp:comment_type'); 276 $comment_parent = $this->get_tag( $comment, 'wp:comment_parent'); 277 278 if ( !comment_exists($comment_author, $comment_date) ) { 279 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_approved', 'comment_type', 'comment_parent'); 280 wp_insert_comment($commentdata); 281 $num_comments++; 282 } 283 } } 284 if ( $num_comments ) 285 printf(' '.__('(%s comments)'), $num_comments); 286 287 // Now for post meta 288 preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta); 289 $postmeta = $postmeta[1]; 290 if ( $postmeta) { foreach ($postmeta as $p) { 291 $key = $this->get_tag( $p, 'wp:meta_key' ); 292 $value = $this->get_tag( $p, 'wp:meta_value' ); 293 add_post_meta( $post_id, $key, $value ); 294 } } 295 296 $index++; 297 } 247 298 248 299 echo '</ol>'; … … 251 302 252 303 echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>'; 253 }254 255 function process_post($post) {256 global $wpdb;257 258 $post_ID = (int) $this->get_tag( $post, 'wp:post_id' );259 if ( $post_ID && !empty($this->posts_processed[$post_ID][1]) ) // Processed already260 return 0;261 262 // There are only ever one of these263 $post_title = $this->get_tag( $post, 'title' );264 $post_date = $this->get_tag( $post, 'wp:post_date' );265 $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );266 $comment_status = $this->get_tag( $post, 'wp:comment_status' );267 $ping_status = $this->get_tag( $post, 'wp:ping_status' );268 $post_status = $this->get_tag( $post, 'wp:status' );269 $post_name = $this->get_tag( $post, 'wp:post_name' );270 $post_parent = $this->get_tag( $post, 'wp:post_parent' );271 $menu_order = $this->get_tag( $post, 'wp:menu_order' );272 $post_type = $this->get_tag( $post, 'wp:post_type' );273 $guid = $this->get_tag( $post, 'guid' );274 $post_author = $this->get_tag( $post, 'dc:creator' );275 276 $post_content = $this->get_tag( $post, 'content:encoded' );277 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $post_content);278 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);279 $post_content = str_replace('<br>', '<br />', $post_content);280 $post_content = str_replace('<hr>', '<hr />', $post_content);281 282 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);283 $categories = $categories[1];284 285 $cat_index = 0;286 foreach ($categories as $category) {287 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category)));288 $cat_index++;289 }290 291 if ($post_id = post_exists($post_title, '', $post_date)) {292 echo '<li>';293 printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));294 } else {295 296 // If it has parent, process parent first.297 $post_parent = (int) $post_parent;298 if ($parent = $this->posts_processed[$post_parent]) {299 if (!$parent[1]) $this->process_post($parent[0]); // If not yet, process the parent first.300 $post_parent = $parent[1]; // New ID of the parent;301 }302 303 echo '<li>';304 printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));305 306 $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor307 308 $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');309 $comment_post_ID = $post_id = wp_insert_post($postdata);310 311 // Memorize old and new ID.312 if ( $post_id && $post_ID && $this->posts_processed[$post_ID] )313 $this->posts_processed[$post_ID][1] = $post_id; // New ID.314 315 // Add categories.316 if (count($categories) > 0) {317 $post_cats = array();318 foreach ($categories as $category) {319 $cat_ID = (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name = '$category'");320 if ($cat_ID == 0) {321 $cat_ID = wp_insert_category(array('cat_name' => $category));322 }323 $post_cats[] = $cat_ID;324 }325 wp_set_post_categories($post_id, $post_cats);326 }327 }328 329 // Now for comments330 preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);331 $comments = $comments[1];332 $num_comments = 0;333 if ( $comments) { foreach ($comments as $comment) {334 $comment_author = $this->get_tag( $comment, 'wp:comment_author');335 $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email');336 $comment_author_IP = $this->get_tag( $comment, 'wp:comment_author_IP');337 $comment_author_url = $this->get_tag( $comment, 'wp:comment_author_url');338 $comment_date = $this->get_tag( $comment, 'wp:comment_date');339 $comment_date_gmt = $this->get_tag( $comment, 'wp:comment_date_gmt');340 $comment_content = $this->get_tag( $comment, 'wp:comment_content');341 $comment_approved = $this->get_tag( $comment, 'wp:comment_approved');342 $comment_type = $this->get_tag( $comment, 'wp:comment_type');343 $comment_parent = $this->get_tag( $comment, 'wp:comment_parent');344 345 if ( !comment_exists($comment_author, $comment_date) ) {346 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_approved', 'comment_type', 'comment_parent');347 wp_insert_comment($commentdata);348 $num_comments++;349 }350 } }351 352 if ( $num_comments )353 printf(' '.__('(%s comments)'), $num_comments);354 355 // Now for post meta356 preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);357 $postmeta = $postmeta[1];358 if ( $postmeta) { foreach ($postmeta as $p) {359 $key = $this->get_tag( $p, 'wp:meta_key' );360 $value = $this->get_tag( $p, 'wp:meta_value' );361 $value = stripslashes($value); // add_post_meta() will escape.362 add_post_meta( $post_id, $key, $value );363 } }364 304 } 365 305 … … 386 326 break; 387 327 case 1 : 388 check_admin_referer('import-upload');389 328 $this->select_authors(); 390 329 break; 391 330 case 2: 392 check_admin_referer('import-wordpress');393 331 $this->import(); 394 332 break;
Note: See TracChangeset
for help on using the changeset viewer.