Files
2meet-data-optimizer/includes/migration/phases/class-tmdo-phase-cleanup.php
T
wpdev 76c01e44df refactor: 全部 128 個生產檔加入 declare(strict_types=1)(PR-H)
對齊 A v3.2.0。型別強制會把隱式轉換變成 TypeError,所以一次全檔加入
並跑完整測試(unit 451 / integration 398 全綠,無迴歸)。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
2026-07-31 06:13:33 +08:00

74 lines
2.1 KiB
PHP

<?php
/**
* Migration phase: remove managed keys from the native meta table.
*
* @package TMDO
* @since 3.0.1
*/
declare(strict_types=1);
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Deletes all managed meta keys from the EAV table after verifying aeav_only mode.
*/
class TMDO_Phase_Cleanup extends TMDO_Migration_Phase_Base {
/**
* Returns the phase slug.
*
* @return string
*/
public function name(): string {
return 'cleanup';
}
/**
* Executes the phase.
*
* @param array $job Job array (by reference).
* @return array{status: string}
* @throws \RuntimeException If mode is not aeav_only or backup is missing.
*/
public function execute( array &$job ): array {
global $wpdb;
if ( ! empty( $job['options']['dry_run'] ) ) {
$this->log( $job, '↪ Cleanup skipped (dry_run)' );
return array( 'status' => 'ok' );
}
$mode = TMDO_Mode_Manager::get( $this->entity_type );
if ( TMDO_Mode_Manager::MODE_AEAV_ONLY !== $mode ) {
throw new \RuntimeException( "Refusing cleanup — mode is {$mode}, must be aeav_only" );
}
if ( ! empty( $job['options']['auto_backup'] ) && empty( $job['backup_path'] ) ) {
throw new \RuntimeException( 'Refusing cleanup — auto_backup requested but no backup_path on record' );
}
$keys = $this->get_managed_keys();
if ( empty( $keys ) ) {
$this->log( $job, '↪ No managed keys to clean' );
return array( 'status' => 'ok' );
}
$adapter = TMDO_Entity_Registry::get_adapter( $this->entity_type );
$meta_table = $adapter->get_native_meta_table();
$placeholders = implode( ',', array_fill( 0, count( $keys ), '%s' ) );
$deleted = (int) $wpdb->query(
$wpdb->prepare( "DELETE FROM `{$meta_table}` WHERE meta_key IN ({$placeholders})", ...$keys )
);
$preflight = TMDO_Migration_Orchestrator::preflight();
$job['metrics']['eav_rows_now'] = $preflight['eav_rows'];
$job['metrics']['ratio_now'] = $preflight['ratio'];
$this->log( $job, sprintf( 'Cleanup: deleted %d EAV row(s); ratio now %s', $deleted, (string) $preflight['ratio'] ) );
return array( 'status' => 'ok' );
}
}