Skip to content

Commit

Permalink
Merge pull request #4070 from 10up/fix/3604
Browse files Browse the repository at this point in the history
Fix: Notice while updating a term is not displaying when plugin is ne…
  • Loading branch information
felipeelia authored Jan 30, 2025
2 parents a5b03a0 + 2967a4a commit 5d96e14
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 9 deletions.
14 changes: 13 additions & 1 deletion includes/classes/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,19 @@ public function get_notices() {
* @param {array} $notices Admin notices
* @return {array} New notices
*/
return apply_filters( 'ep_admin_notices', $this->notices );
$notices = apply_filters( 'ep_admin_notices', $this->notices );

// If the plugin is network-activated and not in the network admin, return the notices whose scope is site.
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK && ! is_network_admin() ) {
$notices = array_filter(
$notices,
function ( $notice ) {
return isset( $notice['scope'] ) && 'site' === $notice['scope'];
}
);
}

return $notices;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions includes/classes/Indexable/Post/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ public function maybe_display_notice_edit_single_term( $notices ) {
),
'type' => 'warning',
'dismiss' => true,
'scope' => 'site',
];
break;
}
Expand All @@ -436,6 +437,7 @@ public function maybe_display_notice_edit_single_term( $notices ) {
),
'type' => 'warning',
'dismiss' => true,
'scope' => 'site',
];

return $notices;
Expand Down Expand Up @@ -470,6 +472,7 @@ public function maybe_display_notice_term_list_screen( $notices ) {
),
'type' => 'warning',
'dismiss' => true,
'scope' => 'site',
];

return $notices;
Expand Down
9 changes: 2 additions & 7 deletions includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,8 @@ function filter_plugin_action_links( $plugin_actions, $plugin_file ) {
* @since 3.0
*/
function maybe_notice( $force = false ) {
// Admins only.
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
if ( ! is_super_admin() || ! is_network_admin() ) {
return false;
}
} elseif ( is_network_admin() || ! current_user_can( Utils\get_capability() ) ) {
return false;
if ( ! current_user_can( Utils\get_capability() ) ) {
return false;
}

/**
Expand Down
93 changes: 92 additions & 1 deletion tests/php/TestAdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,6 @@ function ( $values ) {
* Tests notice is show when number of posts linked with term is greater than number of items per cycle.
*/
public function testNumberOfPostsBiggerThanItemPerCycle() {

global $pagenow, $tax;

// set global variables.
Expand All @@ -673,6 +672,98 @@ public function testNumberOfPostsBiggerThanItemPerCycle() {
$this->assertArrayHasKey( 'too_many_posts_on_term', $notices );
}

/**
* Tests that the admin notice with the scope 'site' appears only on the sub site when the plugin is network-activated.
*
* @group admin-notices
* @group skip-on-single-site
*/
public function test_notice_with_scope_site_shows_only_on_site() {
global $pagenow, $tax;

// set global variables.
$pagenow = 'edit-tags.php';

set_current_screen( 'edit-tags' );
$tax = get_taxonomy( 'category' );

$number_of_posts = ElasticPress\IndexHelper::factory()->get_index_default_per_page() + 10;
$term = $this->factory->term->create_and_get( array( 'taxonomy' => 'category' ) );
$this->posts = $this->factory->post->create_many(
$number_of_posts,
[
'tax_input' => [
'category' => [
$term->term_id,
],
],
]
);

add_action(
'ep_admin_notices',
function ( $notices ) {
$notices['test_notice'] = [
'type' => 'error',
'dismiss' => true,
'html' => 'Test notice',
];

return $notices;
}
);

$notices = ElasticPress\AdminNotices::factory()->get_notices();
$this->assertCount( 1, $notices );
$this->assertArrayHasKey( 'too_many_posts_on_term', $notices );
}

/**
* Tests that the admin notice with the scope 'site' has no effect when WordPress is not on multisite mode.
*
* @group admin-notices
* @group skip-on-multi-site
*/
public function test_notice_with_scope_site_has_no_effect_on_non_multisite() {
global $pagenow, $tax;

// set global variables.
$pagenow = 'edit-tags.php';

set_current_screen( 'edit-tags' );
$tax = get_taxonomy( 'category' );

$number_of_posts = ElasticPress\IndexHelper::factory()->get_index_default_per_page() + 10;
$term = $this->factory->term->create_and_get( array( 'taxonomy' => 'category' ) );
$this->posts = $this->factory->post->create_many(
$number_of_posts,
[
'tax_input' => [
'category' => [
$term->term_id,
],
],
]
);

add_action(
'ep_admin_notices',
function ( $notices ) {
$notices['test_notice'] = [
'type' => 'error',
'dismiss' => true,
'html' => 'Test notice',
];

return $notices;
}
);

$notices = ElasticPress\AdminNotices::factory()->get_notices();
$this->assertArrayHasKey( 'too_many_posts_on_term', $notices );
$this->assertArrayHasKey( 'test_notice', $notices );
}

/**
* Utilitary function to set `ep_post_mapping_version_determined`
* as the wanted Mapping version.
Expand Down

0 comments on commit 5d96e14

Please sign in to comment.