Files
2meet-data-optimizer/includes/migration/class-tmdo-archive-migration.php
T
wpdev d36bb954d1 chore: initial snapshot of 2meet-data-optimizer v0.1.0
Baseline before backporting wp-data-optimizer v3.0.1-v3.4.6.

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

169 lines
4.0 KiB
PHP

<?php
/**
* Zone D (Archive) migration for postmeta from trashed/old posts.
*
* @package WP_Data_Optimizer
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Zone D (Archive) migration — postmeta from trashed/old posts → wpdo_archive.
*
* Unlike other zone migrations that copy specific registered meta_keys,
* the archive migration sweeps ALL postmeta for posts that meet archival criteria:
* - Post is in 'trash' status
* - Post was last modified more than $days ago (default: 90 days)
*
* Data is optionally gzip-compressed before storage.
*/
class TMDO_Archive_Migration extends TMDO_Migration_Base {
/**
* Minimum age in days for archival.
*
* @var int
*/
private int $days;
/**
* Whether to gzip-compress archived values.
*
* @var bool
*/
private bool $compress;
/**
* Constructor.
*
* @param int $days Minimum age in days for archival.
* @param bool $compress Whether to gzip-compress archived values.
*/
public function __construct( int $days = 90, bool $compress = true ) {
$this->days = $days;
$this->compress = $compress;
}
/**
* Returns the module identifier.
*
* @return string Module name.
*/
public function get_module(): string {
return 'archive';
}
/**
* Returns the zone identifier.
*
* @return string Zone name.
*/
public function get_zone(): string {
return 'archive';
}
/**
* Returns the total number of archive-eligible postmeta rows.
*
* @return int Total row count.
*/
protected function count_source(): int {
global $wpdb;
$cutoff = $this->get_cutoff();
return (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*)
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE p.post_status = 'trash'
AND p.post_modified_gmt < %s",
$cutoff
)
);
}
/**
* Migrates one batch of eligible postmeta rows to the archive table.
*
* @param int $offset Starting row offset.
* @return int Number of rows processed.
*/
protected function migrate_batch( int $offset ): int {
global $wpdb;
$cutoff = $this->get_cutoff();
$rows = $wpdb->get_results(
$wpdb->prepare(
"SELECT pm.meta_id, pm.post_id, pm.meta_key, pm.meta_value, p.post_type
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE p.post_status = 'trash'
AND p.post_modified_gmt < %s
ORDER BY pm.meta_id ASC
LIMIT %d OFFSET %d",
$cutoff,
self::BATCH_SIZE,
$offset
),
ARRAY_A
);
if ( empty( $rows ) ) {
return 0;
}
$entries = array();
foreach ( $rows as $row ) {
$entries[] = array(
'post_id' => $row['post_id'],
'post_type' => $row['post_type'],
'meta_key' => $row['meta_key'],
'meta_value' => $row['meta_value'],
'meta_id' => $row['meta_id'],
);
}
TMDO_Zone_Archive::archive_batch( $entries, $this->compress );
return count( $rows );
}
/**
* Verifies that archive table has rows when eligible source rows exist.
*
* @return bool True if verification passes.
*/
public function verify_counts(): bool {
global $wpdb;
$source = $this->count_source();
$table = TMDO_Zone_Archive::table();
$target = (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$table}`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from TMDO_Zone_Archive::table()
// Archive may have more rows than current source (trashed posts may have
// been permanently deleted after archival). So we just check target > 0
// when source is 0, or target >= some threshold.
if ( 0 === $source ) {
return true;
}
return $target >= $source;
}
// ── Private helpers ───────────────────────────────────────────────────
/**
* Returns the cutoff datetime string for archival eligibility.
*
* @return string MySQL datetime string.
*/
private function get_cutoff(): string {
return gmdate( 'Y-m-d H:i:s', time() - ( $this->days * DAY_IN_SECONDS ) );
}
}