| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Handles action 'rebuildNgramCache': schedules a background WP-cron job to |
| 9 |
* rebuild the n-gram spelling cache. Per-user rate limit via transient |
| 10 |
* (10-second window) so an impatient admin can't queue dozens of rebuilds. |
| 11 |
* |
| 12 |
* Behavior preserved verbatim from PluginLogicAdminActions::handlePluginAction |
| 13 |
* branch (covered by NgramCacheRebuildActionTest). |
| 14 |
*/ |
| 15 |
class ABJ_404_Solution_RebuildNgramCacheHandler implements ABJ_404_Solution_AdminActionHandlerInterface { |
| 16 |
|
| 17 |
public function nonceAction(): string { |
| 18 |
return 'abj404_rebuildNgramCache'; |
| 19 |
} |
| 20 |
|
| 21 |
public function nonceArg(): string { |
| 22 |
return '_wpnonce'; |
| 23 |
} |
| 24 |
|
| 25 |
public function useCheckAdminReferer(): bool { |
| 26 |
return true; |
| 27 |
} |
| 28 |
|
| 29 |
public function handle(string $action, string &$sub): string { |
| 30 |
$userId = get_current_user_id(); |
| 31 |
$transientKey = 'abj404_ngram_rebuild_request_' . $userId; |
| 32 |
$recentRequest = get_transient($transientKey); |
| 33 |
|
| 34 |
if ($recentRequest) { |
| 35 |
return __('N-gram cache rebuild is already scheduled or in progress. Please wait for it to complete.', '404-solution'); |
| 36 |
} |
| 37 |
|
| 38 |
// allow-cache-empty: timestamp marker throttles duplicate rebuild clicks; not a cached query payload. |
| 39 |
set_transient($transientKey, abj_clock()->now(), 10); |
| 40 |
|
| 41 |
$dbUpgrades = abj_service('database_upgrades'); |
| 42 |
$nGramUpgrade = $dbUpgrades->components()->nGramUpgrade(); |
| 43 |
$scheduled = $nGramUpgrade->scheduleNGramCacheRebuild(); |
| 44 |
|
| 45 |
if ($scheduled) { |
| 46 |
return __('N-gram cache rebuild has been scheduled and will run in the background. This may take several minutes on large sites. You can continue using the plugin normally.', '404-solution'); |
| 47 |
} |
| 48 |
|
| 49 |
// Asked of the component that owns the chain rather than probed here. |
| 50 |
// WP-Cron identifies an event by hook AND args, and an in-flight |
| 51 |
// single-site chain reschedules itself carrying its offset as an |
| 52 |
// argument, so the bare no-args probe this used to make answered "not |
| 53 |
// queued" for a rebuild that was queued -- telling the admin to go and |
| 54 |
// check a WP-Cron configuration that was working. |
| 55 |
if ($nGramUpgrade->nGramRebuildIsArmed()) { |
| 56 |
return __('N-gram cache rebuild is already scheduled or in progress. Please wait for it to complete.', '404-solution'); |
| 57 |
} |
| 58 |
return __('Failed to schedule N-gram cache rebuild. Please try again or check your WordPress cron configuration.', '404-solution'); |
| 59 |
} |
| 60 |
} |
| 61 |
|