PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/import/rss.php

https://github.com/alx/pressmark
PHP | 202 lines | 145 code | 38 blank | 19 comment | 16 complexity | 1bd06a64b7608a1a1b6e1552c3d9a6d1 MD5 | raw file
  1. <?php
  2. /**
  3. * RSS Importer
  4. *
  5. * @package WordPress
  6. * @subpackage Importer
  7. */
  8. /**
  9. * RSS Importer
  10. *
  11. * Will process a RSS feed for importing posts into WordPress. This is a very
  12. * limited importer and should only be used as the last resort, when no other
  13. * importer is available.
  14. *
  15. * @since unknown
  16. */
  17. class RSS_Import {
  18. var $posts = array ();
  19. var $file;
  20. function header() {
  21. echo '<div class="wrap">';
  22. screen_icon();
  23. echo '<h2>'.__('Import RSS').'</h2>';
  24. }
  25. function footer() {
  26. echo '</div>';
  27. }
  28. function unhtmlentities($string) { // From php.net for < 4.3 compat
  29. $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  30. $trans_tbl = array_flip($trans_tbl);
  31. return strtr($string, $trans_tbl);
  32. }
  33. function greet() {
  34. echo '<div class="narrow">';
  35. echo '<p>'.__('Howdy! This importer allows you to extract posts from an 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>';
  36. wp_import_upload_form("admin.php?import=rss&amp;step=1");
  37. echo '</div>';
  38. }
  39. function _normalize_tag( $matches ) {
  40. return '<' . strtolower( $matches[1] );
  41. }
  42. function get_posts() {
  43. global $wpdb;
  44. set_magic_quotes_runtime(0);
  45. $datalines = file($this->file); // Read the file into an array
  46. $importdata = implode('', $datalines); // squish it
  47. $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
  48. preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
  49. $this->posts = $this->posts[1];
  50. $index = 0;
  51. foreach ($this->posts as $post) {
  52. preg_match('|<title>(.*?)</title>|is', $post, $post_title);
  53. $post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) ));
  54. preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt);
  55. if ($post_date_gmt) {
  56. $post_date_gmt = strtotime($post_date_gmt[1]);
  57. } else {
  58. // if we don't already have something from pubDate
  59. preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt);
  60. $post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
  61. $post_date_gmt = str_replace('T', ' ', $post_date_gmt);
  62. $post_date_gmt = strtotime($post_date_gmt);
  63. }
  64. $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
  65. $post_date = get_date_from_gmt( $post_date_gmt );
  66. preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
  67. $categories = $categories[1];
  68. if (!$categories) {
  69. preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
  70. $categories = $categories[1];
  71. }
  72. $cat_index = 0;
  73. foreach ($categories as $category) {
  74. $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
  75. $cat_index++;
  76. }
  77. preg_match('|<guid.*?>(.*?)</guid>|is', $post, $guid);
  78. if ($guid)
  79. $guid = $wpdb->escape(trim($guid[1]));
  80. else
  81. $guid = '';
  82. preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
  83. $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
  84. if (!$post_content) {
  85. // This is for feeds that put content in description
  86. preg_match('|<description>(.*?)</description>|is', $post, $post_content);
  87. $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1])));
  88. }
  89. // Clean up content
  90. $post_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content);
  91. $post_content = str_replace('<br>', '<br />', $post_content);
  92. $post_content = str_replace('<hr>', '<hr />', $post_content);
  93. $post_author = 1;
  94. $post_status = 'publish';
  95. $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
  96. $index++;
  97. }
  98. }
  99. function import_posts() {
  100. echo '<ol>';
  101. foreach ($this->posts as $post) {
  102. echo "<li>".__('Importing post...');
  103. extract($post);
  104. if ($post_id = post_exists($post_title, $post_content, $post_date)) {
  105. _e('Post already imported');
  106. } else {
  107. $post_id = wp_insert_post($post);
  108. if ( is_wp_error( $post_id ) )
  109. return $post_id;
  110. if (!$post_id) {
  111. _e('Couldn&#8217;t get post ID');
  112. return;
  113. }
  114. if (0 != count($categories))
  115. wp_create_categories($categories, $post_id);
  116. _e('Done !');
  117. }
  118. echo '</li>';
  119. }
  120. echo '</ol>';
  121. }
  122. function import() {
  123. $file = wp_import_handle_upload();
  124. if ( isset($file['error']) ) {
  125. echo $file['error'];
  126. return;
  127. }
  128. $this->file = $file['file'];
  129. $this->get_posts();
  130. $result = $this->import_posts();
  131. if ( is_wp_error( $result ) )
  132. return $result;
  133. wp_import_cleanup($file['id']);
  134. do_action('import_done', 'rss');
  135. echo '<h3>';
  136. printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
  137. echo '</h3>';
  138. }
  139. function dispatch() {
  140. if (empty ($_GET['step']))
  141. $step = 0;
  142. else
  143. $step = (int) $_GET['step'];
  144. $this->header();
  145. switch ($step) {
  146. case 0 :
  147. $this->greet();
  148. break;
  149. case 1 :
  150. check_admin_referer('import-upload');
  151. $result = $this->import();
  152. if ( is_wp_error( $result ) )
  153. echo $result->get_error_message();
  154. break;
  155. }
  156. $this->footer();
  157. }
  158. function RSS_Import() {
  159. // Nothing.
  160. }
  161. }
  162. $rss_import = new RSS_Import();
  163. register_importer('rss', __('RSS'), __('Import posts from an RSS feed.'), array ($rss_import, 'dispatch'));
  164. ?>