[ Index ] |
|
Code source de WordPress 2.1.2 |
1 <?php 2 3 class RSS_Import { 4 5 var $posts = array (); 6 var $file; 7 8 function header() { 9 echo '<div class="wrap">'; 10 echo '<h2>'.__('Import RSS').'</h2>'; 11 } 12 13 function footer() { 14 echo '</div>'; 15 } 16 17 function unhtmlentities($string) { // From php.net for < 4.3 compat 18 $trans_tbl = get_html_translation_table(HTML_ENTITIES); 19 $trans_tbl = array_flip($trans_tbl); 20 return strtr($string, $trans_tbl); 21 } 22 23 function greet() { 24 echo '<div class="narrow">'; 25 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>'; 26 wp_import_upload_form("admin.php?import=rss&step=1"); 27 echo '</div>'; 28 } 29 30 function get_posts() { 31 global $wpdb; 32 33 set_magic_quotes_runtime(0); 34 $datalines = file($this->file); // Read the file into an array 35 $importdata = implode('', $datalines); // squish it 36 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata); 37 38 preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts); 39 $this->posts = $this->posts[1]; 40 $index = 0; 41 foreach ($this->posts as $post) { 42 preg_match('|<title>(.*?)</title>|is', $post, $post_title); 43 $post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) )); 44 45 preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt); 46 47 if ($post_date_gmt) { 48 $post_date_gmt = strtotime($post_date_gmt[1]); 49 } else { 50 // if we don't already have something from pubDate 51 preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt); 52 $post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]); 53 $post_date_gmt = str_replace('T', ' ', $post_date_gmt); 54 $post_date_gmt = strtotime($post_date_gmt); 55 } 56 57 $post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt); 58 $post_date = get_date_from_gmt( $post_date_gmt ); 59 60 preg_match_all('|<category>(.*?)</category>|is', $post, $categories); 61 $categories = $categories[1]; 62 63 if (!$categories) { 64 preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories); 65 $categories = $categories[1]; 66 } 67 68 $cat_index = 0; 69 foreach ($categories as $category) { 70 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category)); 71 $cat_index++; 72 } 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, $post_content); 81 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1]))); 82 83 if (!$post_content) { 84 // This is for feeds that put content in description 85 preg_match('|<description>(.*?)</description>|is', $post, $post_content); 86 $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1]))); 87 } 88 89 // Clean up content 90 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); 91 $post_content = str_replace('<br>', '<br />', $post_content); 92 $post_content = str_replace('<hr>', '<hr />', $post_content); 93 94 $post_author = 1; 95 $post_status = 'publish'; 96 $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories'); 97 $index++; 98 } 99 } 100 101 function import_posts() { 102 echo '<ol>'; 103 104 foreach ($this->posts as $post) { 105 echo "<li>".__('Importing post...'); 106 107 extract($post); 108 109 if ($post_id = post_exists($post_title, $post_content, $post_date)) { 110 _e('Post already imported'); 111 } else { 112 $post_id = wp_insert_post($post); 113 if (!$post_id) { 114 _e("Couldn't get post ID"); 115 return; 116 } 117 118 if (0 != count($categories)) 119 wp_create_categories($categories, $post_id); 120 _e('Done !'); 121 } 122 echo '</li>'; 123 } 124 125 echo '</ol>'; 126 127 } 128 129 function import() { 130 $file = wp_import_handle_upload(); 131 if ( isset($file['error']) ) { 132 echo $file['error']; 133 return; 134 } 135 136 $this->file = $file['file']; 137 $this->get_posts(); 138 $this->import_posts(); 139 wp_import_cleanup($file['id']); 140 141 echo '<h3>'; 142 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')); 143 echo '</h3>'; 144 } 145 146 function dispatch() { 147 if (empty ($_GET['step'])) 148 $step = 0; 149 else 150 $step = (int) $_GET['step']; 151 152 $this->header(); 153 154 switch ($step) { 155 case 0 : 156 $this->greet(); 157 break; 158 case 1 : 159 $this->import(); 160 break; 161 } 162 163 $this->footer(); 164 } 165 166 function RSS_Import() { 167 // Nothing. 168 } 169 } 170 171 $rss_import = new RSS_Import(); 172 173 register_importer('rss', __('RSS'), __('Import posts from an RSS feed'), array ($rss_import, 'dispatch')); 174 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 30 19:41:27 2007 | par Balluche grâce à PHPXref 0.7 |