| @@ -372,8 +372,16 @@ | ||
| 372 | 372 | |
| 373 | 373 | wp_defer_term_counting( true ); |
| 374 | 374 | wp_defer_comment_counting( true ); |
| 375 | 375 | |
| 376 | + // Safety net for an aborted run: a PHP timeout, memory limit, fatal, or a | |
| 377 | + // cut-off request can end the import before import_end() runs, which would | |
| 378 | + // strand wp_defer_term_counting(true) and a stale {taxonomy}_children cache | |
| 379 | + // and silently drop nested-category counts site-wide. A shutdown handler | |
| 380 | + // guarantees the restore runs on the way out; it is idempotent, so the normal | |
| 381 | + // import_end() path (which also calls it) makes this a no-op. (#167) | |
| 382 | + register_shutdown_function( array( $this, 'restore_counting_and_hierarchy' ) ); | |
| 383 | + | |
| 376 | 384 | do_action( 'import_start', $this ); |
| 377 | 385 | |
| 378 | 386 | return true; |
| 379 | 387 | } |
| @@ -385,8 +393,29 @@ | ||
| 385 | 393 | wp_import_cleanup( $this->id ); |
| 386 | 394 | |
| 387 | 395 | wp_cache_flush(); |
| 388 | 396 | |
| 397 | + $this->restore_counting_and_hierarchy(); | |
| 398 | + | |
| 399 | + do_action( 'import_end' ); | |
| 400 | + } | |
| 401 | + | |
| 402 | + /** | |
| 403 | + * Turn term/comment counting back on and rebuild the term hierarchy. | |
| 404 | + * | |
| 405 | + * import_start() registers this as a shutdown handler so an aborted run (timeout, | |
| 406 | + * memory, fatal, cut-off request) that never reaches import_end() cannot strand | |
| 407 | + * wp_defer_term_counting(true) or a stale {taxonomy}_children cache. Idempotent, | |
| 408 | + * so it is safe to call from both import_end() and the shutdown handler. (#167) | |
| 409 | + */ | |
| 410 | + public function restore_counting_and_hierarchy() { | |
| 411 | + static $restored = false; | |
| 412 | + | |
| 413 | + if ( $restored ) { | |
| 414 | + return; | |
| 415 | + } | |
| 416 | + $restored = true; | |
| 417 | + | |
| 389 | 418 | foreach ( get_taxonomies() as $tax ) { |
| 390 | 419 | delete_option( "{$tax}_children" ); |
| 391 | 420 | _get_term_hierarchy( $tax ); |
| 392 | 421 | } |
| @@ -392,10 +421,8 @@ | ||
| 392 | 421 | } |
| 393 | 422 | |
| 394 | 423 | wp_defer_term_counting( false ); |
| 395 | 424 | wp_defer_comment_counting( false ); |
| 396 | - | |
| 397 | - do_action( 'import_end' ); | |
| 398 | 425 | } |
| 399 | 426 | |
| 400 | 427 | /** |
| 401 | 428 | * Retrieve authors from parsed WXR data and set it to `$this->>authors`. |