Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion inc/plugins/class-atomic-wind-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
14 changes: 12 additions & 2 deletions inc/render/class-posts-grid-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'] ) ) {

Expand Down
7 changes: 6 additions & 1 deletion plugins/otter-pro/inc/server/class-live-search-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions tests/test-atomic-wind-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div class="wp-block-atomic-wind-box">INNER</div>';

$result = $this->instance->render_query_loop( $content, $block );

$this->assertSame( '', $result );
$this->assertStringNotContainsString( 'Secret Record Title', $result );

unregister_post_type( 'otter_secret' );
}

public function test_query_loop_renders_a_viewable_post_type() {
$block = $this->make_block( 'atomic-wind/box', array( 'queryPostType' => 'post' ) );
$content = '<div class="wp-block-atomic-wind-box">INNER</div>';

$result = $this->instance->render_query_loop( $content, $block );

// The post created in set_up() is a viewable match, so the wrapper is emitted.
$this->assertNotSame( '', $result );
$this->assertStringContainsString( 'wp-block-atomic-wind-box', $result );
}
}
31 changes: 31 additions & 0 deletions tests/test-live-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,35 @@ public function test_o_post_type_ignored_on_secondary_query() {

$this->assertNotEquals( array( 'otter_shop_product' ), $secondary->get( 'post_type' ) );
}

/**
* A non-viewable type passed via o_post_type is dropped, so an anonymous visitor
* cannot query it through the main search query (otter-internals#319).
*/
public function test_o_post_type_skips_non_viewable_type() {
$live_search = new Live_Search_Server();

$this->go_to( home_url( '/?s=test' ) );
global $wp_query;
$wp_query->set( 'o_post_type', 'otter_shop_coupon' );

$live_search->parse_query( $wp_query );

$this->assertNotEquals( array( 'otter_shop_coupon' ), $wp_query->get( 'post_type' ) );
}

/**
* A mixed list keeps the viewable types and drops the non-viewable one.
*/
public function test_o_post_type_keeps_only_viewable_types() {
$live_search = new Live_Search_Server();

$this->go_to( home_url( '/?s=test' ) );
global $wp_query;
$wp_query->set( 'o_post_type', 'otter_shop_product,otter_shop_coupon' );

$live_search->parse_query( $wp_query );

$this->assertEquals( array( 'otter_shop_product' ), $wp_query->get( 'post_type' ) );
}
}
72 changes: 72 additions & 0 deletions tests/test-post-grid-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,76 @@ public function test_render_sanitization() {

$this->assertEquals( $expected, $output );
}

/**
* Render the block for the given post types and return its HTML.
*
* @param string[] $post_types Post types to query.
* @return string
*/
private function render_for_post_types( $post_types ) {
$this->post_grid_block = new Posts_Grid_Block();
WP_Block_Supports::init();
WP_Block_Supports::$block_to_render = array( 'blockName' => 'themeisle-blocks/posts-grid' );

$attributes = unserialize( serialize( $this->attributes ) );
$attributes['postTypes'] = $post_types;

return $this->post_grid_block->render( $attributes );
}

/**
* A non-public post type is never queried, so its records cannot be exposed
* through the block (otter-internals#319).
*/
public function test_render_skips_non_public_post_types() {
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',
)
);
$this->factory()->post->create(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post_title' => 'Public Blog Title',
)
);

$only_secret = $this->render_for_post_types( array( 'otter_secret' ) );
$this->assertStringNotContainsString( 'Secret Record Title', $only_secret );
// The early return shows nothing for an all-non-viewable list, rather than falling back to posts.
$this->assertStringNotContainsString( 'Public Blog Title', $only_secret );

$mixed = $this->render_for_post_types( array( 'otter_secret', 'post' ) );
$this->assertStringNotContainsString( 'Secret Record Title', $mixed );
$this->assertStringContainsString( 'Public Blog Title', $mixed );

unregister_post_type( 'otter_secret' );
}

/**
* A public post type still renders, so the filter does not block normal use.
*/
public function test_render_keeps_public_post_types() {
$this->factory()->post->create(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post_title' => 'Public Blog Title',
)
);

$this->assertStringContainsString( 'Public Blog Title', $this->render_for_post_types( array( 'post' ) ) );
}
}
Loading