76c01e44df
對齊 A v3.2.0。型別強制會把隱式轉換變成 TypeError,所以一次全檔加入 並跑完整測試(unit 451 / integration 398 全綠,無迴歸)。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
145 lines
4.6 KiB
PHP
145 lines
4.6 KiB
PHP
<?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
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
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() );
|
|
}
|
|
}
|
|
}
|