| 38 | | preg_match_all('|<item>(.*?)</item>|is', $importdata, $posts); |
|---|
| 39 | | $this->posts = $posts[1]; |
|---|
| | 49 | preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts); |
|---|
| | 50 | $this->posts = $this->posts[1]; |
|---|
| | 51 | $index = 0; |
|---|
| | 52 | foreach ($this->posts as $post) { |
|---|
| | 53 | preg_match('|<title>(.*?)</title>|is', $post, $post_title); |
|---|
| | 54 | $post_title = $wpdb->escape(trim($post_title[1])); |
|---|
| | 55 | |
|---|
| | 56 | preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date); |
|---|
| | 57 | |
|---|
| | 58 | if ($post_date) { |
|---|
| | 59 | $post_date = strtotime($post_date[1]); |
|---|
| | 60 | } else { |
|---|
| | 61 | // if we don't already have something from pubDate |
|---|
| | 62 | preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date); |
|---|
| | 63 | $post_date = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date[1]); |
|---|
| | 64 | $post_date = str_replace('T', ' ', $post_date); |
|---|
| | 65 | $post_date = strtotime($post_date); |
|---|
| | 66 | } |
|---|
| | 67 | |
|---|
| | 68 | $post_date = gmdate('Y-m-d H:i:s', $post_date); |
|---|
| | 69 | |
|---|
| | 70 | preg_match_all('|<category>(.*?)</category>|is', $post, $categories); |
|---|
| | 71 | $categories = $categories[1]; |
|---|
| | 72 | |
|---|
| | 73 | if (!$categories) { |
|---|
| | 74 | preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories); |
|---|
| | 75 | $categories = $categories[1]; |
|---|
| | 76 | } |
|---|
| | 77 | |
|---|
| | 78 | $cat_index = 0; |
|---|
| | 79 | foreach ($categories as $category) { |
|---|
| | 80 | $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category)); |
|---|
| | 81 | $cat_index++; |
|---|
| | 82 | } |
|---|
| | 83 | |
|---|
| | 84 | preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid); |
|---|
| | 85 | if ($guid) |
|---|
| | 86 | $guid = $wpdb->escape(trim($guid[1])); |
|---|
| | 87 | else |
|---|
| | 88 | $guid = ''; |
|---|
| | 89 | |
|---|
| | 90 | preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content); |
|---|
| | 91 | $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1]))); |
|---|
| | 92 | |
|---|
| | 93 | if (!$post_content) { |
|---|
| | 94 | // This is for feeds that put content in description |
|---|
| | 95 | preg_match('|<description>(.*?)</description>|is', $post, $post_content); |
|---|
| | 96 | $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1]))); |
|---|
| | 97 | } |
|---|
| | 98 | |
|---|
| | 99 | // Clean up content |
|---|
| | 100 | $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); |
|---|
| | 101 | $post_content = str_replace('<br>', '<br />', $post_content); |
|---|
| | 102 | $post_content = str_replace('<hr>', '<hr />', $post_content); |
|---|
| | 103 | |
|---|
| | 104 | $post_author = 1; |
|---|
| | 105 | $post_status = 'publish'; |
|---|
| | 106 | $post_date_gmt = $post_date; // FIXME |
|---|
| | 107 | $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories'); |
|---|
| | 108 | $index++; |
|---|
| | 109 | } |
|---|
| 54 | | if ($date) |
|---|
| 55 | | : $date = strtotime($date[1]); |
|---|
| 56 | | else |
|---|
| 57 | | : // if we don't already have something from pubDate |
|---|
| 58 | | preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $date); |
|---|
| 59 | | $date = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $date[1]); |
|---|
| 60 | | $date = str_replace('T', ' ', $date); |
|---|
| 61 | | $date = strtotime($date); |
|---|
| 62 | | endif; |
|---|
| | 120 | if ($post_id = post_exists($post_title, $post_content, $post_date)) { |
|---|
| | 121 | echo __('Post already imported'); |
|---|
| | 122 | } else { |
|---|
| | 123 | $post_id = wp_insert_post($post); |
|---|
| | 124 | if (!$post_id) |
|---|
| | 125 | die(__("Couldn't get post ID")); |
|---|
| | 126 | |
|---|
| | 127 | if (0 != count($categories)) |
|---|
| | 128 | wp_create_categories($categories, $post_id); |
|---|
| | 129 | echo __('Done !'); |
|---|
| | 130 | } |
|---|
| | 131 | echo '</li>'; |
|---|
| | 132 | } |
|---|
| 64 | | $post_date = gmdate('Y-m-d H:i:s', $date); |
|---|
| 65 | | |
|---|
| 66 | | preg_match_all('|<category>(.*?)</category>|is', $post, $categories); |
|---|
| 67 | | $categories = $categories[1]; |
|---|
| 68 | | |
|---|
| 69 | | if (!$categories) |
|---|
| 70 | | : preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories); |
|---|
| 71 | | $categories = $categories[1]; |
|---|
| 72 | | endif; |
|---|
| 73 | | |
|---|
| 74 | | preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid); |
|---|
| 75 | | if ($guid) |
|---|
| 76 | | $guid = $wpdb->escape(trim($guid[1])); |
|---|
| 77 | | else |
|---|
| 78 | | $guid = ''; |
|---|
| 79 | | |
|---|
| 80 | | preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $content); |
|---|
| 81 | | $content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($content[1]))); |
|---|
| 82 | | |
|---|
| 83 | | if (!$content) |
|---|
| 84 | | : // This is for feeds that put content in description |
|---|
| 85 | | preg_match('|<description>(.*?)</description>|is', $post, $content); |
|---|
| 86 | | $content = $wpdb->escape(unhtmlentities(trim($content[1]))); |
|---|
| 87 | | endif; |
|---|
| 88 | | |
|---|
| 89 | | // Clean up content |
|---|
| 90 | | $content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $content); |
|---|
| 91 | | $content = str_replace('<br>', '<br />', $content); |
|---|
| 92 | | $content = str_replace('<hr>', '<hr />', $content); |
|---|
| 93 | | |
|---|
| 94 | | // This can mess up on posts with no titles, but checking content is much slower |
|---|
| 95 | | // So we do it as a last resort |
|---|
| 96 | | if ('' == $title) |
|---|
| 97 | | : $dupe = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' AND post_date = '$post_date'"); |
|---|
| 98 | | else |
|---|
| 99 | | : $dupe = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' AND post_date = '$post_date'"); |
|---|
| 100 | | endif; |
|---|
| 101 | | |
|---|
| 102 | | // Now lets put it in the DB |
|---|
| 103 | | if ($dupe) |
|---|
| 104 | | : echo 'Post already imported'; |
|---|
| 105 | | else |
|---|
| 106 | | : $wpdb->query("INSERT INTO $wpdb->posts |
|---|
| 107 | | (post_author, post_date, post_date_gmt, post_content, post_title,post_status, comment_status, ping_status, post_name, guid) |
|---|
| 108 | | VALUES |
|---|
| 109 | | ('$post_author', '$post_date', DATE_ADD('$post_date', INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE), '$content', '$title', 'publish', '$comment_status', '$ping_status', '$post_name', '$guid')"); |
|---|
| 110 | | $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' AND post_date = '$post_date'"); |
|---|
| 111 | | if (!$post_id) |
|---|
| 112 | | die("couldn't get post ID"); |
|---|
| 113 | | if (0 != count($categories)) |
|---|
| 114 | | : foreach ($categories as $post_category) |
|---|
| 115 | | : $post_category = unhtmlentities($post_category); |
|---|
| 116 | | // See if the category exists yet |
|---|
| 117 | | $cat_id = $wpdb->get_var("SELECT cat_ID from $wpdb->categories WHERE cat_name = '$post_category'"); |
|---|
| 118 | | if (!$cat_id && '' != trim($post_category)) { |
|---|
| 119 | | $cat_nicename = sanitize_title($post_category); |
|---|
| 120 | | $wpdb->query("INSERT INTO $wpdb->categories (cat_name, category_nicename) VALUES ('$post_category', '$cat_nicename')"); |
|---|
| 121 | | $cat_id = $wpdb->get_var("SELECT cat_ID from $wpdb->categories WHERE cat_name = '$post_category'"); |
|---|
| 122 | | } |
|---|
| 123 | | if ('' == trim($post_category)) |
|---|
| 124 | | $cat_id = 1; |
|---|
| 125 | | // Double check it's not there already |
|---|
| 126 | | $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = $cat_id"); |
|---|
| 127 | | |
|---|
| 128 | | if (!$exists) { |
|---|
| 129 | | $wpdb->query(" |
|---|
| 130 | | INSERT INTO $wpdb->post2cat |
|---|
| 131 | | (post_id, category_id) |
|---|
| 132 | | VALUES |
|---|
| 133 | | ($post_id, $cat_id) |
|---|
| 134 | | "); |
|---|
| 135 | | } |
|---|
| 136 | | endforeach; |
|---|
| 137 | | else |
|---|
| 138 | | : $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = 1"); |
|---|
| 139 | | if (!$exists) |
|---|
| 140 | | $wpdb->query("INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ($post_id, 1) "); |
|---|
| 141 | | endif; |
|---|
| 142 | | echo 'Done!</li>'; |
|---|
| 143 | | endif; |
|---|
| 144 | | |
|---|
| 145 | | endforeach; |
|---|