From a90b0d297de01ddeb75b6ad4fa428700886b621f Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 15 Nov 2005 23:39:32 +0000 Subject: [PATCH] Import data uploading. wp_import_handle_upload(), wp_import_cleanup(), wp_import_upload_form(). git-svn-id: http://svn.automattic.com/wordpress/trunk@3093 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-functions.php | 50 ++++++++++++++++++++++++++++++++++++ wp-admin/import/rss.php | 34 ++++++++++++------------ 2 files changed, 67 insertions(+), 17 deletions(-) diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index d6356de3af..0f23f76be4 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -1730,4 +1730,54 @@ function wp_shrink_dimensions($width, $height, $wmax = 128, $hmax = 96) { return array((int) ($width / $height * $hmax), $hmax); } +function wp_import_cleanup($file) { + wp_delete_attachment($file['id']); +} + +function wp_import_upload_form($action) { +?> + +
+ + +
+ + +
+
+ false, 'test_type' => false); + $file = wp_handle_upload($_FILES['import'], $overrides); + + if ( isset($file['error']) ) + return $file; + + $url = $file['url']; + $file = $file['file']; + $filename = basename($file); + + // Construct the object array + $object = array( + 'post_title' => $filename, + 'post_content' => $url, + 'post_mime_type' => 'import', + 'guid' => $url + ); + + // Save the data + $id = wp_insert_attachment($object, $file); + + return array('file' => $file, 'id' => $id); +} + ?> \ No newline at end of file diff --git a/wp-admin/import/rss.php b/wp-admin/import/rss.php index 0a647075b8..796148c414 100644 --- a/wp-admin/import/rss.php +++ b/wp-admin/import/rss.php @@ -7,6 +7,7 @@ define('RSSFILE', 'rss.xml'); class RSS_Import { var $posts = array (); + var $file; function header() { echo '
'; @@ -24,20 +25,15 @@ class RSS_Import { } function greet() { - _e("

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. To get started you must edit the following line in this file (import/rss.php)

-

define('RSSFILE', '');

-

You want to define where the RSS file we'll be working with is, for example:

-

define('RSSFILE', 'rss.xml');

-

You have to do this manually for security reasons. When you're done reload this page and we'll take you to the next step.

"); - if ('' != RSSFILE) - echo '' . __('Begin RSS Import »') . ''; + _e("

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.

"); + wp_import_upload_form("admin.php?import=rss&step=1"); } function get_posts() { global $wpdb; set_magic_quotes_runtime(0); - $datalines = file(RSSFILE); // Read the file into an array + $datalines = file($this->file); // Read the file into an array $importdata = implode('', $datalines); // squish it $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata); @@ -115,9 +111,11 @@ class RSS_Import { echo __('Post already imported'); } else { $post_id = wp_insert_post($post); - if (!$post_id) - die(__("Couldn't get post ID")); - + if (!$post_id) { + echo(__("Couldn't get post ID")); + return; + } + if (0 != count($categories)) wp_create_categories($categories, $post_id); echo __('Done !'); @@ -130,15 +128,17 @@ class RSS_Import { } function import() { - // FIXME: Don't die - if ('' == RSSFILE) - die("You must edit the RSSFILE line as described on the previous page to continue."); - - if (!file_exists(RSSFILE)) - die("The file you specified does not seem to exist. Please check the path you've given."); + $file = wp_import_handle_upload(); + if ( isset($file['error']) ) { + echo $file['error']; + return; + } + $this->file = $file['file']; $this->get_posts(); $this->import_posts(); + wp_import_cleanup($file); + echo '

All done. Have fun!

'; }