Skip to content

Add settings export and import in a new Backup tab - #350

Merged
davidperezgar merged 15 commits into
trunkfrom
feature/settings-export-import
Nov 19, 2026
Merged

davidperezgar merged 15 commits into
trunkfrom
feature/settings-export-import

Conversation

@Castellon-ACM

@Castellon-ACM Castellon-ACM commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Closes #351

Summary

Adds a Backup tab to Appearance > FrontBlocks that downloads the FrontBlocks settings as a JSON file and imports one on another site, so a configuration can be cloned between client sites instead of switching modules on by hand.

How it works

Export. Downloads frontblocks-settings-<host>-<date>.json with the module toggles, the simple options and the image format options that do not depend on the site. A toggle that was never saved is exported as false, so the file always describes the effective state.

Import. Merges the file over the current settings. Only the keys in the file change, and everything else stays as it was.

  • Every value is validated against a schema (boolean, one of a list, http or https URL, text up to a length, whole number between two bounds). Invalid and unknown values are skipped and listed.
  • An image format the server cannot produce is downgraded with the same check the settings screen uses, and the summary says so.
  • The two short description options stay exclusive, as on the settings screen.
  • The file must be valid JSON, declare "format": "frontblocks-settings", have a supported format_version, be at most 2 MB and end in .json. A rejected file changes nothing.
  • The result is shown once on the Backup tab: how many settings were applied, which were skipped and why, and which were adjusted.

Content from add-ons. The file has an optional content section that add-ons fill through two filters, frontblocks_export_content and frontblocks_import_content, so things that are not settings can travel too. FrontBlocks PRO uses it for popups (see the PRO pull request). The import hands each section to the add-on that knows it after the settings are applied, and the summary shows what was created, updated and skipped and what needs a review. A section no add-on can import is listed as skipped, and each add-on checks what the current user may create, because the import only requires edit_theme_options.

Never exported: Google Sign-In credentials, license keys, the maintenance image (an attachment ID), the image size configuration and per-site state. See docs/SETTINGS-EXPORT-IMPORT.md.

Security

  • Requires edit_theme_options (the capability of the settings screen) and a nonce, for both export and import. A nonce for the other action is not accepted.
  • The upload is read field by field, checked with is_uploaded_file(), and only decoded as JSON. It is never included or executed.
  • Everything printed in the result summary is escaped.
  • The export contains no credentials, so it can be kept in a repository.

Design notes

  • The form sanitizer needed a way to step aside. update_option( 'frontblocks_settings' ) goes through Settings::sanitize_settings(), which requires the settings form nonce and otherwise returns the previous value, so an import would be lost silently. SettingsTransfer::is_importing() is true only while the import writes the option, and the sanitizer returns the already validated value in that case. A test checks that a normal save without the nonce is still refused.
  • One list of toggles. The boolean settings moved into Settings::get_boolean_option_keys(), used by both the form sanitizer and the importer, so they cannot drift apart.
  • The tab is added with the existing hooks (frontblocks_settings_tabs and frontblocks_settings_tab_panels) in its own class, so Settings.php does not grow. The main save bar is hidden on this tab because it has its own forms.
  • Extensible: the frontblocks_import_settings_schema and frontblocks_export_settings filters let add-ons such as FrontBlocks PRO contribute their own keys, and the two content filters let them move content. A key that is not in the schema is skipped on import.
  • The file limit went from 256 KB to 2 MB because content such as popups can be long.
  • The class is marked @since 1.6.0. Adjust it if this ships in another version.

Testing

  • 65 new unit tests (tests/Unit/SettingsTransferTest.php): schema and value validation, the exported document, file validation, merging, the image format downgrade, the sanitizer bypass, the tab and the export and import requests (capability, nonce, bad uploads), and the content sections (export cleanup, import order, report normalization, escaping).
  • Mutation checks: removing the capability check, either nonce, the format check, the size check, the extension check, the is_uploaded_file() check, the unknown key filter, the merge or the sanitizer bypass makes tests fail.
  • Full suite: 533 tests pass (468 on trunk plus the new ones). composer lint and composer phpstan are clean. The files were also checked for syntax on PHP 7.4.
  • Manual test in Chromium on two clean WordPress installs: exported from one site and imported on the other through the screen. The 34 settings were applied, the destination kept its own Google credentials, maintenance image and custom sizes, and the export contained none of the source site's secrets. Invalid JSON, a .txt file, a newer format, another plugin's format, a file over the size limit and a file with only invalid values were rejected with a message and changed nothing. An editor gets a 403.

Notes for reviewers

  • This pull request has no dependency on FrontBlocks PRO. The PRO pull request that moves popups needs this one to be released first.
  • When Staging mode (Add Staging mode with safety measures (auto-detect beta. domains) #343) is merged, its toggles need to be added to Settings::get_boolean_option_keys() (or declared through the schema filter) to travel with the rest. Nothing breaks if they are not: they are skipped on import.
  • While testing I saw an unrelated JavaScript error on the settings screen that also exists on trunk: Settings.php calls closest('.tw:flex'), which is not a valid selector and needs the colon escaped. It is outside this change.
Open WordPress Playground Preview

- Move the list of boolean settings into Settings::get_boolean_option_keys()
  so the form sanitizer and the importer share one list of toggles.
- Let the form sanitizer hand back a value untouched while a settings
  import is writing the option. It requires the settings form nonce, which
  an import does not have, so the saved option would otherwise be replaced
  by the previous one.
- Make ImageManagement::downgrade_target_to_supported_formats() public and
  static, so an import can reuse the same server support check.
- Hide the main save bar on the Backup tab, which has its own forms.
The Backup tab downloads the FrontBlocks settings as a JSON file and
imports one on another site, to clone a configuration between sites.

- The file declares a format and a format version, and holds only settings
  that are safe to move: module toggles, simple options and the image
  format options that do not depend on the site. Credentials, license keys,
  the maintenance image and the image size configuration are never written.
- The import merges: only the keys in the file change. Every value is
  validated against a schema, and unknown or invalid values are skipped and
  listed afterwards. An image format the server cannot produce is
  downgraded, and the two short description options stay exclusive.
- Files must be valid JSON, declare the format, stay under 256 KB and have
  the .json extension. A rejected file changes nothing.
- Export and import need the edit_theme_options capability and a nonce.
- The frontblocks_import_settings_schema and frontblocks_export_settings
  filters let add-ons contribute their own settings.
Cover the schema and value validation, the exported document, file
validation, merging, the short description exclusion, the image format
downgrade, the sanitizer bypass during an import, the Backup tab and its
export and import requests (capability, nonce, bad uploads).
Add docs/SETTINGS-EXPORT-IMPORT.md with the file format, what is and is
not exported, how the import behaves and how add-ons extend it, and link
it from the readme. Add the changelog entry.
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-11T08:10:57.741464Z
We couldn’t complete this review. Comment @codex review to try again.
743be01 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

Add an optional `content` section to the settings file, filled by add-ons
through two filters, so things that are not settings, such as the popups of
FrontBlocks PRO, can travel with the rest.

- frontblocks_export_content collects the content of each add-on under a
  section id. Invalid ids and empty or non-array sections are dropped.
- frontblocks_import_content hands each section to the add-on that knows it,
  after the settings are applied, and collects a report. The report is
  normalized, and a section nothing can import is listed as skipped.
- The summary shows, for each section, what was created and updated, what
  was skipped and what needs a review, all escaped.
- A content section that is not an object rejects the file.
- Raise the file size limit from 256 KB to 2 MB, since content such as popups
  can be long.
Cover the export filter and its cleanup, the content check of the file, the import filter and its report normalization, sections without a handler, the order against the settings, and how the summary shows and escapes a section.
Describe the content section of the file, the two filters add-ons use, what an import handler must check, and the new 2 MB limit.
@davidperezgar

Copy link
Copy Markdown
Contributor

Review

No blocking security issues. Capability and nonce checks on both actions, is_uploaded_file(), the per-value schema, escaped output and the scoped is_importing() bypass all look right. The ImageManagement sanitizer already returns early without the form nonce, so it does not overwrite imported image values.

Suggested changes

  1. enable_maintenance is exported. Exporting from a staging site with maintenance on puts the destination into maintenance mode right after the import. Consider leaving it out of the schema, or at least listing it under "Adjusted" / warning in the summary.
  2. scroll_top_icon_url is tied to the source site. It usually points to the source site's media library, so after import it hotlinks the source and breaks if that site changes. Either do not export it or flag it in the summary, since the PR states that site-bound values are not exported.
  3. Content import capability (PRO side). frontblocks_import_content only requires edit_theme_options. On multisite those admins lack unfiltered_html, so the add-on must sanitize popup HTML/JS (wp_kses_post or an unfiltered_html check). Worth verifying in the PRO PR.

Minor

  1. image_max_upload_dimension_enabled is a bool in the schema but not in get_boolean_option_keys(), so when it was never saved it is omitted from the export and an import will not turn it off at the destination.
  2. skipped keys come raw from the file and go into a transient. A 2 MB file with many unknown keys creates a very large wp_options row. Consider capping the list (for example 50 entries plus "and N more").
  3. wp_json_encode() can return false; handle_export() would then download an empty file. A wp_die() in that case would be safer.
  4. If the destination already has both short description options on and the file touches neither, apply_settings() turns off move_content_to_short_description. The note explains it, but a test for that case would be good.
  5. @since 1.6.0 while FRBL_VERSION is 1.5.5: confirm it matches the planned release.
  6. The description says Closes #351; Export and import settings as JSON to clone a configuration between sites #351 does not resolve as a pull request in this repo, so check it is the intended issue.

- Resolve the conflicts with Staging Mode: enable_staging joins get_boolean_option_keys() and both changelog entries stay.
- Do not export or import site state: enable_maintenance, enable_staging and scroll_top_icon_url. A file that has them gets them skipped with a reason.
- Export image_max_upload_dimension_enabled as true when it was never saved, so an import can turn it back on.
- Cap the skipped list of the import summary at 50 entries (keys cut to 60 characters) and show how many more.
- Die with an error instead of downloading an empty file when the JSON cannot be encoded.
- Tests for all of the above and for the short description options when the file touches neither.
@Castellon-ACM

Copy link
Copy Markdown
Contributor Author

Conflicts with trunk are resolved (Staging Mode: enable_staging joins Settings::get_boolean_option_keys(), both changelog entries kept) and the review is addressed:

  • enable_maintenance / enable_staging are no longer exported or imported (new SettingsTransfer::SITE_BOUND_KEYS). A file that carries them gets them skipped with the reason "tied to the site it came from, so it is never imported".
  • scroll_top_icon_url is left out for the same reason (it points to the source media library).
  • image_max_upload_dimension_enabled is exported as true when it was never saved (it is on until saved off), so an import can turn it back on.
  • Skipped list in the summary is capped at 50 entries, keys cut to 60 characters, plus an "and N more" line.
  • wp_json_encode() failure now ends in wp_die() (500) instead of an empty download.
  • New tests for each of these and for the short description options when the destination has both on and the file touches neither (8 new, 73 in the file; full suite 625 passing, composer lint and composer phpstan clean).
  • @since 1.6.0 matches the other pending features (Staging Mode is @since 1.6.0 too).
  • Export and import settings as JSON to clone a configuration between sites #351 is an open issue in this repo ("Export and import settings as JSON to clone a configuration between sites"), so Closes #351 is right.
  • Content import capability (PRO): not part of this PR; it needs wp_kses_post / an unfiltered_html check in the add-on, to be verified in the PRO PR.

Not changed on purpose: the Staging measure checkboxes (block emails, etc.) are not exported; they only matter while staging is on, and staging itself is site state.

Castellon-ACM and others added 6 commits November 17, 2026 09:07
Staging Mode was merged with one optional measure, staging_restrict_access. It joins get_boolean_option_keys() for the settings form and, like the other staging state, is not exported or imported.
- Build the Backup URL with Settings::get_settings_url() when FrontBlocks
  has its own menu, and fall back to the Appearance screen otherwise.
- Cap the content sections of an import at 20 and the skipped and notes
  lists that add-ons report, so a crafted file cannot fill the result.
- Export the effective image values (size limit, WebP and AVIF quality,
  target format) when Image Management was never saved, using new
  default constants in ImageManagement.
- Count the length of text values in characters with mb_strlen().
- Fire frontblocks_sanitize_settings on import, as a normal save does.
- List maintenance, staging and the scroll-to-top icon in the "What is
  not included" box, and use Staging::RESTRICT_ACCESS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Link to the Backup tab with Settings::get_settings_url() only: the
  Appearance fallback and its PAGE_SLUG constant are gone now that the
  FrontBlocks menu is in trunk.
- Move the Backup entry after the post types of the FrontBlocks menu,
  right before License, so the sidebar matches the tab bar. The License
  mover becomes Settings::move_tab_entry_last().
- Refuse an export the import would reject (over 2 MB or nested too
  deeply), and report a file nested too deeply with its own message.
- Let only the value the import writes skip the form sanitizer; any other
  write during the import still needs the settings nonce.
- Show a warning, not a success, when nothing was imported, and do not
  count a setting the exclusive rule turned off as applied.
- Fix the translators comment of the exclusive-rule note and use the
  image defaults constants in Frontend\ImageManagement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A toggle of a module the site does not have (FrontBlocks PRO without a
valid license, or WooCommerce not active) is off only because the module
is missing. Exporting it as off turned that module off on a destination
that uses it, for example popups or the WooCommerce features.

The export now skips those toggles, so an import leaves them as they are
on the destination. The PRO and WooCommerce toggle lists move to
Settings::get_pro_option_keys() and get_woocommerce_option_keys(), which
the PRO cards of the settings screen also use, and add-ons can adjust
the skipped keys with the frontblocks_export_unavailable_keys filter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@davidperezgar
davidperezgar merged commit f7b67e4 into trunk Nov 19, 2026
8 checks passed
@davidperezgar
davidperezgar deleted the feature/settings-export-import branch November 19, 2026 19:49
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 and import settings as JSON to clone a configuration between sites

2 participants