Skip to content

Commit

Permalink
Merge pull request #2862 from Automattic/update/allowed_meta_filter_u…
Browse files Browse the repository at this point in the history
…pstream

refactor meta key allowed check
  • Loading branch information
felipeelia committed Aug 3, 2022
2 parents 177a96e + 3083ebc commit 2915993
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 30 deletions.
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 = [];

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 ) );
}
}

0 comments on commit 2915993

Please sign in to comment.