raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress Importer
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Importer
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * WordPress Importer
mp-wp_genesis 11 *
mp-wp_genesis 12 * Will process the WordPress eXtended RSS files that you upload from the export
mp-wp_genesis 13 * file.
mp-wp_genesis 14 *
mp-wp_genesis 15 * @since unknown
mp-wp_genesis 16 */
mp-wp_genesis 17 class WP_Import {
mp-wp_genesis 18
mp-wp_genesis 19 var $post_ids_processed = array ();
mp-wp_genesis 20 var $orphans = array ();
mp-wp_genesis 21 var $file;
mp-wp_genesis 22 var $id;
mp-wp_genesis 23 var $mtnames = array ();
mp-wp_genesis 24 var $newauthornames = array ();
mp-wp_genesis 25 var $allauthornames = array ();
mp-wp_genesis 26
mp-wp_genesis 27 var $author_ids = array ();
mp-wp_genesis 28 var $tags = array ();
mp-wp_genesis 29 var $categories = array ();
mp-wp_genesis 30
mp-wp_genesis 31 var $j = -1;
mp-wp_genesis 32 var $fetch_attachments = false;
mp-wp_genesis 33 var $url_remap = array ();
mp-wp_genesis 34
mp-wp_genesis 35 function header() {
mp-wp_genesis 36 echo '<div class="wrap">';
mp-wp_genesis 37 screen_icon();
mp-wp_genesis 38 echo '<h2>'.__('Import WordPress').'</h2>';
mp-wp_genesis 39 }
mp-wp_genesis 40
mp-wp_genesis 41 function footer() {
mp-wp_genesis 42 echo '</div>';
mp-wp_genesis 43 }
mp-wp_genesis 44
mp-wp_genesis 45 function unhtmlentities($string) { // From php.net for < 4.3 compat
mp-wp_genesis 46 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
mp-wp_genesis 47 $trans_tbl = array_flip($trans_tbl);
mp-wp_genesis 48 return strtr($string, $trans_tbl);
mp-wp_genesis 49 }
mp-wp_genesis 50
mp-wp_genesis 51 function greet() {
mp-wp_genesis 52 echo '<div class="narrow">';
mp-wp_genesis 53 echo '<p>'.__('Howdy! Upload your WordPress eXtended RSS (WXR) file and we&#8217;ll import the posts, pages, comments, custom fields, categories, and tags into this blog.').'</p>';
mp-wp_genesis 54 echo '<p>'.__('Choose a WordPress WXR file to upload, then click Upload file and import.').'</p>';
mp-wp_genesis 55 wp_import_upload_form("admin.php?import=wordpress&amp;step=1");
mp-wp_genesis 56 echo '</div>';
mp-wp_genesis 57 }
mp-wp_genesis 58
mp-wp_genesis 59 function get_tag( $string, $tag ) {
mp-wp_genesis 60 global $wpdb;
mp-wp_genesis 61 preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return);
mp-wp_genesis 62 $return = preg_replace('|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1]);
mp-wp_genesis 63 $return = $wpdb->escape( trim( $return ) );
mp-wp_genesis 64 return $return;
mp-wp_genesis 65 }
mp-wp_genesis 66
mp-wp_genesis 67 function has_gzip() {
mp-wp_genesis 68 return is_callable('gzopen');
mp-wp_genesis 69 }
mp-wp_genesis 70
mp-wp_genesis 71 function fopen($filename, $mode='r') {
mp-wp_genesis 72 if ( $this->has_gzip() )
mp-wp_genesis 73 return gzopen($filename, $mode);
mp-wp_genesis 74 return fopen($filename, $mode);
mp-wp_genesis 75 }
mp-wp_genesis 76
mp-wp_genesis 77 function feof($fp) {
mp-wp_genesis 78 if ( $this->has_gzip() )
mp-wp_genesis 79 return gzeof($fp);
mp-wp_genesis 80 return feof($fp);
mp-wp_genesis 81 }
mp-wp_genesis 82
mp-wp_genesis 83 function fgets($fp, $len=8192) {
mp-wp_genesis 84 if ( $this->has_gzip() )
mp-wp_genesis 85 return gzgets($fp, $len);
mp-wp_genesis 86 return fgets($fp, $len);
mp-wp_genesis 87 }
mp-wp_genesis 88
mp-wp_genesis 89 function fclose($fp) {
mp-wp_genesis 90 if ( $this->has_gzip() )
mp-wp_genesis 91 return gzclose($fp);
mp-wp_genesis 92 return fclose($fp);
mp-wp_genesis 93 }
mp-wp_genesis 94
mp-wp_genesis 95 function get_entries($process_post_func=NULL) {
mp-wp_genesis 96 set_magic_quotes_runtime(0);
mp-wp_genesis 97
mp-wp_genesis 98 $doing_entry = false;
mp-wp_genesis 99 $is_wxr_file = false;
mp-wp_genesis 100
mp-wp_genesis 101 $fp = $this->fopen($this->file, 'r');
mp-wp_genesis 102 if ($fp) {
mp-wp_genesis 103 while ( !$this->feof($fp) ) {
mp-wp_genesis 104 $importline = rtrim($this->fgets($fp));
mp-wp_genesis 105
mp-wp_genesis 106 // this doesn't check that the file is perfectly valid but will at least confirm that it's not the wrong format altogether
mp-wp_genesis 107 if ( !$is_wxr_file && preg_match('|xmlns:wp="http://wordpress[.]org/export/\d+[.]\d+/"|', $importline) )
mp-wp_genesis 108 $is_wxr_file = true;
mp-wp_genesis 109
mp-wp_genesis 110 if ( false !== strpos($importline, '<wp:base_site_url>') ) {
mp-wp_genesis 111 preg_match('|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url);
mp-wp_genesis 112 $this->base_url = $url[1];
mp-wp_genesis 113 continue;
mp-wp_genesis 114 }
mp-wp_genesis 115 if ( false !== strpos($importline, '<wp:category>') ) {
mp-wp_genesis 116 preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category);
mp-wp_genesis 117 $this->categories[] = $category[1];
mp-wp_genesis 118 continue;
mp-wp_genesis 119 }
mp-wp_genesis 120 if ( false !== strpos($importline, '<wp:tag>') ) {
mp-wp_genesis 121 preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag);
mp-wp_genesis 122 $this->tags[] = $tag[1];
mp-wp_genesis 123 continue;
mp-wp_genesis 124 }
mp-wp_genesis 125 if ( false !== strpos($importline, '<item>') ) {
mp-wp_genesis 126 $this->post = '';
mp-wp_genesis 127 $doing_entry = true;
mp-wp_genesis 128 continue;
mp-wp_genesis 129 }
mp-wp_genesis 130 if ( false !== strpos($importline, '</item>') ) {
mp-wp_genesis 131 $doing_entry = false;
mp-wp_genesis 132 if ($process_post_func)
mp-wp_genesis 133 call_user_func($process_post_func, $this->post);
mp-wp_genesis 134 continue;
mp-wp_genesis 135 }
mp-wp_genesis 136 if ( $doing_entry ) {
mp-wp_genesis 137 $this->post .= $importline . "\n";
mp-wp_genesis 138 }
mp-wp_genesis 139 }
mp-wp_genesis 140
mp-wp_genesis 141 $this->fclose($fp);
mp-wp_genesis 142 }
mp-wp_genesis 143
mp-wp_genesis 144 return $is_wxr_file;
mp-wp_genesis 145
mp-wp_genesis 146 }
mp-wp_genesis 147
mp-wp_genesis 148 function get_wp_authors() {
mp-wp_genesis 149 // 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.
mp-wp_genesis 150 $temp = $this->allauthornames;
mp-wp_genesis 151 $authors[0] = array_shift($temp);
mp-wp_genesis 152 $y = count($temp) + 1;
mp-wp_genesis 153 for ($x = 1; $x < $y; $x ++) {
mp-wp_genesis 154 $next = array_shift($temp);
mp-wp_genesis 155 if (!(in_array($next, $authors)))
mp-wp_genesis 156 array_push($authors, "$next");
mp-wp_genesis 157 }
mp-wp_genesis 158
mp-wp_genesis 159 return $authors;
mp-wp_genesis 160 }
mp-wp_genesis 161
mp-wp_genesis 162 function get_authors_from_post() {
mp-wp_genesis 163 global $current_user;
mp-wp_genesis 164
mp-wp_genesis 165 // this will populate $this->author_ids with a list of author_names => user_ids
mp-wp_genesis 166
mp-wp_genesis 167 foreach ( $_POST['author_in'] as $i => $in_author_name ) {
mp-wp_genesis 168
mp-wp_genesis 169 if ( !empty($_POST['user_select'][$i]) ) {
mp-wp_genesis 170 // an existing user was selected in the dropdown list
mp-wp_genesis 171 $user = get_userdata( intval($_POST['user_select'][$i]) );
mp-wp_genesis 172 if ( isset($user->ID) )
mp-wp_genesis 173 $this->author_ids[$in_author_name] = $user->ID;
mp-wp_genesis 174 }
mp-wp_genesis 175 elseif ( $this->allow_create_users() ) {
mp-wp_genesis 176 // nothing was selected in the dropdown list, so we'll use the name in the text field
mp-wp_genesis 177
mp-wp_genesis 178 $new_author_name = trim($_POST['user_create'][$i]);
mp-wp_genesis 179 // if the user didn't enter a name, assume they want to use the same name as in the import file
mp-wp_genesis 180 if ( empty($new_author_name) )
mp-wp_genesis 181 $new_author_name = $in_author_name;
mp-wp_genesis 182
mp-wp_genesis 183 $user_id = username_exists($new_author_name);
mp-wp_genesis 184 if ( !$user_id ) {
mp-wp_genesis 185 $user_id = wp_create_user($new_author_name, wp_generate_password());
mp-wp_genesis 186 }
mp-wp_genesis 187
mp-wp_genesis 188 $this->author_ids[$in_author_name] = $user_id;
mp-wp_genesis 189 }
mp-wp_genesis 190
mp-wp_genesis 191 // failsafe: if the user_id was invalid, default to the current user
mp-wp_genesis 192 if ( empty($this->author_ids[$in_author_name]) ) {
mp-wp_genesis 193 $this->author_ids[$in_author_name] = intval($current_user->ID);
mp-wp_genesis 194 }
mp-wp_genesis 195 }
mp-wp_genesis 196
mp-wp_genesis 197 }
mp-wp_genesis 198
mp-wp_genesis 199 function wp_authors_form() {
mp-wp_genesis 200 ?>
mp-wp_genesis 201 <?php screen_icon(); ?>
mp-wp_genesis 202 <h2><?php _e('Assign Authors'); ?></h2>
mp-wp_genesis 203 <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>
mp-wp_genesis 204 <?php
mp-wp_genesis 205 if ( $this->allow_create_users() ) {
mp-wp_genesis 206 echo '<p>'.__('If a new user is created by WordPress, a password will be randomly generated. Manually change the user\'s details if necessary.')."</p>\n";
mp-wp_genesis 207 }
mp-wp_genesis 208
mp-wp_genesis 209
mp-wp_genesis 210 $authors = $this->get_wp_authors();
mp-wp_genesis 211 echo '<form action="?import=wordpress&amp;step=2&amp;id=' . $this->id . '" method="post">';
mp-wp_genesis 212 wp_nonce_field('import-wordpress');
mp-wp_genesis 213 echo '<ol id="authors">';
mp-wp_genesis 214 $j = -1;
mp-wp_genesis 215 foreach ($authors as $author) {
mp-wp_genesis 216 ++ $j;
mp-wp_genesis 217 echo '<li>'.__('Import author:').' <strong>'.$author.'</strong><br />';
mp-wp_genesis 218 $this->users_form($j, $author);
mp-wp_genesis 219 echo '</li>';
mp-wp_genesis 220 }
mp-wp_genesis 221
mp-wp_genesis 222 if ( $this->allow_fetch_attachments() ) {
mp-wp_genesis 223 ?>
mp-wp_genesis 224 </ol>
mp-wp_genesis 225 <?php screen_icon(); ?>
mp-wp_genesis 226 <h2><?php _e('Import Attachments'); ?></h2>
mp-wp_genesis 227 <p>
mp-wp_genesis 228 <input type="checkbox" value="1" name="attachments" id="import-attachments" />
mp-wp_genesis 229 <label for="import-attachments"><?php _e('Download and import file attachments') ?></label>
mp-wp_genesis 230 </p>
mp-wp_genesis 231
mp-wp_genesis 232 <?php
mp-wp_genesis 233 }
mp-wp_genesis 234
mp-wp_genesis 235 echo '<p class="submit">';
mp-wp_genesis 236 echo '<input type="submit" class="button" value="'.attribute_escape( __('Submit') ).'" />'.'<br />';
mp-wp_genesis 237 echo '</p>';
mp-wp_genesis 238 echo '</form>';
mp-wp_genesis 239
mp-wp_genesis 240 }
mp-wp_genesis 241
mp-wp_genesis 242 function users_form($n, $author) {
mp-wp_genesis 243
mp-wp_genesis 244 if ( $this->allow_create_users() ) {
mp-wp_genesis 245 printf('<label>'.__('Create user %1$s or map to existing'), ' <input type="text" value="'.$author.'" name="'.'user_create['.intval($n).']'.'" maxlength="30" /></label> <br />');
mp-wp_genesis 246 }
mp-wp_genesis 247 else {
mp-wp_genesis 248 echo __('Map to existing').'<br />';
mp-wp_genesis 249 }
mp-wp_genesis 250
mp-wp_genesis 251 // keep track of $n => $author name
mp-wp_genesis 252 echo '<input type="hidden" name="author_in['.intval($n).']" value="'.htmlspecialchars($author).'" />';
mp-wp_genesis 253
mp-wp_genesis 254 $users = get_users_of_blog();
mp-wp_genesis 255 ?><select name="user_select[<?php echo $n; ?>]">
mp-wp_genesis 256 <option value="0"><?php _e('- Select -'); ?></option>
mp-wp_genesis 257 <?php
mp-wp_genesis 258 foreach ($users as $user) {
mp-wp_genesis 259 echo '<option value="'.$user->user_id.'">'.$user->user_login.'</option>';
mp-wp_genesis 260 }
mp-wp_genesis 261 ?>
mp-wp_genesis 262 </select>
mp-wp_genesis 263 <?php
mp-wp_genesis 264 }
mp-wp_genesis 265
mp-wp_genesis 266 function select_authors() {
mp-wp_genesis 267 $is_wxr_file = $this->get_entries(array(&$this, 'process_author'));
mp-wp_genesis 268 if ( $is_wxr_file ) {
mp-wp_genesis 269 $this->wp_authors_form();
mp-wp_genesis 270 }
mp-wp_genesis 271 else {
mp-wp_genesis 272 echo '<h2>'.__('Invalid file').'</h2>';
mp-wp_genesis 273 echo '<p>'.__('Please upload a valid WXR (WordPress eXtended RSS) export file.').'</p>';
mp-wp_genesis 274 }
mp-wp_genesis 275 }
mp-wp_genesis 276
mp-wp_genesis 277 // fetch the user ID for a given author name, respecting the mapping preferences
mp-wp_genesis 278 function checkauthor($author) {
mp-wp_genesis 279 global $current_user;
mp-wp_genesis 280
mp-wp_genesis 281 if ( !empty($this->author_ids[$author]) )
mp-wp_genesis 282 return $this->author_ids[$author];
mp-wp_genesis 283
mp-wp_genesis 284 // failsafe: map to the current user
mp-wp_genesis 285 return $current_user->ID;
mp-wp_genesis 286 }
mp-wp_genesis 287
mp-wp_genesis 288
mp-wp_genesis 289
mp-wp_genesis 290 function process_categories() {
mp-wp_genesis 291 global $wpdb;
mp-wp_genesis 292
mp-wp_genesis 293 $cat_names = (array) get_terms('category', 'fields=names');
mp-wp_genesis 294
mp-wp_genesis 295 while ( $c = array_shift($this->categories) ) {
mp-wp_genesis 296 $cat_name = trim($this->get_tag( $c, 'wp:cat_name' ));
mp-wp_genesis 297
mp-wp_genesis 298 // If the category exists we leave it alone
mp-wp_genesis 299 if ( in_array($cat_name, $cat_names) )
mp-wp_genesis 300 continue;
mp-wp_genesis 301
mp-wp_genesis 302 $category_nicename = $this->get_tag( $c, 'wp:category_nicename' );
mp-wp_genesis 303 $posts_private = (int) $this->get_tag( $c, 'wp:posts_private' );
mp-wp_genesis 304 $links_private = (int) $this->get_tag( $c, 'wp:links_private' );
mp-wp_genesis 305
mp-wp_genesis 306 $parent = $this->get_tag( $c, 'wp:category_parent' );
mp-wp_genesis 307
mp-wp_genesis 308 if ( empty($parent) )
mp-wp_genesis 309 $category_parent = '0';
mp-wp_genesis 310 else
mp-wp_genesis 311 $category_parent = category_exists($parent);
mp-wp_genesis 312
mp-wp_genesis 313 $catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name');
mp-wp_genesis 314
mp-wp_genesis 315 $cat_ID = wp_insert_category($catarr);
mp-wp_genesis 316 }
mp-wp_genesis 317 }
mp-wp_genesis 318
mp-wp_genesis 319 function process_tags() {
mp-wp_genesis 320 global $wpdb;
mp-wp_genesis 321
mp-wp_genesis 322 $tag_names = (array) get_terms('post_tag', 'fields=names');
mp-wp_genesis 323
mp-wp_genesis 324 while ( $c = array_shift($this->tags) ) {
mp-wp_genesis 325 $tag_name = trim($this->get_tag( $c, 'wp:tag_name' ));
mp-wp_genesis 326
mp-wp_genesis 327 // If the category exists we leave it alone
mp-wp_genesis 328 if ( in_array($tag_name, $tag_names) )
mp-wp_genesis 329 continue;
mp-wp_genesis 330
mp-wp_genesis 331 $slug = $this->get_tag( $c, 'wp:tag_slug' );
mp-wp_genesis 332 $description = $this->get_tag( $c, 'wp:tag_description' );
mp-wp_genesis 333
mp-wp_genesis 334 $tagarr = compact('slug', 'description');
mp-wp_genesis 335
mp-wp_genesis 336 $tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr);
mp-wp_genesis 337 }
mp-wp_genesis 338 }
mp-wp_genesis 339
mp-wp_genesis 340 function process_author($post) {
mp-wp_genesis 341 $author = $this->get_tag( $post, 'dc:creator' );
mp-wp_genesis 342 if ($author)
mp-wp_genesis 343 $this->allauthornames[] = $author;
mp-wp_genesis 344 }
mp-wp_genesis 345
mp-wp_genesis 346 function process_posts() {
mp-wp_genesis 347 echo '<ol>';
mp-wp_genesis 348
mp-wp_genesis 349 $this->get_entries(array(&$this, 'process_post'));
mp-wp_genesis 350
mp-wp_genesis 351 echo '</ol>';
mp-wp_genesis 352
mp-wp_genesis 353 wp_import_cleanup($this->id);
mp-wp_genesis 354 do_action('import_done', 'wordpress');
mp-wp_genesis 355
mp-wp_genesis 356 echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
mp-wp_genesis 357 }
mp-wp_genesis 358
mp-wp_genesis 359 function process_post($post) {
mp-wp_genesis 360 global $wpdb;
mp-wp_genesis 361
mp-wp_genesis 362 $post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
mp-wp_genesis 363 if ( $post_ID && !empty($this->post_ids_processed[$post_ID]) ) // Processed already
mp-wp_genesis 364 return 0;
mp-wp_genesis 365
mp-wp_genesis 366 set_time_limit( 60 );
mp-wp_genesis 367
mp-wp_genesis 368 // There are only ever one of these
mp-wp_genesis 369 $post_title = $this->get_tag( $post, 'title' );
mp-wp_genesis 370 $post_date = $this->get_tag( $post, 'wp:post_date' );
mp-wp_genesis 371 $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
mp-wp_genesis 372 $comment_status = $this->get_tag( $post, 'wp:comment_status' );
mp-wp_genesis 373 $ping_status = $this->get_tag( $post, 'wp:ping_status' );
mp-wp_genesis 374 $post_status = $this->get_tag( $post, 'wp:status' );
mp-wp_genesis 375 $post_name = $this->get_tag( $post, 'wp:post_name' );
mp-wp_genesis 376 $post_parent = $this->get_tag( $post, 'wp:post_parent' );
mp-wp_genesis 377 $menu_order = $this->get_tag( $post, 'wp:menu_order' );
mp-wp_genesis 378 $post_type = $this->get_tag( $post, 'wp:post_type' );
mp-wp_genesis 379 $post_password = $this->get_tag( $post, 'wp:post_password' );
mp-wp_genesis 380 $guid = $this->get_tag( $post, 'guid' );
mp-wp_genesis 381 $post_author = $this->get_tag( $post, 'dc:creator' );
mp-wp_genesis 382
mp-wp_genesis 383 $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
mp-wp_genesis 384 $post_excerpt = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_excerpt);
mp-wp_genesis 385 $post_excerpt = str_replace('<br>', '<br />', $post_excerpt);
mp-wp_genesis 386 $post_excerpt = str_replace('<hr>', '<hr />', $post_excerpt);
mp-wp_genesis 387
mp-wp_genesis 388 $post_content = $this->get_tag( $post, 'content:encoded' );
mp-wp_genesis 389 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
mp-wp_genesis 390 $post_content = str_replace('<br>', '<br />', $post_content);
mp-wp_genesis 391 $post_content = str_replace('<hr>', '<hr />', $post_content);
mp-wp_genesis 392
mp-wp_genesis 393 preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags);
mp-wp_genesis 394 $tags = $tags[1];
mp-wp_genesis 395
mp-wp_genesis 396 $tag_index = 0;
mp-wp_genesis 397 foreach ($tags as $tag) {
mp-wp_genesis 398 $tags[$tag_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $tag)));
mp-wp_genesis 399 $tag_index++;
mp-wp_genesis 400 }
mp-wp_genesis 401
mp-wp_genesis 402 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
mp-wp_genesis 403 $categories = $categories[1];
mp-wp_genesis 404
mp-wp_genesis 405 $cat_index = 0;
mp-wp_genesis 406 foreach ($categories as $category) {
mp-wp_genesis 407 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category)));
mp-wp_genesis 408 $cat_index++;
mp-wp_genesis 409 }
mp-wp_genesis 410
mp-wp_genesis 411 $post_exists = post_exists($post_title, '', $post_date);
mp-wp_genesis 412
mp-wp_genesis 413 if ( $post_exists ) {
mp-wp_genesis 414 echo '<li>';
mp-wp_genesis 415 printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
mp-wp_genesis 416 $comment_post_ID = $post_id = $post_exists;
mp-wp_genesis 417 } else {
mp-wp_genesis 418
mp-wp_genesis 419 // If it has parent, process parent first.
mp-wp_genesis 420 $post_parent = (int) $post_parent;
mp-wp_genesis 421 if ($post_parent) {
mp-wp_genesis 422 // if we already know the parent, map it to the local ID
mp-wp_genesis 423 if ( $parent = $this->post_ids_processed[$post_parent] ) {
mp-wp_genesis 424 $post_parent = $parent; // new ID of the parent
mp-wp_genesis 425 }
mp-wp_genesis 426 else {
mp-wp_genesis 427 // record the parent for later
mp-wp_genesis 428 $this->orphans[intval($post_ID)] = $post_parent;
mp-wp_genesis 429 }
mp-wp_genesis 430 }
mp-wp_genesis 431
mp-wp_genesis 432 echo '<li>';
mp-wp_genesis 433
mp-wp_genesis 434 $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
mp-wp_genesis 435
mp-wp_genesis 436 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', 'post_title', 'post_status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', 'menu_order', 'post_type', 'post_password');
mp-wp_genesis 437 $postdata['import_id'] = $post_ID;
mp-wp_genesis 438 if ($post_type == 'attachment') {
mp-wp_genesis 439 $remote_url = $this->get_tag( $post, 'wp:attachment_url' );
mp-wp_genesis 440 if ( !$remote_url )
mp-wp_genesis 441 $remote_url = $guid;
mp-wp_genesis 442
mp-wp_genesis 443 $comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url);
mp-wp_genesis 444 if ( !$post_id or is_wp_error($post_id) )
mp-wp_genesis 445 return $post_id;
mp-wp_genesis 446 }
mp-wp_genesis 447 else {
mp-wp_genesis 448 printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
mp-wp_genesis 449 $comment_post_ID = $post_id = wp_insert_post($postdata);
mp-wp_genesis 450 }
mp-wp_genesis 451
mp-wp_genesis 452 if ( is_wp_error( $post_id ) )
mp-wp_genesis 453 return $post_id;
mp-wp_genesis 454
mp-wp_genesis 455 // Memorize old and new ID.
mp-wp_genesis 456 if ( $post_id && $post_ID ) {
mp-wp_genesis 457 $this->post_ids_processed[intval($post_ID)] = intval($post_id);
mp-wp_genesis 458 }
mp-wp_genesis 459
mp-wp_genesis 460 // Add categories.
mp-wp_genesis 461 if (count($categories) > 0) {
mp-wp_genesis 462 $post_cats = array();
mp-wp_genesis 463 foreach ($categories as $category) {
mp-wp_genesis 464 if ( '' == $category )
mp-wp_genesis 465 continue;
mp-wp_genesis 466 $slug = sanitize_term_field('slug', $category, 0, 'category', 'db');
mp-wp_genesis 467 $cat = get_term_by('slug', $slug, 'category');
mp-wp_genesis 468 $cat_ID = 0;
mp-wp_genesis 469 if ( ! empty($cat) )
mp-wp_genesis 470 $cat_ID = $cat->term_id;
mp-wp_genesis 471 if ($cat_ID == 0) {
mp-wp_genesis 472 $category = $wpdb->escape($category);
mp-wp_genesis 473 $cat_ID = wp_insert_category(array('cat_name' => $category));
mp-wp_genesis 474 if ( is_wp_error($cat_ID) )
mp-wp_genesis 475 continue;
mp-wp_genesis 476 }
mp-wp_genesis 477 $post_cats[] = $cat_ID;
mp-wp_genesis 478 }
mp-wp_genesis 479 wp_set_post_categories($post_id, $post_cats);
mp-wp_genesis 480 }
mp-wp_genesis 481
mp-wp_genesis 482 // Add tags.
mp-wp_genesis 483 if (count($tags) > 0) {
mp-wp_genesis 484 $post_tags = array();
mp-wp_genesis 485 foreach ($tags as $tag) {
mp-wp_genesis 486 if ( '' == $tag )
mp-wp_genesis 487 continue;
mp-wp_genesis 488 $slug = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
mp-wp_genesis 489 $tag_obj = get_term_by('slug', $slug, 'post_tag');
mp-wp_genesis 490 $tag_id = 0;
mp-wp_genesis 491 if ( ! empty($tag_obj) )
mp-wp_genesis 492 $tag_id = $tag_obj->term_id;
mp-wp_genesis 493 if ( $tag_id == 0 ) {
mp-wp_genesis 494 $tag = $wpdb->escape($tag);
mp-wp_genesis 495 $tag_id = wp_insert_term($tag, 'post_tag');
mp-wp_genesis 496 if ( is_wp_error($tag_id) )
mp-wp_genesis 497 continue;
mp-wp_genesis 498 $tag_id = $tag_id['term_id'];
mp-wp_genesis 499 }
mp-wp_genesis 500 $post_tags[] = intval($tag_id);
mp-wp_genesis 501 }
mp-wp_genesis 502 wp_set_post_tags($post_id, $post_tags);
mp-wp_genesis 503 }
mp-wp_genesis 504 }
mp-wp_genesis 505
mp-wp_genesis 506 // Now for comments
mp-wp_genesis 507 preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
mp-wp_genesis 508 $comments = $comments[1];
mp-wp_genesis 509 $num_comments = 0;
mp-wp_genesis 510 if ( $comments) { foreach ($comments as $comment) {
mp-wp_genesis 511 $comment_author = $this->get_tag( $comment, 'wp:comment_author');
mp-wp_genesis 512 $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email');
mp-wp_genesis 513 $comment_author_IP = $this->get_tag( $comment, 'wp:comment_author_IP');
mp-wp_genesis 514 $comment_author_url = $this->get_tag( $comment, 'wp:comment_author_url');
mp-wp_genesis 515 $comment_date = $this->get_tag( $comment, 'wp:comment_date');
mp-wp_genesis 516 $comment_date_gmt = $this->get_tag( $comment, 'wp:comment_date_gmt');
mp-wp_genesis 517 $comment_content = $this->get_tag( $comment, 'wp:comment_content');
mp-wp_genesis 518 $comment_approved = $this->get_tag( $comment, 'wp:comment_approved');
mp-wp_genesis 519 $comment_type = $this->get_tag( $comment, 'wp:comment_type');
mp-wp_genesis 520 $comment_parent = $this->get_tag( $comment, 'wp:comment_parent');
mp-wp_genesis 521
mp-wp_genesis 522 // if this is a new post we can skip the comment_exists() check
mp-wp_genesis 523 if ( !$post_exists || !comment_exists($comment_author, $comment_date) ) {
mp-wp_genesis 524 $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');
mp-wp_genesis 525 wp_insert_comment($commentdata);
mp-wp_genesis 526 $num_comments++;
mp-wp_genesis 527 }
mp-wp_genesis 528 } }
mp-wp_genesis 529
mp-wp_genesis 530 if ( $num_comments )
mp-wp_genesis 531 printf(' '.__ngettext('(%s comment)', '(%s comments)', $num_comments), $num_comments);
mp-wp_genesis 532
mp-wp_genesis 533 // Now for post meta
mp-wp_genesis 534 preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);
mp-wp_genesis 535 $postmeta = $postmeta[1];
mp-wp_genesis 536 if ( $postmeta) { foreach ($postmeta as $p) {
mp-wp_genesis 537 $key = $this->get_tag( $p, 'wp:meta_key' );
mp-wp_genesis 538 $value = $this->get_tag( $p, 'wp:meta_value' );
mp-wp_genesis 539 $value = stripslashes($value); // add_post_meta() will escape.
mp-wp_genesis 540
mp-wp_genesis 541 $this->process_post_meta($post_id, $key, $value);
mp-wp_genesis 542
mp-wp_genesis 543 } }
mp-wp_genesis 544
mp-wp_genesis 545 do_action('import_post_added', $post_id);
mp-wp_genesis 546 print "</li>\n";
mp-wp_genesis 547 }
mp-wp_genesis 548
mp-wp_genesis 549 function process_post_meta($post_id, $key, $value) {
mp-wp_genesis 550 // the filter can return false to skip a particular metadata key
mp-wp_genesis 551 $_key = apply_filters('import_post_meta_key', $key);
mp-wp_genesis 552 if ( $_key ) {
mp-wp_genesis 553 add_post_meta( $post_id, $_key, $value );
mp-wp_genesis 554 do_action('import_post_meta', $post_id, $_key, $value);
mp-wp_genesis 555 }
mp-wp_genesis 556 }
mp-wp_genesis 557
mp-wp_genesis 558 function process_attachment($postdata, $remote_url) {
mp-wp_genesis 559 if ($this->fetch_attachments and $remote_url) {
mp-wp_genesis 560 printf( __('Importing attachment <em>%s</em>... '), htmlspecialchars($remote_url) );
mp-wp_genesis 561
mp-wp_genesis 562 // If the URL is absolute, but does not contain http, upload it assuming the base_site_url variable
mp-wp_genesis 563 if ( preg_match('/^\/[\w\W]+$/', $remote_url) )
mp-wp_genesis 564 $remote_url = rtrim($this->base_url,'/').$remote_url;
mp-wp_genesis 565
mp-wp_genesis 566 $upload = $this->fetch_remote_file($postdata, $remote_url);
mp-wp_genesis 567 if ( is_wp_error($upload) ) {
mp-wp_genesis 568 printf( __('Remote file error: %s'), htmlspecialchars($upload->get_error_message()) );
mp-wp_genesis 569 return $upload;
mp-wp_genesis 570 }
mp-wp_genesis 571 else {
mp-wp_genesis 572 print '('.size_format(filesize($upload['file'])).')';
mp-wp_genesis 573 }
mp-wp_genesis 574
mp-wp_genesis 575 if ( $info = wp_check_filetype($upload['file']) ) {
mp-wp_genesis 576 $postdata['post_mime_type'] = $info['type'];
mp-wp_genesis 577 }
mp-wp_genesis 578 else {
mp-wp_genesis 579 print __('Invalid file type');
mp-wp_genesis 580 return;
mp-wp_genesis 581 }
mp-wp_genesis 582
mp-wp_genesis 583 $postdata['guid'] = $upload['url'];
mp-wp_genesis 584
mp-wp_genesis 585 // as per wp-admin/includes/upload.php
mp-wp_genesis 586 $post_id = wp_insert_attachment($postdata, $upload['file']);
mp-wp_genesis 587 wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
mp-wp_genesis 588
mp-wp_genesis 589 // remap the thumbnail url. this isn't perfect because we're just guessing the original url.
mp-wp_genesis 590 if ( preg_match('@^image/@', $info['type']) && $thumb_url = wp_get_attachment_thumb_url($post_id) ) {
mp-wp_genesis 591 $parts = pathinfo($remote_url);
mp-wp_genesis 592 $ext = $parts['extension'];
mp-wp_genesis 593 $name = basename($parts['basename'], ".{$ext}");
mp-wp_genesis 594 $this->url_remap[$parts['dirname'] . '/' . $name . '.thumbnail.' . $ext] = $thumb_url;
mp-wp_genesis 595 }
mp-wp_genesis 596
mp-wp_genesis 597 return $post_id;
mp-wp_genesis 598 }
mp-wp_genesis 599 else {
mp-wp_genesis 600 printf( __('Skipping attachment <em>%s</em>'), htmlspecialchars($remote_url) );
mp-wp_genesis 601 }
mp-wp_genesis 602 }
mp-wp_genesis 603
mp-wp_genesis 604 function fetch_remote_file($post, $url) {
mp-wp_genesis 605 $upload = wp_upload_dir($post['post_date']);
mp-wp_genesis 606
mp-wp_genesis 607 // extract the file name and extension from the url
mp-wp_genesis 608 $file_name = basename($url);
mp-wp_genesis 609
mp-wp_genesis 610 // get placeholder file in the upload dir with a unique sanitized filename
mp-wp_genesis 611 $upload = wp_upload_bits( $file_name, 0, '', $post['post_date']);
mp-wp_genesis 612 if ( $upload['error'] ) {
mp-wp_genesis 613 echo $upload['error'];
mp-wp_genesis 614 return new WP_Error( 'upload_dir_error', $upload['error'] );
mp-wp_genesis 615 }
mp-wp_genesis 616
mp-wp_genesis 617 // fetch the remote url and write it to the placeholder file
mp-wp_genesis 618 $headers = wp_get_http($url, $upload['file']);
mp-wp_genesis 619
mp-wp_genesis 620 //Request failed
mp-wp_genesis 621 if ( ! $headers ) {
mp-wp_genesis 622 @unlink($upload['file']);
mp-wp_genesis 623 return new WP_Error( 'import_file_error', __('Remote server did not respond') );
mp-wp_genesis 624 }
mp-wp_genesis 625
mp-wp_genesis 626 // make sure the fetch was successful
mp-wp_genesis 627 if ( $headers['response'] != '200' ) {
mp-wp_genesis 628 @unlink($upload['file']);
mp-wp_genesis 629 return new WP_Error( 'import_file_error', sprintf(__('Remote file returned error response %1$d %2$s'), $headers['response'], get_status_header_desc($headers['response']) ) );
mp-wp_genesis 630 }
mp-wp_genesis 631 elseif ( isset($headers['content-length']) && filesize($upload['file']) != $headers['content-length'] ) {
mp-wp_genesis 632 @unlink($upload['file']);
mp-wp_genesis 633 return new WP_Error( 'import_file_error', __('Remote file is incorrect size') );
mp-wp_genesis 634 }
mp-wp_genesis 635
mp-wp_genesis 636 $max_size = $this->max_attachment_size();
mp-wp_genesis 637 if ( !empty($max_size) and filesize($upload['file']) > $max_size ) {
mp-wp_genesis 638 @unlink($upload['file']);
mp-wp_genesis 639 return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', size_format($max_size))) );
mp-wp_genesis 640 }
mp-wp_genesis 641
mp-wp_genesis 642 // keep track of the old and new urls so we can substitute them later
mp-wp_genesis 643 $this->url_remap[$url] = $upload['url'];
mp-wp_genesis 644 // if the remote url is redirected somewhere else, keep track of the destination too
mp-wp_genesis 645 if ( $headers['x-final-location'] != $url )
mp-wp_genesis 646 $this->url_remap[$headers['x-final-location']] = $upload['url'];
mp-wp_genesis 647
mp-wp_genesis 648 return $upload;
mp-wp_genesis 649
mp-wp_genesis 650 }
mp-wp_genesis 651
mp-wp_genesis 652 // sort by strlen, longest string first
mp-wp_genesis 653 function cmpr_strlen($a, $b) {
mp-wp_genesis 654 return strlen($b) - strlen($a);
mp-wp_genesis 655 }
mp-wp_genesis 656
mp-wp_genesis 657 // update url references in post bodies to point to the new local files
mp-wp_genesis 658 function backfill_attachment_urls() {
mp-wp_genesis 659
mp-wp_genesis 660 // make sure we do the longest urls first, in case one is a substring of another
mp-wp_genesis 661 uksort($this->url_remap, array(&$this, 'cmpr_strlen'));
mp-wp_genesis 662
mp-wp_genesis 663 global $wpdb;
mp-wp_genesis 664 foreach ($this->url_remap as $from_url => $to_url) {
mp-wp_genesis 665 // remap urls in post_content
mp-wp_genesis 666 $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, '%s', '%s')", $from_url, $to_url) );
mp-wp_genesis 667 // remap enclosure urls
mp-wp_genesis 668 $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, '%s', '%s') WHERE meta_key='enclosure'", $from_url, $to_url) );
mp-wp_genesis 669 }
mp-wp_genesis 670 }
mp-wp_genesis 671
mp-wp_genesis 672 // update the post_parent of orphans now that we know the local id's of all parents
mp-wp_genesis 673 function backfill_parents() {
mp-wp_genesis 674 global $wpdb;
mp-wp_genesis 675
mp-wp_genesis 676 foreach ($this->orphans as $child_id => $parent_id) {
mp-wp_genesis 677 $local_child_id = $this->post_ids_processed[$child_id];
mp-wp_genesis 678 $local_parent_id = $this->post_ids_processed[$parent_id];
mp-wp_genesis 679 if ($local_child_id and $local_parent_id) {
mp-wp_genesis 680 $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE ID = %d", $local_parent_id, $local_child_id));
mp-wp_genesis 681 }
mp-wp_genesis 682 }
mp-wp_genesis 683 }
mp-wp_genesis 684
mp-wp_genesis 685 function is_valid_meta_key($key) {
mp-wp_genesis 686 // skip _wp_attached_file metadata since we'll regenerate it from scratch
mp-wp_genesis 687 if ( $key == '_wp_attached_file' )
mp-wp_genesis 688 return false;
mp-wp_genesis 689 return $key;
mp-wp_genesis 690 }
mp-wp_genesis 691
mp-wp_genesis 692 // give the user the option of creating new users to represent authors in the import file?
mp-wp_genesis 693 function allow_create_users() {
mp-wp_genesis 694 return apply_filters('import_allow_create_users', true);
mp-wp_genesis 695 }
mp-wp_genesis 696
mp-wp_genesis 697 // give the user the option of downloading and importing attached files
mp-wp_genesis 698 function allow_fetch_attachments() {
mp-wp_genesis 699 return apply_filters('import_allow_fetch_attachments', true);
mp-wp_genesis 700 }
mp-wp_genesis 701
mp-wp_genesis 702 function max_attachment_size() {
mp-wp_genesis 703 // can be overridden with a filter - 0 means no limit
mp-wp_genesis 704 return apply_filters('import_attachment_size_limit', 0);
mp-wp_genesis 705 }
mp-wp_genesis 706
mp-wp_genesis 707 function import_start() {
mp-wp_genesis 708 wp_defer_term_counting(true);
mp-wp_genesis 709 wp_defer_comment_counting(true);
mp-wp_genesis 710 do_action('import_start');
mp-wp_genesis 711 }
mp-wp_genesis 712
mp-wp_genesis 713 function import_end() {
mp-wp_genesis 714 do_action('import_end');
mp-wp_genesis 715
mp-wp_genesis 716 // clear the caches after backfilling
mp-wp_genesis 717 foreach ($this->post_ids_processed as $post_id)
mp-wp_genesis 718 clean_post_cache($post_id);
mp-wp_genesis 719
mp-wp_genesis 720 wp_defer_term_counting(false);
mp-wp_genesis 721 wp_defer_comment_counting(false);
mp-wp_genesis 722 }
mp-wp_genesis 723
mp-wp_genesis 724 function import($id, $fetch_attachments = false) {
mp-wp_genesis 725 $this->id = (int) $id;
mp-wp_genesis 726 $this->fetch_attachments = ($this->allow_fetch_attachments() && (bool) $fetch_attachments);
mp-wp_genesis 727
mp-wp_genesis 728 add_filter('import_post_meta_key', array($this, 'is_valid_meta_key'));
mp-wp_genesis 729 $file = get_attached_file($this->id);
mp-wp_genesis 730 $this->import_file($file);
mp-wp_genesis 731 }
mp-wp_genesis 732
mp-wp_genesis 733 function import_file($file) {
mp-wp_genesis 734 $this->file = $file;
mp-wp_genesis 735
mp-wp_genesis 736 $this->import_start();
mp-wp_genesis 737 $this->get_authors_from_post();
mp-wp_genesis 738 wp_suspend_cache_invalidation(true);
mp-wp_genesis 739 $this->get_entries();
mp-wp_genesis 740 $this->process_categories();
mp-wp_genesis 741 $this->process_tags();
mp-wp_genesis 742 $result = $this->process_posts();
mp-wp_genesis 743 wp_suspend_cache_invalidation(false);
mp-wp_genesis 744 $this->backfill_parents();
mp-wp_genesis 745 $this->backfill_attachment_urls();
mp-wp_genesis 746 $this->import_end();
mp-wp_genesis 747
mp-wp_genesis 748 if ( is_wp_error( $result ) )
mp-wp_genesis 749 return $result;
mp-wp_genesis 750 }
mp-wp_genesis 751
mp-wp_genesis 752 function handle_upload() {
mp-wp_genesis 753 $file = wp_import_handle_upload();
mp-wp_genesis 754 if ( isset($file['error']) ) {
mp-wp_genesis 755 echo '<p>'.__('Sorry, there has been an error.').'</p>';
mp-wp_genesis 756 echo '<p><strong>' . $file['error'] . '</strong></p>';
mp-wp_genesis 757 return false;
mp-wp_genesis 758 }
mp-wp_genesis 759 $this->file = $file['file'];
mp-wp_genesis 760 $this->id = (int) $file['id'];
mp-wp_genesis 761 return true;
mp-wp_genesis 762 }
mp-wp_genesis 763
mp-wp_genesis 764 function dispatch() {
mp-wp_genesis 765 if (empty ($_GET['step']))
mp-wp_genesis 766 $step = 0;
mp-wp_genesis 767 else
mp-wp_genesis 768 $step = (int) $_GET['step'];
mp-wp_genesis 769
mp-wp_genesis 770 $this->header();
mp-wp_genesis 771 switch ($step) {
mp-wp_genesis 772 case 0 :
mp-wp_genesis 773 $this->greet();
mp-wp_genesis 774 break;
mp-wp_genesis 775 case 1 :
mp-wp_genesis 776 check_admin_referer('import-upload');
mp-wp_genesis 777 if ( $this->handle_upload() )
mp-wp_genesis 778 $this->select_authors();
mp-wp_genesis 779 break;
mp-wp_genesis 780 case 2:
mp-wp_genesis 781 check_admin_referer('import-wordpress');
mp-wp_genesis 782 $result = $this->import( $_GET['id'], $_POST['attachments'] );
mp-wp_genesis 783 if ( is_wp_error( $result ) )
mp-wp_genesis 784 echo $result->get_error_message();
mp-wp_genesis 785 break;
mp-wp_genesis 786 }
mp-wp_genesis 787 $this->footer();
mp-wp_genesis 788 }
mp-wp_genesis 789
mp-wp_genesis 790 function WP_Import() {
mp-wp_genesis 791 // Nothing.
mp-wp_genesis 792 }
mp-wp_genesis 793 }
mp-wp_genesis 794
mp-wp_genesis 795 /**
mp-wp_genesis 796 * Register WordPress Importer
mp-wp_genesis 797 *
mp-wp_genesis 798 * @since unknown
mp-wp_genesis 799 * @var WP_Import
mp-wp_genesis 800 * @name $wp_import
mp-wp_genesis 801 */
mp-wp_genesis 802 $wp_import = new WP_Import();
mp-wp_genesis 803
mp-wp_genesis 804 register_importer('wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.'), array ($wp_import, 'dispatch'));
mp-wp_genesis 805
mp-wp_genesis 806 ?>