import.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * WordPress Administration Importer API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Retrieve 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. * Register 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 WP_Error Returns 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. * Handle importer uploading and add 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' => __( '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.' ),
  81. );
  82. }
  83. $overrides = array(
  84. 'test_form' => false,
  85. 'test_type' => false,
  86. );
  87. $_FILES['import']['name'] .= '.txt';
  88. $upload = wp_handle_upload( $_FILES['import'], $overrides );
  89. if ( isset( $upload['error'] ) ) {
  90. return $upload;
  91. }
  92. // Construct the object array
  93. $object = array(
  94. 'post_title' => wp_basename( $upload['file'] ),
  95. 'post_content' => $upload['url'],
  96. 'post_mime_type' => $upload['type'],
  97. 'guid' => $upload['url'],
  98. 'context' => 'import',
  99. 'post_status' => 'private',
  100. );
  101. // Save the data
  102. $id = wp_insert_attachment( $object, $upload['file'] );
  103. /*
  104. * Schedule a cleanup for one day from now in case of failed
  105. * import or missing wp_import_cleanup() call.
  106. */
  107. wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
  108. return array(
  109. 'file' => $upload['file'],
  110. 'id' => $id,
  111. );
  112. }
  113. /**
  114. * Returns a list from WordPress.org of popular importer plugins.
  115. *
  116. * @since 3.5.0
  117. *
  118. * @return array Importers with metadata for each.
  119. */
  120. function wp_get_popular_importers() {
  121. include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
  122. $locale = get_user_locale();
  123. $cache_key = 'popular_importers_' . md5( $locale . $wp_version );
  124. $popular_importers = get_site_transient( $cache_key );
  125. if ( ! $popular_importers ) {
  126. $url = add_query_arg(
  127. array(
  128. 'locale' => $locale,
  129. 'version' => $wp_version,
  130. ),
  131. 'http://api.wordpress.org/core/importers/1.1/'
  132. );
  133. $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ) );
  134. if ( wp_http_supports( array( 'ssl' ) ) ) {
  135. $url = set_url_scheme( $url, 'https' );
  136. }
  137. $response = wp_remote_get( $url, $options );
  138. $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
  139. if ( is_array( $popular_importers ) ) {
  140. set_site_transient( $cache_key, $popular_importers, 2 * DAY_IN_SECONDS );
  141. } else {
  142. $popular_importers = false;
  143. }
  144. }
  145. if ( is_array( $popular_importers ) ) {
  146. // If the data was received as translated, return it as-is.
  147. if ( $popular_importers['translated'] ) {
  148. return $popular_importers['importers'];
  149. }
  150. foreach ( $popular_importers['importers'] as &$importer ) {
  151. // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
  152. $importer['description'] = translate( $importer['description'] );
  153. if ( $importer['name'] != 'WordPress' ) {
  154. // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
  155. $importer['name'] = translate( $importer['name'] );
  156. }
  157. }
  158. return $popular_importers['importers'];
  159. }
  160. return array(
  161. // slug => name, description, plugin slug, and register_importer() slug
  162. 'blogger' => array(
  163. 'name' => __( 'Blogger' ),
  164. 'description' => __( 'Import posts, comments, and users from a Blogger blog.' ),
  165. 'plugin-slug' => 'blogger-importer',
  166. 'importer-id' => 'blogger',
  167. ),
  168. 'wpcat2tag' => array(
  169. 'name' => __( 'Categories and Tags Converter' ),
  170. 'description' => __( 'Convert existing categories to tags or tags to categories, selectively.' ),
  171. 'plugin-slug' => 'wpcat2tag-importer',
  172. 'importer-id' => 'wp-cat2tag',
  173. ),
  174. 'livejournal' => array(
  175. 'name' => __( 'LiveJournal' ),
  176. 'description' => __( 'Import posts from LiveJournal using their API.' ),
  177. 'plugin-slug' => 'livejournal-importer',
  178. 'importer-id' => 'livejournal',
  179. ),
  180. 'movabletype' => array(
  181. 'name' => __( 'Movable Type and TypePad' ),
  182. 'description' => __( 'Import posts and comments from a Movable Type or TypePad blog.' ),
  183. 'plugin-slug' => 'movabletype-importer',
  184. 'importer-id' => 'mt',
  185. ),
  186. 'opml' => array(
  187. 'name' => __( 'Blogroll' ),
  188. 'description' => __( 'Import links in OPML format.' ),
  189. 'plugin-slug' => 'opml-importer',
  190. 'importer-id' => 'opml',
  191. ),
  192. 'rss' => array(
  193. 'name' => __( 'RSS' ),
  194. 'description' => __( 'Import posts from an RSS feed.' ),
  195. 'plugin-slug' => 'rss-importer',
  196. 'importer-id' => 'rss',
  197. ),
  198. 'tumblr' => array(
  199. 'name' => __( 'Tumblr' ),
  200. 'description' => __( 'Import posts &amp; media from Tumblr using their API.' ),
  201. 'plugin-slug' => 'tumblr-importer',
  202. 'importer-id' => 'tumblr',
  203. ),
  204. 'wordpress' => array(
  205. 'name' => 'WordPress',
  206. 'description' => __( 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
  207. 'plugin-slug' => 'wordpress-importer',
  208. 'importer-id' => 'wordpress',
  209. ),
  210. );
  211. }