diff --git a/includes/classes/AdminNotices.php b/includes/classes/AdminNotices.php index f154697043..4c1767fafd 100644 --- a/includes/classes/AdminNotices.php +++ b/includes/classes/AdminNotices.php @@ -773,12 +773,8 @@ protected function process_too_many_fields_notice() { $has_warning = false; if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { - $sites = Utils\get_sites(); + $sites = Utils\get_sites( 0, true ); foreach ( $sites as $site ) { - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { - continue; - } - switch_to_blog( $site['blog_id'] ); list( $has_error, $site_has_warning ) = $this->check_field_count(); diff --git a/includes/classes/Command.php b/includes/classes/Command.php index 0e5ed13139..c7716e36a5 100644 --- a/includes/classes/Command.php +++ b/includes/classes/Command.php @@ -250,13 +250,9 @@ private function put_mapping_helper( $args, $assoc_args ) { $assoc_args['network-wide'] = 0; } - $sites = Utils\get_sites( $assoc_args['network-wide'] ); + $sites = Utils\get_sites( $assoc_args['network-wide'], true ); foreach ( $sites as $site ) { - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { - continue; - } - switch_to_blog( $site['blog_id'] ); foreach ( $non_global_indexable_objects as $indexable ) { @@ -513,7 +509,7 @@ public function delete_index( $args, $assoc_args ) { if ( ! is_numeric( $assoc_args['network-wide'] ) ) { $assoc_args['network-wide'] = 0; } - $sites = Utils\get_sites( $assoc_args['network-wide'] ); + $sites = Utils\get_sites( $assoc_args['network-wide'], false ); foreach ( $sites as $site ) { switch_to_blog( $site['blog_id'] ); @@ -628,14 +624,10 @@ public function epio_set_autosuggest( $args, $assoc_args ) { * @return array|bool */ private function create_network_alias_helper( Indexable $indexable ) { - $sites = Utils\get_sites(); + $sites = Utils\get_sites( 0, true ); $indexes = []; foreach ( $sites as $site ) { - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { - continue; - } - switch_to_blog( $site['blog_id'] ); $indexes[] = $indexable->get_index_name(); diff --git a/includes/classes/Elasticsearch.php b/includes/classes/Elasticsearch.php index 245ff19ef6..3cae25415d 100644 --- a/includes/classes/Elasticsearch.php +++ b/includes/classes/Elasticsearch.php @@ -1740,7 +1740,9 @@ protected function add_query_log( $query ) { * @return array */ public function get_index_names( $status = 'active' ) { - $sites = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ? Utils\get_sites() : array( array( 'blog_id' => get_current_blog_id() ) ); + $sites = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ? + Utils\get_sites( 0, true ) : + array( array( 'blog_id' => get_current_blog_id() ) ); $all_indexables = Indexables::factory()->get_all( null, false, $status ); @@ -1753,9 +1755,6 @@ public function get_index_names( $status = 'active' ) { } foreach ( $sites as $site ) { - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { - continue; - } $non_global_indexes[] = $indexable->get_index_name( $site['blog_id'] ); } } diff --git a/includes/classes/IndexHelper.php b/includes/classes/IndexHelper.php index 0f20791cc9..3b7db2950f 100644 --- a/includes/classes/IndexHelper.php +++ b/includes/classes/IndexHelper.php @@ -164,13 +164,9 @@ protected function build_index_meta() { $this->args['network_wide'] = 0; } - $sites = Utils\get_sites( $this->args['network_wide'] ); + $sites = Utils\get_sites( $this->args['network_wide'], true ); foreach ( $sites as $site ) { - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { - continue; - } - switch_to_blog( $site['blog_id'] ); foreach ( $non_global_indexables as $indexable ) { @@ -941,14 +937,9 @@ protected function create_network_alias() { $indexes = []; $indexable = Indexables::factory()->get( array_shift( $this->index_meta['network_alias'] ) ); - $sites = Utils\get_sites(); + $sites = Utils\get_sites( 0, true ); foreach ( $sites as $site ) { - - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { - continue; - } - switch_to_blog( $site['blog_id'] ); $indexes[] = $indexable->get_index_name(); restore_current_blog(); diff --git a/includes/classes/Indexable/Comment/QueryIntegration.php b/includes/classes/Indexable/Comment/QueryIntegration.php index e145506bbf..cb94d3bbae 100644 --- a/includes/classes/Indexable/Comment/QueryIntegration.php +++ b/includes/classes/Indexable/Comment/QueryIntegration.php @@ -182,7 +182,7 @@ public function maybe_filter_query( $results, WP_Comment_Query $query ) { $this->index = implode( ',', $this->index ); } elseif ( ! empty( $site__not_in ) ) { - $sites = get_sites( + $sites = \get_sites( array( 'fields' => 'ids', 'site__not_in' => $site__not_in, diff --git a/includes/classes/Indexable/Post/QueryIntegration.php b/includes/classes/Indexable/Post/QueryIntegration.php index d559701c5d..100a778759 100644 --- a/includes/classes/Indexable/Post/QueryIntegration.php +++ b/includes/classes/Indexable/Post/QueryIntegration.php @@ -341,7 +341,7 @@ public function get_es_posts( $posts, $query ) { $index = implode( ',', $index ); } elseif ( ! empty( $site__not_in ) ) { - $sites = get_sites( + $sites = \get_sites( array( 'fields' => 'ids', 'site__not_in' => $site__not_in, diff --git a/includes/classes/Indexable/Term/QueryIntegration.php b/includes/classes/Indexable/Term/QueryIntegration.php index abc57caec8..e3d894602f 100644 --- a/includes/classes/Indexable/Term/QueryIntegration.php +++ b/includes/classes/Indexable/Term/QueryIntegration.php @@ -8,9 +8,9 @@ namespace ElasticPress\Indexable\Term; -use ElasticPress\Indexables as Indexables; +use ElasticPress\Indexables; use \WP_Term_Query as WP_Term_Query; -use ElasticPress\Utils as Utils; +use ElasticPress\Utils; if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. @@ -151,7 +151,7 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) { $index = implode( ',', $index ); } elseif ( ! empty( $site__not_in ) ) { - $sites = get_sites( + $sites = \get_sites( array( 'fields' => 'ids', 'site__not_in' => $site__not_in, diff --git a/includes/classes/StatusReport/ElasticPressIo.php b/includes/classes/StatusReport/ElasticPressIo.php index 31f67f6096..d68aa1b83c 100644 --- a/includes/classes/StatusReport/ElasticPressIo.php +++ b/includes/classes/StatusReport/ElasticPressIo.php @@ -120,13 +120,9 @@ protected function get_instant_results_group() : array { $fields = []; if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { - $sites = Utils\get_sites(); + $sites = Utils\get_sites( 0, true ); foreach ( $sites as $site ) { - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { - continue; - } - switch_to_blog( $site['blog_id'] ); $field = $this->get_instant_results_field(); @@ -165,13 +161,9 @@ protected function get_orders_search_group() : array { $fields = []; if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { - $sites = Utils\get_sites(); + $sites = Utils\get_sites( 0, true ); foreach ( $sites as $site ) { - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { - continue; - } - switch_to_blog( $site['blog_id'] ); $field = $this->get_orders_search_field(); diff --git a/includes/classes/StatusReport/IndexableContent.php b/includes/classes/StatusReport/IndexableContent.php index 5dd0ae6d1c..e9f12fc8db 100644 --- a/includes/classes/StatusReport/IndexableContent.php +++ b/includes/classes/StatusReport/IndexableContent.php @@ -46,12 +46,8 @@ protected function get_indexable_content_groups() : array { $groups = []; if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { - $sites = Utils\get_sites(); + $sites = Utils\get_sites( 0, true ); foreach ( $sites as $site ) { - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { - continue; - } - switch_to_blog( $site['blog_id'] ); $groups[] = $this->get_indexable_content_group(); diff --git a/includes/utils.php b/includes/utils.php index 0c1cfa4583..635e119c58 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -296,16 +296,44 @@ function get_site( $site_id ) { /** * Wrapper function for get_sites - allows us to have one central place for the `ep_indexable_sites` filter * - * @param int $limit The maximum amount of sites retrieved, Use 0 to return all sites. - * @since 3.0 + * @param int $limit The maximum amount of sites retrieved, Use 0 to return all sites. + * @param bool $only_indexable Whether should be returned only indexable sites or not. + * @since 3.0, 4.7.0 added `$only_indexable` * @return array */ -function get_sites( $limit = 0 ) { - +function get_sites( $limit = 0, $only_indexable = false ) { if ( ! is_multisite() ) { return []; } + $args = [ + 'limit' => $limit, + 'number' => $limit, + ]; + + if ( $only_indexable ) { + $args = array_merge( + $args, + [ + 'spam' => 0, + 'deleted' => 0, + 'archived' => 0, + 'meta_query' => [ + 'relation' => 'OR', + [ + 'key' => 'ep_indexable', + 'value' => 'no', + 'compare' => '!=', + ], + [ + 'key' => 'ep_indexable', + 'compare' => 'NOT EXISTS', + ], + ], + ] + ); + } + /** * Filter arguments to use to query for sites on network * @@ -314,13 +342,7 @@ function get_sites( $limit = 0 ) { * @param {array} $args Array of args to query sites with. See WP_Site_Query * @return {array} New arguments */ - $args = apply_filters( - 'ep_indexable_sites_args', - array( - 'limit' => $limit, - 'number' => $limit, - ) - ); + $args = apply_filters( 'ep_indexable_sites_args', $args ); $site_objects = \get_sites( $args ); $sites = []; diff --git a/tests/php/TestUninstall.php b/tests/php/TestUninstall.php index 033fe70b9b..d7c98316b7 100644 --- a/tests/php/TestUninstall.php +++ b/tests/php/TestUninstall.php @@ -50,6 +50,41 @@ public function test_delete_transients_by_option_name() { $this->assertFalse( get_transient( 'ep_related_posts_test_2' ) ); } + /** + * Test the `clean_site_meta` method on a single site + * + * @group uninstall + * @group skip-on-multi-site + */ + public function test_clean_site_meta_on_single_site() { + $method = $this->get_protected_method( 'clean_site_meta' ); + $method->invoke( $this->uninstaller ); + $this->expectNotToPerformAssertions(); + } + + /** + * Test the `clean_site_meta` method on a multisite + * + * @group uninstall + * @group skip-on-single-site + */ + public function test_clean_site_meta_on_multi_site() { + $blog_not_indexable = $this->factory->blog->create(); + update_site_meta( $blog_not_indexable, 'ep_indexable', 'no' ); + + $blog_indexable = $this->factory->blog->create(); + update_site_meta( $blog_indexable, 'ep_indexable', 'yes' ); + + $blog_indexable_2 = $this->factory->blog->create(); + + $method = $this->get_protected_method( 'clean_site_meta' ); + $method->invoke( $this->uninstaller ); + + $this->assertSame( '', get_site_meta( $blog_not_indexable, 'ep_indexable', true ) ); + $this->assertSame( '', get_site_meta( $blog_indexable, 'ep_indexable', true ) ); + $this->assertSame( '', get_site_meta( $blog_indexable_2, 'ep_indexable', true ) ); + } + /** * Return a protected method made public. * diff --git a/tests/php/TestUtils.php b/tests/php/TestUtils.php index 3b311a6e11..ba04ce1d62 100644 --- a/tests/php/TestUtils.php +++ b/tests/php/TestUtils.php @@ -469,4 +469,112 @@ public function test_get_language() { $this->assertSame( 'custom_via_filter', Utils\get_language() ); } + + /** + * Test the `get_sites()` method on a single site + * + * @since 4.7.0 + * @group utils + * @group skip-on-multi-site + */ + public function test_get_sites_on_single_site() { + $this->assertSame( [], Utils\get_sites() ); + } + + /** + * Test the `get_sites()` method on a multisite + * + * @since 4.7.0 + * @group utils + * @group skip-on-single-site + */ + public function test_get_sites_on_multi_site() { + $this->factory->blog->create_object( + [ + 'domain' => 'example2.org', + 'title' => 'Example Site 2', + ] + ); + $this->assertCount( 2, Utils\get_sites() ); + + $this->factory->blog->create_object( + [ + 'domain' => 'example3.org', + 'title' => 'Example Site 3', + 'spam' => 1, + ] + ); + $this->assertCount( 3, Utils\get_sites() ); + + $this->factory->blog->create_object( + [ + 'domain' => 'example4.org', + 'title' => 'Example Site 4', + 'deleted' => 1, + ] + ); + $this->assertCount( 4, Utils\get_sites() ); + + $this->factory->blog->create_object( + [ + 'domain' => 'example5.org', + 'title' => 'Example Site 5', + 'archived' => 1, + ] + ); + $this->assertCount( 5, Utils\get_sites() ); + + $blog_6 = $this->factory->blog->create_object( + [ + 'domain' => 'example6.org', + 'title' => 'Example Site 6', + ] + ); + update_site_meta( $blog_6, 'ep_indexable', 'no' ); + $this->assertCount( 6, Utils\get_sites() ); + + // Test the `$only_indexable` parameter + $this->assertCount( 2, Utils\get_sites( 0, true ) ); + + // Test the `$limit` parameter + $this->assertCount( 1, Utils\get_sites( 1, true ) ); + } + + /** + * Test the `ep_indexable_sites_args` filter in the `get_sites()` method + * + * @since 4.7.0 + * @group utils + * @group skip-on-single-site + */ + public function test_get_sites_ep_indexable_sites_args_filter() { + $add_args = function ( $args ) { + $this->assertSame( 3, $args['number'] ); + return $args; + }; + add_filter( 'ep_indexable_sites_args', $add_args ); + + Utils\get_sites( 3 ); + $this->assertGreaterThanOrEqual( 1, did_filter( 'ep_indexable_sites_args' ) ); + } + + /** + * Test the `ep_indexable_sites` filter in the `get_sites()` method + * + * @since 4.7.0 + * @group utils + * @group skip-on-single-site + */ + public function test_get_sites_ep_indexable_sites_filter() { + $add_site = function ( $sites ) { + $this->assertIsArray( $sites ); + $sites['test'] = true; + return $sites; + }; + add_filter( 'ep_indexable_sites', $add_site ); + + $sites = Utils\get_sites(); + $this->assertGreaterThanOrEqual( 1, did_filter( 'ep_indexable_sites' ) ); + $this->assertTrue( $sites['test'] ); + } } diff --git a/uninstall.php b/uninstall.php index a1657378ed..88e6c9a891 100644 --- a/uninstall.php +++ b/uninstall.php @@ -106,6 +106,7 @@ public function __construct() { // Uninstall ElasticPress. $this->clean_options_and_transients(); + $this->clean_site_meta(); $this->remove_elasticpress_capability(); } @@ -181,7 +182,7 @@ protected function clean_options_and_transients() { delete_site_transient( $transient ); } - $sites = get_sites(); + $sites = \get_sites(); foreach ( $sites as $site ) { switch_to_blog( $site->blog_id ); @@ -199,6 +200,22 @@ protected function clean_options_and_transients() { } } + /** + * Delete all site meta + * + * @since 4.7.0 + */ + protected function clean_site_meta() { + if ( ! is_multisite() ) { + return; + } + + $sites = Utils\get_sites(); + foreach ( $sites as $site ) { + delete_site_meta( $site['blog_id'], 'ep_indexable' ); + } + } + /** * Remove the ElasticPress' capability *