refactor(zones): 回填 Zone_Router + Routing_Predicate(PR-C)

A v3.0.1/v3.4.0 的架構深化,B 完全缺席:

新增
- includes/zones/class-tmdo-zone-router.php:module_name / read / write /
  delete_field / delete_post 五個 static 分派點
- includes/class-tmdo-routing-predicate.php:entity_bridge_owns(含
  request-level cache,B 原本沒有)+ should_{write,read,query}_from_zone
  + flush_cache

改寫
- Sync_Bridge 改用兩者,移除 5 個 private wrapper(get_zone_module /
  read_from_zone / write_to_zone / delete_from_zone / is_owned_by_entity_bridge)
  與 44 行 inline cleanup_post → Zone_Router::delete_post 一行(400→269 行)
- Query_Router 抽出 extract_hot_clauses() public static,pre_get_posts 變薄殼
- 收斂 5 處重複的 'hot_'/'cold_' . sanitize_key()(rest-api ×3、
  hot/cold-migration ×2)
- back-compat 補 WPDO_Zone_Router / WPDO_Routing_Predicate alias

測試
- 移植 ZoneRouterTest(250 行)+ RoutingPredicateTest(85 行)
- SyncBridgeEntityGuardTest 的 setUp/tearDownAfterClass 補 flush_cache(),
  否則 entity_bridge_cache 會跨測試污染(A v3.4.6 踩過同一個坑)

unit 409 / integration 398 GREEN

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
This commit is contained in:
2026-07-31 05:47:08 +08:00
parent 98774c5035
commit f187ac5401
14 changed files with 685 additions and 211 deletions
+26 -167
View File
@@ -2,7 +2,7 @@
/**
* Zone-aware dual-write dispatcher for WordPress metadata API.
*
* @package WP_Data_Optimizer
* @package TMDO
*/
if ( ! defined( 'ABSPATH' ) ) {
@@ -40,6 +40,11 @@ class TMDO_Sync_Bridge {
/**
* Register all metadata hooks.
*
* Write hooks (update/add/delete) are always registered because Sync Bridge
* handles zone-registered keys that may not be tracked by the Entity Bridge.
* The per-call is_owned_by_entity_bridge() guard (cached) prevents double-writes
* for keys that the Hook Bus owns when post mode is dual_write or higher.
*/
public function register_hooks(): void {
add_filter( 'get_post_metadata', array( $this, 'intercept_get' ), 10, 5 );
@@ -75,17 +80,16 @@ class TMDO_Sync_Bridge {
}
// P1-24: defer to Hook Bus for reads when post entity is dual_write or higher.
if ( self::is_owned_by_entity_bridge( $meta_key ) ) {
if ( TMDO_Routing_Predicate::entity_bridge_owns( $meta_key ) ) {
return $value;
}
$module = $this->get_zone_module( $field['zone'], $post_type );
if ( ! TMDO_Feature_Flags::is_read_custom( $module ) ) {
if ( ! TMDO_Routing_Predicate::should_read_from_zone( $post_type, $field['zone'] ) ) {
return $value;
}
try {
$zone_value = $this->read_from_zone( $field, $post_id, $post_type, $meta_key );
$zone_value = TMDO_Zone_Router::read( $field, $post_id, $post_type, $meta_key );
if ( null === $zone_value ) {
return $value;
@@ -96,7 +100,7 @@ class TMDO_Sync_Bridge {
return array( $zone_value );
} catch ( \Throwable $e ) {
TMDO_Logger::error( $module, 'get_post_metadata', $e->getMessage() );
TMDO_Logger::error( TMDO_Zone_Router::module_name( $field['zone'], $post_type ), 'get_post_metadata', $e->getMessage() );
return $value;
}
}
@@ -121,11 +125,9 @@ class TMDO_Sync_Bridge {
return $check;
}
// v2.9.2 Entity Bridge guard: when post mode is dual_write or higher
// AND the key is registered in the new Entity Registry, the unified
// Hook Bus is the source of truth — Sync_Bridge must not also write
// to the legacy zone table to avoid duplicate flat writes.
if ( self::is_owned_by_entity_bridge( $meta_key ) ) {
// v2.9.2 Entity Bridge guard — see ADR-001. Hook Bus owns this key when post
// mode is dual_write or higher AND the key is in Entity Registry.
if ( TMDO_Routing_Predicate::entity_bridge_owns( $meta_key ) ) {
return $check;
}
@@ -134,16 +136,15 @@ class TMDO_Sync_Bridge {
return $check;
}
$module = $this->get_zone_module( $field['zone'], $post_type );
if ( ! TMDO_Feature_Flags::is_write_active( $module ) ) {
if ( ! TMDO_Routing_Predicate::should_write_to_zone( $post_type, $field['zone'] ) ) {
return $check;
}
// Write to zone table (non-fatal on failure).
try {
$this->write_to_zone( $field, $post_id, $post_type, $meta_key, $meta_value );
TMDO_Zone_Router::write( $field, $post_id, $post_type, $meta_key, $meta_value );
} catch ( \Throwable $e ) {
TMDO_Logger::error( $module, 'update_post_metadata', $e->getMessage() );
TMDO_Logger::error( TMDO_Zone_Router::module_name( $field['zone'], $post_type ), 'update_post_metadata', $e->getMessage() );
}
// Return null — let WordPress proceed with native postmeta write.
@@ -152,34 +153,6 @@ class TMDO_Sync_Bridge {
return $check;
}
/**
* Whether the given post meta_key is now owned by the Entity Bridge,
* meaning Sync_Bridge should defer to the unified Hook Bus and skip its
* zone write to avoid duplicate flat writes.
*
* Returns true only when ALL of:
* - TMDO_Mode_Manager and TMDO_Entity_Registry classes exist
* - post mode is dual_write or higher (writes_to_flat returns true)
* - the key is registered for entity_type=post
*
* Default post mode is `disabled`, so this returns false in all
* environments that have not opted in to Entity Bridge — keeping the
* legacy Sync_Bridge → zone path unchanged.
*
* @param string $meta_key Meta key being written.
* @return bool
* @since 2.9.2
*/
private static function is_owned_by_entity_bridge( string $meta_key ): bool {
if ( ! class_exists( 'TMDO_Mode_Manager' ) || ! class_exists( 'TMDO_Entity_Registry' ) ) {
return false;
}
if ( ! TMDO_Mode_Manager::writes_to_flat( 'post' ) ) {
return false;
}
return null !== TMDO_Entity_Registry::get_field( 'post', $meta_key );
}
/**
* Intercept add_post_meta — dual-write to zone table.
*
@@ -200,8 +173,8 @@ class TMDO_Sync_Bridge {
return $check;
}
// v2.9.2 Entity Bridge guard — see is_owned_by_entity_bridge() docblock.
if ( self::is_owned_by_entity_bridge( $meta_key ) ) {
// Entity Bridge guard — see ADR-001.
if ( TMDO_Routing_Predicate::entity_bridge_owns( $meta_key ) ) {
return $check;
}
@@ -210,15 +183,14 @@ class TMDO_Sync_Bridge {
return $check;
}
$module = $this->get_zone_module( $field['zone'], $post_type );
if ( ! TMDO_Feature_Flags::is_write_active( $module ) ) {
if ( ! TMDO_Routing_Predicate::should_write_to_zone( $post_type, $field['zone'] ) ) {
return $check;
}
try {
$this->write_to_zone( $field, $post_id, $post_type, $meta_key, $meta_value );
TMDO_Zone_Router::write( $field, $post_id, $post_type, $meta_key, $meta_value );
} catch ( \Throwable $e ) {
TMDO_Logger::error( $module, 'add_post_metadata', $e->getMessage() );
TMDO_Logger::error( TMDO_Zone_Router::module_name( $field['zone'], $post_type ), 'add_post_metadata', $e->getMessage() );
}
return $check;
@@ -251,19 +223,18 @@ class TMDO_Sync_Bridge {
}
// P1-24: skip zone cleanup when Hook Bus owns this key (aeav_only blocks native delete).
if ( self::is_owned_by_entity_bridge( $meta_key ) ) {
if ( TMDO_Routing_Predicate::entity_bridge_owns( $meta_key ) ) {
return;
}
$module = $this->get_zone_module( $field['zone'], $post_type );
if ( ! TMDO_Feature_Flags::is_write_active( $module ) ) {
if ( ! TMDO_Routing_Predicate::should_write_to_zone( $post_type, $field['zone'] ) ) {
return;
}
try {
$this->delete_from_zone( $field, $post_id, $post_type, $meta_key );
TMDO_Zone_Router::delete_field( $field, $post_id, $post_type, $meta_key );
} catch ( \Throwable $e ) {
TMDO_Logger::error( $module, 'deleted_post_meta', $e->getMessage() );
TMDO_Logger::error( TMDO_Zone_Router::module_name( $field['zone'], $post_type ), 'deleted_post_meta', $e->getMessage() );
}
}
@@ -278,44 +249,9 @@ class TMDO_Sync_Bridge {
if ( ! $post_type ) {
return;
}
$registry = TMDO_Schema_Registry::instance();
// Clean Hot zone.
if ( ! empty( $registry->get_hot_columns( $post_type ) ) ) {
try {
TMDO_Zone_Hot::delete( $post_id, $post_type );
} catch ( \Throwable $e ) {
TMDO_Logger::error( 'hot_' . $post_type, 'before_delete_post', $e->getMessage() );
}
}
// Clean Cold zone.
if ( ! empty( $registry->get_cold_meta_keys( $post_type ) ) ) {
try {
TMDO_Zone_Cold::delete( $post_id, $post_type );
} catch ( \Throwable $e ) {
TMDO_Logger::error( 'cold_' . $post_type, 'before_delete_post', $e->getMessage() );
}
}
// Clean Warm zone.
try {
TMDO_Zone_Warm::delete_all( $post_id );
} catch ( \Throwable $e ) {
TMDO_Logger::error( 'warm', 'before_delete_post', $e->getMessage() );
}
// Clean Archive zone.
try {
TMDO_Zone_Archive::delete( $post_id );
} catch ( \Throwable $e ) {
TMDO_Logger::error( 'archive', 'before_delete_post', $e->getMessage() );
}
TMDO_Zone_Router::delete_post( $post_id, $post_type );
}
// ── Private helpers ───────────────────────────────────────────────────
/**
* Get a registered field with a request-level static cache.
*
@@ -330,81 +266,4 @@ class TMDO_Sync_Bridge {
}
return self::$field_cache[ $cache_key ];
}
/**
* Derive module name from zone + post_type for feature flag lookups.
*
* @param string $zone Zone identifier (hot, cold, warm, archive).
* @param string $post_type Post type.
* @return string Module name.
*/
private function get_zone_module( string $zone, string $post_type ): string {
return match ( $zone ) {
'hot' => 'hot_' . sanitize_key( $post_type ),
'cold' => 'cold_' . sanitize_key( $post_type ),
'warm' => 'warm',
'archive' => 'archive',
default => 'unknown',
};
}
/**
* Read a value from the appropriate zone.
*
* @param array $field Field definition from Schema Registry.
* @param int $post_id Post ID.
* @param string $post_type Post type.
* @param string $meta_key Meta key.
* @return mixed Value from zone or null.
*/
private function read_from_zone( array $field, int $post_id, string $post_type, string $meta_key ): mixed {
return match ( $field['zone'] ) {
'hot' => TMDO_Zone_Hot::get( $post_id, $post_type, $field['column'] ),
'cold' => TMDO_Zone_Cold::get( $post_id, $post_type, $meta_key ),
'warm' => TMDO_Zone_Warm::get( $post_id, $meta_key ),
default => null,
};
}
/**
* Write a value to the appropriate zone.
*
* @param array $field Field definition from Schema Registry.
* @param int $post_id Post ID.
* @param string $post_type Post type.
* @param string $meta_key Meta key.
* @param mixed $value Value to write.
* @return void
*/
private function write_to_zone( array $field, int $post_id, string $post_type, string $meta_key, mixed $value ): void {
match ( $field['zone'] ) {
'hot' => TMDO_Zone_Hot::set( $post_id, $post_type, $field['column'], $value ),
'cold' => TMDO_Zone_Cold::set( $post_id, $post_type, $meta_key, $value ),
'warm' => TMDO_Zone_Warm::set(
$post_id,
$meta_key,
is_string( $value ) ? $value : wp_json_encode( $value ),
$field['ttl'] ?? null
),
default => null,
};
}
/**
* Delete a value from the appropriate zone.
*
* @param array $field Field definition from Schema Registry.
* @param int $post_id Post ID.
* @param string $post_type Post type.
* @param string $meta_key Meta key.
* @return void
*/
private function delete_from_zone( array $field, int $post_id, string $post_type, string $meta_key ): void {
match ( $field['zone'] ) {
'hot' => TMDO_Zone_Hot::set( $post_id, $post_type, $field['column'], null ),
'cold' => TMDO_Zone_Cold::remove( $post_id, $post_type, $meta_key ),
'warm' => TMDO_Zone_Warm::delete( $post_id, $meta_key ),
default => null,
};
}
}