📄 blogger.php
字号:
$body = $response['body']; $body = preg_replace("|\<!DOCTYPE.*\<body[^>]*>|ms","",$body); $body = preg_replace("|/?{$blog_opt}.do|","admin.php?import=blogger&noheader=true&step=2&blog={$_GET['blog']}&form={$blog_opt}",$body); $body = str_replace("name='submit'","name='supermit'",$body); $body = str_replace('name="submit"','name="supermit"',$body); $body = str_replace('</body>','',str_replace('</html>','',$body)); $form = "<div style='height:0px;width:0px;overflow:hidden;'>"; $form.= $body; $form.= "</div><script type='text/javascript'>forms=document.getElementsByTagName('form');for(i=0;i<forms.length;i++){if(forms[i].action.search('{$blog_opt}')){forms[i].submit();break;}}</script>"; $output.= '<p>'.sprintf('<strong>%s</strong> in progress, please wait...', $blog_opt)."</p>\n"; } else { $output.= "<p>$blog_opt</p>\n"; } } if ( $form ) die($output . $form); $this->set_next_step(4); $this->do_next_step(); } // Step 3: Cancelled :-) // Step 4: Publish with the new template and settings. function publish_blog() { $this->publish_blogger(5, __('Publishing with new template and options')); } // Step 5: Get the archive URLs from the new blog. function get_archive_urls() { $bloghtml = $this->get_blogger($this->import['blogs'][$_GET['blog']]['url']); if (! strstr($bloghtml['body'], '<a class="archive"') ) die(__('Your Blogger blog did not take the new template or did not respond.')); preg_match_all('#<a class="archive" href="([^"]*)"#', $bloghtml['body'], $archives); foreach ($archives[1] as $archive) { $this->import['blogs'][$_GET['blog']]['archives'][$archive] = false; } $this->set_next_step(6); $this->do_next_step(); } // Step 6: Get each monthly archive, import it, mark it done. function get_archive() { global $wpdb; $output = '<h2>'.__('Importing Blogger archives into WordPress').'</h2>'; $did_one = false; $post_array = $posts = array(); foreach ( $this->import['blogs'][$_GET['blog']]['archives'] as $url => $status ) { $archivename = substr(basename($url),0,7); if ( $status || $did_one ) { $foo = 'bar'; // Do nothing. } else { // Import the selected month $postcount = 0; $skippedpostcount = 0; $commentcount = 0; $skippedcommentcount = 0; $status = __('in progress...'); $this->import['blogs'][$_GET['blog']]['archives']["$url"] = $status; update_option('import-blogger', $import); $archive = $this->get_blogger($url); if ( $archive['code'] > 200 ) continue; $posts = explode('<wordpresspost>', $archive['body']); for ($i = 1; $i < count($posts); $i = $i + 1) { $postparts = explode('<wordpresscomment>', $posts[$i]); $postinfo = explode('|W|P|', $postparts[0]); $post_date = $postinfo[0]; $post_content = $postinfo[2]; // Don't try to re-use the original numbers // because the new, longer numbers are too // big to handle as ints. //$post_number = $postinfo[3]; $post_title = ( $postinfo[4] != '' ) ? $postinfo[4] : $postinfo[3]; $post_author_name = $wpdb->escape(trim($postinfo[1])); $post_author_email = $postinfo[5] ? $postinfo[5] : 'user@wordpress.org'; if ( $this->lump_authors ) { // Ignore Blogger authors. Use the current user_ID for all posts imported. $post_author = $GLOBALS['user_ID']; } else { // Add a user for each new author encountered. if (! username_exists($post_author_name) ) { $user_login = $wpdb->escape($post_author_name); $user_email = $wpdb->escape($post_author_email); $user_password = substr(md5(uniqid(microtime())), 0, 6); $result = wp_create_user( $user_login, $user_password, $user_email ); $status.= sprintf('Registered user <strong>%s</strong>.', $user_login); $this->import['blogs'][$_GET['blog']]['newusers'][] = $user_login; } $userdata = get_userdatabylogin( $post_author_name ); $post_author = $userdata->ID; } $post_date = explode(' ', $post_date); $post_date_Ymd = explode('/', $post_date[0]); $postyear = $post_date_Ymd[2]; $postmonth = zeroise($post_date_Ymd[0], 2); $postday = zeroise($post_date_Ymd[1], 2); $post_date_His = explode(':', $post_date[1]); $posthour = zeroise($post_date_His[0], 2); $postminute = zeroise($post_date_His[1], 2); $postsecond = zeroise($post_date_His[2], 2); if (($post_date[2] == 'PM') && ($posthour != '12')) $posthour = $posthour + 12; else if (($post_date[2] == 'AM') && ($posthour == '12')) $posthour = '00'; $post_date = "$postyear-$postmonth-$postday $posthour:$postminute:$postsecond"; $post_content = addslashes($post_content); $post_content = str_replace(array('<br>','<BR>','<br/>','<BR/>','<br />','<BR />'), "\n", $post_content); // the XHTML touch... ;) $post_title = addslashes($post_title); $post_status = 'publish'; if ( $ID = post_exists($post_title, '', $post_date) ) { $post_array[$i]['ID'] = $ID; $skippedpostcount++; } else { $post_array[$i]['post'] = compact('post_author', 'post_content', 'post_title', 'post_category', 'post_author', 'post_date', 'post_status'); $post_array[$i]['comments'] = false; } // Import any comments attached to this post. if ($postparts[1]) : for ($j = 1; $j < count($postparts); $j = $j + 1) { $commentinfo = explode('|W|P|', $postparts[$j]); $comment_date = explode(' ', $commentinfo[0]); $comment_date_Ymd = explode('/', $comment_date[0]); $commentyear = $comment_date_Ymd[2]; $commentmonth = zeroise($comment_date_Ymd[0], 2); $commentday = zeroise($comment_date_Ymd[1], 2); $comment_date_His = explode(':', $comment_date[1]); $commenthour = zeroise($comment_date_His[0], 2); $commentminute = zeroise($comment_date_His[1], 2); $commentsecond = '00'; if (($comment_date[2] == 'PM') && ($commenthour != '12')) $commenthour = $commenthour + 12; else if (($comment_date[2] == 'AM') && ($commenthour == '12')) $commenthour = '00'; $comment_date = "$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond"; $comment_author = addslashes(strip_tags($commentinfo[1])); if ( strpos($commentinfo[1], 'a href') ) { $comment_author_parts = explode('"', htmlentities($commentinfo[1])); $comment_author_url = $comment_author_parts[1]; } else $comment_author_url = ''; $comment_content = $commentinfo[2]; $comment_content = str_replace(array('<br>','<BR>','<br/>','<BR/>','<br />','<BR />'), "\n", $comment_content); $comment_approved = 1; if ( comment_exists($comment_author, $comment_date) ) { $skippedcommentcount++; } else { $comment = compact('comment_author', 'comment_author_url', 'comment_date', 'comment_content', 'comment_approved'); $post_array[$i]['comments'][$j] = wp_filter_comment($comment); } $commentcount++; } endif; $postcount++; } if ( count($post_array) ) { krsort($post_array); foreach($post_array as $post) { if ( ! $comment_post_ID = $post['ID'] ) $comment_post_ID = wp_insert_post($post['post']); if ( $post['comments'] ) { foreach ( $post['comments'] as $comment ) { $comment['comment_post_ID'] = $comment_post_ID; wp_insert_comment($comment); } } } } $status = sprintf(__('%s post(s) parsed, %s skipped...'), $postcount, $skippedpostcount).' '. sprintf(__('%s comment(s) parsed, %s skipped...'), $commentcoun, $skippedcommentcount).' '. ' <strong>'.__('Done').'</strong>'; $import = $this->import; $import['blogs'][$_GET['blog']]['archives']["$url"] = $status; update_option('import-blogger', $import); $did_one = true; } $output.= "<p>$archivename $status</p>\n"; } if ( ! $did_one ) $this->set_next_step(7); die( $this->refresher(1000) . $output ); } // Step 7: Restore the backed-up settings to Blogger function restore_settings() { $output = '<h1>'.__('Restoring your Blogger options')."</h1>\n"; $did_one = false; // Restore options in reverse order. if ( ! $this->import['reversed'] ) { $this->import['blogs'][$_GET['blog']]['options'] = array_reverse($this->import['blogs'][$_GET['blog']]['options'], true); $this->import['reversed'] = true; update_option('import-blogger', $this->import); } foreach ( $this->import['blogs'][$_GET['blog']]['options'] as $blog_opt => $optary ) { if ( $did_one ) { $output .= "<p>$blog_opt</p>\n"; } elseif ( $optary['restored'] || ! $optary['modify'] ) { $output .= "<p><del>$blog_opt</del></p>\n"; } else { $posturl = "http://www.blogger.com/{$blog_opt}.do"; $headers = array_merge($this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['cookies'], $this->import['cookies']); if ( 'blog-publishing' == $blog_opt) { if ( $optary['backup']['publishMode'] > 0 ) { $response = $this->get_blogger("http://www.blogger.com/blog-publishing.g?blogID={$_GET['blog']}&publishMode={$optary['backup']['publishMode']}", $headers); sleep(2); if ( $response['code'] >= 400 ) die('<h1>Error restoring publishMode.</h1><p>Please tell the devs.</p>' . addslashes(print_r($response, 1)) ); } } if ( $optary['backup'] != $optary['modify'] ) { $response = $this->post_blogger($posturl, $headers, $optary['backup']); if ( $response['code'] >= 400 || strstr($response['body'], 'There are errors on this form') ) { $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['error'] = true; update_option('import-blogger', $this->import); $output .= sprintf(__('%s failed. Trying again.'), "<p><strong>$blog_opt</strong> ").'</p>'; } else { $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['restored'] = true; update_option('import-blogger', $this->import); $output .= sprintf(__('%s restored.'), "<p><strong>$blog_opt</strong> ").'</p>'; } } $did_one = true; } } if ( $did_one ) { die( $this->refresher(1000) . $output ); } elseif ( $this->import['blogs'][$_GET['blog']]['options']['blog-publishing']['backup']['publishMode'] > 0 ) { $this->set_next_step(9); } else { $this->set_next_step(8); } $this->do_next_step(); } // Step 8: Republish, all back to normal function republish_blog() { $this->publish_blogger(9, __('Publishing with original template and options')); } // Step 9: Congratulate the user function congrats() { echo '<h1>'.__('Congratulations!').'</h1><p>'.__('Now that you have imported your Blogger blog into WordPress, what are you going to do? Here are some suggestions:').'</p><ul><li>'.__('That was hard work! Take a break.').'</li>'; if ( count($this->import['blogs']) > 1 ) echo '<li>'.__('In case you haven\'t done it already, you can import the posts from your other blogs:'). $this->show_blogs() . '</li>'; if ( $n = count($this->import['blogs'][$_GET['blog']]['newusers']) ) echo '<li>'.sprintf(__('Go to <a href="%s" target="%s">Authors & Users</a>, where you can modify the new user(s) or delete them. If you want to make all of the imported posts yours, you will be given that option when you delete the new authors.'), 'users.php', '_parent').'</li>'; echo '<li>'.__('For security, click the link below to reset this importer. That will clear your Blogger credentials and options from the database.').'</li>'; echo '</ul>'; } // Figures out what to do, then does it. function start() { if ( $_GET['restart'] == 'true' ) { $this->restart(); } if ( isset($_GET['noheader']) ) { header('Content-Type: text/html; charset=utf-8'); $this->import = get_settings('import-blogger'); if ( false === $this->import ) { $step = 0; } elseif ( isset($_GET['step']) ) { $step = (int) $_GET['step']; } elseif ( isset($_GET['blog']) && isset($this->import['blogs'][$_GET['blog']]['nextstep']) ) { $step = $this->import['blogs'][$_GET['blog']]['nextstep']; } elseif ( is_array($this->import['blogs']) ) { $step = 1; } else { $step = 0; }//echo "Step $step.";//die('<pre>'.print_r($this->import,1).'</pre'); switch ($step) { case 0 : $this->do_login(); break; case 1 : $this->select_blog(); break; case 2 : $this->backup_settings(); break; case 3 : $this->wait_for_blogger(); break; case 4 : $this->publish_blog(); break; case 5 : $this->get_archive_urls(); break; case 6 : $this->get_archive(); break; case 7 : $this->restore_settings(); break; case 8 : $this->republish_blog(); break; case 9 : $this->congrats(); break; } die; } else { $this->greet(); } } function Blogger_Import() { // This space intentionally left blank. }}$blogger_import = new Blogger_Import();register_importer('blogger', 'Blogger', __('Import posts and comments from a Blogger account'), array ($blogger_import, 'start'));?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -