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