=== Subset Sync ===
Contributors: functionform
Tags: migration, database, backup, multisite, export
Requires at least: 6.5
Tested up to: 7.1
Requires PHP: 8.1
Stable tag: 0.21.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Move a WordPress database and media library between sites: push, pull, export and import, with serialization-safe find & replace.

== Description ==

A slimmed-down take on WP Migrate DB Pro: move a WordPress **database** and **media library** between sites. Themes and plugins are out of scope on purpose.

* **Push**: replace a remote site's database and/or uploads with this site's.
* **Pull**: replace this site's database and/or uploads with a remote site's.
* **Export**: download the database as `.sql.gz`, optionally rewritten for another URL and path.
* **Import**: replace this site's database from a `.sql` / `.sql.gz` file. It reads dumps from this plugin, WP Migrate and `mysqldump`.

Site URLs, `http`/`https` differences and file paths are rewritten automatically, including inside serialized and JSON data. You can add your own find & replace pairs too. URLs match case-insensitively and only whole host names (`example.com` leaves `example.com.au` alone); file paths and your own pairs match exactly, and every pair applies in a single pass, so one pair's result is never rewritten by another.

= Multisite =

A network is migrated as a whole: every subsite's tables, the network tables and all of `uploads/` (including `uploads/sites/N`). Both sides must be networks of the same type. Supported directions are network to network (subdomain to subdomain, or subdirectory to subdirectory), single site to single site, and one subsite to or from a single site. To move a subsite, choose it in the admin screen or pass `--subsite=<id|url>` to push, pull or export. A single site can only go into a subsite that already exists. Anything else is refused before any data changes.

= How it works =

* Jobs run in resumable steps, so big sites aren't limited by PHP timeouts or request-size limits. If a request fails or the browser closes, the job can be resumed from the admin screen or with WP-CLI.
* Sites talk through the REST API, falling back to `admin-ajax.php` if the remote's REST API is blocked.
* Tables are built next to the live ones and swapped in with a single atomic rename, so the site never sees a half-migrated database.
* Find & replace never unserializes data. Serialized values are rewritten by a tokenizer that changes only string contents, so PHP object injection isn't possible.
* Before a push, pull or import replaces a database, that database is backed up first. Backups are listed on the Backups screen and can be downloaded, restored or deleted.

= Security =

* Every site-to-site request is signed with HMAC-SHA256 over the action, a timestamp, a single-use nonce and the exact request body. Stale or replayed requests are refused.
* Each action is checked against the destination's *Allow push* / *Allow pull* settings, both off by default.
* SQL from a remote site can only touch temporary copies of tables carrying this site's own prefix; live tables change only during the final swap.
* File paths are checked to stay inside `uploads`, and executable or server-config files are never sent or written.

== Installation ==

1. Install and activate the plugin on both sites. On multisite, network-activate it.
2. On the site you'll connect **to**, open **Subset → Sync → Connection** and tick *Allow push* and/or *Allow pull*. Both are off by default. On multisite the screen is at **Network Admin → Subset → Sync**.
3. Copy that site's connection info (its URL plus a secret key) and paste it into Subset Sync on the other site.

== Frequently Asked Questions ==

= Does this migrate themes and plugins? =

No. Only the database and the media library (`uploads/`) are in scope. Views, triggers, stored routines and events aren't migrated either, only tables and their data.

= Is a subsite ↔ single-site migration supported? =

Yes, since 0.5.0. Choose the subsite in the admin screen or pass `--subsite=<id|url>` to push, pull or export. A single site can only be moved into a subsite that already exists, and importing a file into a subsite isn't supported.

= What happens to `home`, `siteurl` and my connection settings after a pull or import? =

They stay with the destination, along with `upload_path`, `upload_url_path`, `blog_public` and this plugin's own activation state. Use the `wpmig_preserved_options` filter to keep more.

= I'm signed out after a pull or import from the admin screen. Why? =

A pull or import replaces the users table. You're signed back in as the user with the same username in the new data; if that user doesn't exist or can't manage the plugin, you're signed out and need to log in with an account from the source.

= Should I skip the backup on a large site? =

Maybe. Before a push, pull or import replaces a database, Subset Sync backs it up first, and on a large database that can take about as long as the migration itself. The preview (and `--dry-run`) show the backup's estimated size up front, so you can judge that cost before starting. If your host already takes its own backups, or you have a recent export on hand, untick *Back up … before replacing it* (or pass `--skip-backup`) to skip Subset Sync's.

= What happens to backups and settings if I delete the plugin? =

Deleting the plugin (not just deactivating it) removes everything it made: its settings and keys, leftover migration tables, and `uploads/.wpmig` including every backup. Download any backup you want to keep first.

= How do I upgrade from WP Migration? =

Up to 0.8.0 this plugin was called WP Migration, in a `wp-migration` folder. Subset Sync lives in a new folder, so WordPress installs it alongside the old copy rather than updating it. Both use the same settings and storage, so your settings, saved connections and backups carry over. On every site you migrate between:

1. Deactivate WP Migration.
2. Install and activate Subset Sync (network-activate it on multisite).
3. Delete WP Migration.

Do step 2 before step 3. WP Migration's uninstaller deletes the settings, connections and backups the two share, and only an active Subset Sync stops it. If Subset Sync isn't active yet, remove the old copy without running its uninstaller: `wp plugin delete wp-migration`, or delete its folder.

Update every site you migrate between: 0.9.0 won't connect to a site still on WP Migration 0.8.0 or earlier.

While WP Migration is still installed, Tools → Site Health and a note under its row on the Plugins screen say what to do with it.

= I uploaded the new version by hand and now see Subset Sync twice =

Before 0.17.0 the plugin lived in a folder called `subset-migration`, so uploading 0.17.0 adds it next to the old copy instead of replacing it. Both copies use the same settings. Deactivate the old copy, then delete it: the Plugins screen marks which one is the old copy. If you update through the Subset Installer you won't see this, because the update replaces the old folder.

= Is this available on the command line? =

Yes, via WP-CLI: `wp subset sync push`, `pull`, `export`, `import`, `resume`, `cancel`, `backups`, `restore`, `connections`, `key regenerate`, `settings` and more. Everything the admin screen does can be done from the command line. See the plugin's README for the full command list.

== Screenshots ==

1. Subset → Sync: push or pull a database and media library between two connected sites.
2. Connection: allow push and/or pull, and share a site's URL and secret key.
3. Backups: download, restore or delete database backups made before a push, pull or import.

== Changelog ==

= 0.21.1 =
* Security: The Subset menu and its Overview page now need the same capability as a Subset page you can open, so a subscriber no longer reaches the Overview.
* Changed: Built on subset-core 1.13.3.

= 0.21.0 =
* Security: Update Subset Sync on both sites to get this check. Each push or pull request now names the site it was signed for, and a site on 0.21.0 refuses one meant for another site that shares its connection key (production and a staging copy of it, say). Sites on 0.20.x still push and pull to and from this version as before; they just don't check the target.
* Changed: If you connect to a site at an address WordPress doesn't know as its own (by IP address, or through a proxy that changes the host), and that site runs 0.21.0 or later, push and pull now fail with "The request was signed for …". Connect with the site's WordPress Address, or accept the extra address on that site with the `wpmig_signed_target_matches` filter.
* Security: Saved connection keys are now stored encrypted with `AUTH_KEY` and `AUTH_SALT` from wp-config.php. Existing keys are encrypted when you update. If those salts change later, using a connection says it can't be read: Re-key it. Sites without those salts in wp-config.php keep storing keys as before. If you go back to an earlier version, re-key your saved connections.
* Security: Importing a .sql file only loads tables with this site's table prefix. Other tables are skipped and logged, and a file with none of this site's tables isn't imported. Statements that could read or change other tables are refused.
* Security: A push or pull can no longer drop or copy tables outside the WordPress prefix in a shared database.
* New for developers: `wpmig_signed_target_matches` accepts extra addresses as this site's, and `wpmig_require_signed_target` refuses requests from sites on releases before 0.21.0.
* Changed: Built on subset-core 1.13.2.

= 0.20.2 =
* Fixed: `wp help` shows whole sentences as the summaries of the `diff`, `connections`, `status` and `history` commands, not cut-off ones.
* Changed: Built on subset-core 1.11.2.

= 0.20.1 =
* Changed: In a terminal, the preview before a push or pull is laid out as aligned rows and leaves out what changes nothing: skips with no rows, media with nothing to send, and samples find & replace doesn't touch. Each sample is one line starting just before the rewrite. Piped output is unchanged.
* Fixed: The pull confirmation read "Overwrite this site's the database…".

= 0.20.0 =
* Changed: WP-CLI commands are now `wp subset sync …`. `wp subset migration …` and `wp migration …` still work, with a warning, until 1.0.
* New: In a terminal, push, pull, export, import, restore and resume show a live list of stages with a spinner, progress bar, speed and time left. Pass `--verbose` to see every log line, including one per table. Piped output and `--format=json` are unchanged.
* New for developers: `WPMigration\Plugin::CLI_COMMAND` names the command add-ons register under.

= 0.19.0 =
* Changed: Add-ons now hook `wpmig_admin_tab_{slug}`; the old `_admin_panel_` hook still works but is deprecated.
* New for developers: `wpmig_admin_enqueue` now gets the current tab.
* Under the hood: shares more code with the other Subset plugins.

= 0.18.0 =
* Changed: Push and pull now refuse a remote site on a local or private address (such as `127.0.0.1` or `192.168.x.x`), on a port other than 80, 443 or 8080, or whose host doesn't resolve. The error says why. To sync two sites on one development machine or a local network, add `add_filter( 'wpmig_reject_unsafe_urls', '__return_false' );` to a must-use plugin.
* New: The Sync screen shows a warning while "Verify SSL certificates of remote sites" is off.

= 0.17.0 =
* Changed: The plugin now lives in a `subset-sync` folder, with the text domain `subset-sync`. If you update through the Subset Installer, nothing changes for you: the update moves it and keeps it active. Settings, saved connections, backups and integrations keep working, and sites on the old folder still connect to sites on the new one.
* New: If you upload this version by hand, it lands next to the old `subset-migration` copy. It waits until you deactivate the old copy, then takes over with the same settings, and the Plugins screen and Site Health tell you to delete the old copy.
* Changed: After a push or pull from a site still on the old `subset-migration` folder, only this copy stays active, not the old one as well.
* Changed: Built on subset-core 1.10.0.

= 0.16.2 =
* Fixed: `wp help` for the `settings` command is written for users, and its examples use this plugin's own command.
* Changed: Built on subset-core 1.9.1.

= 0.16.1 =
* WordPress no longer logs a "translation loading was triggered too early" notice for this plugin on admin pages.

= 0.16.0 =
* The connection key is stored in its own option, and can be set in wp-config.php with the `WPMIG_KEY` constant. While the constant is set, the key can't be changed from the Sync screen or WP-CLI. A key saved by an earlier version keeps working and moves across by itself.
* Settings are saved through the shared Subset settings code, and `wp subset migration settings get` accepts `--format`.
* Subset Sync Pro can reuse this plugin's signed-request code for file transfers.
* Built on subset-core 1.9.0.

= 0.15.0 =
* Subset Sync now requires WordPress 6.5 or later.
* Media library: a folder path sent by the other site must be inside this site's uploads folder, so a connected site can no longer create folders elsewhere on the server.
* Site Health: the secret-file check is labelled "Subset Sync" instead of "Security", and the label turns red when the problem is critical.
* The daily cleanup of abandoned jobs is scheduled when the plugin is activated or updated, and deactivating the plugin on a network clears it on every site.
* The readme now says a subsite can move to or from a single site, which has worked since 0.5.0.
* Built on subset-core 1.8.1.

= 0.14.4 =
* Subset Migration is now called Subset Sync, and its screen is Subset → Sync. Only the name changes: the plugin folder, settings, saved connections, backups and `wp subset migration` commands stay the same.
* Site Health: the check that your server keeps Subset Sync's file-naming secret private now runs in the background, so the Site Health screen no longer waits up to five seconds for it to load.

= 0.14.3 =
* Migrate tab: the Remote subsite field in Advanced options now only appears when the other site is a multisite network.
* Migrate tab: the automatic find & replace list is folded into a one-line summary you can open to see every pair.
* Preview and progress: moving focus to the heading no longer draws a clipped line across the page.
* Connection tab: "Verify SSL certificates of remote sites" has its own row, and this site's connection info no longer repeats its heading in the toggle.
* Backups: a shorter empty-list message.
* Long file paths in the find & replace list wrap on narrow screens.

= 0.14.2 =
* Connection tab: renamed from "Connection settings". It now opens on Settings, then Saved connections, and this site's connection info is collapsed until you ask for it, so the key is not shown in plain text.
* Migrate tab: when Preview push or Preview pull is unavailable, the reason is now shown next to the button and read out by screen readers. Activating the button moves you to the connection field.
* Migrate tab: each option says in a line what it does to the site (Export is marked safe), a hint explains that images and other uploads only move when Media library is ticked, and the intro says Push and Pull need Subset Migration on a second site.
* Preview: says the database covers posts, pages, comments, users and settings.
* After an import that signs you out, the notice now says why and what to do.
* Backups: the empty list explains when backups are taken, and add-ons can place their buttons beside the list.
* History: Failed and Cancelled runs are marked in words and weight, not only text.
* The WP-CLI hint is now on the Migrate tab as well.
* Intro and description text no longer runs across the whole screen, the empty gap under the heading is gone, and switching tabs no longer draws a box around the panel.

= 0.14.1 =
* Export no longer scrolls sideways on a phone.
* History: on small screens each run stacks into one block instead of scrolling sideways, and "Run again" stays visible.
* Expandable sections such as Advanced options get the same focus outline as the other controls, and the × that removes a row on the Migrate tab is easier to tap.

= 0.14.0 =
* Migrate tab: the options stay hidden until you have a valid connection, and the hint now sits under the connection field. The Advanced options heading lists what you have changed from the defaults, the table list collapses and no longer scrolls inside itself, and mobile layouts are fixed.
* History: "Run again" sets up the Migrate tab for the same site and direction (nothing runs until you start it), dates use your site's date format, and the arrows between sites now point one way.
* Backups: the screen states the real number of backups kept.
* Connection settings: "Generate new key" now says what it will break, and there is a WP-CLI hint.
* Screen readers: the Backups, History and table lists now announce that they are loading.
* About tab: plain wp-admin styling.

= 0.13.1 =
* Accessibility and design polish: the screen now mirrors correctly in right-to-left languages, the progress bar and selected options stay visible in high-contrast mode, focus outlines show only for keyboard users, and the progress heading and diff markers are cleaner for screen readers and easier to read.
* `?tab=<slug>` now opens a given tab directly. Add-ons can add their own rows and settings to the Settings form.

= 0.13.0 =
* The screen moves to the shared Subset menu: Subset > Migration (Network Admin > Subset > Migration on multisite). The old Tools and Network Settings URLs redirect, and the page slug is unchanged.

= 0.12.1 =
* On a push or pull, the options after the connection stay faded and can't be used, and the Preview button is disabled, until a saved connection is picked or valid connection info is pasted.

= 0.12.0 =
* The Migrate tab shows which site a migration replaces: each choice draws its direction with the replaced end marked, nothing is pre-selected, and the preview opens with a From → To diagram.
* Replacing a database now always asks for confirmation: a checkbox naming the destination, or typing its domain when it's marked production.
* Warnings when the backup is off or media deletion is on; first-run setup steps and a check of pasted connection info; Advanced options grouped, with a summary of what changed.
* A finished migration ends on what happened, where the backup is and a link to the site; after a failure or cancel, it says first that the destination's database is unchanged.
* Add-ons can add tabs and sections to the admin screen (`wpmig_admin_tabs` and friends); Migration Pro uses them for its Files and License tabs.
* Fixed a PHP warning when saving settings stored before the production setting existed.

= 0.11.0 =
* Pull and save / Push and save: when the connection info you paste is for a site you haven't saved, a second button saves the connection (named after the remote's host) once the preview shows it works, then runs the migration against it so your options are remembered.
* Each push or pull on the History tab has a Migrate button that sets the Migrate tab up for the same site again: its saved connection and options if there is one, otherwise its URL, ready for the key.

= 0.10.0 =
* The migrate form lists the find & replace a push or pull makes on its own (the remote site's URL to this one's on a pull, and the reverse on a push, plus http ↔ https and the file path) as soon as the remote site is entered, under Advanced options.

= 0.9.0 =
* WP Migration is now Subset Migration, in a new `subset-migration` folder, so WordPress installs it as a new plugin rather than updating the old one. Your settings, saved connections and backups carry over. See "How do I upgrade from WP Migration?" in the FAQ before removing the old copy.
* While WP Migration is still installed, the Plugins screen says what to do with it. With Subset Migration active, deleting WP Migration from the Plugins screen no longer deletes the settings, connections and backups the two share.
* WP-CLI commands are now `wp subset migration …`. `wp migration …` still works, with a warning, until 1.0.
* Update every site you migrate between: SQL now travels as raw compressed bytes instead of base64 inside JSON (a new site-to-site protocol), so a site on 0.9.0 won't connect to one on 0.8.0 or earlier. Incompressible data sends about a quarter fewer bytes, and building a chunk takes about a third less memory on both sides.
* Faster database loads: each chunk's rows are committed in batches instead of one commit per INSERT, and push requests are filled to the remote's limit instead of about a third of it.
* PHP 8.1 or newer is now required (it was 7.4). Sites on an older PHP keep the previous version until PHP is upgraded.
* The request-signing and shared plumbing now come from the shared Subset Core library, bundled inside the plugin under its own namespace.
* Fixed: exporting one subsite of a network restarted `usermeta` from the beginning every step, so a large one never finished.
* Fixed: importing a plain `mysqldump` with no header read almost the whole file before starting, which timed out on hosts with a 60-second limit.
* Fixed: picking a saved connection that remembered `--tables` on the admin screen migrated every table.
* New: an About tab with a short introduction to Subset and a link to subsetwp.com.
* Fixed: migrating a network to one under a different path (say `/` to `/net/`) gave subsites on a mapped custom domain that path too (`custom-shop.com/net/`). They keep their own now.
* For developers: the `wpmig_excluded_options`, `wpmig_network_map` and `wpmig_network_conversion` filters, so another plugin can keep its own options out of every export and map subsites between network types.

= 0.8.0 =
* Faster media transfer: files travel as raw bytes instead of base64 inside JSON, cutting a 1 GB push from 75s to 60s, a third less data on the wire, and less than half the memory on the receiving site.
* Faster database loads: staging tables are created without their secondary indexes, which are built once each table is fully loaded instead of row by row.
* Fixed: pulling a media file larger than 16 MB could drop a chunk, either failing with "Out-of-order chunk" or leaving a truncated file behind while reporting success.
* Fixed: a media pull from a remote site low on memory could stall forever. It now always makes progress, or stops with a clear error you can resume from.
* A site on a different protocol version is refused on every request, not only when connecting, so a job resumed after one site updates stops with a clear "update both sites" message.

= 0.7.1 =
* Fixed: pulling a media file larger than 16 MB could drop a chunk, either failing with "Out-of-order chunk" or leaving a truncated file behind while reporting success.

= 0.7.0 =
* Security: working-file names now also depend on `AUTH_KEY`/`AUTH_SALT` from wp-config.php, so a host that serves PHP files in uploads as plain text no longer exposes the export's file name. A Site Health check warns about such hosts.
* Security: importing a .sql file on a server with `NO_BACKSLASH_ESCAPES` no longer breaks (or lets the file inject SQL into) the find & replace step.
* Fixed: find & replace stopped rewriting anything above about 225 pairs (a multisite network with that many subsites on their own domains). It now handles any number and refuses loudly if it ever can't.
* Fixed: a file import with a different table prefix renamed any option ending in `user_roles`, not just WordPress's own.
* Settings can be locked from wp-config.php: `WPMIG_ALLOW_PUSH`, `WPMIG_ALLOW_PULL`, `WPMIG_IS_PRODUCTION` and `WPMIG_VERIFY_SSL`.
* `--tables` migrates only the tables you list (the opposite of `--exclude-tables`), and saved connections remember it.
* `wp subset migration status` shows the current job without advancing it, and `--format=json` gives push, pull, export and import one machine-readable summary.
* Migration history: a History tab and `wp subset migration history` list the last 50 migrations, including CLI ones.
* A `wpmig_migration_finished` action fires once for every migration, admin or CLI.
* Faster on large sites: keyset paging for tables without an integer primary key (12x faster on a 1M-row table), a single find & replace pre-check (38x faster with 500 pairs), batched table drops when a migration finishes, and chunk sizes that grow on fast links (3x faster over a slow one) without exceeding the site's memory.
* Backups are written faster (gzip level 3), and the preview and `--dry-run` show their estimated size.

= 0.6.0 =
* Admin screen redesign: every native alert, confirm and prompt dialog is replaced with inline confirmations and WordPress notices, and destructive actions are shown in red.
* Migrate tab: options appear only when they apply, less-used ones sit under Advanced options, and missing input is flagged next to the field.
* Table checklist: search, select all or none, and a count of what's left out. On a pull it lists the remote site's tables.
* The import confirmation and a production site's domain are entered in the preview panel, and a job can't be started twice by clicking again.
* After a failed or cancelled migration, the progress panel points to the backup that was taken, and stays visible after a reload until dismissed.
* Accessibility: keyboard-navigable tabs, focus moved to each new panel, quieter screen reader announcements, labelled fields and better contrast.
* Works on narrow screens, and help text is shorter and more consistent.

= 0.5.0 =
* Saved connections: pick a remote by name instead of pasting its URL and key (`wp subset migration pull staging`).
* Preview before replacing anything: tables, media, every find & replace pair and sample rewrites. `--dry-run` shows it and stops.
* Production guard: mark a site as production, and type its domain to push to, pull into, import into or restore it.
* Choose which tables to migrate (`--exclude-tables`). Core tables can't be skipped.
* Optionally skip spam and trashed comments, revisions and auto-drafts (`--lean`).
* Transfer speed and time left in the progress bar and the CLI.
* `wp subset migration diff` compares two sites without changing anything.
* Optional email when a migration you started finishes, fails or stalls, plus a `wpmig_job_notification` action.
* Multisite: migrate one subsite to a single site and back (`--subsite`). Users merging into a network are matched by login only.
* Translatable, with a `.pot` template.
* Fixed: about one connection key in 64 started with "-", which WP-CLI read as an option.

= 0.4.0 =
* First public release: push, pull, export and import of the database and media library, serialization-safe find & replace, multisite support, resumable jobs, automatic backups and a WP-CLI interface.

== Upgrade Notice ==

= 0.9.0 =
A new plugin folder and site-to-site protocol. Deactivate WP Migration, then install and activate Subset Migration on every site you migrate between, before deleting the old copy.

= 0.8.0 =
Update every site you migrate between: 0.8.0 uses a new site-to-site protocol and won't connect to sites still on 0.7.x or earlier.

= 0.7.1 =
Fixes truncated media files when pulling large uploads. Connects to sites on 0.5.0 to 0.7.0.

= 0.7.0 =
Security and large-site fixes. Connects to sites on 0.5.0 and 0.6.0, so you can update sites one at a time.

= 0.6.0 =
A cleaner, more accessible admin screen. Connects to sites on 0.5.0, so you can update sites one at a time.

= 0.5.0 =
Update every site you migrate between: 0.5.0 uses a new site-to-site protocol and won't connect to sites still on 0.4.0.

= 0.4.0 =
First public release.
