Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor meta key allowed check #2862

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
92 changes: 64 additions & 28 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -791,37 +791,33 @@ protected function get_term_order( $term_taxonomy_id, $object_id ) {
}

/**
* Prepare post meta to send to ES
* Checks if meta key is allowed
*
* @param string $meta_key meta key to check
* @param WP_Post $post Post object
* @since 0.1.0
* @return array
* @since 4.3.0
* @return boolean
*/
public function prepare_meta( $post ) {
/**
* Filter pre-prepare meta for a post
*
* @hook ep_prepare_meta_data
* @param {array} $meta Meta data
* @param {WP_Post} $post Post object
* @return {array} New meta
*/
$meta = apply_filters( 'ep_prepare_meta_data', (array) get_post_meta( $post->ID ), $post );
public function is_meta_allowed( $meta_key, $post ) {
$test_metas = [
$meta_key => true,
];

if ( empty( $meta ) ) {
/**
* Filter final list of prepared meta.
*
* @hook ep_prepared_post_meta
* @param {array} $prepared_meta Prepared meta
* @param {WP_Post} $post Post object
* @since 3.4
* @return {array} Prepared meta
*/
return apply_filters( 'ep_prepared_post_meta', [], $post );
}
$filtered_test_metas = $this->filter_allowed_metas( $test_metas, $post );

$prepared_meta = [];
return array_key_exists( $meta_key, $filtered_test_metas );
}

/**
* Filter post meta to only the allowed ones to be send to ES
*
* @param array $metas Key => value pairs of post meta
* @param WP_Post $post Post object
* @since 4.3.0
* @return array
*/
public function filter_allowed_metas( $metas, $post ) {
$filtered_metas = [];

/**
* Filter indexable protected meta keys for posts
Expand All @@ -845,7 +841,7 @@ public function prepare_meta( $post ) {
*/
$excluded_public_keys = apply_filters( 'ep_prepare_meta_excluded_public_keys', [], $post );

foreach ( $meta as $key => $value ) {
foreach ( $metas as $key => $value ) {

$allow_index = false;

Expand All @@ -871,9 +867,49 @@ public function prepare_meta( $post ) {
* @return {bool} New whitelist value
*/
if ( true === $allow_index || apply_filters( 'ep_prepare_meta_whitelist_key', false, $key, $post ) ) {
$prepared_meta[ $key ] = maybe_unserialize( $value );
$filtered_metas[ $key ] = $value;
}
}
return $filtered_metas;
}

/**
* Prepare post meta to send to ES
*
* @param WP_Post $post Post object
* @since 0.1.0
* @return array
*/
public function prepare_meta( $post ) {
/**
* Filter pre-prepare meta for a post
*
* @hook ep_prepare_meta_data
* @param {array} $meta Meta data
* @param {WP_Post} $post Post object
* @return {array} New meta
*/
$meta = apply_filters( 'ep_prepare_meta_data', (array) get_post_meta( $post->ID ), $post );

if ( empty( $meta ) ) {
/**
* Filter final list of prepared meta.
*
* @hook ep_prepared_post_meta
* @param {array} $prepared_meta Prepared meta
* @param {WP_Post} $post Post object
* @since 3.4
* @return {array} Prepared meta
*/
return apply_filters( 'ep_prepared_post_meta', [], $post );
}

$filtered_metas = $this->filter_allowed_metas( $meta, $post );
$prepared_meta = [];
rebeccahum marked this conversation as resolved.
Show resolved Hide resolved

foreach ( $filtered_metas as $key => $value ) {
$prepared_meta[ $key ] = maybe_unserialize( $value );
}

/**
* Filter final list of prepared meta.
Expand Down
4 changes: 2 additions & 2 deletions includes/classes/Indexable/Post/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ function( $object_id ) {
$indexable_post_statuses = $indexable->get_indexable_post_status();
$post_type = get_post_type( $object_id );

$allowed_meta_to_be_indexed = $indexable->prepare_meta( $post );
if ( ! in_array( $meta_key, array_keys( $allowed_meta_to_be_indexed ), true ) ) {
$is_meta_allowed = $indexable->is_meta_allowed( $meta_key, $post );
if ( ! $is_meta_allowed ) {
return;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -6867,4 +6867,32 @@ public function testMetaWithoutValue() {
$this->assertEquals( $expected_result, $query->post_count );

}

/**
* Tests is_meta_allowed
*
* @return void
* @group is_meta_allowed
*/
public function testIsMetaAllowed() {
$meta_not_protected = 'meta';
$meta_not_protected_excluded = 'meta_excluded';
$meta_protected = '_meta';
$meta_protected_allowed = '_meta_allowed';

add_filter( 'ep_prepare_meta_allowed_protected_keys', function () use ( $meta_protected_allowed ) {
return [ $meta_protected_allowed ];
} );
add_filter( 'ep_prepare_meta_excluded_public_keys', function () use ( $meta_not_protected_excluded ) {
return [ $meta_not_protected_excluded ];
} );

$indexable = \ElasticPress\Indexables::factory()->get( 'post' );

$this->assertTrue( $indexable->is_meta_allowed( $meta_not_protected, null ) );
$this->assertTrue( $indexable->is_meta_allowed( $meta_protected_allowed, null ) );

$this->assertFalse( $indexable->is_meta_allowed( $meta_not_protected_excluded, null ) );
$this->assertFalse( $indexable->is_meta_allowed( $meta_protected, null ) );
}
}