Add annotations REST API using custom post type. #4385
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
A REST API for annotations using a custom post type (take 2).
This PR is an alternative to annotations as comment types in #4386
API Documentation
See: https://speca.io/jaswrks/wp-annotation-posts
Conforms to the W3C annotation data model for annotation selectors and maintains some parity with the W3C annotation object model and protocol — while still doing things the WordPress way, which maximizes compatibility with WP REST API utilities, including those in JavaScript.
Tip: Review annotation creation examples to see how W3C annotation selectors work.
In the future, we could add an additional API controller that supports the JSON-LD annotation protocol, and simply map those requests to the official WP REST API internally; e.g.,
rest_do_request()
. I've been over that protocol carefully. The API presented in this PR collects enough information, in the right way, to eventually support it if we'd like to.For now, my feeling is that the JSON-LD protocol for annotations is still very young, and so are annotations in WordPress. From a practical standpoint, I suggest that we make the JSON-LD protocol a secondary objective once Gutenberg annotations have matured somewhat; i.e., to maximize compatibility with other annotation clients following the W3C.
How Has This Been Tested?
Types of changes
My initial work on an API for annotations began in #3807. Since then, the main change has been to consider the W3C Annotation Data model. See note above regarding W3C conformity/parity.
There has also been some refactoring, code cleanup, docBlock, and unit test enhancement.
Checklist:
Annotations as a Custom Post Type
Pros and cons. Can you think of any others?
Post Type Pros
Leverages powerful extensibility features provided by WP core; e.g.,
register_post_type()
and all that comes with a custom post type. So everything from the post type, to the capabilities, to statuses. All of that is well-supported by core and many plugins.Given post type extensibility, with this approach it seems less likely we'll run into technical roadblocks in the future. For example, what if annotations mature into something that requires additional features or functionality that's only feasible to develop in a registered post type?
Post Type Cons
Post semantics are not as close to those of annotations. An annotation is more like a comment than anything else. It would be awesome if there was a
register_comment_type()
function supported by core. https://core.trac.wordpress.org/ticket/25674Given it's a 'post' type, we don't get to easily inherit functionality and semantics associated with comments. Such as spam-checking, flood-checking, content filters, author handling, notifications, etc. Therefore, adding an entirely new post type for annotations (much like comments) will require just a little more work if front-end annotations become a thing.
Back-End Annotation Permissions
Simplified Explanation
If you can
edit_posts
, and you canedit_post
(this post). Or, if you're the post author. Then you can read all, and create, edit, delete your own back-end annotations in this post.Administrators and Editors can edit and delete any post, so they can read, edit, and delete any annotation without restriction.
Subscribers and the public have no access to back-end annotations whatsoever.
Front-End Annotation Permissions
Simplified Explanation
Almost exactly the same as comments. They adhere to the same rules with respect to comments being open, or not, and anyone who can
moderate_comments
can moderate front-end annotations.Note: As a security precaution, front-end annotations are disabled internally for now. At this time, the main focus is on back-end annotations. Front-end annotations are simply being considered for the purpose of optimizing our approach and the object model.
TODO