Skip to content

Commit

Permalink
Merge pull request #3516 from 10up/fix/3470-empty-post-meta-key
Browse files Browse the repository at this point in the history
Exclude meta with empty post meta keys
  • Loading branch information
felipeelia authored Jul 4, 2023
2 parents 6ae5246 + 142ebc8 commit f38785c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,9 @@ public function prepare_meta( $post ) {
$prepared_meta = [];

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

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3886,6 +3886,45 @@ public function filter_ep_prepare_meta_excluded_public_keys( $meta_keys ) {

}

/**
* Test to verify that empty meta key should be excluded before sync.
*
* @since 4.6.1
* @group post
*/
public function testEmptyMetaKey() {
global $wpdb;
$post_id = $this->ep_factory->post->create();
$post = get_post( $post_id );
$meta_key = '';
$meta_value_1 = 'Meta value for empty key';
$meta_values = array(
'value 1',
'value 2',
);
add_post_meta( $post_id, 'test_meta_1', $meta_values );

$wpdb->insert(
$wpdb->postmeta,
array(
'post_id' => $post_id,
'meta_key' => $meta_key,
'meta_value' => $meta_value_1,
),
array( '%d', '%s', '%s' )
);
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE meta_key=%s AND post_id = %d", $meta_key, $post_id ) );

$this->assertSame( $meta_key, $row->meta_key );
$this->assertSame( $meta_value_1, $row->meta_value );

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

$this->assertIsArray( $meta_data );
$this->assertCount( 1, $meta_data );
$this->assertArrayHasKey( 'test_meta_1', $meta_data );
}

/**
* Test meta preparation
*
Expand Down Expand Up @@ -8074,6 +8113,9 @@ public function testExcludeFromSearchQuery() {

/**
* Tests that post meta value should be empty when it is not set.
*
* @since 4.6.1
* @group post
*/
public function testMetaValueNotSet() {
$post_ids = array();
Expand Down

0 comments on commit f38785c

Please sign in to comment.