| | 197 | } |
|---|
| | 198 | |
|---|
| | 199 | function save_post(&$post, &$comments, &$pings) { |
|---|
| | 200 | // Reset the counter |
|---|
| | 201 | set_time_limit(30); |
|---|
| | 202 | $post = get_object_vars($post); |
|---|
| | 203 | $post = add_magic_quotes($post); |
|---|
| | 204 | $post = (object) $post; |
|---|
| | 205 | |
|---|
| | 206 | if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) { |
|---|
| | 207 | echo '<li>'; |
|---|
| | 208 | printf(__('Post <i>%s</i> already exists.'), stripslashes($post->post_title)); |
|---|
| | 209 | } else { |
|---|
| | 210 | echo '<li>'; |
|---|
| | 211 | printf(__('Importing post <i>%s</i>...'), stripslashes($post->post_title)); |
|---|
| | 212 | |
|---|
| | 213 | if ( '' != $post->extended ) |
|---|
| | 214 | $post->post_content .= "\n<!--more-->\n$post->extended"; |
|---|
| | 215 | |
|---|
| | 216 | $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor |
|---|
| | 217 | $post_id = wp_insert_post($post); |
|---|
| | 218 | |
|---|
| | 219 | // Add categories. |
|---|
| | 220 | if ( 0 != count($post->categories) ) { |
|---|
| | 221 | wp_create_categories($post->categories, $post_id); |
|---|
| | 222 | } |
|---|
| | 223 | } |
|---|
| | 224 | |
|---|
| | 225 | $num_comments = 0; |
|---|
| | 226 | foreach ( $comments as $comment ) { |
|---|
| | 227 | $comment = get_object_vars($comment); |
|---|
| | 228 | $comment = add_magic_quotes($comment); |
|---|
| | 229 | |
|---|
| | 230 | if ( !comment_exists($comment['comment_author'], $comment['comment_date'])) { |
|---|
| | 231 | $comment['comment_post_ID'] = $post_id; |
|---|
| | 232 | $comment = wp_filter_comment($comment); |
|---|
| | 233 | wp_insert_comment($comment); |
|---|
| | 234 | $num_comments++; |
|---|
| | 235 | } |
|---|
| | 236 | } |
|---|
| | 237 | |
|---|
| | 238 | if ( $num_comments ) |
|---|
| | 239 | printf(' '.__('(%s comments)'), $num_comments); |
|---|
| | 240 | |
|---|
| | 241 | $num_pings = 0; |
|---|
| | 242 | foreach ( $pings as $ping ) { |
|---|
| | 243 | $ping = get_object_vars($ping); |
|---|
| | 244 | $ping = add_magic_quotes($ping); |
|---|
| | 245 | |
|---|
| | 246 | if ( !comment_exists($ping['comment_author'], $ping['comment_date'])) { |
|---|
| | 247 | $ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}"; |
|---|
| | 248 | $ping['comment_post_ID'] = $post_id; |
|---|
| | 249 | $ping = wp_filter_comment($ping); |
|---|
| | 250 | wp_insert_comment($ping); |
|---|
| | 251 | $num_pings++; |
|---|
| | 252 | } |
|---|
| | 253 | } |
|---|
| | 254 | |
|---|
| | 255 | if ( $num_pings ) |
|---|
| | 256 | printf(' '.__('(%s pings)'), $num_pings); |
|---|
| | 257 | |
|---|
| | 258 | echo "</li>"; |
|---|
| | 259 | //ob_flush();flush(); |
|---|
| 202 | | foreach ($this->posts as $post) { |
|---|
| 203 | | if ('' != trim($post)) { |
|---|
| 204 | | ++ $i; |
|---|
| 205 | | unset ($post_categories); |
|---|
| 206 | | |
|---|
| 207 | | // Take the pings out first |
|---|
| 208 | | preg_match("|(-----\n\nPING:.*)|s", $post, $pings); |
|---|
| 209 | | $post = preg_replace("|(-----\n\nPING:.*)|s", '', $post); |
|---|
| 210 | | |
|---|
| 211 | | // Then take the comments out |
|---|
| 212 | | preg_match("|(-----\nCOMMENT:.*)|s", $post, $comments); |
|---|
| 213 | | $post = preg_replace("|(-----\nCOMMENT:.*)|s", '', $post); |
|---|
| 214 | | |
|---|
| 215 | | // We ignore the keywords |
|---|
| 216 | | $post = preg_replace("|(-----\nKEYWORDS:.*)|s", '', $post); |
|---|
| 217 | | |
|---|
| 218 | | // We want the excerpt |
|---|
| 219 | | preg_match("|-----\nEXCERPT:(.*)|s", $post, $excerpt); |
|---|
| 220 | | $post_excerpt = $wpdb->escape(trim($excerpt[1])); |
|---|
| 221 | | $post = preg_replace("|(-----\nEXCERPT:.*)|s", '', $post); |
|---|
| 222 | | |
|---|
| 223 | | // We're going to put extended body into main body with a more tag |
|---|
| 224 | | preg_match("|-----\nEXTENDED BODY:(.*)|s", $post, $extended); |
|---|
| 225 | | $extended = trim($extended[1]); |
|---|
| 226 | | if ('' != $extended) |
|---|
| 227 | | $extended = "\n<!--more-->\n$extended"; |
|---|
| 228 | | $post = preg_replace("|(-----\nEXTENDED BODY:.*)|s", '', $post); |
|---|
| 229 | | |
|---|
| 230 | | // Now for the main body |
|---|
| 231 | | preg_match("|-----\nBODY:(.*)|s", $post, $body); |
|---|
| 232 | | $body = trim($body[1]); |
|---|
| 233 | | $post_content = $wpdb->escape($body.$extended); |
|---|
| 234 | | $post = preg_replace("|(-----\nBODY:.*)|s", '', $post); |
|---|
| 235 | | |
|---|
| 236 | | // Grab the metadata from what's left |
|---|
| 237 | | $metadata = explode("\n", $post); |
|---|
| 238 | | foreach ($metadata as $line) { |
|---|
| 239 | | preg_match("/^(.*?):(.*)/", $line, $token); |
|---|
| 240 | | $key = trim($token[1]); |
|---|
| 241 | | $value = trim($token[2]); |
|---|
| 242 | | // Now we decide what it is and what to do with it |
|---|
| 243 | | switch ($key) { |
|---|
| 244 | | case '' : |
|---|
| 245 | | break; |
|---|
| 246 | | case 'AUTHOR' : |
|---|
| 247 | | $post_author = $value; |
|---|
| 248 | | break; |
|---|
| 249 | | case 'TITLE' : |
|---|
| 250 | | $post_title = $wpdb->escape($value); |
|---|
| 251 | | break; |
|---|
| 252 | | case 'STATUS' : |
|---|
| 253 | | // "publish" and "draft" enumeration items match up; no change required |
|---|
| 254 | | $post_status = $value; |
|---|
| 255 | | if (empty ($post_status)) |
|---|
| 256 | | $post_status = 'publish'; |
|---|
| 257 | | break; |
|---|
| 258 | | case 'ALLOW COMMENTS' : |
|---|
| 259 | | $post_allow_comments = $value; |
|---|
| 260 | | if ($post_allow_comments == 1) { |
|---|
| 261 | | $comment_status = 'open'; |
|---|
| 262 | | } else { |
|---|
| 263 | | $comment_status = 'closed'; |
|---|
| 264 | | } |
|---|
| 265 | | break; |
|---|
| 266 | | case 'CONVERT BREAKS' : |
|---|
| 267 | | $post_convert_breaks = $value; |
|---|
| 268 | | break; |
|---|
| 269 | | case 'ALLOW PINGS' : |
|---|
| 270 | | $ping_status = trim($meta[2][0]); |
|---|
| 271 | | if ($ping_status == 1) { |
|---|
| 272 | | $ping_status = 'open'; |
|---|
| 273 | | } else { |
|---|
| 274 | | $ping_status = 'closed'; |
|---|
| 275 | | } |
|---|
| 276 | | break; |
|---|
| 277 | | case 'PRIMARY CATEGORY' : |
|---|
| 278 | | if (! empty ($value) ) |
|---|
| 279 | | $post_categories[] = $wpdb->escape($value); |
|---|
| 280 | | break; |
|---|
| 281 | | case 'CATEGORY' : |
|---|
| 282 | | if (! empty ($value) ) |
|---|
| 283 | | $post_categories[] = $wpdb->escape($value); |
|---|
| 284 | | break; |
|---|
| 285 | | case 'DATE' : |
|---|
| 286 | | $post_modified = strtotime($value); |
|---|
| 287 | | $post_modified = date('Y-m-d H:i:s', $post_modified); |
|---|
| 288 | | $post_modified_gmt = get_gmt_from_date("$post_modified"); |
|---|
| 289 | | $post_date = $post_modified; |
|---|
| 290 | | $post_date_gmt = $post_modified_gmt; |
|---|
| 291 | | break; |
|---|
| 292 | | default : |
|---|
| 293 | | // echo "\n$key: $value"; |
|---|
| 294 | | break; |
|---|
| 295 | | } // end switch |
|---|
| 296 | | } // End foreach |
|---|
| 297 | | |
|---|
| 298 | | // Let's check to see if it's in already |
|---|
| 299 | | if ($post_id = post_exists($post_title, '', $post_date)) { |
|---|
| 300 | | echo '<li>'; |
|---|
| 301 | | printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); |
|---|
| 302 | | } else { |
|---|
| 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 checkauthor |
|---|
| 307 | | |
|---|
| 308 | | $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'); |
|---|
| 309 | | $post_id = wp_insert_post($postdata); |
|---|
| 310 | | // Add categories. |
|---|
| 311 | | if (0 != count($post_categories)) { |
|---|
| 312 | | wp_create_categories($post_categories, $post_id); |
|---|
| 313 | | } |
|---|
| | 277 | |
|---|
| | 278 | while ( $line = fgets($handle) ) { |
|---|
| | 279 | $line = trim($line); |
|---|
| | 280 | |
|---|
| | 281 | if ( '-----' == $line ) { |
|---|
| | 282 | // Finishing a multi-line field |
|---|
| | 283 | if ( 'comment' == $context ) { |
|---|
| | 284 | $comments[] = $comment; |
|---|
| | 285 | $comment = new StdClass(); |
|---|
| | 286 | } else if ( 'ping' == $context ) { |
|---|
| | 287 | $pings[] = $ping; |
|---|
| | 288 | $ping = new StdClass(); |
|---|
| 315 | | |
|---|
| 316 | | $comment_post_ID = (int) $post_id; |
|---|
| 317 | | $comment_approved = 1; |
|---|
| 318 | | |
|---|
| 319 | | // Now for comments |
|---|
| 320 | | $comments = explode("-----\nCOMMENT:", $comments[0]); |
|---|
| 321 | | $num_comments = 0; |
|---|
| 322 | | foreach ($comments as $comment) { |
|---|
| 323 | | if ('' != trim($comment)) { |
|---|
| 324 | | // Author |
|---|
| 325 | | preg_match("|AUTHOR:(.*)|", $comment, $comment_author); |
|---|
| 326 | | $comment_author = $wpdb->escape(trim($comment_author[1])); |
|---|
| 327 | | $comment = preg_replace('|(\n?AUTHOR:.*)|', '', $comment); |
|---|
| 328 | | preg_match("|EMAIL:(.*)|", $comment, $comment_author_email); |
|---|
| 329 | | $comment_author_email = $wpdb->escape(trim($comment_author_email[1])); |
|---|
| 330 | | $comment = preg_replace('|(\n?EMAIL:.*)|', '', $comment); |
|---|
| 331 | | |
|---|
| 332 | | preg_match("|IP:(.*)|", $comment, $comment_author_IP); |
|---|
| 333 | | $comment_author_IP = trim($comment_author_IP[1]); |
|---|
| 334 | | $comment = preg_replace('|(\n?IP:.*)|', '', $comment); |
|---|
| 335 | | |
|---|
| 336 | | preg_match("|URL:(.*)|", $comment, $comment_author_url); |
|---|
| 337 | | $comment_author_url = $wpdb->escape(trim($comment_author_url[1])); |
|---|
| 338 | | $comment = preg_replace('|(\n?URL:.*)|', '', $comment); |
|---|
| 339 | | |
|---|
| 340 | | preg_match("|DATE:(.*)|", $comment, $comment_date); |
|---|
| 341 | | $comment_date = trim($comment_date[1]); |
|---|
| 342 | | $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); |
|---|
| 343 | | $comment = preg_replace('|(\n?DATE:.*)|', '', $comment); |
|---|
| 344 | | |
|---|
| 345 | | $comment_content = $wpdb->escape(trim($comment)); |
|---|
| 346 | | $comment_content = str_replace('-----', '', $comment_content); |
|---|
| 347 | | // Check if it's already there |
|---|
| 348 | | if (!comment_exists($comment_author, $comment_date)) { |
|---|
| 349 | | $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_approved'); |
|---|
| 350 | | $commentdata = wp_filter_comment($commentdata); |
|---|
| 351 | | wp_insert_comment($commentdata); |
|---|
| 352 | | $num_comments++; |
|---|
| 353 | | } |
|---|
| 354 | | } |
|---|
| | 290 | $context = ''; |
|---|
| | 291 | } else if ( '--------' == $line ) { |
|---|
| | 292 | // Finishing a post. |
|---|
| | 293 | $context = ''; |
|---|
| | 294 | $this->save_post($post, $comments, $pings); |
|---|
| | 295 | $post = new StdClass; |
|---|
| | 296 | $comment = new StdClass(); |
|---|
| | 297 | $ping = new StdClass(); |
|---|
| | 298 | $comments = array(); |
|---|
| | 299 | $pings = array(); |
|---|
| | 300 | } else if ( 'BODY:' == $line ) { |
|---|
| | 301 | $context = 'body'; |
|---|
| | 302 | } else if ( 'EXTENDED BODY:' == $line ) { |
|---|
| | 303 | $context = 'extended'; |
|---|
| | 304 | } else if ( 'EXCERPT:' == $line ) { |
|---|
| | 305 | $context = 'excerpt'; |
|---|
| | 306 | } else if ( 'KEYWORDS:' == $line ) { |
|---|
| | 307 | $context = 'keywords'; |
|---|
| | 308 | } else if ( 'COMMENT:' == $line ) { |
|---|
| | 309 | $context = 'comment'; |
|---|
| | 310 | } else if ( 'PING:' == $line ) { |
|---|
| | 311 | $context = 'ping'; |
|---|
| | 312 | } else if ( 0 === strpos($line, "AUTHOR:") ) { |
|---|
| | 313 | $author = trim( substr($line, strlen("AUTHOR:")) ); |
|---|
| | 314 | if ( '' == $context ) |
|---|
| | 315 | $post->post_author = $author; |
|---|
| | 316 | else if ( 'comment' == $context ) |
|---|
| | 317 | $comment->comment_author = $author; |
|---|
| | 318 | } else if ( 0 === strpos($line, "TITLE:") ) { |
|---|
| | 319 | $title = trim( substr($line, strlen("TITLE:")) ); |
|---|
| | 320 | if ( '' == $context ) |
|---|
| | 321 | $post->post_title = $title; |
|---|
| | 322 | else if ( 'ping' == $context ) |
|---|
| | 323 | $ping->title = $title; |
|---|
| | 324 | } else if ( 0 === strpos($line, "STATUS:") ) { |
|---|
| | 325 | $status = trim( substr($line, strlen("STATUS:")) ); |
|---|
| | 326 | if ( empty($status) ) |
|---|
| | 327 | $status = 'publish'; |
|---|
| | 328 | $post->post_status = $status; |
|---|
| | 329 | } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) { |
|---|
| | 330 | $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) ); |
|---|
| | 331 | if ( $allow == 1 ) |
|---|
| | 332 | $post->comment_status = 'open'; |
|---|
| | 333 | else |
|---|
| | 334 | $post->comment_status = 'closed'; |
|---|
| | 335 | } else if ( 0 === strpos($line, "ALLOW PINGS:") ) { |
|---|
| | 336 | $allow = trim( substr($line, strlen("ALLOW PINGS:")) ); |
|---|
| | 337 | if ( $allow == 1 ) |
|---|
| | 338 | $post->ping_status = 'open'; |
|---|
| | 339 | else |
|---|
| | 340 | $post->ping_status = 'closed'; |
|---|
| | 341 | } else if ( 0 === strpos($line, "CATEGORY:") ) { |
|---|
| | 342 | $category = trim( substr($line, strlen("CATEGORY:")) ); |
|---|
| | 343 | if ( '' != $category ) |
|---|
| | 344 | $post->categories[] = $category; |
|---|
| | 345 | } else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) { |
|---|
| | 346 | $category = trim( substr($line, strlen("PRIMARY CATEGORY:")) ); |
|---|
| | 347 | if ( '' != $category ) |
|---|
| | 348 | $post->categories[] = $category; |
|---|
| | 349 | } else if ( 0 === strpos($line, "DATE:") ) { |
|---|
| | 350 | $date = trim( substr($line, strlen("DATE:")) ); |
|---|
| | 351 | $date = strtotime($date); |
|---|
| | 352 | $date = date('Y-m-d H:i:s', $date); |
|---|
| | 353 | $date_gmt = get_gmt_from_date($date); |
|---|
| | 354 | if ( '' == $context ) { |
|---|
| | 355 | $post->post_modified = $date; |
|---|
| | 356 | $post->post_modified_gmt = $date_gmt; |
|---|
| | 357 | $post->post_date = $date; |
|---|
| | 358 | $post->post_date_gmt = $date_gmt; |
|---|
| | 359 | } else if ( 'comment' == $context ) { |
|---|
| | 360 | $comment->comment_date = $date; |
|---|
| | 361 | } else if ( 'ping' == $context ) { |
|---|
| | 362 | $ping->comment_date = $date; |
|---|
| 356 | | if ( $num_comments ) |
|---|
| 357 | | printf(' '.__('(%s comments)'), $num_comments); |
|---|
| 358 | | |
|---|
| 359 | | // Finally the pings |
|---|
| 360 | | // fix the double newline on the first one |
|---|
| 361 | | $pings[0] = str_replace("-----\n\n", "-----\n", $pings[0]); |
|---|
| 362 | | $pings = explode("-----\nPING:", $pings[0]); |
|---|
| 363 | | $num_pings = 0; |
|---|
| 364 | | foreach ($pings as $ping) { |
|---|
| 365 | | if ('' != trim($ping)) { |
|---|
| 366 | | // 'Author' |
|---|
| 367 | | preg_match("|BLOG NAME:(.*)|", $ping, $comment_author); |
|---|
| 368 | | $comment_author = $wpdb->escape(trim($comment_author[1])); |
|---|
| 369 | | $ping = preg_replace('|(\n?BLOG NAME:.*)|', '', $ping); |
|---|
| 370 | | |
|---|
| 371 | | preg_match("|IP:(.*)|", $ping, $comment_author_IP); |
|---|
| 372 | | $comment_author_IP = trim($comment_author_IP[1]); |
|---|
| 373 | | $ping = preg_replace('|(\n?IP:.*)|', '', $ping); |
|---|
| 374 | | |
|---|
| 375 | | preg_match("|URL:(.*)|", $ping, $comment_author_url); |
|---|
| 376 | | $comment_author_url = $wpdb->escape(trim($comment_author_url[1])); |
|---|
| 377 | | $ping = preg_replace('|(\n?URL:.*)|', '', $ping); |
|---|
| 378 | | |
|---|
| 379 | | preg_match("|DATE:(.*)|", $ping, $comment_date); |
|---|
| 380 | | $comment_date = trim($comment_date[1]); |
|---|
| 381 | | $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); |
|---|
| 382 | | $ping = preg_replace('|(\n?DATE:.*)|', '', $ping); |
|---|
| 383 | | |
|---|
| 384 | | preg_match("|TITLE:(.*)|", $ping, $ping_title); |
|---|
| 385 | | $ping_title = $wpdb->escape(trim($ping_title[1])); |
|---|
| 386 | | $ping = preg_replace('|(\n?TITLE:.*)|', '', $ping); |
|---|
| 387 | | |
|---|
| 388 | | $comment_content = $wpdb->escape(trim($ping)); |
|---|
| 389 | | $comment_content = str_replace('-----', '', $comment_content); |
|---|
| 390 | | |
|---|
| 391 | | $comment_content = "<strong>$ping_title</strong>\n\n$comment_content"; |
|---|
| 392 | | |
|---|
| 393 | | $comment_type = 'trackback'; |
|---|
| 394 | | |
|---|
| 395 | | // Check if it's already there |
|---|
| 396 | | if (!comment_exists($comment_author, $comment_date)) { |
|---|
| 397 | | $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_type', 'comment_approved'); |
|---|
| 398 | | $commentdata = wp_filter_comment($commentdata); |
|---|
| 399 | | wp_insert_comment($commentdata); |
|---|
| 400 | | $num_pings++; |
|---|
| 401 | | } |
|---|
| 402 | | } |
|---|
| | 364 | } else if ( 0 === strpos($line, "EMAIL:") ) { |
|---|
| | 365 | $email = trim( substr($line, strlen("EMAIL:")) ); |
|---|
| | 366 | if ( 'comment' == $context ) |
|---|
| | 367 | $comment->comment_author_email = $email; |
|---|
| | 368 | else |
|---|
| | 369 | $ping->comment_author_email = $email; |
|---|
| | 370 | } else if ( 0 === strpos($line, "IP:") ) { |
|---|
| | 371 | $ip = trim( substr($line, strlen("IP:")) ); |
|---|
| | 372 | if ( 'comment' == $context ) |
|---|
| | 373 | $comment->comment_author_IP = $ip; |
|---|
| | 374 | else |
|---|
| | 375 | $ping->comment_author_IP = $ip; |
|---|
| | 376 | } else if ( 0 === strpos($line, "URL:") ) { |
|---|
| | 377 | $url = trim( substr($line, strlen("URL:")) ); |
|---|
| | 378 | if ( 'comment' == $context ) |
|---|
| | 379 | $comment->comment_author_url = $url; |
|---|
| | 380 | else |
|---|
| | 381 | $ping->comment_author_url = $url; |
|---|
| | 382 | } else if ( 0 === strpos($line, "BLOG NAME:") ) { |
|---|
| | 383 | $blog = trim( substr($line, strlen("BLOG NAME:")) ); |
|---|
| | 384 | $ping->comment_author = $blog; |
|---|
| | 385 | } else { |
|---|
| | 386 | // Processing multi-line field, check context. |
|---|
| | 387 | |
|---|
| | 388 | $line .= "\n"; |
|---|
| | 389 | if ( 'body' == $context ) { |
|---|
| | 390 | $post->post_content .= $line; |
|---|
| | 391 | } else if ( 'extended' == $context ) { |
|---|
| | 392 | $post->extended .= $line; |
|---|
| | 393 | } else if ( 'excerpt' == $context ) { |
|---|
| | 394 | $post->post_excerpt .= $line; |
|---|
| | 395 | } else if ( 'comment' == $context ) { |
|---|
| | 396 | $comment->comment_content .= $line; |
|---|
| | 397 | } else if ( 'ping' == $context ) { |
|---|
| | 398 | $ping->comment_content .= $line; |
|---|