Skip to content

Port the term write abilities (wp/term-create, wp/term-update, wp/term-delete) from WordPress/ai PR #1103 - #11

Merged
nikmclaughlin merged 2 commits into
mainfrom
add/term-writes
Oct 30, 2026
Merged

nikmclaughlin merged 2 commits into
mainfrom
add/term-writes

Conversation

@nikmclaughlin

Copy link
Copy Markdown
Contributor

What?

Adds the three term write abilities to the package: wp/term-create, wp/term-update, and wp/term-delete — a faithful port of WordPress/ai PR #1103 ("Add core/term-create, core/term-update, and core/term-delete abilities", author @jorgefilipecosta), incubating under the experimental tier's wp/ prefix (D5; D7: no aliases). Builds on the merged wp/terms-query (PR #10).

Why?

Agents can read taxonomy terms but not manage them. This port gives consumers one canonical copy while the upstream proposal settles (plan item B6, part 2 — including the upstream's third ability, term-delete, which this port carries in full).

How?

  • src/Terms/Abstract_Term_Write.php (shared base) + Term_Create.php / Term_Update.php / Term_Delete.php. Terms_Query is a hand-authored wp/* class, so the established media sharing pattern applies: helpers (fields projection, term lookup, item schema, exposure check) extracted verbatim with private → protected at the copy site; Terms_Query itself untouched.
  • Contracts: create takes a taxonomy and a name, plus optional slug, description, and parent (hierarchical taxonomies only). Update takes the term's id and the values to change. Delete takes the term's id and force, which must be true — terms cannot be trashed, so deletion is permanent. On update and delete, taxonomy is an optional guard. The term comes back in the shape wp/terms-query uses, limited by fields; a delete returns { deleted, previous }.
  • Floors: creating needs edit_terms in a hierarchical taxonomy or assign_terms in a flat one; updating needs edit_term; deleting needs delete_term. A missing term, or one outside the exposed taxonomies or the guard, is denied like a missing capability.
  • The default category is not protected by any code of its own (as upstream): core's map_meta_cap() answers do_not_allow for delete_term against the term named by the default_{$taxonomy} option, and wp_delete_term() returns 0 for it — the denial surfaces through the ported permission path. No bespoke protection logic added, faithfully.
  • Not ported from upstream: the single Terms registration class (this package's Registrar owns registration), core/ names, gating and unregister-core-copy wiring (D5/D7), REST run-endpoint tests and the JS e2e spec (this repo pins the contract at WP_Ability::execute() + direct calls), and multisite-specific assertions (single-site wp-env).
  • Manifest: three entries, tier experimental, core_since: null, proposal → the upstream PR; annotations ported exactly (create/update POST-served, delete DELETE-served).

Use of AI tools

Tool(s): goose (agent). Model(s): provided via the harness. Used for: implementation, tests, and docs. Reviewed and edited by the committer.

Testing Instructions

  1. Load the package in a wp_abilities_api_init callback: ( new WordPress\Abilities\Registrar )->register_all();
  2. Run wp/term-create with { "taxonomy": "post_tag", "name": "Example" } → the term through the terms-query projection; wp/term-update with { "id": <id>, "name": "Renamed" }; wp/term-delete with { "id": <id>, "force": true } → { deleted: true, previous: … }. The default category delete is refused; force missing/false is rejected; taxonomy-guard mismatches are denied.
  3. Floors: editors create/update/delete categories (edit_terms/edit_term/delete_term); creating a tag needs only assign_terms; below-floor roles denied per gate.
  4. Gates: composer run lint:phpcs && composer run lint:phpstan && composer run verify:manifest && composer run verify:coreify, and the PHPUnit suite on both wp-env environments (trunk + stable, one at a time) pass.

Changelog entries

Added (CHANGELOG.md): wp/term-create, wp/term-update, wp/term-delete — tier experimental, faithful ports of the term write abilities from WordPress/ai PR #1103 by @jorgefilipecosta, answering through the wp/terms-query field projection.

Co-authored-by: jorgefilipecosta jorgefilipecosta@users.noreply.github.com

nikmclaughlin and others added 2 commits October 30, 2026 00:06
…lities from WordPress/ai PR #1103

## What?

Adds the three term write abilities to the package: a faithful port of
WordPress/ai PR #1103 ("Add `core/term-create`, `core/term-update`, and
`core/term-delete` abilities", author @jorgefilipecosta), incubating under the
experimental tier's `wp/` prefix on the shared `content` category and field
projection of `wp/terms-query` (PR #10). The PR's `core/term-*` registration
names, gating, and unregister-core-copy wiring are not copied; no aliases
exist (D7).

## Why?

Agents can read terms through abilities (`wp/terms-query`) but not write them.
This package ports the three writes so consumers get one canonical copy while
the upstream proposal settles.

## How?

- **The abilities:** `src/Terms/Abstract_Term_Write.php` (shared base) +
  `Term_Create.php` / `Term_Update.php` / `Term_Delete.php` mirror the PR's
  logic — labels, descriptions, schemas, annotations (create POST semantics,
  update POST with destructive=true idempotent=false, delete DELETE with
  destructive=true idempotent=true), error codes, and the guard semantics.
  Create takes a `taxonomy` and a `name`, plus optional `slug`,
  `description`, and `parent` (hierarchical only); update takes `id` and the
  values to change; delete takes `id` and `force`, which must be true because
  terms cannot be trashed (non-forced answers `terms_trash_not_supported`,
  501). On update and delete, `taxonomy` is an optional guard.
- **The floors:** creating needs `edit_terms` in a hierarchical taxonomy or
  `assign_terms` in a flat one; updating needs `edit_term`; deleting needs
  `delete_term`. A missing term, one outside the exposed taxonomies, or one
  outside the guard is denied like a missing capability — a generic denial
  through the validated transport, `terms_term_invalid` (404) on a direct
  call; a malformed `id` or non-string guard fails closed, never cast onto
  another term.
- **The default category is not protected by any code of its own** (as upstream): core's `map_meta_cap()` answers `do_not_allow` for `delete_term`
  against the term named by the `default_{$taxonomy}` option, and
  `wp_delete_term()` returns 0 for it — the denial surfaces as the generic
  transport denial, `terms_cannot_delete` (403) on a direct call, and
  `terms_cannot_delete` (500) when a filter grants the capability anyway.
- **Answers through the query's contract:** written terms return through the
  `wp/terms-query` projection (`fields` picking the subset, the ID always
  included, `parent` only for hierarchical taxonomies); a delete returns
  `{ deleted, previous }`.
- **Code sharing (media pattern):** `Terms_Query` is a hand-authored `wp/*`
  class, so its shared helpers (`fields` projection, term lookup, item
  schema) are extracted verbatim from this package's PR #10 port with
  `private` relaxed to `protected`, into `Abstract_Term_Write`; `Terms_Query`
  itself keeps its byte-stable backport shape (verify:coreify holds).
- **Companions:** the taxonomy opt-in machinery the writes ride (`show_in_abilities`
  on WP_Taxonomy, polyfilled pre-7.2) and the projection both came from PR
  #10; the writes need nothing beyond riding them. Manifest entries declare
  the `taxonomy_show_in_abilities` support feature, as the query does.

Co-authored-by: jorgefilipecosta <jorgefilipecosta@users.noreply.github.com>
…n contract

## What?

A test-only trunk-drift fix for the settings-update suite (no `src/` change).
Core r65044 (#14882, #66383) added `WP_Ability::coerce_input()`, which the
Abilities API now runs after validation and before the permission check and
execute callback. The `uniqueItems` fixture `('1', '01')` — unique as strings,
colliding after the cast — is refused by that layer with `ability_invalid_input`
before the plugin code runs, so
`SettingsUpdateTest::test_a_value_sanitizing_makes_invalid_is_refused`, which
pinned the former contract (`settings_invalid_param` from the plugin), failed
deterministically against the trunk nightly (CI run 41034270103).

## How?

- The colliding row (`test_a_value_sanitizing_makes_invalid_is_refused`) now
  pins the framework contract where the coercion layer exists (the exact fixture
  core's own `data_input_coercion_errors` regression test pins):
  `ability_invalid_input`, no error data (refusal came from the framework, the
  plugin's execute callback never ran), nothing written. Hosts without the
  layer (7.1-, the stable matrix job) keep the plugin's `settings_invalid_param`
  mapping assertions.
- A distinct-after-coercion row (`test_a_list_unique_after_coercion_still_updates`)
  keeps the callback-reachable list coverage: items casting to distinct integers
  ('1' and '02') pass validation and coercion, and the coerced list is what the
  answer carries and what gets stored.
- The mapping row (`test_the_plugin_sanitize_refusal_maps_to_settings_invalid_param`)
  covers the plugin's unchanged `settings_invalid_param` semantics (400, the
  named param, the all-or-nothing order) directly on the plugin's execute path,
  as the companion read row already does, since `execute()` can no longer reach
  the callback with the colliding fixture on coercion hosts.
- The `su_list_setting` registration the three rows share moved to
  `register_unique_list_setting()`.

Verified: green against the current trunk nightly (post-r65044) and stable
(7.1.4), one wp-env at a time; `phpcs` and `phpstan` clean.
@nikmclaughlin

Copy link
Copy Markdown
Contributor Author

Note on the second commit (f411a4d) — trunk-drift test-only fix:

Core trunk r65044 (wordpress-develop #14882) added schema-aware input coercion (WP_Ability::coerce_input()) to the Abilities API execute pipeline — running after schema validation but before permission checks and the ability's callback. This changed an observable contract: inputs whose values collide after type-casting (e.g. ('1', '01') under uniqueItems: true) are now refused by the API layer with ability_invalid_input, and the ability's callbacks never run.

That made one settings-update test (from #8, carried here via the branch base) fail against the new nightly — deterministically, on any commit after r65044, while remaining green on stable (its Abilities API comes from the bridge shim, pre-r65044). No code fix was warranted: the port's settings_invalid_param behavior is unchanged and correct for inputs that reach it.

The fix is test-only, in two parts:

  1. The unique-after-cast case is re-pinned to the new API contract (ability_invalid_input, callbacks-never-ran assertion) — conditionally per environment, so it stays green on both trunk and stable.
  2. A new direct-to-callback fixture (the class's established reflection pattern) keeps the port's own settings_invalid_param mapping covered on both environments, since the colliding fixture can no longer reach the callback on trunk.

Verified locally against the current nightly (trunk @ 072110f4, post-r65044, class file byte-identical to the r65044 change): SettingsUpdate class green on both envs (16 tests / 84 assertions trunk, 16 / 85 stable). Full diagnosis: state/selftest-evidence/pr11-run41034270103-trunk-ci-diagnosis.md (ops layer, not committed).

@nikmclaughlin
nikmclaughlin merged commit 8290c03 into main Oct 30, 2026
12 checks passed
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.

1 participant