WP_Importer

WP_Importer base class.

Defined (1)

The class is defined in the following location(s).

/wp-admin/includes/class-wp-importer.php  
  1. class WP_Importer { 
  2. /** 
  3. * Class Constructor 
  4. */ 
  5. public function __construct() {} 
  6.  
  7. /** 
  8. * Returns array with imported permalinks from WordPress database 
  9. * @global wpdb $wpdb WordPress database abstraction object. 
  10. * @param string $importer_name 
  11. * @param string $bid 
  12. * @return array 
  13. */ 
  14. public function get_imported_posts( $importer_name, $bid ) { 
  15. global $wpdb; 
  16.  
  17. $hashtable = array(); 
  18.  
  19. $limit = 100; 
  20. $offset = 0; 
  21.  
  22. // Grab all posts in chunks 
  23. do { 
  24. $meta_key = $importer_name . '_' . $bid . '_permalink'; 
  25. $sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '%s' LIMIT %d, %d", $meta_key, $offset, $limit ); 
  26. $results = $wpdb->get_results( $sql ); 
  27.  
  28. // Increment offset 
  29. $offset = ( $limit + $offset ); 
  30.  
  31. if ( !empty( $results ) ) { 
  32. foreach ( $results as $r ) { 
  33. // Set permalinks into array 
  34. $hashtable[$r->meta_value] = intval( $r->post_id ); 
  35. } while ( count( $results ) == $limit ); 
  36.  
  37. // Unset to save memory. 
  38. unset( $results, $r ); 
  39.  
  40. return $hashtable; 
  41.  
  42. /** 
  43. * Return count of imported permalinks from WordPress database 
  44. * @global wpdb $wpdb WordPress database abstraction object. 
  45. * @param string $importer_name 
  46. * @param string $bid 
  47. * @return int 
  48. */ 
  49. public function count_imported_posts( $importer_name, $bid ) { 
  50. global $wpdb; 
  51.  
  52. $count = 0; 
  53.  
  54. // Get count of permalinks 
  55. $meta_key = $importer_name . '_' . $bid . '_permalink'; 
  56. $sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = '%s'", $meta_key ); 
  57.  
  58. $result = $wpdb->get_results( $sql ); 
  59.  
  60. if ( !empty( $result ) ) 
  61. $count = intval( $result[0]->cnt ); 
  62.  
  63. // Unset to save memory. 
  64. unset( $results ); 
  65.  
  66. return $count; 
  67.  
  68. /** 
  69. * Set array with imported comments from WordPress database 
  70. * @global wpdb $wpdb WordPress database abstraction object. 
  71. * @param string $bid 
  72. * @return array 
  73. */ 
  74. public function get_imported_comments( $bid ) { 
  75. global $wpdb; 
  76.  
  77. $hashtable = array(); 
  78.  
  79. $limit = 100; 
  80. $offset = 0; 
  81.  
  82. // Grab all comments in chunks 
  83. do { 
  84. $sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d, %d", $offset, $limit ); 
  85. $results = $wpdb->get_results( $sql ); 
  86.  
  87. // Increment offset 
  88. $offset = ( $limit + $offset ); 
  89.  
  90. if ( !empty( $results ) ) { 
  91. foreach ( $results as $r ) { 
  92. // Explode comment_agent key 
  93. list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent ); 
  94. $source_comment_id = intval( $source_comment_id ); 
  95.  
  96. // Check if this comment came from this blog 
  97. if ( $bid == $ca_bid ) { 
  98. $hashtable[$source_comment_id] = intval( $r->comment_ID ); 
  99. } while ( count( $results ) == $limit ); 
  100.  
  101. // Unset to save memory. 
  102. unset( $results, $r ); 
  103.  
  104. return $hashtable; 
  105.  
  106. /** 
  107. * @param int $blog_id 
  108. * @return int|void 
  109. */ 
  110. public function set_blog( $blog_id ) { 
  111. if ( is_numeric( $blog_id ) ) { 
  112. $blog_id = (int) $blog_id; 
  113. } else { 
  114. $blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id ); 
  115. if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) { 
  116. fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" ); 
  117. exit(); 
  118. if ( empty( $parsed['path'] ) ) 
  119. $parsed['path'] = '/'; 
  120. $blog = get_blog_details( array( 'domain' => $parsed['host'], 'path' => $parsed['path'] ) ); 
  121. if ( !$blog ) { 
  122. fwrite( STDERR, "Error: Could not find blog\n" ); 
  123. exit(); 
  124. $blog_id = (int) $blog->blog_id; 
  125.  
  126. if ( function_exists( 'is_multisite' ) ) { 
  127. if ( is_multisite() ) 
  128. switch_to_blog( $blog_id ); 
  129.  
  130. return $blog_id; 
  131.  
  132. /** 
  133. * @param int $user_id 
  134. * @return int|void 
  135. */ 
  136. public function set_user( $user_id ) { 
  137. if ( is_numeric( $user_id ) ) { 
  138. $user_id = (int) $user_id; 
  139. } else { 
  140. $user_id = (int) username_exists( $user_id ); 
  141.  
  142. if ( !$user_id || !wp_set_current_user( $user_id ) ) { 
  143. fwrite( STDERR, "Error: can not find user\n" ); 
  144. exit(); 
  145.  
  146. return $user_id; 
  147.  
  148. /** 
  149. * Sort by strlen, longest string first 
  150. * @param string $a 
  151. * @param string $b 
  152. * @return int 
  153. */ 
  154. public function cmpr_strlen( $a, $b ) { 
  155. return strlen( $b ) - strlen( $a ); 
  156.  
  157. /** 
  158. * GET URL 
  159. * @param string $url 
  160. * @param string $username 
  161. * @param string $password 
  162. * @param bool $head 
  163. * @return array 
  164. */ 
  165. public function get_page( $url, $username = '', $password = '', $head = false ) { 
  166. // Increase the timeout 
  167. add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) ); 
  168.  
  169. $headers = array(); 
  170. $args = array(); 
  171. if ( true === $head ) 
  172. $args['method'] = 'HEAD'; 
  173. if ( !empty( $username ) && !empty( $password ) ) 
  174. $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" ); 
  175.  
  176. $args['headers'] = $headers; 
  177.  
  178. return wp_safe_remote_request( $url, $args ); 
  179.  
  180. /** 
  181. * Bump up the request timeout for http requests 
  182. * @param int $val 
  183. * @return int 
  184. */ 
  185. public function bump_request_timeout( $val ) { 
  186. return 60; 
  187.  
  188. /** 
  189. * Check if user has exceeded disk quota 
  190. * @return bool 
  191. */ 
  192. public function is_user_over_quota() { 
  193. if ( function_exists( 'upload_is_user_over_quota' ) ) { 
  194. if ( upload_is_user_over_quota() ) { 
  195. return true; 
  196.  
  197. return false; 
  198.  
  199. /** 
  200. * Replace newlines, tabs, and multiple spaces with a single space 
  201. * @param string $string 
  202. * @return string 
  203. */ 
  204. public function min_whitespace( $string ) { 
  205. return preg_replace( '|[\r\n\t ]+|', ' ', $string ); 
  206.  
  207. /** 
  208. * Reset global variables that grow out of control during imports 
  209. * @global wpdb $wpdb 
  210. * @global array $wp_actions 
  211. */ 
  212. public function stop_the_insanity() { 
  213. global $wpdb, $wp_actions; 
  214. // Or define( 'WP_IMPORTING', true ); 
  215. $wpdb->queries = array(); 
  216. // Reset $wp_actions to keep it from growing out of control 
  217. $wp_actions = array();