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
/**
2022-07-29 11:22:10 +02:00
* Retrieves the 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:41:30 +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:21:16 +01:00
if ( is_array ( $wp_importers ) ) {
2014-03-03 18:09:14 +01:00
uasort ( $wp_importers , '_usort_by_first_member' );
2014-03-03 17:21:16 +01:00
}
2007-05-25 09:16:21 +02:00
return $wp_importers ;
}
2014-03-03 17:21:16 +01:00
/**
2022-07-29 11:22:10 +02:00
* Sorts a multidimensional array by first member of each top level member .
2014-03-03 17:21:16 +01:00
*
* 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:09:14 +01:00
function _usort_by_first_member ( $a , $b ) {
2014-03-03 17:21:16 +01:00
return strnatcasecmp ( $a [ 0 ], $b [ 0 ] );
}
2008-10-02 03:03:26 +02:00
/**
2022-07-29 11:22:10 +02:00
* Registers 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:41:30 +02:00
* @ global array $wp_importers
*
2015-05-29 23:17:27 +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:58:25 +02:00
* @ param callable $callback Callback to run .
2021-01-05 18:16:11 +01:00
* @ return void | WP_Error Void on success . 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:11:00 +01:00
if ( is_wp_error ( $callback ) ) {
2007-09-18 18:32:22 +02:00
return $callback ;
2017-12-01 00:11:00 +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
/**
2022-07-29 11:22:10 +02:00
* Handles importer uploading and adds 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
*
2022-07-29 11:22:10 +02: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:10:24 +01:00
if ( ! isset ( $_FILES [ 'import' ] ) ) {
return array (
2020-01-06 17:16:03 +01:00
'error' => sprintf (
/* translators: 1: php.ini, 2: post_max_size, 3: upload_max_filesize */
__ ( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your %1$s file or by %2$s being defined as smaller than %3$s in %1$s.' ),
'php.ini' ,
'post_max_size' ,
'upload_max_filesize'
),
2014-12-20 22:10:24 +01:00
);
2009-09-23 09:06:58 +02:00
}
2009-09-24 19:19:13 +02:00
2017-12-01 00:11:00 +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:11:00 +01:00
$upload = wp_handle_upload ( $_FILES [ 'import' ], $overrides );
2007-05-25 09:16:21 +02:00
2014-12-20 22:10:24 +01:00
if ( isset ( $upload [ 'error' ] ) ) {
return $upload ;
}
2007-05-25 09:16:21 +02:00
2022-04-14 17:15:12 +02:00
// Construct the attachment array.
$attachment = array (
2019-03-01 21:58:52 +01:00
'post_title' => wp_basename ( $upload [ 'file' ] ),
2017-12-01 00:11:00 +01:00
'post_content' => $upload [ 'url' ],
2014-12-20 22:10:24 +01:00
'post_mime_type' => $upload [ 'type' ],
2017-12-01 00:11:00 +01:00
'guid' => $upload [ 'url' ],
'context' => 'import' ,
'post_status' => 'private' ,
2007-05-25 09:16:21 +02:00
);
2020-01-18 01:54:04 +01:00
// Save the data.
2022-04-14 17:15:12 +02:00
$id = wp_insert_attachment ( $attachment , $upload [ 'file' ] );
2007-05-25 09:16:21 +02:00
2014-07-17 11:14:16 +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:11:00 +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 () {
2017-12-01 00:11:00 +01:00
$locale = get_user_locale ();
2024-07-27 02:27:16 +02:00
$cache_key = 'popular_importers_' . md5 ( $locale . wp_get_wp_version () );
2016-07-17 17:32:30 +02:00
$popular_importers = get_site_transient ( $cache_key );
2012-11-17 08:20:04 +01:00
if ( ! $popular_importers ) {
2017-12-01 00:11:00 +01:00
$url = add_query_arg (
array (
'locale' => $locale ,
2024-07-27 02:27:16 +02:00
'version' => wp_get_wp_version (),
2018-08-17 03:51:36 +02:00
),
'http://api.wordpress.org/core/importers/1.1/'
2017-12-01 00:11:00 +01:00
);
2024-07-27 02:27:16 +02:00
$options = array ( 'user-agent' => 'WordPress/' . wp_get_wp_version () . '; ' . home_url ( '/' ) );
2017-09-27 10:00:49 +02:00
if ( wp_http_supports ( array ( 'ssl' ) ) ) {
$url = set_url_scheme ( $url , 'https' );
}
2017-12-01 00:11:00 +01:00
$response = wp_remote_get ( $url , $options );
2013-09-14 21:31:08 +02:00
$popular_importers = json_decode ( wp_remote_retrieve_body ( $response ), true );
2012-11-17 08:20:04 +01:00
2016-07-17 17:32:30 +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:32:30 +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:11:00 +01:00
if ( $popular_importers [ 'translated' ] ) {
2012-11-17 08:20:04 +01:00
return $popular_importers [ 'importers' ];
2017-12-01 00:11:00 +01:00
}
2012-11-17 08:20:04 +01:00
foreach ( $popular_importers [ 'importers' ] as & $importer ) {
2019-01-11 07:04:49 +01:00
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
2012-11-17 08:20:04 +01:00
$importer [ 'description' ] = translate ( $importer [ 'description' ] );
2020-02-09 17:55:09 +01:00
if ( 'WordPress' !== $importer [ 'name' ] ) {
2019-01-11 07:04:49 +01:00
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
2012-11-17 08:20:04 +01:00
$importer [ 'name' ] = translate ( $importer [ 'name' ] );
2017-12-01 00:11:00 +01:00
}
2012-11-17 08:20:04 +01:00
}
return $popular_importers [ 'importers' ];
}
return array (
2020-01-18 01:54:04 +01:00
// slug => name, description, plugin slug, and register_importer() slug.
2017-12-01 00:11:00 +01:00
'blogger' => array (
'name' => __ ( 'Blogger' ),
2016-07-17 17:32:30 +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:11:00 +01:00
'wpcat2tag' => array (
'name' => __ ( 'Categories and Tags Converter' ),
2016-07-17 17:32:30 +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:11:00 +01:00
'name' => __ ( 'LiveJournal' ),
2016-07-17 17:32:30 +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:11:00 +01:00
'name' => __ ( 'Movable Type and TypePad' ),
2016-07-17 17:32:30 +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:11:00 +01:00
'rss' => array (
'name' => __ ( 'RSS' ),
2016-07-17 17:32:30 +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:11:00 +01:00
'tumblr' => array (
'name' => __ ( 'Tumblr' ),
2016-07-17 17:32:30 +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:11:00 +01:00
'wordpress' => array (
'name' => 'WordPress' ,
2016-07-17 17:32:30 +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' ,
),
);
}