Skip to content

Port the wp/templates-query ability from WordPress/ai PR #1309 - #32

Open
yaman-694 wants to merge 1 commit into
WordPress:mainfrom
yaman-694:add/templates-query-ability
Open

yaman-694 wants to merge 1 commit into
WordPress:mainfrom
yaman-694:add/templates-query-ability

Conversation

@yaman-694

Copy link
Copy Markdown

What?

Ports the read-only templates query ability from WordPress/ai#1309 into this package as wp/templates-query, as requested in WordPress/ai#1309 (comment).
The upstream PR closes WordPress/ai#1303.

The ability incubates under the experimental wp/ prefix (NAME-LIFECYCLE.md).
The upstream core/templates-query name is not carried, and there is no alias.

Why?

An agent that wants to change how a page looks needs to know first whether the theme supports block templates, which templates exist, and which ones a user has already edited.
Today the only way to find out is to try.

How?

  • src/Templates/Templates_Query.php on Abstract_Core_Ability.
    It takes no input (an empty object is accepted) and returns { block_theme, theme, templates }.
  • The templates come from get_block_templates() (wp_template only, not template parts), so they match what the Site Editor shows.
    Each has slug, title, source (theme, custom or plugin) and has_theme_file, which tells an edited theme template from one a user created.
    They are sorted by slug, because get_block_templates() lists edited templates before theme files.
  • source is an open string, not an enum: it comes from core and from filters, and an unexpected value should not fail the whole response.
  • The input schema has no empty properties key, because PHP encodes it as the JSON array [], which the @wordpress/abilities validator rejects before the ability runs.
    A test pins this.
  • Authorization: required_capabilities is edit_theme_options, the capability the Site Editor needs, with no permission callback.
  • Meta: readonly: true, destructive: false, idempotent: true and open_world: false, plus show_in_rest: true and mcp.public: true, as on the other wp/ read abilities.
  • Manifest: tier experimental, category site, core_since: null, and proposal pointing to the upstream PR.
  • Not ported: the upstream Custom Abilities experiment gate and the stand-down when a copy is already registered, since the Registrar owns registration here.

Differences from the upstream PR

Only the name, the open_world: false annotation, and mcp.public in place of public.
On a local site I loaded the upstream class next to the port and compared them:
label, description, category, both schemas, permissions and output were identical for administrator, editor, subscriber and logged-out users, for no input, {}, unknown keys and non-object input, on a block theme with theme, edited and user-created templates and on a classic theme.
Over the REST run route and the MCP adapter, the responses differ only by the ability name inside error messages.

Use of AI tools

Testing Instructions

  1. Load the package and call WordPress\Abilities\Registrar::register_all(); (for example with tools/wordpress-abilities-bridge).
  2. As an administrator on a block theme such as Twenty Twenty-Five, run wp/templates-query (GET /wp-json/wp-abilities/v1/abilities/wp/templates-query/run): block_theme is true and the theme's templates have source: "theme".
  3. Edit a template in the Site Editor (for example Single Posts) and run it again: it has source: "custom" and has_theme_file: true.
  4. Create a new template for one page and run it again: it has source: "custom" and has_theme_file: false.
  5. Switch to a classic theme: block_theme is false and templates is empty.
  6. As an editor, the ability is refused (403), and a POST to the run route is rejected (405).

Automated:

  • tests/Abilities/TemplatesQueryTest.php: 13 tests, using a block and a classic fixture theme in tests/Fixtures/themes.
  • The full suite passes on both wp-env environments (stable 7.1.5 and trunk), 301 tests each.
    The one warning (Abstract_Media_Write.php:977) and the skipped tests come from the existing media and settings tests.
  • composer run lint:phpcs, composer run lint:phpstan, composer run verify:manifest and composer run verify:coreify pass.

Changelog entry

Added - wp/templates-query, tier experimental, a port of the templates query ability from WordPress/ai PR #1309: reports whether the active theme is a block theme and lists its block templates, with where each one comes from and whether the theme ships it as a file.

Adds the read-only `wp/templates-query` ability. It reports whether the
active theme is a block theme, its name, and its block templates sorted
by slug, each with where it comes from and whether the theme ships it
as a file. It requires `edit_theme_options`.

The ability incubates under the experimental `wp/` prefix; the upstream
`core/templates-query` name is not carried.
@yaman-694
yaman-694 requested a review from a team as a code owner November 23, 2026 14:30
@github-actions

github-actions Bot commented Nov 23, 2026 •

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @nikolas4175-godaddy.

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

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: nikolas4175-godaddy.

Co-authored-by: yaman-694 <yamanzyan@git.wordpress.org>
Co-authored-by: nikmclaughlin <nikskyverge@git.wordpress.org>
Co-authored-by: dkotter <dkotter@git.wordpress.org>
Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>
Co-authored-by: whyisjake <whyisjake@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@nikmclaughlin

Copy link
Copy Markdown
Contributor

Thanks @yaman-694 for putting this together! I'm working on an update following discussion in #18 to rename the experimental tier of abilities under the core-experimental/ namespace. This will need to follow that as well, so you can feel free to change it out now, or I'll let you know when the rest of the repo has moved over.

@nikmclaughlin

Copy link
Copy Markdown
Contributor

FYI: @yaman-694 Experimental tier now lives under the core-experimental/ namespace, as of #33

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.

Add a read ability for block templates and whether the active theme supports them

2 participants