Files
prautoblogger/includes/class-deactivator.php
peptiderepo b5f77765f8 feat(cost): persist run_id on generated posts + back-populate for v0.8.1 reaper
The orphan-research-row reaper (upcoming commit) needs a cheap way to
answer "which posts belong to this run_id?" when amortizing a stranded
`llm_research` row back to its siblings. Relying on the generation_log
post_id column is circular: if the pipeline died before amortize_research
ran, we'd trust the same table we're trying to repair.

- `Publisher::build_meta()` now accepts `?string $run_id` and writes
  `_prautoblogger_run_id => $run_id` when non-empty. Null/empty preserves
  existing null-safe legacy behaviour.
- `Publisher::create_post()` forwards the $run_id it already receives
  into build_meta — one-line plumbing, no new parameters at call sites.
- `Activator::backpopulate_run_id_meta_v081()` runs once per site,
  gated by `prautoblogger_migrated_run_id_meta_v081` option. Walks
  `wp_prautoblogger_generation_log`, pairs (post_id, min(run_id)),
  writes `_prautoblogger_run_id` on any post that doesn't already have
  it. Uses `metadata_exists()` so it's a no-op on re-run.
- Activator also schedules the new daily `prautoblogger_reap_orphan_research_rows`
  cron at 03:15 — 15 min after the primary generation cron — so live
  pipelines have time to finish amortizing cleanly before the reaper scans.
- Deactivator unschedules the new hook alongside the existing cron hooks.

No user-visible behaviour change yet. The reaper handler ships in the
next commit; this one just ensures it will have the data + schedule it
needs when it lands.

Part of the v0.8.1 bundle (thread research-cost-reaper).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 02:21:13 +07:00

69 lines
1.6 KiB
PHP
Executable File

<?php
declare(strict_types=1);
/**
* Handles plugin deactivation: clears scheduled cron events and transients.
*
* Note: does NOT delete data or options — that happens in uninstall.php.
* Deactivation should be reversible (user can reactivate and pick up where they left off).
*
* Triggered by: WordPress `register_deactivation_hook` in prautoblogger.php.
* Dependencies: None.
*
* @see class-activator.php — Sets up what this class tears down.
* @see uninstall.php — Permanent data removal on plugin deletion.
*/
class PRAutoBlogger_Deactivator {
/**
* Run deactivation tasks.
*
* Side effects: removes WP-Cron events and deletes transients.
*
* @return void
*/
public static function deactivate(): void {
self::clear_cron();
self::clear_transients();
}
/**
* Remove all scheduled cron events belonging to this plugin.
*
* @return void
*/
private static function clear_cron(): void {
$hooks = [
'prautoblogger_daily_generation',
'prautoblogger_collect_metrics',
'prautoblogger_reap_orphan_research_rows',
];
foreach ( $hooks as $hook ) {
$timestamp = wp_next_scheduled( $hook );
if ( false !== $timestamp ) {
wp_unschedule_event( $timestamp, $hook );
}
}
}
/**
* Delete all plugin transients.
*
* Side effects: deletes transients from wp_options.
*
* @return void
*/
private static function clear_transients(): void {
$transients = [
'prautoblogger_reddit_token',
'prautoblogger_generation_lock',
'prautoblogger_openrouter_models',
];
foreach ( $transients as $transient ) {
delete_transient( $transient );
}
}
}