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