import.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * WordPress Administration Importer API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Retrieves the list of importers.
  10. *
  11. * @since 2.0.0
  12. *
  13. * @global array $wp_importers
  14. * @return array
  15. */
  16. function get_importers() {
  17. global $wp_importers;
  18. if ( is_array( $wp_importers ) ) {
  19. uasort( $wp_importers, '_usort_by_first_member' );
  20. }
  21. return $wp_importers;
  22. }
  23. /**
  24. * Sorts a multidimensional array by first member of each top level member.
  25. *
  26. * Used by uasort() as a callback, should not be used directly.
  27. *
  28. * @since 2.9.0
  29. * @access private
  30. *
  31. * @param array $a
  32. * @param array $b
  33. * @return int
  34. */
  35. function _usort_by_first_member( $a, $b ) {
  36. return strnatcasecmp( $a[0], $b[0] );
  37. }
  38. /**
  39. * Registers importer for WordPress.
  40. *
  41. * @since 2.0.0
  42. *
  43. * @global array $wp_importers
  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 callable $callback Callback to run.
  49. * @return void|WP_Error Void on success. 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. }
  56. $wp_importers[ $id ] = array( $name, $description, $callback );
  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. * Handles importer uploading and adds attachment.
  72. *
  73. * @since 2.0.0
  74. *
  75. * @return array Uploaded file's details on success, error message on failure.
  76. */
  77. function wp_import_handle_upload() {
  78. if ( ! isset( $_FILES['import'] ) ) {
  79. return array(
  80. 'error' => sprintf(
  81. /* translators: 1: php.ini, 2: post_max_size, 3: upload_max_filesize */
  82. __( '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.' ),
  83. 'php.ini',
  84. 'post_max_size',
  85. 'upload_max_filesize'
  86. ),
  87. );
  88. }
  89. $overrides = array(
  90. 'test_form' => false,
  91. 'test_type' => false,
  92. );
  93. $_FILES['import']['name'] .= '.txt';
  94. $upload = wp_handle_upload( $_FILES['import'], $overrides );
  95. if ( isset( $upload['error'] ) ) {
  96. return $upload;
  97. }
  98. // Construct the attachment array.
  99. $attachment = array(
  100. 'post_title' => wp_basename( $upload['file'] ),
  101. 'post_content' => $upload['url'],
  102. 'post_mime_type' => $upload['type'],
  103. 'guid' => $upload['url'],
  104. 'context' => 'import',
  105. 'post_status' => 'private',
  106. );
  107. // Save the data.
  108. $id = wp_insert_attachment( $attachment, $upload['file'] );
  109. /*
  110. * Schedule a cleanup for one day from now in case of failed
  111. * import or missing wp_import_cleanup() call.
  112. */
  113. wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
  114. return array(
  115. 'file' => $upload['file'],
  116. 'id' => $id,
  117. );
  118. }
  119. /**
  120. * Returns a list from WordPress.org of popular importer plugins.
  121. *
  122. * @since 3.5.0
  123. *
  124. * @return array Importers with metadata for each.
  125. */
  126. function wp_get_popular_importers() {
  127. // Include an unmodified $wp_version.
  128. require ABSPATH . WPINC . '/version.php';
  129. $locale = get_user_locale();
  130. $cache_key = 'popular_importers_' . md5( $locale . $wp_version );
  131. $popular_importers = get_site_transient( $cache_key );
  132. if ( ! $popular_importers ) {
  133. $url = add_query_arg(
  134. array(
  135. 'locale' => $locale,
  136. 'version' => $wp_version,
  137. ),
  138. 'http://api.wordpress.org/core/importers/1.1/'
  139. );
  140. $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ) );
  141. if ( wp_http_supports( array( 'ssl' ) ) ) {
  142. $url = set_url_scheme( $url, 'https' );
  143. }
  144. $response = wp_remote_get( $url, $options );
  145. $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
  146. if ( is_array( $popular_importers ) ) {
  147. set_site_transient( $cache_key, $popular_importers, 2 * DAY_IN_SECONDS );
  148. } else {
  149. $popular_importers = false;
  150. }
  151. }
  152. if ( is_array( $popular_importers ) ) {
  153. // If the data was received as translated, return it as-is.
  154. if ( $popular_importers['translated'] ) {
  155. return $popular_importers['importers'];
  156. }
  157. foreach ( $popular_importers['importers'] as &$importer ) {
  158. // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
  159. $importer['description'] = translate( $importer['description'] );
  160. if ( 'WordPress' !== $importer['name'] ) {
  161. // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
  162. $importer['name'] = translate( $importer['name'] );
  163. }
  164. }
  165. return $popular_importers['importers'];
  166. }
  167. return array(
  168. // slug => name, description, plugin slug, and register_importer() slug.
  169. 'blogger' => array(
  170. 'name' => __( 'Blogger' ),
  171. 'description' => __( 'Import posts, comments, and users from a Blogger blog.' ),
  172. 'plugin-slug' => 'blogger-importer',
  173. 'importer-id' => 'blogger',
  174. ),
  175. 'wpcat2tag' => array(
  176. 'name' => __( 'Categories and Tags Converter' ),
  177. 'description' => __( 'Convert existing categories to tags or tags to categories, selectively.' ),
  178. 'plugin-slug' => 'wpcat2tag-importer',
  179. 'importer-id' => 'wp-cat2tag',
  180. ),
  181. 'livejournal' => array(
  182. 'name' => __( 'LiveJournal' ),
  183. 'description' => __( 'Import posts from LiveJournal using their API.' ),
  184. 'plugin-slug' => 'livejournal-importer',
  185. 'importer-id' => 'livejournal',
  186. ),
  187. 'movabletype' => array(
  188. 'name' => __( 'Movable Type and TypePad' ),
  189. 'description' => __( 'Import posts and comments from a Movable Type or TypePad blog.' ),
  190. 'plugin-slug' => 'movabletype-importer',
  191. 'importer-id' => 'mt',
  192. ),
  193. 'rss' => array(
  194. 'name' => __( 'RSS' ),
  195. 'description' => __( 'Import posts from an RSS feed.' ),
  196. 'plugin-slug' => 'rss-importer',
  197. 'importer-id' => 'rss',
  198. ),
  199. 'tumblr' => array(
  200. 'name' => __( 'Tumblr' ),
  201. 'description' => __( 'Import posts &amp; media from Tumblr using their API.' ),
  202. 'plugin-slug' => 'tumblr-importer',
  203. 'importer-id' => 'tumblr',
  204. ),
  205. 'wordpress' => array(
  206. 'name' => 'WordPress',
  207. 'description' => __( 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
  208. 'plugin-slug' => 'wordpress-importer',
  209. 'importer-id' => 'wordpress',
  210. ),
  211. );
  212. }