Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

322 wiersze
7.1 KiB

  1. <?php
  2. /**
  3. * WP_Importer base class
  4. */
  5. class WP_Importer {
  6. /**
  7. * Class Constructor
  8. *
  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 $bid
  18. * @return array
  19. */
  20. public function get_imported_posts( $importer_name, $bid ) {
  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 . '_' . $bid . '_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] = intval( $r->post_id );
  36. }
  37. }
  38. } while ( count( $results ) == $limit );
  39. // Unset to save memory.
  40. unset( $results, $r );
  41. return $hashtable;
  42. }
  43. /**
  44. * Return count of imported permalinks from WordPress database
  45. *
  46. * @global wpdb $wpdb WordPress database abstraction object.
  47. *
  48. * @param string $importer_name
  49. * @param string $bid
  50. * @return int
  51. */
  52. public function count_imported_posts( $importer_name, $bid ) {
  53. global $wpdb;
  54. $count = 0;
  55. // Get count of permalinks
  56. $meta_key = $importer_name . '_' . $bid . '_permalink';
  57. $sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = '%s'", $meta_key );
  58. $result = $wpdb->get_results( $sql );
  59. if ( !empty( $result ) )
  60. $count = intval( $result[0]->cnt );
  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. *
  102. * @param int $blog_id
  103. * @return int|void
  104. */
  105. public function set_blog( $blog_id ) {
  106. if ( is_numeric( $blog_id ) ) {
  107. $blog_id = (int) $blog_id;
  108. } else {
  109. $blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
  110. if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
  111. fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
  112. exit();
  113. }
  114. if ( empty( $parsed['path'] ) ) {
  115. $parsed['path'] = '/';
  116. }
  117. $blogs = get_sites( array( 'domain' => $parsed['host'], 'number' => 1, 'path' => $parsed['path'] ) );
  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. return $blog_id;
  130. }
  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. if ( !empty( $username ) && !empty( $password ) )
  175. $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
  176. $args['headers'] = $headers;
  177. return wp_safe_remote_request( $url, $args );
  178. }
  179. /**
  180. * Bump up the request timeout for http requests
  181. *
  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. *
  191. * @return bool
  192. */
  193. public function is_user_over_quota() {
  194. if ( function_exists( 'upload_is_user_over_quota' ) ) {
  195. if ( upload_is_user_over_quota() ) {
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. /**
  202. * Replace newlines, tabs, and multiple spaces with a single space
  203. *
  204. * @param string $string
  205. * @return string
  206. */
  207. public function min_whitespace( $string ) {
  208. return preg_replace( '|[\r\n\t ]+|', ' ', $string );
  209. }
  210. /**
  211. * Reset global variables that grow out of control during imports
  212. *
  213. * @global wpdb $wpdb
  214. * @global array $wp_actions
  215. */
  216. public function stop_the_insanity() {
  217. global $wpdb, $wp_actions;
  218. // Or define( 'WP_IMPORTING', true );
  219. $wpdb->queries = array();
  220. // Reset $wp_actions to keep it from growing out of control
  221. $wp_actions = array();
  222. }
  223. }
  224. /**
  225. * Returns value of command line params.
  226. * Exits when a required param is not set.
  227. *
  228. * @param string $param
  229. * @param bool $required
  230. * @return mixed
  231. */
  232. function get_cli_args( $param, $required = false ) {
  233. $args = $_SERVER['argv'];
  234. $out = array();
  235. $last_arg = null;
  236. $return = null;
  237. $il = sizeof( $args );
  238. for ( $i = 1, $il; $i < $il; $i++ ) {
  239. if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
  240. $parts = explode( "=", $match[1] );
  241. $key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );
  242. if ( isset( $parts[1] ) ) {
  243. $out[$key] = $parts[1];
  244. } else {
  245. $out[$key] = true;
  246. }
  247. $last_arg = $key;
  248. } elseif ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
  249. for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
  250. $key = $match[1]{$j};
  251. $out[$key] = true;
  252. }
  253. $last_arg = $key;
  254. } elseif ( $last_arg !== null ) {
  255. $out[$last_arg] = $args[$i];
  256. }
  257. }
  258. // Check array for specified param
  259. if ( isset( $out[$param] ) ) {
  260. // Set return value
  261. $return = $out[$param];
  262. }
  263. // Check for missing required param
  264. if ( !isset( $out[$param] ) && $required ) {
  265. // Display message and exit
  266. echo "\"$param\" parameter is required but was not specified\n";
  267. exit();
  268. }
  269. return $return;
  270. }