Skip to content

Commit

Permalink
Merge pull request #3577 from 10up/feature/issue-3574
Browse files Browse the repository at this point in the history
Further changes using the ep_indexable site meta
  • Loading branch information
felipeelia authored Aug 7, 2023
2 parents 1b72888 + a9d92e0 commit c4450d3
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 63 deletions.
6 changes: 1 addition & 5 deletions includes/classes/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 3 additions & 11 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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'] );
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 3 additions & 4 deletions includes/classes/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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'] );
}
}
Expand Down
13 changes: 2 additions & 11 deletions includes/classes/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Indexable/Comment/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Indexable/Post/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions includes/classes/Indexable/Term/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 2 additions & 10 deletions includes/classes/StatusReport/ElasticPressIo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions includes/classes/StatusReport/IndexableContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
44 changes: 33 additions & 11 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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 = [];
Expand Down
35 changes: 35 additions & 0 deletions tests/php/TestUninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading

0 comments on commit c4450d3

Please sign in to comment.