author | Anthony Ly <anthonyly.com@gmail.com> |
Mon, 19 Nov 2012 18:26:13 +0100 | |
changeset 194 | 32102edaa81b |
parent 136 | bde1974c263b |
child 204 | 09a1c134465b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Administration Importer API. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Retrieve list of importers. |
|
11 |
* |
|
12 |
* @since 2.0.0 |
|
13 |
* |
|
14 |
* @return array |
|
15 |
*/ |
|
16 |
function get_importers() { |
|
17 |
global $wp_importers; |
|
18 |
if ( is_array($wp_importers) ) |
|
19 |
uasort($wp_importers, create_function('$a, $b', 'return strcmp($a[0], $b[0]);')); |
|
20 |
return $wp_importers; |
|
21 |
} |
|
22 |
||
23 |
/** |
|
24 |
* Register importer for WordPress. |
|
25 |
* |
|
26 |
* @since 2.0.0 |
|
27 |
* |
|
28 |
* @param string $id Importer tag. Used to uniquely identify importer. |
|
29 |
* @param string $name Importer name and title. |
|
30 |
* @param string $description Importer description. |
|
31 |
* @param callback $callback Callback to run. |
|
32 |
* @return WP_Error Returns WP_Error when $callback is WP_Error. |
|
33 |
*/ |
|
34 |
function register_importer( $id, $name, $description, $callback ) { |
|
35 |
global $wp_importers; |
|
36 |
if ( is_wp_error( $callback ) ) |
|
37 |
return $callback; |
|
38 |
$wp_importers[$id] = array ( $name, $description, $callback ); |
|
39 |
} |
|
40 |
||
41 |
/** |
|
42 |
* Cleanup importer. |
|
43 |
* |
|
44 |
* Removes attachment based on ID. |
|
45 |
* |
|
46 |
* @since 2.0.0 |
|
47 |
* |
|
48 |
* @param string $id Importer ID. |
|
49 |
*/ |
|
50 |
function wp_import_cleanup( $id ) { |
|
51 |
wp_delete_attachment( $id ); |
|
52 |
} |
|
53 |
||
54 |
/** |
|
55 |
* Handle importer uploading and add attachment. |
|
56 |
* |
|
57 |
* @since 2.0.0 |
|
58 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
59 |
* @return array Uploaded file's details on success, error message on failure |
136 | 60 |
*/ |
61 |
function wp_import_handle_upload() { |
|
62 |
if ( !isset($_FILES['import']) ) { |
|
63 |
$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.' ); |
|
64 |
return $file; |
|
65 |
} |
|
66 |
||
67 |
$overrides = array( 'test_form' => false, 'test_type' => false ); |
|
68 |
$_FILES['import']['name'] .= '.txt'; |
|
69 |
$file = wp_handle_upload( $_FILES['import'], $overrides ); |
|
70 |
||
71 |
if ( isset( $file['error'] ) ) |
|
72 |
return $file; |
|
73 |
||
74 |
$url = $file['url']; |
|
75 |
$type = $file['type']; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
76 |
$file = $file['file']; |
136 | 77 |
$filename = basename( $file ); |
78 |
||
79 |
// Construct the object array |
|
80 |
$object = array( 'post_title' => $filename, |
|
81 |
'post_content' => $url, |
|
82 |
'post_mime_type' => $type, |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
83 |
'guid' => $url, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
84 |
'context' => 'import', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
85 |
'post_status' => 'private' |
136 | 86 |
); |
87 |
||
88 |
// Save the data |
|
89 |
$id = wp_insert_attachment( $object, $file ); |
|
90 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
91 |
// schedule a cleanup for one day from now in case of failed import or missing wp_import_cleanup() call |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
92 |
wp_schedule_single_event( time() + 86400, 'importer_scheduled_cleanup', array( $id ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
93 |
|
136 | 94 |
return array( 'file' => $file, 'id' => $id ); |
95 |
} |