2007-05-25 09:16:21 +02:00
< ? php
2008-10-02 03:03:26 +02:00
/**
* WordPress Administration Importer API .
*
* @ package WordPress
* @ subpackage Administration
*/
2007-05-25 09:16:21 +02:00
2008-10-02 03:03:26 +02:00
/**
2008-10-10 20:21:16 +02:00
* Retrieve list of importers .
2008-10-02 03:03:26 +02:00
*
2008-10-10 20:21:16 +02:00
* @ since 2.0 . 0
2008-10-02 03:03:26 +02:00
*
2015-05-28 23:40:27 +02:00
* @ global array $wp_importers
2008-10-10 20:21:16 +02:00
* @ return array
2008-10-02 03:03:26 +02:00
*/
2007-05-25 09:16:21 +02:00
function get_importers () {
global $wp_importers ;
2014-03-03 17:20:12 +01:00
if ( is_array ( $wp_importers ) ) {
2014-03-03 18:08:26 +01:00
uasort ( $wp_importers , '_usort_by_first_member' );
2014-03-03 17:20:12 +01:00
}
2007-05-25 09:16:21 +02:00
return $wp_importers ;
}
2014-03-03 17:20:12 +01:00
/**
* Sorts a multidimensional array by first member of each top level member
*
* Used by uasort () as a callback , should not be used directly .
*
* @ since 2.9 . 0
* @ access private
*
* @ param array $a
* @ param array $b
* @ return int
*/
2014-03-03 18:08:26 +01:00
function _usort_by_first_member ( $a , $b ) {
2014-03-03 17:20:12 +01:00
return strnatcasecmp ( $a [ 0 ], $b [ 0 ] );
}
2008-10-02 03:03:26 +02:00
/**
2008-10-10 20:21:16 +02:00
* Register importer for WordPress .
2008-10-02 03:03:26 +02:00
*
2008-10-10 20:21:16 +02:00
* @ since 2.0 . 0
2008-10-02 03:03:26 +02:00
*
2015-05-28 23:40:27 +02:00
* @ global array $wp_importers
*
2015-05-29 23:16:59 +02:00
* @ param string $id Importer tag . Used to uniquely identify importer .
* @ param string $name Importer name and title .
* @ param string $description Importer description .
2015-09-26 01:57:46 +02:00
* @ param callable $callback Callback to run .
2008-10-10 20:21:16 +02:00
* @ return WP_Error Returns WP_Error when $callback is WP_Error .
2008-10-02 03:03:26 +02:00
*/
2007-05-25 09:16:21 +02:00
function register_importer ( $id , $name , $description , $callback ) {
global $wp_importers ;
2017-12-01 00:09:33 +01:00
if ( is_wp_error ( $callback ) ) {
2007-09-18 18:32:22 +02:00
return $callback ;
2017-12-01 00:09:33 +01:00
}
$wp_importers [ $id ] = array ( $name , $description , $callback );
2007-05-25 09:16:21 +02:00
}
2008-10-02 03:03:26 +02:00
/**
2008-10-10 20:21:16 +02:00
* Cleanup importer .
2008-10-02 03:03:26 +02:00
*
2008-10-10 20:21:16 +02:00
* Removes attachment based on ID .
2008-10-02 03:03:26 +02:00
*
2008-10-10 20:21:16 +02:00
* @ since 2.0 . 0
*
* @ param string $id Importer ID .
2008-10-02 03:03:26 +02:00
*/
2007-05-25 09:16:21 +02:00
function wp_import_cleanup ( $id ) {
wp_delete_attachment ( $id );
}
2008-10-02 03:03:26 +02:00
/**
2008-10-10 20:21:16 +02:00
* Handle importer uploading and add attachment .
2008-10-02 03:03:26 +02:00
*
2008-10-10 20:21:16 +02:00
* @ since 2.0 . 0
2008-10-02 03:03:26 +02:00
*
2010-11-29 15:40:43 +01:00
* @ return array Uploaded file ' s details on success , error message on failure
2008-10-02 03:03:26 +02:00
*/
2007-05-25 09:16:21 +02:00
function wp_import_handle_upload () {
2014-12-20 22:09:11 +01:00
if ( ! isset ( $_FILES [ 'import' ] ) ) {
return array (
2017-12-01 00:09:33 +01:00
'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.' ),
2014-12-20 22:09:11 +01:00
);
2009-09-23 09:06:58 +02:00
}
2009-09-24 19:19:13 +02:00
2017-12-01 00:09:33 +01:00
$overrides = array (
'test_form' => false ,
'test_type' => false ,
);
2009-02-20 22:39:20 +01:00
$_FILES [ 'import' ][ 'name' ] .= '.txt' ;
2017-12-01 00:09:33 +01:00
$upload = wp_handle_upload ( $_FILES [ 'import' ], $overrides );
2007-05-25 09:16:21 +02:00
2014-12-20 22:09:11 +01:00
if ( isset ( $upload [ 'error' ] ) ) {
return $upload ;
}
2007-05-25 09:16:21 +02:00
// Construct the object array
2014-12-20 22:09:11 +01:00
$object = array (
2017-12-01 00:09:33 +01:00
'post_title' => basename ( $upload [ 'file' ] ),
'post_content' => $upload [ 'url' ],
2014-12-20 22:09:11 +01:00
'post_mime_type' => $upload [ 'type' ],
2017-12-01 00:09:33 +01:00
'guid' => $upload [ 'url' ],
'context' => 'import' ,
'post_status' => 'private' ,
2007-05-25 09:16:21 +02:00
);
// Save the data
2014-12-20 22:09:11 +01:00
$id = wp_insert_attachment ( $object , $upload [ 'file' ] );
2007-05-25 09:16:21 +02:00
2014-07-17 11:13:53 +02:00
/*
* Schedule a cleanup for one day from now in case of failed
* import or missing wp_import_cleanup () call .
*/
2012-09-25 07:26:19 +02:00
wp_schedule_single_event ( time () + DAY_IN_SECONDS , 'importer_scheduled_cleanup' , array ( $id ) );
2011-05-23 01:25:28 +02:00
2017-12-01 00:09:33 +01:00
return array (
'file' => $upload [ 'file' ],
'id' => $id ,
);
2007-05-25 09:16:21 +02:00
}
2012-11-17 08:20:04 +01:00
/**
* Returns a list from WordPress . org of popular importer plugins .
*
* @ since 3.5 . 0
*
* @ return array Importers with metadata for each .
*/
function wp_get_popular_importers () {
2014-05-18 22:51:44 +02:00
include ( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
2012-11-17 08:20:04 +01:00
2017-12-01 00:09:33 +01:00
$locale = get_user_locale ();
$cache_key = 'popular_importers_' . md5 ( $locale . $wp_version );
2016-07-17 17:31:29 +02:00
$popular_importers = get_site_transient ( $cache_key );
2012-11-17 08:20:04 +01:00
if ( ! $popular_importers ) {
2017-12-01 00:09:33 +01:00
$url = add_query_arg (
array (
'locale' => $locale ,
'version' => $wp_version ,
), 'http://api.wordpress.org/core/importers/1.1/'
);
2017-09-27 09:59:14 +02:00
$options = array ( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url ( '/' ) );
if ( wp_http_supports ( array ( 'ssl' ) ) ) {
$url = set_url_scheme ( $url , 'https' );
}
2017-12-01 00:09:33 +01:00
$response = wp_remote_get ( $url , $options );
2013-09-14 21:30:44 +02:00
$popular_importers = json_decode ( wp_remote_retrieve_body ( $response ), true );
2012-11-17 08:20:04 +01:00
2016-07-17 17:31:29 +02:00
if ( is_array ( $popular_importers ) ) {
set_site_transient ( $cache_key , $popular_importers , 2 * DAY_IN_SECONDS );
} else {
2012-11-17 08:20:04 +01:00
$popular_importers = false ;
2016-07-17 17:31:29 +02:00
}
2012-11-17 08:20:04 +01:00
}
if ( is_array ( $popular_importers ) ) {
// If the data was received as translated, return it as-is.
2017-12-01 00:09:33 +01:00
if ( $popular_importers [ 'translated' ] ) {
2012-11-17 08:20:04 +01:00
return $popular_importers [ 'importers' ];
2017-12-01 00:09:33 +01:00
}
2012-11-17 08:20:04 +01:00
foreach ( $popular_importers [ 'importers' ] as & $importer ) {
$importer [ 'description' ] = translate ( $importer [ 'description' ] );
2017-12-01 00:09:33 +01:00
if ( $importer [ 'name' ] != 'WordPress' ) {
2012-11-17 08:20:04 +01:00
$importer [ 'name' ] = translate ( $importer [ 'name' ] );
2017-12-01 00:09:33 +01:00
}
2012-11-17 08:20:04 +01:00
}
return $popular_importers [ 'importers' ];
}
return array (
// slug => name, description, plugin slug, and register_importer() slug
2017-12-01 00:09:33 +01:00
'blogger' => array (
'name' => __ ( 'Blogger' ),
2016-07-17 17:31:29 +02:00
'description' => __ ( 'Import posts, comments, and users from a Blogger blog.' ),
2012-11-17 08:20:04 +01:00
'plugin-slug' => 'blogger-importer' ,
'importer-id' => 'blogger' ,
),
2017-12-01 00:09:33 +01:00
'wpcat2tag' => array (
'name' => __ ( 'Categories and Tags Converter' ),
2016-07-17 17:31:29 +02:00
'description' => __ ( 'Convert existing categories to tags or tags to categories, selectively.' ),
2012-11-17 08:20:04 +01:00
'plugin-slug' => 'wpcat2tag-importer' ,
'importer-id' => 'wp-cat2tag' ,
),
'livejournal' => array (
2017-12-01 00:09:33 +01:00
'name' => __ ( 'LiveJournal' ),
2016-07-17 17:31:29 +02:00
'description' => __ ( 'Import posts from LiveJournal using their API.' ),
2012-11-17 08:20:04 +01:00
'plugin-slug' => 'livejournal-importer' ,
'importer-id' => 'livejournal' ,
),
'movabletype' => array (
2017-12-01 00:09:33 +01:00
'name' => __ ( 'Movable Type and TypePad' ),
2016-07-17 17:31:29 +02:00
'description' => __ ( 'Import posts and comments from a Movable Type or TypePad blog.' ),
2012-11-17 08:20:04 +01:00
'plugin-slug' => 'movabletype-importer' ,
'importer-id' => 'mt' ,
),
2017-12-01 00:09:33 +01:00
'opml' => array (
'name' => __ ( 'Blogroll' ),
2016-07-17 17:31:29 +02:00
'description' => __ ( 'Import links in OPML format.' ),
2012-11-17 08:20:04 +01:00
'plugin-slug' => 'opml-importer' ,
'importer-id' => 'opml' ,
),
2017-12-01 00:09:33 +01:00
'rss' => array (
'name' => __ ( 'RSS' ),
2016-07-17 17:31:29 +02:00
'description' => __ ( 'Import posts from an RSS feed.' ),
2012-11-17 08:20:04 +01:00
'plugin-slug' => 'rss-importer' ,
'importer-id' => 'rss' ,
),
2017-12-01 00:09:33 +01:00
'tumblr' => array (
'name' => __ ( 'Tumblr' ),
2016-07-17 17:31:29 +02:00
'description' => __ ( 'Import posts & media from Tumblr using their API.' ),
2012-11-17 08:20:04 +01:00
'plugin-slug' => 'tumblr-importer' ,
'importer-id' => 'tumblr' ,
),
2017-12-01 00:09:33 +01:00
'wordpress' => array (
'name' => 'WordPress' ,
2016-07-17 17:31:29 +02:00
'description' => __ ( 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
2012-11-17 08:20:04 +01:00
'plugin-slug' => 'wordpress-importer' ,
'importer-id' => 'wordpress' ,
),
);
}