|
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 * |
|
59 * @return array Uploaded file's details on success, error message on failure |
|
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']; |
|
76 $file = $file['file']; |
|
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, |
|
83 'guid' => $url, |
|
84 'context' => 'import', |
|
85 'post_status' => 'private' |
|
86 ); |
|
87 |
|
88 // Save the data |
|
89 $id = wp_insert_attachment( $object, $file ); |
|
90 |
|
91 // schedule a cleanup for one day from now in case of failed import or missing wp_import_cleanup() call |
|
92 wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) ); |
|
93 |
|
94 return array( 'file' => $file, 'id' => $id ); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Returns a list from WordPress.org of popular importer plugins. |
|
99 * |
|
100 * @since 3.5.0 |
|
101 * |
|
102 * @return array Importers with metadata for each. |
|
103 */ |
|
104 function wp_get_popular_importers() { |
|
105 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version |
|
106 |
|
107 $locale = get_locale(); |
|
108 $popular_importers = get_site_transient( 'popular_importers_' . $locale ); |
|
109 |
|
110 if ( ! $popular_importers ) { |
|
111 $url = add_query_arg( 'locale', get_locale(), 'http://api.wordpress.org/core/importers/1.1/' ); |
|
112 $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() ); |
|
113 $response = wp_remote_get( $url, $options ); |
|
114 $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
115 |
|
116 if ( is_array( $popular_importers ) ) |
|
117 set_site_transient( 'popular_importers_' . $locale, $popular_importers, 2 * DAY_IN_SECONDS ); |
|
118 else |
|
119 $popular_importers = false; |
|
120 } |
|
121 |
|
122 if ( is_array( $popular_importers ) ) { |
|
123 // If the data was received as translated, return it as-is. |
|
124 if ( $popular_importers['translated'] ) |
|
125 return $popular_importers['importers']; |
|
126 |
|
127 foreach ( $popular_importers['importers'] as &$importer ) { |
|
128 $importer['description'] = translate( $importer['description'] ); |
|
129 if ( $importer['name'] != 'WordPress' ) |
|
130 $importer['name'] = translate( $importer['name'] ); |
|
131 } |
|
132 return $popular_importers['importers']; |
|
133 } |
|
134 |
|
135 return array( |
|
136 // slug => name, description, plugin slug, and register_importer() slug |
|
137 'blogger' => array( |
|
138 'name' => __( 'Blogger' ), |
|
139 'description' => __( 'Install the Blogger importer to import posts, comments, and users from a Blogger blog.' ), |
|
140 'plugin-slug' => 'blogger-importer', |
|
141 'importer-id' => 'blogger', |
|
142 ), |
|
143 'wpcat2tag' => array( |
|
144 'name' => __( 'Categories and Tags Converter' ), |
|
145 'description' => __( 'Install the category/tag converter to convert existing categories to tags or tags to categories, selectively.' ), |
|
146 'plugin-slug' => 'wpcat2tag-importer', |
|
147 'importer-id' => 'wp-cat2tag', |
|
148 ), |
|
149 'livejournal' => array( |
|
150 'name' => __( 'LiveJournal' ), |
|
151 'description' => __( 'Install the LiveJournal importer to import posts from LiveJournal using their API.' ), |
|
152 'plugin-slug' => 'livejournal-importer', |
|
153 'importer-id' => 'livejournal', |
|
154 ), |
|
155 'movabletype' => array( |
|
156 'name' => __( 'Movable Type and TypePad' ), |
|
157 'description' => __( 'Install the Movable Type importer to import posts and comments from a Movable Type or TypePad blog.' ), |
|
158 'plugin-slug' => 'movabletype-importer', |
|
159 'importer-id' => 'mt', |
|
160 ), |
|
161 'opml' => array( |
|
162 'name' => __( 'Blogroll' ), |
|
163 'description' => __( 'Install the blogroll importer to import links in OPML format.' ), |
|
164 'plugin-slug' => 'opml-importer', |
|
165 'importer-id' => 'opml', |
|
166 ), |
|
167 'rss' => array( |
|
168 'name' => __( 'RSS' ), |
|
169 'description' => __( 'Install the RSS importer to import posts from an RSS feed.' ), |
|
170 'plugin-slug' => 'rss-importer', |
|
171 'importer-id' => 'rss', |
|
172 ), |
|
173 'tumblr' => array( |
|
174 'name' => __( 'Tumblr' ), |
|
175 'description' => __( 'Install the Tumblr importer to import posts & media from Tumblr using their API.' ), |
|
176 'plugin-slug' => 'tumblr-importer', |
|
177 'importer-id' => 'tumblr', |
|
178 ), |
|
179 'wordpress' => array( |
|
180 'name' => 'WordPress', |
|
181 'description' => __( 'Install the WordPress importer to import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ), |
|
182 'plugin-slug' => 'wordpress-importer', |
|
183 'importer-id' => 'wordpress', |
|
184 ), |
|
185 ); |
|
186 } |