Files
2meet-data-optimizer/includes/adapters/class-tmdo-adapter-comment.php
T
wpdev d36bb954d1 chore: initial snapshot of 2meet-data-optimizer v0.1.0
Baseline before backporting wp-data-optimizer v3.0.1-v3.4.6.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
2026-07-31 05:06:36 +08:00

106 lines
2.9 KiB
PHP

<?php
/**
* TMDO_Adapter_Comment - Comment 實體適配器
*
* @package WP_Data_Optimizer
*/
// 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_Comment implements TMDO_Entity_Adapter_Interface {
public function get_entity_type(): string {
return 'comment';
}
public function get_native_meta_table(): string {
global $wpdb;
return $wpdb->commentmeta;
}
public function get_entity_id_column(): string {
return 'comment_id';
}
public function get_primary_table(): string {
global $wpdb;
return $wpdb->comments;
}
public function get_primary_id_column(): string {
return 'comment_ID';
}
public function get_cache_group(): string {
return 'wpdo_comment_profile';
}
public function get_delete_hook(): string {
return 'delete_comment';
}
public function extend_native_query( $query_object ): void {
if ( ! ( $query_object instanceof WP_Comment_Query ) ) {
return;
}
$wpdo_query = $query_object->query_vars['wpdo_meta_query'] ?? null;
if ( empty( $wpdo_query ) || ! is_array( $wpdo_query ) ) {
return;
}
// WP_Comment_Query 使用 comments_clauses filter
add_filter(
'comments_clauses',
function ( $clauses ) use ( $wpdo_query ) {
$compiled = TMDO_Query_Compiler::compile( 'comment', $wpdo_query );
if ( ! empty( $compiled['joins'] ) ) {
$clauses['join'] .= ' ' . implode( ' ', $compiled['joins'] );
}
if ( ! empty( $compiled['where'] ) ) {
$clauses['where'] .= ' AND (' . implode( ' AND ', $compiled['where'] ) . ')';
}
return $clauses;
},
10,
1
);
}
public function get_entity_ids_after( int $after_id, int $limit ): array {
global $wpdb;
return array_map(
'intval',
$wpdb->get_col(
$wpdb->prepare(
"SELECT comment_ID FROM `{$wpdb->comments}` WHERE comment_ID > %d ORDER BY comment_ID ASC LIMIT %d",
$after_id,
$limit
)
) ?: array()
);
}
}
// 註冊 pre_get_comments hook(unit test 下 add_action 未載入時跳過)
if ( function_exists( 'add_action' ) ) {
add_action(
'pre_get_comments',
function ( $query ) {
$adapter = TMDO_Entity_Registry::get_adapter( 'comment' );
if ( $adapter ) {
$adapter->extend_native_query( $query );
}
},
10,
1
);
}