fix(2meet-data-optimizer): filter REST listings collection by post_status and post_type

- GET /wpdo/v1/listings rejects unregistered or non-viewable post types (400) and joins posts so Zone A only lists published posts of that type
- WP_Query fallback now returns only show_in_rest hot columns, matching the Zone A path
- Verify: core unit 605 / integration 432 OK (new RestApi tests); 1M bench: page 7ms, total COUNT 0.12s → 1.7s (tracked in PR-030)
- PR: PR-015
This commit is contained in:
2026-09-25 23:10:46 +08:00
parent b407f235ac
commit 9dc4516ca0
5 changed files with 129 additions and 15 deletions
+38
View File
@@ -21,6 +21,8 @@ class RestApiTest extends TestCase {
$GLOBALS['_wp_post_types'] = [];
$GLOBALS['_wp_current_user_can'] = [];
$GLOBALS['_wp_valid_nonces'] = [];
$GLOBALS['_wp_query_posts'] = [];
$GLOBALS['_wp_registered_post_types'] = [ 'hp_listing' => true ];
$_COOKIE = [];
// Reset Feature Flags cache via reflection.
@@ -153,6 +155,42 @@ class RestApiTest extends TestCase {
$this->assertIsArray( $response->get_data() );
}
public function test_get_listings_rejects_unregistered_post_type(): void {
$req = new WP_REST_Request( 'GET', '/wpdo/v1/listings' );
$req->set_param( 'post_type', 'no_such_type' );
$response = $this->api->get_listings( $req );
$this->assertSame( 400, $response->get_status() );
}
public function test_get_listings_rejects_non_viewable_post_type(): void {
$GLOBALS['_wp_registered_post_types']['hp_internal'] = false;
$req = new WP_REST_Request( 'GET', '/wpdo/v1/listings' );
$req->set_param( 'post_type', 'hp_internal' );
$response = $this->api->get_listings( $req );
$this->assertSame( 400, $response->get_status() );
}
public function test_listings_fallback_only_returns_show_in_rest_columns(): void {
$registry = WPDO_Schema_Registry::instance();
$registry->register( 'unit_rest', array( 'post_type' => 'hp_listing', 'meta_key' => 'hp_price', 'zone' => 'hot', 'column' => 'hp_price' ) );
$registry->register( 'unit_rest', array( 'post_type' => 'hp_listing', 'meta_key' => 'hp_secret', 'zone' => 'hot', 'column' => 'hp_secret', 'show_in_rest' => false ) );
$post = new stdClass();
$post->ID = 21;
$post->post_type = 'hp_listing';
$GLOBALS['_wp_query_posts'] = array( $post );
$GLOBALS['_wp_postmeta'][21] = array( 'hp_price' => '99', 'hp_secret' => 'hidden' );
$req = new WP_REST_Request( 'GET', '/wpdo/v1/listings' );
$req->set_param( 'post_type', 'hp_listing' );
$data = $this->api->get_listings( $req )->get_data();
$this->assertSame( '99', $data[0]['hp_price'] );
$this->assertArrayNotHasKey( 'hp_secret', $data[0] );
}
// ── Pagination headers ────────────────────────────────────────────────────
public function test_listings_fallback_sets_pagination_headers(): void {