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:
@@ -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'] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user