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

Only use allowed protected key is found #3250

Merged
merged 1 commit into from
Jan 6, 2023
Merged
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
Only use allowed protected key is found
  • Loading branch information
felipeelia committed Jan 6, 2023
commit cb38f721c32eac686dc75eef9d7252184ee0d12a
10 changes: 7 additions & 3 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2428,15 +2428,19 @@ public function get_distinct_meta_field_keys_db( bool $force_refresh = false ) :
* and only the private keys allowed by the `ep_prepare_meta_allowed_protected_keys` filter.
* This query does not order by on purpose, as that also brings a performance penalty.
*/
$allowed_protected_keys = apply_filters( 'ep_prepare_meta_allowed_protected_keys', [], new \WP_Post( (object) [] ) );
$placeholders = implode( ',', array_fill( 0, count( $allowed_protected_keys ), '%s' ) );
$allowed_protected_keys = apply_filters( 'ep_prepare_meta_allowed_protected_keys', [], new \WP_Post( (object) [] ) );
$allowed_protected_keys_sql = '';
if ( ! empty( $allowed_protected_keys ) ) {
$placeholders = implode( ',', array_fill( 0, count( $allowed_protected_keys ), '%s' ) );
$allowed_protected_keys_sql = " OR meta_key IN ( {$placeholders} ) ";
}

$meta_keys = $wpdb->get_col(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
$wpdb->prepare(
"SELECT DISTINCT meta_key
FROM {$wpdb->postmeta}
WHERE meta_key NOT LIKE %s OR meta_key IN ( {$placeholders} )
WHERE meta_key NOT LIKE %s {$allowed_protected_keys_sql}
LIMIT 800",
'\_%',
...$allowed_protected_keys
Expand Down
13 changes: 9 additions & 4 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -7956,7 +7956,6 @@ public function testExcludeFromSearchFilterDoesNotApplyForAdminQueries() {
$this->assertEquals( 5, $query->post_count );
}


/**
* Tests get_distinct_meta_field_keys_db
*
Expand All @@ -7969,7 +7968,13 @@ public function testGetDistinctMetaFieldKeysDb() {
$indexable = \ElasticPress\Indexables::factory()->get( 'post' );

$meta_keys = $wpdb->get_col( "SELECT DISTINCT meta_key FROM {$wpdb->postmeta} ORDER BY meta_key" );
$this->assertSame( $meta_keys, $indexable->get_distinct_meta_field_keys_db() );
$this->assertSame( $meta_keys, $indexable->get_distinct_meta_field_keys_db( true ) );

// Make sure it works if no allowed protected key is found
add_filter( 'ep_prepare_meta_allowed_protected_keys', '__return_empty_array' );
$this->assertSame( $meta_keys, $indexable->get_distinct_meta_field_keys_db( true ) );
$this->assertEmpty( $wpdb->last_error );
remove_filter( 'ep_prepare_meta_allowed_protected_keys', '__return_empty_array' );

/**
* Test the `ep_post_pre_meta_keys_db` filter
Expand All @@ -7983,7 +7988,7 @@ public function testGetDistinctMetaFieldKeysDb() {
$num_queries = $wpdb->num_queries;
$this->assertGreaterThan( 0, $num_queries );

$this->assertSame( [ 'totally_custom_key' ], $indexable->get_distinct_meta_field_keys_db() );
$this->assertSame( [ 'totally_custom_key' ], $indexable->get_distinct_meta_field_keys_db( true ) );
$this->assertSame( $num_queries, $wpdb->num_queries );

remove_filter( 'ep_post_pre_meta_keys_db', $return_custom_array );
Expand All @@ -7996,7 +8001,7 @@ public function testGetDistinctMetaFieldKeysDb() {
};
add_filter( 'ep_post_meta_keys_db', $return_custom_array );

$this->assertSame( array_merge( $meta_keys, [ 'custom_key' ] ), $indexable->get_distinct_meta_field_keys_db() );
$this->assertSame( array_merge( $meta_keys, [ 'custom_key' ] ), $indexable->get_distinct_meta_field_keys_db( true ) );
}

/**
Expand Down