PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/rss-importer/rss-importer.php

https://bitbucket.org/babinkochana/triptrills
PHP | 227 lines | 153 code | 43 blank | 31 comment | 20 complexity | 176112ff3b32677ba22f0fa82cef1191 MD5 | raw file
Possible License(s): MIT, Apache-2.0, GPL-3.0, 0BSD, GPL-2.0
  1. <?php
  2. /*
  3. Plugin Name: RSS Importer
  4. Plugin URI: http://wordpress.org/extend/plugins/rss-importer/
  5. Description: Import posts from an RSS feed.
  6. Author: wordpressdotorg
  7. Author URI: http://wordpress.org/
  8. Version: 0.2
  9. Stable tag: 0.2
  10. License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  11. Text Domain: rss-importer
  12. */
  13. if ( !defined('WP_LOAD_IMPORTERS') )
  14. return;
  15. // Load Importer API
  16. require_once ABSPATH . 'wp-admin/includes/import.php';
  17. if ( !class_exists( 'WP_Importer' ) ) {
  18. $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
  19. if ( file_exists( $class_wp_importer ) )
  20. require_once $class_wp_importer;
  21. }
  22. /**
  23. * RSS Importer
  24. *
  25. * @package WordPress
  26. * @subpackage Importer
  27. */
  28. /**
  29. * RSS Importer
  30. *
  31. * Will process a RSS feed for importing posts into WordPress. This is a very
  32. * limited importer and should only be used as the last resort, when no other
  33. * importer is available.
  34. *
  35. * @since unknown
  36. */
  37. if ( class_exists( 'WP_Importer' ) ) {
  38. class RSS_Import extends WP_Importer {
  39. var $posts = array ();
  40. var $file;
  41. function header() {
  42. echo '<div class="wrap">';
  43. screen_icon();
  44. echo '<h2>'.__('Import RSS', 'rss-importer').'</h2>';
  45. }
  46. function footer() {
  47. echo '</div>';
  48. }
  49. function greet() {
  50. echo '<div class="narrow">';
  51. echo '<p>'.__('Howdy! This importer allows you to extract posts from an RSS 2.0 file into your WordPress site. 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.', 'rss-importer').'</p>';
  52. wp_import_upload_form("admin.php?import=rss&amp;step=1");
  53. echo '</div>';
  54. }
  55. function _normalize_tag( $matches ) {
  56. return '<' . strtolower( $matches[1] );
  57. }
  58. function get_posts() {
  59. global $wpdb;
  60. set_magic_quotes_runtime(0);
  61. $datalines = file($this->file); // Read the file into an array
  62. $importdata = implode('', $datalines); // squish it
  63. $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
  64. preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
  65. $this->posts = $this->posts[1];
  66. $index = 0;
  67. foreach ($this->posts as $post) {
  68. preg_match('|<title>(.*?)</title>|is', $post, $post_title);
  69. $post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) ));
  70. preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt);
  71. if ($post_date_gmt) {
  72. $post_date_gmt = strtotime($post_date_gmt[1]);
  73. } else {
  74. // if we don't already have something from pubDate
  75. preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt);
  76. $post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
  77. $post_date_gmt = str_replace('T', ' ', $post_date_gmt);
  78. $post_date_gmt = strtotime($post_date_gmt);
  79. }
  80. $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
  81. $post_date = get_date_from_gmt( $post_date_gmt );
  82. preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
  83. $categories = $categories[1];
  84. if (!$categories) {
  85. preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
  86. $categories = $categories[1];
  87. }
  88. $cat_index = 0;
  89. foreach ($categories as $category) {
  90. $categories[$cat_index] = $wpdb->escape( html_entity_decode( $category ) );
  91. $cat_index++;
  92. }
  93. preg_match('|<guid.*?>(.*?)</guid>|is', $post, $guid);
  94. if ($guid)
  95. $guid = $wpdb->escape(trim($guid[1]));
  96. else
  97. $guid = '';
  98. preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
  99. $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
  100. if (!$post_content) {
  101. // This is for feeds that put content in description
  102. preg_match('|<description>(.*?)</description>|is', $post, $post_content);
  103. $post_content = $wpdb->escape( html_entity_decode( trim( $post_content[1] ) ) );
  104. }
  105. // Clean up content
  106. $post_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content);
  107. $post_content = str_replace('<br>', '<br />', $post_content);
  108. $post_content = str_replace('<hr>', '<hr />', $post_content);
  109. $post_author = 1;
  110. $post_status = 'publish';
  111. $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
  112. $index++;
  113. }
  114. }
  115. function import_posts() {
  116. echo '<ol>';
  117. foreach ($this->posts as $post) {
  118. echo "<li>".__('Importing post...', 'rss-importer');
  119. extract($post);
  120. if ($post_id = post_exists($post_title, $post_content, $post_date)) {
  121. _e('Post already imported', 'rss-importer');
  122. } else {
  123. $post_id = wp_insert_post($post);
  124. if ( is_wp_error( $post_id ) )
  125. return $post_id;
  126. if (!$post_id) {
  127. _e('Couldn&#8217;t get post ID', 'rss-importer');
  128. return;
  129. }
  130. if (0 != count($categories))
  131. wp_create_categories($categories, $post_id);
  132. _e('Done!', 'rss-importer');
  133. }
  134. echo '</li>';
  135. }
  136. echo '</ol>';
  137. }
  138. function import() {
  139. $file = wp_import_handle_upload();
  140. if ( isset($file['error']) ) {
  141. echo $file['error'];
  142. return;
  143. }
  144. $this->file = $file['file'];
  145. $this->get_posts();
  146. $result = $this->import_posts();
  147. if ( is_wp_error( $result ) )
  148. return $result;
  149. wp_import_cleanup($file['id']);
  150. do_action('import_done', 'rss');
  151. echo '<h3>';
  152. printf(__('All done. <a href="%s">Have fun!</a>', 'rss-importer'), get_option('home'));
  153. echo '</h3>';
  154. }
  155. function dispatch() {
  156. if (empty ($_GET['step']))
  157. $step = 0;
  158. else
  159. $step = (int) $_GET['step'];
  160. $this->header();
  161. switch ($step) {
  162. case 0 :
  163. $this->greet();
  164. break;
  165. case 1 :
  166. check_admin_referer('import-upload');
  167. $result = $this->import();
  168. if ( is_wp_error( $result ) )
  169. echo $result->get_error_message();
  170. break;
  171. }
  172. $this->footer();
  173. }
  174. function RSS_Import() {
  175. // Nothing.
  176. }
  177. }
  178. $rss_import = new RSS_Import();
  179. register_importer('rss', __('RSS', 'rss-importer'), __('Import posts from an RSS feed.', 'rss-importer'), array ($rss_import, 'dispatch'));
  180. } // class_exists( 'WP_Importer' )
  181. function rss_importer_init() {
  182. load_plugin_textdomain( 'rss-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
  183. }
  184. add_action( 'init', 'rss_importer_init' );