Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions docs/SETTINGS-EXPORT-IMPORT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Settings Export and Import

Copy the FrontBlocks configuration from one site to another with a JSON file. Open **FrontBlocks > Backup** in the admin menu.

## Using it

1. On the site you want to copy, open the **Backup** tab and click **Download settings file**.
2. On the other site, open the **Backup** tab, choose the file and click **Import settings**.
3. A summary lists how many settings were applied, which ones were skipped and why, and which ones were adjusted to fit the site.

Only users with the `edit_theme_options` capability (the same as the settings screen) can export or import, and both requests are protected with nonces.

## What is exported

The `frontblocks_settings` option, limited to the settings that make sense on any site:

| Group | Keys |
| --- | --- |
| Module toggles | Every checkbox of the settings screen (blocks, optional features, WooCommerce, PRO toggles such as `enable_popups` or `enable_language_banner`). A toggle that was never saved is exported as `false` (`image_max_upload_dimension_enabled`, which is on until it is saved off, is exported as `true`). Toggles of modules the site cannot use are left out, see below. |
| Options | `events_type`, `scroll_top_position`, `maintenance_title` |
| Image Management | `image_format_target`, `image_max_upload_dimension_enabled`, `image_max_upload_dimension`, `image_format_quality_webp`, `image_format_quality_avif`. When Image Management was never saved, the values it uses are exported: `none`, `true`, `2048`, `82` and `60`. |

## Content from add-ons

Settings are not the only thing an add-on may want to move. The file has an optional `content` section, filled by add-ons. FrontBlocks PRO uses it to move the **popups**, with their display settings (see its `docs/POPUPS-EXPORT-IMPORT.md`).

- The export asks the add-ons for their content, and it is written under the id of each section.
- The import applies the settings first, then hands every section to the add-on that knows it. An add-on may depend on a toggle that was just imported.
- The result shows, for each section, how many items were created and updated, what was skipped and why, and what needs a review.
- A section that no add-on on this site can import is reported as skipped. Nothing is created for it.
- At most 20 sections are imported. Any section after that is not handed to the add-ons and is reported as one skipped entry.
- The `skipped` and `notes` lists of each section keep at most 50 entries in the summary, which says how many more there were.
- Each add-on checks what the current user may create, because the import only requires `edit_theme_options`.

## What is not exported

These belong to a single site or are secret, so they are never written to the file. Set them up again on each site.

- Google Sign-In credentials (client ID and secret) and any license key.
- The maintenance image: it is a media library attachment ID.
- `enable_maintenance`, `enable_staging` and `staging_restrict_access`: they describe the state of one site, and importing them would put the destination in maintenance or staging mode. If a file has them, they are skipped and listed.
- `scroll_top_icon_url`: it usually points to the media library of the source site, so the destination would hotlink it. If a file has it, it is skipped and listed.
- The image size configuration (disabled, overridden and custom sizes) and the core size options it writes, because they depend on the sizes each theme registers.
- Activation dates, dismissed notices and other per-site state.
- Toggles of modules the exporting site cannot use: the PRO features (including custom post types) when FrontBlocks PRO is missing or has no valid license, and the WooCommerce features when WooCommerce is not active. They are off there only because the module is missing, so exporting them would turn the module off on a destination that uses it. An import leaves those settings as they are. Add-ons can change the list with the `frontblocks_export_unavailable_keys` filter.

The file contains no credentials, so it is safe to keep in a repository.

## File format

```json
{
"format": "frontblocks-settings",
"format_version": 1,
"plugin_version": "1.6.0",
"exported_at": "2026-11-11T10:00:00Z",
"source_site": "example.com",
"settings": {
"enable_back_button": true,
"enable_popups": false,
"scroll_top_position": "bottom-left"
},
"content": {
"popups": [
{ "slug": "welcome", "title": "Welcome", "status": "publish", "content": "...", "settings": { "trigger": "delay" } }
]
}
}
```

`content` is optional and only present when an add-on provides content.

`format_version` lets a future version refuse or migrate a file it does not understand. A file with a newer version than the site supports is rejected with a message asking to update FrontBlocks.

## How the import works

- **It merges.** Only the keys present in the file change. Everything else on the destination site stays as it is, including the values that are never exported.
- **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). A value that does not match is skipped and listed, never stored.
- **Unknown keys are skipped** and listed, so a file cannot write arbitrary data into the option. The summary keeps at most 50 skipped entries (key names cut to 60 characters) and says how many more there were, so a file full of unknown keys cannot make a huge stored result.
- **Values that depend on the server are adjusted.** An image format target the server cannot produce, for example AVIF without support, is downgraded the same way the settings screen does, and the summary says so.
- **The two short description options exclude each other**, as they do on the settings screen. If an import would turn both on, one is turned off and the summary says which.
- **The `frontblocks_sanitize_settings` action runs** with the merged settings, right before they are stored, as it does when the settings screen is saved.
- **Limits.** The file must be valid JSON, declare `"format": "frontblocks-settings"`, be at most 2 MB and have the `.json` extension. The file is only decoded, never executed or included. A rejected file changes nothing.

## Extending it

Add-ons such as FrontBlocks PRO can contribute their own settings with two filters. Declare how a key is validated, then add its value to the export.

```php
add_filter(
'frontblocks_import_settings_schema',
function ( $schema ) {
$schema['my_addon_mode'] = array(
'type' => 'enum',
'values' => array( 'simple', 'advanced' ),
);
return $schema;
}
);

add_filter(
'frontblocks_export_settings',
function ( $settings, $options ) {
if ( isset( $options['my_addon_mode'] ) ) {
$settings['my_addon_mode'] = $options['my_addon_mode'];
}
return $settings;
},
10,
2
);
```

Supported rule types:

| `type` | Extra keys | Accepts |
| --- | --- | --- |
| `bool` | | `true`, `false`, `1`, `0`, `"1"`, `"0"` |
| `enum` | `values` | one of the listed strings |
| `url` | | an empty string or an `http` or `https` URL |
| `text` | `max` (default 255) | text up to `max` characters (counted with `mb_strlen()`, so multibyte text is not penalized), sanitized with `sanitize_text_field()` |
| `int` | `min`, `max` | a whole number between the bounds |

### Content sections

Add-ons move content with two more filters. Return the data under a section id (lowercase letters, numbers, hyphens and underscores) on export, and on import read that section, validate everything, and add a report.

```php
add_filter(
'frontblocks_export_content',
function ( $content ) {
$content['my-addon'] = array( array( 'slug' => 'one', 'title' => 'One' ) );
return $content;
}
);

add_filter(
'frontblocks_import_content',
function ( $report, $sections ) {
if ( ! isset( $sections['my-addon'] ) ) {
return $report;
}

// Check what the user may create, validate each item and save it.
$report['my-addon'] = array(
'label' => __( 'My add-on', 'my-addon' ),
'created' => 1,
'updated' => 0,
'skipped' => array( 'Two' => 'it has no title' ),
'notes' => array( 'One' => 'check the image, it may not exist on this site' ),
);

return $report;
},
10,
2
);
```

- `created` and `updated` are counts. `skipped` and `notes` map a label to a short reason. Everything is escaped when shown.
- Replace IDs that only mean something on the source site (pages, media, forms) with something that can be found again, such as a path, and say in `notes` what the user has to review.
- Save through WordPress functions as the current user, so users without `unfiltered_html` keep getting their content filtered.

Keys that are not in the schema are skipped on import, so an add-on must declare a key before it can travel. Never add credentials or values that only make sense on the source site to the export.

## Code

- `includes/Admin/SettingsTransfer.php`: schema, file validation, import, content sections and the Backup tab.
- `Settings::get_boolean_option_keys()` is the single list of toggles, shared with the settings form sanitizer.
- The tab is added through the `frontblocks_settings_tabs` filter and the `frontblocks_settings_tab_panels` action (see [Extending Settings Tabs](./SETTINGS-TAB-EXTENSIONS.md)).
- While an import writes the option, `SettingsTransfer::is_importing()` is true. The settings form sanitizer checks it, because it normally requires the form nonce, which an import does not have.
44 changes: 32 additions & 12 deletions includes/Admin/ImageManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ public function __construct() {
*/
const JOB_TYPES = array( 'regenerate', 'convert', 'cleanup' );

/**
* Max upload width/height, in pixels, until the module is saved.
*/
const DEFAULT_MAX_UPLOAD_DIMENSION = 2048;

/**
* WebP quality until the module is saved.
*/
const DEFAULT_QUALITY_WEBP = 82;

/**
* AVIF quality until the module is saved.
*/
const DEFAULT_QUALITY_AVIF = 60;

/**
* Modern format target until the module is saved: no conversion.
*/
const DEFAULT_FORMAT_TARGET = 'none';

/**
* Register the settings section and fields.
*
Expand Down Expand Up @@ -101,7 +121,7 @@ public function section_callback() {
* @param string $target Requested target: 'none', 'webp', 'avif', or 'both'.
* @return string A target the current server can produce, or 'none'.
*/
private function downgrade_target_to_supported_formats( $target ) {
public static function downgrade_target_to_supported_formats( $target ) {
$webp_supported = wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) );
$avif_supported = wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) );

Expand Down Expand Up @@ -189,10 +209,10 @@ public function field_enable_image_management() {
$disabled = (array) ( $options['image_sizes_disabled'] ?? array() );
$overrides = (array) ( $options['image_sizes_overrides'] ?? array() );
$custom = (array) ( $options['image_sizes_custom'] ?? array() );
$format_target = (string) ( $options['image_format_target'] ?? 'none' );
$format_target = (string) ( $options['image_format_target'] ?? self::DEFAULT_FORMAT_TARGET );
$quality_by_fmt = FrontendImageManagement::get_quality_settings( $options );
$max_upload_dimension_enabled = (bool) ( $options['image_max_upload_dimension_enabled'] ?? true );
$max_upload_dimension = (int) ( $options['image_max_upload_dimension'] ?? 2048 );
$max_upload_dimension = (int) ( $options['image_max_upload_dimension'] ?? self::DEFAULT_MAX_UPLOAD_DIMENSION );

$sizes_info = $this->get_registered_sizes_info();
$usage = FrontendImageManagement::estimate_disk_usage_by_size( wp_list_pluck( $sizes_info, 'name' ) );
Expand Down Expand Up @@ -333,20 +353,20 @@ public function sanitize_settings( $value, $option ) {
$posted = wp_unslash( $_POST['frontblocks_settings'] ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- nonce verified above; every value read from $posted below is sanitized individually.

$allowed_targets = array( 'none', 'webp', 'avif', 'both' );
$posted_target = $posted['image_format_target'] ?? 'none';
$requested_target = in_array( $posted_target, $allowed_targets, true ) ? $posted_target : 'none';
$value['image_format_target'] = $this->downgrade_target_to_supported_formats( $requested_target );
$posted_target = $posted['image_format_target'] ?? self::DEFAULT_FORMAT_TARGET;
$requested_target = in_array( $posted_target, $allowed_targets, true ) ? $posted_target : self::DEFAULT_FORMAT_TARGET;
$value['image_format_target'] = self::downgrade_target_to_supported_formats( $requested_target );

$value['image_max_upload_dimension_enabled'] = ! empty( $posted['image_max_upload_dimension_enabled'] );

$max_dimension = absint( $posted['image_max_upload_dimension'] ?? 2048 );
$value['image_max_upload_dimension'] = $max_dimension >= 300 ? min( $max_dimension, 10000 ) : 2048;
$max_dimension = absint( $posted['image_max_upload_dimension'] ?? self::DEFAULT_MAX_UPLOAD_DIMENSION );
$value['image_max_upload_dimension'] = $max_dimension >= 300 ? min( $max_dimension, 10000 ) : self::DEFAULT_MAX_UPLOAD_DIMENSION;

$webp_quality = absint( $posted['image_format_quality_webp'] ?? 82 );
$value['image_format_quality_webp'] = $webp_quality > 0 ? min( $webp_quality, 100 ) : 82;
$webp_quality = absint( $posted['image_format_quality_webp'] ?? self::DEFAULT_QUALITY_WEBP );
$value['image_format_quality_webp'] = $webp_quality > 0 ? min( $webp_quality, 100 ) : self::DEFAULT_QUALITY_WEBP;

$avif_quality = absint( $posted['image_format_quality_avif'] ?? 60 );
$value['image_format_quality_avif'] = $avif_quality > 0 ? min( $avif_quality, 100 ) : 60;
$avif_quality = absint( $posted['image_format_quality_avif'] ?? self::DEFAULT_QUALITY_AVIF );
$value['image_format_quality_avif'] = $avif_quality > 0 ? min( $avif_quality, 100 ) : self::DEFAULT_QUALITY_AVIF;

$config = array();
if ( ! empty( $posted['image_sizes_config'] ) ) {
Expand Down
Loading
Loading