Skip to content

feat(alerts): add export and import for alert settings#1934

Open
faisalahammad wants to merge 1 commit into
xwp:developfrom
faisalahammad:feature/1664-alert-settings-export-import
Open

feat(alerts): add export and import for alert settings#1934
faisalahammad wants to merge 1 commit into
xwp:developfrom
faisalahammad:feature/1664-alert-settings-export-import

Conversation

@faisalahammad

Copy link
Copy Markdown

Summary

Adds export and import for Stream alert settings so admins can clone alert rules across WordPress installs instead of reconfiguring each site. A new WP_Stream\Alert_Port class serializes all configured alerts to JSON on export and re-creates them on import.
Fixes #1664

Changes

New: WP_Stream\Alert_Port (classes/class-alert-port.php)

  • get_export_data() builds the payload from Alerts::get_alerts(), preserving status, alert_type, and the full alert_meta array verbatim (including third-party keys added via the wp_stream_alerts_save_meta filter). Skips ID, date, author, and title because those are site-local or regenerated on save.
  • handle_export() sends a JSON download, gated on Alerts::CAPABILITY and a nonce.
  • handle_import() handles the uploaded file via wp_handle_upload (json mime only), then appends each entry through Alert::save().
  • import_alerts() skips entries with an unknown alert_type (notifier not active on target) or non-array entries, and resets trigger_author to "any user" when the stored user ID does not exist locally (user IDs differ across sites).
  • UI renders an Export button and an Import form on the Alerts list screen via the views_edit-wp_stream_alerts filter. Result counts show as admin notices.
  • New filter wp_stream_alert_export_data for altering the export payload.

classes/class-admin.php

Instantiates Alert_Port in Admin::init() next to Export, with a matching public $alert_port property.

Why this approach

Alerts are a custom post type (wp_stream_alerts) with two post-meta rows (alert_type, alert_meta). Alerts::get_alerts() and Alert::save() already round-trip the full state, so portability is serialization plus a file form, not new DB code. Built as a standalone class (not on the Abilities API) so it works on every supported install, not only WP 6.9+ with the Abilities setting on. Import is append-only because alerts have no stable natural key (title is derived).

Testing

Test 1: Export

  1. Create one or two alerts on Stream > Alerts.
  2. Click Export above the list table.
  3. Open the downloaded stream-alerts.json.
    Result: every alert is listed with its trigger and notifier settings, no ID/date/author.

Test 2: Import round-trip

  1. Export the file.
  2. Delete one alert on the Alerts screen.
  3. Import the file.
    Result: the alert reappears with the original settings. Success notice shows imported/skipped counts.

Test 3: Unknown notifier type

  1. Edit the JSON to set one alert alert_type to a value not registered on the install.
  2. Import it.
    Result: that alert is skipped, others import. Notice reports the skipped count.

Test 4: Missing user on target site

  1. Import a file where trigger_author is a user ID that does not exist.
    Result: the alert imports with author reset to "any user" (not skipped).

Automated: PHPCS (main and tests standards) clean. PHPUnit Test_Alert_Port passes, 7 tests / 29 assertions, covering export shape, import round-trip, unknown-type skip, missing-user reset, and non-array entry skip.

- Add WP_Stream\Alert_Port class to serialize alert rules to JSON and import them back
- Export downloads all alerts with triggers and notifier settings intact
- Import appends new alerts, skips unknown notifier types, resets missing trigger authors to any user
- Gate export/import on the Stream settings capability with nonce checks
- Wire the controls into the Alerts list screen via the views filter

Lets admins clone alert settings across WordPress installs without reconfiguring each one.

Fixes xwp#1664
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.

🆕 Export / import alert settings

1 participant