Skip to content

Commit

Permalink
Merge pull request #3539 from 10up/feature/3514
Browse files Browse the repository at this point in the history
Remove Media Attachments from search
  • Loading branch information
felipeelia authored Jul 19, 2023
2 parents 1908727 + 2a61849 commit 860cad6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
20 changes: 13 additions & 7 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ public function search_setup() {
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
add_filter( 'ep_post_filters', [ $this, 'exclude_posts_from_search' ], 10, 3 );
add_action( 'post_submitbox_misc_actions', [ $this, 'output_exclude_from_search_setting' ] );
add_action( 'edit_post', [ $this, 'save_exclude_from_search_meta' ], 10, 2 );
add_action( 'edit_post', [ $this, 'save_exclude_from_search_meta' ] );
add_filter( 'ep_skip_query_integration', [ $this, 'skip_query_integration' ], 10, 2 );

add_action( 'attachment_submitbox_misc_actions', [ $this, 'output_exclude_from_search_setting' ], 15 );
add_action( 'edit_attachment', [ $this, 'save_exclude_from_search_meta' ] );
}


Expand Down Expand Up @@ -775,7 +778,6 @@ public function exclude_posts_from_search( $filters, $args, $query ) {
* @param WP_POST $post Post object.
*/
public function output_exclude_from_search_setting( $post ) {

$searchable_post_types = $this->get_searchable_post_types();
if ( ! in_array( $post->post_type, $searchable_post_types, true ) ) {
return;
Expand All @@ -784,7 +786,13 @@ public function output_exclude_from_search_setting( $post ) {
<div class="misc-pub-section">
<input id="ep_exclude_from_search" name="ep_exclude_from_search" type="checkbox" value="1" <?php checked( get_post_meta( get_the_ID(), 'ep_exclude_from_search', true ) ); ?>>
<label for="ep_exclude_from_search"><?php esc_html_e( 'Exclude from search results', 'elasticpress' ); ?></label>
<p class="howto"><?php esc_html_e( 'Excludes this post from the results of your site\'s search form while ElasticPress is active.', 'elasticpress' ); ?></p>
<p class="howto">
<?php if ( 'attachment' === $post->post_type ) : ?>
<?php esc_html_e( 'Excludes this media from the results of your site\'s search form while ElasticPress is active.', 'elasticpress' ); ?>
<?php else : ?>
<?php esc_html_e( 'Excludes this post from the results of your site\'s search form while ElasticPress is active.', 'elasticpress' ); ?>
<?php endif; ?>
</p>
<?php wp_nonce_field( 'save-exclude-from-search', 'ep-exclude-from-search-nonce' ); ?>
</div>
<?php
Expand All @@ -793,11 +801,9 @@ public function output_exclude_from_search_setting( $post ) {
/**
* Saves exclude from search meta.
*
* @param int $post_id The post ID.
* @param WP_Post $post Post object.
* @param int $post_id The post ID.
*/
public function save_exclude_from_search_meta( $post_id, $post ) {

public function save_exclude_from_search_meta( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/php/features/TestDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,43 @@ public function testSearchNormalPost() {
$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 1, count( $query->posts ) );
}

/**
* Tests query doesn't return the post if `ep_exclude_from_search` meta is set.
*
* @since 4.7.0
*/
public function testExcludeFromSearchQuery() {
ElasticPress\Features::factory()->activate_feature( 'search' );
ElasticPress\Features::factory()->activate_feature( 'documents' );
ElasticPress\Features::factory()->setup_features();

$this->ep_factory->post->create_many(
2,
array(
'post_content' => 'find me in search',
'meta_input' => array( 'ep_exclude_from_search' => false ),
'post_type' => 'attachment',
'post_mime_type' => 'application/msword',
)
);
$this->ep_factory->post->create(
array(
'post_content' => 'exclude from search',
'meta_input' => array( 'ep_exclude_from_search' => true ),
'post_type' => 'attachment',
'post_mime_type' => 'application/msword',
)
);

ElasticPress\Elasticsearch::factory()->refresh_indices();

$args = array(
's' => 'search',
);
$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, $query->post_count );
}
}

0 comments on commit 860cad6

Please sign in to comment.