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