Files
prautoblogger/includes/class-deactivator.php
peptiderepo def070d15a feat: Live Runware model catalog sync — v0.15.0
Implements daily WP-Cron sync from Runware's /v1/models endpoint with 24h TTL caching,
on-demand AJAX refresh button in PRAdmin (Images settings), smart fallback logic, and
pricing merge from the authoritative cost table.

## Key additions:
- class-runware-model-catalog.php: ~300 lines, fetches/normalizes/caches models,
  exposes sync()/get_models()/get_last_synced_at()/is_stale() public API
- AJAX handler: prautoblogger_sync_runware_models_now with nonce + manage_options check
- Daily cron registered on activation, unscheduled on deactivation
- Caching strategy: <24h fresh → use cache; stale → sync; sync fails → fallback
- Pricing merge: costs from class-runware-image-pricing.php injected on every sync
- Fallback never returns empty array (hardcoded 15-model list)
- Unit tests: sync success/failure, stale cache, fallback behavior, pricing merge

## File changes:
- class-image-model-registry.php: refactored to delegate Runware to catalog,
  extracted get_runware_fallback_models() and get_openrouter_models()
- class-settings-fields-extended.php: added runware_catalog_sync field type
  for status display + Sync button in Images settings section
- class-prautoblogger.php: registered daily cron + AJAX hook
- class-activator.php: schedule prautoblogger_sync_runware_models on activation
- class-deactivator.php: unschedule on deactivation
- class-ajax-handlers.php: added on_ajax_sync_runware_models_now() handler
- class-runware-image-pricing.php: added @see cross-reference to catalog
- ARCHITECTURE.md: updated Runware row in External API Integrations table
- prautoblogger.php: version bump to 0.15.0
- CHANGELOG.md: detailed entry describing the fix and why it was necessary

Fixes the pattern from v0.13.8 and v0.14.0 where hardcoded Runware model
expansion without pricing class updates led to silent fallback-to-schnell bugs.
2026-04-26 04:27:17 +00:00

72 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
/**
* phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- class naming convention differs from WordPress standard
*
* 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 = array(
'prautoblogger_daily_generation',
'prautoblogger_collect_metrics',
'prautoblogger_reap_orphan_research_rows',
'prautoblogger_sync_runware_models',
);
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 = array(
'prautoblogger_reddit_token',
'prautoblogger_generation_lock',
'prautoblogger_openrouter_models',
);
foreach ( $transients as $transient ) {
delete_transient( $transient );
}
}
}