1<?php
2/**
3 * WordPress Administration Importer API.
4 *
5 * @package WordPress
6 * @subpackage Administration
7 */
8
9/**
10 * Retrieves the list of importers.
11 *
12 * @since 2.0.0
13 *
14 * @global array $wp_importers
15 * @return array
16 */
17function get_importers() {
18 global $wp_importers;
19 if ( is_array( $wp_importers ) ) {
20 uasort( $wp_importers, '_usort_by_first_member' );
21 }
22 return $wp_importers;
23}
24
25/**
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 */
37function _usort_by_first_member( $a, $b ) {
38 return strnatcasecmp( $a[0], $b[0] );
39}
40
41/**
42 * Registers importer for WordPress.
43 *
44 * @since 2.0.0
45 *
46 * @global array $wp_importers
47 *
48 * @param string $id Importer tag. Used to uniquely identify importer.
49 * @param string $name Importer name and title.
50 * @param string $description Importer description.
51 * @param callable $callback Callback to run.
52 * @return void|WP_Error Void on success. WP_Error when $callback is WP_Error.
53 */
54function register_importer( $id, $name, $description, $callback ) {
55 global $wp_importers;
56 if ( is_wp_error( $callback ) ) {
57 return $callback;
58 }
59 $wp_importers[ $id ] = array( $name, $description, $callback );
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 */
71function wp_import_cleanup( $id ) {
72 wp_delete_attachment( $id );
73}
74
75/**
76 * Handles importer uploading and adds attachment.
77 *
78 * @since 2.0.0
79 *
80 * @return array Uploaded file's details on success, error message on failure.
81 */
82function wp_import_handle_upload() {
83 if ( ! isset( $_FILES['import'] ) ) {
84 return array(
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 ),
92 );
93 }
94
95 $overrides = array(
96 'test_form' => false,
97 'test_type' => false,
98 );
99 $_FILES['import']['name'] .= '.txt';
100 $upload = wp_handle_upload( $_FILES['import'], $overrides );
101
102 if ( isset( $upload['error'] ) ) {
103 return $upload;
104 }
105
106 // Construct the attachment array.
107 $attachment = array(
108 'post_title' => wp_basename( $upload['file'] ),
109 'post_content' => $upload['url'],
110 'post_mime_type' => $upload['type'],
111 'guid' => $upload['url'],
112 'context' => 'import',
113 'post_status' => 'private',
114 );
115
116 // Save the data.
117 $id = wp_insert_attachment( $attachment, $upload['file'] );
118
119 /*
120 * Schedule a cleanup for one day from now in case of failed
121 * import or missing wp_import_cleanup() call.
122 */
123 wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
124
125 return array(
126 'file' => $upload['file'],
127 'id' => $id,
128 );
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 */
138function wp_get_popular_importers() {
139 $locale = get_user_locale();
140 $cache_key = 'popular_importers_' . md5( $locale . wp_get_wp_version() );
141 $popular_importers = get_site_transient( $cache_key );
142
143 if ( ! $popular_importers ) {
144 $url = add_query_arg(
145 array(
146 'locale' => $locale,
147 'version' => wp_get_wp_version(),
148 ),
149 'http://api.wordpress.org/core/importers/1.1/'
150 );
151 $options = array( 'user-agent' => 'WordPress/' . wp_get_wp_version() . '; ' . home_url( '/' ) );
152
153 if ( wp_http_supports( array( 'ssl' ) ) ) {
154 $url = set_url_scheme( $url, 'https' );
155 }
156
157 $response = wp_remote_get( $url, $options );
158 $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
159
160 if ( is_array( $popular_importers ) ) {
161 set_site_transient( $cache_key, $popular_importers, 2 * DAY_IN_SECONDS );
162 } else {
163 $popular_importers = false;
164 }
165 }
166
167 if ( is_array( $popular_importers ) ) {
168 // If the data was received as translated, return it as-is.
169 if ( $popular_importers['translated'] ) {
170 return $popular_importers['importers'];
171 }
172
173 foreach ( $popular_importers['importers'] as &$importer ) {
174 // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
175 $importer['description'] = translate( $importer['description'] );
176 if ( 'WordPress' !== $importer['name'] ) {
177 // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
178 $importer['name'] = translate( $importer['name'] );
179 }
180 }
181 return $popular_importers['importers'];
182 }
183
184 return array(
185 // slug => name, description, plugin slug, and register_importer() slug.
186 'blogger' => array(
187 'name' => __( 'Blogger' ),
188 'description' => __( 'Import posts, comments, and users from a Blogger blog.' ),
189 'plugin-slug' => 'blogger-importer',
190 'importer-id' => 'blogger',
191 ),
192 'wpcat2tag' => array(
193 'name' => __( 'Categories and Tags Converter' ),
194 'description' => __( 'Convert existing categories to tags or tags to categories, selectively.' ),
195 'plugin-slug' => 'wpcat2tag-importer',
196 'importer-id' => 'wp-cat2tag',
197 ),
198 'livejournal' => array(
199 'name' => __( 'LiveJournal' ),
200 'description' => __( 'Import posts from LiveJournal using their API.' ),
201 'plugin-slug' => 'livejournal-importer',
202 'importer-id' => 'livejournal',
203 ),
204 'movabletype' => array(
205 'name' => __( 'Movable Type and TypePad' ),
206 'description' => __( 'Import posts and comments from a Movable Type or TypePad blog.' ),
207 'plugin-slug' => 'movabletype-importer',
208 'importer-id' => 'mt',
209 ),
210 'rss' => array(
211 'name' => __( 'RSS' ),
212 'description' => __( 'Import posts from an RSS feed.' ),
213 'plugin-slug' => 'rss-importer',
214 'importer-id' => 'rss',
215 ),
216 'tumblr' => array(
217 'name' => __( 'Tumblr' ),
218 'description' => __( 'Import posts & media from Tumblr using their API.' ),
219 'plugin-slug' => 'tumblr-importer',
220 'importer-id' => 'tumblr',
221 ),
222 'wordpress' => array(
223 'name' => 'WordPress',
224 'description' => __( 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
225 'plugin-slug' => 'wordpress-importer',
226 'importer-id' => 'wordpress',
227 ),
228 );
229}
230