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 );
109
* Schedule a cleanup for one day from now in case of failed
110
* import or missing wp_import_cleanup() call.
112
wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
114
return array( 'file' => $file, 'id' => $id );
118
* Returns a list from WordPress.org of popular importer plugins.
122
* @return array Importers with metadata for each.
124
function wp_get_popular_importers() {
125
include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
127
$locale = get_locale();
128
$popular_importers = get_site_transient( 'popular_importers_' . $locale );
130
if ( ! $popular_importers ) {
131
$url = add_query_arg( 'locale', get_locale(), 'http://api.wordpress.org/core/importers/1.1/' );
132
$options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() );
133
$response = wp_remote_get( $url, $options );
134
$popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
136
if ( is_array( $popular_importers ) )
137
set_site_transient( 'popular_importers_' . $locale, $popular_importers, 2 * DAY_IN_SECONDS );
139
$popular_importers = false;
142
if ( is_array( $popular_importers ) ) {
143
// If the data was received as translated, return it as-is.
144
if ( $popular_importers['translated'] )
145
return $popular_importers['importers'];
147
foreach ( $popular_importers['importers'] as &$importer ) {
148
$importer['description'] = translate( $importer['description'] );
149
if ( $importer['name'] != 'WordPress' )
150
$importer['name'] = translate( $importer['name'] );
152
return $popular_importers['importers'];
156
// slug => name, description, plugin slug, and register_importer() slug
158
'name' => __( 'Blogger' ),
159
'description' => __( 'Install the Blogger importer to import posts, comments, and users from a Blogger blog.' ),
160
'plugin-slug' => 'blogger-importer',
161
'importer-id' => 'blogger',
163
'wpcat2tag' => array(
164
'name' => __( 'Categories and Tags Converter' ),
165
'description' => __( 'Install the category/tag converter to convert existing categories to tags or tags to categories, selectively.' ),
166
'plugin-slug' => 'wpcat2tag-importer',
167
'importer-id' => 'wp-cat2tag',
169
'livejournal' => array(
170
'name' => __( 'LiveJournal' ),
171
'description' => __( 'Install the LiveJournal importer to import posts from LiveJournal using their API.' ),
172
'plugin-slug' => 'livejournal-importer',
173
'importer-id' => 'livejournal',
175
'movabletype' => array(
176
'name' => __( 'Movable Type and TypePad' ),
177
'description' => __( 'Install the Movable Type importer to import posts and comments from a Movable Type or TypePad blog.' ),
178
'plugin-slug' => 'movabletype-importer',
179
'importer-id' => 'mt',
182
'name' => __( 'Blogroll' ),
183
'description' => __( 'Install the blogroll importer to import links in OPML format.' ),
184
'plugin-slug' => 'opml-importer',
185
'importer-id' => 'opml',
188
'name' => __( 'RSS' ),
189
'description' => __( 'Install the RSS importer to import posts from an RSS feed.' ),
190
'plugin-slug' => 'rss-importer',
191
'importer-id' => 'rss',
194
'name' => __( 'Tumblr' ),
195
'description' => __( 'Install the Tumblr importer to import posts & media from Tumblr using their API.' ),
196
'plugin-slug' => 'tumblr-importer',
197
'importer-id' => 'tumblr',
199
'wordpress' => array(
200
'name' => 'WordPress',
201
'description' => __( 'Install the WordPress importer to import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
202
'plugin-slug' => 'wordpress-importer',
203
'importer-id' => 'wordpress',