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:
@@ -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