| [ Index ] |
WordPress Source Cross Reference |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* By Shayne Sweeney - http://www.theshayne.com/ */ 4 5 class BW_Import { 6 7 var $file; 8 9 function header() { 10 echo '<div class="wrap">'; 11 echo '<h2>'.__('Import Blogware').'</h2>'; 12 } 13 14 function footer() { 15 echo '</div>'; 16 } 17 18 function unhtmlentities($string) { // From php.net for < 4.3 compat 19 $trans_tbl = get_html_translation_table(HTML_ENTITIES); 20 $trans_tbl = array_flip($trans_tbl); 21 return strtr($string, $trans_tbl); 22 } 23 24 function greet() { 25 echo '<p>'.__('Howdy! This importer allows you to extract posts from Blogware XML export file into your blog. Pick a Blogware file to upload and click Import.').'</p>'; 26 wp_import_upload_form("admin.php?import=blogware&step=1"); 27 } 28 29 function import_posts() { 30 global $wpdb, $current_user; 31 32 set_magic_quotes_runtime(0); 33 $importdata = file($this->file); // Read the file into an array 34 $importdata = implode('', $importdata); // squish it 35 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata); 36 37 preg_match_all('|(<item[^>]+>(.*?)</item>)|is', $importdata, $posts); 38 $posts = $posts[1]; 39 unset($importdata); 40 echo '<ol>'; 41 foreach ($posts as $post) { 42 flush(); 43 preg_match('|<item type=\"(.*?)\">|is', $post, $post_type); 44 $post_type = $post_type[1]; 45 if($post_type == "photo") { 46 preg_match('|<photoFilename>(.*?)</photoFilename>|is', $post, $post_title); 47 } else { 48 preg_match('|<title>(.*?)</title>|is', $post, $post_title); 49 } 50 $post_title = $wpdb->escape(trim($post_title[1])); 51 52 preg_match('|<pubDate>(.*?)</pubDate>|is', $post, $post_date); 53 $post_date = strtotime($post_date[1]); 54 $post_date = gmdate('Y-m-d H:i:s', $post_date); 55 56 preg_match_all('|<category>(.*?)</category>|is', $post, $categories); 57 $categories = $categories[1]; 58 59 $cat_index = 0; 60 foreach ($categories as $category) { 61 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category)); 62 $cat_index++; 63 } 64 65 if(strcasecmp($post_type, "photo") === 0) { 66 preg_match('|<sizedPhotoUrl>(.*?)</sizedPhotoUrl>|is', $post, $post_content); 67 $post_content = '<img src="'.trim($post_content[1]).'" />'; 68 $post_content = $this->unhtmlentities($post_content); 69 } else { 70 preg_match('|<body>(.*?)</body>|is', $post, $post_content); 71 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1])); 72 $post_content = $this->unhtmlentities($post_content); 73 } 74 75 // Clean up content 76 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); 77 $post_content = str_replace('<br>', '<br />', $post_content); 78 $post_content = str_replace('<hr>', '<hr />', $post_content); 79 $post_content = $wpdb->escape($post_content); 80 81 $post_author = $current_user->ID; 82 preg_match('|<postStatus>(.*?)</postStatus>|is', $post, $post_status); 83 $post_status = trim($post_status[1]); 84 85 echo '<li>'; 86 if ($post_id = post_exists($post_title, $post_content, $post_date)) { 87 printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); 88 } else { 89 printf(__('Importing post <i>%s</i>...'), stripslashes($post_title)); 90 $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status'); 91 $post_id = wp_insert_post($postdata); 92 if (!$post_id) { 93 _e("Couldn't get post ID"); 94 echo '</li>'; 95 break; 96 } 97 if(0 != count($categories)) 98 wp_create_categories($categories, $post_id); 99 } 100 101 preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments); 102 $comments = $comments[1]; 103 104 if ( $comments ) { 105 $comment_post_ID = $post_id; 106 $num_comments = 0; 107 foreach ($comments as $comment) { 108 preg_match('|<body>(.*?)</body>|is', $comment, $comment_content); 109 $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1])); 110 $comment_content = $this->unhtmlentities($comment_content); 111 112 // Clean up content 113 $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content); 114 $comment_content = str_replace('<br>', '<br />', $comment_content); 115 $comment_content = str_replace('<hr>', '<hr />', $comment_content); 116 $comment_content = $wpdb->escape($comment_content); 117 118 preg_match('|<pubDate>(.*?)</pubDate>|is', $comment, $comment_date); 119 $comment_date = trim($comment_date[1]); 120 $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); 121 122 preg_match('|<author>(.*?)</author>|is', $comment, $comment_author); 123 $comment_author = $wpdb->escape(trim($comment_author[1])); 124 125 $comment_author_email = NULL; 126 127 $comment_approved = 1; 128 // Check if it's already there 129 if (!comment_exists($comment_author, $comment_date)) { 130 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved'); 131 $commentdata = wp_filter_comment($commentdata); 132 wp_insert_comment($commentdata); 133 $num_comments++; 134 } 135 } 136 } 137 if ( $num_comments ) { 138 echo ' '; 139 printf(__('(%s comments)'), $num_comments); 140 } 141 echo '</li>'; 142 flush(); 143 ob_flush(); 144 } 145 echo '</ol>'; 146 } 147 148 function import() { 149 $file = wp_import_handle_upload(); 150 if ( isset($file['error']) ) { 151 echo $file['error']; 152 return; 153 } 154 155 $this->file = $file['file']; 156 $this->import_posts(); 157 wp_import_cleanup($file['id']); 158 159 echo '<h3>'; 160 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')); 161 echo '</h3>'; 162 } 163 164 function dispatch() { 165 if (empty ($_GET['step'])) 166 $step = 0; 167 else 168 $step = (int) $_GET['step']; 169 170 $this->header(); 171 172 switch ($step) { 173 case 0 : 174 $this->greet(); 175 break; 176 case 1 : 177 $this->import(); 178 break; 179 } 180 181 $this->footer(); 182 } 183 184 function BW_Import() { 185 // Nothing. 186 } 187 } 188 189 $blogware_import = new BW_Import(); 190 191 register_importer('blogware', 'Blogware', __('Import posts from Blogware'), array ($blogware_import, 'dispatch')); 192 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Jul 15 11:57:04 2006 | Courtesy of Taragana |