author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
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; |
|
9 | 56 |
if ( is_wp_error( $callback ) ) { |
0 | 57 |
return $callback; |
9 | 58 |
} |
59 |
$wp_importers[ $id ] = array( $name, $description, $callback ); |
|
0 | 60 |
} |
61 |
||
62 |
/** |
|
63 |
* Cleanup importer. |
|
64 |
* |
|
65 |
* Removes attachment based on ID. |
|
66 |
* |
|
67 |
* @since 2.0.0 |
|
68 |
* |
|
69 |
* @param string $id Importer ID. |
|
70 |
*/ |
|
71 |
function wp_import_cleanup( $id ) { |
|
72 |
wp_delete_attachment( $id ); |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* Handle importer uploading and add attachment. |
|
77 |
* |
|
78 |
* @since 2.0.0 |
|
79 |
* |
|
80 |
* @return array Uploaded file's details on success, error message on failure |
|
81 |
*/ |
|
82 |
function wp_import_handle_upload() { |
|
5 | 83 |
if ( ! isset( $_FILES['import'] ) ) { |
84 |
return array( |
|
16 | 85 |
'error' => sprintf( |
86 |
/* translators: 1: php.ini, 2: post_max_size, 3: upload_max_filesize */ |
|
87 |
__( '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.' ), |
|
88 |
'php.ini', |
|
89 |
'post_max_size', |
|
90 |
'upload_max_filesize' |
|
91 |
), |
|
5 | 92 |
); |
0 | 93 |
} |
94 |
||
9 | 95 |
$overrides = array( |
96 |
'test_form' => false, |
|
97 |
'test_type' => false, |
|
98 |
); |
|
0 | 99 |
$_FILES['import']['name'] .= '.txt'; |
9 | 100 |
$upload = wp_handle_upload( $_FILES['import'], $overrides ); |
0 | 101 |
|
5 | 102 |
if ( isset( $upload['error'] ) ) { |
103 |
return $upload; |
|
104 |
} |
|
0 | 105 |
|
16 | 106 |
// Construct the object array. |
5 | 107 |
$object = array( |
9 | 108 |
'post_title' => wp_basename( $upload['file'] ), |
109 |
'post_content' => $upload['url'], |
|
5 | 110 |
'post_mime_type' => $upload['type'], |
9 | 111 |
'guid' => $upload['url'], |
112 |
'context' => 'import', |
|
113 |
'post_status' => 'private', |
|
0 | 114 |
); |
115 |
||
16 | 116 |
// Save the data. |
5 | 117 |
$id = wp_insert_attachment( $object, $upload['file'] ); |
0 | 118 |
|
5 | 119 |
/* |
120 |
* Schedule a cleanup for one day from now in case of failed |
|
121 |
* import or missing wp_import_cleanup() call. |
|
122 |
*/ |
|
0 | 123 |
wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) ); |
124 |
||
9 | 125 |
return array( |
126 |
'file' => $upload['file'], |
|
127 |
'id' => $id, |
|
128 |
); |
|
0 | 129 |
} |
130 |
||
131 |
/** |
|
132 |
* Returns a list from WordPress.org of popular importer plugins. |
|
133 |
* |
|
134 |
* @since 3.5.0 |
|
135 |
* |
|
136 |
* @return array Importers with metadata for each. |
|
137 |
*/ |
|
138 |
function wp_get_popular_importers() { |
|
16 | 139 |
// Include an unmodified $wp_version. |
140 |
require ABSPATH . WPINC . '/version.php'; |
|
0 | 141 |
|
9 | 142 |
$locale = get_user_locale(); |
143 |
$cache_key = 'popular_importers_' . md5( $locale . $wp_version ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
$popular_importers = get_site_transient( $cache_key ); |
0 | 145 |
|
146 |
if ( ! $popular_importers ) { |
|
9 | 147 |
$url = add_query_arg( |
148 |
array( |
|
149 |
'locale' => $locale, |
|
150 |
'version' => $wp_version, |
|
151 |
), |
|
152 |
'http://api.wordpress.org/core/importers/1.1/' |
|
153 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
154 |
$options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
if ( wp_http_supports( array( 'ssl' ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
$url = set_url_scheme( $url, 'https' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
|
9 | 160 |
$response = wp_remote_get( $url, $options ); |
0 | 161 |
$popular_importers = json_decode( wp_remote_retrieve_body( $response ), true ); |
162 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
if ( is_array( $popular_importers ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
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
|
165 |
} else { |
0 | 166 |
$popular_importers = false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
} |
0 | 168 |
} |
169 |
||
170 |
if ( is_array( $popular_importers ) ) { |
|
171 |
// If the data was received as translated, return it as-is. |
|
9 | 172 |
if ( $popular_importers['translated'] ) { |
0 | 173 |
return $popular_importers['importers']; |
9 | 174 |
} |
0 | 175 |
|
176 |
foreach ( $popular_importers['importers'] as &$importer ) { |
|
9 | 177 |
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText |
0 | 178 |
$importer['description'] = translate( $importer['description'] ); |
16 | 179 |
if ( 'WordPress' !== $importer['name'] ) { |
9 | 180 |
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText |
0 | 181 |
$importer['name'] = translate( $importer['name'] ); |
9 | 182 |
} |
0 | 183 |
} |
184 |
return $popular_importers['importers']; |
|
185 |
} |
|
186 |
||
187 |
return array( |
|
16 | 188 |
// slug => name, description, plugin slug, and register_importer() slug. |
9 | 189 |
'blogger' => array( |
190 |
'name' => __( 'Blogger' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
'description' => __( 'Import posts, comments, and users from a Blogger blog.' ), |
0 | 192 |
'plugin-slug' => 'blogger-importer', |
193 |
'importer-id' => 'blogger', |
|
194 |
), |
|
9 | 195 |
'wpcat2tag' => array( |
196 |
'name' => __( 'Categories and Tags Converter' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
'description' => __( 'Convert existing categories to tags or tags to categories, selectively.' ), |
0 | 198 |
'plugin-slug' => 'wpcat2tag-importer', |
199 |
'importer-id' => 'wp-cat2tag', |
|
200 |
), |
|
201 |
'livejournal' => array( |
|
9 | 202 |
'name' => __( 'LiveJournal' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
'description' => __( 'Import posts from LiveJournal using their API.' ), |
0 | 204 |
'plugin-slug' => 'livejournal-importer', |
205 |
'importer-id' => 'livejournal', |
|
206 |
), |
|
207 |
'movabletype' => array( |
|
9 | 208 |
'name' => __( 'Movable Type and TypePad' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
'description' => __( 'Import posts and comments from a Movable Type or TypePad blog.' ), |
0 | 210 |
'plugin-slug' => 'movabletype-importer', |
211 |
'importer-id' => 'mt', |
|
212 |
), |
|
9 | 213 |
'rss' => array( |
214 |
'name' => __( 'RSS' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
215 |
'description' => __( 'Import posts from an RSS feed.' ), |
0 | 216 |
'plugin-slug' => 'rss-importer', |
217 |
'importer-id' => 'rss', |
|
218 |
), |
|
9 | 219 |
'tumblr' => array( |
220 |
'name' => __( 'Tumblr' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
'description' => __( 'Import posts & media from Tumblr using their API.' ), |
0 | 222 |
'plugin-slug' => 'tumblr-importer', |
223 |
'importer-id' => 'tumblr', |
|
224 |
), |
|
9 | 225 |
'wordpress' => array( |
226 |
'name' => 'WordPress', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
'description' => __( 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ), |
0 | 228 |
'plugin-slug' => 'wordpress-importer', |
229 |
'importer-id' => 'wordpress', |
|
230 |
), |
|
231 |
); |
|
232 |
} |