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
96 lines
2.5 KiB
PHP
96 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* TMDO_Adapter_Post - Post 實體適配器
|
|
*
|
|
* @package WP_Data_Optimizer
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
// phpcs:disable Squiz.Commenting,Generic.Commenting,WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber,Generic.CodeAnalysis.UnusedFunctionParameter,Generic.CodeAnalysis.EmptyStatement,Squiz.PHP.DisallowMultipleAssignments,Squiz.PHP.DisallowSizeFunctionsInLoops,WordPress.WP.I18n.MissingTranslatorsComment,WordPress.PHP.NoSilencedErrors,WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents,Squiz.PHP.CommentedOutCode,Universal.NamingConventions.NoReservedKeywordParameterNames,WordPress.PHP.YodaConditions,Squiz.Commenting.InlineComment.InvalidEndChar -- PR-1 ported from UAE; cleanup PR scheduled.
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
final class TMDO_Adapter_Post implements TMDO_Entity_Adapter_Interface {
|
|
|
|
public function get_entity_type(): string {
|
|
return 'post';
|
|
}
|
|
|
|
public function get_native_meta_table(): string {
|
|
global $wpdb;
|
|
return $wpdb->postmeta;
|
|
}
|
|
|
|
public function get_entity_id_column(): string {
|
|
return 'post_id';
|
|
}
|
|
|
|
public function get_primary_table(): string {
|
|
global $wpdb;
|
|
return $wpdb->posts;
|
|
}
|
|
|
|
public function get_primary_id_column(): string {
|
|
return 'ID';
|
|
}
|
|
|
|
public function get_cache_group(): string {
|
|
return 'wpdo_post_profile';
|
|
}
|
|
|
|
public function get_delete_hook(): string {
|
|
return 'before_delete_post';
|
|
}
|
|
|
|
/**
|
|
* 擴充 WP_Query 支援 wpdo_meta_query / wpdo_orderby
|
|
*/
|
|
public function extend_native_query( $query_object ): void {
|
|
if ( ! ( $query_object instanceof WP_Query ) ) {
|
|
return;
|
|
}
|
|
|
|
$wpdo_query = $query_object->get( 'wpdo_meta_query' );
|
|
if ( empty( $wpdo_query ) || ! is_array( $wpdo_query ) ) {
|
|
return;
|
|
}
|
|
|
|
TMDO_Query_Compiler::inject_into_wp_query( $query_object, $wpdo_query );
|
|
}
|
|
|
|
/**
|
|
* Cursor-based 取得 post IDs
|
|
*/
|
|
public function get_entity_ids_after( int $after_id, int $limit ): array {
|
|
global $wpdb;
|
|
|
|
return array_map(
|
|
'intval',
|
|
$wpdb->get_col(
|
|
$wpdb->prepare(
|
|
"SELECT ID FROM `{$wpdb->posts}` WHERE ID > %d ORDER BY ID ASC LIMIT %d",
|
|
$after_id,
|
|
$limit
|
|
)
|
|
) ?: array()
|
|
);
|
|
}
|
|
}
|
|
|
|
// 註冊 pre_get_posts 以啟用 wpdo_meta_query(unit test 下 add_action 未載入時跳過)
|
|
if ( function_exists( 'add_action' ) ) {
|
|
add_action(
|
|
'pre_get_posts',
|
|
function ( $query ) {
|
|
$adapter = TMDO_Entity_Registry::get_adapter( 'post' );
|
|
if ( $adapter ) {
|
|
$adapter->extend_native_query( $query );
|
|
}
|
|
},
|
|
10,
|
|
1
|
|
);
|
|
}
|