Нема описа

class-wp-importer.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * WP_Importer base class
  4. */
  5. class WP_Importer {
  6. /**
  7. * Class Constructor
  8. */
  9. public function __construct() {}
  10. /**
  11. * Returns array with imported permalinks from WordPress database
  12. *
  13. * @global wpdb $wpdb WordPress database abstraction object.
  14. *
  15. * @param string $importer_name
  16. * @param string $blog_id
  17. * @return array
  18. */
  19. public function get_imported_posts( $importer_name, $blog_id ) {
  20. global $wpdb;
  21. $hashtable = array();
  22. $limit = 100;
  23. $offset = 0;
  24. // Grab all posts in chunks.
  25. do {
  26. $meta_key = $importer_name . '_' . $blog_id . '_permalink';
  27. $sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT %d,%d", $meta_key, $offset, $limit );
  28. $results = $wpdb->get_results( $sql );
  29. // Increment offset.
  30. $offset = ( $limit + $offset );
  31. if ( ! empty( $results ) ) {
  32. foreach ( $results as $r ) {
  33. // Set permalinks into array.
  34. $hashtable[ $r->meta_value ] = (int) $r->post_id;
  35. }
  36. }
  37. } while ( count( $results ) == $limit );
  38. return $hashtable;
  39. }
  40. /**
  41. * Return count of imported permalinks from WordPress database
  42. *
  43. * @global wpdb $wpdb WordPress database abstraction object.
  44. *
  45. * @param string $importer_name
  46. * @param string $blog_id
  47. * @return int
  48. */
  49. public function count_imported_posts( $importer_name, $blog_id ) {
  50. global $wpdb;
  51. $count = 0;
  52. // Get count of permalinks.
  53. $meta_key = $importer_name . '_' . $blog_id . '_permalink';
  54. $sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key );
  55. $result = $wpdb->get_results( $sql );
  56. if ( ! empty( $result ) ) {
  57. $count = (int) $result[0]->cnt;
  58. }
  59. return $count;
  60. }
  61. /**
  62. * Set array with imported comments from WordPress database
  63. *
  64. * @global wpdb $wpdb WordPress database abstraction object.
  65. *
  66. * @param string $blog_id
  67. * @return array
  68. */
  69. public function get_imported_comments( $blog_id ) {
  70. global $wpdb;
  71. $hashtable = array();
  72. $limit = 100;
  73. $offset = 0;
  74. // Grab all comments in chunks.
  75. do {
  76. $sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit );
  77. $results = $wpdb->get_results( $sql );
  78. // Increment offset.
  79. $offset = ( $limit + $offset );
  80. if ( ! empty( $results ) ) {
  81. foreach ( $results as $r ) {
  82. // Explode comment_agent key.
  83. list ( $comment_agent_blog_id, $source_comment_id ) = explode( '-', $r->comment_agent );
  84. $source_comment_id = (int) $source_comment_id;
  85. // Check if this comment came from this blog.
  86. if ( $blog_id == $comment_agent_blog_id ) {
  87. $hashtable[ $source_comment_id ] = (int) $r->comment_ID;
  88. }
  89. }
  90. }
  91. } while ( count( $results ) == $limit );
  92. return $hashtable;
  93. }
  94. /**
  95. * @param int $blog_id
  96. * @return int|void
  97. */
  98. public function set_blog( $blog_id ) {
  99. if ( is_numeric( $blog_id ) ) {
  100. $blog_id = (int) $blog_id;
  101. } else {
  102. $blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
  103. $parsed = parse_url( $blog );
  104. if ( ! $parsed || empty( $parsed['host'] ) ) {
  105. fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
  106. exit;
  107. }
  108. if ( empty( $parsed['path'] ) ) {
  109. $parsed['path'] = '/';
  110. }
  111. $blogs = get_sites(
  112. array(
  113. 'domain' => $parsed['host'],
  114. 'number' => 1,
  115. 'path' => $parsed['path'],
  116. )
  117. );
  118. if ( ! $blogs ) {
  119. fwrite( STDERR, "Error: Could not find blog\n" );
  120. exit;
  121. }
  122. $blog = array_shift( $blogs );
  123. $blog_id = (int) $blog->blog_id;
  124. }
  125. if ( function_exists( 'is_multisite' ) ) {
  126. if ( is_multisite() ) {
  127. switch_to_blog( $blog_id );
  128. }
  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. *
  151. * @param string $a
  152. * @param string $b
  153. * @return int
  154. */
  155. public function cmpr_strlen( $a, $b ) {
  156. return strlen( $b ) - strlen( $a );
  157. }
  158. /**
  159. * GET URL
  160. *
  161. * @param string $url
  162. * @param string $username
  163. * @param string $password
  164. * @param bool $head
  165. * @return array
  166. */
  167. public function get_page( $url, $username = '', $password = '', $head = false ) {
  168. // Increase the timeout.
  169. add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
  170. $headers = array();
  171. $args = array();
  172. if ( true === $head ) {
  173. $args['method'] = 'HEAD';
  174. }
  175. if ( ! empty( $username ) && ! empty( $password ) ) {
  176. $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
  177. }
  178. $args['headers'] = $headers;
  179. return wp_safe_remote_request( $url, $args );
  180. }
  181. /**
  182. * Bump up the request timeout for http requests
  183. *
  184. * @param int $val
  185. * @return int
  186. */
  187. public function bump_request_timeout( $val ) {
  188. return 60;
  189. }
  190. /**
  191. * Check if user has exceeded disk quota
  192. *
  193. * @return bool
  194. */
  195. public function is_user_over_quota() {
  196. if ( function_exists( 'upload_is_user_over_quota' ) ) {
  197. if ( upload_is_user_over_quota() ) {
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. /**
  204. * Replace newlines, tabs, and multiple spaces with a single space.
  205. *
  206. * @param string $text
  207. * @return string
  208. */
  209. public function min_whitespace( $text ) {
  210. return preg_replace( '|[\r\n\t ]+|', ' ', $text );
  211. }
  212. /**
  213. * Resets global variables that grow out of control during imports.
  214. *
  215. * @since 3.0.0
  216. *
  217. * @global wpdb $wpdb WordPress database abstraction object.
  218. * @global int[] $wp_actions
  219. */
  220. public function stop_the_insanity() {
  221. global $wpdb, $wp_actions;
  222. // Or define( 'WP_IMPORTING', true );
  223. $wpdb->queries = array();
  224. // Reset $wp_actions to keep it from growing out of control.
  225. $wp_actions = array();
  226. }
  227. }
  228. /**
  229. * Returns value of command line params.
  230. * Exits when a required param is not set.
  231. *
  232. * @param string $param
  233. * @param bool $required
  234. * @return mixed
  235. */
  236. function get_cli_args( $param, $required = false ) {
  237. $args = $_SERVER['argv'];
  238. if ( ! is_array( $args ) ) {
  239. $args = array();
  240. }
  241. $out = array();
  242. $last_arg = null;
  243. $return = null;
  244. $il = count( $args );
  245. for ( $i = 1, $il; $i < $il; $i++ ) {
  246. if ( (bool) preg_match( '/^--(.+)/', $args[ $i ], $match ) ) {
  247. $parts = explode( '=', $match[1] );
  248. $key = preg_replace( '/[^a-z0-9]+/', '', $parts[0] );
  249. if ( isset( $parts[1] ) ) {
  250. $out[ $key ] = $parts[1];
  251. } else {
  252. $out[ $key ] = true;
  253. }
  254. $last_arg = $key;
  255. } elseif ( (bool) preg_match( '/^-([a-zA-Z0-9]+)/', $args[ $i ], $match ) ) {
  256. for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
  257. $key = $match[1][ $j ];
  258. $out[ $key ] = true;
  259. }
  260. $last_arg = $key;
  261. } elseif ( null !== $last_arg ) {
  262. $out[ $last_arg ] = $args[ $i ];
  263. }
  264. }
  265. // Check array for specified param.
  266. if ( isset( $out[ $param ] ) ) {
  267. // Set return value.
  268. $return = $out[ $param ];
  269. }
  270. // Check for missing required param.
  271. if ( ! isset( $out[ $param ] ) && $required ) {
  272. // Display message and exit.
  273. echo "\"$param\" parameter is required but was not specified\n";
  274. exit;
  275. }
  276. return $return;
  277. }