PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wp-admin/includes/class-wp-importer.php

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