[ Root ] [ Search ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

title

Body

[close]

/wp-admin/import/ -> rss.php (source)

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


Generated: Thu May 20 22:30:08 2010 Cross-referenced by PHPXref 0.7