Skip to content

fix: check theme directory writability before saving to prevent data loss - #914

Open
CookieDarb wants to merge 1 commit into
WordPress:trunkfrom
CookieDarb:fix/save-theme-writable-check
Open

CookieDarb wants to merge 1 commit into
WordPress:trunkfrom
CookieDarb:fix/save-theme-writable-check

Conversation

@CookieDarb

Copy link
Copy Markdown

What?

Prevents destructive data loss when attempting to save changes to an active theme whose directory lacks write permissions.

Closes #770

Why?

When saving theme changes in the Site Editor via Create Block Theme (POST /create-block-theme/v1/save), the plugin writes the updated configuration to theme files (e.g. theme.json, templates, patterns) and then clears the corresponding user customizations from the database (e.g. clear_user_styles_customizations() clearing wp_global_styles).

If the active theme directory is not writable (e.g., due to file ownership or permissions in containerized or shared hosting environments), file_put_contents() triggers a PHP warning and fails silently. However, the endpoint still proceeded to wipe user customizations from the database and returned 200 SUCCESS. The editor UI then displayed "Theme saved successfully" and reloaded the page, permanently destroying the user's unsaved changes without them ever being written to disk.

How?

  • includes/class-create-block-theme-api.php:
    • Added a pre-flight check in rest_save_theme() using wp_is_writable( get_stylesheet_directory() ).
    • If the active theme directory is not writable, the endpoint immediately halts execution and returns a 403 WP_Error (theme_not_writable). This ensures no database customizations are deleted, and triggers the existing frontend error notification snackbar in the Site Editor.
  • tests/test-class-create-block-theme-api.php:
    • Added test_save_endpoint_fails_when_theme_directory_not_writable to verify that POST /create-block-theme/v1/save rejects with HTTP 403 and code theme_not_writable when the active theme directory is not writable, with full try ... finally cleanup.

Testing Instructions

  1. Ensure the active theme directory has read-only permissions:
    chmod -R 555 wp-content/themes/<your-active-block-theme>
  2. Open the WordPress Site Editor (Appearance > Editor).
  3. Make any style change (e.g., change background color or typography under Styles).
  4. Open the Create Block Theme sidebar and click Save Changes > Save.
  5. Verify:
    • The editor displays an error notification stating that the active theme directory is not writable.
    • The page does not reload.
    • User customizations in the Site Editor are preserved and not discarded from the database.
  6. Restore permissions on the theme directory:
    chmod -R 755 wp-content/themes/<your-active-block-theme>
  7. Click Save Changes again and verify that changes save successfully.

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini
Used for: Reviewing the existing implementation and suggesting the changes. Final decisions and edits were made by me.

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.

file_put_contents function does not report write/permissions error properly

1 participant