You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

213 rivejä
6.0 KiB

  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. $wp_importers[$id] = array ( $name, $description, $callback );
  56. }
  57. /**
  58. * Cleanup importer.
  59. *
  60. * Removes attachment based on ID.
  61. *
  62. * @since 2.0.0
  63. *
  64. * @param string $id Importer ID.
  65. */
  66. function wp_import_cleanup( $id ) {
  67. wp_delete_attachment( $id );
  68. }
  69. /**
  70. * Handle importer uploading and add attachment.
  71. *
  72. * @since 2.0.0
  73. *
  74. * @return array Uploaded file's details on success, error message on failure
  75. */
  76. function wp_import_handle_upload() {
  77. if ( ! isset( $_FILES['import'] ) ) {
  78. return array(
  79. '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.' )
  80. );
  81. }
  82. $overrides = array( 'test_form' => false, 'test_type' => false );
  83. $_FILES['import']['name'] .= '.txt';
  84. $upload = wp_handle_upload( $_FILES['import'], $overrides );
  85. if ( isset( $upload['error'] ) ) {
  86. return $upload;
  87. }
  88. // Construct the object array
  89. $object = array(
  90. 'post_title' => basename( $upload['file'] ),
  91. 'post_content' => $upload['url'],
  92. 'post_mime_type' => $upload['type'],
  93. 'guid' => $upload['url'],
  94. 'context' => 'import',
  95. 'post_status' => 'private'
  96. );
  97. // Save the data
  98. $id = wp_insert_attachment( $object, $upload['file'] );
  99. /*
  100. * Schedule a cleanup for one day from now in case of failed
  101. * import or missing wp_import_cleanup() call.
  102. */
  103. wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
  104. return array( 'file' => $upload['file'], 'id' => $id );
  105. }
  106. /**
  107. * Returns a list from WordPress.org of popular importer plugins.
  108. *
  109. * @since 3.5.0
  110. *
  111. * @return array Importers with metadata for each.
  112. */
  113. function wp_get_popular_importers() {
  114. include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
  115. $locale = get_user_locale();
  116. $cache_key = 'popular_importers_' . md5( $locale . $wp_version );
  117. $popular_importers = get_site_transient( $cache_key );
  118. if ( ! $popular_importers ) {
  119. $url = add_query_arg( array(
  120. 'locale' => get_user_locale(),
  121. 'version' => $wp_version,
  122. ), 'http://api.wordpress.org/core/importers/1.1/' );
  123. $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() );
  124. $response = wp_remote_get( $url, $options );
  125. $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
  126. if ( is_array( $popular_importers ) ) {
  127. set_site_transient( $cache_key, $popular_importers, 2 * DAY_IN_SECONDS );
  128. } else {
  129. $popular_importers = false;
  130. }
  131. }
  132. if ( is_array( $popular_importers ) ) {
  133. // If the data was received as translated, return it as-is.
  134. if ( $popular_importers['translated'] )
  135. return $popular_importers['importers'];
  136. foreach ( $popular_importers['importers'] as &$importer ) {
  137. $importer['description'] = translate( $importer['description'] );
  138. if ( $importer['name'] != 'WordPress' )
  139. $importer['name'] = translate( $importer['name'] );
  140. }
  141. return $popular_importers['importers'];
  142. }
  143. return array(
  144. // slug => name, description, plugin slug, and register_importer() slug
  145. 'blogger' => array(
  146. 'name' => __( 'Blogger' ),
  147. 'description' => __( 'Import posts, comments, and users from a Blogger blog.' ),
  148. 'plugin-slug' => 'blogger-importer',
  149. 'importer-id' => 'blogger',
  150. ),
  151. 'wpcat2tag' => array(
  152. 'name' => __( 'Categories and Tags Converter' ),
  153. 'description' => __( 'Convert existing categories to tags or tags to categories, selectively.' ),
  154. 'plugin-slug' => 'wpcat2tag-importer',
  155. 'importer-id' => 'wp-cat2tag',
  156. ),
  157. 'livejournal' => array(
  158. 'name' => __( 'LiveJournal' ),
  159. 'description' => __( 'Import posts from LiveJournal using their API.' ),
  160. 'plugin-slug' => 'livejournal-importer',
  161. 'importer-id' => 'livejournal',
  162. ),
  163. 'movabletype' => array(
  164. 'name' => __( 'Movable Type and TypePad' ),
  165. 'description' => __( 'Import posts and comments from a Movable Type or TypePad blog.' ),
  166. 'plugin-slug' => 'movabletype-importer',
  167. 'importer-id' => 'mt',
  168. ),
  169. 'opml' => array(
  170. 'name' => __( 'Blogroll' ),
  171. 'description' => __( 'Import links in OPML format.' ),
  172. 'plugin-slug' => 'opml-importer',
  173. 'importer-id' => 'opml',
  174. ),
  175. 'rss' => array(
  176. 'name' => __( 'RSS' ),
  177. 'description' => __( 'Import posts from an RSS feed.' ),
  178. 'plugin-slug' => 'rss-importer',
  179. 'importer-id' => 'rss',
  180. ),
  181. 'tumblr' => array(
  182. 'name' => __( 'Tumblr' ),
  183. 'description' => __( 'Import posts &amp; media from Tumblr using their API.' ),
  184. 'plugin-slug' => 'tumblr-importer',
  185. 'importer-id' => 'tumblr',
  186. ),
  187. 'wordpress' => array(
  188. 'name' => 'WordPress',
  189. 'description' => __( 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
  190. 'plugin-slug' => 'wordpress-importer',
  191. 'importer-id' => 'wordpress',
  192. ),
  193. );
  194. }