3 /* By Shayne Sweeney - http://www.theshayne.com/ */
10 echo '<div class="wrap">';
11 echo '<h2>'.__('Import Blogware').'</h2>';
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);
25 echo '<div class="narrow">';
26 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>';
27 wp_import_upload_form("admin.php?import=blogware&step=1");
31 function import_posts() {
32 global $wpdb, $current_user;
34 set_magic_quotes_runtime(0);
35 $importdata = file($this->file); // Read the file into an array
36 $importdata = implode('', $importdata); // squish it
37 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
39 preg_match_all('|(<item[^>]+>(.*?)</item>)|is', $importdata, $posts);
43 foreach ($posts as $post) {
45 preg_match('|<item type=\"(.*?)\">|is', $post, $post_type);
46 $post_type = $post_type[1];
47 if($post_type == "photo") {
48 preg_match('|<photoFilename>(.*?)</photoFilename>|is', $post, $post_title);
50 preg_match('|<title>(.*?)</title>|is', $post, $post_title);
52 $post_title = $wpdb->escape(trim($post_title[1]));
54 preg_match('|<pubDate>(.*?)</pubDate>|is', $post, $post_date);
55 $post_date = strtotime($post_date[1]);
56 $post_date = gmdate('Y-m-d H:i:s', $post_date);
58 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
59 $categories = $categories[1];
62 foreach ($categories as $category) {
63 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
67 if(strcasecmp($post_type, "photo") === 0) {
68 preg_match('|<sizedPhotoUrl>(.*?)</sizedPhotoUrl>|is', $post, $post_content);
69 $post_content = '<img src="'.trim($post_content[1]).'" />';
70 $post_content = $this->unhtmlentities($post_content);
72 preg_match('|<body>(.*?)</body>|is', $post, $post_content);
73 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
74 $post_content = $this->unhtmlentities($post_content);
78 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
79 $post_content = str_replace('<br>', '<br />', $post_content);
80 $post_content = str_replace('<hr>', '<hr />', $post_content);
81 $post_content = $wpdb->escape($post_content);
83 $post_author = $current_user->ID;
84 preg_match('|<postStatus>(.*?)</postStatus>|is', $post, $post_status);
85 $post_status = trim($post_status[1]);
88 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
89 printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
91 printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
92 $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
93 $post_id = wp_insert_post($postdata);
94 if ( is_wp_error( $post_id ) ) {
98 _e("Couldn't get post ID");
102 if(0 != count($categories))
103 wp_create_categories($categories, $post_id);
106 preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
107 $comments = $comments[1];
110 $comment_post_ID = (int) $post_id;
112 foreach ($comments as $comment) {
113 preg_match('|<body>(.*?)</body>|is', $comment, $comment_content);
114 $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
115 $comment_content = $this->unhtmlentities($comment_content);
118 $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content);
119 $comment_content = str_replace('<br>', '<br />', $comment_content);
120 $comment_content = str_replace('<hr>', '<hr />', $comment_content);
121 $comment_content = $wpdb->escape($comment_content);
123 preg_match('|<pubDate>(.*?)</pubDate>|is', $comment, $comment_date);
124 $comment_date = trim($comment_date[1]);
125 $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
127 preg_match('|<author>(.*?)</author>|is', $comment, $comment_author);
128 $comment_author = $wpdb->escape(trim($comment_author[1]));
130 $comment_author_email = NULL;
132 $comment_approved = 1;
133 // Check if it's already there
134 if (!comment_exists($comment_author, $comment_date)) {
135 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
136 $commentdata = wp_filter_comment($commentdata);
137 wp_insert_comment($commentdata);
142 if ( $num_comments ) {
144 printf( __ngettext('%s comment', '%s comments', $num_comments), $num_comments );
154 $file = wp_import_handle_upload();
155 if ( isset($file['error']) ) {
160 $this->file = $file['file'];
161 $result = $this->import_posts();
162 if ( is_wp_error( $result ) )
164 wp_import_cleanup($file['id']);
165 do_action('import_done', 'blogware');
167 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
171 function dispatch() {
172 if (empty ($_GET['step']))
175 $step = (int) $_GET['step'];
184 $result = $this->import();
185 if ( is_wp_error( $result ) )
186 $result->get_error_message();
193 function BW_Import() {
198 $blogware_import = new BW_Import();
200 register_importer('blogware', __('Blogware'), __('Import posts from Blogware.'), array ($blogware_import, 'dispatch'));