Skip to content

Read icon sets from a directory outside the plugin first - #210

Open
Contributolo wants to merge 1 commit into
inline0:mainfrom
Contributolo:feat/icon-sets-content-dir
Open

Contributolo wants to merge 1 commit into
inline0:mainfrom
Contributolo:feat/icon-sets-content-dir

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Refs #8. This is "release A" from my last comment there: the step that changes nothing visible and makes it possible to drop the 44 icon sets from the ZIP later (13.4 MB to about 7.6 MB) without an existing site losing an icon.

What changes

  • New Blockstudio\Icons class with one lookup for both readers. Rest::icons() and bs_render_icon() each built the path to includes/icons themselves. Both now call Icons::set_path(), which looks in wp-content/blockstudio/icons first and in the plugin folder second. Name validation is still Utils::icon_set_path(), for both directories.
  • On activation and after each update, Migrate calls Icons::store_bundled_sets(). It copies the bundled sets to that directory, skips sets that are already stored with the same content, and writes each file aside before swapping it in, so a reader never sees half a set. It is not tied to a version key, so it runs for whichever release this lands in.
  • New filter blockstudio/icons/dir. It moves the directory, and an empty string stores nothing and keeps reading from the plugin folder only.

Nothing is removed. A site where the copy fails (read-only wp-content, for example) keeps working from the bundled sets exactly as today. The stored value {set, subSet, icon}, the REST route and the picker are unchanged, and there is no network request.

Cost

The copy takes about 22 MB of disk in wp-content and runs once per update in the request that triggers the migration. Later runs only hash the files. A site that uses no icon fields can opt out with add_filter('blockstudio/icons/dir', '__return_empty_string');.

What a follow-up would do

Not part of this PR, and only if you want to go that way: stop shipping includes/icons, publish the sets as release assets, and fetch a missing set into the same directory when it is first picked. The lookup added here already resolves a set that exists only in the stored directory (covered by a test), so that step would not touch rendering again.

Tests

tests/unit/icons/IconStoreTest.php: fallback to the bundled set, full copy with identical content and no leftover temporary files, repeated runs writing only missing or changed sets, stored set winning for bs_icon() and the REST route, a set that is only stored, traversal out of the stored directory, the empty-directory opt-out, and the copy on update and on first activation.

Run locally in WordPress Playground: tests/unit/icons (66 tests), tests/unit/migrate (40) and tests/unit/rest (25) pass. PHPCS and PHPStan are clean on the changed files, npm run docs:check passes. I did not run the full suites, E2E, or the tests of the PHPStan package. blockstudio/icons/dir is added to the hook list in HookCallbackRule. I left the changelog and the bundled LLM files to the release.

Happy to change the directory, the filter name, or to make the copy opt-in instead of opt-out if you prefer that.

The icon picker and bs_render_icon() share one lookup: a set stored in wp-content/blockstudio/icons wins, the bundled set is the fallback. Activation and updates copy the bundled sets there, so a later release can stop shipping them without existing sites losing an icon. The blockstudio/icons/dir filter moves the directory, and an empty string keeps the sets in the plugin only.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant