Files
2meet-data-optimizer/includes/zones/class-tmdo-zone-warm.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

194 lines
5.0 KiB
PHP

<?php
/**
* Zone B (Warm) handler for KV table with optional TTL.
*
* @package WP_Data_Optimizer
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Zone B (Warm) handler — KV table with optional TTL auto-cleanup.
*
* Single table: wpdo_warm
* Structure: post_id + meta_key + meta_value + expires_at
*
* Use for transient-like data that benefits from DB-backed persistence
* but doesn't need to live forever (e.g. cached computations, temporary flags).
*
* Expired entries are cleaned up by the wpdo_warm_cleanup cron (hourly).
*/
class TMDO_Zone_Warm {
/**
* Get the warm table name.
*/
public static function table(): string {
return TMDO_DB::table( 'wpdo_warm' );
}
/**
* Read a value from the warm zone.
*
* Returns null if not found or if expired.
*
* @param int $post_id Post ID.
* @param string $meta_key Meta key.
* @return string|null
*/
public static function get( int $post_id, string $meta_key ): ?string {
global $wpdb;
$table = self::table();
$now = TMDO_DB::now();
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from TMDO_Zone_Warm::table() via TMDO_DB::table().
$val = $wpdb->get_var(
$wpdb->prepare(
"SELECT meta_value FROM `{$table}`
WHERE post_id = %d AND meta_key = %s
AND (expires_at IS NULL OR expires_at > %s)
LIMIT 1",
$post_id,
$meta_key,
$now
)
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
return $val;
}
/**
* Read all warm values for a post.
*
* @param int $post_id Post ID.
* @return array<string, string> meta_key => meta_value pairs.
*/
public static function get_all( int $post_id ): array {
global $wpdb;
$table = self::table();
$now = TMDO_DB::now();
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from TMDO_Zone_Warm::table() via TMDO_DB::table().
$rows = $wpdb->get_results(
$wpdb->prepare(
"SELECT meta_key, meta_value FROM `{$table}`
WHERE post_id = %d AND (expires_at IS NULL OR expires_at > %s)",
$post_id,
$now
),
ARRAY_A
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$result = array();
foreach ( $rows ?: array() as $row ) {
$result[ $row['meta_key'] ] = $row['meta_value'];
}
return $result;
}
/**
* Set a value in the warm zone with optional TTL.
*
* @param int $post_id Post ID.
* @param string $meta_key Meta key.
* @param string $value Value to store.
* @param int|null $ttl TTL in seconds. Null = no expiry.
*/
public static function set( int $post_id, string $meta_key, string $value, ?int $ttl = null ): void {
global $wpdb;
$table = self::table();
$now = TMDO_DB::now();
$expires_at = null;
if ( $ttl && $ttl > 0 ) {
$expires_at = gmdate( 'Y-m-d H:i:s', time() + $ttl );
}
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from TMDO_Zone_Warm::table() via TMDO_DB::table().
$existing = $wpdb->get_var(
$wpdb->prepare(
"SELECT id FROM `{$table}` WHERE post_id = %d AND meta_key = %s LIMIT 1",
$post_id,
$meta_key
)
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( $existing ) {
$update_data = array( 'meta_value' => $value );
$update_format = array( '%s' );
if ( null !== $expires_at ) {
$update_data['expires_at'] = $expires_at;
$update_format[] = '%s';
}
$wpdb->update( $table, $update_data, array( 'id' => (int) $existing ), $update_format, array( '%d' ) );
} else {
$wpdb->insert(
$table,
array(
'post_id' => $post_id,
'meta_key' => $meta_key,
'meta_value' => $value,
'expires_at' => $expires_at,
'created_at' => $now,
),
array( '%d', '%s', '%s', $expires_at ? '%s' : null, '%s' )
);
}
}
/**
* Delete a specific key from the warm zone.
*
* @param int $post_id Post ID.
* @param string $meta_key Meta key to delete.
* @return void
*/
public static function delete( int $post_id, string $meta_key ): void {
global $wpdb;
$wpdb->delete(
self::table(),
array(
'post_id' => $post_id,
'meta_key' => $meta_key,
),
array( '%d', '%s' )
);
}
/**
* Delete all warm entries for a post.
*
* @param int $post_id Post ID.
* @return void
*/
public static function delete_all( int $post_id ): void {
global $wpdb;
$wpdb->delete( self::table(), array( 'post_id' => $post_id ), array( '%d' ) );
}
/**
* Purge all expired entries (called by cron).
*
* @return int Number of rows deleted.
*/
public static function purge_expired(): int {
global $wpdb;
$table = self::table();
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from TMDO_Zone_Warm::table() via TMDO_DB::table().
return (int) $wpdb->query(
$wpdb->prepare(
"DELETE FROM `{$table}` WHERE expires_at IS NOT NULL AND expires_at < %s",
TMDO_DB::now()
)
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
}