/wp-admin/includes/import.php

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