Skip to content

Commit

Permalink
Merge pull request #2517 from 10up/fix/synonyms-and-cli-index
Browse files Browse the repository at this point in the history
Synonyms: ignore ep-synonym post status
  • Loading branch information
felipeelia authored Dec 14, 2021
2 parents 4376229 + e155c51 commit 4fe6a5c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion includes/classes/Feature/Search/Synonyms.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,11 @@ public function get_synonym_post_id() {
'post_type' => self::POST_TYPE_NAME,
'posts_per_page' => 1,
'orderby' => 'modified',
'post_status' => 'any',
)
);

$this->synonym_post_id = 1 === $query_synonym_post->post_count ? $query_synonym_post->posts[0] : false;
$this->synonym_post_id = ( $query_synonym_post->post_count >= 1 ) ? $query_synonym_post->posts[0] : false;

if ( ! $this->synonym_post_id ) {
$this->synonym_post_id = $this->insert_default_synonym_post();
Expand Down
31 changes: 31 additions & 0 deletions includes/classes/Upgrades.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function setup() {
$routines = [
'3.5.2' => [ 'upgrade_3_5_2', 'init' ],
'3.5.3' => [ 'upgrade_3_5_3', 'init' ],
'3.6.6' => [ 'upgrade_3_6_6', 'init' ],
];

array_walk( $routines, [ $this, 'run_upgrade_routine' ] );
Expand Down Expand Up @@ -131,6 +132,36 @@ public function upgrade_3_5_3() {
delete_site_option( 'elasticpress_synonyms_post_id' );
}

/**
* Upgrade routine of v3.6.6.
*
* Delete all synonyms that have the post content identical to the example we set.
* In previous versions we had a bug that created several posts with it.
*
* @see https://github.com/10up/ElasticPress/issues/2516
*/
public function upgrade_3_6_6() {
global $wpdb;

$synonyms = \ElasticPress\Features::factory()->get_registered_feature( 'search' )->synonyms;

$synonyms_example_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_content = %s LIMIT 100",
$synonyms::POST_TYPE_NAME,
$synonyms->example_synonym_list()
)
);

if ( ! $synonyms_example_ids ) {
return;
}

foreach ( $synonyms_example_ids as $synonym_post_id ) {
wp_delete_post( $synonym_post_id, true );
}
}

/**
* Check if a reindex is needed based on the version number.
*/
Expand Down

0 comments on commit 4fe6a5c

Please sign in to comment.