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
*
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 ;
2007-12-19 18:48:51 +01:00
if ( is_array ( $wp_importers ) )
uasort ( $wp_importers , create_function ( '$a, $b' , 'return strcmp($a[0], $b[0]);' ));
2007-05-25 09:16:21 +02:00
return $wp_importers ;
}
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
*
2008-10-10 20:21:16 +02:00
* @ param string $id Importer tag . Used to uniquely identify importer .
* @ param string $name Importer name and title .
* @ param string $description Importer description .
* @ param callback $callback Callback to run .
* @ 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 ;
2007-09-18 18:32:22 +02:00
if ( is_wp_error ( $callback ) )
return $callback ;
2007-05-25 09:16:21 +02:00
$wp_importers [ $id ] = array ( $name , $description , $callback );
}
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 () {
2009-09-23 09:06:58 +02:00
if ( ! isset ( $_FILES [ 'import' ]) ) {
2009-09-24 19:19:13 +02:00
$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.' );
2009-09-23 09:06:58 +02:00
return $file ;
}
2009-09-24 19:19:13 +02:00
2007-05-25 09:16:21 +02:00
$overrides = array ( 'test_form' => false , 'test_type' => false );
2009-02-20 22:39:20 +01:00
$_FILES [ 'import' ][ 'name' ] .= '.txt' ;
2007-05-25 09:16:21 +02:00
$file = wp_handle_upload ( $_FILES [ 'import' ], $overrides );
if ( isset ( $file [ 'error' ] ) )
return $file ;
$url = $file [ 'url' ];
$type = $file [ 'type' ];
2010-11-29 15:40:43 +01:00
$file = $file [ 'file' ];
2007-05-25 09:16:21 +02:00
$filename = basename ( $file );
// Construct the object array
$object = array ( 'post_title' => $filename ,
'post_content' => $url ,
'post_mime_type' => $type ,
2011-05-23 01:25:28 +02:00
'guid' => $url ,
'context' => 'import' ,
'post_status' => 'private'
2007-05-25 09:16:21 +02:00
);
// Save the data
$id = wp_insert_attachment ( $object , $file );
2011-05-23 01:25:28 +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
2007-05-25 09:16:21 +02:00
return array ( 'file' => $file , 'id' => $id );
}