PageRenderTime 55ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/2.0/wp-admin/import/rss.php

#
PHP | 175 lines | 145 code | 25 blank | 5 comment | 8 complexity | 48752c550c90aa9bf7e47f98aef74586 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. // Example:
  3. // define('RSSFILE', '/home/example/public_html/rss.xml');
  4. define('RSSFILE', 'rss.xml');
  5. class RSS_Import {
  6. var $posts = array ();
  7. var $file;
  8. function header() {
  9. echo '<div class="wrap">';
  10. echo '<h2>'.__('Import RSS').'</h2>';
  11. }
  12. function footer() {
  13. echo '</div>';
  14. }
  15. function unhtmlentities($string) { // From php.net for < 4.3 compat
  16. $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  17. $trans_tbl = array_flip($trans_tbl);
  18. return strtr($string, $trans_tbl);
  19. }
  20. function greet() {
  21. echo '<p>'.__('Howdy! This importer allows you to extract posts from any RSS 2.0 file into your blog. This is useful if you want to import your posts from a system that is not handled by a custom import tool. Pick an RSS file to upload and click Import.').'</p>';
  22. wp_import_upload_form("admin.php?import=rss&amp;step=1");
  23. }
  24. function get_posts() {
  25. global $wpdb;
  26. set_magic_quotes_runtime(0);
  27. $datalines = file($this->file); // Read the file into an array
  28. $importdata = implode('', $datalines); // squish it
  29. $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
  30. preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
  31. $this->posts = $this->posts[1];
  32. $index = 0;
  33. foreach ($this->posts as $post) {
  34. preg_match('|<title>(.*?)</title>|is', $post, $post_title);
  35. $post_title = $wpdb->escape(trim($post_title[1]));
  36. preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date);
  37. if ($post_date) {
  38. $post_date = strtotime($post_date[1]);
  39. } else {
  40. // if we don't already have something from pubDate
  41. preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date);
  42. $post_date = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date[1]);
  43. $post_date = str_replace('T', ' ', $post_date);
  44. $post_date = strtotime($post_date);
  45. }
  46. $post_date = gmdate('Y-m-d H:i:s', $post_date);
  47. preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
  48. $categories = $categories[1];
  49. if (!$categories) {
  50. preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
  51. $categories = $categories[1];
  52. }
  53. $cat_index = 0;
  54. foreach ($categories as $category) {
  55. $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
  56. $cat_index++;
  57. }
  58. preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid);
  59. if ($guid)
  60. $guid = $wpdb->escape(trim($guid[1]));
  61. else
  62. $guid = '';
  63. preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
  64. $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
  65. if (!$post_content) {
  66. // This is for feeds that put content in description
  67. preg_match('|<description>(.*?)</description>|is', $post, $post_content);
  68. $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1])));
  69. }
  70. // Clean up content
  71. $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
  72. $post_content = str_replace('<br>', '<br />', $post_content);
  73. $post_content = str_replace('<hr>', '<hr />', $post_content);
  74. $post_author = 1;
  75. $post_status = 'publish';
  76. $this->posts[$index] = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
  77. $index++;
  78. }
  79. }
  80. function import_posts() {
  81. echo '<ol>';
  82. foreach ($this->posts as $post) {
  83. echo "<li>".__('Importing post...');
  84. extract($post);
  85. if ($post_id = post_exists($post_title, $post_content, $post_date)) {
  86. _e('Post already imported');
  87. } else {
  88. $post_id = wp_insert_post($post);
  89. if (!$post_id) {
  90. _e("Couldn't get post ID");
  91. return;
  92. }
  93. if (0 != count($categories))
  94. wp_create_categories($categories, $post_id);
  95. _e('Done !');
  96. }
  97. echo '</li>';
  98. }
  99. echo '</ol>';
  100. }
  101. function import() {
  102. $file = wp_import_handle_upload();
  103. if ( isset($file['error']) ) {
  104. echo $file['error'];
  105. return;
  106. }
  107. $this->file = $file['file'];
  108. $this->get_posts();
  109. $this->import_posts();
  110. wp_import_cleanup($file['id']);
  111. echo '<h3>';
  112. printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
  113. echo '</h3>';
  114. }
  115. function dispatch() {
  116. if (empty ($_GET['step']))
  117. $step = 0;
  118. else
  119. $step = (int) $_GET['step'];
  120. $this->header();
  121. switch ($step) {
  122. case 0 :
  123. $this->greet();
  124. break;
  125. case 1 :
  126. $this->import();
  127. break;
  128. }
  129. $this->footer();
  130. }
  131. function RSS_Import() {
  132. // Nothing.
  133. }
  134. }
  135. $rss_import = new RSS_Import();
  136. register_importer('rss', 'RSS', __('Import posts from an RSS feed'), array ($rss_import, 'dispatch'));
  137. ?>