Skip to content
Open
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
13 changes: 9 additions & 4 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use WP_User;
use ElasticPress\Elasticsearch;
use ElasticPress\Indexable;
use ElasticPress\Feature\Search\Weighting;

if ( ! defined( 'ABSPATH' ) ) {
// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -852,8 +853,10 @@ public function is_meta_allowed( $meta_key, $post ) {
public function filter_allowed_metas( $metas, $post ) {
$filtered_metas = [];

$search = \ElasticPress\Features::factory()->get_registered_feature( 'search' );
if ( $search && ! empty( $search->weighting ) && 'manual' === $search->weighting->get_meta_mode() ) {
$search = \ElasticPress\Features::factory()->get_registered_feature( 'search' );
$weighting = ( $search && ! empty( $search->weighting ) ) ? $search->weighting : new Weighting();

if ( 'manual' === $weighting->get_meta_mode() ) {
$filtered_metas = $this->filter_allowed_metas_manual( $metas, $post );
} else {
$filtered_metas = $this->filter_allowed_metas_auto( $metas, $post );
Expand Down Expand Up @@ -2552,8 +2555,10 @@ protected function filter_allowed_metas_manual( $metas, $post ) {
return $filtered_metas;
}

$weighting = $search_feature->weighting->get_weighting_configuration_with_defaults();
$is_searchable = in_array( $search_feature, $search_feature->get_searchable_post_types(), true );
$weighting_obj = ( $search_feature && ! empty( $search_feature->weighting ) ) ? $search_feature->weighting : new \ElasticPress\Feature\Search\Weighting();
$weighting = $weighting_obj->get_weighting_configuration_with_defaults();
$is_searchable = $search_feature && in_array( $post->post_type, $search_feature->get_searchable_post_types(), true );

if ( empty( $weighting[ $post->post_type ] ) && $is_searchable ) {
return $filtered_metas;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -4040,6 +4040,40 @@ public function testPrepareMetaManual() {
);
}

/**
* Test that filter_allowed_metas keeps manual mode when the Search
* feature is disabled (its `weighting` property is unavailable).
*
* @since 5.4.0
* @group post
*/
public function testPrepareMetaWithSearchDisabled() {
if ( $this->is_network_activate() ) {
$this->markTestSkipped();
}

$search = ElasticPress\Features::factory()->get_registered_feature( 'search' );
$original_weighting = $search->weighting;
$search->weighting = null;

$post_id = $this->ep_factory->post->create(
[
'meta_input' => [
'test_key1' => 'allowed value',
'not_allowed_key1' => 'disallowed value',
],
]
);

$post = get_post( $post_id );
$prepared_meta = ElasticPress\Indexables::factory()->get( 'post' )->prepare_meta( $post );

$this->assertArrayHasKey( 'test_key1', $prepared_meta );
$this->assertArrayNotHasKey( 'not_allowed_key1', $prepared_meta );

$search->weighting = $original_weighting;
}

/**
* Helper method for filtering private meta keys
*
Expand Down
Loading