f48df1a33f
- WooCommerce reads product meta directly and writes stock by SQL, so a flat copy drifted (R-2.1 V1 T4); product meta now stays in native only (plan decision 2). TMDO_Post_Fields registers wp_core, attachment and nav_menu_item. Existing wp_wpdo_post_wc_product tables are left in place and still dropped on uninstall; the orphan restore registers nothing for them while native meta holds the keys; `wp tmdo bridge-restore-native post --group=wc_product --retire` archives them. - Tests that route products register the former group themselves (tmdo_test_register_wc_product_group(), same name and table); live router Q06 runs on wp_core's _wp_page_template instead of _sku. The post stress tester already skips flat tables that do not exist. - Verify: PostFieldsRegistrationTest (RED before); dev36: wc_product absent from the registry and not restored as an orphan, a CLI order took stock 452 → 450 in native with no DB error (reverted), drift post 10 groups 0. - PR: PR-077
427 lines
14 KiB
PHP
427 lines
14 KiB
PHP
<?php
|
|
/**
|
|
* Suite: a routed WP_Query returns exactly what the native meta_query returns.
|
|
*
|
|
* Bugs (dev36 qbench, 2026-09-26): the post router pulled registered clauses
|
|
* out of any meta_query and ANDed them, so OR relations (Q04, Q16), queries on
|
|
* several post types (Q08), untyped comparisons on numeric columns (Q14,
|
|
* native compares strings) and orderby on a named clause (Q15) returned other
|
|
* posts than WordPress would; LIKE compared without wildcards (Q06). A key
|
|
* of a second group (wp_core _thumbnail_id, Q05) was never joined, and every
|
|
* routed query carried a GROUP BY although flat rows are one per post.
|
|
*
|
|
* Every case runs the same query twice on fixture posts — router hooks off,
|
|
* then on with post mode shadow_read — and compares the ids in order.
|
|
*
|
|
* @package WP_Data_Optimizer
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Run a WP_Query restricted to $ids, optionally through the post router.
|
|
*
|
|
* @param array<string, mixed> $args Query args (meta_query, post_type, orderby …).
|
|
* @param int[] $ids Fixture posts to search within.
|
|
* @param bool $routed Hook a TMDO_Post_Query_Router for this query.
|
|
* @return array{ids:int[], routed:bool, sql:string}
|
|
*/
|
|
function tmdo_live_router_query( array $args, array $ids, bool $routed ): array {
|
|
global $wpdb;
|
|
$router = new TMDO_Post_Query_Router();
|
|
if ( $routed ) {
|
|
$router->register_hooks();
|
|
}
|
|
try {
|
|
$wpdb->last_error = '';
|
|
$q = new WP_Query(
|
|
$args + array(
|
|
'post__in' => $ids,
|
|
'post_status' => 'any',
|
|
'posts_per_page' => -1,
|
|
'orderby' => 'ID',
|
|
'order' => 'ASC',
|
|
'fields' => 'ids',
|
|
'no_found_rows' => true,
|
|
'cache_results' => false,
|
|
'suppress_filters' => false,
|
|
)
|
|
);
|
|
} finally {
|
|
tmdo_live_router_unhook( $router );
|
|
}
|
|
tmdo_live_assert_same( '', $wpdb->last_error, 'SQL error' );
|
|
return array(
|
|
'ids' => array_map( 'intval', $q->posts ),
|
|
'routed' => ! empty( $q->get( 'wpdo_post_clauses' ) ) || ! empty( $q->get( 'wpdo_post_sort' ) ),
|
|
'sql' => (string) $q->request,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Remove every hook TMDO_Post_Query_Router::register_hooks() added.
|
|
*
|
|
* @param TMDO_Post_Query_Router $router Router instance.
|
|
*/
|
|
function tmdo_live_router_unhook( TMDO_Post_Query_Router $router ): void {
|
|
foreach ( array( 'pre_get_posts', 'posts_join', 'posts_where', 'posts_orderby', 'posts_request', 'split_the_query', 'found_posts_query' ) as $method ) {
|
|
foreach ( 'posts_request' === $method ? array( 'posts_request', 'posts_request_ids' ) : array( $method ) as $hook ) {
|
|
$priority = has_filter( $hook, array( $router, $method ) );
|
|
if ( false !== $priority ) {
|
|
remove_filter( $hook, array( $router, $method ), $priority );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create posts of $post_type with the given meta, in post mode shadow_read
|
|
* (so the Hook Bus keeps the flat rows in step).
|
|
*
|
|
* @param string $post_type Post type.
|
|
* @param array<string, array<string, mixed>> $posts Label => meta.
|
|
* @return array<string, int> Label => post id.
|
|
*/
|
|
function tmdo_live_router_posts( string $post_type, array $posts ): array {
|
|
$ids = array();
|
|
foreach ( $posts as $label => $meta ) {
|
|
$ids[ $label ] = TMDO_Live_Fixtures::post( $post_type );
|
|
foreach ( $meta as $key => $value ) {
|
|
update_post_meta( $ids[ $label ], $key, $value );
|
|
}
|
|
}
|
|
return $ids;
|
|
}
|
|
|
|
/**
|
|
* Listing fixtures shared by the hp_listing cases.
|
|
*
|
|
* @return array<string, int>
|
|
*/
|
|
function tmdo_live_router_listings(): array {
|
|
foreach ( array( 'hp_price', 'hp_verified', 'hp_featured', 'hp_hourly_rate' ) as $key ) {
|
|
TMDO_Live_Fixtures::field( 'post', $key, 'hp_listing' );
|
|
}
|
|
TMDO_Live_Fixtures::use_modes( array( 'post' => 'shadow_read' ) );
|
|
return tmdo_live_router_posts(
|
|
'hp_listing',
|
|
array(
|
|
'L1' => array( 'hp_price' => '10000', 'hp_featured' => '1', 'hp_verified' => '0', 'hp_hourly_rate' => '40' ),
|
|
'L2' => array( 'hp_price' => '9500', 'hp_verified' => '1', 'hp_hourly_rate' => '30' ),
|
|
'L3' => array( 'hp_price' => '500', 'hp_featured' => '0', 'hp_hourly_rate' => '45' ),
|
|
'L4' => array( 'hp_price' => '20000', 'tmdo_live_unregistered' => '0' ),
|
|
'L5' => array(),
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Assert the routed query equals native; optionally that it was routed at all.
|
|
*
|
|
* @param array<string, mixed> $args Query args.
|
|
* @param int[] $ids Fixture ids.
|
|
* @param bool|null $expect_routed true / false to assert, null to ignore.
|
|
* @param bool $ordered Compare order too (false when the order has ties).
|
|
*/
|
|
function tmdo_live_router_assert_native( array $args, array $ids, ?bool $expect_routed = null, bool $ordered = true ): void {
|
|
$native = tmdo_live_router_query( $args, $ids, false );
|
|
$routed = tmdo_live_router_query( $args, $ids, true );
|
|
if ( ! $ordered ) {
|
|
sort( $native['ids'] );
|
|
sort( $routed['ids'] );
|
|
}
|
|
tmdo_live_assert_same( $native['ids'], $routed['ids'], 'routed ids vs native' );
|
|
if ( null !== $expect_routed ) {
|
|
tmdo_live_assert_same( $expect_routed, $routed['routed'], 'query routed to the flat table' );
|
|
}
|
|
if ( $routed['routed'] ) {
|
|
tmdo_live_assert_true( false === stripos( $routed['sql'], 'GROUP BY' ), 'routed query adds no GROUP BY: ' . $routed['sql'] );
|
|
}
|
|
}
|
|
|
|
TMDO_Live::test(
|
|
'Q04 OR across registered keys returns the native posts',
|
|
static function () {
|
|
$l = tmdo_live_router_listings();
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_query' => array(
|
|
'relation' => 'OR',
|
|
array( 'key' => 'hp_featured', 'value' => '1' ),
|
|
array( 'key' => 'hp_verified', 'value' => '1' ),
|
|
),
|
|
),
|
|
array_values( $l )
|
|
);
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'Q16 OR mixing a registered and an unregistered key returns the native posts',
|
|
static function () {
|
|
$l = tmdo_live_router_listings();
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_query' => array(
|
|
'relation' => 'OR',
|
|
array( 'key' => 'hp_price', 'value' => 1000, 'compare' => '<', 'type' => 'NUMERIC' ),
|
|
array( 'key' => 'tmdo_live_unregistered', 'value' => '0' ),
|
|
),
|
|
),
|
|
array_values( $l )
|
|
);
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'Q14 untyped comparison on a numeric column keeps string semantics',
|
|
static function () {
|
|
$l = tmdo_live_router_listings();
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_query' => array( array( 'key' => 'hp_price', 'value' => '9000', 'compare' => '>' ) ),
|
|
),
|
|
array_values( $l )
|
|
);
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'Q15 orderby on a named clause keeps the native order, routed',
|
|
static function () {
|
|
$l = tmdo_live_router_listings();
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_query' => array(
|
|
'rate' => array( 'key' => 'hp_hourly_rate', 'value' => 50, 'compare' => '<', 'type' => 'NUMERIC' ),
|
|
'price' => array( 'key' => 'hp_price', 'compare' => 'EXISTS', 'type' => 'NUMERIC' ),
|
|
),
|
|
'orderby' => 'price',
|
|
'order' => 'ASC',
|
|
),
|
|
array_values( $l ),
|
|
true
|
|
);
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'Q08 several post types return the native posts',
|
|
static function () {
|
|
$l = tmdo_live_router_listings();
|
|
TMDO_Live_Fixtures::field( 'post', 'hp_budget', 'hp_request' );
|
|
$r = tmdo_live_router_posts( 'hp_request', array( 'R1' => array( 'hp_budget' => '90000' ) ) );
|
|
update_post_meta( $l['L2'], 'hp_budget', '85000' );
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => array( 'hp_listing', 'hp_request' ),
|
|
'meta_query' => array( array( 'key' => 'hp_budget', 'value' => 80000, 'compare' => '>=', 'type' => 'NUMERIC' ) ),
|
|
),
|
|
array_merge( array_values( $l ), array_values( $r ) )
|
|
);
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'typed AND on registered keys is routed and returns the native posts',
|
|
static function () {
|
|
$l = tmdo_live_router_listings();
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_query' => array(
|
|
array( 'key' => 'hp_price', 'value' => array( 9000, 15000 ), 'compare' => 'BETWEEN', 'type' => 'NUMERIC' ),
|
|
array( 'key' => 'hp_verified', 'value' => '1' ),
|
|
),
|
|
),
|
|
array_values( $l ),
|
|
true
|
|
);
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'Q06 LIKE matches substrings and escapes wildcards, routed',
|
|
static function () {
|
|
// A text key of wp_core (WooCommerce's _sku left the flat tables in 1.2.0).
|
|
TMDO_Live_Fixtures::field( 'post', '_wp_page_template' );
|
|
TMDO_Live_Fixtures::use_modes( array( 'post' => 'shadow_read' ) );
|
|
$p = tmdo_live_router_posts(
|
|
'post',
|
|
array(
|
|
'P1' => array( '_wp_page_template' => 'tmdo-live-B1-1' ),
|
|
'P2' => array( '_wp_page_template' => 'tmdo-live-B1-10' ),
|
|
'P3' => array( '_wp_page_template' => 'tmdo-live-B2-1' ),
|
|
)
|
|
);
|
|
foreach ( array( 'B1-1', 'B1_1' ) as $needle ) {
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => 'post',
|
|
'meta_query' => array( array( 'key' => '_wp_page_template', 'value' => $needle, 'compare' => 'LIKE' ) ),
|
|
),
|
|
array_values( $p ),
|
|
true
|
|
);
|
|
}
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'Q05 keys of two groups are both joined, routed',
|
|
static function () {
|
|
$l = tmdo_live_router_listings();
|
|
TMDO_Live_Fixtures::field( 'post', '_thumbnail_id', 'hp_listing' );
|
|
update_post_meta( $l['L1'], '_thumbnail_id', '123' );
|
|
update_post_meta( $l['L3'], '_thumbnail_id', '124' );
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_query' => array(
|
|
array( 'key' => 'hp_hourly_rate', 'value' => 42, 'compare' => '>=', 'type' => 'NUMERIC' ),
|
|
array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS' ),
|
|
),
|
|
),
|
|
array_values( $l ),
|
|
true
|
|
);
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'NOT EXISTS with explicit statuses and date order keeps posts without a flat row',
|
|
static function () {
|
|
$l = tmdo_live_router_listings();
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => 'hp_listing',
|
|
'post_status' => array( 'draft' ),
|
|
'orderby' => 'date',
|
|
'order' => 'DESC',
|
|
'meta_query' => array( array( 'key' => 'hp_featured', 'compare' => 'NOT EXISTS' ) ),
|
|
),
|
|
array_values( $l ),
|
|
true,
|
|
false
|
|
);
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'HivePress sort shape (NOT EXISTS OR EXISTS, named clause) keeps the native order, routed',
|
|
static function () {
|
|
$l = tmdo_live_router_listings();
|
|
foreach ( array( 'ASC', 'DESC' ) as $order ) {
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_query' => array(
|
|
array(
|
|
'relation' => 'OR',
|
|
'price__order' => array( 'key' => 'hp_price', 'type' => 'DECIMAL', 'compare' => 'NOT EXISTS' ),
|
|
array( 'key' => 'hp_price', 'type' => 'DECIMAL', 'compare' => 'EXISTS' ),
|
|
),
|
|
),
|
|
'orderby' => 'price__order',
|
|
'order' => $order,
|
|
),
|
|
array_values( $l ),
|
|
true
|
|
);
|
|
}
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'Q07 meta_key + meta_value_num keeps the native posts and order, routed',
|
|
static function () {
|
|
$l = tmdo_live_router_listings();
|
|
tmdo_live_router_assert_native(
|
|
array(
|
|
'post_type' => 'hp_listing',
|
|
'meta_key' => 'hp_price',
|
|
'orderby' => 'meta_value_num',
|
|
'order' => 'DESC',
|
|
),
|
|
array_values( $l ),
|
|
true
|
|
);
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'a routed full-row search keeps the split query (IDs first) and native found_posts',
|
|
static function () {
|
|
global $wpdb;
|
|
$l = tmdo_live_router_listings();
|
|
$args = array(
|
|
'post_type' => 'hp_listing',
|
|
'post__in' => array_values( $l ),
|
|
'post_status' => 'any',
|
|
'posts_per_page' => 2,
|
|
'orderby' => 'ID',
|
|
'order' => 'ASC',
|
|
'meta_query' => array( array( 'key' => 'hp_price', 'value' => 1000, 'compare' => '>=', 'type' => 'NUMERIC' ) ),
|
|
);
|
|
$native = new WP_Query( $args );
|
|
$router = new TMDO_Post_Query_Router();
|
|
$router->register_hooks();
|
|
try {
|
|
$routed = new WP_Query( $args );
|
|
} finally {
|
|
tmdo_live_router_unhook( $router );
|
|
}
|
|
|
|
tmdo_live_assert_same( wp_list_pluck( $native->posts, 'ID' ), wp_list_pluck( $routed->posts, 'ID' ), 'page of posts' );
|
|
tmdo_live_assert_same( $native->found_posts, $routed->found_posts, 'found_posts' );
|
|
tmdo_live_assert_true( false !== strpos( $routed->request, 'wpdoflat_' ), 'routed: ' . $routed->request );
|
|
tmdo_live_assert_true( 1 === preg_match( '/^\s*SELECT\s+' . preg_quote( $wpdb->posts, '/' ) . '\.ID\s/', $routed->request ), 'IDs first: ' . $routed->request );
|
|
}
|
|
);
|
|
|
|
TMDO_Live::test(
|
|
'a filtered search ordered by a flat column drives from the flat table and matches native',
|
|
static function () {
|
|
foreach ( array( 'hp_price', 'hp_hourly_rate' ) as $key ) {
|
|
TMDO_Live_Fixtures::field( 'post', $key, 'hp_listing' );
|
|
}
|
|
TMDO_Live_Fixtures::use_modes( array( 'post' => 'shadow_read' ) );
|
|
// A price band no seeded listing uses keeps the result to the fixtures.
|
|
tmdo_live_router_posts(
|
|
'hp_listing',
|
|
array(
|
|
'A' => array( 'hp_price' => '9876501', 'hp_hourly_rate' => '12' ),
|
|
'B' => array( 'hp_price' => '9876502', 'hp_hourly_rate' => '15' ),
|
|
'C' => array( 'hp_price' => '9876503' ),
|
|
)
|
|
);
|
|
$args = array(
|
|
'post_type' => 'hp_listing',
|
|
'post_status' => 'any',
|
|
'posts_per_page' => 10,
|
|
'fields' => 'ids',
|
|
'suppress_filters' => false,
|
|
'meta_query' => array( array( 'key' => 'hp_price', 'value' => array( 9876500, 9876510 ), 'compare' => 'BETWEEN', 'type' => 'NUMERIC' ) ),
|
|
'meta_key' => 'hp_price',
|
|
'orderby' => 'meta_value_num',
|
|
'order' => 'DESC',
|
|
);
|
|
$native = new WP_Query( $args );
|
|
$router = new TMDO_Post_Query_Router();
|
|
$router->register_hooks();
|
|
try {
|
|
$routed = new WP_Query( $args );
|
|
} finally {
|
|
tmdo_live_router_unhook( $router );
|
|
}
|
|
|
|
tmdo_live_assert_same( array_map( 'intval', $native->posts ), array_map( 'intval', $routed->posts ), 'ids' );
|
|
tmdo_live_assert_same( $native->found_posts, $routed->found_posts, 'found_posts' );
|
|
tmdo_live_assert_true( 0 === strpos( ltrim( $routed->request ), 'SELECT STRAIGHT_JOIN' ), 'flat-first: ' . $routed->request );
|
|
}
|
|
);
|