Skip to content

fix(alerts): stop non-stop email alerts - #2007

Open
faisalahammad wants to merge 1 commit into
xwp:developfrom
faisalahammad:fix/1855-non-stop-alerts
Open

fix(alerts): stop non-stop email alerts#2007
faisalahammad wants to merge 1 commit into
xwp:developfrom
faisalahammad:fix/1855-non-stop-alerts

Conversation

@faisalahammad

Copy link
Copy Markdown

Fixes #1855.

Email alerts fired on every matching record with no limit. A user whose alert matched routine settings records received an email every few minutes on every page load, and deleting the alerts or the plugin did not stop it: alert posts survived deletion because the plugin ships no uninstall routine (data removal was disabled in 3.9.3), so a reinstall brought the old alerts back.

Repro: create an email alert on Settings > Updated, then change any whitelisted setting the way a plugin does on page load. Stream inserts a matching record per request and Alert_Type_Email::alert() calls wp_mail() for each one. Trash or delete the alert, then deactivate and delete the plugin, and emails stop only until the alert post is found in the database again on the next install.

This PR adds a per-alert send throttle and restores cleanup on plugin deletion.

Changes

alerts/class-alert-type-email.php

Before:

public function alert( $record_id, $recordarr, $alert ) {
    $options = wp_parse_args( ... );
    ...
    wp_mail( $options['email_recipient'], $options['email_subject'], $message );
}

After:

public function alert( $record_id, $recordarr, $alert ) {
    if ( ! $this->is_send_allowed( $alert ) ) {
        return;
    }
    ...
    $this->update_last_sent( $alert );
    wp_mail( $options['email_recipient'], $options['email_subject'], $message );
}

Why: one email per alert per 300 seconds bounds the flood while the trigger keeps matching. The interval is filterable with the new wp_stream_alert_email_throttle filter, 0 restores the old per-record behavior. The last-sent time lives in a separate _wp_stream_email_last_sent post meta key, so it does not appear in alert_meta output such as the stream/get-alerts ability.

uninstall.php (new)

Why: deleting the plugin now removes all wp_stream_alerts posts on every site of a network, Stream options per site and network site options (wp_stream, wp_stream_network, wp_stream_db*), Stream user meta, and the WP-Cron and Action Scheduler purge/reset events. Deletes use core APIs (wp_delete_post, delete_option, delete_user_meta, delete_site_option) so hooks and object caches stay consistent; a reinstall can no longer resurrect old alerts. The wp_stream and wp_streammeta log tables are kept, following the data-removal decision from 3.9.3.

One note for the reporter of #1855: the cleanup runs when the fixed version is deleted from the Plugins screen. A site already affected by 4.1.2 needs one reinstall-then-delete cycle to clear the leftover alert posts.

Testing

Automated:

  • New Test_Alert_Type_Email covers: send allowed with no prior email, blocked within the interval, allowed after the interval, 0 interval sends every time, and one real send per window through a mocked mailer.
  • Full suite: 435 tests pass on single site and multisite (npm run test:php, npm run test:php-multisite).
  • npm run lint:php and npm run lint:php-tests: 0 errors.

Manual:

  1. Create an email alert for Posts > Updated.
  2. Quick-edit and update the same post three times within a minute.
  3. Result: one email, not three. After 5 minutes the next trigger sends again.

Checklist

  • Project documentation has been updated to reflect the changes in this pull request, if applicable.
  • I have tested the changes in the local development environment (see contributing.md).
  • I have added phpunit tests.

Release Changelog

  • Fix: Throttle email alerts to one send per alert every 5 minutes, with a wp_stream_alert_email_throttle filter to change or disable the interval.
  • Fix: Remove alert configuration, Stream options, user meta, and scheduled jobs when the plugin is deleted, so old alerts cannot come back after a reinstall.

Release Checklist

  • This pull request is to the master branch.
  • Release version follows semantic versioning. Does it include breaking changes?
  • Update changelog in readme.txt.
  • Bump version in stream.php.
  • Bump Stable tag in readme.txt.
  • Bump version in classes/class-plugin.php.
  • Draft a release on GitHub.

Change [ ] to [x] to mark the items as done.

Throttle email alerts to one send per 5 minutes per alert, and remove
alert configuration, options, and scheduled jobs on plugin uninstall.

Repeating matching records no longer flood the inbox, and deleting the
plugin no longer leaves enabled alerts behind to fire again.

Fixes xwp#1855
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non Stop Alerts

1 participant