class-wp-importer.php 7.3 KB

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