Skip to content

[in progress] Abilities API: Add a core/content ability#12195

Draft
jorgefilipecosta wants to merge 2 commits into
WordPress:trunkfrom
jorgefilipecosta:add/core-content-ability
Draft

[in progress] Abilities API: Add a core/content ability#12195
jorgefilipecosta wants to merge 2 commits into
WordPress:trunkfrom
jorgefilipecosta:add/core-content-ability

Conversation

@jorgefilipecosta

Copy link
Copy Markdown
Member

Summary

Adds a read-only core/content ability that retrieves one or more posts of a post type exposed to abilities. It is a sibling of the core/settings ability (#12141) and follows the same structure.

A new show_in_abilities post type argument controls exposure (enabled by default for post and page). Only post types with a truthy show_in_abilities are queryable through the ability.

Behavior

  • Fetch one post by id, or by slug (slug requires post_type, since slugs are not unique across types).
  • Query many by post_type (required unless id is given), filtered by status, author, parent, with page/per_page.
  • fields selects which fields each post returns. Per-post fields: always id, type, status, date, modified, slug, link; and, when the post type supports them, title, excerpt, raw_content, author {id, display_name}, parent.
  • Output: { posts: [...], total, total_pages }.

Security

Follows the REST posts model, with defense in depth:

  • A coarse status/capability gate (the post type's read / edit_posts / read_private_posts caps), plus an authoritative per-post read_post check on every returned row (author/status scoped).
  • Default status is publish; non-public statuses require the appropriate capabilities.
  • Password-protected content (raw_content/excerpt) is withheld from users who cannot edit the post.
  • A single, uniform not-found response for missing / unexposed / mismatched / unreadable posts, to avoid leaking the existence of posts.
  • Protected/private title prefixes are stripped for machine consumers.

Pagination

The ability accepts page/per_page and returns total/total_pages in the body. A companion PR adds standard X-WP-Total / X-WP-TotalPages response headers at the REST run-controller level; this ability works with or without that change (the totals are always in the body).

Tests

tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php (direct execute/permission coverage) and tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php (REST dispatch), covering schema, retrieval, the field selector, pagination totals, and the full permission/visibility matrix (status gating, author scoping, private posts, password withholding, anti-enumeration).

Adds a read-only `core/content` ability that retrieves one or more posts of a
post type exposed to abilities via a new `show_in_abilities` post type argument
(enabled for `post` and `page` by default). Fetch a single post by ID or by
slug, or query multiple posts filtered by post type, status, author, or parent,
selecting a support-aware set of fields per post.

Permissions follow the REST posts model: a coarse status/capability gate plus an
authoritative per-post read_post check, with password-protected content withheld
from users who cannot edit the post and a uniform not-found response to avoid
leaking the existence of posts.
@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Mirrors the refinements from the core/settings review that also apply to core/content:
- Memoize the exposed post types so the input schema and the permission/execute
  callbacks derive from a single walk of the registered post types.
- Default the input schema to an empty object so the type:object default serializes as {}.
- Harden input/value handling (type guards, a capability resolver, and a non-negative
  integer helper) against loosely-typed request data.
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