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'] ) ) {
81 '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.' )
85 $overrides = array( 'test_form' => false, 'test_type' => false );
86 $_FILES['import']['name'] .= '.txt';
87 $upload = wp_handle_upload( $_FILES['import'], $overrides );
89 if ( isset( $upload['error'] ) ) {
93 // Construct the object array
95 'post_title' => basename( $upload['file'] ),
96 'post_content' => $upload['url'],
97 'post_mime_type' => $upload['type'],
98 'guid' => $upload['url'],
99 'context' => 'import',
100 'post_status' => 'private'
104 $id = wp_insert_attachment( $object, $upload['file'] );
107 * Schedule a cleanup for one day from now in case of failed
108 * import or missing wp_import_cleanup() call.
110 wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
112 return array( 'file' => $upload['file'], 'id' => $id );
116 * Returns a list from WordPress.org of popular importer plugins.
120 * @return array Importers with metadata for each.
122 function wp_get_popular_importers() {
123 include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
125 $locale = get_locale();
126 $popular_importers = get_site_transient( 'popular_importers_' . $locale );
128 if ( ! $popular_importers ) {
129 $url = add_query_arg( 'locale', get_locale(), 'http://api.wordpress.org/core/importers/1.1/' );
130 $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() );
131 $response = wp_remote_get( $url, $options );
132 $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
134 if ( is_array( $popular_importers ) )
135 set_site_transient( 'popular_importers_' . $locale, $popular_importers, 2 * DAY_IN_SECONDS );
137 $popular_importers = false;
140 if ( is_array( $popular_importers ) ) {
141 // If the data was received as translated, return it as-is.
142 if ( $popular_importers['translated'] )
143 return $popular_importers['importers'];
145 foreach ( $popular_importers['importers'] as &$importer ) {
146 $importer['description'] = translate( $importer['description'] );
147 if ( $importer['name'] != 'WordPress' )
148 $importer['name'] = translate( $importer['name'] );
150 return $popular_importers['importers'];
154 // slug => name, description, plugin slug, and register_importer() slug
156 'name' => __( 'Blogger' ),
157 'description' => __( 'Install the Blogger importer to import posts, comments, and users from a Blogger blog.' ),
158 'plugin-slug' => 'blogger-importer',
159 'importer-id' => 'blogger',
161 'wpcat2tag' => array(
162 'name' => __( 'Categories and Tags Converter' ),
163 'description' => __( 'Install the category/tag converter to convert existing categories to tags or tags to categories, selectively.' ),
164 'plugin-slug' => 'wpcat2tag-importer',
165 'importer-id' => 'wp-cat2tag',
167 'livejournal' => array(
168 'name' => __( 'LiveJournal' ),
169 'description' => __( 'Install the LiveJournal importer to import posts from LiveJournal using their API.' ),
170 'plugin-slug' => 'livejournal-importer',
171 'importer-id' => 'livejournal',
173 'movabletype' => array(
174 'name' => __( 'Movable Type and TypePad' ),
175 'description' => __( 'Install the Movable Type importer to import posts and comments from a Movable Type or TypePad blog.' ),
176 'plugin-slug' => 'movabletype-importer',
177 'importer-id' => 'mt',
180 'name' => __( 'Blogroll' ),
181 'description' => __( 'Install the blogroll importer to import links in OPML format.' ),
182 'plugin-slug' => 'opml-importer',
183 'importer-id' => 'opml',
186 'name' => __( 'RSS' ),
187 'description' => __( 'Install the RSS importer to import posts from an RSS feed.' ),
188 'plugin-slug' => 'rss-importer',
189 'importer-id' => 'rss',
192 'name' => __( 'Tumblr' ),
193 'description' => __( 'Install the Tumblr importer to import posts & media from Tumblr using their API.' ),
194 'plugin-slug' => 'tumblr-importer',
195 'importer-id' => 'tumblr',
197 'wordpress' => array(
198 'name' => 'WordPress',
199 'description' => __( 'Install the WordPress importer to import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
200 'plugin-slug' => 'wordpress-importer',
201 'importer-id' => 'wordpress',