fix(hivepress): detector active-plugin 偵測 + 4 個 interceptor 改繼承核心基底(F2/F3/F4)
F3 detector(A v3.4.5):HivePress addon 不暴露任何 per-addon class/const,
它們是透過 add_filter('hivepress/v1/extensions') 註冊目錄,所以原本的
class_exists / defined 偵測永遠只認得 core → 反 EAV 覆蓋率卡在 1/13。
改以 get_option('active_plugins') 為第一級訊號(新增 active_plugin_files()),
version_for() 對 addon 改讀其主檔 Version: header。覆蓋率回到 7/13。
F2 interceptor:reviews / messages / memberships / requests 改繼承核心的
TMDO_Standard_Post_Interceptor,573 → 344 行。FIELD_MAP 由 private
提升為 public(late static binding 從基底讀取)。
bootstrap 的 require 迴圈加核心版本守衛:這 4 個檔案在舊核心下會於
parse 階段就 fatal,class_exists 守衛來不及。
F4 listing-stats:view 計數改用 Zone_Warm::increment() 原子遞增,
flush 改原子 UPDATE,消除 get()+set() 的 lost-update race。
F5:主檔補 TablePrefix header(打包終檢 §10 schema drift 主路徑)。
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 @@
|
||||
/**
|
||||
* Requests interceptor for HivePress Request posts.
|
||||
*
|
||||
* @package WP_Data_Optimizer
|
||||
* @package TMDO
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
@@ -11,9 +11,10 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
|
||||
/**
|
||||
* Requests interceptor — intercepts HivePress Request reads/writes.
|
||||
*
|
||||
* Operates on hpct_requests table.
|
||||
*/
|
||||
class TMDO_Requests_Interceptor extends TMDO_Interceptor_Base {
|
||||
class TMDO_Requests_Interceptor extends TMDO_Standard_Post_Interceptor {
|
||||
|
||||
/**
|
||||
* Module identifier.
|
||||
@@ -22,110 +23,52 @@ class TMDO_Requests_Interceptor extends TMDO_Interceptor_Base {
|
||||
*/
|
||||
protected string $module = 'requests';
|
||||
|
||||
private const FIELD_MAP = array(
|
||||
/**
|
||||
* Meta key → flat column mapping.
|
||||
*/
|
||||
public const FIELD_MAP = array(
|
||||
'hp_vendor' => 'vendor_id',
|
||||
'hp_listing' => 'listing_id',
|
||||
'hp_budget' => 'budget',
|
||||
);
|
||||
|
||||
/**
|
||||
* Registers WordPress hooks for this interceptor.
|
||||
* Returns the WordPress post_type slug handled by this interceptor.
|
||||
*
|
||||
* @return void
|
||||
* @return string
|
||||
*/
|
||||
public function register_hooks(): void {
|
||||
add_action( 'wp_insert_post', array( $this, 'action_insert_post' ), 10, 3 );
|
||||
add_filter( 'update_post_metadata', array( $this, 'filter_update_meta' ), 10, 5 );
|
||||
add_action( 'before_delete_post', array( $this, 'action_delete_post' ), 10, 2 );
|
||||
protected function get_post_type(): string {
|
||||
return 'hp_request';
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes request record when a post is deleted.
|
||||
* Returns the table key passed to TMDO_DB::table().
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_key(): string {
|
||||
return 'hpct_requests';
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the INSERT row for a newly created request post.
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @param \WP_Post $post Post object.
|
||||
* @return void
|
||||
* @param string $now Formatted datetime string.
|
||||
* @return array{values: array, formats: array}
|
||||
*/
|
||||
public function action_delete_post( int $post_id, \WP_Post $post ): void {
|
||||
if ( 'hp_request' !== $post->post_type || ! $this->is_active() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
global $wpdb;
|
||||
$wpdb->delete( TMDO_DB::table( 'hpct_requests' ), array( 'post_id' => $post_id ), array( '%d' ) );
|
||||
} catch ( \Throwable $e ) {
|
||||
TMDO_Logger::error( $this->module, 'before_delete_post', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters update_post_metadata for hp_request posts.
|
||||
*
|
||||
* @param mixed $check Whether to short-circuit.
|
||||
* @param int $post_id Post ID.
|
||||
* @param string $meta_key Meta key.
|
||||
* @param mixed $meta_value Meta value.
|
||||
* @param mixed $prev_value Previous meta value.
|
||||
* @return mixed Filtered check value.
|
||||
*/
|
||||
public function filter_update_meta( $check, int $post_id, string $meta_key, $meta_value, $prev_value ) {
|
||||
if ( 'hp_request' !== get_post_type( $post_id ) || ! isset( self::FIELD_MAP[ $meta_key ] ) ) {
|
||||
return $check;
|
||||
}
|
||||
|
||||
if ( ! $this->is_active() ) {
|
||||
return $check;
|
||||
}
|
||||
|
||||
try {
|
||||
global $wpdb;
|
||||
$col = self::FIELD_MAP[ $meta_key ];
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
'UPDATE ' . TMDO_DB::table( 'hpct_requests' ) . " SET `{$col}` = %s, updated_at = %s WHERE post_id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared -- Table name from TMDO_DB::table(); column validated by FIELD_MAP constant.
|
||||
$meta_value,
|
||||
TMDO_DB::now(),
|
||||
$post_id
|
||||
)
|
||||
);
|
||||
} catch ( \Throwable $e ) {
|
||||
TMDO_Logger::error( $this->module, 'update_post_metadata', $e->getMessage() );
|
||||
}
|
||||
|
||||
return $check;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a new request record when a post is inserted.
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @param \WP_Post $post Post object.
|
||||
* @param bool $update Whether this is an update.
|
||||
* @return void
|
||||
*/
|
||||
public function action_insert_post( int $post_id, \WP_Post $post, bool $update ): void {
|
||||
if ( $update || 'hp_request' !== $post->post_type || ! $this->is_active() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
global $wpdb;
|
||||
$now = TMDO_DB::now();
|
||||
$wpdb->insert(
|
||||
TMDO_DB::table( 'hpct_requests' ),
|
||||
array(
|
||||
'post_id' => $post_id,
|
||||
'user_id' => (int) $post->post_author,
|
||||
'status' => $post->post_status,
|
||||
'message' => $post->post_content,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
),
|
||||
array( '%d', '%d', '%s', '%s', '%s', '%s' )
|
||||
);
|
||||
} catch ( \Throwable $e ) {
|
||||
TMDO_Logger::error( $this->module, 'wp_insert_post', $e->getMessage() );
|
||||
}
|
||||
protected function build_insert_data( int $post_id, \WP_Post $post, string $now ): array {
|
||||
return array(
|
||||
'values' => array(
|
||||
'post_id' => $post_id,
|
||||
'user_id' => (int) $post->post_author,
|
||||
'status' => $post->post_status,
|
||||
'message' => $post->post_content,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
),
|
||||
'formats' => array( '%d', '%d', '%s', '%s', '%s', '%s' ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user