3 * WordPress Administration Importer API.
6 * @subpackage Administration
10 * Retrieve list of importers.
14 * @global array $wp_importers
17 function get_importers() {
19 if ( is_array( $wp_importers ) ) {
20 uasort( $wp_importers, '_usort_by_first_member' );
26 * Sorts a multidimensional array by first member of each top level member
28 * Used by uasort() as a callback, should not be used directly.
37 function _usort_by_first_member( $a, $b ) {
38 return strnatcasecmp( $a[0], $b[0] );
42 * Register importer for WordPress.
46 * @global array $wp_importers
48 * @param string $id Importer tag. Used to uniquely identify importer.
49 * @param string $name Importer name and title.
50 * @param string $description Importer description.
51 * @param callback $callback Callback to run.
52 * @return WP_Error Returns WP_Error when $callback is WP_Error.
54 function register_importer( $id, $name, $description, $callback ) {
56 if ( is_wp_error( $callback ) )
58 $wp_importers[$id] = array ( $name, $description, $callback );
64 * Removes attachment based on ID.
68 * @param string $id Importer ID.
70 function wp_import_cleanup( $id ) {
71 wp_delete_attachment( $id );
75 * Handle importer uploading and add attachment.
79 * @return array Uploaded file's details on success, error message on failure
81 function wp_import_handle_upload() {
82 if ( ! isset( $_FILES['import'] ) ) {
84 '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.' )
88 $overrides = array( 'test_form' => false, 'test_type' => false );
89 $_FILES['import']['name'] .= '.txt';
90 $upload = wp_handle_upload( $_FILES['import'], $overrides );
92 if ( isset( $upload['error'] ) ) {
96 // Construct the object array
98 'post_title' => basename( $upload['file'] ),
99 'post_content' => $upload['url'],
100 'post_mime_type' => $upload['type'],
101 'guid' => $upload['url'],
102 'context' => 'import',
103 'post_status' => 'private'
107 $id = wp_insert_attachment( $object, $upload['file'] );
110 * Schedule a cleanup for one day from now in case of failed
111 * import or missing wp_import_cleanup() call.
113 wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
115 return array( 'file' => $upload['file'], 'id' => $id );
119 * Returns a list from WordPress.org of popular importer plugins.
123 * @return array Importers with metadata for each.
125 function wp_get_popular_importers() {
126 include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
128 $locale = get_locale();
129 $popular_importers = get_site_transient( 'popular_importers_' . $locale );
131 if ( ! $popular_importers ) {
132 $url = add_query_arg( 'locale', get_locale(), 'http://api.wordpress.org/core/importers/1.1/' );
133 $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() );
134 $response = wp_remote_get( $url, $options );
135 $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
137 if ( is_array( $popular_importers ) )
138 set_site_transient( 'popular_importers_' . $locale, $popular_importers, 2 * DAY_IN_SECONDS );
140 $popular_importers = false;
143 if ( is_array( $popular_importers ) ) {
144 // If the data was received as translated, return it as-is.
145 if ( $popular_importers['translated'] )
146 return $popular_importers['importers'];
148 foreach ( $popular_importers['importers'] as &$importer ) {
149 $importer['description'] = translate( $importer['description'] );
150 if ( $importer['name'] != 'WordPress' )
151 $importer['name'] = translate( $importer['name'] );
153 return $popular_importers['importers'];
157 // slug => name, description, plugin slug, and register_importer() slug
159 'name' => __( 'Blogger' ),
160 'description' => __( 'Install the Blogger importer to import posts, comments, and users from a Blogger blog.' ),
161 'plugin-slug' => 'blogger-importer',
162 'importer-id' => 'blogger',
164 'wpcat2tag' => array(
165 'name' => __( 'Categories and Tags Converter' ),
166 'description' => __( 'Install the category/tag converter to convert existing categories to tags or tags to categories, selectively.' ),
167 'plugin-slug' => 'wpcat2tag-importer',
168 'importer-id' => 'wp-cat2tag',
170 'livejournal' => array(
171 'name' => __( 'LiveJournal' ),
172 'description' => __( 'Install the LiveJournal importer to import posts from LiveJournal using their API.' ),
173 'plugin-slug' => 'livejournal-importer',
174 'importer-id' => 'livejournal',
176 'movabletype' => array(
177 'name' => __( 'Movable Type and TypePad' ),
178 'description' => __( 'Install the Movable Type importer to import posts and comments from a Movable Type or TypePad blog.' ),
179 'plugin-slug' => 'movabletype-importer',
180 'importer-id' => 'mt',
183 'name' => __( 'Blogroll' ),
184 'description' => __( 'Install the blogroll importer to import links in OPML format.' ),
185 'plugin-slug' => 'opml-importer',
186 'importer-id' => 'opml',
189 'name' => __( 'RSS' ),
190 'description' => __( 'Install the RSS importer to import posts from an RSS feed.' ),
191 'plugin-slug' => 'rss-importer',
192 'importer-id' => 'rss',
195 'name' => __( 'Tumblr' ),
196 'description' => __( 'Install the Tumblr importer to import posts & media from Tumblr using their API.' ),
197 'plugin-slug' => 'tumblr-importer',
198 'importer-id' => 'tumblr',
200 'wordpress' => array(
201 'name' => 'WordPress',
202 'description' => __( 'Install the WordPress importer to import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
203 'plugin-slug' => 'wordpress-importer',
204 'importer-id' => 'wordpress',