| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
// allow-no-test-found: exercised by MultisiteNGramRaceConditionTest via the create-tables upgrade flow |
| 8 |
|
| 9 |
/** |
| 10 |
* One-time initialization of the N-gram spell-check cache. |
| 11 |
* |
| 12 |
* Owns the "has the cache ever been built?" decision and, when it has not, |
| 13 |
* schedules an asynchronous WP-Cron rebuild (instead of blocking plugin |
| 14 |
* activation) and surfaces a "build scheduled" admin notice on a version |
| 15 |
* upgrade. Extracted from the table-bootstrap orchestrator because building |
| 16 |
* the spell-check cache is a distinct concern from creating the schema: it |
| 17 |
* has its own multisite-awareness (network-aware initialization flag), its |
| 18 |
* own admin-notice UX, and is reached only after the tables already exist. |
| 19 |
* |
| 20 |
* Collaborator of ABJ_404_Solution_DatabaseUpgradeBootstrap, which constructs |
| 21 |
* it with the upgrade coordinator (to reach the N-gram upgrade delegate) and |
| 22 |
* the plugin logger. |
| 23 |
*/ |
| 24 |
class ABJ_404_Solution_DatabaseUpgradeNGramCacheInitializer { |
| 25 |
|
| 26 |
/** @var ABJ_404_Solution_DatabaseUpgradeCoordinator */ |
| 27 |
private $coordinator; |
| 28 |
|
| 29 |
/** @var ABJ_404_Solution_Logging */ |
| 30 |
private $logger; |
| 31 |
|
| 32 |
/** |
| 33 |
* @param ABJ_404_Solution_DatabaseUpgradeCoordinator $coordinator |
| 34 |
* @param ABJ_404_Solution_Logging $logger |
| 35 |
*/ |
| 36 |
public function __construct(ABJ_404_Solution_DatabaseUpgradeCoordinator $coordinator, |
| 37 |
ABJ_404_Solution_Logging $logger) { |
| 38 |
$this->coordinator = $coordinator; |
| 39 |
$this->logger = $logger; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* If the N-gram cache has never been built, schedule an async rebuild via |
| 44 |
* WP-Cron and (on a version upgrade) show a "build scheduled" admin notice. |
| 45 |
* No-op once the cache is already initialized. |
| 46 |
* |
| 47 |
* @param bool $updatingToNewVersion Whether this run is a version upgrade |
| 48 |
* (controls whether the admin notice is surfaced). |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function scheduleRebuildIfUninitialized($updatingToNewVersion) { |
| 52 |
// One-time N-gram cache initialization (async via WP-Cron to prevent blocking) |
| 53 |
// MULTISITE: Use network-aware option getter to check initialization status |
| 54 |
if ($this->coordinator->nGramUpgrade()->getNetworkAwareOption('abj404_ngram_cache_initialized') !== '1') { |
| 55 |
$this->logger->debugMessage("N-gram cache not initialized. Scheduling background build..."); |
| 56 |
|
| 57 |
// Schedule async rebuild via WP-Cron instead of blocking activation |
| 58 |
$this->coordinator->nGramUpgrade()->scheduleNGramCacheRebuild(); |
| 59 |
|
| 60 |
// Show admin notice that build is scheduled |
| 61 |
if ($updatingToNewVersion && function_exists('add_settings_error')) { |
| 62 |
$context = is_multisite() && $this->coordinator->nGramUpgrade()->isNetworkActivated() ? ' across all sites in the network' : ''; |
| 63 |
$message = sprintf( |
| 64 |
__('404 Solution: N-gram spell check cache is being built in the background%s to optimize performance. This may take a few minutes on large sites.', '404-solution'), |
| 65 |
$context |
| 66 |
); |
| 67 |
add_settings_error('abj404_settings', 'ngram_cache_scheduled', $message, 'updated'); |
| 68 |
} |
| 69 |
|
| 70 |
$this->logger->infoMessage("N-gram cache rebuild scheduled via WP-Cron."); |
| 71 |
} else { |
| 72 |
$this->logger->debugMessage("N-gram cache already initialized. Skipping rebuild."); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|