3 * WordPress Administration Importer API.
6 * @subpackage Administration
10 * Retrieve list of importers.
16 function get_importers() {
18 if ( is_array( $wp_importers ) ) {
19 uasort( $wp_importers, '_usort_by_first_member' );
25 * Sorts a multidimensional array by first member of each top level member
27 * Used by uasort() as a callback, should not be used directly.
36 function _usort_by_first_member( $a, $b ) {
37 return strnatcasecmp( $a[0], $b[0] );
41 * Register importer for WordPress.
45 * @param string $id Importer tag. Used to uniquely identify importer.
46 * @param string $name Importer name and title.
47 * @param string $description Importer description.
48 * @param callback $callback Callback to run.
49 * @return WP_Error Returns WP_Error when $callback is WP_Error.
51 function register_importer( $id, $name, $description, $callback ) {
53 if ( is_wp_error( $callback ) )
55 $wp_importers[$id] = array ( $name, $description, $callback );
61 * Removes attachment based on ID.
65 * @param string $id Importer ID.
67 function wp_import_cleanup( $id ) {
68 wp_delete_attachment( $id );
72 * Handle importer uploading and add attachment.
76 * @return array Uploaded file's details on success, error message on failure
78 function wp_import_handle_upload() {
79 if ( !isset($_FILES['import']) ) {
80 $file['error'] = __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.' );
84 $overrides = array( 'test_form' => false, 'test_type' => false );
85 $_FILES['import']['name'] .= '.txt';
86 $file = wp_handle_upload( $_FILES['import'], $overrides );
88 if ( isset( $file['error'] ) )
92 $type = $file['type'];
93 $file = $file['file'];
94 $filename = basename( $file );
96 // Construct the object array
97 $object = array( 'post_title' => $filename,
98 'post_content' => $url,
99 'post_mime_type' => $type,
101 'context' => 'import',
102 'post_status' => 'private'
106 $id = wp_insert_attachment( $object, $file );
108 // schedule a cleanup for one day from now in case of failed import or missing wp_import_cleanup() call
109 wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
111 return array( 'file' => $file, 'id' => $id );
115 * Returns a list from WordPress.org of popular importer plugins.
119 * @return array Importers with metadata for each.
121 function wp_get_popular_importers() {
122 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
124 $locale = get_locale();
125 $popular_importers = get_site_transient( 'popular_importers_' . $locale );
127 if ( ! $popular_importers ) {
128 $url = add_query_arg( 'locale', get_locale(), 'http://api.wordpress.org/core/importers/1.1/' );
129 $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() );
130 $response = wp_remote_get( $url, $options );
131 $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
133 if ( is_array( $popular_importers ) )
134 set_site_transient( 'popular_importers_' . $locale, $popular_importers, 2 * DAY_IN_SECONDS );
136 $popular_importers = false;
139 if ( is_array( $popular_importers ) ) {
140 // If the data was received as translated, return it as-is.
141 if ( $popular_importers['translated'] )
142 return $popular_importers['importers'];
144 foreach ( $popular_importers['importers'] as &$importer ) {
145 $importer['description'] = translate( $importer['description'] );
146 if ( $importer['name'] != 'WordPress' )
147 $importer['name'] = translate( $importer['name'] );
149 return $popular_importers['importers'];
153 // slug => name, description, plugin slug, and register_importer() slug
155 'name' => __( 'Blogger' ),
156 'description' => __( 'Install the Blogger importer to import posts, comments, and users from a Blogger blog.' ),
157 'plugin-slug' => 'blogger-importer',
158 'importer-id' => 'blogger',
160 'wpcat2tag' => array(
161 'name' => __( 'Categories and Tags Converter' ),
162 'description' => __( 'Install the category/tag converter to convert existing categories to tags or tags to categories, selectively.' ),
163 'plugin-slug' => 'wpcat2tag-importer',
164 'importer-id' => 'wp-cat2tag',
166 'livejournal' => array(
167 'name' => __( 'LiveJournal' ),
168 'description' => __( 'Install the LiveJournal importer to import posts from LiveJournal using their API.' ),
169 'plugin-slug' => 'livejournal-importer',
170 'importer-id' => 'livejournal',
172 'movabletype' => array(
173 'name' => __( 'Movable Type and TypePad' ),
174 'description' => __( 'Install the Movable Type importer to import posts and comments from a Movable Type or TypePad blog.' ),
175 'plugin-slug' => 'movabletype-importer',
176 'importer-id' => 'mt',
179 'name' => __( 'Blogroll' ),
180 'description' => __( 'Install the blogroll importer to import links in OPML format.' ),
181 'plugin-slug' => 'opml-importer',
182 'importer-id' => 'opml',
185 'name' => __( 'RSS' ),
186 'description' => __( 'Install the RSS importer to import posts from an RSS feed.' ),
187 'plugin-slug' => 'rss-importer',
188 'importer-id' => 'rss',
191 'name' => __( 'Tumblr' ),
192 'description' => __( 'Install the Tumblr importer to import posts & media from Tumblr using their API.' ),
193 'plugin-slug' => 'tumblr-importer',
194 'importer-id' => 'tumblr',
196 'wordpress' => array(
197 'name' => 'WordPress',
198 'description' => __( 'Install the WordPress importer to import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
199 'plugin-slug' => 'wordpress-importer',
200 'importer-id' => 'wordpress',