author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
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 |
||
16 | 28 |
// Grab all posts in chunks. |
0 | 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 |
|
16 | 34 |
// Increment offset. |
0 | 35 |
$offset = ( $limit + $offset ); |
36 |
||
9 | 37 |
if ( ! empty( $results ) ) { |
0 | 38 |
foreach ( $results as $r ) { |
16 | 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 |
||
16 | 65 |
// Get count of permalinks. |
0 | 66 |
$meta_key = $importer_name . '_' . $bid . '_permalink'; |
16 | 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 |
||
16 | 97 |
// Grab all comments in chunks. |
0 | 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 |
||
16 | 102 |
// Increment offset. |
0 | 103 |
$offset = ( $limit + $offset ); |
104 |
||
9 | 105 |
if ( ! empty( $results ) ) { |
0 | 106 |
foreach ( $results as $r ) { |
16 | 107 |
// Explode comment_agent key. |
0 | 108 |
list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent ); |
9 | 109 |
$source_comment_id = intval( $source_comment_id ); |
0 | 110 |
|
16 | 111 |
// Check if this comment came from this blog. |
0 | 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 { |
|
16 | 133 |
$blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id ); |
134 |
$parsed = parse_url( $blog ); |
|
135 |
if ( ! $parsed || empty( $parsed['host'] ) ) { |
|
0 | 136 |
fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" ); |
16 | 137 |
exit; |
0 | 138 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
if ( empty( $parsed['path'] ) ) { |
0 | 140 |
$parsed['path'] = '/'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
} |
9 | 142 |
$blogs = get_sites( |
143 |
array( |
|
144 |
'domain' => $parsed['host'], |
|
145 |
'number' => 1, |
|
146 |
'path' => $parsed['path'], |
|
147 |
) |
|
148 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
if ( ! $blogs ) { |
0 | 150 |
fwrite( STDERR, "Error: Could not find blog\n" ); |
16 | 151 |
exit; |
0 | 152 |
} |
9 | 153 |
$blog = array_shift( $blogs ); |
0 | 154 |
$blog_id = (int) $blog->blog_id; |
155 |
} |
|
156 |
||
157 |
if ( function_exists( 'is_multisite' ) ) { |
|
9 | 158 |
if ( is_multisite() ) { |
0 | 159 |
switch_to_blog( $blog_id ); |
9 | 160 |
} |
0 | 161 |
} |
162 |
||
163 |
return $blog_id; |
|
164 |
} |
|
165 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
* @param int $user_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
168 |
* @return int|void |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
*/ |
5 | 170 |
public function set_user( $user_id ) { |
0 | 171 |
if ( is_numeric( $user_id ) ) { |
172 |
$user_id = (int) $user_id; |
|
173 |
} else { |
|
174 |
$user_id = (int) username_exists( $user_id ); |
|
175 |
} |
|
176 |
||
9 | 177 |
if ( ! $user_id || ! wp_set_current_user( $user_id ) ) { |
0 | 178 |
fwrite( STDERR, "Error: can not find user\n" ); |
16 | 179 |
exit; |
0 | 180 |
} |
181 |
||
182 |
return $user_id; |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Sort by strlen, longest string first |
|
187 |
* |
|
188 |
* @param string $a |
|
189 |
* @param string $b |
|
190 |
* @return int |
|
191 |
*/ |
|
5 | 192 |
public function cmpr_strlen( $a, $b ) { |
0 | 193 |
return strlen( $b ) - strlen( $a ); |
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* GET URL |
|
198 |
* |
|
199 |
* @param string $url |
|
200 |
* @param string $username |
|
201 |
* @param string $password |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
* @param bool $head |
0 | 203 |
* @return array |
204 |
*/ |
|
5 | 205 |
public function get_page( $url, $username = '', $password = '', $head = false ) { |
16 | 206 |
// Increase the timeout. |
0 | 207 |
add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) ); |
208 |
||
209 |
$headers = array(); |
|
9 | 210 |
$args = array(); |
211 |
if ( true === $head ) { |
|
0 | 212 |
$args['method'] = 'HEAD'; |
9 | 213 |
} |
214 |
if ( ! empty( $username ) && ! empty( $password ) ) { |
|
0 | 215 |
$headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" ); |
9 | 216 |
} |
0 | 217 |
|
218 |
$args['headers'] = $headers; |
|
219 |
||
220 |
return wp_safe_remote_request( $url, $args ); |
|
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Bump up the request timeout for http requests |
|
225 |
* |
|
226 |
* @param int $val |
|
227 |
* @return int |
|
228 |
*/ |
|
5 | 229 |
public function bump_request_timeout( $val ) { |
0 | 230 |
return 60; |
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* Check if user has exceeded disk quota |
|
235 |
* |
|
236 |
* @return bool |
|
237 |
*/ |
|
5 | 238 |
public function is_user_over_quota() { |
0 | 239 |
if ( function_exists( 'upload_is_user_over_quota' ) ) { |
5 | 240 |
if ( upload_is_user_over_quota() ) { |
0 | 241 |
return true; |
242 |
} |
|
243 |
} |
|
244 |
||
245 |
return false; |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Replace newlines, tabs, and multiple spaces with a single space |
|
250 |
* |
|
251 |
* @param string $string |
|
252 |
* @return string |
|
253 |
*/ |
|
5 | 254 |
public function min_whitespace( $string ) { |
0 | 255 |
return preg_replace( '|[\r\n\t ]+|', ' ', $string ); |
256 |
} |
|
257 |
||
258 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
* Resets global variables that grow out of control during imports. |
0 | 260 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
261 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
* @global array $wp_actions |
0 | 265 |
*/ |
5 | 266 |
public function stop_the_insanity() { |
0 | 267 |
global $wpdb, $wp_actions; |
268 |
// Or define( 'WP_IMPORTING', true ); |
|
269 |
$wpdb->queries = array(); |
|
16 | 270 |
// Reset $wp_actions to keep it from growing out of control. |
0 | 271 |
$wp_actions = array(); |
272 |
} |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Returns value of command line params. |
|
277 |
* Exits when a required param is not set. |
|
278 |
* |
|
279 |
* @param string $param |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
* @param bool $required |
0 | 281 |
* @return mixed |
282 |
*/ |
|
283 |
function get_cli_args( $param, $required = false ) { |
|
284 |
$args = $_SERVER['argv']; |
|
9 | 285 |
if ( ! is_array( $args ) ) { |
286 |
$args = array(); |
|
287 |
} |
|
0 | 288 |
|
289 |
$out = array(); |
|
290 |
||
291 |
$last_arg = null; |
|
9 | 292 |
$return = null; |
0 | 293 |
|
294 |
$il = sizeof( $args ); |
|
295 |
||
296 |
for ( $i = 1, $il; $i < $il; $i++ ) { |
|
9 | 297 |
if ( (bool) preg_match( '/^--(.+)/', $args[ $i ], $match ) ) { |
298 |
$parts = explode( '=', $match[1] ); |
|
299 |
$key = preg_replace( '/[^a-z0-9]+/', '', $parts[0] ); |
|
0 | 300 |
|
301 |
if ( isset( $parts[1] ) ) { |
|
9 | 302 |
$out[ $key ] = $parts[1]; |
0 | 303 |
} else { |
9 | 304 |
$out[ $key ] = true; |
0 | 305 |
} |
306 |
||
307 |
$last_arg = $key; |
|
9 | 308 |
} elseif ( (bool) preg_match( '/^-([a-zA-Z0-9]+)/', $args[ $i ], $match ) ) { |
0 | 309 |
for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) { |
16 | 310 |
$key = $match[1][ $j ]; |
9 | 311 |
$out[ $key ] = true; |
0 | 312 |
} |
313 |
||
314 |
$last_arg = $key; |
|
16 | 315 |
} elseif ( null !== $last_arg ) { |
9 | 316 |
$out[ $last_arg ] = $args[ $i ]; |
0 | 317 |
} |
318 |
} |
|
319 |
||
16 | 320 |
// Check array for specified param. |
9 | 321 |
if ( isset( $out[ $param ] ) ) { |
16 | 322 |
// Set return value. |
9 | 323 |
$return = $out[ $param ]; |
0 | 324 |
} |
325 |
||
16 | 326 |
// Check for missing required param. |
9 | 327 |
if ( ! isset( $out[ $param ] ) && $required ) { |
16 | 328 |
// Display message and exit. |
0 | 329 |
echo "\"$param\" parameter is required but was not specified\n"; |
16 | 330 |
exit; |
0 | 331 |
} |
332 |
||
333 |
return $return; |
|
334 |
} |