author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 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 |
*/ |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
198 |
public function get_page( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
199 |
$url, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
200 |
$username = '', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
201 |
#[\SensitiveParameter] |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
202 |
$password = '', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
203 |
$head = false |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
204 |
) { |
16 | 205 |
// Increase the timeout. |
0 | 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 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
223 |
* Bumps up the request timeout for http requests. |
0 | 224 |
* |
225 |
* @param int $val |
|
226 |
* @return int |
|
227 |
*/ |
|
5 | 228 |
public function bump_request_timeout( $val ) { |
0 | 229 |
return 60; |
230 |
} |
|
231 |
||
232 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
233 |
* Checks if user has exceeded disk quota. |
0 | 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 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
248 |
* Replaces newlines, tabs, and multiple spaces with a single space. |
0 | 249 |
* |
19 | 250 |
* @param string $text |
0 | 251 |
* @return string |
252 |
*/ |
|
19 | 253 |
public function min_whitespace( $text ) { |
254 |
return preg_replace( '|[\r\n\t ]+|', ' ', $text ); |
|
0 | 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. |
18 | 263 |
* @global int[] $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(); |
|
16 | 269 |
// Reset $wp_actions to keep it from growing out of control. |
0 | 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 |
|
18 | 293 |
$il = count( $args ); |
0 | 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++ ) { |
16 | 309 |
$key = $match[1][ $j ]; |
9 | 310 |
$out[ $key ] = true; |
0 | 311 |
} |
312 |
||
313 |
$last_arg = $key; |
|
16 | 314 |
} elseif ( null !== $last_arg ) { |
9 | 315 |
$out[ $last_arg ] = $args[ $i ]; |
0 | 316 |
} |
317 |
} |
|
318 |
||
16 | 319 |
// Check array for specified param. |
9 | 320 |
if ( isset( $out[ $param ] ) ) { |
16 | 321 |
// Set return value. |
9 | 322 |
$return = $out[ $param ]; |
0 | 323 |
} |
324 |
||
16 | 325 |
// Check for missing required param. |
9 | 326 |
if ( ! isset( $out[ $param ] ) && $required ) { |
16 | 327 |
// Display message and exit. |
0 | 328 |
echo "\"$param\" parameter is required but was not specified\n"; |
16 | 329 |
exit; |
0 | 330 |
} |
331 |
||
332 |
return $return; |
|
333 |
} |