Skip to content

Commit

Permalink
Send separate notification per plugin/theme when update is available
Browse files Browse the repository at this point in the history
Fixes #167.
  • Loading branch information
chesio committed Jan 9, 2025
1 parent 24c8e4b commit e24e46a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## Upcoming version 0.26.0 (????-??-??)

...
### Changed

* Separate plugin and theme update notification is sent for each available plugin and theme update [#167](https://github.com/chesio/bc-security/issues/167). Legacy behaviour can be enabled with following filters: `bc-security/filter:all-plugin-updates-in-one-notification` for plugins and `bc-security/filter:all-theme-updates-in-one-notification` for themes.

## Version 0.25.0 (2024-10-28)

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ Note: _Known IP address_ is an IP address from which a successful login attempt

You can mute all email notifications by setting constant `BC_SECURITY_MUTE_NOTIFICATIONS` to `true` via `define('BC_SECURITY_MUTE_NOTIFICATIONS', true);`. If you run a website in multiple environments (development, staging, production etc.), you may find it disturbing to receive email notifications from development or any environment other than production. Declaring the constant for particular environment only is very easy, if you use a [multi-environment setup](https://github.com/chesio/wp-multi-env-config).

By default plugin/theme update notifications are sent separately for each discovered plugin/theme update, but you can use following filter to send only one notification in case there are multiple plugin/theme updates discovered at once:
* `bc-security/filter:all-plugin-updates-in-one-notification`
* `bc-security/filter:all-theme-updates-in-one-notification`

### Events logging

Following events triggered by BC Security are logged:
Expand Down
21 changes: 21 additions & 0 deletions classes/BlueChip/Security/Modules/Notifications/Hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace BlueChip\Security\Modules\Notifications;

/**
* Hooks available in notifications module
*/
interface Hooks
{
/**
* Filter: whether to send one notification in case there are multiple plugin updates discovered at once?
*/
public const ALL_PLUGIN_UPDATES_IN_ONE_NOTIFICATION = 'bc-security/filter:all-plugin-updates-in-one-notification';

/**
* Filter: whether to send one notification in case there are multiple theme updates discovered at once?
*/
public const ALL_THEME_UPDATES_IN_ONE_NOTIFICATION = 'bc-security/filter:all-theme-updates-in-one-notification';
}
34 changes: 30 additions & 4 deletions classes/BlueChip/Security/Modules/Notifications/Watchman.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,23 @@ private function watchPluginUpdatesAvailable($update_transient): void
return empty($notified_version) || \version_compare($notified_version, $plugin_update_data->new_version, '<');
}, ARRAY_FILTER_USE_BOTH);

if (empty($plugin_updates)) {
return;
if ($plugin_updates !== []) {
if (apply_filters(Hooks::ALL_PLUGIN_UPDATES_IN_ONE_NOTIFICATION, false)) {
$this->notifyAboutPluginUpdatesAvailable($plugin_updates);
} else {
foreach ($plugin_updates as $plugin_file => $plugin_update_data) {
$this->notifyAboutPluginUpdatesAvailable([$plugin_file => $plugin_update_data]);
}
}
}
}


/**
* @param array<string,object> $plugin_updates Plugin file and related update object.
*/
private function notifyAboutPluginUpdatesAvailable(array $plugin_updates): void
{
$subject = __('Plugin updates available', 'bc-security');
$message = new Message();

Expand Down Expand Up @@ -265,10 +278,23 @@ private function watchThemeUpdatesAvailable($update_transient): void
return empty($last_version) || \version_compare($last_version, $theme_update_data['new_version'], '<');
}, ARRAY_FILTER_USE_BOTH);

if (empty($theme_updates)) {
return;
if ($theme_updates !== []) {
if (apply_filters(Hooks::ALL_THEME_UPDATES_IN_ONE_NOTIFICATION, false)) {
$this->notifyAboutThemeUpdatesAvailable($theme_updates);
} else {
foreach ($theme_updates as $theme_slug => $theme_update_data) {
$this->notifyAboutThemeUpdatesAvailable([$theme_slug => $theme_update_data]);
}
}
}
}


/**
* @param array<string,object> $theme_updates Theme slug and related update object.
*/
private function notifyAboutThemeUpdatesAvailable(array $theme_updates): void
{
$subject = __('Theme updates available', 'bc-security');
$message = new Message();

Expand Down

0 comments on commit e24e46a

Please sign in to comment.