feat(alerts): add export and import for alert settings#1934
Open
faisalahammad wants to merge 1 commit into
Open
feat(alerts): add export and import for alert settings#1934faisalahammad wants to merge 1 commit into
faisalahammad wants to merge 1 commit into
Conversation
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_Portclass 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 fromAlerts::get_alerts(), preservingstatus,alert_type, and the fullalert_metaarray verbatim (including third-party keys added via thewp_stream_alerts_save_metafilter). SkipsID, date, author, and title because those are site-local or regenerated on save.handle_export()sends a JSON download, gated onAlerts::CAPABILITYand a nonce.handle_import()handles the uploaded file viawp_handle_upload(json mime only), then appends each entry throughAlert::save().import_alerts()skips entries with an unknownalert_type(notifier not active on target) or non-array entries, and resetstrigger_authorto "any user" when the stored user ID does not exist locally (user IDs differ across sites).views_edit-wp_stream_alertsfilter. Result counts show as admin notices.wp_stream_alert_export_datafor altering the export payload.classes/class-admin.phpInstantiates
Alert_PortinAdmin::init()next toExport, with a matchingpublic $alert_portproperty.Why this approach
Alerts are a custom post type (
wp_stream_alerts) with two post-meta rows (alert_type,alert_meta).Alerts::get_alerts()andAlert::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
stream-alerts.json.Result: every alert is listed with its trigger and notifier settings, no ID/date/author.
Test 2: Import round-trip
Result: the alert reappears with the original settings. Success notice shows imported/skipped counts.
Test 3: Unknown notifier type
alert_typeto a value not registered on the install.Result: that alert is skipped, others import. Notice reports the skipped count.
Test 4: Missing user on target site
trigger_authoris 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_Portpasses, 7 tests / 29 assertions, covering export shape, import round-trip, unknown-type skip, missing-user reset, and non-array entry skip.