| [ Index ] |
WordPress Source Cross Reference |
[Summary view] [Print] [Text view]
1 <?php 2 3 class WP_Import { 4 5 var $posts = array (); 6 var $file; 7 var $id; 8 var $mtnames = array (); 9 var $newauthornames = array (); 10 var $j = -1; 11 12 function header() { 13 echo '<div class="wrap">'; 14 echo '<h2>'.__('Import WordPress').'</h2>'; 15 } 16 17 function footer() { 18 echo '</div>'; 19 } 20 21 function unhtmlentities($string) { // From php.net for < 4.3 compat 22 $trans_tbl = get_html_translation_table(HTML_ENTITIES); 23 $trans_tbl = array_flip($trans_tbl); 24 return strtr($string, $trans_tbl); 25 } 26 27 function greet() { 28 echo '<p>'.__('Howdy! Upload your WordPress eXtended RSS (WXR) file and we’ll import the posts and comments into this blog.').'</p>'; 29 wp_import_upload_form("admin.php?import=wordpress&step=1"); 30 } 31 32 function get_tag( $string, $tag ) { 33 preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return); 34 $return = addslashes( trim( $return[1] ) ); 35 return $return; 36 } 37 38 function users_form($n) { 39 global $wpdb, $testing; 40 $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID"); 41 ?><select name="userselect[<?php echo $n; ?>]"> 42 <option value="#NONE#">- Select -</option> 43 <?php 44 foreach ($users as $user) { 45 echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>'; 46 } 47 ?> 48 </select> 49 <?php 50 } 51 52 //function to check the authorname and do the mapping 53 function checkauthor($author) { 54 global $wpdb; 55 //mtnames is an array with the names in the mt import file 56 $pass = 'changeme'; 57 if (!(in_array($author, $this->mtnames))) { //a new mt author name is found 58 ++ $this->j; 59 $this->mtnames[$this->j] = $author; //add that new mt author name to an array 60 $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user 61 if (!$user_id) { //banging my head against the desk now. 62 if ($newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname 63 $user_id = wp_create_user($author, $pass); 64 $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank. 65 } else { 66 $user_id = wp_create_user($this->newauthornames[$this->j], $pass); 67 } 68 } else { 69 return $user_id; // return pre-existing wp username if it exists 70 } 71 } else { 72 $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array 73 $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames 74 } 75 76 return $user_id; 77 } 78 79 function get_entries() { 80 set_magic_quotes_runtime(0); 81 $importdata = file($this->file); // Read the file into an array 82 $importdata = implode('', $importdata); // squish it 83 $importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata); 84 preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts); 85 $this->posts = $this->posts[1]; 86 } 87 88 function get_wp_authors() { 89 $temp = array (); 90 $i = -1; 91 foreach ($this->posts as $post) { 92 if ('' != trim($post)) { 93 ++ $i; 94 $author = $this->get_tag( $post, 'dc:creator' ); 95 array_push($temp, "$author"); //store the extracted author names in a temporary array 96 } 97 } 98 99 // 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. 100 $authors[0] = array_shift($temp); 101 $y = count($temp) + 1; 102 for ($x = 1; $x < $y; $x ++) { 103 $next = array_shift($temp); 104 if (!(in_array($next, $authors))) 105 array_push($authors, "$next"); 106 } 107 108 return $authors; 109 } 110 111 function get_authors_from_post() { 112 $formnames = array (); 113 $selectnames = array (); 114 115 foreach ($_POST['user'] as $key => $line) { 116 $newname = trim(stripslashes($line)); 117 if ($newname == '') 118 $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. 119 array_push($formnames, "$newname"); 120 } // $formnames is the array with the form entered names 121 122 foreach ($_POST['userselect'] as $user => $key) { 123 $selected = trim(stripslashes($key)); 124 array_push($selectnames, "$selected"); 125 } 126 127 $count = count($formnames); 128 for ($i = 0; $i < $count; $i ++) { 129 if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form 130 array_push($this->newauthornames, "$selectnames[$i]"); 131 } else { 132 array_push($this->newauthornames, "$formnames[$i]"); 133 } 134 } 135 } 136 137 function wp_authors_form() { 138 ?> 139 <h2><?php _e('Assign Authors'); ?></h2> 140 <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> 141 <p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p> 142 <?php 143 144 145 $authors = $this->get_wp_authors(); 146 echo '<ol id="authors">'; 147 echo '<form action="?import=wordpress&step=2&id=' . $this->id . '" method="post">'; 148 $j = -1; 149 foreach ($authors as $author) { 150 ++ $j; 151 echo '<li>Current author: <strong>'.$author.'</strong><br />'.'Create user <input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30"> <br /> or map to existing '; 152 $this->users_form($j); 153 echo '</li>'; 154 } 155 156 echo '<input type="submit" value="Submit">'.'<br/>'; 157 echo '</form>'; 158 echo '</ol>'; 159 160 } 161 162 function select_authors() { 163 $file = wp_import_handle_upload(); 164 if ( isset($file['error']) ) { 165 $this->header(); 166 echo '<p>Sorry, there has been an error.</p>'; 167 echo '<p><strong>' . $file['error'] . '</strong></p>'; 168 $this->footer(); 169 return; 170 } 171 $this->file = $file['file']; 172 $this->id = $file['id']; 173 174 $this->get_entries(); 175 $this->wp_authors_form(); 176 } 177 178 function process_posts() { 179 global $wpdb; 180 $i = -1; 181 echo '<ol>'; 182 foreach ($this->posts as $post) { 183 184 // There are only ever one of these 185 $post_title = $this->get_tag( $post, 'title' ); 186 $post_date = $this->get_tag( $post, 'wp:post_date' ); 187 $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' ); 188 $comment_status = $this->get_tag( $post, 'wp:comment_status' ); 189 $ping_status = $this->get_tag( $post, 'wp:ping_status' ); 190 $post_status = $this->get_tag( $post, 'wp:status' ); 191 $post_parent = $this->get_tag( $post, 'wp:post_parent' ); 192 $post_type = $this->get_tag( $post, 'wp:post_type' ); 193 $guid = $this->get_tag( $post, 'guid' ); 194 $post_author = $this->get_tag( $post, 'dc:creator' ); 195 196 $post_content = $this->get_tag( $post, 'content:encoded' ); 197 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $post_content); 198 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); 199 $post_content = str_replace('<br>', '<br />', $post_content); 200 $post_content = str_replace('<hr>', '<hr />', $post_content); 201 202 preg_match_all('|<category>(.*?)</category>|is', $post, $categories); 203 $categories = $categories[1]; 204 205 $cat_index = 0; 206 foreach ($categories as $category) { 207 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category)); 208 $cat_index++; 209 } 210 211 if ($post_id = post_exists($post_title, '', $post_date)) { 212 echo '<li>'; 213 printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); 214 } else { 215 echo '<li>'; 216 printf(__('Importing post <i>%s</i>...'), stripslashes($post_title)); 217 218 $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor 219 220 $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'); 221 $comment_post_ID = $post_id = wp_insert_post($postdata); 222 // Add categories. 223 if (0 != count($categories)) { 224 wp_create_categories($categories, $post_id); 225 } 226 } 227 228 // Now for comments 229 preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments); 230 $comments = $comments[1]; 231 $num_comments = 0; 232 if ( $comments) { foreach ($comments as $comment) { 233 $comment_author = $this->get_tag( $comment, 'wp:comment_author'); 234 $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email'); 235 $comment_author_IP = $this->get_tag( $comment, 'wp:comment_author_IP'); 236 $comment_author_url = $this->get_tag( $comment, 'wp:comment_author_url'); 237 $comment_date = $this->get_tag( $comment, 'wp:comment_date'); 238 $comment_date_gmt = $this->get_tag( $comment, 'wp:comment_date_gmt'); 239 $comment_content = $this->get_tag( $comment, 'wp:comment_content'); 240 $comment_approved = $this->get_tag( $comment, 'wp:comment_approved'); 241 $comment_type = $this->get_tag( $comment, 'wp:comment_type'); 242 $comment_parent = $this->get_tag( $comment, 'wp:comment_parent'); 243 244 if ( !comment_exists($comment_author, $comment_date) ) { 245 $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'); 246 wp_insert_comment($commentdata); 247 $num_comments++; 248 } 249 } } 250 if ( $num_comments ) 251 printf(__(' (%s comments)'), $num_comments); 252 253 // Now for post meta 254 preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta); 255 $postmeta = $postmeta[1]; 256 if ( $postmeta) { foreach ($postmeta as $p) { 257 $key = $this->get_tag( $p, 'wp:meta_key' ); 258 $value = $this->get_tag( $p, 'wp:meta_value' ); 259 add_post_meta( $post_id, $key, $value ); 260 } } 261 262 $index++; 263 } 264 265 echo '</ol>'; 266 267 wp_import_cleanup($this->id); 268 269 echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3>'; 270 } 271 272 function import() { 273 $this->id = (int) $_GET['id']; 274 275 $this->file = get_attached_file($this->id); 276 $this->get_authors_from_post(); 277 $this->get_entries(); 278 $this->process_posts(); 279 } 280 281 function dispatch() { 282 if (empty ($_GET['step'])) 283 $step = 0; 284 else 285 $step = (int) $_GET['step']; 286 287 $this->header(); 288 switch ($step) { 289 case 0 : 290 $this->greet(); 291 break; 292 case 1 : 293 $this->select_authors(); 294 break; 295 case 2: 296 $this->import(); 297 break; 298 } 299 $this->footer(); 300 } 301 302 function WP_Import() { 303 // Nothing. 304 } 305 } 306 307 $wp_import = new WP_Import(); 308 309 register_importer('wordpress', 'WordPress', __('Import posts from a WordPress export file'), array ($wp_import, 'dispatch')); 310 311 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Jul 15 11:57:04 2006 | Courtesy of Taragana |