diff --git a/src/Sync/Singular.php b/src/Sync/Singular.php index 066cc06..e31be29 100644 --- a/src/Sync/Singular.php +++ b/src/Sync/Singular.php @@ -79,6 +79,8 @@ function( $hook ) { add_action( $hook, [ $this, 'completeUnsync' ], Constants::LATE_HOOK_PRIORITY ); } ); + + add_action( 'transition_post_status', [ $this, 'maybeUnsyncByStatus' ], Constants::LATE_HOOK_PRIORITY, 3 ); } /** @@ -105,7 +107,7 @@ private function getIdsInSyncQueue( $syncManager ) { * @return bool */ public function manageSyncQueue( $halt, $syncManager, $indexableSlug ) { - if ( \WPML\ElasticPress\Constants::INDEXABLE_SLUG_POST !== $indexableSlug ) { + if ( Constants::INDEXABLE_SLUG_POST !== $indexableSlug ) { return $halt; } @@ -234,4 +236,29 @@ public function clearIndexLanguageByMeta( $metaId, $postId ) { $this->clearIndexLanguage( $postId ); } + /** + * @param string $newStatus + * @param string $oldStatus + * @param \WP_Post $post + */ + public function maybeUnsyncByStatus( $newStatus, $oldStatus, $post ) { + if ( $oldStatus === $newStatus ) { + return; + } + + $postIndexable = $this->indexables->get( Constants::INDEXABLE_SLUG_POST ); + if ( false === $postIndexable ) { + return; + } + + $indexablePostStati = $postIndexable->get_indexable_post_status(); + if ( in_array( $post->post_status, $indexablePostStati, true ) ) { + return; + } + + $this->startUnsync( $post->ID ); + $this->clearIndexLanguage( $post->ID ); + $this->completeUnsync( $post->ID ); + } + } diff --git a/tests/phpstan/stubs/elasticpress.stub b/tests/phpstan/stubs/elasticpress.stub index 5a2b4a2..640d7b4 100644 --- a/tests/phpstan/stubs/elasticpress.stub +++ b/tests/phpstan/stubs/elasticpress.stub @@ -67,6 +67,11 @@ namespace ElasticPress { */ public function put_mapping( $index, $mapping, $return_type = 'bool' ) {} + /** + * @return string[] + */ + public function get_cluster_indices() {} + /** * @return boolean */ @@ -160,6 +165,11 @@ namespace ElasticPress { */ public function get_index_name( $blog_id = null ) {} + /** + * @return array + */ + public function get_indexable_post_status() {} + /** * @return array */