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:
@@ -111,6 +111,8 @@ $tmdo_class_aliases = array(
|
||||
'TMDO_Zone_Hot' => 'WPDO_Zone_Hot',
|
||||
'TMDO_Zone_Warm' => 'WPDO_Zone_Warm',
|
||||
'TMDO_Zone_Cold' => 'WPDO_Zone_Cold',
|
||||
'TMDO_Zone_Router' => 'WPDO_Zone_Router',
|
||||
'TMDO_Routing_Predicate' => 'WPDO_Routing_Predicate',
|
||||
'TMDO_Zone_Archive' => 'WPDO_Zone_Archive',
|
||||
'TMDO_Interceptor_Base' => 'WPDO_Interceptor_Base',
|
||||
'TMDO_Query_Interceptor_Base' => 'WPDO_Query_Interceptor_Base',
|
||||
|
||||
@@ -537,7 +537,7 @@ class TMDO_REST_API {
|
||||
$order = strtoupper( (string) ( $request->get_param( 'order' ) ?? 'DESC' ) );
|
||||
$offset = ( $page - 1 ) * $per_page;
|
||||
|
||||
$module = 'hot_' . sanitize_key( $post_type );
|
||||
$module = TMDO_Zone_Router::module_name( 'hot', $post_type );
|
||||
|
||||
if ( TMDO_Feature_Flags::is_read_custom( $module ) ) {
|
||||
return $this->listings_from_zone_a( $post_type, $per_page, $offset, $orderby, $order, $request );
|
||||
@@ -571,7 +571,7 @@ class TMDO_REST_API {
|
||||
);
|
||||
|
||||
// Zone A.
|
||||
$hot_module = 'hot_' . sanitize_key( $post_type );
|
||||
$hot_module = TMDO_Zone_Router::module_name( 'hot', $post_type );
|
||||
if ( TMDO_Feature_Flags::is_read_custom( $hot_module ) ) {
|
||||
$hot = TMDO_Zone_Hot::get_row( $post_id, $post_type );
|
||||
if ( $hot ) {
|
||||
@@ -585,7 +585,7 @@ class TMDO_REST_API {
|
||||
}
|
||||
|
||||
// Zone C.
|
||||
$cold_module = 'cold_' . sanitize_key( $post_type );
|
||||
$cold_module = TMDO_Zone_Router::module_name( 'cold', $post_type );
|
||||
if ( TMDO_Feature_Flags::is_read_custom( $cold_module ) ) {
|
||||
$cold = TMDO_Zone_Cold::get_blob( $post_id, $post_type );
|
||||
$data = array_merge( $data, $cold );
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Routing predicate — centralises Zone ownership and activation checks.
|
||||
*
|
||||
* Two recurring predicates previously scattered across callers:
|
||||
*
|
||||
* 1. entity_bridge_owns(): was TMDO_Sync_Bridge::is_owned_by_entity_bridge() —
|
||||
* answers "does the Hook Bus own this meta_key (Zone Bridge must skip it)?".
|
||||
*
|
||||
* 2. should_write_to_zone() / should_read_from_zone() / should_query_from_zone():
|
||||
* were inline TMDO_Zone_Router::module_name() + TMDO_Feature_Flags::is_*()
|
||||
* pairs repeated in Sync_Bridge, Query_Router, and REST API.
|
||||
*
|
||||
* @package TMDO
|
||||
* @since 0.2.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Centralised routing predicate for Zone write/read/query activation checks.
|
||||
*/
|
||||
class TMDO_Routing_Predicate {
|
||||
|
||||
/**
|
||||
* Request-level cache for entity_bridge_owns().
|
||||
*
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
private static array $entity_bridge_cache = array();
|
||||
|
||||
/**
|
||||
* Returns true when the Entity Bridge (Hook Bus) owns writes for $meta_key on
|
||||
* the 'post' entity type — Zone Sync_Bridge must not also write to a Zone table.
|
||||
*
|
||||
* Replaces the former TMDO_Sync_Bridge::is_owned_by_entity_bridge().
|
||||
*
|
||||
* @param string $meta_key Meta key being written.
|
||||
* @return bool
|
||||
*/
|
||||
public static function entity_bridge_owns( string $meta_key ): bool {
|
||||
if ( isset( self::$entity_bridge_cache[ $meta_key ] ) ) {
|
||||
return self::$entity_bridge_cache[ $meta_key ];
|
||||
}
|
||||
|
||||
$result = false;
|
||||
if ( class_exists( 'TMDO_Mode_Manager' )
|
||||
&& class_exists( 'TMDO_Entity_Registry' )
|
||||
&& TMDO_Mode_Manager::writes_to_flat( 'post' )
|
||||
&& null !== TMDO_Entity_Registry::get_field( 'post', $meta_key ) ) {
|
||||
$result = true;
|
||||
}
|
||||
|
||||
self::$entity_bridge_cache[ $meta_key ] = $result;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when Zone dual-write is active for the given post_type + zone.
|
||||
*
|
||||
* Replaces the repeated inline pattern:
|
||||
* $module = TMDO_Zone_Router::module_name( $zone, $post_type );
|
||||
* TMDO_Feature_Flags::is_write_active( $module );
|
||||
*
|
||||
* @param string $post_type Post type slug.
|
||||
* @param string $zone Zone identifier: 'hot', 'cold', 'warm', 'archive'.
|
||||
* @return bool
|
||||
*/
|
||||
public static function should_write_to_zone( string $post_type, string $zone ): bool {
|
||||
return TMDO_Feature_Flags::is_write_active(
|
||||
TMDO_Zone_Router::module_name( $zone, $post_type )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when Zone read-from-custom is active for the given post_type + zone.
|
||||
*
|
||||
* @param string $post_type Post type slug.
|
||||
* @param string $zone Zone identifier.
|
||||
* @return bool
|
||||
*/
|
||||
public static function should_read_from_zone( string $post_type, string $zone ): bool {
|
||||
return TMDO_Feature_Flags::is_read_custom(
|
||||
TMDO_Zone_Router::module_name( $zone, $post_type )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when Zone query-rewrite is active for the given post_type + zone.
|
||||
*
|
||||
* @param string $post_type Post type slug.
|
||||
* @param string $zone Zone identifier.
|
||||
* @return bool
|
||||
*/
|
||||
public static function should_query_from_zone( string $post_type, string $zone ): bool {
|
||||
return TMDO_Feature_Flags::is_query_active(
|
||||
TMDO_Zone_Router::module_name( $zone, $post_type )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush the request-level entity bridge cache.
|
||||
* Call this in test setUp to isolate test cases.
|
||||
*/
|
||||
public static function flush_cache(): void {
|
||||
self::$entity_bridge_cache = array();
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ class TMDO_Cold_Migration extends TMDO_Migration_Base {
|
||||
* @return string Module name.
|
||||
*/
|
||||
public function get_module(): string {
|
||||
return 'cold_' . sanitize_key( $this->post_type );
|
||||
return TMDO_Zone_Router::module_name( 'cold', $this->post_type );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -39,7 +39,7 @@ class TMDO_Hot_Migration extends TMDO_Migration_Base {
|
||||
* @return string Module name.
|
||||
*/
|
||||
public function get_module(): string {
|
||||
return 'hot_' . sanitize_key( $this->post_type );
|
||||
return TMDO_Zone_Router::module_name( 'hot', $this->post_type );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
/**
|
||||
* Zone A Query Router for flat-column hot table rewrites.
|
||||
*
|
||||
* @package WP_Data_Optimizer
|
||||
* @package TMDO
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
@@ -47,6 +47,53 @@ class TMDO_Query_Router {
|
||||
/**
|
||||
* Extract meta_query clauses that can be served by hot tables.
|
||||
*
|
||||
* Separated from the hook callback so it can be called and asserted directly
|
||||
* in unit tests without triggering a full WP_Query lifecycle.
|
||||
*
|
||||
* @param array $raw_meta_query Raw meta_query array from WP_Query.
|
||||
* @param string[] $post_types Post types being queried.
|
||||
* @return array{hot_clauses: array<string, list<array>>, remaining: array} Extracted and leftover clauses.
|
||||
*/
|
||||
public static function extract_hot_clauses( array $raw_meta_query, array $post_types ): array {
|
||||
$registry = TMDO_Schema_Registry::instance();
|
||||
$hot_clauses = array();
|
||||
$remaining = array();
|
||||
|
||||
foreach ( $raw_meta_query as $k => $clause ) {
|
||||
if ( 'relation' === $k || ! is_array( $clause ) || ! isset( $clause['key'] ) ) {
|
||||
$remaining[ $k ] = $clause;
|
||||
continue;
|
||||
}
|
||||
|
||||
$matched = false;
|
||||
foreach ( $post_types as $pt ) {
|
||||
$field = $registry->get_field( $pt, $clause['key'] );
|
||||
if ( $field && 'hot' === $field['zone'] && TMDO_Routing_Predicate::should_query_from_zone( $pt, 'hot' ) ) {
|
||||
$hot_clauses[ $pt ][] = array(
|
||||
'column' => $field['column'],
|
||||
'value' => $clause['value'] ?? '',
|
||||
'compare' => strtoupper( trim( $clause['compare'] ?? '=' ) ),
|
||||
'type' => strtoupper( trim( $clause['type'] ?? 'CHAR' ) ),
|
||||
);
|
||||
$matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $matched ) {
|
||||
$remaining[ $k ] = $clause;
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'hot_clauses' => $hot_clauses,
|
||||
'remaining' => $remaining,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect hot-table clauses in meta_query and stash them for later hooks.
|
||||
*
|
||||
* @param \WP_Query $query The WP_Query object.
|
||||
* @return void
|
||||
*/
|
||||
@@ -65,53 +112,19 @@ class TMDO_Query_Router {
|
||||
return;
|
||||
}
|
||||
|
||||
$registry = TMDO_Schema_Registry::instance();
|
||||
$hot_clauses = array(); // Keyed by post_type.
|
||||
$remaining = array();
|
||||
$result = self::extract_hot_clauses( $raw_meta_query, $post_types );
|
||||
|
||||
foreach ( $raw_meta_query as $k => $clause ) {
|
||||
if ( 'relation' === $k || ! is_array( $clause ) || ! isset( $clause['key'] ) ) {
|
||||
$remaining[ $k ] = $clause;
|
||||
continue;
|
||||
}
|
||||
|
||||
$matched = false;
|
||||
foreach ( $post_types as $pt ) {
|
||||
$field = $registry->get_field( $pt, $clause['key'] );
|
||||
if ( $field && 'hot' === $field['zone'] ) {
|
||||
// Check module is query-active.
|
||||
$module = 'hot_' . sanitize_key( $pt );
|
||||
if ( ! TMDO_Feature_Flags::is_query_active( $module ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$hot_clauses[ $pt ][] = array(
|
||||
'column' => $field['column'],
|
||||
'value' => $clause['value'] ?? '',
|
||||
'compare' => strtoupper( trim( $clause['compare'] ?? '=' ) ),
|
||||
'type' => strtoupper( trim( $clause['type'] ?? 'CHAR' ) ),
|
||||
);
|
||||
$matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $matched ) {
|
||||
$remaining[ $k ] = $clause;
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $hot_clauses ) ) {
|
||||
if ( empty( $result['hot_clauses'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Preserve relation if there are remaining clauses.
|
||||
$remaining = $result['remaining'];
|
||||
if ( isset( $raw_meta_query['relation'] ) && ! isset( $remaining['relation'] ) ) {
|
||||
$remaining['relation'] = $raw_meta_query['relation'];
|
||||
}
|
||||
|
||||
$query->set( 'meta_query', $remaining );
|
||||
$query->set( self::QUERY_VAR, $hot_clauses );
|
||||
$query->set( self::QUERY_VAR, $result['hot_clauses'] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
/**
|
||||
* Central zone dispatch for read, write, delete, and module-name resolution.
|
||||
*
|
||||
* Previously, `'hot_' . sanitize_key($post_type)` was repeated across the REST
|
||||
* API, Query Router, migrations, and Sync Bridge. Moving the canonical form
|
||||
* here gives all callers a single place to look and lets the three dispatch
|
||||
* methods be tested without setting up hook infrastructure.
|
||||
*
|
||||
* @package TMDO
|
||||
* @since 0.2.0
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Central zone dispatch for read, write, delete, and module-name resolution.
|
||||
*/
|
||||
class TMDO_Zone_Router {
|
||||
|
||||
/**
|
||||
* Returns the Feature Flags module name for a zone + post_type pair.
|
||||
*
|
||||
* @param string $zone Zone identifier: 'hot', 'cold', 'warm', 'archive'.
|
||||
* @param string $post_type Post type slug.
|
||||
* @return string Module name suitable for TMDO_Feature_Flags::is_*().
|
||||
*/
|
||||
public static function module_name( 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 field 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 if not found / zone not supported.
|
||||
*/
|
||||
public static function read( 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 field 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
|
||||
*/
|
||||
public static function write( 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 single field 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
|
||||
*/
|
||||
public static function delete_field( 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,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all Zone data for a post being permanently removed.
|
||||
*
|
||||
* Callers only need post_id and post_type; zone enumeration and per-zone
|
||||
* error isolation are handled here. Hot and Cold are skipped when the
|
||||
* post_type has no registered fields in that zone (table may not exist).
|
||||
*
|
||||
* @param int $post_id Post ID being deleted.
|
||||
* @param string $post_type Post type slug.
|
||||
* @return void
|
||||
*/
|
||||
public static function delete_post( int $post_id, string $post_type ): void {
|
||||
$registry = TMDO_Schema_Registry::instance();
|
||||
|
||||
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, 'delete_post', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
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, 'delete_post', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
TMDO_Zone_Warm::delete_all( $post_id );
|
||||
} catch ( \Throwable $e ) {
|
||||
TMDO_Logger::error( 'warm', 'delete_post', $e->getMessage() );
|
||||
}
|
||||
|
||||
try {
|
||||
TMDO_Zone_Archive::delete( $post_id );
|
||||
} catch ( \Throwable $e ) {
|
||||
TMDO_Logger::error( 'archive', 'delete_post', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user