Skip to content

Conversation

@mgiannopoulos24
Copy link
Contributor

Description

This PR introduces a "Lock" feature for code snippets. When a snippet is locked, it becomes read-only in the editor and cannot be
trashed or permanently deleted until it is unlocked. This provides an essential safety layer for critical production snippets.

Related issue

Issue #256

Changes

  1. Database & Core Logic (PHP)
    • Schema: Added a locked (TINYINT) column to the snippets database tables in DB::create_table.
    • Data Model: Updated the Snippet class and Data_Item logic to support the new property.
    • Enforcement: Modified save_snippet, trash_snippet, and delete_snippet in snippet-ops.php to intercept and block modification or deletion requests if the snippet's lock status is active.
  2. REST API
    • Controller: Updated Snippets_REST_Controller to include locked in the JSON schema, allowing the React frontend to read and persist the lock state.
    • Fetching: Modified the DB fetch queries to ensure the locked property is returned in collection and single-item requests.
  3. Frontend Editor (React & TypeScript)
    • State Management: Updated useSnippetForm to automatically set the editor to isReadOnly mode when a snippet is locked.
    • Race Condition Fix: Enhanced useSubmitSnippet to accept a snippet override, ensuring that immediate toggles (like the Lock switch) send the most recent data to the server without waiting for a re-render.
    • Components:
      • Created LockControl.tsx for the sidebar.
      • Updated DeleteButton.tsx to disable itself when the lock is active.
        Integrated the toggle into the EditorSidebar.
  4. List Table UI
    • Visibility: Added a dedicated "Locked" column immediately following the snippet name.
    • Icons: Implemented a "Tick" (dashicons-yes) for locked items and a "Dimmed X" (dashicons-no-alt) for unlocked items.
    • Row Actions: Dynamically hidden the "Trash" action link in the list table for any snippet that is currently locked.

Screenshots

  • Showcase of locked column:

  • Showcase of the editor of a locked script:

How to Test

  1. Navigate to Snippets > Settings > Debug and click Upgrade Database Table.
  2. Edit an existing snippet and toggle the Lock Snippet switch in the sidebar to ON.
  3. Verify that the code editor and title field become uneditable.
  4. Verify that the Delete button in the sidebar is disabled.
  5. Return to the All Snippets list and verify the new Locked column shows a blue tick.
  6. Hover over the locked snippet in the list and confirm the Trash link is missing.
  7. Unlock the snippet and verify all fields and delete actions are restored.

@mgiannopoulos24
Copy link
Contributor Author

mgiannopoulos24 commented Dec 19, 2025

@imantsk Should I change the branch I make the PRs on, in future PRs? I saw yesterday that after getting merged to core branch, it got reverted.

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.

2 participants