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