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