0
|
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; |
5
|
18 |
if ( is_array( $wp_importers ) ) { |
|
19 |
uasort( $wp_importers, '_usort_by_first_member' ); |
|
20 |
} |
0
|
21 |
return $wp_importers; |
|
22 |
} |
|
23 |
|
|
24 |
/** |
5
|
25 |
* Sorts a multidimensional array by first member of each top level member |
|
26 |
* |
|
27 |
* Used by uasort() as a callback, should not be used directly. |
|
28 |
* |
|
29 |
* @since 2.9.0 |
|
30 |
* @access private |
|
31 |
* |
|
32 |
* @param array $a |
|
33 |
* @param array $b |
|
34 |
* @return int |
|
35 |
*/ |
|
36 |
function _usort_by_first_member( $a, $b ) { |
|
37 |
return strnatcasecmp( $a[0], $b[0] ); |
|
38 |
} |
|
39 |
|
|
40 |
/** |
0
|
41 |
* Register importer for WordPress. |
|
42 |
* |
|
43 |
* @since 2.0.0 |
|
44 |
* |
|
45 |
* @param string $id Importer tag. Used to uniquely identify importer. |
|
46 |
* @param string $name Importer name and title. |
|
47 |
* @param string $description Importer description. |
|
48 |
* @param callback $callback Callback to run. |
|
49 |
* @return WP_Error Returns WP_Error when $callback is WP_Error. |
|
50 |
*/ |
|
51 |
function register_importer( $id, $name, $description, $callback ) { |
|
52 |
global $wp_importers; |
|
53 |
if ( is_wp_error( $callback ) ) |
|
54 |
return $callback; |
|
55 |
$wp_importers[$id] = array ( $name, $description, $callback ); |
|
56 |
} |
|
57 |
|
|
58 |
/** |
|
59 |
* Cleanup importer. |
|
60 |
* |
|
61 |
* Removes attachment based on ID. |
|
62 |
* |
|
63 |
* @since 2.0.0 |
|
64 |
* |
|
65 |
* @param string $id Importer ID. |
|
66 |
*/ |
|
67 |
function wp_import_cleanup( $id ) { |
|
68 |
wp_delete_attachment( $id ); |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* Handle importer uploading and add attachment. |
|
73 |
* |
|
74 |
* @since 2.0.0 |
|
75 |
* |
|
76 |
* @return array Uploaded file's details on success, error message on failure |
|
77 |
*/ |
|
78 |
function wp_import_handle_upload() { |
5
|
79 |
if ( ! isset( $_FILES['import'] ) ) { |
|
80 |
return array( |
|
81 |
'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.' ) |
|
82 |
); |
0
|
83 |
} |
|
84 |
|
|
85 |
$overrides = array( 'test_form' => false, 'test_type' => false ); |
|
86 |
$_FILES['import']['name'] .= '.txt'; |
5
|
87 |
$upload = wp_handle_upload( $_FILES['import'], $overrides ); |
0
|
88 |
|
5
|
89 |
if ( isset( $upload['error'] ) ) { |
|
90 |
return $upload; |
|
91 |
} |
0
|
92 |
|
|
93 |
// Construct the object array |
5
|
94 |
$object = array( |
|
95 |
'post_title' => basename( $upload['file'] ), |
|
96 |
'post_content' => $upload['url'], |
|
97 |
'post_mime_type' => $upload['type'], |
|
98 |
'guid' => $upload['url'], |
0
|
99 |
'context' => 'import', |
|
100 |
'post_status' => 'private' |
|
101 |
); |
|
102 |
|
|
103 |
// Save the data |
5
|
104 |
$id = wp_insert_attachment( $object, $upload['file'] ); |
0
|
105 |
|
5
|
106 |
/* |
|
107 |
* Schedule a cleanup for one day from now in case of failed |
|
108 |
* import or missing wp_import_cleanup() call. |
|
109 |
*/ |
0
|
110 |
wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) ); |
|
111 |
|
5
|
112 |
return array( 'file' => $upload['file'], 'id' => $id ); |
0
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* Returns a list from WordPress.org of popular importer plugins. |
|
117 |
* |
|
118 |
* @since 3.5.0 |
|
119 |
* |
|
120 |
* @return array Importers with metadata for each. |
|
121 |
*/ |
|
122 |
function wp_get_popular_importers() { |
5
|
123 |
include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version |
0
|
124 |
|
|
125 |
$locale = get_locale(); |
|
126 |
$popular_importers = get_site_transient( 'popular_importers_' . $locale ); |
|
127 |
|
|
128 |
if ( ! $popular_importers ) { |
|
129 |
$url = add_query_arg( 'locale', get_locale(), 'http://api.wordpress.org/core/importers/1.1/' ); |
|
130 |
$options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() ); |
|
131 |
$response = wp_remote_get( $url, $options ); |
|
132 |
$popular_importers = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
133 |
|
|
134 |
if ( is_array( $popular_importers ) ) |
|
135 |
set_site_transient( 'popular_importers_' . $locale, $popular_importers, 2 * DAY_IN_SECONDS ); |
|
136 |
else |
|
137 |
$popular_importers = false; |
|
138 |
} |
|
139 |
|
|
140 |
if ( is_array( $popular_importers ) ) { |
|
141 |
// If the data was received as translated, return it as-is. |
|
142 |
if ( $popular_importers['translated'] ) |
|
143 |
return $popular_importers['importers']; |
|
144 |
|
|
145 |
foreach ( $popular_importers['importers'] as &$importer ) { |
|
146 |
$importer['description'] = translate( $importer['description'] ); |
|
147 |
if ( $importer['name'] != 'WordPress' ) |
|
148 |
$importer['name'] = translate( $importer['name'] ); |
|
149 |
} |
|
150 |
return $popular_importers['importers']; |
|
151 |
} |
|
152 |
|
|
153 |
return array( |
|
154 |
// slug => name, description, plugin slug, and register_importer() slug |
|
155 |
'blogger' => array( |
|
156 |
'name' => __( 'Blogger' ), |
|
157 |
'description' => __( 'Install the Blogger importer to import posts, comments, and users from a Blogger blog.' ), |
|
158 |
'plugin-slug' => 'blogger-importer', |
|
159 |
'importer-id' => 'blogger', |
|
160 |
), |
|
161 |
'wpcat2tag' => array( |
|
162 |
'name' => __( 'Categories and Tags Converter' ), |
|
163 |
'description' => __( 'Install the category/tag converter to convert existing categories to tags or tags to categories, selectively.' ), |
|
164 |
'plugin-slug' => 'wpcat2tag-importer', |
|
165 |
'importer-id' => 'wp-cat2tag', |
|
166 |
), |
|
167 |
'livejournal' => array( |
|
168 |
'name' => __( 'LiveJournal' ), |
|
169 |
'description' => __( 'Install the LiveJournal importer to import posts from LiveJournal using their API.' ), |
|
170 |
'plugin-slug' => 'livejournal-importer', |
|
171 |
'importer-id' => 'livejournal', |
|
172 |
), |
|
173 |
'movabletype' => array( |
|
174 |
'name' => __( 'Movable Type and TypePad' ), |
|
175 |
'description' => __( 'Install the Movable Type importer to import posts and comments from a Movable Type or TypePad blog.' ), |
|
176 |
'plugin-slug' => 'movabletype-importer', |
|
177 |
'importer-id' => 'mt', |
|
178 |
), |
|
179 |
'opml' => array( |
|
180 |
'name' => __( 'Blogroll' ), |
|
181 |
'description' => __( 'Install the blogroll importer to import links in OPML format.' ), |
|
182 |
'plugin-slug' => 'opml-importer', |
|
183 |
'importer-id' => 'opml', |
|
184 |
), |
|
185 |
'rss' => array( |
|
186 |
'name' => __( 'RSS' ), |
|
187 |
'description' => __( 'Install the RSS importer to import posts from an RSS feed.' ), |
|
188 |
'plugin-slug' => 'rss-importer', |
|
189 |
'importer-id' => 'rss', |
|
190 |
), |
|
191 |
'tumblr' => array( |
|
192 |
'name' => __( 'Tumblr' ), |
|
193 |
'description' => __( 'Install the Tumblr importer to import posts & media from Tumblr using their API.' ), |
|
194 |
'plugin-slug' => 'tumblr-importer', |
|
195 |
'importer-id' => 'tumblr', |
|
196 |
), |
|
197 |
'wordpress' => array( |
|
198 |
'name' => 'WordPress', |
|
199 |
'description' => __( 'Install the WordPress importer to import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ), |
|
200 |
'plugin-slug' => 'wordpress-importer', |
|
201 |
'importer-id' => 'wordpress', |
|
202 |
), |
|
203 |
); |
|
204 |
} |