diff --git a/inc/plugins/class-atomic-wind-blocks.php b/inc/plugins/class-atomic-wind-blocks.php index b6de8b43d..e0924c025 100644 --- a/inc/plugins/class-atomic-wind-blocks.php +++ b/inc/plugins/class-atomic-wind-blocks.php @@ -1253,11 +1253,18 @@ public function render_query_loop( $block_content, $block ) { return $block_content; } + // The post type comes from the request. Only loop over a publicly viewable one, so a non-public + // type (e.g. a contact-form inbox) whose entries are stored as `publish` cannot be exposed. + $post_type = sanitize_key( $post_type ); + if ( ! is_post_type_viewable( $post_type ) ) { + return ''; + } + global $post; $saved_post = $post; $args = array( - 'post_type' => sanitize_key( $post_type ), + 'post_type' => $post_type, 'posts_per_page' => isset( $block['attrs']['queryCount'] ) ? min( absint( $block['attrs']['queryCount'] ), 100 ) : 3, 'orderby' => isset( $block['attrs']['queryOrderBy'] ) ? sanitize_key( $block['attrs']['queryOrderBy'] ) : sanitize_key( 'date' ), 'order' => isset( $block['attrs']['queryOrder'] ) && strtoupper( $block['attrs']['queryOrder'] ) === 'ASC' ? 'ASC' : 'DESC', diff --git a/inc/render/class-posts-grid-block.php b/inc/render/class-posts-grid-block.php index 623109101..3ccdbbfba 100644 --- a/inc/render/class-posts-grid-block.php +++ b/inc/render/class-posts-grid-block.php @@ -359,8 +359,18 @@ protected function retrieve_posts( $attributes, $count_posts, $page_number, &$to $categories = join( ', ', $cats ); } + // Only query publicly viewable post types; the attribute comes from the request and could name + // a non-public type (e.g. a contact-form inbox) whose entries are stored with the `publish` status. + $requested_post_types = (array) $attributes['postTypes']; + $post_types = array_values( array_filter( $requested_post_types, 'is_post_type_viewable' ) ); + + // An explicit list of only non-viewable types returns nothing, instead of falling back to posts. + if ( ! empty( $requested_post_types ) && empty( $post_types ) ) { + return array(); + } + $args = array( - 'post_type' => $attributes['postTypes'], + 'post_type' => $post_types, 'posts_per_page' => $attributes['postsToShow'], 'post_status' => 'publish', 'order' => isset( $attributes['order'] ) ? $attributes['order'] : 'DESC', @@ -378,7 +388,7 @@ protected function retrieve_posts( $attributes, $count_posts, $page_number, &$to } // Handle the case when the post type is a WooCommerce product. - if ( isset( $args['post_type'] ) && in_array( 'product', $args['post_type'] ) && function_exists( 'wc_get_products' ) ) { + if ( in_array( 'product', $args['post_type'], true ) && function_exists( 'wc_get_products' ) ) { if ( isset( $attributes['categories'] ) ) { diff --git a/plugins/otter-pro/inc/server/class-live-search-server.php b/plugins/otter-pro/inc/server/class-live-search-server.php index e90d954ef..19d567e36 100644 --- a/plugins/otter-pro/inc/server/class-live-search-server.php +++ b/plugins/otter-pro/inc/server/class-live-search-server.php @@ -74,7 +74,12 @@ public function add_query_vars( $query_vars ) { */ public function parse_query( $query ) { if ( $query->is_main_query() && $query->is_search() && $query->get( 'o_post_type' ) ) { - $query->set( 'post_type', explode( ',', $query->get( 'o_post_type' ) ) ); + // Only publicly viewable types; a non-public type (e.g. a contact-form inbox) whose entries are + // `publish` must not be queryable by an anonymous visitor through `?o_post_type=`. + $post_types = array_values( array_filter( explode( ',', $query->get( 'o_post_type' ) ), 'is_post_type_viewable' ) ); + if ( ! empty( $post_types ) ) { + $query->set( 'post_type', $post_types ); + } } return $query; diff --git a/tests/test-atomic-wind-blocks.php b/tests/test-atomic-wind-blocks.php index eec14bc58..83abea7c8 100644 --- a/tests/test-atomic-wind-blocks.php +++ b/tests/test-atomic-wind-blocks.php @@ -2597,4 +2597,47 @@ public function test_enqueue_base_css_registers_frontend_style_without_enqueuing $this->inline_style_for( 'atomic-wind-base' ) ); } + + // ------------------------------------------------------- + // render_query_loop — post type visibility (otter-internals#319) + // ------------------------------------------------------- + + public function test_query_loop_skips_non_viewable_post_type() { + register_post_type( + 'otter_secret', + array( + 'public' => false, + 'publicly_queryable' => false, + 'exclude_from_search' => true, + ) + ); + $this->factory()->post->create( + array( + 'post_type' => 'otter_secret', + 'post_status' => 'publish', + 'post_title' => 'Secret Record Title', + ) + ); + + $block = $this->make_block( 'atomic-wind/box', array( 'queryPostType' => 'otter_secret' ) ); + $content = '