Skip to content

fix(166): expose SCF fields as inline-token (Bits) source for RichText#519

Open
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:feature/166-scf-field-bits
Open

fix(166): expose SCF fields as inline-token (Bits) source for RichText#519
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:feature/166-scf-field-bits

Conversation

@faisalahammad

Copy link
Copy Markdown

Summary

Adds an scf/field inline-token ("Bit") source so SCF field values can be inserted mid-string inside paragraph, heading, and list-item blocks. Bits are persisted as <span class="scf-field-bit" data-bit="...">fallback</span> and resolved at render time via a render_block filter (priority 100) using get_field(). Compatible with existing acf/field block bindings (different namespace, different layer).

Designed around the only stable community API for Bits today (PRC prc-block-bit). When WP/Gutenberg lands core Bits, the walker is a single-class regex-tractable filter; migration path is detection in the same filter.

Closes #166

What's in this PR

PHP (new):

  • includes/Bits/Registry.php — singleton registry. Auto-registers scf/field with attribute schema (key, target, fallback, format: text|html). Public register()/unregister(); fires scf/bits/register action at init priority 11 for third-party bits.
  • includes/Bits/Walker.phprender_block filter. Early-exits if no scf-field-bit present; uses Tag Processor with a regex fallback for older WP. Text format -> esc_html; HTML -> wp_kses_post; empty/missing -> fallback. Deactivated SCF -> fallback visible.
  • includes/Bits/Editor.php — enqueues editor script on WP 6.5+; localizes field list via window.scfFieldBits.

JS (new):

  • assets/src/js/bits/index.js and toolbar.js — toolbar button on paragraph/heading/list-item via BlockControls, picker modal, persists the span shape.

Bootstrap + wiring (modified):

  • secure-custom-fields.php — instantiates Walker and Editor; boots Registry on init priority 11.
  • includes/assets.php — registers scf-field-bits script.
  • composer.json — PSR-4 mapping for SCF\Bits\.
  • webpack.config.js — adds js/scf-field-bits entry.

Tests:

  • tests/php/includes/bits/Test_Bits_Registry.php — 7 tests (defaults, idempotent boot, validation, register/unregister, filtering).
  • tests/php/includes/bits/Test_Bits_Walker.php — 8 tests (early exit, substitution, fallback, html kses, raw cap, script strip).

How to test

  1. Install the build at scf-bits-166.zip (branch commit also fine via composer install && npm run build).
  2. Add a SCF Text field "forecast_today".
  3. In a paragraph block, click the new SCF field toolbar button; pick forecast_today / text / fallback "sunny"; Insert.
  4. Save the post with field value "cloudy with rain"; the paragraph renders "cloudy with rain".
  5. Empty the field; the rendered span falls back to "sunny".
  6. Deactivate the plugin; the post still shows the fallback text (no error).

Full 6-scenario manual pass is in TESTING_INSTRUCTIONS.md (untracked).

Verification

  • PHPUnit: 2845 passing, 15 new (7 + 8) bits tests passing.
  • PHPCS clean on includes/Bits/ and tests/php/includes/bits/.
  • JS lint (eslint + prettier) clean.
  • Webpack builds scf-field-bits bundle without errors.

Use of AI Tools

Used standard editor tooling to read documentation, draft commit messages, and structure the PR — final code was reviewed and tested manually. The repository maintainers and reviewing engineers should consider this PR's contents as written by a human contributor.

Expose SCF fields as a source for inline tokens (Bits) that can be
inserted mid-string inside paragraph, heading, and list-item blocks.

- New includes/Bits/Registry.php singleton with built-in scf/field
  source; register()/unregister() and an scf/bits/register action
  hook for third-party bits.
- New includes/Bits/Walker.php render_block filter (priority 100)
  resolves persisted span shapes via get_field() and replaces inner
  HTML; Tag Processor path with regex fallback for older WP.
- New includes/Bits/Editor.php enqueues the RTE picker script for
  WP 6.5+ and localizes the field list via window.scfFieldBits.
- New assets/src/js/bits/ entry adds an SCF field button to the
  BlockControls of paragraph/heading/list-item with a picker modal.
- PHPUnit tests for Registry (7) and Walker (8).

Fixes WordPress#166
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @[email protected].

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

Core Committers: Use this line as a base for the props when committing in SVN:

Props priethor.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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.

Expose Custom Fields as a source for inline tokens

1 participant